Skip to content

Commit

Permalink
tr2/shell: fix unclean config saves
Browse files Browse the repository at this point in the history
The EnumMap module was shut down before the config could save the enum
option values.
  • Loading branch information
rr- committed Oct 29, 2024
1 parent aa4bba0 commit 1af7d60
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 51 deletions.
2 changes: 1 addition & 1 deletion docs/tr2/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4471,7 +4471,7 @@ typedef enum {
0x0051B928 - int32_t g_HiRes;
0x0051B930 - RGB_888 g_GamePalette8[256];
0x0051BCC0 - APP_SETTINGS g_SavedAppSettings;
0x0051BD20 - char g_ErrorMessage[128];
0x0051BD20 + char g_ErrorMessage[128];
0x0051BDA8 - int32_t g_MasterVolume;
0x0051BDAC - MCIDEVICEID g_MciDeviceID;
0x0051BDB0 - int32_t g_CD_LoopTrack;
Expand Down
19 changes: 1 addition & 18 deletions src/tr2/decomp/decomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ int32_t __stdcall WinMain(

int32_t result = WinGameStart();
if (result) {
Shell_Shutdown();
RenderErrorBox(result);
Shell_Shutdown();
if (!SE_ShowSetupDialog(0, 0)) {
goto cleanup;
}
Expand All @@ -253,13 +253,11 @@ int32_t __stdcall WinMain(
g_StopInventory = 0;
g_IsGameToExit = 0;
Shell_Main();
Config_Write();
Shell_Shutdown();
SE_WriteAppSettings(&g_SavedAppSettings);
}

cleanup:
Shell_Cleanup();
return g_AppResultCode;
}

Expand Down Expand Up @@ -556,21 +554,6 @@ int32_t __cdecl WinGameStart(void)
return 0;
}

void __cdecl Shell_Shutdown(void)
{
Console_Shutdown();
WinInFinish();
RenderFinish(true);
WinVidFinish();
WinVidHideGameWindow();
if (g_ErrorMessage[0]) {
MessageBoxA(NULL, g_ErrorMessage, NULL, MB_ICONWARNING);
}
Text_Shutdown();
UI_Shutdown();
Config_Shutdown();
}

int16_t __cdecl TitleSequence(void)
{
GF_N_LoadStrings(-1);
Expand Down
1 change: 0 additions & 1 deletion src/tr2/decomp/decomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ bool __cdecl DInputCreate(void);
void __cdecl DInputRelease(void);
void __cdecl WinInReadKeyboard(uint8_t *input_data);
int32_t __cdecl WinGameStart(void);
void __cdecl Shell_Shutdown(void);
int16_t __cdecl TitleSequence(void);
void __cdecl WinVidSetMinWindowSize(int32_t width, int32_t height);
void __cdecl WinVidSetMaxWindowSize(int32_t width, int32_t height);
Expand Down
53 changes: 27 additions & 26 deletions src/tr2/game/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#define GAMEBUF_MEM_CAP 0x380000

// TODO: refactor the hell out of me
BOOL __cdecl Shell_Main(void)
void __cdecl Shell_Main(void)
{
g_HiRes = 0;
g_ScreenSizer = 0;
Expand All @@ -42,17 +42,17 @@ BOOL __cdecl Shell_Main(void)

Config_Read();
if (!S_InitialiseSystem()) {
return false;
return;
}

if (!GF_LoadScriptFile("data\\tombPC.dat")) {
Shell_ExitSystem("GameMain: could not load script file");
return false;
return;
}

if (!GF_N_Load("cfg/TR2X_gameflow.json5")) {
Shell_ExitSystem("GameMain: could not load new script file");
return false;
return;
}

InitialiseStartInfo();
Expand Down Expand Up @@ -81,12 +81,12 @@ BOOL __cdecl Shell_Main(void)
const bool is_frontend_fail = GF_DoFrontendSequence();
if (g_IsGameToExit) {
Config_Write();
return true;
return;
}

if (is_frontend_fail) {
strcpy(g_ErrorMessage, "GameMain: failed in GF_DoFrontendSequence()");
return false;
Shell_ExitSystem("GameMain: failed in GF_DoFrontendSequence()");
return;
}

S_FadeToBlack();
Expand All @@ -105,11 +105,10 @@ BOOL __cdecl Shell_Main(void)
GF_DoLevelSequence(g_GameFlow.single_level, GFL_NORMAL);
} else {
if (gf_param > g_GameFlow.num_levels) {
sprintf(
g_ErrorMessage,
Shell_ExitSystemFmt(
"GameMain: STARTGAME with invalid level number (%d)",
gf_param);
return false;
return;
}
gf_option = GF_DoLevelSequence(gf_param, GFL_NORMAL);
}
Expand All @@ -118,11 +117,10 @@ BOOL __cdecl Shell_Main(void)
case GFD_START_SAVED_GAME:
S_LoadGame(&g_SaveGame, sizeof(SAVEGAME_INFO), gf_param);
if (g_SaveGame.current_level > g_GameFlow.num_levels) {
sprintf(
g_ErrorMessage,
Shell_ExitSystemFmt(
"GameMain: STARTSAVEDGAME with invalid level number (%d)",
g_SaveGame.current_level);
return false;
return;
}
gf_option = GF_DoLevelSequence(g_SaveGame.current_level, GFL_SAVED);
break;
Expand All @@ -145,10 +143,9 @@ BOOL __cdecl Shell_Main(void)
if (g_GameFlow.title_disabled) {
if (g_GameFlow.title_replace < 0
|| g_GameFlow.title_replace == GFD_EXIT_TO_TITLE) {
strcpy(
g_ErrorMessage,
Shell_ExitSystem(
"GameMain Failed: Title disabled & no replacement");
return false;
return;
}
gf_option = g_GameFlow.title_replace;
} else {
Expand All @@ -164,23 +161,27 @@ BOOL __cdecl Shell_Main(void)
}

S_SaveSettings();
GameBuf_Shutdown();
EnumMap_Shutdown();
GameString_Shutdown();
return true;
Config_Write();
}

void __cdecl Shell_Cleanup(void)
void __cdecl Shell_Shutdown(void)
{
Music_Shutdown();
GameString_Shutdown();
Console_Shutdown();
WinInFinish();
RenderFinish(true);
WinVidFinish();
WinVidHideGameWindow();
Text_Shutdown();
UI_Shutdown();
GameBuf_Shutdown();
Config_Shutdown();
}

void __cdecl Shell_ExitSystem(const char *message)
void __cdecl Shell_ExitSystem(const char *const message)
{
GameBuf_Shutdown();
strcpy(g_ErrorMessage, message);
MessageBoxA(NULL, message, NULL, MB_ICONWARNING);
Shell_Shutdown();
Shell_Cleanup();
exit(1);
}

Expand Down
4 changes: 2 additions & 2 deletions src/tr2/game/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "global/types.h"

BOOL __cdecl Shell_Main(void);
void __cdecl Shell_Cleanup(void);
void __cdecl Shell_Main(void);
void __cdecl Shell_Shutdown(void);
void __cdecl Shell_ExitSystem(const char *message);
void __cdecl Shell_ExitSystemFmt(const char *fmt, ...);
1 change: 0 additions & 1 deletion src/tr2/global/vars_decomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@
#define g_WinVidNeedToResetBuffers (*(bool*)0x0051BC30)
#define g_IsWet (*(int32_t*)0x0051BC38)
#define g_SavedAppSettings (*(APP_SETTINGS*)0x0051BCC0)
#define g_ErrorMessage (*(char(*)[128])0x0051BD20)
#define g_IsTitleLoaded (*(BOOL*)0x0051BDA0)
#define g_MasterVolume (*(int32_t*)0x0051BDA8)
#define g_MciDeviceID (*(MCIDEVICEID*)0x0051BDAC)
Expand Down
3 changes: 1 addition & 2 deletions src/tr2/inject_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ static void M_DecompGeneral(const bool enable)
INJECT(enable, 0x0044E520, WinMain);
INJECT(enable, 0x0044E700, GameInit);
INJECT(enable, 0x0044E7A0, WinGameStart);
INJECT(enable, 0x0044E820, Shell_Shutdown);
INJECT(enable, 0x0044E8E0, ScreenshotPCX);
INJECT(enable, 0x0044E9F0, CompPCX);
INJECT(enable, 0x0044EAA0, EncodeLinePCX);
Expand Down Expand Up @@ -430,7 +429,7 @@ static void M_Math(const bool enable)

static void M_Shell(const bool enable)
{
INJECT(enable, 0x0044E770, Shell_Cleanup);
INJECT(enable, 0x0044E820, Shell_Shutdown);
INJECT(enable, 0x0044E890, Shell_ExitSystem);
INJECT(enable, 0x00454980, Shell_Main);
}
Expand Down

0 comments on commit 1af7d60

Please sign in to comment.