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/dwarfstack-*/
subprojects/dwarfstack.wrap
subprojects/uthash-*/
subprojects/uthash.wrap
.dotnet/
.local/

View File

@ -1,46 +1,15 @@
#include "game_string.h"
#include <libtrx/memory.h>
#include <libtrx/utils.h>
#include <libtrx/game/game_string.h>
#include <assert.h>
#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)
void GameString_Init(void)
{
assert(id >= 0);
assert(id < GS_NUMBER_OF);
Memory_FreePointer(&m_StringMap[id]);
m_StringMap[id] = Memory_DupStr(value);
// IWYU pragma: begin_keep
#include "game_string.def"
// IWYU pragma: end_keep
}
const char *GameString_Get(const GAME_STRING_ID id)
void GameString_Shutdown(void)
{
return m_StringMap[id] != NULL ? (const char *)m_StringMap[id]
: m_DefaultStringMap[id];
}
GAME_STRING_ID GameString_IDFromEnum(const char *const key)
{
return ENUM_STRING_GET(GAME_STRING_ID, key, GS_INVALID);
GameString_Clear();
}

View File

@ -1,19 +1,6 @@
#pragma once
#include <libtrx/enum_str.h>
#include <libtrx/game/game_string.h>
#define GS(id) GameString_Get(GS_##id)
#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);
void GameString_Init(void);
void GameString_Shutdown(void);

View File

@ -5,6 +5,8 @@
#include "global/enum_str.h"
#include "global/types.h"
#include <libtrx/log.h>
#include <assert.h>
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)
{
const GAME_STRING_ID game_string =
ENUM_STRING_GET(GAME_STRING_ID, key, GS_INVALID);
if (game_string != GS_INVALID) {
GameString_Set(game_string, value);
if (!GameString_IsKnown(key)) {
LOG_ERROR("Invalid game string key: %s", key);
} else if (value == NULL) {
LOG_ERROR("Invalid game string value: %s", key);
} else {
GameString_Define(key, value);
}
}

View File

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

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