diff --git a/source/duke3d/src/gameexec.cpp b/source/duke3d/src/gameexec.cpp index 37030b3ca..60d2971b4 100644 --- a/source/duke3d/src/gameexec.cpp +++ b/source/duke3d/src/gameexec.cpp @@ -4636,22 +4636,17 @@ finish_qsprintf: if (kFile < 0) continue; - int numElements = kfilelength(kFile) / Gv_GetArrayElementSize(arrayNum); + int const numElements = Gv_GetArrayElementSize(arrayNum) ? kfilelength(kFile) / Gv_GetArrayElementSize(arrayNum) : 1; - if (numElements == 0) + if (numElements > 0) { - Baligned_free(aGameArrays[arrayNum].pValues); - aGameArrays[arrayNum].pValues = NULL; - aGameArrays[arrayNum].size = numElements; - } - else if (numElements > 0) - { - int const numBytes = numElements * Gv_GetArrayElementSize(arrayNum); + int numBytes = numElements * Gv_GetArrayElementSize(arrayNum); Baligned_free(aGameArrays[arrayNum].pValues); - aGameArrays[arrayNum].pValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, numElements * Gv_GetArrayElementSize(arrayNum)); - aGameArrays[arrayNum].size = numElements; + aGameArrays[arrayNum].size = numBytes ? numElements : kfilelength(kFile); + aGameArrays[arrayNum].pValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, Gv_GetArrayAllocSize(arrayNum)); + numBytes = Gv_GetArrayAllocSize(arrayNum); switch (aGameArrays[arrayNum].flags & GAMEARRAY_TYPE_MASK) { @@ -4672,10 +4667,8 @@ finish_qsprintf: case GAMEARRAY_INT32: case GAMEARRAY_INT16: case GAMEARRAY_UINT8: - kread(kFile, aGameArrays[arrayNum].pValues, numBytes); - break; case GAMEARRAY_BITMAP: - kread(kFile, aGameArrays[arrayNum].pValues, (numElements + 7) >> 3); + kread(kFile, aGameArrays[arrayNum].pValues, numBytes); break; } } @@ -4713,7 +4706,7 @@ finish_qsprintf: } int const numElements = aGameArrays[arrayNum].size; - int const numBytes = numElements * Gv_GetArrayElementSize(arrayNum); + int const numBytes = Gv_GetArrayAllocSize(arrayNum); switch (aGameArrays[arrayNum].flags & GAMEARRAY_TYPE_MASK) { @@ -4733,10 +4726,9 @@ finish_qsprintf: case GAMEARRAY_INT32: case GAMEARRAY_INT16: case GAMEARRAY_UINT8: + case GAMEARRAY_BITMAP: Bfwrite(aGameArrays[arrayNum].pValues, 1, numBytes, fil); break; - case GAMEARRAY_BITMAP: - Bfwrite(aGameArrays[arrayNum].pValues, 1, (numElements + 7) >> 3, fil); } Bfclose(fil); @@ -6066,20 +6058,10 @@ void G_SaveMapState(void) if ((aGameArrays[i].flags & GAMEARRAY_RESTORE) == 0) continue; - int size; - - switch (aGameArrays[i].flags & (GAMEARRAY_UINT8 | GAMEARRAY_INT16 | GAMEARRAY_INT32)) - { - case GAMEARRAY_UINT8: size = sizeof(uint8_t); break; - case GAMEARRAY_INT16: size = sizeof(uint16_t); break; - case GAMEARRAY_INT32: size = sizeof(uint32_t); break; - default: size = sizeof(uintptr_t); break; - } - if (!save->arrays[i]) - save->arrays[i] = (intptr_t *)Xaligned_alloc(16, aGameArrays[i].size * size); + save->arrays[i] = (intptr_t *)Xaligned_alloc(16, Gv_GetArrayAllocSize(i)); - Bmemcpy(&save->arrays[i][0], &aGameArrays[i].pValues[0], aGameArrays[i].size * size); + Bmemcpy(&save->arrays[i][0], &aGameArrays[i].pValues[0], Gv_GetArrayAllocSize(i)); } #else int32_t slen; @@ -6207,17 +6189,7 @@ void G_RestoreMapState(void) if ((aGameArrays[i].flags & GAMEARRAY_RESTORE) == 0 || !pSavedState->arrays[i]) continue; - int size; - - switch (aGameArrays[i].flags & (GAMEARRAY_UINT8 | GAMEARRAY_INT16 | GAMEARRAY_INT32)) - { - case GAMEARRAY_UINT8: size = sizeof(uint8_t); break; - case GAMEARRAY_INT16: size = sizeof(uint16_t); break; - case GAMEARRAY_INT32: size = sizeof(uint32_t); break; - default: size = sizeof(uintptr_t); break; - } - - Bmemcpy(&aGameArrays[i].pValues[0], &pSavedState->arrays[i][0], aGameArrays[i].size * size); + Bmemcpy(&aGameArrays[i].pValues[0], &pSavedState->arrays[i][0], Gv_GetArrayAllocSize(i)); } #else if (pSavedState->savecode) diff --git a/source/duke3d/src/gameexec.h b/source/duke3d/src/gameexec.h index 3cdbbfacc..bcfd48d64 100644 --- a/source/duke3d/src/gameexec.h +++ b/source/duke3d/src/gameexec.h @@ -100,12 +100,12 @@ static FORCE_INLINE int32_t VM_OnEvent(int nEventID, int spriteNum, int playerNu } #define CON_ERRPRINTF(Text, ...) do { \ - OSD_Printf("Line %d, %s: %s", Text, g_errorLineNum, keyw[g_tw], ## __VA_ARGS__); \ + OSD_Printf("Line %d, %s: " Text, g_errorLineNum, keyw[g_tw], ## __VA_ARGS__); \ } while (0) #define CON_CRITICALERRPRINTF(Text, ...) do { \ - OSD_Printf("Line %d, %s: %s", Text, g_errorLineNum, keyw[g_tw], ## __VA_ARGS__); \ - wm_msgbox(APPNAME, "Line %d, %s: %s", Text, g_errorLineNum, keyw[g_tw], ## __VA_ARGS__); \ + OSD_Printf("Line %d, %s: " Text, g_errorLineNum, keyw[g_tw], ## __VA_ARGS__); \ + wm_msgbox(APPNAME, "Line %d, %s: " Text, g_errorLineNum, keyw[g_tw], ## __VA_ARGS__); \ } while (0) void G_GetTimeDate(int32_t * const pValues); diff --git a/source/duke3d/src/gamevars.cpp b/source/duke3d/src/gamevars.cpp index cf9980b64..c63bddfa2 100644 --- a/source/duke3d/src/gamevars.cpp +++ b/source/duke3d/src/gamevars.cpp @@ -177,10 +177,11 @@ int Gv_ReadSave(int32_t kFile) hash_add(&h_arrays, aGameArrays[i].szLabel, i, 1); intptr_t const asize = aGameArrays[i].size; + if (asize != 0) { - aGameArrays[i].pValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, asize * Gv_GetArrayElementSize(i)); - if (kdfread(aGameArrays[i].pValues, Gv_GetArrayElementSize(i) * aGameArrays[i].size, 1, kFile) < 1) goto corrupt; + aGameArrays[i].pValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, Gv_GetArrayAllocSize(i)); + if (kdfread(aGameArrays[i].pValues, Gv_GetArrayAllocSize(i), 1, kFile) < 1) goto corrupt; } else aGameArrays[i].pValues = NULL; @@ -287,7 +288,7 @@ void Gv_WriteSave(FILE *fil) dfwrite(&aGameArrays[i],sizeof(gamearray_t),1,fil); dfwrite(aGameArrays[i].szLabel,sizeof(uint8_t) * MAXARRAYLABEL, 1, fil); - dfwrite(aGameArrays[i].pValues, Gv_GetArrayElementSize(i) * aGameArrays[i].size, 1, fil); + dfwrite(aGameArrays[i].pValues, Gv_GetArrayAllocSize(i), 1, fil); } dfwrite(apScriptEvents,sizeof(apScriptEvents),1,fil); @@ -584,6 +585,14 @@ int __fastcall Gv_GetArrayElementSize(int const arrayIdx) return typeSize; } +int __fastcall Gv_GetArrayAllocSize(int const arrayIdx) +{ + if (aGameArrays[arrayIdx].flags & GAMEARRAY_BITMAP) + return (aGameArrays[arrayIdx].size + 7) >> 3; + + return aGameArrays[arrayIdx].size * Gv_GetArrayElementSize(arrayIdx); +} + int __fastcall Gv_GetArrayValue(int const id, int index) { if (aGameArrays[id].flags & GAMEARRAY_STRIDE2) diff --git a/source/duke3d/src/gamevars.h b/source/duke3d/src/gamevars.h index 46544bba2..d62149670 100644 --- a/source/duke3d/src/gamevars.h +++ b/source/duke3d/src/gamevars.h @@ -103,6 +103,7 @@ extern int32_t g_gameVarCount; extern int32_t g_gameArrayCount; int __fastcall Gv_GetArrayElementSize(int const arrayIdx); +int __fastcall Gv_GetArrayAllocSize(int const arrayIdx); int __fastcall Gv_GetArrayValue(int const id, int index); int __fastcall Gv_GetVar(int id, int spriteNum, int playerNum); void __fastcall Gv_SetVar(int const id, int const lValue, int const spriteNum, int const playerNum);