10 #include "../stdafx.h" 11 #include "../strgen/strgen.h" 13 #include "../fileio_func.h" 14 #include "../tar_type.h" 15 #include "../script/squirrel_class.hpp" 16 #include "../strings_func.h" 21 #include "table/strings.h" 26 #include "../safeguards.h" 28 void CDECL strgen_warning(
const char *s, ...)
39 void CDECL strgen_error(
const char *s, ...)
50 void NORETURN CDECL strgen_fatal(
const char *s, ...)
58 throw std::exception();
68 this->language =
stredup(language, end !=
nullptr ? end - 1 :
nullptr);
87 if (fh ==
nullptr)
return nullptr;
91 const char *langname = strrchr(file, PATHSEPCHAR);
92 if (langname ==
nullptr) {
99 if (*langname ==
'.' || *langname == 0)
return nullptr;
101 std::unique_ptr<LanguageStrings> ret(
new LanguageStrings(langname, strchr(langname,
'.')));
104 while (to_read != 0 && fgets(buffer,
sizeof(buffer), fh) !=
nullptr) {
105 size_t len = strlen(buffer);
109 while (i > 0 && (buffer[i - 1] ==
'\r' || buffer[i - 1] ==
'\n' || buffer[i - 1] ==
' ')) i--;
112 ret->lines.emplace_back(buffer, i);
130 StringList::const_iterator
p;
131 StringList::const_iterator
end;
145 char *
ReadLine(
char *buffer,
const char *last)
override 147 if (this->p == this->end)
return nullptr;
149 strecpy(buffer, this->p->c_str(), last);
183 void Write(
const byte *buffer,
size_t length)
185 this->strings.emplace_back((
const char *)buffer, length);
203 if (stringid == (
int)this->strings.size()) this->strings.emplace_back(name);
228 void Scan(
const char *directory)
233 bool AddFile(
const char *filename,
size_t basepath_length,
const char *tar_filename)
override 235 if (strcmp(filename, exclude) == 0)
return true;
238 if (ls ==
nullptr)
return false;
254 char *e = strrchr(filename, PATHSEPCHAR);
255 if (e ==
nullptr)
return nullptr;
262 if (ls ==
nullptr)
return nullptr;
271 size_t len = strlen(filename);
273 const char *tar_filename = info->
GetTarFile();
274 TarList::iterator iter;
275 if (tar_filename !=
nullptr && (iter = _tar_list[
GAME_DIR].find(tar_filename)) != _tar_list[
GAME_DIR].end()) {
278 TarFileList::iterator tar;
281 if (tar->second.tar_filename != iter->first)
continue;
284 if (tar->first.size() <= len || tar->first.compare(0, len, filename) != 0)
continue;
285 if (tar->first.compare(tar->first.size() - 4, 4,
".txt") != 0)
continue;
287 scanner.
AddFile(tar->first.c_str(), 0, tar_filename);
291 scanner.
Scan(filename);
308 if (_errors != 0)
throw std::exception();
310 this->version = data.
Version();
315 for (
const auto &p : this->raw_strings) {
317 StringListReader translation_reader(data, *p,
false, strcmp(p->language,
"english") != 0);
319 if (_errors != 0)
throw std::exception();
337 if (
id >= _current_data->
cur_language->lines.size())
return GetStringPtr(STR_UNDEFINED);
349 if (_current_data ==
nullptr)
return;
351 HSQUIRRELVM vm = engine->
GetVM();
352 sq_pushroottable(vm);
353 sq_pushstring(vm,
"GSText", -1);
354 if (SQ_FAILED(sq_get(vm, -2)))
return;
358 sq_pushstring(vm, p.c_str(), -1);
359 sq_pushinteger(vm, idx);
374 if (_current_data ==
nullptr)
return;
380 char *l = strrchr(temp,
'.');
381 assert(l !=
nullptr);
385 char *
language = strrchr(temp, PATHSEPCHAR);
386 assert(language !=
nullptr);
390 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.