Keep a set of preferences in the midend.

This commit introduces a serialisation format for the user preferences
stored in game_ui, using the keyword identifiers that get_prefs is
required to write into its list of config_item. As a result, the
serialisation format looks enough like an ordinary config file that a
user could write one by hand.

The preferences for the game backend are kept in serialised form in
me->be_prefs. The typical use of this is to apply it to a just-created
game_ui by calling midend_apply_prefs(), which deserialises the prefs
buffer into a list of config_item and passes it to the backend's
set_prefs function, overwriting the preference fields (but no others)
of the game_ui.

This is duly done when creating a new game, when loading a game from a
save file, and also when printing a puzzle. To make the latter work,
document_add_puzzle now takes a game_ui (and keeps ownership of it
afterwards), and passes that to the backend's compute_size and print
functions.

The backend's own get_prefs and set_prefs functions are wrapped by
midend_get_prefs and midend_set_prefs. This is partly as a convenience
(it deals with optionally constructing a game_ui specially to call the
backend with), but mostly so that there will be a convenient place in
the midend to add standard preferences applying across all puzzles.
No cross-puzzle preferences are provided yet.

There are two external interfaces to all this, and in this commit,
neither one is yet called by any frontend:

A new pair of midend functions is exposed to the front end, called
midend_load_prefs and midend_save_prefs. These have a similar API to
midend_serialise and midend_deserialise, taking a read/write function
pointer and a context. So front ends that can already load/save a game
to a file on disk should find it easy to add a similar set of
functions loading/saving user preferences.

Secondly, a new value CFG_PREFS is added to the enumeration of
configuration dialog types, alongside the ones for the Custom game
type, entering a game description and entering a random seed. This
should make it easy for frontends to offer a Preferences dialog,
because it will operate almost exactly like three dialogs they already
handle.
This commit is contained in:
Simon Tatham
2023-04-22 12:54:11 +01:00
parent ea6be8f0af
commit bb1ab36108
6 changed files with 403 additions and 20 deletions

View File

@ -3542,7 +3542,11 @@ viewing the existing one). The mid-end generates this dialog box
description itself. This should be used when the user selects
\q{Random Seed} from the game menu (or equivalent).
(A fourth value \cw{CFG_FRONTEND_SPECIFIC} is provided in this
\dt \cw{CFG_PREFS}
\dd Requests a box suitable for configuring user preferences.
(An additional value \cw{CFG_FRONTEND_SPECIFIC} is provided in this
enumeration, so that frontends can extend it for their own internal
use. For example, you might wrap this function with a
\cw{frontend_get_config} which handles some values of \c{which} itself
@ -3796,6 +3800,32 @@ application is a monolithic one containing all the puzzles. See
identify a save file before you instantiate your mid-end in the first
place.
\H{midend-save-prefs} \cw{midend_save_prefs()}
\c void midend_save_prefs(
\c midend *me, void (*write)(void *ctx, const void *buf, int len),
\c void *wctx);
Calling this function causes the mid-end to write out the states of
all user-settable preference options, including its own cross-platform
preferences and ones exported by a particular game via
\cw{get_prefs()} and \cw{set_prefs()} (\k{backend-get-prefs},
\k{backend-set-prefs}). The output is a textual format suitable for
writing into a configuration file on disk.
The \c{write} and \c{wctx} parameters have the same semantics as for
\cw{midend_serialise()} (\k{midend-serialise}).
\H{midend-load-prefs} \cw{midend_load_prefs()}
\c const char *midend_load_prefs(
\c midend *me, bool (*read)(void *ctx, void *buf, int len),
\c void *rctx);
This function is used to load a configuration file in the same format
emitted by \cw{midend_save_prefs()}, and import all the preferences
described in the file into the current mid-end.
\H{identify-game} \cw{identify_game()}
\c const char *identify_game(char **name,