save/restore changes - part 3

This is the third of a series of savefile-related changes.

    This adds early-days experimental support for a completely optional
    'sfctool' utility (savefile conversion tool), to be able to export
    a savefile's contents into a more portable format. There are likely
    to be bugs at this stage. In this initial first-attempt, the export
    format is a very simple ascii output.

    NetHack can be built entirely, without also building this tool.
    NetHack has no dependencies on the tool.

    Attempts were made to minimize duplication of existing NetHack code.
    To achieve that, unfortunately, #ifdef SFCTOOL and #ifndef SFCTOOL
    had to be sprinkled around through some of the existing NetHack
    source code, so that it could be re-used for building the utility.

    The process for building the sfctool typically recompiles the source
    files with #define SFCTOOL and a distinct object file with SF- is
    produced.

sfctool notes:

    Universal ctags is used and required to produce the sfctool utility.

    Some targets were added to the Unix and Windows Makefiles to
    facilitate the build process.

         make sfctool

    That should build a copy in util.

    Note: At present, the Unix Makefiles do not copy sfctool over to the
          NetHack playground during 'make install' or 'make update'.
          Until that gets resolved by someone, The tool will
          have to be manually copied there by the builder/admin if
          desired.
          cp util/sfctool ~/nh/install/games/lib/nethackdir/sfctool

    Also, a separate Visual Studio sfctool.sln solution was written and
    placed in sys/windows/vs. That has has only very limited testing.

    Usage:

      i)  To convert an existing savefile to an exportascii format
          that co-resides with the savefile:

          sfctool -c savefile

          That *must* be executed on the same platform / architecture /
          data model that produced the save file in the first place.

     ii)  To unconvert an existing exportascii format export file to a
          historical format savefile that can then be used by NetHack:

          sfctool -u savefile

          That must be executed on the same target platform / architecture /
          data model that was used to build the NetHack that will
          utilize the save file that results.

     A Windows example:

          sfctool -c Fred.NetHack-saved-game

          That should result in creation of Fred.NetHack-saved-game.exportascii
          from existing savefile:
              %USERPROFILE%\AppData\Local\NetHack\3.7\Fred.NetHack-saved-game

     A Unix example:

          sfctool -c 1000wizard

          That should result in creation of 1000wizard.exportascii.gz
          from existing savefile in the playground save directory:
              1000wizard.gz

  Current Mechanics:
     1. Makefile recipe, or script uses universal ctags to produce
        util/sf.tags.

     2. util/sftags is built and executed to read util/sf.tags and
        generate: include/sfproto.h and src/sfdata.c.

     3. util/sfctool is built from the following:
        generated file compiled with -DSFCTOOL:
                    src/sfdata.c       -> sfdata.o
        existing files compiled with -DSFCTOOL:
                    util/sfctool.c     -> sfctool.o
                    util/sfexpasc.c    -> sfexpasc.o
                    src/alloc.c        -> sf-alloc.o
                    src/monst.c        -> sf-monst.o
                    src/objects.c      -> sf-objects.o
                    src/sfbase.c       -> sfbase.o
                    src/sfstruct.c     -> sfstruct.o
                    src/nhlua.c        -> sf-nhlua.o
                    util/panic.c       -> panic.o
                    src/date.c         -> sf-date.o
                    src/decl.c         -> sf-decl.o
                    src/artifact.c     -> sf-artifact.o
                    src/dungeon.c      -> sf-dungeon.o
                    src/end.c          -> sf-end.o
                    src/engrave.c      -> sf-engrave.o
                    src/cfgfiles.c     -> sf-cfgfiles.o
                    src/files.c        -> sf-files.o
                    src/light.c        -> sf-light.o
                    src/mdlib.c        -> sf-mdlib.o
                    src/mkmaze.c       -> sf-mkmaze.o
                    src/mkroom.c       -> sf-mkroom.o
                    src/o_init.c       -> sf-o_init.o
                    src/region.c       -> sf-region.o
                    src/restore.c      -> sf-restore.o
                    src/rumors.c       -> sf-rumors.o
                    src/sys.c          -> sf-sys.o
                    src/timeout.c      -> sf-timeout.o
                    src/track.c        -> sf-track.o
                    src/version.c      -> sf-version.o
                    src/worm.c         -> sf-worm.o
                    src/strutil.c      -> strutil.o
This commit is contained in:
nhmall
2025-05-25 20:38:17 -04:00
parent 82fd29c429
commit a654d08c3b
49 changed files with 7578 additions and 257 deletions

View File

@@ -35,6 +35,7 @@ staticfn char *is_config_section(char *);
staticfn boolean handle_config_section(char *);
boolean parse_config_line(char *);
staticfn char *find_optparam(const char *);
#ifndef SFCTOOL
staticfn boolean cnf_line_OPTIONS(char *);
staticfn boolean cnf_line_AUTOPICKUP_EXCEPTION(char *);
staticfn boolean cnf_line_BINDINGS(char *);
@@ -53,6 +54,7 @@ staticfn boolean cnf_line_NAME(char *);
staticfn boolean cnf_line_ROLE(char *);
staticfn boolean cnf_line_dogname(char *);
staticfn boolean cnf_line_catname(char *);
#endif /* SFCTOOL */
#ifdef SYSCF
staticfn boolean cnf_line_WIZARDS(char *);
staticfn boolean cnf_line_SHELLERS(char *);
@@ -80,12 +82,12 @@ staticfn boolean cnf_line_PANICTRACE_GDB(char *);
staticfn boolean cnf_line_GDBPATH(char *);
staticfn boolean cnf_line_GREPPATH(char *);
staticfn boolean cnf_line_CRASHREPORTURL(char *);
staticfn boolean cnf_line_SAVEFORMAT(char *);
staticfn boolean cnf_line_BONESFORMAT(char *);
staticfn boolean cnf_line_ACCESSIBILITY(char *);
staticfn boolean cnf_line_PORTABLE_DEVICE_PATHS(char *);
staticfn void parseformat(int *, char *);
#endif /* SYSCF */
#ifndef SFCTOOL
staticfn boolean cnf_line_BOULDER(char *);
staticfn boolean cnf_line_MENUCOLOR(char *);
staticfn boolean cnf_line_HILITE_STATUS(char *);
@@ -101,6 +103,7 @@ staticfn boolean cnf_line_QT_TILEWIDTH(char *);
staticfn boolean cnf_line_QT_TILEHEIGHT(char *);
staticfn boolean cnf_line_QT_FONTSIZE(char *);
staticfn boolean cnf_line_QT_COMPACT(char *);
#endif /* SFCTOOL */
struct _cnf_parser_state; /* defined below (far below...) */
staticfn void cnf_parser_init(struct _cnf_parser_state *parser);
staticfn void cnf_parser_done(struct _cnf_parser_state *parser);
@@ -109,6 +112,13 @@ staticfn void parse_conf_buf(struct _cnf_parser_state *parser,
/* next one is in extern.h; why here too? */
boolean parse_conf_str(const char *str, boolean (*proc)(char *arg));
#ifdef SFCTOOL
#ifdef wait_synch
#undef wait_synch
#endif
#define wait_synch()
#endif /* SFCTOOL */
/* ---------- BEGIN CONFIG FILE HANDLING ----------- */
/* used for messaging. Also used in options.c */
@@ -151,6 +161,8 @@ get_default_configfile(void)
const char *backward_compat_configfile = "nethack.cnf";
#endif
#ifndef SFCTOOL
/* #saveoptions - save config options into file */
int
do_write_config_file(void)
@@ -195,6 +207,7 @@ do_write_config_file(void)
}
return ECMD_OK;
}
#endif /* SFCTOOL */
/* remember the name of the file we're accessing;
if may be used in option reject messages */
@@ -455,7 +468,11 @@ choose_random_part(char *str, char sep)
nsep++;
str++;
}
#ifndef SFCTOOL
csep = rn2(nsep);
#else
csep = 1;
#endif
str = begin;
while ((csep > 0) && *str) {
str++;
@@ -571,6 +588,8 @@ find_optparam(const char *buf)
return bufp;
}
#ifndef SFCTOOL
staticfn boolean
cnf_line_OPTIONS(char *origbuf)
{
@@ -759,6 +778,7 @@ cnf_line_catname(char *bufp)
(void) strncpy(gc.catname, bufp, PL_PSIZ - 1);
return TRUE;
}
#endif /* SFCTOOL */
#ifdef SYSCF
@@ -1088,20 +1108,6 @@ cnf_line_CRASHREPORTURL(char *bufp)
return TRUE;
}
staticfn boolean
cnf_line_SAVEFORMAT(char *bufp)
{
parseformat(sysopt.saveformat, bufp);
return TRUE;
}
staticfn boolean
cnf_line_BONESFORMAT(char *bufp)
{
parseformat(sysopt.bonesformat, bufp);
return TRUE;
}
staticfn boolean
cnf_line_ACCESSIBILITY(char *bufp)
{
@@ -1135,6 +1141,8 @@ cnf_line_PORTABLE_DEVICE_PATHS(char *bufp)
}
#endif /* SYSCF */
#ifndef SFCTOOL
staticfn boolean
cnf_line_BOULDER(char *bufp)
{
@@ -1271,6 +1279,7 @@ cnf_line_QT_COMPACT(char *bufp)
#endif
return TRUE;
}
#endif /* SFCTOOL */
typedef boolean (*config_line_stmt_func)(char *);
@@ -1288,6 +1297,7 @@ static const struct match_config_line_stmt {
boolean origbuf;
config_line_stmt_func fn;
} config_line_stmt[] = {
#ifndef SFCTOOL
/* OPTIONS handled separately */
{ "OPTIONS", 4, FALSE, TRUE, cnf_line_OPTIONS },
CNFL_N(AUTOPICKUP_EXCEPTION, 5),
@@ -1309,6 +1319,7 @@ static const struct match_config_line_stmt {
CNFL_NA(CHARACTER, 4, ROLE),
CNFL_N(dogname, 3),
CNFL_N(catname, 3),
#endif /* SFCTOOL */
#ifdef SYSCF
CNFL_S(WIZARDS, 7),
CNFL_S(SHELLERS, 8),
@@ -1336,11 +1347,10 @@ static const struct match_config_line_stmt {
CNFL_S(CRASHREPORTURL, 13),
CNFL_S(GDBPATH, 7),
CNFL_S(GREPPATH, 7),
CNFL_S(SAVEFORMAT, 10),
CNFL_S(BONESFORMAT, 11),
CNFL_S(ACCESSIBILITY, 13),
CNFL_S(PORTABLE_DEVICE_PATHS, 8),
#endif /*SYSCF*/
#ifndef SFCTOOL
CNFL_N(BOULDER, 3),
CNFL_N(MENUCOLOR, 9),
CNFL_N(HILITE_STATUS, 6),
@@ -1356,6 +1366,7 @@ static const struct match_config_line_stmt {
CNFL_N(QT_TILEHEIGHT, 13),
CNFL_N(QT_FONTSIZE, 11),
CNFL_N(QT_COMPACT, 10)
#endif /* SFCTOOL */
};
#undef CNFL_N
@@ -1485,6 +1496,7 @@ config_error_nextline(const char *line)
return TRUE;
}
#ifndef SFCTOOL
int
l_get_config_errors(lua_State *L)
{
@@ -1510,6 +1522,7 @@ l_get_config_errors(lua_State *L)
return 1;
}
#endif /* SFCTOOL */
/* varargs 'config_error_add()' moved to pline.c */
void
@@ -1600,9 +1613,10 @@ read_config_file(const char *filename, int src)
if (!(fp = fopen_config_file(filename, src)))
return FALSE;
#ifndef SFCTOOL
/* begin detection of duplicate configfile options */
reset_duplicate_opt_detection();
#endif /* SFCTOOL */
free_config_sections();
iflags.parse_config_file_src = src;
@@ -1610,8 +1624,10 @@ read_config_file(const char *filename, int src)
(void) fclose(fp);
free_config_sections();
#ifndef SFCTOOL
/* turn off detection of duplicate configfile options */
reset_duplicate_opt_detection();
#endif /* SFCTOOL */
return rv;
}
@@ -1859,38 +1875,6 @@ vconfig_error_add(const char *str, va_list the_args)
}
#ifdef SYSCF
staticfn void
parseformat(int *arr, char *str)
{
const char *legal[] = { "historical", "cnv" };
int i, kwi = 0, words = 0;
char *p = str, *keywords[2] = { NULL };
while (*p) {
while (*p && isspace((uchar) *p)) {
*p = '\0';
p++;
}
if (*p) {
words++;
if (kwi < 2)
keywords[kwi++] = p;
}
while (*p && !isspace((uchar) *p))
p++;
}
if (!words) {
impossible("missing format list");
return;
}
while (--kwi >= 0)
if (kwi < 2) {
for (i = 0; i < SIZE(legal); ++i) {
if (!strcmpi(keywords[kwi], legal[i]))
arr[kwi] = i + 1;
}
}
}
#ifdef SYSCF_FILE
void
assure_syscf_file(void)
@@ -1924,8 +1908,10 @@ assure_syscf_file(void)
close(fd);
return;
}
#ifndef SFCTOOL
if (gd.deferred_showpaths)
do_deferred_showpaths(1); /* does not return */
#endif
raw_printf("Unable to open SYSCF_FILE.\n");
exit(EXIT_FAILURE);
}