Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
game-string: manage game strings in libtrx
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 17, 2024
1 parent da5502e commit 7f755ef
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ __pycache__/
subprojects/packagecache/
subprojects/dwarfstack-*/
subprojects/dwarfstack.wrap
subprojects/uthash-*/
subprojects/uthash.wrap
.dotnet/
.local/
45 changes: 7 additions & 38 deletions src/game/game_string.c
Original file line number Diff line number Diff line change
@@ -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)
{
assert(id >= 0);
assert(id < GS_NUMBER_OF);
Memory_FreePointer(&m_StringMap[id]);
m_StringMap[id] = Memory_DupStr(value);
}

const char *GameString_Get(const GAME_STRING_ID id)
void GameString_Init(void)
{
return m_StringMap[id] != NULL ? (const char *)m_StringMap[id]
: m_DefaultStringMap[id];
// IWYU pragma: begin_keep
#include "game_string.def"
// IWYU pragma: end_keep
}

GAME_STRING_ID GameString_IDFromEnum(const char *const key)
void GameString_Shutdown(void)
{
return ENUM_STRING_GET(GAME_STRING_ID, key, GS_INVALID);
GS_Clear();
}
19 changes: 3 additions & 16 deletions src/game/game_string.h
Original file line number Diff line number Diff line change
@@ -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);
12 changes: 8 additions & 4 deletions src/game/gameflow/gameflow_new.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "global/enum_str.h"
#include "global/types.h"

#include <libtrx/log.h>

#include <assert.h>

GAMEFLOW_NEW g_GameflowNew;
Expand All @@ -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);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/game/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -160,6 +163,7 @@ BOOL __cdecl Shell_Main(void)
S_SaveSettings();
GameBuf_Shutdown();
Config_Write();
GameString_Shutdown();
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion subprojects/libtrx

0 comments on commit 7f755ef

Please sign in to comment.