12 #include "../stdafx.h" 13 #include "../strgen/strgen.h" 15 #include "../fileio_func.h" 16 #include "../tar_type.h" 17 #include "../script/squirrel_class.hpp" 18 #include "../strings_func.h" 23 #include "table/strings.h" 28 #include "../safeguards.h" 30 void CDECL strgen_warning(
const char *s, ...)
41 void CDECL strgen_error(
const char *s, ...)
52 void NORETURN CDECL strgen_fatal(
const char *s, ...)
60 throw std::exception();
70 this->language =
stredup(language, end !=
nullptr ? end - 1 :
nullptr);
89 if (fh ==
nullptr)
return nullptr;
93 const char *langname = strrchr(file, PATHSEPCHAR);
94 if (langname ==
nullptr) {
101 if (*langname ==
'.' || *langname == 0)
return nullptr;
103 std::unique_ptr<LanguageStrings> ret(
new LanguageStrings(langname, strchr(langname,
'.')));
106 while (to_read != 0 && fgets(buffer,
sizeof(buffer), fh) !=
nullptr) {
107 size_t len = strlen(buffer);
111 while (i > 0 && (buffer[i - 1] ==
'\r' || buffer[i - 1] ==
'\n' || buffer[i - 1] ==
' ')) i--;
114 ret->lines.emplace_back(buffer, i);
132 StringList::const_iterator
p;
133 StringList::const_iterator
end;
147 char *
ReadLine(
char *buffer,
const char *last)
override 149 if (this->p == this->end)
return nullptr;
151 strecpy(buffer, this->p->c_str(), last);
185 void Write(
const byte *buffer,
size_t length)
187 this->strings.emplace_back((
const char *)buffer, length);
205 if (stringid == (
int)this->strings.size()) this->strings.emplace_back(name);
230 void Scan(
const char *directory)
235 bool AddFile(
const char *filename,
size_t basepath_length,
const char *tar_filename)
override 237 if (strcmp(filename, exclude) == 0)
return true;
240 if (ls ==
nullptr)
return false;
256 char *e = strrchr(filename, PATHSEPCHAR);
257 if (e ==
nullptr)
return nullptr;
264 if (ls ==
nullptr)
return nullptr;
273 size_t len = strlen(filename);
275 const char *tar_filename = info->
GetTarFile();
276 TarList::iterator iter;
277 if (tar_filename !=
nullptr && (iter = _tar_list[
GAME_DIR].find(tar_filename)) != _tar_list[
GAME_DIR].end()) {
280 TarFileList::iterator tar;
283 if (tar->second.tar_filename != iter->first)
continue;
286 if (tar->first.size() <= len || tar->first.compare(0, len, filename) != 0)
continue;
287 if (tar->first.compare(tar->first.size() - 4, 4,
".txt") != 0)
continue;
289 scanner.
AddFile(tar->first.c_str(), 0, tar_filename);
293 scanner.
Scan(filename);
310 if (_errors != 0)
throw std::exception();
312 this->version = data.
Version();
317 for (
const auto &p : this->raw_strings) {
319 StringListReader translation_reader(data, *p,
false, strcmp(p->language,
"english") != 0);
321 if (_errors != 0)
throw std::exception();
339 if (
id >= _current_data->
cur_language->lines.size())
return GetStringPtr(STR_UNDEFINED);
351 if (_current_data ==
nullptr)
return;
353 HSQUIRRELVM vm = engine->
GetVM();
354 sq_pushroottable(vm);
355 sq_pushstring(vm,
"GSText", -1);
356 if (SQ_FAILED(sq_get(vm, -2)))
return;
360 sq_pushstring(vm, p.c_str(), -1);
361 sq_pushinteger(vm, idx);
376 if (_current_data ==
nullptr)
return;
382 char *l = strrchr(temp,
'.');
383 assert(l !=
nullptr);
387 char *
language = strrchr(temp, PATHSEPCHAR);
388 assert(language !=
nullptr);
392 if (strcmp(p->language, language) == 0) {
std::vector< std::shared_ptr< LanguageStrings > > compiled_strings
The compiled strings per language, first must be English/the master language!.
LanguageStrings(const char *language, const char *end=nullptr)
Create a new container for language strings.
Class for writing an encoded language.
virtual void WriteLang(const StringData &data)
Actually write the language.
int _cur_line
The current line we're parsing in the input file.
Container for all the game strings.
void WriteStringID(const char *name, int stringid)
Write the string ID.
A reader that simply reads using fopen.
void RegisterGameTranslation(Squirrel *engine)
Register the current translation to the Squirrel engine.
uint Scan(const char *extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
const LanguageMetadata * _current_language
The currently loaded language.
Base functions regarding game texts.
Subdirectory for all game scripts.
#define lastof(x)
Get the last element of an fixed size array.
char * ReadLine(char *buffer, const char *last) override
Read a single line from the source of strings.
All static information from an Game like name, version, etc.
Helper for scanning for files with a given name.
const char * GetMainScript() const
Get the filename of the main.nut script.
Information about the currently known strings.
TranslationWriter(StringList &strings)
Writer for the encoded data.
void Scan(const char *directory)
Scan.
LanguageScanner(GameStrings *gs, const char *exclude)
Initialise.
Scanner to find language files in a GameScript directory.
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
void Compile()
Compile the language.
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
GameStrings * _current_data
The currently loaded game strings.
Container for the raw (unencoded) language strings of a language.
StringList lines
The lines of the file to pass into the parser/encoder.
Auto-close a file upon scope exit.
uint Version() const
Make a hash of the file to get a unique "version number".
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
StringList::const_iterator p
The current location of the iteration.
void WriteHeader(const LanguagePackHeader *header)
Write the header metadata.
GameStrings * LoadTranslations()
Load all translations that we know of.
void FreeTranslation()
Free all data related to the translation.
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
Check whether the given file exists.
static class GameInfo * GetInfo()
Get the current GameInfo.
HSQUIRRELVM GetVM()
Get the squirrel VM.
void Finalise()
Finalise writing the file.
StringList & strings
The encoded strings.
virtual void ParseFile()
Start parsing the file.
#define DEBUG(name, level,...)
Output a line of debugging information.
const char * _file
The filename of the input, so we can refer to it in errors/warnings.
GameInfo keeps track of all information of an Game, like Author, Description, ... ...
std::shared_ptr< LanguageStrings > cur_language
The current (compiled) language.
Base class for all language writers.
StringList & strings
The string names.
std::vector< std::string > StringList
Type for a list of strings.
StringList::const_iterator end
The end of the iteration.
std::unique_ptr< LanguageStrings > ReadRawLanguageStrings(const char *file)
Read all the raw language strings from the given file.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
StringNameWriter(StringList &strings)
Writer for the string names.
Base functions for all Games.
~LanguageStrings()
Free everything.
std::vector< std::unique_ptr< LanguageStrings > > raw_strings
The raw strings per language, first must be English/the master language!.
void Write(const byte *buffer, size_t length)
Write a number of bytes.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Class for writing the string IDs.
void Finalise(const StringData &data)
Finalise writing the file.
const char * GetGameStringPtr(uint id)
Get the string pointer of a particular game string.
const char * language
Name of the language (base filename).
StringList string_names
The names of the compiled strings.
const char * GetTarFile() const
Get the filename of the tar the script is in.
Helper for reading strings.
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
Add a file with the given filename.
void WriteLength(uint length)
Write the length as a simple gamma.
StringListReader(StringData &data, const LanguageStrings &strings, bool master, bool translation)
Create the reader.