chiark / gitweb /
Fix assertion failure if you Undo right at startup.
authorSimon Tatham <anakin@pobox.com>
Fri, 6 Oct 2017 18:49:05 +0000 (19:49 +0100)
committerSimon Tatham <anakin@pobox.com>
Fri, 6 Oct 2017 18:49:05 +0000 (19:49 +0100)
The initial call to midend_new_game() was creating a partial
serialisation containing no game states at all, which meant that if
your first UI action was an undo operation, the game would try to
deserialise that and complain that it was incomplete. Now we detect
that in advance and don't create a useless serialisation in the first
place.

midend.c

index 1a65121d9c03381a55630804cc76bfb8341e2102..b5578065f3183ccb2fed6a5abb6d254e7ea092d3 100644 (file)
--- a/midend.c
+++ b/midend.c
@@ -403,7 +403,20 @@ static void newgame_serialise_write(void *ctx, const void *buf, int len)
 void midend_new_game(midend *me)
 {
     me->newgame_undo_len = 0;
-    midend_serialise(me, newgame_serialise_write, me);
+    if (me->nstates != 0) {
+        /*
+         * Serialise the whole of the game that we're about to
+         * supersede, so that the 'New Game' action can be undone
+         * later. But if nstates == 0, that means there _isn't_ a
+         * current game (not even a starting position), because this
+         * is the initial call to midend_new_game when the midend is
+         * first set up; in that situation, we want to avoid writing
+         * out any serialisation, because it would be useless anyway
+         * and just confuse us into thinking we had something to undo
+         * to.
+         */
+        midend_serialise(me, newgame_serialise_write, me);
+    }
 
     midend_stop_anim(me);
     midend_free_game(me);