From 47227fc90f2b0dbf6e01282b87d16ee24db96353 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 8 Nov 2019 00:26:14 +0100 Subject: [PATCH 1/9] - replaced FILE * with FileReader in savegame code. FILE* is too inflexible, with FileReader I can plug in a transparent compressor. --- source/common/utility/files.h | 6 ++ source/duke3d/src/demo.cpp | 32 +++--- source/duke3d/src/demo.h | 2 +- source/duke3d/src/game.cpp | 7 +- source/duke3d/src/gamevars.cpp | 36 +++---- source/duke3d/src/gamevars.h | 2 +- source/duke3d/src/savegame.cpp | 175 +++++++++++++++------------------ source/duke3d/src/savegame.h | 4 +- 8 files changed, 128 insertions(+), 136 deletions(-) diff --git a/source/common/utility/files.h b/source/common/utility/files.h index 92d6eccc3..8cf17bfd8 100644 --- a/source/common/utility/files.h +++ b/source/common/utility/files.h @@ -325,6 +325,11 @@ public: virtual long Tell(); virtual long Seek(long offset, int mode); size_t Printf(const char *fmt, ...) GCCPRINTF(2,3); + void Close() + { + if (File != NULL) fclose(File); + File = nullptr; + } protected: @@ -343,6 +348,7 @@ public: BufferWriter() {} virtual size_t Write(const void *buffer, size_t len) override; TArray *GetBuffer() { return &mBuffer; } + TArray&& TakeBuffer() { return std::move(mBuffer); } }; #endif diff --git a/source/duke3d/src/demo.cpp b/source/duke3d/src/demo.cpp index 37045ef45..82ce7a1d7 100644 --- a/source/duke3d/src/demo.cpp +++ b/source/duke3d/src/demo.cpp @@ -38,7 +38,7 @@ BEGIN_DUKE_NS char g_firstDemoFile[BMAX_PATH]; -buildvfs_FILE g_demo_filePtr{}; // write +FileWriter *g_demo_filePtr{}; // write FileReader g_demo_recFilePtr; // read int32_t g_demo_cnt; @@ -163,23 +163,24 @@ void G_OpenDemoWrite(void) demonum++; - g_demo_filePtr = buildvfs_fopen_read(demofn); + g_demo_filePtr = FileWriter::Open(demofn); if (g_demo_filePtr == NULL) break; - MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); + delete g_demo_filePtr; } while (1); - g_demo_filePtr = buildvfs_fopen_write(demofn); + g_demo_filePtr = FileWriter::Open(demofn); if (g_demo_filePtr == NULL) return; - i=sv_saveandmakesnapshot(g_demo_filePtr, nullptr, -1, demorec_diffs_cvar, demorec_diffcompress_cvar, + i=sv_saveandmakesnapshot(*g_demo_filePtr, nullptr, -1, demorec_diffs_cvar, demorec_diffcompress_cvar, (demorec_seeds_cvar<<1)); if (i) { - MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); + delete g_demo_filePtr; + g_demo_filePtr = nullptr; error_wopen_demo: Bstrcpy(apStrings[QUOTE_RESERVED4], "FAILED STARTING DEMO RECORDING. SEE CONSOLE FOR DETAILS."); P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); @@ -232,13 +233,13 @@ static void Demo_WriteSync() { int16_t tmpreccnt; - buildvfs_fwrite("sYnC", 4, 1, g_demo_filePtr); + g_demo_filePtr->Write("sYnC", 4); tmpreccnt = (int16_t)ud.reccnt; - buildvfs_fwrite(&tmpreccnt, sizeof(int16_t), 1, g_demo_filePtr); + g_demo_filePtr->Write(&tmpreccnt, sizeof(int16_t)); if (demorec_seeds) - buildvfs_fwrite(g_demo_seedbuf, 1, ud.reccnt, g_demo_filePtr); + g_demo_filePtr->Write(g_demo_seedbuf, ud.reccnt); - buildvfs_fwrite(recsync, sizeof(input_t), ud.reccnt, g_demo_filePtr); + g_demo_filePtr->Write(recsync, sizeof(input_t)* ud.reccnt); ud.reccnt = 0; } @@ -275,16 +276,17 @@ void G_CloseDemoWrite(void) if (ud.reccnt > 0) Demo_WriteSync(); - buildvfs_fwrite("EnD!", 4, 1, g_demo_filePtr); + g_demo_filePtr->Write("EnD!", 4); // lastly, we need to write the number of written recsyncs to the demo file - if (buildvfs_fseek_abs(g_demo_filePtr, offsetof(savehead_t, reccnt))) - perror("G_CloseDemoWrite: final fseek"); + if (g_demo_filePtr->Write(g_demo_filePtr, offsetof(savehead_t, reccnt))) + Printf("G_CloseDemoWrite: final fseek\n"); else - buildvfs_fwrite(&g_demo_cnt, sizeof(g_demo_cnt), 1, g_demo_filePtr); + g_demo_filePtr->Write(&g_demo_cnt, sizeof(g_demo_cnt)); ud.recstat = ud.m_recstat = 0; - MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); + delete g_demo_filePtr; + g_demo_filePtr = nullptr; sv_freemem(); diff --git a/source/duke3d/src/demo.h b/source/duke3d/src/demo.h index 95d73570a..8e9c2b3ad 100644 --- a/source/duke3d/src/demo.h +++ b/source/duke3d/src/demo.h @@ -32,7 +32,7 @@ BEGIN_DUKE_NS #define DEMOFN_FMT "edemo%03d.edm" #define MAXDEMOS 1000 -extern buildvfs_FILE g_demo_filePtr; +extern FileWriter * g_demo_filePtr; extern char g_firstDemoFile[BMAX_PATH]; extern int32_t g_demo_cnt; diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index ea2b3892b..ef0730eb4 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -261,8 +261,11 @@ void G_GameExit(const char *msg) if (ud.recstat == 1) G_CloseDemoWrite(); - else if (ud.recstat == 2) - MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); + else if (ud.recstat == 2) + { + delete g_demo_filePtr; + g_demo_filePtr = nullptr; + } // JBF: fixes crash on demo playback // PK: modified from original diff --git a/source/duke3d/src/gamevars.cpp b/source/duke3d/src/gamevars.cpp index 92f773388..75f05f97b 100644 --- a/source/duke3d/src/gamevars.cpp +++ b/source/duke3d/src/gamevars.cpp @@ -236,34 +236,34 @@ corrupt: } // Note that this entire function is totally architecture dependent and needs to be fixed (which won't be easy...) -void Gv_WriteSave(buildvfs_FILE fil) +void Gv_WriteSave(FileWriter &fil) { // AddLog("Saving Game Vars to File"); - buildvfs_fwrite("BEG: EDuke32", 12, 1, fil); + fil.Write("BEG: EDuke32", 12); - buildvfs_fwrite(&g_gameVarCount,sizeof(g_gameVarCount),1,fil); + fil.Write(&g_gameVarCount,sizeof(g_gameVarCount)); for (bssize_t i = 0; i < g_gameVarCount; i++) { - buildvfs_fwrite(&(aGameVars[i]), sizeof(gamevar_t), 1, fil); - buildvfs_fwrite(aGameVars[i].szLabel, sizeof(uint8_t) * MAXVARLABEL, 1, fil); + fil.Write(&(aGameVars[i]), sizeof(gamevar_t)); + fil.Write(aGameVars[i].szLabel, sizeof(uint8_t) * MAXVARLABEL); if (aGameVars[i].flags & GAMEVAR_PERPLAYER) - buildvfs_fwrite(aGameVars[i].pValues, sizeof(intptr_t) * MAXPLAYERS, 1, fil); + fil.Write(aGameVars[i].pValues, sizeof(intptr_t) * MAXPLAYERS); else if (aGameVars[i].flags & GAMEVAR_PERACTOR) - buildvfs_fwrite(aGameVars[i].pValues, sizeof(intptr_t) * MAXSPRITES, 1, fil); + fil.Write(aGameVars[i].pValues, sizeof(intptr_t) * MAXSPRITES); } - buildvfs_fwrite(&g_gameArrayCount,sizeof(g_gameArrayCount),1,fil); + fil.Write(&g_gameArrayCount,sizeof(g_gameArrayCount)); for (bssize_t i = 0; i < g_gameArrayCount; i++) { // write for .size and .dwFlags (the rest are pointers): - buildvfs_fwrite(&aGameArrays[i], sizeof(gamearray_t), 1, fil); - buildvfs_fwrite(aGameArrays[i].szLabel, sizeof(uint8_t) * MAXARRAYLABEL, 1, fil); + fil.Write(&aGameArrays[i], sizeof(gamearray_t)); + fil.Write(aGameArrays[i].szLabel, sizeof(uint8_t) * MAXARRAYLABEL); if ((aGameArrays[i].flags & GAMEARRAY_SYSTEM) != GAMEARRAY_SYSTEM) - buildvfs_fwrite(aGameArrays[i].pValues, Gv_GetArrayAllocSize(i), 1, fil); + fil.Write(aGameArrays[i].pValues, Gv_GetArrayAllocSize(i)); } uint8_t savedstate[MAXVOLUMES * MAXLEVELS]; @@ -273,7 +273,7 @@ void Gv_WriteSave(buildvfs_FILE fil) if (g_mapInfo[i].savedstate != NULL) savedstate[i] = 1; - buildvfs_fwrite(savedstate, sizeof(savedstate), 1, fil); + fil.Write(savedstate, sizeof(savedstate)); for (bssize_t i = 0; i < (MAXVOLUMES * MAXLEVELS); i++) { @@ -281,27 +281,27 @@ void Gv_WriteSave(buildvfs_FILE fil) mapstate_t &sv = *g_mapInfo[i].savedstate; - buildvfs_fwrite(g_mapInfo[i].savedstate, sizeof(mapstate_t), 1, fil); + fil.Write(g_mapInfo[i].savedstate, sizeof(mapstate_t)); for (bssize_t j = 0; j < g_gameVarCount; j++) { if (aGameVars[j].flags & GAMEVAR_NORESET) continue; if (aGameVars[j].flags & GAMEVAR_PERPLAYER) - buildvfs_fwrite(sv.vars[j], sizeof(intptr_t) * MAXPLAYERS, 1, fil); + fil.Write(sv.vars[j], sizeof(intptr_t) * MAXPLAYERS); else if (aGameVars[j].flags & GAMEVAR_PERACTOR) - buildvfs_fwrite(sv.vars[j], sizeof(intptr_t) * MAXSPRITES, 1, fil); + fil.Write(sv.vars[j], sizeof(intptr_t) * MAXSPRITES); } - buildvfs_fwrite(sv.arraysiz, sizeof(sv.arraysiz), 1, fil); + fil.Write(sv.arraysiz, sizeof(sv.arraysiz)); for (bssize_t j = 0; j < g_gameArrayCount; j++) if (aGameArrays[j].flags & GAMEARRAY_RESTORE) { - buildvfs_fwrite(sv.arrays[j], Gv_GetArrayAllocSizeForCount(j, sv.arraysiz[j]), 1, fil); + fil.Write(sv.arrays[j], Gv_GetArrayAllocSizeForCount(j, sv.arraysiz[j])); } } - buildvfs_fwrite("EOF: EDuke32", 12, 1, fil); + fil.Write("EOF: EDuke32", 12); } void Gv_DumpValues(void) diff --git a/source/duke3d/src/gamevars.h b/source/duke3d/src/gamevars.h index 6a15a1799..42761cf83 100644 --- a/source/duke3d/src/gamevars.h +++ b/source/duke3d/src/gamevars.h @@ -159,7 +159,7 @@ void Gv_InitWeaponPointers(void); void Gv_RefreshPointers(void); void Gv_ResetVars(void); int Gv_ReadSave(FileReader &kFile); -void Gv_WriteSave(buildvfs_FILE fil); +void Gv_WriteSave(FileWriter &fil); void Gv_Clear(void); #else extern int32_t g_noResetVars; diff --git a/source/duke3d/src/savegame.cpp b/source/duke3d/src/savegame.cpp index 8a3876140..090447874 100644 --- a/source/duke3d/src/savegame.cpp +++ b/source/duke3d/src/savegame.cpp @@ -738,92 +738,75 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) Net_WaitForServer(); ready2send = 0; - char fn[BMAX_PATH]; + FString fn; - errno = 0; - buildvfs_FILE fil; + errno = 0; + buildvfs_FILE fil; - if (sv.isValid()) - { - if (snprintf(fn, sizeof(fn), "%s%s", M_GetSavegamesPath().GetChars(), sv.path)) - { - OSD_Printf("G_SavePlayer: file name \"%s\" too long\n", sv.path); - goto saveproblem; - } - fil = buildvfs_fopen_write(fn); - } - else - { - static char const SaveName[] = "save0000.esv"; - int const len = snprintf(fn, ARRAY_SIZE(fn), "%s%s", M_GetSavegamesPath().GetChars(), SaveName); - if (len >= ARRAY_SSIZE(fn)-1) - { - OSD_Printf("G_SavePlayer: could not form automatic save path\n"); - goto saveproblem; - } - char * zeros = fn + (len-8); - fil = savecounter.opennextfile(fn, zeros); - savecounter.count++; - // don't copy the mod dir into sv.path - Bstrcpy(sv.path, fn + (len-(ARRAY_SIZE(SaveName)-1))); - } + if (sv.isValid()) + { + fn.Format("%s%s", M_GetSavegamesPath().GetChars(), sv.path); + fil = fopen(fn, "wb"); + } + else + { + static char const SaveName[] = "save0000.svz"; + fn.Format("%s%s", M_GetSavegamesPath().GetChars(), SaveName); - if (!fil) - { - OSD_Printf("G_SavePlayer: failed opening \"%s\" for writing: %s\n", - fn, strerror(errno)); - goto saveproblem; - } + auto fnp = fn.LockBuffer(); + char* zeros = fnp + (fn.Len() - 8); + fil = savecounter.opennextfile(fnp, zeros); + fn.UnlockBuffer(); + savecounter.count++; + // don't copy the mod dir into sv.path + Bstrcpy(sv.path, fn + (fn.Len() - (ARRAY_SIZE(SaveName) - 1))); + } - sv.isExt = 0; + FileWriter fw(fil); + if (!fil) + { + OSD_Printf("G_SavePlayer: failed opening \"%s\" for writing: %s\n", + fn, strerror(errno)); + ready2send = 1; + Net_WaitForServer(); - // temporary hack - ud.user_map = G_HaveUserMap(); + G_RestoreTimers(); + ototalclock = totalclock; + return -1; + } + else + { + sv.isExt = 0; -#ifdef POLYMER - if (videoGetRenderMode() == REND_POLYMER) - polymer_resetlights(); -#endif + // temporary hack + ud.user_map = G_HaveUserMap(); - VM_OnEvent(EVENT_SAVEGAME, g_player[myconnectindex].ps->i, myconnectindex); + VM_OnEvent(EVENT_SAVEGAME, g_player[myconnectindex].ps->i, myconnectindex); - portableBackupSave(sv.path, sv.name, ud.last_stateless_volume, ud.last_stateless_level); + portableBackupSave(sv.path, sv.name, ud.last_stateless_volume, ud.last_stateless_level); - // SAVE! - sv_saveandmakesnapshot(fil, sv.name, 0, 0, 0, 0, isAutoSave); + // SAVE! + sv_saveandmakesnapshot(fw, sv.name, 0, 0, 0, 0, isAutoSave); - buildvfs_fclose(fil); + fw.Close(); - if (!g_netServer && ud.multimode < 2) - { - OSD_Printf("Saved: %s\n", fn); -#ifdef LUNATIC - if (!g_savedOK) - Bstrcpy(apStrings[QUOTE_RESERVED4], "^10Failed Saving Game"); - else -#endif - Bstrcpy(apStrings[QUOTE_RESERVED4], "Game Saved"); - P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); - } + if (!g_netServer && ud.multimode < 2) + { + OSD_Printf("Saved: %s\n", fn); + strcpy(apStrings[QUOTE_RESERVED4], "Game Saved"); + P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); + } - ready2send = 1; - Net_WaitForServer(); + ready2send = 1; + Net_WaitForServer(); - G_RestoreTimers(); - ototalclock = totalclock; + G_RestoreTimers(); + ototalclock = totalclock; - VM_OnEvent(EVENT_POSTSAVEGAME, g_player[myconnectindex].ps->i, myconnectindex); + VM_OnEvent(EVENT_POSTSAVEGAME, g_player[myconnectindex].ps->i, myconnectindex); - return 0; - -saveproblem: - ready2send = 1; - Net_WaitForServer(); - - G_RestoreTimers(); - ototalclock = totalclock; - - return -1; + return 0; + } } int32_t G_LoadPlayerMaybeMulti(savebrief_t & sv) @@ -917,7 +900,7 @@ static inline void ds_get(const dataspec_t *spec, void **ptr, int32_t *cnt) } // write state to file and/or to dump -static uint8_t *writespecdata(const dataspec_t *spec, buildvfs_FILE fil, uint8_t *dump) +static uint8_t *writespecdata(const dataspec_t *spec, FileWriter *fil, uint8_t *dump) { for (; spec->flags != DS_END; spec++) { @@ -932,7 +915,7 @@ static uint8_t *writespecdata(const dataspec_t *spec, buildvfs_FILE fil, uint8_t continue; else if (spec->flags & DS_STRING) { - buildvfs_fwrite(spec->ptr, Bstrlen((const char *)spec->ptr), 1, fil); // not null-terminated! + fil->Write(spec->ptr, Bstrlen((const char *)spec->ptr)); // not null-terminated! continue; } @@ -952,7 +935,7 @@ static uint8_t *writespecdata(const dataspec_t *spec, buildvfs_FILE fil, uint8_t if (fil) { - buildvfs_fwrite(ptr, spec->size, cnt, fil); + fil->Write(ptr, spec->size * cnt); } if (dump && (spec->flags & (DS_NOCHK|DS_CMP)) == 0) @@ -1522,10 +1505,8 @@ static const dataspec_t svgm_anmisc[] = { DS_END, 0, 0, 0 } }; -#if !defined LUNATIC static dataspec_gv_t *svgm_vars=NULL; -#endif -static uint8_t *dosaveplayer2(buildvfs_FILE fil, uint8_t *mem); +static uint8_t *dosaveplayer2(FileWriter &fil, uint8_t *mem); static int32_t doloadplayer2(FileReader &fil, uint8_t **memptr); static void postloadplayer(int32_t savegamep); @@ -1629,7 +1610,7 @@ static void SV_AllocSnap(int32_t allocinit) } // make snapshot only if spot < 0 (demo) -int32_t sv_saveandmakesnapshot(buildvfs_FILE fil, char const *name, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress, bool isAutoSave) +int32_t sv_saveandmakesnapshot(FileWriter &fil, char const *name, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress, bool isAutoSave) { savehead_t h; @@ -1699,24 +1680,24 @@ int32_t sv_saveandmakesnapshot(buildvfs_FILE fil, char const *name, int8_t spot, // write header - buildvfs_fwrite(&h, sizeof(savehead_t), 1, fil); + fil.Write(&h, sizeof(savehead_t)); // for savegames, the file offset after the screenshot goes here; // for demos, we keep it 0 to signify that we didn't save one - buildvfs_fwrite("\0\0\0\0", 4, 1, fil); if (spot >= 0 && tileData(TILE_SAVESHOT)) { - int32_t ofs; - + + int v = 64000; + fil.Write(&v, 4); // write the screenshot compressed - buildvfs_fwrite(tileData(TILE_SAVESHOT), 320, 200, fil); + fil.Write(tileData(TILE_SAVESHOT), 320*200); - // write the current file offset right after the header - ofs = buildvfs_ftell(fil); - buildvfs_fseek_abs(fil, sizeof(savehead_t)); - buildvfs_fwrite(&ofs, 4, 1, fil); - buildvfs_fseek_abs(fil, ofs); } + else + { + int v = 64000; + fil.Write(&v, 4); + } if (spot >= 0) @@ -1883,7 +1864,7 @@ int32_t sv_loadsnapshot(FileReader &fil, int32_t spot, savehead_t *h) } -uint32_t sv_writediff(buildvfs_FILE fil) +uint32_t sv_writediff(FileWriter *fil) { uint8_t *p = svsnapshot; uint8_t *d = svdiff; @@ -1901,10 +1882,10 @@ uint32_t sv_writediff(buildvfs_FILE fil) uint32_t const diffsiz = d - svdiff; - buildvfs_fwrite("dIfF",4,1,fil); - buildvfs_fwrite(&diffsiz, sizeof(diffsiz), 1, fil); + fil->Write("dIfF",4); + fil->Write(&diffsiz, sizeof(diffsiz)); - buildvfs_fwrite(svdiff, 1, diffsiz, fil); + fil->Write(svdiff, diffsiz); return diffsiz; } @@ -2168,19 +2149,19 @@ static void sv_restload() LUNATIC_CB const char *(*El_SerializeGamevars)(int32_t *slenptr, int32_t levelnum); #endif -static uint8_t *dosaveplayer2(buildvfs_FILE fil, uint8_t *mem) +static uint8_t *dosaveplayer2(FileWriter &fil, uint8_t *mem) { #ifdef DEBUGGINGAIDS uint8_t *tmem = mem; int32_t t=timerGetTicks(); #endif - mem=writespecdata(svgm_udnetw, fil, mem); // user settings, players & net + mem=writespecdata(svgm_udnetw, &fil, mem); // user settings, players & net PRINTSIZE("ud"); - mem=writespecdata(svgm_secwsp, fil, mem); // sector, wall, sprite + mem=writespecdata(svgm_secwsp, &fil, mem); // sector, wall, sprite PRINTSIZE("sws"); - mem=writespecdata(svgm_script, fil, mem); // script + mem=writespecdata(svgm_script, &fil, mem); // script PRINTSIZE("script"); - mem=writespecdata(svgm_anmisc, fil, mem); // animates, quotes & misc. + mem=writespecdata(svgm_anmisc, &fil, mem); // animates, quotes & misc. PRINTSIZE("animisc"); #if !defined LUNATIC diff --git a/source/duke3d/src/savegame.h b/source/duke3d/src/savegame.h index 4f5133572..165a38c0d 100644 --- a/source/duke3d/src/savegame.h +++ b/source/duke3d/src/savegame.h @@ -121,10 +121,10 @@ extern uint16_t g_nummenusaves; int32_t sv_updatestate(int32_t frominit); int32_t sv_readdiff(FileReader& fil); -uint32_t sv_writediff(buildvfs_FILE fil); +uint32_t sv_writediff(FileWriter *fil); int32_t sv_loadheader(FileReader &fil, int32_t spot, savehead_t *h); int32_t sv_loadsnapshot(FileReader &fil, int32_t spot, savehead_t *h); -int32_t sv_saveandmakesnapshot(buildvfs_FILE fil, char const *name, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress, bool isAutoSave = false); +int32_t sv_saveandmakesnapshot(FileWriter &fil, char const *name, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress, bool isAutoSave = false); void sv_freemem(); void G_DeleteSave(savebrief_t const & sv); void G_DeleteOldSaves(void); From a40be954f1bf701e8fd7e4c31efc1994b3a50f87 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 8 Nov 2019 01:04:27 +0100 Subject: [PATCH 2/9] - same change for RedNukem front end. --- source/common/initfs.cpp | 4 +- source/duke3d/src/demo.cpp | 9 +- source/duke3d/src/savegame.cpp | 22 ++--- source/rr/src/demo.cpp | 37 ++++---- source/rr/src/demo.h | 2 +- source/rr/src/game.cpp | 7 +- source/rr/src/savegame.cpp | 152 +++++++++++++++------------------ source/rr/src/savegame.h | 10 +-- 8 files changed, 106 insertions(+), 137 deletions(-) diff --git a/source/common/initfs.cpp b/source/common/initfs.cpp index d4dfecd96..2d0c6192c 100644 --- a/source/common/initfs.cpp +++ b/source/common/initfs.cpp @@ -157,9 +157,9 @@ void D_AddWildFile (TArray &wadfiles, const char *value) { return; } - const char *wadfile = BaseFileSearch (value, ".wad", false); + FString wadfile = BaseFileSearch (value, ".wad", false); - if (wadfile != NULL) + if (wadfile.Len() != 0) { D_AddFile (wadfiles, wadfile); } diff --git a/source/duke3d/src/demo.cpp b/source/duke3d/src/demo.cpp index 82ce7a1d7..2e1ebf159 100644 --- a/source/duke3d/src/demo.cpp +++ b/source/duke3d/src/demo.cpp @@ -65,7 +65,6 @@ static void Demo_RestoreModes(int32_t menu) g_player[myconnectindex].ps->gm |= MODE_DEMO; } -// This is utterly gross. Global configuration should not be manipulated like this. void Demo_PrepareWarp(void) { if (!g_demo_paused) @@ -279,10 +278,7 @@ void G_CloseDemoWrite(void) g_demo_filePtr->Write("EnD!", 4); // lastly, we need to write the number of written recsyncs to the demo file - if (g_demo_filePtr->Write(g_demo_filePtr, offsetof(savehead_t, reccnt))) - Printf("G_CloseDemoWrite: final fseek\n"); - else - g_demo_filePtr->Write(&g_demo_cnt, sizeof(g_demo_cnt)); + g_demo_filePtr->Write(&g_demo_cnt, sizeof(g_demo_cnt)); ud.recstat = ud.m_recstat = 0; delete g_demo_filePtr; @@ -418,7 +414,8 @@ static void Demo_FinishProfile(void) dn, gms, (gms*1000.0)/nt); } - if (nf > 0) { + if (nf > 0) + { OSD_Printf("== demo %d: %d frames (%d frames/gametic)\n", dn, nf, g_demo_profile-1); OSD_Printf("== demo %d drawrooms times: %.03f s (%.03f ms/frame)\n", dn, dms1/1000.0, dms1/nf); diff --git a/source/duke3d/src/savegame.cpp b/source/duke3d/src/savegame.cpp index 090447874..a4b9ab68c 100644 --- a/source/duke3d/src/savegame.cpp +++ b/source/duke3d/src/savegame.cpp @@ -206,7 +206,7 @@ static void ReadSaveGameHeaders_CACHE1D(TArray &saves) static void ReadSaveGameHeaders_Internal(void) { - FString pattern = M_GetSavegamesPath() + "*.esv"; + FString pattern = M_GetSavegamesPath() + "*.bsv"; TArray saves; D_AddWildFile(saves, pattern); // potentially overallocating but programmatically simple @@ -750,7 +750,7 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) } else { - static char const SaveName[] = "save0000.svz"; + static char const SaveName[] = "save0000.bsv"; fn.Format("%s%s", M_GetSavegamesPath().GetChars(), SaveName); auto fnp = fn.LockBuffer(); @@ -1002,7 +1002,7 @@ static int32_t readspecdata(const dataspec_t *spec, FileReader *fil, uint8_t **d if (!ptr || !cnt) continue; - if (fil != nullptr) + if (fil != nullptr && fil->isOpen()) { auto const mem = (dump && (spec->flags & DS_NOCHK) == 0) ? dump : (uint8_t *)ptr; int const siz = cnt * spec->size; @@ -1376,11 +1376,6 @@ static const dataspec_t svgm_udnetw[] = { 0, connectpoint2, sizeof(connectpoint2), 1 }, { 0, &randomseed, sizeof(randomseed), 1 }, { 0, &g_globalRandom, sizeof(g_globalRandom), 1 }, -#ifdef LUNATIC - // Save game tic count for Lunatic because it is exposed to userland. See - // test/helixspawner.lua for an example. - { 0, &g_moveThingsCount, sizeof(g_moveThingsCount), 1 }, -#endif // { 0, &lockclock_dummy, sizeof(lockclock), 1 }, { DS_END, 0, 0, 0 } }; @@ -1695,7 +1690,7 @@ int32_t sv_saveandmakesnapshot(FileWriter &fil, char const *name, int8_t spot, i } else { - int v = 64000; + int v = 0; fil.Write(&v, 4); } @@ -1810,7 +1805,7 @@ int32_t sv_loadsnapshot(FileReader &fil, int32_t spot, savehead_t *h) } if (i > 0) { - if (fil.Seek(i, FileReader::SeekSet) != i) + if (fil.Seek(i, FileReader::SeekCur) < 0) { OSD_Printf("sv_snapshot: failed skipping over the screenshot.\n"); return 8; @@ -2185,13 +2180,6 @@ static int32_t doloadplayer2(FileReader &fil, uint8_t **memptr) PRINTSIZE("ud"); if (readspecdata(svgm_secwsp, &fil, &mem)) return -4; PRINTSIZE("sws"); -#ifdef LUNATIC - { - int32_t ret = El_ReadSaveCode(fil); - if (ret < 0) - return ret; - } -#endif if (readspecdata(svgm_script, &fil, &mem)) return -5; PRINTSIZE("script"); if (readspecdata(svgm_anmisc, &fil, &mem)) return -6; diff --git a/source/rr/src/demo.cpp b/source/rr/src/demo.cpp index 9cf547c5d..15ff12524 100644 --- a/source/rr/src/demo.cpp +++ b/source/rr/src/demo.cpp @@ -37,8 +37,8 @@ BEGIN_RR_NS char g_firstDemoFile[BMAX_PATH]; -buildvfs_FILE g_demo_filePtr{}; // write -FileReader g_demo_recFilePtr; +FileWriter *g_demo_filePtr{}; // write +FileReader g_demo_recFilePtr; // read int32_t g_demo_cnt; int32_t g_demo_goalCnt=0; @@ -156,7 +156,7 @@ void G_OpenDemoWrite(void) if (demonum == MAXDEMOS) return; - if (snprintf(demofn, sizeof(demofn), "%s" DEMOFN_FMT, M_GetSavegamesPath().GetChars(), demonum)) + if (snprintf(demofn, sizeof(demofn), "%s" DEMOFN_FMT, M_GetDemoPath().GetChars(), demonum)) { initprintf("Couldn't start demo writing: INTERNAL ERROR: file name too long\n"); goto error_wopen_demo; @@ -164,25 +164,26 @@ void G_OpenDemoWrite(void) demonum++; - g_demo_filePtr = buildvfs_fopen_read(demofn); + g_demo_filePtr = FileWriter::Open(demofn); if (g_demo_filePtr == NULL) break; - MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); + delete g_demo_filePtr; } while (1); - g_demo_filePtr = buildvfs_fopen_write(demofn); + g_demo_filePtr = FileWriter::Open(demofn); if (g_demo_filePtr == NULL) return; - i=sv_saveandmakesnapshot(g_demo_filePtr, nullptr, -1, demorec_diffs_cvar, demorec_diffcompress_cvar, + i=sv_saveandmakesnapshot(*g_demo_filePtr, nullptr, -1, demorec_diffs_cvar, demorec_diffcompress_cvar, (demorec_seeds_cvar<<1)); if (i) { - MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); + delete g_demo_filePtr; + g_demo_filePtr = nullptr; error_wopen_demo: - Bstrcpy(apStrings[QUOTE_RESERVED4], "FAILED STARTING DEMO RECORDING. SEE OSD FOR DETAILS."); + Bstrcpy(apStrings[QUOTE_RESERVED4], "FAILED STARTING DEMO RECORDING. SEE CONSOLE FOR DETAILS."); P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); ud.recstat = ud.m_recstat = 0; return; @@ -233,13 +234,13 @@ static void Demo_WriteSync() { int16_t tmpreccnt; - buildvfs_fwrite("sYnC", 4, 1, g_demo_filePtr); + g_demo_filePtr->Write("sYnC", 4); tmpreccnt = (int16_t)ud.reccnt; - buildvfs_fwrite(&tmpreccnt, sizeof(int16_t), 1, g_demo_filePtr); + g_demo_filePtr->Write(&tmpreccnt, sizeof(int16_t)); if (demorec_seeds) - buildvfs_fwrite(g_demo_seedbuf, 1, ud.reccnt, g_demo_filePtr); + g_demo_filePtr->Write(g_demo_seedbuf, ud.reccnt); - buildvfs_fwrite(recsync, sizeof(input_t), ud.reccnt, g_demo_filePtr); + g_demo_filePtr->Write(recsync, sizeof(input_t)* ud.reccnt); ud.reccnt = 0; } @@ -276,16 +277,14 @@ void G_CloseDemoWrite(void) if (ud.reccnt > 0) Demo_WriteSync(); - buildvfs_fwrite("EnD!", 4, 1, g_demo_filePtr); + g_demo_filePtr->Write("EnD!", 4); // lastly, we need to write the number of written recsyncs to the demo file - if (buildvfs_fseek_abs(g_demo_filePtr, offsetof(savehead_t, reccnt))) - perror("G_CloseDemoWrite: final fseek"); - else - buildvfs_fwrite(&g_demo_cnt, sizeof(g_demo_cnt), 1, g_demo_filePtr); + g_demo_filePtr->Write(&g_demo_cnt, sizeof(g_demo_cnt)); ud.recstat = ud.m_recstat = 0; - MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); + delete g_demo_filePtr; + g_demo_filePtr = nullptr; sv_freemem(); diff --git a/source/rr/src/demo.h b/source/rr/src/demo.h index fafeea1c7..372b842fa 100644 --- a/source/rr/src/demo.h +++ b/source/rr/src/demo.h @@ -33,7 +33,7 @@ BEGIN_RR_NS #define LDEMOFN_FMT "demo%d.dmo" #define MAXDEMOS 1000 -extern FILE *g_demo_filePtr; +extern FileWriter * g_demo_filePtr; extern char g_firstDemoFile[BMAX_PATH]; extern int32_t g_demo_cnt; diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index ac77c66a2..ad1961488 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -361,8 +361,11 @@ void G_GameExit(const char *msg) if (ud.recstat == 1) G_CloseDemoWrite(); - else if (ud.recstat == 2) - MAYBE_FCLOSE_AND_NULL(g_demo_filePtr); + else if (ud.recstat == 2) + { + delete g_demo_filePtr; + g_demo_filePtr = nullptr; + } // JBF: fixes crash on demo playback // PK: modified from original diff --git a/source/rr/src/savegame.cpp b/source/rr/src/savegame.cpp index 868f42f23..52f7f3961 100644 --- a/source/rr/src/savegame.cpp +++ b/source/rr/src/savegame.cpp @@ -186,7 +186,7 @@ static void ReadSaveGameHeaders_CACHE1D(TArray& saves) static void ReadSaveGameHeaders_Internal(void) { - FString pattern = M_GetSavegamesPath() + "*.esv"; + FString pattern = M_GetSavegamesPath() + "*.bsv"; TArray saves; D_AddWildFile(saves, pattern); @@ -480,55 +480,52 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) Net_WaitForEverybody(); ready2send = 0; - char temp[BMAX_PATH]; + FString fn; - errno = 0; - FILE *fil; + errno = 0; + buildvfs_FILE fil; - if (sv.isValid()) - { - if (snprintf(temp, sizeof(temp), "%s%s", M_GetSavegamesPath().GetChars(), sv.path)) - { - OSD_Printf("G_SavePlayer: file name \"%s\" too long\n", sv.path); - goto saveproblem; - } - fil = fopen(temp, "wb"); - } - else - { - static char const SaveName[] = "save0000.esv"; - int const len = snprintf(temp, ARRAY_SIZE(temp), "%s%s", M_GetSavegamesPath().GetChars(), SaveName); - if (len >= ARRAY_SSIZE(temp)-1) - { - OSD_Printf("G_SavePlayer: could not form automatic save path\n"); - goto saveproblem; - } - char * zeros = temp + (len-8); - fil = savecounter.opennextfile(temp, zeros); - savecounter.count++; - // don't copy the mod dir into sv.path - Bstrcpy(sv.path, temp + (len-(ARRAY_SIZE(SaveName)-1))); - } + if (sv.isValid()) + { + fn.Format("%s%s", M_GetSavegamesPath().GetChars(), sv.path); + fil = fopen(fn, "wb"); + } + else + { + static char const SaveName[] = "save0000.bsv"; + fn.Format("%s%s", M_GetSavegamesPath().GetChars(), SaveName); - if (!fil) - { - OSD_Printf("G_SavePlayer: failed opening \"%s\" for writing: %s\n", - temp, strerror(errno)); - goto saveproblem; - } + auto fnp = fn.LockBuffer(); + char* zeros = fnp + (fn.Len() - 8); + fil = savecounter.opennextfile(fnp, zeros); + fn.UnlockBuffer(); + savecounter.count++; + // don't copy the mod dir into sv.path + Bstrcpy(sv.path, fn + (fn.Len() - (ARRAY_SIZE(SaveName) - 1))); + } - // temporary hack - ud.user_map = G_HaveUserMap(); + FileWriter fw(fil); + if (!fil) + { + OSD_Printf("G_SavePlayer: failed opening \"%s\" for writing: %s\n", + fn, strerror(errno)); + ready2send = 1; + Net_WaitForEverybody(); -#ifdef POLYMER - if (videoGetRenderMode() == REND_POLYMER) - polymer_resetlights(); -#endif + G_RestoreTimers(); + ototalclock = totalclock; + return -1; + } + else + { + // temporary hack + ud.user_map = G_HaveUserMap(); - // SAVE! - sv_saveandmakesnapshot(fil, sv.name, 0, 0, 0, 0, isAutoSave); - fclose(fil); + // SAVE! + sv_saveandmakesnapshot(fw, sv.name, 0, 0, 0, 0, isAutoSave); + + fw.Close(); if (!g_netServer && ud.multimode < 2) { @@ -543,15 +540,7 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) ototalclock = totalclock; return 0; - -saveproblem: - ready2send = 1; - Net_WaitForEverybody(); - - G_RestoreTimers(); - ototalclock = totalclock; - - return -1; + } } int32_t G_LoadPlayerMaybeMulti(savebrief_t & sv) @@ -637,7 +626,7 @@ static inline void ds_get(const dataspec_t *spec, void **ptr, int32_t *cnt) } // write state to file and/or to dump -static uint8_t *writespecdata(const dataspec_t *spec, FILE *fil, uint8_t *dump) +static uint8_t *writespecdata(const dataspec_t *spec, FileWriter *fil, uint8_t *dump) { for (; spec->flags != DS_END; spec++) { @@ -652,7 +641,7 @@ static uint8_t *writespecdata(const dataspec_t *spec, FILE *fil, uint8_t *dump) continue; else if (spec->flags & DS_STRING) { - fwrite(spec->ptr, Bstrlen((const char *)spec->ptr), 1, fil); // not null-terminated! + fil->Write(spec->ptr, Bstrlen((const char *)spec->ptr)); // not null-terminated! continue; } @@ -672,7 +661,7 @@ static uint8_t *writespecdata(const dataspec_t *spec, FILE *fil, uint8_t *dump) if (fil) { - fwrite(ptr, spec->size, cnt, fil); + fil->Write(ptr, spec->size * cnt); } if (dump && (spec->flags & (DS_NOCHK|DS_CMP)) == 0) @@ -739,7 +728,7 @@ static int32_t readspecdata(const dataspec_t *spec, FileReader *fil, uint8_t **d if (!ptr || !cnt) continue; - if (fil->isOpen()) + if (fil != nullptr && fil->isOpen()) { auto const mem = (dump && (spec->flags & DS_NOCHK) == 0) ? dump : (uint8_t *)ptr; int const siz = cnt * spec->size; @@ -1267,7 +1256,7 @@ static const dataspec_t svgm_anmisc[] = { DS_END, 0, 0, 0 } }; -static uint8_t *dosaveplayer2(FILE *fil, uint8_t *mem); +static uint8_t *dosaveplayer2(FileWriter *fil, uint8_t *mem); static int32_t doloadplayer2(FileReader &fil, uint8_t **memptr); static void postloadplayer(int32_t savegamep); @@ -1299,7 +1288,7 @@ static void SV_AllocSnap(int32_t allocinit) } // make snapshot only if spot < 0 (demo) -int32_t sv_saveandmakesnapshot(FILE *fil, char const *name, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress, bool isAutoSave) +int32_t sv_saveandmakesnapshot(FileWriter &fil, char const *name, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress, bool isAutoSave) { savehead_t h; @@ -1347,10 +1336,6 @@ int32_t sv_saveandmakesnapshot(FILE *fil, char const *name, int8_t spot, int8_t { // savegame Bstrncpyz(h.savename, name, sizeof(h.savename)); -#ifdef __ANDROID__ - Bstrncpyz(h.volname, g_volumeNames[ud.volume_number], sizeof(h.volname)); - Bstrncpyz(h.skillname, g_skillNames[ud.player_skill], sizeof(h.skillname)); -#endif } else { @@ -1367,40 +1352,37 @@ int32_t sv_saveandmakesnapshot(FILE *fil, char const *name, int8_t spot, int8_t // write header - fwrite(&h, sizeof(savehead_t), 1, fil); + fil.Write(&h, sizeof(savehead_t)); // for savegames, the file offset after the screenshot goes here; // for demos, we keep it 0 to signify that we didn't save one - fwrite("\0\0\0\0", 4, 1, fil); if (spot >= 0 && tileData(TILE_SAVESHOT)) { - int32_t ofs; - + + int v = 64000; + fil.Write(&v, 4); // write the screenshot compressed - fwrite(tileData(TILE_SAVESHOT), 320, 200, fil); + fil.Write(tileData(TILE_SAVESHOT), 320*200); - // write the current file offset right after the header - ofs = ftell(fil); - fseek(fil, sizeof(savehead_t), SEEK_SET); - fwrite(&ofs, 4, 1, fil); - fseek(fil, ofs, SEEK_SET); } + else + { + int v = 0; + fil.Write(&v, 4); + } -#ifdef DEBUGGINGAIDS - OSD_Printf("sv_saveandmakesnapshot: snapshot size: %d bytes.\n", svsnapsiz); -#endif if (spot >= 0) { // savegame - dosaveplayer2(fil, NULL); + dosaveplayer2(&fil, NULL); } else { // demo SV_AllocSnap(0); - uint8_t * const p = dosaveplayer2(fil, svsnapshot); + uint8_t * const p = dosaveplayer2(&fil, svsnapshot); if (p != svsnapshot+svsnapsiz) { @@ -1441,7 +1423,7 @@ int32_t sv_loadheader(FileReader &fil, int32_t spot, savehead_t *h) #ifndef DEBUGGINGAIDS if (havedemo) #endif - OSD_Printf("Incompatible file version. Expected %d.%d.%d.%d.%0x, found %d.%d.%d.%d.%0x\n", SV_MAJOR_VER, SV_MINOR_VER, BYTEVERSION, + OSD_Printf("Incompatible savegame. Expected version %d.%d.%d.%d.%0x, found %d.%d.%d.%d.%0x\n", SV_MAJOR_VER, SV_MINOR_VER, BYTEVERSION, ud.userbytever, g_scriptcrc, h->majorver, h->minorver, h->bytever, h->userbytever, h->scriptcrc); if (h->majorver == SV_MAJOR_VER && h->minorver == SV_MINOR_VER) @@ -1500,8 +1482,8 @@ int32_t sv_loadsnapshot(FileReader &fil, int32_t spot, savehead_t *h) } if (i > 0) { - if (fil.Seek(i, FileReader::SeekSet) != i) - { + if (fil.Seek(i, FileReader::SeekCur) < 0) + { OSD_Printf("sv_snapshot: failed skipping over the screenshot.\n"); return 8; } @@ -1554,7 +1536,7 @@ int32_t sv_loadsnapshot(FileReader &fil, int32_t spot, savehead_t *h) } -uint32_t sv_writediff(FILE *fil) +uint32_t sv_writediff(FileWriter *fil) { uint8_t *p = svsnapshot; uint8_t *d = svdiff; @@ -1569,10 +1551,10 @@ uint32_t sv_writediff(FILE *fil) uint32_t const diffsiz = d - svdiff; - fwrite("dIfF",4,1,fil); - fwrite(&diffsiz, sizeof(diffsiz), 1, fil); + fil->Write("dIfF",4); + fil->Write(&diffsiz, sizeof(diffsiz)); - fwrite(svdiff, 1, diffsiz, fil); + fil->Write(svdiff, diffsiz); return diffsiz; } @@ -1746,7 +1728,7 @@ static void sv_restload() # define PRINTSIZE(name) do { } while (0) #endif -static uint8_t *dosaveplayer2(FILE *fil, uint8_t *mem) +static uint8_t *dosaveplayer2(FileWriter *fil, uint8_t *mem) { #ifdef DEBUGGINGAIDS uint8_t *tmem = mem; diff --git a/source/rr/src/savegame.h b/source/rr/src/savegame.h index 4aadb3fbf..d4f912af6 100644 --- a/source/rr/src/savegame.h +++ b/source/rr/src/savegame.h @@ -111,11 +111,11 @@ extern menusave_t * g_menusaves; extern uint16_t g_nummenusaves; int32_t sv_updatestate(int32_t frominit); -int32_t sv_readdiff(FileReader &fil); -uint32_t sv_writediff(FILE *fil); -int32_t sv_loadheader(FileReader& fil, int32_t spot, savehead_t* h); -int32_t sv_loadsnapshot(FileReader& fil, int32_t spot, savehead_t* h); -int32_t sv_saveandmakesnapshot(FILE *fil, char const *name, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress, bool isAutoSave = false); +int32_t sv_readdiff(FileReader& fil); +uint32_t sv_writediff(FileWriter *fil); +int32_t sv_loadheader(FileReader &fil, int32_t spot, savehead_t *h); +int32_t sv_loadsnapshot(FileReader &fil, int32_t spot, savehead_t *h); +int32_t sv_saveandmakesnapshot(FileWriter &fil, char const *name, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress, bool isAutoSave = false); void sv_freemem(); void G_DeleteSave(savebrief_t const & sv); void G_DeleteOldSaves(void); From 4fc56203c27d82860ff93c93d0e9c06cc3ac8c21 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 8 Nov 2019 01:36:32 +0100 Subject: [PATCH 3/9] - implemented savegame compression Unfortunately necessary because Ion Fury savegames store 120 GB(!!) of data, mostly zeros. Unlike the old method, this compresses the entire savegame as one block using a ZLib stream so it should be a lot more efficient now. --- source/common/filesystem/file_zip.cpp | 2 +- source/common/utility/files.h | 29 +++- source/common/utility/files_decompress.cpp | 170 ++++++++++++++++++--- source/duke3d/src/savegame.cpp | 46 ++++-- source/rr/src/savegame.cpp | 64 +++++--- 5 files changed, 256 insertions(+), 55 deletions(-) diff --git a/source/common/filesystem/file_zip.cpp b/source/common/filesystem/file_zip.cpp index 7790d78b6..579391147 100644 --- a/source/common/filesystem/file_zip.cpp +++ b/source/common/filesystem/file_zip.cpp @@ -68,7 +68,7 @@ static bool UncompressZipLump(char *Cache, FileReader &Reader, int Method, int L case METHOD_LZMA: { FileReader frz; - if (frz.OpenDecompressor(Reader, LumpSize, Method, false, [](const char* err) { I_Error("%s", err); })) + if (frz.OpenDecompressor(Reader, LumpSize, Method, false, nullptr)) { frz.Read(Cache, LumpSize); } diff --git a/source/common/utility/files.h b/source/common/utility/files.h index 8cf17bfd8..d9eb00b1c 100644 --- a/source/common/utility/files.h +++ b/source/common/utility/files.h @@ -56,6 +56,7 @@ enum METHOD_PPMD = 98, METHOD_LZSS = 1337, // not used in Zips - this is for Console Doom compression METHOD_ZLIB = 1338, // Zlib stream with header, used by compressed nodes. + METHOD_TRANSFEROWNER = 0x8000, }; class FileReaderInterface @@ -85,6 +86,11 @@ public: { ErrorCallback = cb; } + void SetOwnsReader(); + +protected: + FileReader *File = nullptr; + FileReader OwnedFile; }; class MemoryReader : public FileReaderInterface @@ -316,7 +322,7 @@ public: } virtual ~FileWriter() { - if (File != NULL) fclose(File); + Close(); } static FileWriter *Open(const char *filename); @@ -325,7 +331,7 @@ public: virtual long Tell(); virtual long Seek(long offset, int mode); size_t Printf(const char *fmt, ...) GCCPRINTF(2,3); - void Close() + virtual void Close() { if (File != NULL) fclose(File); File = nullptr; @@ -351,4 +357,23 @@ public: TArray&& TakeBuffer() { return std::move(mBuffer); } }; +class CompressedFileWriter : public FileWriter +{ + FileWriter *target; + struct z_stream_s *zipstream; + uint8_t outbuf[1024]; + size_t compressedSize; + bool ownsWriter; + + size_t WriteBlock(const void *buffer, size_t bytes); + +public: + CompressedFileWriter(FileWriter *wr, bool transfer = false); + CompressedFileWriter(FILE *wr); + ~CompressedFileWriter() { Close(); } + virtual size_t Write(const void *buffer, size_t len) override; + virtual void Close() override; + +}; + #endif diff --git a/source/common/utility/files_decompress.cpp b/source/common/utility/files_decompress.cpp index 4770c1440..7136620e6 100644 --- a/source/common/utility/files_decompress.cpp +++ b/source/common/utility/files_decompress.cpp @@ -63,7 +63,7 @@ void DecompressorBase::DecompressionError(const char *error, ...) const va_end(argptr); if (ErrorCallback != nullptr) ErrorCallback(errortext); - else std::terminate(); + else throw std::runtime_error(errortext); } long DecompressorBase::Tell () const @@ -82,6 +82,12 @@ char *DecompressorBase::Gets(char *strbuf, int len) return nullptr; } +void DecompressorBase::SetOwnsReader() +{ + OwnedFile = std::move(*File); + File = &OwnedFile; +} + // // M_ZlibError // @@ -125,17 +131,17 @@ class DecompressorZ : public DecompressorBase { enum { BUFF_SIZE = 4096 }; - FileReader &File; bool SawEOF; z_stream Stream; uint8_t InBuff[BUFF_SIZE]; public: - DecompressorZ (FileReader &file, bool zip, const std::function& cb) - : File(file), SawEOF(false) + DecompressorZ (FileReader *file, bool zip, const std::function& cb) + : SawEOF(false) { int err; + File = file; SetErrorCallback(cb); FillBuffer (); @@ -187,7 +193,7 @@ public: void FillBuffer () { - auto numread = File.Read (InBuff, BUFF_SIZE); + auto numread = File->Read (InBuff, BUFF_SIZE); if (numread < BUFF_SIZE) { @@ -216,17 +222,17 @@ class DecompressorBZ2 : public DecompressorBase { enum { BUFF_SIZE = 4096 }; - FileReader &File; bool SawEOF; bz_stream Stream; uint8_t InBuff[BUFF_SIZE]; public: - DecompressorBZ2 (FileReader &file, const std::function& cb) - : File(file), SawEOF(false) + DecompressorBZ2 (FileReader *file, const std::function& cb) + : SawEOF(false) { int err; + File = file; SetErrorCallback(cb); stupidGlobal = this; FillBuffer (); @@ -281,7 +287,7 @@ public: void FillBuffer () { - auto numread = File.Read(InBuff, BUFF_SIZE); + auto numread = File->Read(InBuff, BUFF_SIZE); if (numread < BUFF_SIZE) { @@ -325,7 +331,6 @@ class DecompressorLZMA : public DecompressorBase { enum { BUFF_SIZE = 4096 }; - FileReader &File; bool SawEOF; CLzmaDec Stream; size_t Size; @@ -335,18 +340,19 @@ class DecompressorLZMA : public DecompressorBase public: - DecompressorLZMA (FileReader &file, size_t uncompressed_size, const std::function& cb) - : File(file), SawEOF(false) + DecompressorLZMA (FileReader *file, size_t uncompressed_size, const std::function& cb) + : SawEOF(false) { uint8_t header[4 + LZMA_PROPS_SIZE]; int err; + File = file; SetErrorCallback(cb); Size = uncompressed_size; OutProcessed = 0; // Read zip LZMA properties header - if (File.Read(header, sizeof(header)) < (long)sizeof(header)) + if (File->Read(header, sizeof(header)) < (long)sizeof(header)) { DecompressionError("DecompressorLZMA: File too short\n"); } @@ -423,7 +429,7 @@ public: void FillBuffer () { - auto numread = File.Read(InBuff, BUFF_SIZE); + auto numread = File->Read(InBuff, BUFF_SIZE); if (numread < BUFF_SIZE) { @@ -445,7 +451,6 @@ class DecompressorLZSS : public DecompressorBase { enum { BUFF_SIZE = 4096, WINDOW_SIZE = 4096, INTERNAL_BUFFER_SIZE = 128 }; - FileReader &File; bool SawEOF; uint8_t InBuff[BUFF_SIZE]; @@ -476,7 +481,7 @@ class DecompressorLZSS : public DecompressorBase if(Stream.AvailIn) memmove(InBuff, Stream.In, Stream.AvailIn); - auto numread = File.Read(InBuff+Stream.AvailIn, BUFF_SIZE-Stream.AvailIn); + auto numread = File->Read(InBuff+Stream.AvailIn, BUFF_SIZE-Stream.AvailIn); if (numread < BUFF_SIZE) { @@ -563,8 +568,9 @@ class DecompressorLZSS : public DecompressorBase } public: - DecompressorLZSS(FileReader &file, const std::function& cb) : File(file), SawEOF(false) + DecompressorLZSS(FileReader *file, const std::function& cb) : File(file), SawEOF(false) { + File = file; SetErrorCallback(cb); Stream.State = STREAM_EMPTY; Stream.WindowData = Stream.InternalBuffer = Stream.Window+WINDOW_SIZE; @@ -629,29 +635,35 @@ public: bool FileReader::OpenDecompressor(FileReader &parent, Size length, int method, bool seekable, const std::function& cb) { DecompressorBase *dec = nullptr; - switch (method) + FileReader *p = &parent; + switch (method & ~METHOD_TRANSFEROWNER) { case METHOD_DEFLATE: case METHOD_ZLIB: - dec = new DecompressorZ(parent, method == METHOD_DEFLATE, cb); + dec = new DecompressorZ(p, method == METHOD_DEFLATE, cb); break; case METHOD_BZIP2: - dec = new DecompressorBZ2(parent, cb); + dec = new DecompressorBZ2(p, cb); break; case METHOD_LZMA: - dec = new DecompressorLZMA(parent, length, cb); + dec = new DecompressorLZMA(p, length, cb); break; case METHOD_LZSS: - dec = new DecompressorLZSS(parent, cb); + dec = new DecompressorLZSS(p, cb); break; // todo: METHOD_IMPLODE, METHOD_SHRINK default: return false; } + if (method & METHOD_TRANSFEROWNER) + { + dec->SetOwnsReader(); + } + dec->Length = (long)length; if (!seekable) { @@ -666,3 +678,117 @@ bool FileReader::OpenDecompressor(FileReader &parent, Size length, int method, b return false; } } + + + +//========================================================================== +// +// +// +//========================================================================== + +CompressedFileWriter::CompressedFileWriter(FileWriter *targ, bool transfer) +{ + target = targ; + zipstream = new z_stream; + + compressedSize = 0; + zipstream->next_in = Z_NULL; + zipstream->avail_in = 0; + zipstream->zalloc = Z_NULL; + zipstream->zfree = Z_NULL; + int err = deflateInit2 (zipstream, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY); + if (err != Z_OK) + { + delete zipstream; + zipstream = nullptr; + return; + } + zipstream->next_out = outbuf; + zipstream->avail_out = sizeof(outbuf); + ownsWriter = transfer; +} + +//========================================================================== +// +// +// +//========================================================================== + +CompressedFileWriter::CompressedFileWriter(FILE *targ) + : CompressedFileWriter(new FileWriter(targ), true) +{ +} + +//========================================================================== +// +// +// +//========================================================================== + +size_t CompressedFileWriter::Write(const void *buffer, size_t bytes) +{ + size_t wrote = 0; + size_t towrite = bytes; + + zipstream->next_in = (Bytef *)buffer; + while (bytes > 0) + { + auto chunk = std::min(towrite, (size_t)0x40000000); + zipstream->avail_in = chunk; + buffer = ((char*)buffer) + chunk; + towrite -= chunk; + + while (zipstream->avail_in != 0) + { + if (zipstream->avail_out == 0) + { + zipstream->next_out = outbuf; + zipstream->avail_out = 1024; + wrote += 1024; + target->Write(outbuf, 1024); + } + deflate (zipstream, Z_NO_FLUSH); + } + } + compressedSize += wrote; + return bytes; +} + +//========================================================================== +// +// +// +//========================================================================== + +void CompressedFileWriter::Close() +{ + if (!zipstream) return; + // Flush the zlib stream buffer. + + for (bool done = false;;) + { + auto len = sizeof(outbuf) - zipstream->avail_out; + if (len != 0) + { + compressedSize += len; + + target->Write(outbuf, len); + zipstream->next_out = outbuf; + zipstream->avail_out = sizeof(outbuf); + } + if (done) + { + break; + } + auto err = deflate (zipstream, Z_FINISH); + done = stream.avail_out != 0 || err == Z_STREAM_END; + if (err != Z_STREAM_END && err != Z_OK) + { + break; + } + } + deflateEnd (zipstream); + delete zipstream; + zipstream = nullptr; +} diff --git a/source/duke3d/src/savegame.cpp b/source/duke3d/src/savegame.cpp index a4b9ab68c..49d477ac7 100644 --- a/source/duke3d/src/savegame.cpp +++ b/source/duke3d/src/savegame.cpp @@ -152,14 +152,36 @@ uint16_t g_nummenusaves; static menusave_t * g_internalsaves; static uint16_t g_numinternalsaves; +static FileReader OpenSavegame(const char *fn) +{ + auto file = fopenFileReader(fn, 0); + if (!file.isOpen()) + return file; + + char buffer[13]; + file.Read(buffer, 13); + if (memcmp(buffer, "DEMOLITION_ED", 13)) + return FileReader(); + + FileReader fr; + try + { + fr.OpenDecompressor(file, file.GetLength()-13, METHOD_DEFLATE, false, nullptr); + } + catch(std::runtime_error & err) + { + Printf("%s: %s\n", fn, err.what()); + } + return fr; +} + static void ReadSaveGameHeaders_CACHE1D(TArray &saves) { savehead_t h; - for (auto &save : saves) + for (FString &save : saves) { - char const * fn = save; - auto fil = fopenFileReader(fn, 0); + auto fil = OpenSavegame(save); if (!fil.isOpen()) continue; @@ -176,7 +198,7 @@ static void ReadSaveGameHeaders_CACHE1D(TArray &saves) { if (FURY) { - FStringf extfn("%s.ext", fn); + FStringf extfn("%s.ext", save.GetChars()); auto extfil = fopenFileReader(extfn, 0); if (extfil.isOpen()) { @@ -191,7 +213,7 @@ static void ReadSaveGameHeaders_CACHE1D(TArray &saves) msv.isAutoSave = h.isAutoSave(); - strncpy(msv.brief.path, fn, ARRAY_SIZE(msv.brief.path)); + strncpy(msv.brief.path, save.GetChars(), ARRAY_SIZE(msv.brief.path)); ++g_numinternalsaves; if (k >= 0 && h.savename[0] != '\0') @@ -293,7 +315,7 @@ void ReadSaveGameHeaders(void) int32_t G_LoadSaveHeaderNew(char const *fn, savehead_t *saveh) { - auto fil = fopenFileReader(fn, 0); + auto fil = OpenSavegame(fn); if (!fil.isOpen()) return -1; @@ -342,7 +364,7 @@ int32_t G_LoadPlayer(savebrief_t & sv) int level = -1; int skill = -1; - auto fil = fopenFileReader(sv.path, 0); + auto fil = OpenSavegame(sv.path); if (fil.isOpen()) { @@ -569,7 +591,7 @@ int32_t G_LoadPlayer(savebrief_t & sv) return 0; } - auto fil = fopenFileReader(sv.path, 0); + auto fil = OpenSavegame(sv.path); if (!fil.isOpen()) return -1; @@ -762,11 +784,10 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) Bstrcpy(sv.path, fn + (fn.Len() - (ARRAY_SIZE(SaveName) - 1))); } - FileWriter fw(fil); if (!fil) { OSD_Printf("G_SavePlayer: failed opening \"%s\" for writing: %s\n", - fn, strerror(errno)); + fn.GetChars(), strerror(errno)); ready2send = 1; Net_WaitForServer(); @@ -776,6 +797,9 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) } else { + fwrite("DEMOLITION_ED", 13, 1, fil); + CompressedFileWriter fw(fil); + sv.isExt = 0; // temporary hack @@ -792,7 +816,7 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) if (!g_netServer && ud.multimode < 2) { - OSD_Printf("Saved: %s\n", fn); + OSD_Printf("Saved: %s\n", fn.GetChars()); strcpy(apStrings[QUOTE_RESERVED4], "Game Saved"); P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); } diff --git a/source/rr/src/savegame.cpp b/source/rr/src/savegame.cpp index 52f7f3961..45ec8562b 100644 --- a/source/rr/src/savegame.cpp +++ b/source/rr/src/savegame.cpp @@ -147,14 +147,37 @@ uint16_t g_nummenusaves; static menusave_t * g_internalsaves; static uint16_t g_numinternalsaves; +static FileReader OpenSavegame(const char *fn) +{ + auto file = fopenFileReader(fn, 0); + if (!file.isOpen()) + return file; + + char buffer[13]; + file.Read(buffer, 13); + if (memcmp(buffer, "DEMOLITION_RN", 13)) + return FileReader(); + + FileReader fr; + try + { + fr.OpenDecompressor(file, file.GetLength()-13, METHOD_DEFLATE|METHOD_TRANSFEROWNER, false, nullptr); + } + catch(std::runtime_error & err) + { + Printf("%s: %s\n", fn, err.what()); + } + return fr; +} + static void ReadSaveGameHeaders_CACHE1D(TArray& saves) { savehead_t h; - for (auto& save : saves) + for (FString& save : saves) { char const* fn = save; - auto fil = fopenFileReader(fn, 0); + auto fil = OpenSavegame(fn); if (!fil.isOpen()) continue; @@ -274,7 +297,7 @@ void ReadSaveGameHeaders(void) int32_t G_LoadSaveHeaderNew(char const *fn, savehead_t *saveh) { - auto fil = fopenFileReader(fn, 0); + auto fil = OpenSavegame(fn); if (!fil.isOpen()) return -1; @@ -316,7 +339,7 @@ static int different_user_map; // XXX: keyboard input 'blocked' after load fail? (at least ESC?) int32_t G_LoadPlayer(savebrief_t & sv) { - auto fil = fopenFileReader(sv.path, 0); + auto fil = OpenSavegame(sv.path); if (!fil.isOpen()) return -1; @@ -504,11 +527,10 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) Bstrcpy(sv.path, fn + (fn.Len() - (ARRAY_SIZE(SaveName) - 1))); } - FileWriter fw(fil); if (!fil) { OSD_Printf("G_SavePlayer: failed opening \"%s\" for writing: %s\n", - fn, strerror(errno)); + fn.GetChars(), strerror(errno)); ready2send = 1; Net_WaitForEverybody(); @@ -518,6 +540,9 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) } else { + fwrite("DEMOLITION_RN", 13, 1, fil); + CompressedFileWriter fw(fil); + // temporary hack ud.user_map = G_HaveUserMap(); @@ -527,19 +552,20 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) fw.Close(); - if (!g_netServer && ud.multimode < 2) - { - Bstrcpy(apStrings[QUOTE_RESERVED4], "Game Saved"); - P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); - } - - ready2send = 1; - Net_WaitForEverybody(); - - G_RestoreTimers(); - ototalclock = totalclock; - - return 0; + if (!g_netServer && ud.multimode < 2) + { + OSD_Printf("Saved: %s\n", fn.GetChars()); + Bstrcpy(apStrings[QUOTE_RESERVED4], "Game Saved"); + P_DoQuote(QUOTE_RESERVED4, g_player[myconnectindex].ps); + } + + ready2send = 1; + Net_WaitForEverybody(); + + G_RestoreTimers(); + ototalclock = totalclock; + + return 0; } } From 9aa275f99689f4367c98a2f86363dbfb0f965c63 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 8 Nov 2019 02:02:54 +0100 Subject: [PATCH 4/9] - got rid of some editor-only code and the entire buildvfs header Nearly all file write access now uses the FileWriter class, which is UTF-8-safe on all platforms - unlike stdio. --- source/audiolib/src/driver_sdl.cpp | 1 - source/blood/src/blood.cpp | 1 - source/blood/src/db.cpp | 363 ---------------------- source/blood/src/replace.cpp | 2 - source/build/include/animvpx.h | 1 - source/build/include/build.h | 6 +- source/build/include/cache1d.h | 4 - source/build/include/common.h | 1 - source/build/include/compat.h | 29 +- source/build/include/palette.h | 1 - source/build/include/vfs.h | 93 ------ source/build/src/cache1d.cpp | 3 - source/build/src/common.cpp | 16 +- source/build/src/compat.cpp | 2 - source/build/src/defs.cpp | 2 - source/build/src/engine.cpp | 274 ---------------- source/build/src/mdsprite.cpp | 2 - source/build/src/palette.cpp | 2 - source/build/src/screenshot.cpp | 34 +- source/build/src/scriptfile.cpp | 2 - source/build/src/sdlayer.cpp | 2 - source/build/src/tiles.cpp | 3 - source/build/src/voxmodel.cpp | 2 - source/common/searchpaths.cpp | 5 +- source/duke3d/src/anim.cpp | 2 - source/duke3d/src/common.cpp | 2 - source/duke3d/src/common_game.h | 2 - source/duke3d/src/config.cpp | 2 - source/duke3d/src/demo.cpp | 2 - source/duke3d/src/demo.h | 1 - source/duke3d/src/game.cpp | 35 --- source/duke3d/src/gamedef.cpp | 2 - source/duke3d/src/gameexec.cpp | 2 - source/duke3d/src/gamevars.cpp | 2 - source/duke3d/src/gamevars.h | 2 - source/duke3d/src/network.cpp | 2 - source/duke3d/src/osdcmds.cpp | 2 - source/duke3d/src/player.cpp | 6 +- source/duke3d/src/premap.cpp | 3 - source/duke3d/src/savegame.cpp | 14 +- source/duke3d/src/savegame.h | 2 - source/duke3d/src/sounds.cpp | 2 - source/libsmackerdec/include/FileStream.h | 1 - source/rr/src/demo.cpp | 3 - source/rr/src/game.cpp | 9 - source/rr/src/savegame.cpp | 8 +- source/sw/src/cheats.cpp | 2 - 47 files changed, 37 insertions(+), 922 deletions(-) diff --git a/source/audiolib/src/driver_sdl.cpp b/source/audiolib/src/driver_sdl.cpp index 2d06915c1..9e8b9d5b3 100644 --- a/source/audiolib/src/driver_sdl.cpp +++ b/source/audiolib/src/driver_sdl.cpp @@ -28,7 +28,6 @@ #include "multivoc.h" #include "mutex.h" #include "sdl_inc.h" -#include "vfs.h" enum { SDLErr_Warning = -2, diff --git a/source/blood/src/blood.cpp b/source/blood/src/blood.cpp index 4b2b445f2..f397ffaec 100644 --- a/source/blood/src/blood.cpp +++ b/source/blood/src/blood.cpp @@ -27,7 +27,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "mmulti.h" #include "compat.h" #include "renderlayer.h" -#include "vfs.h" #include "fx_man.h" #include "common.h" #include "common_game.h" diff --git a/source/blood/src/db.cpp b/source/blood/src/db.cpp index fd8c08622..22d9ac786 100644 --- a/source/blood/src/db.cpp +++ b/source/blood/src/db.cpp @@ -1278,374 +1278,11 @@ int dbLoadMap(const char *pPath, int *pX, int *pY, int *pZ, short *pAngle, short return 0; } -int dbSaveMap(const char *pPath, int nX, int nY, int nZ, short nAngle, short nSector) -{ - char sMapExt[BMAX_PATH]; - int16_t tpskyoff[256]; - int nSpriteNum; - psky_t *pSky = tileSetupSky(0); - gSkyCount = 1<lognumtiles; - gMapRev++; - nSpriteNum = 0; - strcpy(sMapExt, pPath); - ChangeExtension(sMapExt, ".MAP"); - int nSize = sizeof(MAPSIGNATURE)+sizeof(MAPHEADER); - if (byte_1A76C8) - { - nSize += sizeof(MAPHEADER2); - } - for (int i = 0; i < gSkyCount; i++) - tpskyoff[i] = pSky->tileofs[i]; - nSize += gSkyCount*sizeof(tpskyoff[0]); - nSize += sizeof(sectortype)*numsectors; - for (int i = 0; i < numsectors; i++) - { - if (sector[i].extra > 0) - { - nSize += nXSectorSize; - } - } - nSize += sizeof(walltype)*numwalls; - for (int i = 0; i < numwalls; i++) - { - if (wall[i].extra > 0) - { - nSize += nXWallSize; - } - } - for (int i = 0; i < kMaxSprites; i++) - { - if (sprite[i].statnum < kMaxStatus) - { - nSpriteNum++; - if (sprite[i].extra > 0) - { - nSize += nXSpriteSize; - } - } - } - nSize += sizeof(spritetype)*nSpriteNum; - nSize += 4; - char *pData = (char*)Xmalloc(nSize); - IOBuffer IOBuffer1 = IOBuffer(nSize, pData); - MAPSIGNATURE header; - memcpy(&header, "BLM\x1a", 4); - if (byte_1A76C8) - { - header.version = 0x700; - byte_1A76C7 = 1; - } - else - { - header.version = 0x603; - byte_1A76C7 = 0; - } - IOBuffer1.Write(&header, sizeof(header)); - MAPHEADER mapheader; - mapheader.at0 = B_LITTLE32(nX); - mapheader.at4 = B_LITTLE32(nY); - mapheader.at8 = B_LITTLE32(nZ); - mapheader.atc = B_LITTLE16(nAngle); - mapheader.ate = B_LITTLE16(nSector); - mapheader.at10 = B_LITTLE16(pSky->lognumtiles); - mapheader.at12 = B_LITTLE32(gVisibility); - if (byte_1A76C6) - { - gSongId = 0x7474614d; - } - else - { - gSongId = 0; - } - mapheader.at16 = B_LITTLE32(gSongId); - mapheader.at1a = parallaxtype; - mapheader.at1b = gMapRev; - mapheader.at1f = B_LITTLE16(numsectors); - mapheader.at21 = B_LITTLE16(numwalls); - mapheader.at23 = B_LITTLE16(nSpriteNum); - if (byte_1A76C7) - { - dbCrypt((char*)&mapheader, sizeof(MAPHEADER), 'ttaM'); - } - IOBuffer1.Write(&mapheader, sizeof(MAPHEADER)); - if (byte_1A76C8) - { - byte_19AE44.at48 = nXSectorSize; - byte_19AE44.at44 = nXWallSize; - byte_19AE44.at40 = nXSpriteSize; - dbCrypt((char*)&byte_19AE44, sizeof(MAPHEADER2), numwalls); - IOBuffer1.Write(&byte_19AE44, sizeof(MAPHEADER2)); - dbCrypt((char*)&byte_19AE44, sizeof(MAPHEADER2), numwalls); - } - if (byte_1A76C8) - { - dbCrypt((char*)tpskyoff, gSkyCount*sizeof(tpskyoff[0]), gSkyCount*sizeof(tpskyoff[0])); - } - IOBuffer1.Write(tpskyoff, gSkyCount*sizeof(tpskyoff[0])); - if (byte_1A76C8) - { - dbCrypt((char*)tpskyoff, gSkyCount*sizeof(tpskyoff[0]), gSkyCount*sizeof(tpskyoff[0])); - } - for (int i = 0; i < numsectors; i++) - { - if (byte_1A76C8) - { - dbCrypt((char*)§or[i], sizeof(sectortype), gMapRev*sizeof(sectortype)); - } - IOBuffer1.Write(§or[i], sizeof(sectortype)); - if (byte_1A76C8) - { - dbCrypt((char*)§or[i], sizeof(sectortype), gMapRev*sizeof(sectortype)); - } - if (sector[i].extra > 0) - { - char pBuffer[nXSectorSize]; - BitWriter bitWriter(pBuffer, nXSectorSize); - XSECTOR* pXSector = &xsector[sector[i].extra]; - bitWriter.write(pXSector->reference, 14); - bitWriter.write(pXSector->state, 1); - bitWriter.write(pXSector->busy, 17); - bitWriter.write(pXSector->data, 16); - bitWriter.write(pXSector->txID, 10); - bitWriter.write(pXSector->busyWaveA, 3); - bitWriter.write(pXSector->busyWaveB, 3); - bitWriter.write(pXSector->rxID, 10); - bitWriter.write(pXSector->command, 8); - bitWriter.write(pXSector->triggerOn, 1); - bitWriter.write(pXSector->triggerOff, 1); - bitWriter.write(pXSector->busyTimeA, 12); - bitWriter.write(pXSector->waitTimeA, 12); - bitWriter.write(pXSector->restState, 1); - bitWriter.write(pXSector->interruptable, 1); - bitWriter.write(pXSector->amplitude, 8); - bitWriter.write(pXSector->freq, 8); - bitWriter.write(pXSector->reTriggerA, 1); - bitWriter.write(pXSector->reTriggerB, 1); - bitWriter.write(pXSector->phase, 8); - bitWriter.write(pXSector->wave, 4); - bitWriter.write(pXSector->shadeAlways, 1); - bitWriter.write(pXSector->shadeFloor, 1); - bitWriter.write(pXSector->shadeCeiling, 1); - bitWriter.write(pXSector->shadeWalls, 1); - bitWriter.write(pXSector->shade, 8); - bitWriter.write(pXSector->panAlways, 1); - bitWriter.write(pXSector->panFloor, 1); - bitWriter.write(pXSector->panCeiling, 1); - bitWriter.write(pXSector->Drag, 1); - bitWriter.write(pXSector->Underwater, 1); - bitWriter.write(pXSector->Depth, 3); - bitWriter.write(pXSector->panVel, 8); - bitWriter.write(pXSector->panAngle, 11); - bitWriter.write(pXSector->unused1, 1); - bitWriter.write(pXSector->decoupled, 1); - bitWriter.write(pXSector->triggerOnce, 1); - bitWriter.write(pXSector->isTriggered, 1); - bitWriter.write(pXSector->Key, 3); - bitWriter.write(pXSector->Push, 1); - bitWriter.write(pXSector->Vector, 1); - bitWriter.write(pXSector->Reserved, 1); - bitWriter.write(pXSector->Enter, 1); - bitWriter.write(pXSector->Exit, 1); - bitWriter.write(pXSector->Wallpush, 1); - bitWriter.write(pXSector->color, 1); - bitWriter.write(pXSector->unused2, 1); - bitWriter.write(pXSector->busyTimeB, 12); - bitWriter.write(pXSector->waitTimeB, 12); - bitWriter.write(pXSector->stopOn, 1); - bitWriter.write(pXSector->stopOff, 1); - bitWriter.write(pXSector->ceilpal, 4); - bitWriter.write(pXSector->offCeilZ, 32); - bitWriter.write(pXSector->onCeilZ, 32); - bitWriter.write(pXSector->offFloorZ, 32); - bitWriter.write(pXSector->onFloorZ, 32); - bitWriter.write(pXSector->marker0, 16); - bitWriter.write(pXSector->marker1, 16); - bitWriter.write(pXSector->Crush, 1); - bitWriter.write(pXSector->ceilXPanFrac, 8); - bitWriter.write(pXSector->ceilYPanFrac, 8); - bitWriter.write(pXSector->floorXPanFrac, 8); - bitWriter.write(pXSector->damageType, 3); - bitWriter.write(pXSector->floorpal, 4); - bitWriter.write(pXSector->floorYPanFrac, 8); - bitWriter.write(pXSector->locked, 1); - bitWriter.write(pXSector->windVel, 10); - bitWriter.write(pXSector->windAng, 11); - bitWriter.write(pXSector->windAlways, 1); - bitWriter.write(pXSector->dudeLockout, 1); - bitWriter.write(pXSector->bobTheta, 11); - bitWriter.write(pXSector->bobZRange, 5); - bitWriter.write(pXSector->bobSpeed, 12); - bitWriter.write(pXSector->bobAlways, 1); - bitWriter.write(pXSector->bobFloor, 1); - bitWriter.write(pXSector->bobCeiling, 1); - bitWriter.write(pXSector->bobRotate, 1); - IOBuffer1.Write(pBuffer, nXSectorSize); - } - } - for (int i = 0; i < numwalls; i++) - { - if (byte_1A76C8) - { - dbCrypt((char*)&wall[i], sizeof(walltype), gMapRev*sizeof(sectortype) | 0x7474614d); - } - IOBuffer1.Write(&wall[i], sizeof(walltype)); - if (byte_1A76C8) - { - dbCrypt((char*)&wall[i], sizeof(walltype), gMapRev*sizeof(sectortype) | 0x7474614d); - } - if (wall[i].extra > 0) - { - char pBuffer[nXWallSize]; - BitWriter bitWriter(pBuffer, nXWallSize); - XWALL* pXWall = &xwall[wall[i].extra]; - bitWriter.write(pXWall->reference, 14); - bitWriter.write(pXWall->state, 1); - bitWriter.write(pXWall->busy, 17); - bitWriter.write(pXWall->data, 16); - bitWriter.write(pXWall->txID, 10); - bitWriter.write(pXWall->unused1, 6); - bitWriter.write(pXWall->rxID, 10); - bitWriter.write(pXWall->command, 8); - bitWriter.write(pXWall->triggerOn, 1); - bitWriter.write(pXWall->triggerOff, 1); - bitWriter.write(pXWall->busyTime, 12); - bitWriter.write(pXWall->waitTime, 12); - bitWriter.write(pXWall->restState, 1); - bitWriter.write(pXWall->interruptable, 1); - bitWriter.write(pXWall->panAlways, 1); - bitWriter.write(pXWall->panXVel, 8); - bitWriter.write(pXWall->panYVel, 8); - bitWriter.write(pXWall->decoupled, 1); - bitWriter.write(pXWall->triggerOnce, 1); - bitWriter.write(pXWall->isTriggered, 1); - bitWriter.write(pXWall->key, 3); - bitWriter.write(pXWall->triggerPush, 1); - bitWriter.write(pXWall->triggerVector, 1); - bitWriter.write(pXWall->triggerTouch, 1); - bitWriter.write(pXWall->unused2, 2); - bitWriter.write(pXWall->xpanFrac, 8); - bitWriter.write(pXWall->ypanFrac, 8); - bitWriter.write(pXWall->locked, 1); - bitWriter.write(pXWall->dudeLockout, 1); - bitWriter.write(pXWall->unused3, 4); - bitWriter.write(pXWall->unused4, 32); - IOBuffer1.Write(pBuffer, nXWallSize); - } - } - for (int i = 0; i < kMaxSprites; i++) - { - if (sprite[i].statnum < kMaxStatus) - { - if (byte_1A76C8) - { - dbCrypt((char*)&sprite[i], sizeof(spritetype), gMapRev*sizeof(spritetype) | 'ttaM'); - } - IOBuffer1.Write(&sprite[i], sizeof(spritetype)); - if (byte_1A76C8) - { - dbCrypt((char*)&sprite[i], sizeof(spritetype), gMapRev*sizeof(spritetype) | 'ttaM'); - } - if (sprite[i].extra > 0) - { - char pBuffer[nXSpriteSize]; - BitWriter bitWriter(pBuffer, nXSpriteSize); - XSPRITE* pXSprite = &xsprite[sprite[i].extra]; - bitWriter.write(pXSprite->reference, 14); - bitWriter.write(pXSprite->state, 1); - bitWriter.write(pXSprite->busy, 17); - bitWriter.write(pXSprite->txID, 10); - bitWriter.write(pXSprite->rxID, 10); - bitWriter.write(pXSprite->command, 8); - bitWriter.write(pXSprite->triggerOn, 1); - bitWriter.write(pXSprite->triggerOff, 1); - bitWriter.write(pXSprite->wave, 2); - bitWriter.write(pXSprite->busyTime, 12); - bitWriter.write(pXSprite->waitTime, 12); - bitWriter.write(pXSprite->restState, 1); - bitWriter.write(pXSprite->Interrutable, 1); - bitWriter.write(pXSprite->unused1, 2); - bitWriter.write(pXSprite->respawnPending, 2); - bitWriter.write(pXSprite->unused2, 1); - bitWriter.write(pXSprite->lT, 1); - bitWriter.write(pXSprite->dropMsg, 8); - bitWriter.write(pXSprite->Decoupled, 1); - bitWriter.write(pXSprite->triggerOnce, 1); - bitWriter.write(pXSprite->isTriggered, 1); - bitWriter.write(pXSprite->key, 3); - bitWriter.write(pXSprite->Push, 1); - bitWriter.write(pXSprite->Vector, 1); - bitWriter.write(pXSprite->Impact, 1); - bitWriter.write(pXSprite->Pickup, 1); - bitWriter.write(pXSprite->Touch, 1); - bitWriter.write(pXSprite->Sight, 1); - bitWriter.write(pXSprite->Proximity, 1); - bitWriter.write(pXSprite->unused3, 2); - bitWriter.write(pXSprite->lSkill, 5); - bitWriter.write(pXSprite->lS, 1); - bitWriter.write(pXSprite->lB, 1); - bitWriter.write(pXSprite->lC, 1); - bitWriter.write(pXSprite->DudeLockout, 1); - bitWriter.write(pXSprite->data1, 16); - bitWriter.write(pXSprite->data2, 16); - bitWriter.write(pXSprite->data3, 16); - bitWriter.write(pXSprite->goalAng, 11); - bitWriter.write(pXSprite->dodgeDir, 2); - bitWriter.write(pXSprite->locked, 1); - bitWriter.write(pXSprite->medium, 2); - bitWriter.write(pXSprite->respawn, 2); - bitWriter.write(pXSprite->data4, 16); - bitWriter.write(pXSprite->unused4, 6); - bitWriter.write(pXSprite->lockMsg, 8); - bitWriter.write(pXSprite->health, 12); - bitWriter.write(pXSprite->dudeDeaf, 1); - bitWriter.write(pXSprite->dudeAmbush, 1); - bitWriter.write(pXSprite->dudeGuard, 1); - bitWriter.write(pXSprite->dudeFlag4, 1); - bitWriter.write(pXSprite->target, 16); - bitWriter.write(pXSprite->targetX, 32); - bitWriter.write(pXSprite->targetY, 32); - bitWriter.write(pXSprite->targetZ, 32); - bitWriter.write(pXSprite->burnTime, 16); - bitWriter.write(pXSprite->burnSource, 16); - bitWriter.write(pXSprite->height, 16); - bitWriter.write(pXSprite->stateTimer, 16); - IOBuffer1.Write(pBuffer, nXSpriteSize); - } - } - } - unsigned int nCRC = Bcrc32(pData, nSize-4, 0); - IOBuffer1.Write(&nCRC, 4); - int nHandle = Bopen(sMapExt, BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY, BS_IREAD|BS_IWRITE); - if (nHandle == -1) - { - initprintf("Couldn't open \"%s\" for writing: %s\n", sMapExt, strerror(errno)); - Bfree(pData); - return -1; - } - if (Bwrite(nHandle, pData, nSize) != nSize) - { - initprintf("Couldn't write to \"%s\": %s\n", sMapExt, strerror(errno)); - Bclose(nHandle); - Bfree(pData); - return -1; - } - Bclose(nHandle); - Bfree(pData); - return 0; - -} - int32_t qloadboard(const char* filename, char flags, vec3_t* dapos, int16_t* daang, int16_t* dacursectnum) { // NUKE-TODO: implement flags, see mapedit.cpp return dbLoadMap(filename, &dapos->x, &dapos->y, &dapos->z, (short*)daang, (short*)dacursectnum, NULL); } -int32_t qsaveboard(const char* filename, const vec3_t* dapos, int16_t daang, int16_t dacursectnum) -{ - // NUKE-TODO: see mapedit.cpp - byte_1A76C6 = byte_1A76C8 = byte_1A76C7 = 1; - return dbSaveMap(filename, dapos->x, dapos->y, dapos->z, daang, dacursectnum); -} END_BLD_NS diff --git a/source/blood/src/replace.cpp b/source/blood/src/replace.cpp index 2a48ed420..f15ebf611 100644 --- a/source/blood/src/replace.cpp +++ b/source/blood/src/replace.cpp @@ -84,7 +84,6 @@ int32_t qdeletesprite(int16_t nSprite); int32_t qchangespritesect(int16_t nSprite, int16_t nSector); int32_t qchangespritestat(int16_t nSprite, int16_t nStatus); int32_t qloadboard(const char* filename, char flags, vec3_t* dapos, int16_t* daang, int16_t* dacursectnum); -int32_t qsaveboard(const char* filename, const vec3_t* dapos, int16_t daang, int16_t dacursectnum); void HookReplaceFunctions(void) { @@ -98,7 +97,6 @@ void HookReplaceFunctions(void) changespritestat_replace = qchangespritestat; loadvoxel_replace = qloadvoxel; loadboard_replace = qloadboard; - saveboard_replace = qsaveboard; playing_blood = true; } diff --git a/source/build/include/animvpx.h b/source/build/include/animvpx.h index 36956a2fa..1aa98eebf 100644 --- a/source/build/include/animvpx.h +++ b/source/build/include/animvpx.h @@ -33,7 +33,6 @@ typedef struct #pragma pack(pop) #ifndef ANIMVPX_STANDALONE -#include "vfs.h" extern const char *animvpx_read_ivf_header_errmsg[7]; int32_t animvpx_read_ivf_header(FileReader & inhandle, animvpx_ivf_header_t *hdr); diff --git a/source/build/include/build.h b/source/build/include/build.h index 1bee22c67..7a6357275 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -27,7 +27,6 @@ static_assert('\xff' == 255, "Char must be unsigned!"); #include "palette.h" #include "pragmas.h" -#include "vfs.h" #include "cache1d.h" #include "textures.h" #include "c_cvars.h" @@ -1020,8 +1019,8 @@ int videoCaptureScreen(); struct OutputFileCounter { uint16_t count = 0; - buildvfs_FILE opennextfile(char *, char *); - buildvfs_FILE opennextfile_withext(char *, const char *); + FileWriter *opennextfile(char *, char *); + FileWriter *opennextfile_withext(char *, const char *); }; // PLAG: line utility functions @@ -1270,7 +1269,6 @@ extern int32_t(*changespritesect_replace)(int16_t spritenum, int16_t newsectnum) extern int32_t(*changespritestat_replace)(int16_t spritenum, int16_t newstatnum); extern void(*loadvoxel_replace)(int32_t voxel); extern int32_t(*loadboard_replace)(const char *filename, char flags, vec3_t *dapos, int16_t *daang, int16_t *dacursectnum); -extern int32_t(*saveboard_replace)(const char *filename, const vec3_t *dapos, int16_t daang, int16_t dacursectnum); #ifdef USE_OPENGL extern void(*PolymostProcessVoxels_Callback)(void); #endif diff --git a/source/build/include/cache1d.h b/source/build/include/cache1d.h index ae590dcb8..0468fe660 100644 --- a/source/build/include/cache1d.h +++ b/source/build/include/cache1d.h @@ -12,12 +12,8 @@ #include "compat.h" #include "files.h" -#include "vfs.h" - void cacheAllocateBlock(intptr_t *newhandle, int32_t newbytes, uint8_t *newlockptr); -using buildvfs_kfd = int32_t; - extern int32_t pathsearchmode; // 0 = gamefs mode (default), 1 = localfs mode (editor's mode) #include "filesystem/filesystem.h" diff --git a/source/build/include/common.h b/source/build/include/common.h index 1f6cc8452..c91407641 100644 --- a/source/build/include/common.h +++ b/source/build/include/common.h @@ -8,7 +8,6 @@ #define EDUKE32_COMMON_H_ #include "cache1d.h" -#include "vfs.h" #include "compat.h" #include "pragmas.h" // klabs #include "scriptfile.h" diff --git a/source/build/include/compat.h b/source/build/include/compat.h index dbc9bf4e4..7e60e6747 100644 --- a/source/build/include/compat.h +++ b/source/build/include/compat.h @@ -438,8 +438,7 @@ defined __x86_64__ || defined __amd64__ || defined _M_X64 || defined _M_IA64 || #ifdef _MSC_VER # if defined _M_AMD64 || defined _M_ARM64 || defined _M_X64 || defined _WIN64 -// should be int64_t, if not for a suspected VS compiler bug -typedef int32_t ssize_t; +typedef int64_t ssize_t; # else typedef int32_t ssize_t; # endif @@ -479,31 +478,7 @@ typedef FILE BFILE; #define Bcalloc calloc #define Brealloc realloc #define Bfree free -#define Bopen open -#define Bclose close -#define Bwrite write -#define Bread read -#define Blseek lseek -#define Bstat stat -#define Bfstat fstat -#define Bfileno fileno -#define Bferror ferror -#define Bfopen fopen -#define Bfclose fclose -#define Bfflush fflush -#define Bfeof feof -#define Bfgetc fgetc -#define Brewind rewind -#define Bfgets fgets -#define Bfputc fputc -#define Bfputs fputs -#define Bfread fread -#define Bfwrite fwrite -#define Bfprintf fprintf -#define Bfscanf fscanf -#define Bfseek fseek -#define Bftell ftell -#define Bputs puts + #define Bstrcpy strcpy #define Bstrncpy strncpy #define Bstrcmp strcmp diff --git a/source/build/include/palette.h b/source/build/include/palette.h index 30e485547..9eb43f818 100644 --- a/source/build/include/palette.h +++ b/source/build/include/palette.h @@ -12,7 +12,6 @@ #define palette_h_ #include "cache1d.h" -#include "vfs.h" #define MAXBASEPALS 256 #define MAXPALOOKUPS 256 diff --git a/source/build/include/vfs.h b/source/build/include/vfs.h index b6d04f282..8b1378917 100644 --- a/source/build/include/vfs.h +++ b/source/build/include/vfs.h @@ -1,94 +1 @@ -#pragma once - -#ifndef vfs_h_ -#define vfs_h_ - -#include "compat.h" - - -#include -#include -#include -#ifdef _WIN32 -# include -#endif - -using buildvfs_FILE = FILE *; -#define buildvfs_EOF EOF -#define buildvfs_fread(p, s, n, fp) fread((p), (s), (n), (fp)) -#define buildvfs_fwrite(p, s, n, fp) fwrite((p), (s), (n), (fp)) -#define buildvfs_fopen_read(fn) fopen((fn), "rb") -#define buildvfs_fopen_write(fn) fopen((fn), "wb") -#define buildvfs_fopen_write_text(fn) fopen((fn), "w") -#define buildvfs_fopen_append(fn) fopen((fn), "ab") -#define buildvfs_fgetc(fp) fgetc(fp) -#define buildvfs_fputc(c, fp) fputc((c), (fp)) -#define buildvfs_fgets(str, size, fp) fgets((str), (size), (fp)) -#define buildvfs_fclose(fp) fclose(fp) -#define buildvfs_feof(fp) feof(fp) -#define buildvfs_ftell(fp) ftell(fp) -#define buildvfs_fseek_abs(fp, o) fseek((fp), (o), SEEK_SET) -#define buildvfs_fseek_rel(fp, o) fseek((fp), (o), SEEK_CUR) -#define buildvfs_rewind(fp) rewind(fp) - -static inline int64_t buildvfs_length(int fd) -{ -#ifdef _WIN32 - return filelength(fd); -#else - struct stat st; - return fstat(fd, &st) < 0 ? -1 : st.st_size; -#endif -} - -#define buildvfs_getcwd(buf, size) getcwd((buf), (size)) - -using buildvfs_fd = int; -#define buildvfs_fd_invalid (-1) -#define buildvfs_read(fd, p, s) read((fd), (p), (s)) -#define buildvfs_write(fd, p, s) write((fd), (p), (s)) -#define buildvfs_open_read(fn) open((fn), O_RDONLY) -#define buildvfs_open_write(fn) open((fn), O_BINARY|O_TRUNC|O_CREAT|O_WRONLY, S_IREAD|S_IWRITE) -// #define buildvfs_open_append(fn) todo(fn) -#define buildvfs_close(fd) close(fd) - -static inline int64_t buildvfs_flength(FILE * f) -{ -#ifdef _WIN32 - return filelength(_fileno(f)); -#else - return buildvfs_length(fileno(f)); -#endif -} -#define buildvfs_exists(fn) (access((fn), 0) == 0) -static inline int buildvfs_isdir(char const *path) -{ - struct Bstat st; - return (Bstat(path, &st) ? 0 : (st.st_mode & S_IFDIR) == S_IFDIR); -} -#define buildvfs_unlink(path) unlink(path) - - - -#define MAYBE_FCLOSE_AND_NULL(fileptr) do { \ - if (fileptr) { buildvfs_fclose(fileptr); fileptr = buildvfs_FILE{}; } \ -} while (0) - -static inline void buildvfs_fputstrptr(buildvfs_FILE fp, char const * str) -{ - buildvfs_fwrite(str, 1, strlen(str), fp); -} - -static inline void buildvfs_fputs(char const * str, buildvfs_FILE fp) -{ - buildvfs_fwrite(str, 1, strlen(str), fp); -} - -template -static inline void buildvfs_fputstr(buildvfs_FILE fp, char const (&str)[N]) -{ - buildvfs_fwrite(&str, 1, N-1, fp); -} - -#endif // vfs_h_ diff --git a/source/build/src/cache1d.cpp b/source/build/src/cache1d.cpp index 272ed90be..9b4dae789 100644 --- a/source/build/src/cache1d.cpp +++ b/source/build/src/cache1d.cpp @@ -17,9 +17,6 @@ #include "pragmas.h" #include "baselayer.h" -#include "vfs.h" - - uint8_t toupperlookup[256] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, diff --git a/source/build/src/common.cpp b/source/build/src/common.cpp index ab248f2e2..4333aab95 100644 --- a/source/build/src/common.cpp +++ b/source/build/src/common.cpp @@ -7,7 +7,6 @@ #include "common.h" -#include "vfs.h" #include "../../glbackend/glbackend.h" // def/clipmap handling @@ -293,16 +292,15 @@ static char* KeyValues_FindKeyValue(char **vdfbuf, char * const vdfbufend, const void Paths_ParseSteamKeyValuesForPaths(const char *vdf, SteamPathParseFunc func) { - buildvfs_fd fd = buildvfs_open_read(vdf); - int32_t size = buildvfs_length(fd); - char *vdfbufstart, *vdfbuf, *vdfbufend; + FileReader fr = fopenFileReader(vdf, 0); + auto size = fr.GetLength(); + char *vdfbuf, *vdfbufend; - if (size <= 0) + if (size == 0) return; - vdfbufstart = vdfbuf = (char*)Xmalloc(size); - size = (int32_t)buildvfs_read(fd, vdfbuf, size); - buildvfs_close(fd); + auto vdfbuffer = fr.ReadPadded(1); + vdfbuf = (char*)vdfbuffer.Data(); vdfbufend = vdfbuf + size; if (KeyValues_FindParentKey(&vdfbuf, vdfbufend, "LibraryFolders")) @@ -312,6 +310,4 @@ void Paths_ParseSteamKeyValuesForPaths(const char *vdf, SteamPathParseFunc func) while ((result = KeyValues_FindKeyValue(&vdfbuf, vdfbufend, NULL)) != NULL) func(result); } - - Xfree(vdfbufstart); } diff --git a/source/build/src/compat.cpp b/source/build/src/compat.cpp index 085ef2077..fdcffc559 100644 --- a/source/build/src/compat.cpp +++ b/source/build/src/compat.cpp @@ -31,8 +31,6 @@ #include "baselayer.h" -#include "vfs.h" - ////////// PANICKING ALLOCATION FUNCTIONS ////////// static void (*g_MemErrHandler)(int32_t line, const char *file, const char *func); diff --git a/source/build/src/defs.cpp b/source/build/src/defs.cpp index f91098025..85f759bc2 100644 --- a/source/build/src/defs.cpp +++ b/source/build/src/defs.cpp @@ -23,8 +23,6 @@ # include "hightile.h" #endif -#include "vfs.h" - enum scripttoken_t { T_INCLUDE = 0, diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index 1b24bbcf8..16072345a 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -33,9 +33,6 @@ #include "../../glbackend/glbackend.h" #endif - -#include "vfs.h" - ////////// // Compilation switches for optional/extended engine features @@ -10019,208 +10016,6 @@ static int32_t get_mapversion(void) return 7; } -// -// saveboard -// -int32_t(*saveboard_replace)(const char *filename, const vec3_t *dapos, int16_t daang, int16_t dacursectnum) = NULL; -int32_t saveboard(const char *filename, const vec3_t *dapos, int16_t daang, int16_t dacursectnum) -{ - if (saveboard_replace) - return saveboard_replace(filename, dapos, daang, dacursectnum); - - int16_t numsprites, ts; - int32_t i, j, tl; - - // First, some checking. - for (j=0; j MAXSTATUS) - { - initprintf("Map error: sprite #%d(%d,%d) with an illegal statnum(%d)\n", - j,TrackerCast(sprite[j].x),TrackerCast(sprite[j].y),TrackerCast(sprite[j].statnum)); - changespritestat(j,0); - } - - if ((unsigned)sprite[j].sectnum > MAXSECTORS) - { - initprintf("Map error: sprite #%d(%d,%d) with an illegal sectnum(%d)\n", - j,TrackerCast(sprite[j].x),TrackerCast(sprite[j].y),TrackerCast(sprite[j].sectnum)); - changespritesect(j,0); - } - } - - // Count the number of sprites. - numsprites = 0; - for (j=0; jx); buildvfs_write(fil,&tl,4); - tl = B_LITTLE32(dapos->y); buildvfs_write(fil,&tl,4); - tl = B_LITTLE32(dapos->z); buildvfs_write(fil,&tl,4); - ts = B_LITTLE16(daang); buildvfs_write(fil,&ts,2); - ts = B_LITTLE16(dacursectnum); buildvfs_write(fil,&ts,2); - - ts = B_LITTLE16(numsectors); buildvfs_write(fil,&ts,2); - - while (1) // if, really - { - usectortypev7 *const tsect = (usectortypev7 *)Xmalloc(sizeof(usectortypev7) * numsectors); - uwalltypev7 *twall; - -#ifdef NEW_MAP_FORMAT - for (i=0; iwallptr = B_LITTLE16(sec->wallptr); - sec->wallnum = B_LITTLE16(sec->wallnum); - sec->ceilingz = B_LITTLE32(sec->ceilingz); - sec->floorz = B_LITTLE32(sec->floorz); - sec->ceilingstat = B_LITTLE16(sec->ceilingstat); - sec->floorstat = B_LITTLE16(sec->floorstat); - sec->ceilingpicnum = B_LITTLE16(sec->ceilingpicnum); - sec->ceilingheinum = B_LITTLE16(sec->ceilingheinum); - sec->floorpicnum = B_LITTLE16(sec->floorpicnum); - sec->floorheinum = B_LITTLE16(sec->floorheinum); - sec->lotag = B_LITTLE16(sec->lotag); - sec->hitag = B_LITTLE16(sec->hitag); - sec->extra = B_LITTLE16(sec->extra); -#ifdef YAX_ENABLE__COMPAT - if (editstatus == 0) - { - // if in-game, pack game-time bunchnum data back into structs - int32_t cf, bn; - - for (cf=0; cf<2; cf++) - if ((bn=yax_getbunch(i, cf)) >= 0) - YAX_PTRBUNCHNUM(tsect, i, cf) = bn; - } -#endif - } - - buildvfs_write(fil, tsect, sizeof(sectortypev7)*numsectors); - Xfree(tsect); - - ts = B_LITTLE16(numwalls); - buildvfs_write(fil,&ts,2); - - twall = (uwalltypev7 *)Xmalloc(sizeof(uwalltypev7) * numwalls); - -#ifdef NEW_MAP_FORMAT - for (i=0; ix = B_LITTLE32(wal->x); - wal->y = B_LITTLE32(wal->y); - wal->point2 = B_LITTLE16(wal->point2); - wal->nextwall = B_LITTLE16(wal->nextwall); - wal->nextsector = B_LITTLE16(wal->nextsector); - wal->cstat = B_LITTLE16(wal->cstat); - wal->picnum = B_LITTLE16(wal->picnum); - wal->overpicnum = B_LITTLE16(wal->overpicnum); -#ifdef YAX_ENABLE__COMPAT - if (editstatus == 0) - { - // if in-game, pack game-time yax-nextwall data back into structs - int16_t ynw; - if ((ynw=yax_getnextwall(i, YAX_CEILING))>=0) - YAX_PTRNEXTWALL(twall,i,YAX_CEILING) = ynw; - if ((ynw=yax_getnextwall(i, YAX_FLOOR))>=0) - YAX_PTRNEXTWALL(twall,i,YAX_FLOOR) = ynw; - } -#endif - wal->lotag = B_LITTLE16(wal->lotag); - wal->hitag = B_LITTLE16(wal->hitag); - wal->extra = B_LITTLE16(wal->extra); - } - - buildvfs_write(fil, twall, sizeof(walltypev7)*numwalls); - Xfree(twall); - - ts = B_LITTLE16(numsprites); buildvfs_write(fil,&ts,2); - - if (numsprites > 0) - { - auto const tspri = (uspritetype *)Xmalloc(sizeof(spritetype) * numsprites); - auto spri = tspri; - - for (j=0; jx = B_LITTLE32(spri->x); - spri->y = B_LITTLE32(spri->y); - spri->z = B_LITTLE32(spri->z); - spri->cstat = B_LITTLE16(spri->cstat); - spri->picnum = B_LITTLE16(spri->picnum); - spri->sectnum = B_LITTLE16(spri->sectnum); - spri->statnum = B_LITTLE16(spri->statnum); - spri->ang = B_LITTLE16(spri->ang); - spri->owner = B_LITTLE16(spri->owner); - spri->xvel = B_LITTLE16(spri->xvel); - spri->yvel = B_LITTLE16(spri->yvel); - spri->zvel = B_LITTLE16(spri->zvel); - spri->lotag = B_LITTLE16(spri->lotag); - spri->hitag = B_LITTLE16(spri->hitag); - spri->extra = B_LITTLE16(spri->extra); - spri++; - } - } - - buildvfs_write(fil, tspri, sizeof(spritetype)*numsprites); - Xfree(tspri); - } - - buildvfs_close(fil); - return 0; - } - - buildvfs_close(fil); - return -1; -} #define YSAVES ((xdim*MAXSPRITES)>>7) @@ -11659,75 +11454,6 @@ void rotatepoint(vec2_t const pivot, vec2_t p, int16_t const daang, vec2_t * con p2->y = dmulscale14(p.y, dacos, p.x, dasin) + pivot.y; } - - -#if KRANDDEBUG -# include -# define KRD_MAXCALLS 262144 -# define KRD_DEPTH 8 -static int32_t krd_numcalls=0; -static void *krd_fromwhere[KRD_MAXCALLS][KRD_DEPTH]; -static int32_t krd_enabled=0; - -void krd_enable(int which) // 0: disable, 1: rec, 2: play -{ - krd_enabled = which; - - if (which) - Bmemset(krd_fromwhere, 0, sizeof(krd_fromwhere)); -} - -int32_t krd_print(const char *filename) -{ - buildvfs_FILE fp; - int32_t i, j; - - if (!krd_enabled) return 1; - krd_enabled = 0; - - fp = buildvfs_fopen_write(filename); - if (!fp) { OSD_Printf("krd_print (2): fopen"); return 1; } - - for (i=0; i=KRD_DEPTH || krd_fromwhere[i][j]==NULL) - { - fprintf(fp, "\n"); - break; - } - fprintf(fp, " [%p]", krd_fromwhere[i][j]); - } - } - - krd_numcalls = 0; - - buildvfs_fclose(fp); - return 0; -} -#endif // KRANDDEBUG - -#if KRANDDEBUG || defined LUNATIC -// -// krand -// -int32_t krand(void) -{ -// randomseed = (randomseed*27584621)+1; - randomseed = (randomseed * 1664525ul) + 221297ul; -#ifdef KRANDDEBUG - if (krd_enabled) - if (krd_numcalls < KRD_MAXCALLS) - { - backtrace(krd_fromwhere[krd_numcalls], KRD_DEPTH); - krd_numcalls++; - } -#endif - return ((uint32_t)randomseed)>>16; -} -#endif - int32_t setaspect_new_use_dimen = 0; void videoSetCorrectedAspect() diff --git a/source/build/src/mdsprite.cpp b/source/build/src/mdsprite.cpp index 695be96a1..5fd32bd5d 100644 --- a/source/build/src/mdsprite.cpp +++ b/source/build/src/mdsprite.cpp @@ -17,8 +17,6 @@ #include "bitmap.h" #include "../../glbackend/glbackend.h" -#include "vfs.h" - static int32_t curextra=MAXTILES; #define MIN_CACHETIME_PRINT 10 diff --git a/source/build/src/palette.cpp b/source/build/src/palette.cpp index 4b64771c0..ad2955f9f 100644 --- a/source/build/src/palette.cpp +++ b/source/build/src/palette.cpp @@ -18,8 +18,6 @@ #include "common.h" #include "../../glbackend/glbackend.h" -#include "vfs.h" - uint8_t *basepaltable[MAXBASEPALS] = { palette }; uint8_t basepalreset=1; uint8_t curbasepal; diff --git a/source/build/src/screenshot.cpp b/source/build/src/screenshot.cpp index 973077093..3a6de4d2e 100644 --- a/source/build/src/screenshot.cpp +++ b/source/build/src/screenshot.cpp @@ -9,7 +9,6 @@ #include "gamecontrol.h" #include "printf.h" -#include "vfs.h" #include "../../glbackend/glbackend.h" EXTERN_CVAR(Float, png_gamma) @@ -17,7 +16,7 @@ EXTERN_CVAR(Float, png_gamma) // screencapture // -buildvfs_FILE OutputFileCounter::opennextfile(char *fn, char *zeros) +FileWriter *OutputFileCounter::opennextfile(char *fn, char *zeros) { do // JBF 2004022: So we don't overwrite existing screenshots { @@ -27,25 +26,18 @@ buildvfs_FILE OutputFileCounter::opennextfile(char *fn, char *zeros) zeros[1] = ((count/100)%10)+'0'; zeros[2] = ((count/10)%10)+'0'; zeros[3] = (count%10)+'0'; -#ifdef USE_PHYSFS - buildvfs_FILE file; - if ((file = buildvfs_fopen_read(fn)) == nullptr) break; - buildvfs_fclose(file); -#else - struct Bstat st; - if (Bstat(fn, &st) == -1) break; -#endif + if (!FileExists(fn)) break; count++; } while (1); - return buildvfs_fopen_write(fn); + return FileWriter::Open(fn); } -buildvfs_FILE OutputFileCounter::opennextfile_withext(char *fn, const char *ext) +FileWriter *OutputFileCounter::opennextfile_withext(char *fn, const char *ext) { char *dot = strrchr(fn, '.'); strcpy(dot+1, ext); - return opennextfile(fn, dot-4); + return opennextfile(fn, dot-4); } static OutputFileCounter capturecounter; @@ -111,20 +103,18 @@ int videoCaptureScreen() else autoname << currentGame; autoname << "_0000"; char* fn = autoname.LockBuffer(); - buildvfs_FILE fp = capturecounter.opennextfile_withext(fn, "png"); + FileWriter *fil = capturecounter.opennextfile_withext(fn, "png"); autoname.UnlockBuffer(); - if (fp == nullptr) + if (fil == nullptr) { return -1; } - FileWriter writer(fp); uint8_t * const imgBuf = (uint8_t *) Xmalloc(xdim * ydim * (HICOLOR ? 3 : 1)); videoBeginDrawing(); //{{{ -#ifdef USE_OPENGL if (HICOLOR) { getScreen(imgBuf); @@ -135,15 +125,14 @@ int videoCaptureScreen() for (int i = 0, numRows = ydim >> 1; i < numRows; ++i) { - Bmemcpy(rowBuf, imgBuf + i * bytesPerLine, bytesPerLine); - Bmemcpy(imgBuf + i * bytesPerLine, imgBuf + (ydim - i - 1) * bytesPerLine, bytesPerLine); - Bmemcpy(imgBuf + (ydim - i - 1) * bytesPerLine, rowBuf, bytesPerLine); + memcpy(rowBuf, imgBuf + i * bytesPerLine, bytesPerLine); + memcpy(imgBuf + i * bytesPerLine, imgBuf + (ydim - i - 1) * bytesPerLine, bytesPerLine); + memcpy(imgBuf + (ydim - i - 1) * bytesPerLine, rowBuf, bytesPerLine); } Xfree(rowBuf); } else -#endif { for (bssize_t i = 0; i < 256; ++i) { @@ -158,7 +147,8 @@ int videoCaptureScreen() videoEndDrawing(); //}}} - WritePNGfile(&writer, imgBuf, Palette, HICOLOR ? SS_RGB : SS_PAL, xdim, ydim, HICOLOR? xdim*3 : xdim, png_gamma); + WritePNGfile(fil, imgBuf, Palette, HICOLOR ? SS_RGB : SS_PAL, xdim, ydim, HICOLOR? xdim*3 : xdim, png_gamma); + delete fil; Xfree(imgBuf); Printf("Saved screenshot to %s\n", fn); capturecounter.count++; diff --git a/source/build/src/scriptfile.cpp b/source/build/src/scriptfile.cpp index aa9b0245d..dd1fa5f88 100644 --- a/source/build/src/scriptfile.cpp +++ b/source/build/src/scriptfile.cpp @@ -11,8 +11,6 @@ #include "compat.h" #include "cache1d.h" -#include "vfs.h" - #define ISWS(x) ((x == ' ') || (x == '\t') || (x == '\r') || (x == '\n')) static inline void skipoverws(scriptfile *sf) { if ((sf->textptr < sf->eof) && (!sf->textptr[0])) sf->textptr++; } diff --git a/source/build/src/sdlayer.cpp b/source/build/src/sdlayer.cpp index ee3d60f08..1eb29c789 100644 --- a/source/build/src/sdlayer.cpp +++ b/source/build/src/sdlayer.cpp @@ -54,8 +54,6 @@ # include "win32/winbits.h" #endif -#include "vfs.h" - CVAR(Int, r_displayindex, 0, CVAR_ARCHIVE | CVAR_VIDEOCONFIG) CVAR(Int, r_borderless, 2, CVAR_ARCHIVE | CVAR_VIDEOCONFIG) CVAR(Int, maxrefreshfreq, 0, CVAR_ARCHIVE | CVAR_VIDEOCONFIG) diff --git a/source/build/src/tiles.cpp b/source/build/src/tiles.cpp index d53892232..09b301025 100644 --- a/source/build/src/tiles.cpp +++ b/source/build/src/tiles.cpp @@ -12,9 +12,6 @@ #include "engine_priv.h" #include "cache1d.h" -#include "vfs.h" - - // // copytilepiece // diff --git a/source/build/src/voxmodel.cpp b/source/build/src/voxmodel.cpp index c03b83f4e..0d3c2a5bb 100644 --- a/source/build/src/voxmodel.cpp +++ b/source/build/src/voxmodel.cpp @@ -14,8 +14,6 @@ #include "palette.h" #include "../../glbackend/glbackend.h" -#include "vfs.h" - //For loading/conversion only static vec3_t voxsiz; diff --git a/source/common/searchpaths.cpp b/source/common/searchpaths.cpp index 9fd01954a..e7cc03255 100644 --- a/source/common/searchpaths.cpp +++ b/source/common/searchpaths.cpp @@ -1123,12 +1123,11 @@ const char* G_ConFile(void) bool AddINIFile(const char* pzFile, bool bForce = false) { char* pzFN; - struct Bstat st; static INICHAIN* pINIIter = NULL; if (!bForce) { if (findfrompath(pzFile, &pzFN)) return false; // failed to resolve the filename - if (Bstat(pzFN, &st)) + if (!FileExists(pzFN)) { Bfree(pzFN); return false; @@ -1185,4 +1184,4 @@ void ScanINIFiles(void) break; } } -#endif \ No newline at end of file +#endif diff --git a/source/duke3d/src/anim.cpp b/source/duke3d/src/anim.cpp index 8d733abba..a0a0e2e02 100644 --- a/source/duke3d/src/anim.cpp +++ b/source/duke3d/src/anim.cpp @@ -36,8 +36,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # include "animvpx.h" #endif -#include "vfs.h" - BEGIN_DUKE_NS diff --git a/source/duke3d/src/common.cpp b/source/duke3d/src/common.cpp index 754834fa0..74f0e56ad 100644 --- a/source/duke3d/src/common.cpp +++ b/source/duke3d/src/common.cpp @@ -13,8 +13,6 @@ #include "rts.h" #include "gamecontrol.h" -#include "vfs.h" - #include "common.h" #include "common_game.h" diff --git a/source/duke3d/src/common_game.h b/source/duke3d/src/common_game.h index b0a58ac6e..0416c554c 100644 --- a/source/duke3d/src/common_game.h +++ b/source/duke3d/src/common_game.h @@ -10,8 +10,6 @@ #include "collections.h" #include "gamecontrol.h" -#include "vfs.h" - BEGIN_DUKE_NS diff --git a/source/duke3d/src/config.cpp b/source/duke3d/src/config.cpp index 7e4d9f34f..3aaa33a6d 100644 --- a/source/duke3d/src/config.cpp +++ b/source/duke3d/src/config.cpp @@ -27,8 +27,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "renderlayer.h" #include "cmdline.h" -#include "vfs.h" - #if defined RENDERTYPESDL && defined SDL_TARGET && SDL_TARGET > 1 # include "sdl_inc.h" #endif diff --git a/source/duke3d/src/demo.cpp b/source/duke3d/src/demo.cpp index 2e1ebf159..723f7aba5 100644 --- a/source/duke3d/src/demo.cpp +++ b/source/duke3d/src/demo.cpp @@ -31,8 +31,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "i_specialpaths.h" #include "printf.h" -#include "vfs.h" - BEGIN_DUKE_NS diff --git a/source/duke3d/src/demo.h b/source/duke3d/src/demo.h index 8e9c2b3ad..d01128835 100644 --- a/source/duke3d/src/demo.h +++ b/source/duke3d/src/demo.h @@ -24,7 +24,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define demo_h_ #include "compat.h" -#include "vfs.h" #include "cache1d.h" BEGIN_DUKE_NS diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index ef0730eb4..8a21166f2 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -46,8 +46,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "m_argv.h" #include "filesystem/filesystem.h" -#include "vfs.h" - // Uncomment to prevent anything except mirrors from drawing. It is sensible to // also uncomment ENGINE_CLEAR_SCREEN in build/src/engine_priv.h. //#define DEBUG_MIRRORS_ONLY @@ -296,9 +294,6 @@ void G_GameExit(const char *msg) I_Error("%s", msg); } } - - Bfflush(NULL); - throw ExitEvent(0); } @@ -1168,10 +1163,8 @@ void G_DrawRooms(int32_t playerNum, int32_t smoothRatio) void G_DumpDebugInfo(void) { -#if !defined LUNATIC static char const s_WEAPON[] = "WEAPON"; int32_t i,j,x; - // buildvfs_FILE fp = buildvfs_fopen_write("condebug.log"); VM_ScriptInfo(insptr, 64); buildprint("\nCurrent gamevar values:\n"); @@ -1235,17 +1228,12 @@ void G_DumpDebugInfo(void) } } Gv_DumpValues(); -// buildvfs_fclose(fp); -#endif - saveboard("debug.map", &g_player[myconnectindex].ps->pos, fix16_to_int(g_player[myconnectindex].ps->q16ang), - g_player[myconnectindex].ps->cursectnum); } // if is true, set the moveflag unconditionally, // else only if it equals 0. static int32_t G_InitActor(int32_t i, int32_t tilenum, int32_t set_movflag_uncond) { -#if !defined LUNATIC if (g_tile[tilenum].execPtr) { SH(i) = *(g_tile[tilenum].execPtr); @@ -1257,26 +1245,6 @@ static int32_t G_InitActor(int32_t i, int32_t tilenum, int32_t set_movflag_uncon return 1; } -#else - if (El_HaveActor(tilenum)) - { - // ^^^ C-CON takes precedence for now. - const el_actor_t *a = &g_elActors[tilenum]; - auto movflagsptr = &AC_MOVFLAGS(&sprite[i], &actor[i]); - - SH(i) = a->strength; - AC_ACTION_ID(actor[i].t_data) = a->act.id; - AC_MOVE_ID(actor[i].t_data) = a->mov.id; - Bmemcpy(&actor[i].ac, &a->act.ac, sizeof(struct action)); - Bmemcpy(&actor[i].mv, &a->mov.mv, sizeof(struct move)); - - if (set_movflag_uncond || *movflagsptr == 0) - *movflagsptr = a->movflags; - - return 1; - } -#endif - return 0; } @@ -5712,7 +5680,6 @@ void G_Shutdown(void) CONTROL_Shutdown(); engineUnInit(); G_Cleanup(); - Bfflush(NULL); } /* @@ -5995,8 +5962,6 @@ static void G_Startup(void) G_LoadLookups(); screenpeek = myconnectindex; - - Bfflush(NULL); } static void P_SetupMiscInputSettings(void) diff --git a/source/duke3d/src/gamedef.cpp b/source/duke3d/src/gamedef.cpp index 3b6ba716b..c4093393d 100644 --- a/source/duke3d/src/gamedef.cpp +++ b/source/duke3d/src/gamedef.cpp @@ -37,8 +37,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "printf.h" #include "m_argv.h" -#include "vfs.h" - BEGIN_DUKE_NS #define LINE_NUMBER (g_lineNumber << 12) diff --git a/source/duke3d/src/gameexec.cpp b/source/duke3d/src/gameexec.cpp index f394f87cf..dd99b204a 100644 --- a/source/duke3d/src/gameexec.cpp +++ b/source/duke3d/src/gameexec.cpp @@ -38,8 +38,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "base64.h" #include "version.h" -#include "vfs.h" - #include "debugbreak.h" BEGIN_DUKE_NS diff --git a/source/duke3d/src/gamevars.cpp b/source/duke3d/src/gamevars.cpp index 75f05f97b..7bbda3cd6 100644 --- a/source/duke3d/src/gamevars.cpp +++ b/source/duke3d/src/gamevars.cpp @@ -27,8 +27,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "savegame.h" #include "gamecvars.h" -#include "vfs.h" - BEGIN_DUKE_NS #define gamevars_c_ diff --git a/source/duke3d/src/gamevars.h b/source/duke3d/src/gamevars.h index 42761cf83..66d0b8e22 100644 --- a/source/duke3d/src/gamevars.h +++ b/source/duke3d/src/gamevars.h @@ -26,8 +26,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "fix16.hpp" #include "gamedef.h" -#include "vfs.h" - BEGIN_DUKE_NS diff --git a/source/duke3d/src/network.cpp b/source/duke3d/src/network.cpp index 5ad00ef05..550005252 100644 --- a/source/duke3d/src/network.cpp +++ b/source/duke3d/src/network.cpp @@ -41,8 +41,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "enet.h" #include "m_crc32.h" -#include "vfs.h" - BEGIN_DUKE_NS // Data needed even if netcode is disabled diff --git a/source/duke3d/src/osdcmds.cpp b/source/duke3d/src/osdcmds.cpp index d32febc05..e9e512ed1 100644 --- a/source/duke3d/src/osdcmds.cpp +++ b/source/duke3d/src/osdcmds.cpp @@ -32,8 +32,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "savegame.h" #include "sbar.h" -#include "vfs.h" - BEGIN_DUKE_NS diff --git a/source/duke3d/src/player.cpp b/source/duke3d/src/player.cpp index 88d7ae4b4..18636bc64 100644 --- a/source/duke3d/src/player.cpp +++ b/source/duke3d/src/player.cpp @@ -5729,15 +5729,15 @@ int portableBackupSave(const char * path, const char * name, int volume, int lev char * encoded = sjson_stringify(ctx, root, " "); - buildvfs_FILE fil = buildvfs_fopen_write(fn); + FileWriter *fil = FileWriter::Open(fn); if (!fil) { sjson_destroy_context(ctx); return 1; } - buildvfs_fwrite(encoded, strlen(encoded), 1, fil); - buildvfs_fclose(fil); + fil->Write(encoded, strlen(encoded)); + delete fil; sjson_free_string(ctx, encoded); sjson_destroy_context(ctx); diff --git a/source/duke3d/src/premap.cpp b/source/duke3d/src/premap.cpp index 76e774ce5..49d5e366a 100644 --- a/source/duke3d/src/premap.cpp +++ b/source/duke3d/src/premap.cpp @@ -28,9 +28,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "duke3d.h" #include "menus.h" #include "savegame.h" - -#include "vfs.h" - BEGIN_DUKE_NS static uint8_t precachehightile[2][(MAXTILES+7)>>3]; diff --git a/source/duke3d/src/savegame.cpp b/source/duke3d/src/savegame.cpp index 49d477ac7..2b137d24c 100644 --- a/source/duke3d/src/savegame.cpp +++ b/source/duke3d/src/savegame.cpp @@ -31,8 +31,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "gamecontrol.h" #include "version.h" -#include "vfs.h" - BEGIN_DUKE_NS @@ -717,9 +715,9 @@ void G_DeleteSave(savebrief_t const & sv) return; } - buildvfs_unlink(temp); + remove(temp); Bstrcat(temp, ".ext"); - buildvfs_unlink(temp); + remove(temp); } void G_DeleteOldSaves(void) @@ -763,12 +761,12 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) FString fn; errno = 0; - buildvfs_FILE fil; + FileWriter *fil; if (sv.isValid()) { fn.Format("%s%s", M_GetSavegamesPath().GetChars(), sv.path); - fil = fopen(fn, "wb"); + fil = FileWriter::Open(fn); } else { @@ -797,8 +795,8 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) } else { - fwrite("DEMOLITION_ED", 13, 1, fil); - CompressedFileWriter fw(fil); + fil->Write("DEMOLITION_ED", 13); + CompressedFileWriter fw(fil, true); sv.isExt = 0; diff --git a/source/duke3d/src/savegame.h b/source/duke3d/src/savegame.h index 165a38c0d..d66e837f9 100644 --- a/source/duke3d/src/savegame.h +++ b/source/duke3d/src/savegame.h @@ -25,8 +25,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "game.h" -#include "vfs.h" - BEGIN_DUKE_NS diff --git a/source/duke3d/src/sounds.cpp b/source/duke3d/src/sounds.cpp index c69034008..74e561b9e 100644 --- a/source/duke3d/src/sounds.cpp +++ b/source/duke3d/src/sounds.cpp @@ -30,8 +30,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "openaudio.h" #include -#include "vfs.h" - BEGIN_DUKE_NS diff --git a/source/libsmackerdec/include/FileStream.h b/source/libsmackerdec/include/FileStream.h index f9ef83c23..3f00eac1a 100644 --- a/source/libsmackerdec/include/FileStream.h +++ b/source/libsmackerdec/include/FileStream.h @@ -21,7 +21,6 @@ #define _SmackerFileStream_h_ #include -#include "vfs.h" #include "compat.h" #include #include "files.h" diff --git a/source/rr/src/demo.cpp b/source/rr/src/demo.cpp index 15ff12524..706f7a8e1 100644 --- a/source/rr/src/demo.cpp +++ b/source/rr/src/demo.cpp @@ -30,9 +30,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "screens.h" #include "i_specialpaths.h" -#include "vfs.h" - - BEGIN_RR_NS char g_firstDemoFile[BMAX_PATH]; diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index ad1961488..70d448aca 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -394,9 +394,6 @@ void G_GameExit(const char *msg) I_Error(msg); } } - - Bfflush(NULL); - throw ExitEvent(0); } @@ -1573,9 +1570,6 @@ void G_DumpDebugInfo(void) j = nextspritestat[j]; } } -// fclose(fp); - saveboard("debug.map", &g_player[myconnectindex].ps->pos, fix16_to_int(g_player[myconnectindex].ps->q16ang), - g_player[myconnectindex].ps->cursectnum); } // if is true, set the moveflag unconditionally, @@ -7079,7 +7073,6 @@ void G_Shutdown(void) G_SetFog(0); engineUnInit(); G_Cleanup(); - Bfflush(NULL); } /* @@ -7393,8 +7386,6 @@ static void G_Startup(void) G_LoadLookups(); screenpeek = myconnectindex; - - Bfflush(NULL); } static void P_SetupMiscInputSettings(void) diff --git a/source/rr/src/savegame.cpp b/source/rr/src/savegame.cpp index 45ec8562b..9d274b2ab 100644 --- a/source/rr/src/savegame.cpp +++ b/source/rr/src/savegame.cpp @@ -506,12 +506,12 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) FString fn; errno = 0; - buildvfs_FILE fil; + FileWriter *fil; if (sv.isValid()) { fn.Format("%s%s", M_GetSavegamesPath().GetChars(), sv.path); - fil = fopen(fn, "wb"); + fil = FileWriter::Open(fn); } else { @@ -540,8 +540,8 @@ int32_t G_SavePlayer(savebrief_t & sv, bool isAutoSave) } else { - fwrite("DEMOLITION_RN", 13, 1, fil); - CompressedFileWriter fw(fil); + fil->Write("DEMOLITION_RN", 13); + CompressedFileWriter fw(fil, true); // temporary hack ud.user_map = G_HaveUserMap(); diff --git a/source/sw/src/cheats.cpp b/source/sw/src/cheats.cpp index 3ee777e7c..fb6dc1c20 100644 --- a/source/sw/src/cheats.cpp +++ b/source/sw/src/cheats.cpp @@ -250,8 +250,6 @@ void EveryCheatToggle(PLAYERp pp, char *cheat_string) void SaveCheat(PLAYERp pp, char *UNUSED(cheat_string)) { - saveboard("swsave.map", (vec3_t *)pp, - pp->pang, pp->cursectnum); } void GeorgeFunc(PLAYERp pp, char *UNUSED(cheat_string)) From f52a58d9ac946236d28511c59a48f0e2eeaf165e Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 8 Nov 2019 17:41:19 +0100 Subject: [PATCH 5/9] - fixed compilation. --- source/common/utility/files.h | 45 ++++++++++++---------- source/common/utility/files_decompress.cpp | 4 +- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/source/common/utility/files.h b/source/common/utility/files.h index d9eb00b1c..a332ea2dd 100644 --- a/source/common/utility/files.h +++ b/source/common/utility/files.h @@ -59,6 +59,8 @@ enum METHOD_TRANSFEROWNER = 0x8000, }; +class FileReader; + class FileReaderInterface { public: @@ -72,27 +74,6 @@ public: long GetLength () const { return Length; } }; -class DecompressorBase : public FileReaderInterface -{ - std::function ErrorCallback = nullptr; -public: - // These do not work but need to be defined to satisfy the FileReaderInterface. - // They will just error out when called. - long Tell() const override; - long Seek(long offset, int origin) override; - char *Gets(char *strbuf, int len) override; - void DecompressionError(const char* error, ...) const; - void SetErrorCallback(const std::function& cb) - { - ErrorCallback = cb; - } - void SetOwnsReader(); - -protected: - FileReader *File = nullptr; - FileReader OwnedFile; -}; - class MemoryReader : public FileReaderInterface { protected: @@ -307,6 +288,28 @@ public: friend class FWadCollection; }; +class DecompressorBase : public FileReaderInterface +{ + std::function ErrorCallback = nullptr; +public: + // These do not work but need to be defined to satisfy the FileReaderInterface. + // They will just error out when called. + long Tell() const override; + long Seek(long offset, int origin) override; + char* Gets(char* strbuf, int len) override; + void DecompressionError(const char* error, ...) const; + void SetErrorCallback(const std::function& cb) + { + ErrorCallback = cb; + } + void SetOwnsReader(); + +protected: + FileReader* File = nullptr; + FileReader OwnedFile; +}; + + diff --git a/source/common/utility/files_decompress.cpp b/source/common/utility/files_decompress.cpp index 7136620e6..3f89c8a4f 100644 --- a/source/common/utility/files_decompress.cpp +++ b/source/common/utility/files_decompress.cpp @@ -568,7 +568,7 @@ class DecompressorLZSS : public DecompressorBase } public: - DecompressorLZSS(FileReader *file, const std::function& cb) : File(file), SawEOF(false) + DecompressorLZSS(FileReader *file, const std::function& cb) : SawEOF(false) { File = file; SetErrorCallback(cb); @@ -782,7 +782,7 @@ void CompressedFileWriter::Close() break; } auto err = deflate (zipstream, Z_FINISH); - done = stream.avail_out != 0 || err == Z_STREAM_END; + done = zipstream->avail_out != 0 || err == Z_STREAM_END; if (err != Z_STREAM_END && err != Z_OK) { break; From 125ec7ad2cef5b67b83e12dc85a04643e89d67fd Mon Sep 17 00:00:00 2001 From: hendricks266 Date: Fri, 8 Nov 2019 04:22:58 +0000 Subject: [PATCH 6/9] SW: Replace MAXLONG with INT32_MAX git-svn-id: https://svn.eduke32.com/eduke32@8281 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/sw/src/break.cpp | 6 +++--- source/sw/src/game.h | 2 +- source/sw/src/morph.cpp | 4 ++-- source/sw/src/sector.cpp | 6 +++--- source/sw/src/sync.cpp | 2 +- source/sw/src/track.cpp | 18 +++++++++--------- source/sw/src/weapon.cpp | 4 ++-- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/source/sw/src/break.cpp b/source/sw/src/break.cpp index 9d60bf756..916076ef0 100644 --- a/source/sw/src/break.cpp +++ b/source/sw/src/break.cpp @@ -597,7 +597,7 @@ int AutoBreakWall(WALLp wallp, int hit_x, int hit_y, int hit_z, short ang, short // Check to see if it should break with current weapon type if (!CheckBreakToughness(break_info, type)) return FALSE; - if (hit_x != MAXLONG) + if (hit_x != INT32_MAX) { vec3_t hit_pos = { hit_x, hit_y, hit_z }; // need correct location for spawning shrap @@ -811,7 +811,7 @@ int WallBreakPosition(short hit_wall, short *sectnum, int *x, int *y, int *z, sh updatesectorz(*x,*y,*z,sectnum); if (*sectnum < 0) { - *x = MAXLONG; // don't spawn shrap, just change wall + *x = INT32_MAX; // don't spawn shrap, just change wall return FALSE; } @@ -830,7 +830,7 @@ SWBOOL HitBreakWall(WALLp wp, int hit_x, int hit_y, int hit_z, short ang, short return TRUE; } - //if (hit_x == MAXLONG) + //if (hit_x == INT32_MAX) { short sectnum; WallBreakPosition(wp - wall, §num, &hit_x, &hit_y, &hit_z, &ang); diff --git a/source/sw/src/game.h b/source/sw/src/game.h index ed577269e..7f6b65ba3 100644 --- a/source/sw/src/game.h +++ b/source/sw/src/game.h @@ -2269,7 +2269,7 @@ extern SWBOOL NightVision; int _PlayerSound(const char *file, int line, int num, int *x, int *y, int *z, Voc3D_Flags flags, PLAYERp pp); #define PlayerSound(num, x, y, z, flags, pp) _PlayerSound(__FILE__, __LINE__, (num), (x), (y), (z), (flags), (pp)) -#define MAXSO (MAXLONG) +#define MAXSO (INT32_MAX) /////////////////////////////////////////////////////////////// // diff --git a/source/sw/src/morph.cpp b/source/sw/src/morph.cpp index 0e858ebeb..4915d3938 100644 --- a/source/sw/src/morph.cpp +++ b/source/sw/src/morph.cpp @@ -47,7 +47,7 @@ DoSectorObjectSetScale(short match) for (sop = SectorObject; sop < &SectorObject[MAX_SECTOR_OBJECTS]; sop++) { - if (sop->xmid == MAXLONG) + if (sop->xmid == INT32_MAX) continue; if (sop->match_event == match) @@ -122,7 +122,7 @@ DoSOevent(short match, short state) for (sop = SectorObject; sop < &SectorObject[MAX_SECTOR_OBJECTS]; sop++) { - if (sop->xmid == MAXLONG) + if (sop->xmid == INT32_MAX) continue; if (sop->match_event == match) diff --git a/source/sw/src/sector.cpp b/source/sw/src/sector.cpp index ea9e41c8c..2b62da57e 100644 --- a/source/sw/src/sector.cpp +++ b/source/sw/src/sector.cpp @@ -496,7 +496,7 @@ SectorSetup(void) SectorObject[ndx].Animator = NULL; SectorObject[ndx].controller = NULL; SectorObject[ndx].sp_child = NULL; - SectorObject[ndx].xmid = MAXLONG; + SectorObject[ndx].xmid = INT32_MAX; } memset(SineWaveFloor, -1, sizeof(SineWaveFloor)); @@ -1451,7 +1451,7 @@ DoSectorObjectKillMatch(short match) for (sop = SectorObject; sop < &SectorObject[MAX_SECTOR_OBJECTS]; sop++) { - if (sop->xmid == MAXLONG) + if (sop->xmid == INT32_MAX) continue; if (sop->match_event == match) @@ -3419,7 +3419,7 @@ DoSector(void) for (sop = SectorObject; sop < &SectorObject[MAX_SECTOR_OBJECTS]; sop++) { - if (sop->xmid == MAXLONG /*|| sop->xmid == MAXSO*/) + if (sop->xmid == INT32_MAX /*|| sop->xmid == MAXSO*/) continue; diff --git a/source/sw/src/sync.cpp b/source/sw/src/sync.cpp index 8302a169a..63ff219ee 100644 --- a/source/sw/src/sync.cpp +++ b/source/sw/src/sync.cpp @@ -109,7 +109,7 @@ SOSync(void) for (sop = SectorObject; sop < &SectorObject[MAX_SECTOR_OBJECTS]; sop++) { - // if (sop->xmid == MAXLONG) + // if (sop->xmid == INT32_MAX) // continue; updatecrc(crc, (sop->xmid) & 255); diff --git a/source/sw/src/track.cpp b/source/sw/src/track.cpp index 5bcc85dfa..b3f5255ef 100644 --- a/source/sw/src/track.cpp +++ b/source/sw/src/track.cpp @@ -1428,7 +1428,7 @@ PostSetupSectorObject(void) for (sop = SectorObject; sop < &SectorObject[MAX_SECTOR_OBJECTS]; sop++) { - if (sop->xmid == MAXLONG) + if (sop->xmid == INT32_MAX) continue; FindMainSector(sop); } @@ -1442,7 +1442,7 @@ PlayerOnObject(short sectnum_match) SECTOR_OBJECTp sop; // place each sector object on the track - //for (i = 0; (SectorObject[i].xmid != MAXLONG) && (i < MAX_SECTOR_OBJECTS); i++) + //for (i = 0; (SectorObject[i].xmid != INT32_MAX) && (i < MAX_SECTOR_OBJECTS); i++) for (i = 0; (i < MAX_SECTOR_OBJECTS); i++) { sop = &SectorObject[i]; @@ -1477,7 +1477,7 @@ PlaceSectorObjectsOnTracks(void) TRACK_POINTp tpoint = NULL; short spnum, next_spnum; - if (sop->xmid == MAXLONG) + if (sop->xmid == INT32_MAX) continue; @@ -1693,14 +1693,14 @@ MovePoints(SECTOR_OBJECTp sop, short delta_ang, int nx, int ny) short i, nexti, rot_ang; SWBOOL PlayerMove = TRUE; - if (sop->xmid >= (int)MAXSO) + if (sop->xmid >= MAXSO) PlayerMove = FALSE; // move along little midpoint sop->xmid += BOUND_4PIX(nx); sop->ymid += BOUND_4PIX(ny); - if (sop->xmid >= (int)MAXSO) + if (sop->xmid >= MAXSO) PlayerMove = FALSE; // move child sprite along also @@ -1882,7 +1882,7 @@ PlayerPart: // Does not necessarily move with the sector so must accout for // moving across sectors - if (sop->xmid < (int)MAXSO) // special case for operating SO's + if (sop->xmid < MAXSO) // special case for operating SO's setspritez(sop->sp_num[i], (vec3_t *)sp); } @@ -1902,7 +1902,7 @@ PlayerPart: // update here AFTER sectors/player has been manipulated // prevents you from falling into map HOLEs created by moving // Sectors and sprites around. - //if (sop->xmid < (int)MAXSO) + //if (sop->xmid < MAXSO) COVERupdatesector(pp->posx, pp->posy, &pp->cursectnum); // in case you are in a whirlpool @@ -2077,7 +2077,7 @@ DetectSectorObject(SECTORp sectph) // move all points to nx,ny for (sop = SectorObject; sop < &SectorObject[MAX_SECTOR_OBJECTS]; sop++) { - if (sop->xmid == MAXLONG /*|| sop->xmid == MAXSO*/) + if (sop->xmid == INT32_MAX /*|| sop->xmid == MAXSO*/) continue; for (sectp = sop->sectp, j = 0; *sectp; sectp++, j++) @@ -2105,7 +2105,7 @@ DetectSectorObjectByWall(WALLp wph) // move all points to nx,ny for (sop = SectorObject; sop < &SectorObject[MAX_SECTOR_OBJECTS]; sop++) { - if (sop->xmid == MAXLONG /*|| sop->xmid == MAXSO*/) + if (sop->xmid == INT32_MAX /*|| sop->xmid == MAXSO*/) continue; for (sectp = sop->sectp, j = 0; *sectp; sectp++, j++) diff --git a/source/sw/src/weapon.cpp b/source/sw/src/weapon.cpp index d1b089480..90178d946 100644 --- a/source/sw/src/weapon.cpp +++ b/source/sw/src/weapon.cpp @@ -7762,10 +7762,10 @@ void TraverseBreakableWalls(short start_sect, int x, int y, int z, short ang, in if (WallBreakPosition(j, §num, &hit_x, &hit_y, &hit_z, &wall_ang)) { - if (hit_x != MAXLONG && sectnum >= 0 && FAFcansee(x, y, z, start_sect, hit_x, hit_y, hit_z, sectnum)) + if (hit_x != INT32_MAX && sectnum >= 0 && FAFcansee(x, y, z, start_sect, hit_x, hit_y, hit_z, sectnum)) { //HitBreakWall(&wall[j], x, y, z, ang, 0); - HitBreakWall(&wall[j], MAXLONG, MAXLONG, MAXLONG, ang, 0); + HitBreakWall(&wall[j], INT32_MAX, INT32_MAX, INT32_MAX, ang, 0); break_count++; if (break_count > 4) From fbb75aae0bd0a6c4a0edef2b2d91e53a45a3f190 Mon Sep 17 00:00:00 2001 From: hendricks266 Date: Fri, 8 Nov 2019 04:23:02 +0000 Subject: [PATCH 7/9] SW: Replace UCHAR with unsigned char git-svn-id: https://svn.eduke32.com/eduke32@8282 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/sw/src/menus.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/sw/src/menus.cpp b/source/sw/src/menus.cpp index e9be2cc84..fa3ba7491 100644 --- a/source/sw/src/menus.cpp +++ b/source/sw/src/menus.cpp @@ -4424,7 +4424,7 @@ SetupMenu(void) void MNU_DoMenu(CTLType type, PLAYERp pp) { SWBOOL resetitem; - UCHAR key; + unsigned char key; int zero = 0; static int handle2 = 0; static int limitmove=0; From dc10a8b03d81049247afe9bd270e93c7df65eb46 Mon Sep 17 00:00:00 2001 From: hendricks266 Date: Fri, 8 Nov 2019 04:23:06 +0000 Subject: [PATCH 8/9] SW, KenBuild: Replace strupr with Bstrupr git-svn-id: https://svn.eduke32.com/eduke32@8283 1a8010ca-5511-0410-912e-c29ae57300e0 # Conflicts: # source/kenbuild/src/kdmeng.cpp # source/sw/src/bldscript.cpp # source/sw/src/jbhlp.cpp --- source/sw/src/cheats.cpp | 2 +- source/sw/src/scrip2.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/sw/src/cheats.cpp b/source/sw/src/cheats.cpp index fb6dc1c20..3c3f6e655 100644 --- a/source/sw/src/cheats.cpp +++ b/source/sw/src/cheats.cpp @@ -424,7 +424,7 @@ void CheatInput(void) } // make sure string is lower cased - strlwr(CheatInputString); + Bstrlwr(CheatInputString); // check for at least one single match for (i = 0; i < SIZ(ci); i++) diff --git a/source/sw/src/scrip2.cpp b/source/sw/src/scrip2.cpp index cc17198dd..16b1bd7a9 100644 --- a/source/sw/src/scrip2.cpp +++ b/source/sw/src/scrip2.cpp @@ -99,7 +99,7 @@ SWBOOL LoadScriptFile(const char *filename) // Convert filebuffer to all upper case - //strupr(scriptbuffer); + //Bstrupr(scriptbuffer); script_p = scriptbuffer; scriptend_p = script_p + size; From d3e6807f615d1c2b7740f1e1f53cd28cc18657dc Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 8 Nov 2019 17:50:32 +0100 Subject: [PATCH 9/9] - fixed infinite loop in compression code. --- source/common/utility/files_decompress.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/common/utility/files_decompress.cpp b/source/common/utility/files_decompress.cpp index 3f89c8a4f..c6707a283 100644 --- a/source/common/utility/files_decompress.cpp +++ b/source/common/utility/files_decompress.cpp @@ -732,7 +732,7 @@ size_t CompressedFileWriter::Write(const void *buffer, size_t bytes) size_t towrite = bytes; zipstream->next_in = (Bytef *)buffer; - while (bytes > 0) + while (towrite > 0) { auto chunk = std::min(towrite, (size_t)0x40000000); zipstream->avail_in = chunk;