Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localization cleanup #511

Open
wants to merge 61 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
0e88906
Minor fixes to localization.cpp
winterheart Jul 31, 2024
6d682c2
Remove calling once guard in LoadStringTables()
winterheart Jul 31, 2024
ef1be32
Add unittests for localization functions
winterheart Jul 31, 2024
e5caf32
Rewrite localization to use std::string
winterheart Jul 31, 2024
ac7b574
Use std::fs::path in localization functions
winterheart Aug 1, 2024
c214992
Unify load messages in scripts
winterheart Aug 1, 2024
0a156ba
Fill OsirisModule functions manually
winterheart Aug 1, 2024
b4ea38e
Implement MessageMap localization for scripts
winterheart Aug 1, 2024
5568bb0
Normalize message file names
winterheart Aug 2, 2024
7c7a5a2
Convert TrainingMission to new Message system
winterheart Aug 2, 2024
a8b3b77
Convert Y2K to new Message system
winterheart Aug 2, 2024
3540f2b
Convert BatteriesIncluded to new Message system
winterheart Aug 2, 2024
4899a17
Convert barney.cpp to new Message system
winterheart Aug 2, 2024
abdd9d2
Convert BossCamera.cpp to new Message system
winterheart Aug 2, 2024
849d5e1
Convert CanyonsCTF.cpp to new Message system
winterheart Aug 2, 2024
3c440e9
Convert CellTestLevel.cpp to new Message system
winterheart Aug 2, 2024
e287242
Convert ChrisTest.cpp to new Message system
winterheart Aug 2, 2024
f73fc52
Convert Geodomes.cpp to new Message system
winterheart Aug 2, 2024
f3c1807
Convert HalfPipe.cpp to new Message system
winterheart Aug 2, 2024
bcfdd54
Convert InfernalBolt.cpp to new Message system
winterheart Aug 2, 2024
d37e816
Convert Inversion.cpp to new Message system
winterheart Aug 2, 2024
01d9a31
Convert LEVEL0.cpp to new Message system
winterheart Aug 2, 2024
aab9471
Convert level1.cpp to new Message system
winterheart Aug 2, 2024
187c25c
Convert level2.cpp to new Message system
winterheart Aug 2, 2024
7ffa332
Convert level3.cpp to new Message system
winterheart Aug 2, 2024
76f9148
Convert level4.cpp to new Message system
winterheart Aug 2, 2024
f9d8d11
Convert level5.cpp to new Message system
winterheart Aug 2, 2024
ec650e5
Convert level6.cpp to new Message system
winterheart Aug 2, 2024
e6e8d7c
Convert level7.cpp to new Message system
winterheart Aug 2, 2024
cee4486
Convert level8.cpp to new Message system
winterheart Aug 2, 2024
1c9578d
Convert level9.cpp to new Message system
winterheart Aug 2, 2024
a7df0de
Convert level10.cpp to new Message system
winterheart Aug 2, 2024
821279f
Convert level11.cpp to new Message system
winterheart Aug 2, 2024
1059816
Convert level12.cpp to new Message system
winterheart Aug 2, 2024
38b6e7b
Convert level13.cpp to new Message system
winterheart Aug 2, 2024
94f2a0e
Convert level14.cpp to new Message system
winterheart Aug 2, 2024
6def6d6
Convert level15.cpp to new Message system
winterheart Aug 2, 2024
9d5eef1
Convert level16.cpp to new Message system
winterheart Aug 2, 2024
3ea8533
Convert level17.cpp to new Message system
winterheart Aug 2, 2024
d64d814
Convert levelS1.cpp to new Message system
winterheart Aug 2, 2024
3a4e593
Convert levelS2.cpp to new Message system
winterheart Aug 2, 2024
849bde3
Convert merc1.cpp to new Message system
winterheart Aug 2, 2024
5ccf7aa
Convert merc02.cpp to new Message system
winterheart Aug 2, 2024
66ebd0e
Convert merc3.cpp to new Message system
winterheart Aug 2, 2024
71fdd82
Convert merc4.cpp to new Message system
winterheart Aug 2, 2024
dd46162
Convert merc5.cpp to new Message system
winterheart Aug 2, 2024
38c1791
Convert merc6.cpp to new Message system
winterheart Aug 2, 2024
f646fb0
Convert merc7.cpp to new Message system
winterheart Aug 2, 2024
8ca8cef
Convert myPowerHouse.cpp to new Message system
winterheart Aug 2, 2024
91a091a
Convert Mysterious_Isle.cpp to new Message system
winterheart Aug 2, 2024
9ef1f7a
Convert orbital.cpp to new Message system
winterheart Aug 2, 2024
2aa5d0e
Convert Paranoia.cpp to new Message system
winterheart Aug 2, 2024
778d243
Convert PiccuStation.cpp to new Message system
winterheart Aug 2, 2024
2b47948
Convert Polaris.cpp to new Message system
winterheart Aug 2, 2024
f83f087
Convert Quadsomniac.cpp to new Message system
winterheart Aug 2, 2024
77081cd
Convert RudeAwakening.cpp to new Message system
winterheart Aug 2, 2024
ead5ed3
Convert SewerRat.cpp to new Message system
winterheart Aug 2, 2024
faf1f3f
Add to tOSIRISModuleInit serial_version field
winterheart Aug 11, 2024
77bb924
Rename GetMessage() to GetMessageMap()
winterheart Aug 11, 2024
fca5318
Remove static variable String_table_size
winterheart Sep 7, 2024
37e163a
Change GetStringFromTable() signature
winterheart Sep 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Minor fixes to localization.cpp
winterheart committed Sep 23, 2024
commit 0e889067a47cee513bb2a6e38725eb7235003695
106 changes: 54 additions & 52 deletions Descent3/localization.cpp
Original file line number Diff line number Diff line change
@@ -96,6 +96,7 @@
#include "localization.h"
#include "log.h"
#include "mem.h"
#include "pserror.h"

struct tLangTag {
const char *tag;
@@ -116,42 +117,42 @@ namespace {
int Localization_language = -1;

int String_table_size = 0;
winterheart marked this conversation as resolved.
Show resolved Hide resolved
char **String_table = NULL;
char **String_table = nullptr;

// list of the string table files, they will be loaded in the order they are listed
const char *String_table_list[] = {"D3.STR", NULL};
const char *String_table_list[] = {"D3.STR", nullptr};

const char *_Error_string = "!!ERROR MISSING STRING!!";
const char *_Empty_string = "\0";
const char *Error_string = "!!ERROR MISSING STRING!!";
const char *Empty_string = "\0";
} // namespace

void Localization_SetLanguage(int type) {
ASSERT(type >= 0 && type < Num_languages);
Localization_language = type;
}

int Localization_GetLanguage(void) { return Localization_language; }
int Localization_GetLanguage() { return Localization_language; }

#define COMMENT_TAG "!/!" // This line is to be ignored

#define STAG_CONTINUE -1 // this line is just a continuation of the last line
#define STAG_EMPTY -2 // empty line
#define STAG_COMMENT -3 // comment line
#define STAG_CONTINUE (-1) // this line is just a continuation of the last line
#define STAG_EMPTY (-2) // empty line
#define STAG_COMMENT (-3) // comment line
// 0 -> Num_languages means it's the start of a string that begins with that language

#define MAX_LINE_LENGTH 1024
#define MAX_STRING_LENGTH 8 * MAX_LINE_LENGTH
#define MAX_STRING_LENGTH (8 * MAX_LINE_LENGTH)
#define MAX_TAG_LENGTH 3

int GetTotalStringCount(void);
int GetTotalStringCount();
int LoadStringFile(const char *filename, int starting_offset);
int8_t _parse_line_information(char *line);
char *_parse_string_tag(char *buffer);
char *_parse_escape_chars(char *buffer);
int parse_line_information(char *line);
char *parse_string_tag(char *buffer);
char *parse_escape_chars(char *buffer);

// Call this to load up the string tables into memory
// Returns the number of strings loaded, if this is 0, then the program MUST not continue
int LoadStringTables(void) {
int LoadStringTables() {
static bool called = false;
int old_language;

@@ -186,8 +187,9 @@ int LoadStringTables(void) {
return 0;
}

for (int tcount = 0; tcount < string_count; tcount++)
String_table[tcount] = NULL;
for (int tcount = 0; tcount < string_count; tcount++) {
String_table[tcount] = nullptr;
}

int runcount = 0;
int temp;
@@ -219,17 +221,17 @@ int LoadStringTables(void) {
}
winterheart marked this conversation as resolved.
Show resolved Hide resolved

// Deallocates all the memory used for the string tables
void FreeStringTables(void) {
void FreeStringTables() {
DestroyStringTable(String_table, String_table_size);
String_table = NULL;
String_table = nullptr;
}

const char *GetStringFromTable(int index) {
if ((index < 0) || (index >= String_table_size))
winterheart marked this conversation as resolved.
Show resolved Hide resolved
return _Error_string;
return Error_string;

if (!String_table[index])
return _Empty_string;
return Empty_string;

return String_table[index];
}
@@ -267,7 +269,7 @@ bool CreateStringTable(const char *filename, char ***table, int *size) {
ASSERT(Localization_language != -1);
if (!filename) {
if (table)
*table = NULL;
*table = nullptr;
if (size)
*size = 0;
return false;
@@ -279,7 +281,7 @@ bool CreateStringTable(const char *filename, char ***table, int *size) {
file = cfopen(fname, "rt");
if (!file) {
if (table)
*table = NULL;
*table = nullptr;
if (size)
*size = 0;
return false;
@@ -297,7 +299,7 @@ bool CreateStringTable(const char *filename, char ***table, int *size) {

while (!cfeof(file)) {
cf_ReadString(tempbuffer, MAX_LINE_LENGTH + 1, file);
if (_parse_line_information(tempbuffer) == Localization_language)
if (parse_line_information(tempbuffer) == Localization_language)
scount++;
}
cfclose(file);
@@ -315,7 +317,7 @@ bool CreateStringTable(const char *filename, char ***table, int *size) {
// no strings found
Localization_language = old_language;
LOG_WARNING << "Localization: Warning, 0 strings found in " << filename;
*table = NULL;
*table = nullptr;
*size = 0;
return true;
}
@@ -327,7 +329,7 @@ bool CreateStringTable(const char *filename, char ***table, int *size) {
*table = mem_rmalloc<char *>(scount);
if (!*table) {
if (table)
*table = NULL;
*table = nullptr;
if (size)
*size = 0;
Localization_language = old_language;
@@ -336,13 +338,13 @@ bool CreateStringTable(const char *filename, char ***table, int *size) {

strtable = *table;
for (int tcount = 0; tcount < scount; tcount++)
strtable[tcount] = NULL;
strtable[tcount] = nullptr;

// now load the strings
file = cfopen(fname, "rt");
if (!file) {
if (table)
*table = NULL;
*table = nullptr;
if (size)
*size = 0;
Localization_language = old_language;
@@ -361,13 +363,13 @@ bool CreateStringTable(const char *filename, char ***table, int *size) {
if (scount >= 198)
scount = scount;

line_info = _parse_line_information(tempbuffer);
line_info = parse_line_information(tempbuffer);

switch (line_info) {
case STAG_CONTINUE:
if (reading_string) {
// we need to add on to the working buffer
string += _parse_escape_chars(tempbuffer);
string += parse_escape_chars(tempbuffer);
}
break;
case STAG_EMPTY:
@@ -398,7 +400,7 @@ bool CreateStringTable(const char *filename, char ***table, int *size) {
reading_string = true;
// start filling in the buffer
string.Destroy();
string = _parse_escape_chars(_parse_string_tag(tempbuffer));
string = parse_escape_chars(parse_string_tag(tempbuffer));
}

} else {
@@ -440,7 +442,7 @@ void DestroyStringTable(char **table, int size) {

// returns the total number of strings in all the string table files
// returns 0 on error
int GetTotalStringCount(void) {
int GetTotalStringCount() {
int scount = 0;
int findex = 0;
CFILE *file;
@@ -457,7 +459,7 @@ int GetTotalStringCount(void) {

while (!cfeof(file)) {
cf_ReadString(tempbuffer, MAX_LINE_LENGTH + 1, file);
if (_parse_line_information(tempbuffer) == Localization_language)
if (parse_line_information(tempbuffer) == Localization_language)
scount++;
}

@@ -494,13 +496,13 @@ int LoadStringFile(const char *filename, int starting_offset) {
if (scount >= 198)
scount = scount;

line_info = _parse_line_information(buffer);
line_info = parse_line_information(buffer);

switch (line_info) {
case STAG_CONTINUE:
if (reading_string) {
// we need to add on to the working buffer
string += _parse_escape_chars(buffer);
string += parse_escape_chars(buffer);
}
break;
case STAG_EMPTY:
@@ -531,7 +533,7 @@ int LoadStringFile(const char *filename, int starting_offset) {
reading_string = true;
// start filling in the buffer
string.Destroy();
string = _parse_escape_chars(_parse_string_tag(buffer));
string = parse_escape_chars(parse_string_tag(buffer));
}

} else {
@@ -555,7 +557,7 @@ int LoadStringFile(const char *filename, int starting_offset) {
}

// returns STAG_* information about the line
int8_t _parse_line_information(char *line) {
int parse_line_information(char *line) {
for (int i = 0; i < Num_languages; i++) {
if (Language_tags[i].length == -1)
Language_tags[i].length = strlen(Language_tags[i].tag);
@@ -572,8 +574,8 @@ int8_t _parse_line_information(char *line) {
}

// parses a string_tag out
char *_parse_string_tag(char *buffer) {
int8_t i = _parse_line_information(buffer);
char *parse_string_tag(char *buffer) {
int i = parse_line_information(buffer);

switch (i) {
case STAG_CONTINUE:
@@ -595,7 +597,7 @@ char *_parse_string_tag(char *buffer) {
}

// parses out escape chars...like /t,/n
char *_parse_escape_chars(char *buffer) {
char *parse_escape_chars(char *buffer) {
char tempbuffer[MAX_STRING_LENGTH];
int t_index, b_index;

@@ -653,8 +655,8 @@ char *_parse_escape_chars(char *buffer) {
}

GrowString::GrowString() {
root.string_data = NULL;
root.next = NULL;
root.string_data = nullptr;
root.next = nullptr;
curr = &root;
}
GrowString::~GrowString() { Destroy(); }
@@ -674,21 +676,21 @@ void GrowString::operator+=(char *str) {
}
sprintf(node->string_data, "\n%s", str);
curr->next = node;
node->next = NULL;
node->next = nullptr;
curr = node;
} else {
root.string_data = mem_rmalloc<char>(strlen(str) + 1);
if (!root.string_data)
return;
strcpy(root.string_data, str);
root.next = NULL;
root.next = nullptr;
}
}

void GrowString::Destroy(void) {
void GrowString::Destroy() {
if (root.string_data)
mem_free(root.string_data);
root.string_data = NULL;
root.string_data = nullptr;

tbufferinfo *c, *next;
c = next = root.next;
@@ -699,8 +701,8 @@ void GrowString::Destroy(void) {
mem_free(c);
c = next;
}
root.next = NULL;
root.string_data = NULL;
root.next = nullptr;
root.string_data = nullptr;
curr = &root;
}

@@ -710,7 +712,7 @@ GrowString GrowString::operator+(char *str) {
}

GrowString GrowString::operator+(GrowString &gs) {
char *str = NULL;
char *str = nullptr;
gs.GetString(&str);
*this += str;
if (str)
@@ -719,7 +721,7 @@ GrowString GrowString::operator+(GrowString &gs) {
}

void GrowString::operator+=(GrowString &gs) {
char *str = NULL;
char *str = nullptr;
gs.GetString(&str);
*this += str;
if (str)
@@ -732,15 +734,15 @@ void GrowString::operator=(char *str) {
}

void GrowString::operator=(GrowString &gs) {
char *str = NULL;
char *str = nullptr;
gs.GetString(&str);
*this = str;
if (str)
mem_free(str);
}

void GrowString::GetString(char **str) {
*str = NULL;
*str = nullptr;
int size = Length();
if (size == -1)
return;
@@ -760,7 +762,7 @@ void GrowString::GetString(char **str) {
}
}

int GrowString::Length(void) {
int GrowString::Length() {
if (!root.string_data)
return -1;

14 changes: 7 additions & 7 deletions Descent3/localization.h
Original file line number Diff line number Diff line change
@@ -38,23 +38,23 @@
* $NoKeywords: $
*/

#ifndef __LOCALIZATION_H__
#define __LOCALIZATION_H__
#ifndef LOCALIZATION_H
#define LOCALIZATION_H

#define LANGUAGE_ENGLISH 0
#define LANGUAGE_GERMAN 1
#define LANGUAGE_SPANISH 2
#define LANGUAGE_ITALIAN 3
#define LANGUAGE_FRENCH 4
void Localization_SetLanguage(int type);
int Localization_GetLanguage(void);
int Localization_GetLanguage();

// Call this to load up the string tables into memory
// Returns the number of strings loaded, if this is 0, then the program MUST not continue
int LoadStringTables(void);
int LoadStringTables();

// Deallocates all the memory used for the string tables
void FreeStringTables(void);
void FreeStringTables();

// Returns a pointer to the string at the index location from the string table
// if it is a bad index given, then the pointer to the error string "ERROR MISSING STRING" is given
@@ -78,15 +78,15 @@ class GrowString {
public:
GrowString();
~GrowString();
void Destroy(void);
void Destroy();
void operator+=(char *str);
GrowString operator+(char *str);
GrowString operator+(GrowString &gs);
void operator+=(GrowString &gs);
void operator=(char *str);
void operator=(GrowString &gs);
void GetString(char **str);
int Length(void);
int Length();

private:
tbufferinfo root;