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

@@ -8,6 +8,8 @@
/* #define SFLOGGING */ /* debugging */
staticfn void sfstruct_read_error(void);
/* historical full struct savings */
#ifdef SAVEFILE_DEBUGGING
@@ -29,6 +31,9 @@ staticfn void logging_finish(void);
#define SFI_BODY(dt) \
{ \
if (nhfp->eof) { \
sfstruct_read_error(); \
} \
mread(nhfp->fd, (genericptr_t) d_##dt, sizeof *d_##dt); \
if (restoreinfo.mread_flags == -1) \
nhfp->eof = TRUE; \
@@ -46,20 +51,37 @@ void historical_sfi_##dtyp(NHFILE *nhfp, dtyp *d_##dtyp,
const char *myname UNUSED) \
SFI_BODY(dtyp)
#define SFO_CBODY(dt) \
{ \
normalize_pointers_##dt(d_##dt); \
bwrite(nhfp->fd, (genericptr_t) d_##dt, sizeof *d_##dt); \
}
#define SFI_CBODY(dt) \
{ \
if (nhfp->eof) { \
sfstruct_read_error(); \
} \
mread(nhfp->fd, (genericptr_t) d_##dt, sizeof *d_##dt); \
normalize_pointers_##dt(d_##dt); \
if (restoreinfo.mread_flags == -1) \
nhfp->eof = TRUE; \
}
#define SF_C(keyw, dtyp) \
void historical_sfo_##dtyp(NHFILE *, keyw dtyp *d_##dtyp, \
const char *); \
void historical_sfi_##dtyp(NHFILE *, keyw dtyp *d_##dtyp, \
const char *); \
extern void normalize_pointers_##dtyp(keyw dtyp *d_##dtyp); \
\
void historical_sfo_##dtyp(NHFILE *nhfp, keyw dtyp *d_##dtyp, \
const char *myname UNUSED) \
SFO_BODY(dtyp) \
SFO_CBODY(dtyp) \
\
void historical_sfi_##dtyp(NHFILE *nhfp, keyw dtyp *d_##dtyp, \
const char *myname UNUSED) \
SFI_BODY(dtyp)
SFI_CBODY(dtyp)
#define SF_X(xxx, dtyp) \
void historical_sfo_##dtyp(NHFILE *, xxx *d_##dtyp, const char *, int); \
@@ -121,7 +143,7 @@ SF_C(union, any)
SF_A(aligntyp)
SF_A(boolean)
SF_A(coordxy)
SF_A(genericptr_t)
//SF_A(genericptr_t)
SF_A(int)
SF_A(int16)
SF_A(int32)
@@ -159,6 +181,32 @@ historical_sfi_char(NHFILE *nhfp, char *d_char,
if (restoreinfo.mread_flags == -1)
nhfp->eof = TRUE;
}
//extern void sfo_genericptr(NHFILE *, void **, const char *);
//extern void sfi_genericptr(NHFILE *, void **, const char *);
//extern void sfo_x_genericptr(NHFILE *, void **, const char *);
//extern void sfi_x_genericptr(NHFILE *, void **, const char *);
void historical_sfo_genericptr_t(NHFILE *, genericptr_t *d_genericptr_t,
const char *);
void historical_sfi_genericptr_t(NHFILE *, genericptr_t *d_genericptr_t,
const char *);
void
historical_sfo_genericptr_t(NHFILE *nhfp, genericptr_t *d_genericptr_t,
const char *myname UNUSED)
{
bwrite(nhfp->fd, (genericptr_t) d_genericptr_t, sizeof *d_genericptr_t);
}
void
historical_sfi_genericptr_t(NHFILE *nhfp, genericptr_t *d_genericptr_t,
const char *myname UNUSED)
{
if (nhfp->eof) {
sfstruct_read_error();
}
mread(nhfp->fd, (genericptr_t) d_genericptr_t, sizeof *d_genericptr_t);
if (restoreinfo.mread_flags == -1)
nhfp->eof = TRUE;
}
SF_X(uint8_t, bitfield)
@@ -592,6 +640,7 @@ mread(int fd, genericptr_t buf, unsigned len)
restoreinfo.mread_flags = -1;
return;
} else {
#ifndef SFCTOOL
pline("Read %d instead of %u bytes.", (int) rlen, len);
display_nhwindow(WIN_MESSAGE, TRUE); /* flush before error() */
if (program_state.restoring) {
@@ -600,10 +649,18 @@ mread(int fd, genericptr_t buf, unsigned len)
error("Error restoring old game.");
}
panic("Error reading level file.");
#else
printf("Read %d instead of %u bytes.\n", (int) rlen, len);
#endif
}
}
}
staticfn void
sfstruct_read_error(void)
{
/* problem */;
}
#ifdef SFLOGGING
staticfn void
@@ -628,4 +685,3 @@ logging_finish(void)
icnt = 0L;
}
#endif