game-string: manage game strings in libtrx

This commit is contained in:
Marcin Kurczewski 2024-09-17 23:00:37 +02:00
parent fe41b2a077
commit ca58dd4eaf
No known key found for this signature in database
GPG Key ID: CC65E6FD28CAE42A
6 changed files with 25 additions and 59 deletions

2
.gitignore vendored
View File

@ -6,5 +6,7 @@ __pycache__/
subprojects/packagecache/ subprojects/packagecache/
subprojects/dwarfstack-*/ subprojects/dwarfstack-*/
subprojects/dwarfstack.wrap subprojects/dwarfstack.wrap
subprojects/uthash-*/
subprojects/uthash.wrap
.dotnet/ .dotnet/
.local/ .local/

View File

@ -1,46 +1,15 @@
#include "game_string.h" #include "game_string.h"
#include <libtrx/memory.h> #include <libtrx/game/game_string.h>
#include <libtrx/utils.h>
#include <assert.h> void GameString_Init(void)
#include <stddef.h>
#include <string.h>
static char *m_StringMap[GS_NUMBER_OF] = { 0 };
#undef GS_DEFINE
#define GS_DEFINE(id, str) str,
static const char *m_DefaultStringMap[GS_NUMBER_OF] = {
#include "game/game_string.def"
};
#undef GS_DEFINE
#define GS_DEFINE(id, str) \
{ \
QUOTE(id), \
GS_##id, \
},
ENUM_STRING_MAP ENUM_STRING_MAP(GAME_STRING_ID)[] = {
#include "game/game_string.def"
{ NULL, -1 }
};
void GameString_Set(const GAME_STRING_ID id, const char *const value)
{ {
assert(id >= 0); // IWYU pragma: begin_keep
assert(id < GS_NUMBER_OF); #include "game_string.def"
Memory_FreePointer(&m_StringMap[id]); // IWYU pragma: end_keep
m_StringMap[id] = Memory_DupStr(value);
} }
const char *GameString_Get(const GAME_STRING_ID id) void GameString_Shutdown(void)
{ {
return m_StringMap[id] != NULL ? (const char *)m_StringMap[id] GameString_Clear();
: m_DefaultStringMap[id];
}
GAME_STRING_ID GameString_IDFromEnum(const char *const key)
{
return ENUM_STRING_GET(GAME_STRING_ID, key, GS_INVALID);
} }

View File

@ -1,19 +1,6 @@
#pragma once #pragma once
#include <libtrx/enum_str.h> #include <libtrx/game/game_string.h>
#define GS(id) GameString_Get(GS_##id) void GameString_Init(void);
void GameString_Shutdown(void);
#undef GS_DEFINE
#define GS_DEFINE(id, str) GS_##id,
typedef enum GAME_STRING_ID {
GS_INVALID = -1,
#include "game/game_string.def"
GS_NUMBER_OF,
} GAME_STRING_ID;
extern ENUM_STRING_MAP ENUM_STRING_MAP(GAME_STRING_ID)[];
void GameString_Set(GAME_STRING_ID id, const char *value);
const char *GameString_Get(GAME_STRING_ID id);
GAME_STRING_ID GameString_IDFromEnum(const char *str);

View File

@ -5,6 +5,8 @@
#include "global/enum_str.h" #include "global/enum_str.h"
#include "global/types.h" #include "global/types.h"
#include <libtrx/log.h>
#include <assert.h> #include <assert.h>
GAMEFLOW_NEW g_GameflowNew; GAMEFLOW_NEW g_GameflowNew;
@ -27,10 +29,12 @@ static void GF_N_LoadObjectString(
static void GF_N_LoadGameString(const char *const key, const char *const value) static void GF_N_LoadGameString(const char *const key, const char *const value)
{ {
const GAME_STRING_ID game_string = if (!GameString_IsKnown(key)) {
ENUM_STRING_GET(GAME_STRING_ID, key, GS_INVALID); LOG_ERROR("Invalid game string key: %s", key);
if (game_string != GS_INVALID) { } else if (value == NULL) {
GameString_Set(game_string, value); LOG_ERROR("Invalid game string value: %s", key);
} else {
GameString_Define(key, value);
} }
} }

View File

@ -4,6 +4,7 @@
#include "decomp/decomp.h" #include "decomp/decomp.h"
#include "game/console.h" #include "game/console.h"
#include "game/demo.h" #include "game/demo.h"
#include "game/game_string.h"
#include "game/gameflow.h" #include "game/gameflow.h"
#include "game/gameflow/reader.h" #include "game/gameflow/reader.h"
#include "game/input.h" #include "game/input.h"
@ -25,6 +26,8 @@ BOOL __cdecl Shell_Main(void)
g_GameSizer = 1.0; g_GameSizer = 1.0;
g_GameSizerCopy = 1.0; g_GameSizerCopy = 1.0;
GameString_Init();
Config_Read(); Config_Read();
if (!S_InitialiseSystem()) { if (!S_InitialiseSystem()) {
return false; return false;
@ -160,6 +163,7 @@ BOOL __cdecl Shell_Main(void)
S_SaveSettings(); S_SaveSettings();
GameBuf_Shutdown(); GameBuf_Shutdown();
Config_Write(); Config_Write();
GameString_Shutdown();
return true; return true;
} }

@ -1 +1 @@
Subproject commit 8ad35cc897de2af3d1b58407adfbf2908ade5dd6 Subproject commit 0d395b79ec7cea7afd40fe241a535b3bb48602a6