shithub: puzzles

Download patch

ref: 9be7db547aa2eba68492dc3326ea36ebeeb63505
parent: 418cb3a5671404d2d91bf221887df2be2ae2654f
author: Ben Harris <bjh21@bjh21.me.uk>
date: Mon Feb 13 18:22:59 EST 2023

Add a game_state argument to decode_ui()

Some games would like a way to check that the parameters in the encoded
UI string are consistent with the game parameters.  Since this might
depend on the current state of the game (this being what changed_state()
is for), implement this by adding a game_state parameter to decode_ui().
Nothing currently uses it, though Guess usefully could.

--- a/blackbox.c
+++ b/blackbox.c
@@ -511,7 +511,8 @@
     return dupstr(buf);
 }
 
-static void decode_ui(game_ui *ui, const char *encoding)
+static void decode_ui(game_ui *ui, const char *encoding,
+                      const game_state *state)
 {
     sscanf(encoding, "E%d", &ui->errors);
 }
--- a/devel.but
+++ b/devel.but
@@ -890,7 +890,8 @@
 
 \S{backend-decode-ui} \cw{decode_ui()}
 
-\c void (*decode_ui)(game_ui *ui, const char *encoding);
+\c void (*decode_ui)(game_ui *ui, const char *encoding,
+\c                   const game_state *state);
 
 This function parses a string previously output by \cw{encode_ui()},
 and writes the decoded data back into the freshly-created \c{game_ui}
@@ -897,7 +898,9 @@
 structure provided.  If the string is invalid, the function should do
 the best it can, which might just mean not changing the \c{game_ui}
 structure at all.  This might happen if a save file is corrupted, or
-simply from a newer version that encodes more \c{game_ui} data.
+simply from a newer version that encodes more \c{game_ui} data.  The
+current \c{game_state} is provided in case the function needs to
+refer to it for validation.
 
 Like \cw{encode_ui()}, \cw{decode_ui()} is optional.  If a back-end
 doesn't need this function it can just set the pointer to \cw{NULL}.
--- a/guess.c
+++ b/guess.c
@@ -452,7 +452,8 @@
     return sresize(ret, p - ret, char);
 }
 
-static void decode_ui(game_ui *ui, const char *encoding)
+static void decode_ui(game_ui *ui, const char *encoding,
+                      const game_state *state)
 {
     int i;
     const char *p = encoding;
--- a/inertia.c
+++ b/inertia.c
@@ -1527,7 +1527,8 @@
     return dupstr(buf);
 }
 
-static void decode_ui(game_ui *ui, const char *encoding)
+static void decode_ui(game_ui *ui, const char *encoding,
+                      const game_state *state)
 {
     int p = 0;
     sscanf(encoding, "D%d%n", &ui->deaths, &p);
--- a/midend.c
+++ b/midend.c
@@ -2542,7 +2542,8 @@
 
     data.ui = me->ourgame->new_ui(data.states[0].state);
     if (data.uistr && me->ourgame->decode_ui)
-        me->ourgame->decode_ui(data.ui, data.uistr);
+        me->ourgame->decode_ui(data.ui, data.uistr,
+                               data.states[data.statepos-1].state);
 
     /*
      * Run the externally provided check function, and abort if it
--- a/mines.c
+++ b/mines.c
@@ -2413,7 +2413,8 @@
     return dupstr(buf);
 }
 
-static void decode_ui(game_ui *ui, const char *encoding)
+static void decode_ui(game_ui *ui, const char *encoding,
+                      const game_state *state)
 {
     int p= 0;
     sscanf(encoding, "D%d%n", &ui->deaths, &p);
--- a/net.c
+++ b/net.c
@@ -2041,7 +2041,8 @@
     return dupstr(buf);
 }
 
-static void decode_ui(game_ui *ui, const char *encoding)
+static void decode_ui(game_ui *ui, const char *encoding,
+                      const game_state *state)
 {
     sscanf(encoding, "O%d,%d;C%d,%d",
 	   &ui->org_x, &ui->org_y, &ui->cx, &ui->cy);
--- a/puzzles.h
+++ b/puzzles.h
@@ -664,7 +664,8 @@
     game_ui *(*new_ui)(const game_state *state);
     void (*free_ui)(game_ui *ui);
     char *(*encode_ui)(const game_ui *ui);
-    void (*decode_ui)(game_ui *ui, const char *encoding);
+    void (*decode_ui)(game_ui *ui, const char *encoding,
+                      const game_state *state);
     key_label *(*request_keys)(const game_params *params, int *nkeys);
     void (*changed_state)(game_ui *ui, const game_state *oldstate,
                           const game_state *newstate);