From 220d86a583132d3e2b896d1a4ddb2f62d08d2ee3 Mon Sep 17 00:00:00 2001 From: terminx Date: Fri, 28 Sep 2018 04:27:48 +0000 Subject: [PATCH] Fix savegame crash issue git-svn-id: https://svn.eduke32.com/eduke32@6983 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/duke3d/src/duke3d.h | 4 +- source/duke3d/src/gamedef.cpp | 2 - source/duke3d/src/gamedef.h | 2 - source/duke3d/src/savegame.cpp | 85 ++++++++++++---------------------- 4 files changed, 31 insertions(+), 62 deletions(-) diff --git a/source/duke3d/src/duke3d.h b/source/duke3d/src/duke3d.h index cfae8925a..e9e8abcde 100644 --- a/source/duke3d/src/duke3d.h +++ b/source/duke3d/src/duke3d.h @@ -58,10 +58,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // increase by 3, because atomic GRP adds 1, and Shareware adds 2 #ifdef LUNATIC // Lunatic -# define BYTEVERSION_EDUKE32 330 +# define BYTEVERSION_EDUKE32 333 #else // Non-Lua build -# define BYTEVERSION_EDUKE32 330 +# define BYTEVERSION_EDUKE32 333 #endif //#define BYTEVERSION_13 27 diff --git a/source/duke3d/src/gamedef.cpp b/source/duke3d/src/gamedef.cpp index e64fcc6a1..a325143e2 100644 --- a/source/duke3d/src/gamedef.cpp +++ b/source/duke3d/src/gamedef.cpp @@ -2672,7 +2672,6 @@ LUNATIC_EXTERN int32_t C_SetDefName(const char *name) } defaultprojectile_t DefaultProjectile; -int32_t g_numProjectiles = 0; EDUKE32_STATIC_ASSERT(sizeof(projectile_t) == sizeof(DefaultProjectile)); @@ -2695,7 +2694,6 @@ LUNATIC_EXTERN void C_DefineProjectile(int32_t j, int32_t what, int32_t val) { C_AllocProjectile(j); *g_tile[j].proj = DefaultProjectile; - g_numProjectiles += 2; } projectile_t * const proj = g_tile[j].proj; diff --git a/source/duke3d/src/gamedef.h b/source/duke3d/src/gamedef.h index 6f36cfc25..2bd5b60c6 100644 --- a/source/duke3d/src/gamedef.h +++ b/source/duke3d/src/gamedef.h @@ -162,8 +162,6 @@ void C_FreeProjectile(int32_t j); void C_InitQuotes(void); void C_InitProjectiles(void); -extern int32_t g_numProjectiles; - typedef struct { int spriteNum; int playerNum; diff --git a/source/duke3d/src/savegame.cpp b/source/duke3d/src/savegame.cpp index e688485d3..76f3cd56e 100644 --- a/source/duke3d/src/savegame.cpp +++ b/source/duke3d/src/savegame.cpp @@ -1097,10 +1097,7 @@ static uint32_t calcsz(const dataspec_t *spec) static void sv_prespriteextsave(); static void sv_postspriteext(); #endif -#if !defined LUNATIC -static void sv_prescriptsave_once(); -static void sv_postscript_once(); -#else +#if defined LUNATIC // Recreate Lua state. // XXX: It may matter a great deal when this is run from if the Lua code refers // to C-side data at file scope. Such usage is strongly discouraged though. @@ -1127,7 +1124,9 @@ static void sv_postprojectilesave(); static void sv_preprojectileload(); static void sv_postprojectileload(); -static projectile_t *ProjectileData; +static projectile_t *savegame_projectiledata; +static uint8_t savegame_projectiles[MAXSPRITES>>3]; +static int32_t savegame_projectilecnt = 0; #define SVARDATALEN \ ((sizeof(g_player[0].user_name)+sizeof(g_player[0].pcolor)+sizeof(g_player[0].pteam) \ @@ -1242,18 +1241,12 @@ static char svgm_script_string [] = "blK:scri"; static const dataspec_t svgm_script[] = { { DS_STRING, (void *)svgm_script_string, 0, 1 }, -#if !defined LUNATIC - { DS_SAVEFN|DS_NOCHK, (void *)&sv_prescriptsave_once, 0, 1 }, -#endif { DS_SAVEFN, (void *) &sv_preprojectilesave, 0, 1 }, + { DS_NOCHK, &savegame_projectiles, sizeof(savegame_projectiles), 1 }, { DS_LOADFN, (void *) &sv_preprojectileload, 0, 1 }, - { DS_NOCHK, &g_tile[0], sizeof(tiledata_t), MAXTILES }, - { DS_DYNAMIC|DS_CNT(g_numProjectiles), &ProjectileData, sizeof(projectile_t), (intptr_t)&g_numProjectiles }, + { DS_DYNAMIC|DS_CNT(savegame_projectilecnt), &savegame_projectiledata, sizeof(projectile_t), (intptr_t)&savegame_projectilecnt }, { DS_SAVEFN, (void *) &sv_postprojectilesave, 0, 1 }, { DS_LOADFN, (void *) &sv_postprojectileload, 0, 1 }, -#if !defined LUNATIC - { DS_SAVEFN|DS_LOADFN|DS_NOCHK, (void *)&sv_postscript_once, 0, 1 }, -#endif { 0, &actor[0], sizeof(actor_t), MAXSPRITES }, { DS_SAVEFN|DS_LOADFN, (void *)&sv_postactordata, 0, 1 }, #if defined LUNATIC @@ -1802,20 +1795,6 @@ void sv_postyaxload(void) } #endif -#if !defined LUNATIC -static void sv_prescriptsave_once() -{ - G_Util_PtrToIdx2(&g_tile[0].execPtr, MAXTILES, sizeof(tiledata_t), apScript, P2I_FWD_NON0); - G_Util_PtrToIdx2(&g_tile[0].loadPtr, MAXTILES, sizeof(tiledata_t), apScript, P2I_FWD_NON0); -} - -static void sv_postscript_once() -{ - G_Util_PtrToIdx2(&g_tile[0].execPtr, MAXTILES, sizeof(tiledata_t), apScript, P2I_BACK_NON0); - G_Util_PtrToIdx2(&g_tile[0].loadPtr, MAXTILES, sizeof(tiledata_t), apScript, P2I_BACK_NON0); -} -#endif - static void sv_postactordata() { #ifdef POLYMER @@ -1869,26 +1848,25 @@ static void sv_quoteload() static void sv_preprojectilesave() { - if (ProjectileData != NULL || g_numProjectiles > 0) - ProjectileData = (projectile_t *) Xrealloc(ProjectileData, sizeof(projectile_t) * g_numProjectiles); -#ifdef DEBUGGINGAIDS - int onumprojectiles = g_numProjectiles; -#endif - g_numProjectiles = 0; + savegame_projectilecnt = 0; + + for (bssize_t i=0; i 0) + savegame_projectiledata = (projectile_t *) Xrealloc(savegame_projectiledata, sizeof(projectile_t) * savegame_projectilecnt); + + int32_t cnt = 0; for (bssize_t i=0; i>3] |= 1<<(i&7); + Bmemcpy(&savegame_projectiledata[cnt++], g_tile[i].proj, sizeof(projectile_t)); } } - -#ifdef DEBUGGINGAIDS - Bassert(g_numProjectiles == onumprojectiles); -#endif } static void sv_postprojectilesave() @@ -1898,35 +1876,30 @@ static void sv_postprojectilesave() static void sv_preprojectileload() { - if (ProjectileData != NULL || g_numProjectiles > 0) - ProjectileData = (projectile_t *) Xrealloc(ProjectileData, sizeof(projectile_t) * g_numProjectiles); + int32_t cnt = 0; for (bssize_t i=0; i>3]&(1<<(i&7))) + cnt++; + } + + if (savegame_projectiledata != NULL || cnt > 0) + savegame_projectiledata = (projectile_t *) Xrealloc(savegame_projectiledata, sizeof(projectile_t) * cnt); } static void sv_postprojectileload() { -#ifdef DEBUGGINGAIDS - int onumprojectiles = g_numProjectiles; -#endif - g_numProjectiles = 0; + int32_t cnt = 0; for (bssize_t i=0; i>3]&(1<<(i&7))) { C_AllocProjectile(i); - Bmemcpy(g_tile[i].proj, &ProjectileData[g_numProjectiles], sizeof(projectile_t)); - Bmemcpy(g_tile[i].defproj, &ProjectileData[g_numProjectiles+1], sizeof(projectile_t)); - g_numProjectiles += 2; + Bmemcpy(g_tile[i].proj, &savegame_projectiledata[cnt++], sizeof(projectile_t)); } } - -#ifdef DEBUGGINGAIDS - Bassert(g_numProjectiles == onumprojectiles); -#endif - // DO_FREE_AND_NULL(ProjectileData); }