Files
source/Utils/XML_Language.cpp
fd2c74727e Make LANGUAGE switchable in runtime (#653)
squashed into a single commit because doing it in steps that all
compiled involved a lot of boilerplate that is pointless to commit to
the repository. GH still has the PR for reference.

g_lang, MAX_MESSAGES_ON_MAP_BOTTOM, and GetLanguagePrefix() were
compile-time constants selected by the ENGLISH/GERMAN/... build
define. They're now runtime, defaulting to the same per-exe value the
define used to pick, and overridable at startup from [Ja2 Settings]
LANGUAGE in Ja2.ini.

-

XMLTacticalMessages is filled at runtime from NewTacticalMessages.xml,
never from compiled-in per-language data: all 8 per-language definitions
were the identical all-zero { L"" }. Delete them and define one shared
buffer in Utils/XML_Language.cpp (the loader) instead of pointer-rebinding
it like the static tables — this drops 9 dead 800KB zero-buffers (8
namespaced copies in LanguageStrings.cpp plus the standalone one) and
leaves no bind-ordering hazard for the XML load path.

-

ExportStrings.cpp privately recompiled one language's full text table
by #including the raw _<LANG>Text.cpp inside namespace Loc, keyed off
the exe-level ENGLISH/GERMAN/... compile macro (whichever the build
happened to select). That's redundant with the pointer globals every
other subsystem already uses (Text.h / LanguageStrings.cpp).

Drop the private copy; the unqualified table names in Loc::ExportStrings
now resolve to the global pointer externs, which BindLanguageStrings has
already rebound to the runtime g_lang by the time this runs (EXPORT_STRINGS
ini flag, checked after GetRuntimeSettings in sgp.cpp). gs_Lang (used only
by Loc::Translate for the Polish/Russian byte remap on raw .edt exports) is
now derived from g_lang via ToLocLanguage, so the export always matches
whatever language is actually active instead of a compile-time pick.

-

Editor/popupmenu.cpp and Strategic/Scheduling.cpp kept their own
call-site extern of gszScheduleActions as CHAR16[NUM_SCHEDULE_ACTIONS][20]
after the real definition changed into a rebindable pointer
(CHAR16 (*)[20], LanguageStrings.cpp). MSVC decays the outer array
dimension when mangling globals, so both declarations produce the same
symbol (?gszScheduleActions@@3PAY0BE@_WA) and the mismatch linked
silently -- but the array-typed TUs then indexed the 4-byte pointer
slot itself as string data, so the editor schedule popup and the map
schedule message text read garbage.

-

add text.def to be single-source of truth on symbol names and use it
 .much simpler, less error prone if adding more strings

-

add pseudo interface for language state. MAX_SAGES_ON_BOTTOM must always
 change in lockstep with g_lang. this isn't foolproof but better

---------

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-07-21 05:33:03 -03:00

213 lines
5.6 KiB
C++

#include "sgp.h"
#include "Debug Control.h"
#include "expat.h"
#include "XML.h"
#include "Interface.h"
#include "LuaInitNPCs.h"
#include "email.h"
#include "InterfaceItemImages.h"
#include "Soldier Profile.h"
#include "mercs.h"
#include "Encrypted File.h"
#include "GameSettings.h"
#include "Text.h"
#include "XML_Language.h"
typedef enum
{
LANGUAGE_ELEMENT_NONE = 0,
LANGUAGE_ELEMENT_LIST,
LANGUAGE_ELEMENT,
LANGUAGE_ELEMENT_PROPERTY,
LANGUAGE_ELEMENT_SUBLIST,
LANGUAGE_ELEMENT_SUBLIST_PROPERTY,
} LANGUAGE_PARSE_STAGE;
LANGUAGE_LOCATION zlanguageText[1000];
// Single shared buffer for all languages (declared in Text.h): filled at runtime from
// NewTacticalMessages.xml below, never from compiled-in per-language data, so it is not
// part of the BindLanguageStrings pointer-rebind scheme.
CHAR16 XMLTacticalMessages[1000][MAX_MESSAGE_NAMES_CHARS];
typedef struct
{
LANGUAGE_PARSE_STAGE curElement;
CHAR8 szCharData[MAX_CHAR_DATA_LENGTH+1];
LANGUAGE_LOCATION curLanguageData;
UINT32 maxArraySize;
UINT32 curIndex;
UINT32 currentDepth;
UINT32 maxReadDepth;
}languageLocationParseData;
BOOLEAN LanguageLocation_TextOnly;
LANGUAGE_LOCATION *pLang;
UINT32 FileTypeXml = 0;
static void XMLCALL
languageLocationStartElementHandle(void *userData, const XML_Char *name, const XML_Char **atts)
{
languageLocationParseData * pData = (languageLocationParseData *)userData;
if(pData->currentDepth <= pData->maxReadDepth) //are we reading this element?
{
if(strcmp(name, "MESSAGES") == 0 && pData->curElement == LANGUAGE_ELEMENT_NONE)
{
pData->curElement = LANGUAGE_ELEMENT_LIST;
pData->maxReadDepth++; //we are not skipping this element
}
else if(strcmp(name, "TEXT") == 0 && pData->curElement == LANGUAGE_ELEMENT_LIST)
{
pData->curElement = LANGUAGE_ELEMENT;
pData->maxReadDepth++; //we are not skipping this element
}
else if(pData->curElement == LANGUAGE_ELEMENT &&
(strcmp(name, "uiIndex") == 0 ||
strcmp(name, "Message") == 0 ))
{
pData->curElement = LANGUAGE_ELEMENT_PROPERTY;
pData->maxReadDepth++; //we are not skipping this element
}
pData->szCharData[0] = '\0';
}
pData->currentDepth++;
}
static void XMLCALL
languageLocationCharacterDataHandle(void *userData, const XML_Char *str, int len)
{
languageLocationParseData * pData = (languageLocationParseData *)userData;
if( (pData->currentDepth <= pData->maxReadDepth) &&
(strlen(pData->szCharData) < MAX_CHAR_DATA_LENGTH)
){
strncat(pData->szCharData,str,__min((unsigned int)len,MAX_CHAR_DATA_LENGTH-strlen(pData->szCharData)));
}
}
static void XMLCALL
languageLocationEndElementHandle(void *userData, const XML_Char *name)
{
languageLocationParseData * pData = (languageLocationParseData *)userData;
if(pData->currentDepth <= pData->maxReadDepth)
{
if(strcmp(name, "MESSAGES") == 0)
{
pData->curElement = LANGUAGE_ELEMENT_NONE;
}
else if(strcmp(name, "TEXT") == 0)
{
pData->curElement = LANGUAGE_ELEMENT_LIST;
if (!LanguageLocation_TextOnly )
{
pLang[pData->curLanguageData.uiIndex].uiIndex = pData->curLanguageData.uiIndex;
if ( FileTypeXml == 0 )
wcscpy(XMLTacticalMessages[pData->curLanguageData.uiIndex], pData->curLanguageData.Message);
}
else
{
if ( FileTypeXml == 0 )
wcscpy(XMLTacticalMessages[pData->curLanguageData.uiIndex], pData->curLanguageData.Message);
}
}
else if(strcmp(name, "uiIndex") == 0)
{
pData->curElement = LANGUAGE_ELEMENT;
pData->curLanguageData.uiIndex = (UINT32) strtoul(pData->szCharData, NULL, 0);
}
else if(strcmp(name, "Message") == 0 )
{
pData->curElement = LANGUAGE_ELEMENT;
MultiByteToWideChar( CP_UTF8, 0, pData->szCharData, -1, pData->curLanguageData.Message, sizeof(pData->curLanguageData.Message)/sizeof(pData->curLanguageData.Message[0]) );
pData->curLanguageData.Message[sizeof(pData->curLanguageData.Message)/sizeof(pData->curLanguageData.Message[0]) - 1] = '\0';
}
pData->maxReadDepth--;
}
pData->currentDepth--;
}
BOOLEAN ReadInLanguageLocation(STR fileName, BOOLEAN localizedVersion, LANGUAGE_LOCATION *Lang, UINT32 FileType2 )
{
HWFILE hFile;
UINT32 uiBytesRead;
UINT32 uiFSize;
CHAR8 * lpcBuffer;
XML_Parser parser = XML_ParserCreate(NULL);
languageLocationParseData pData;
DebugMsg(TOPIC_JA2, DBG_LEVEL_3, "Loading NewTacticalMessages.xml" );
LanguageLocation_TextOnly = localizedVersion;
// Open file
hFile = FileOpen( fileName, FILE_ACCESS_READ, FALSE );
if ( !hFile )
return( localizedVersion );
uiFSize = FileGetSize(hFile);
lpcBuffer = (CHAR8 *) MemAlloc(uiFSize+1);
//Read in block
if ( !FileRead( hFile, lpcBuffer, uiFSize, &uiBytesRead ) )
{
MemFree(lpcBuffer);
return( FALSE );
}
lpcBuffer[uiFSize] = 0; //add a null terminator
FileClose( hFile );
XML_SetElementHandler(parser, languageLocationStartElementHandle, languageLocationEndElementHandle);
XML_SetCharacterDataHandler(parser, languageLocationCharacterDataHandle);
memset(&pData,0,sizeof(pData));
XML_SetUserData(parser, &pData);
pLang = Lang;
FileTypeXml = FileType2;
if(!XML_Parse(parser, lpcBuffer, uiFSize, TRUE))
{
CHAR8 errorBuf[511];
sprintf(errorBuf, "XML Parser Error in NewTacticalMessages.xml: %s at line %d", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser));
LiveMessage(errorBuf);
MemFree(lpcBuffer);
return FALSE;
}
MemFree(lpcBuffer);
XML_ParserFree(parser);
return( TRUE );
}