OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
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();
74 auto pos = file.rfind(PATHSEPCHAR);
76 std::string langname = file.substr(pos + 1);
79 if (langname.empty() || langname.front() ==
'.')
return LanguageStrings();
84 while (to_read != 0 && fgets(buffer,
sizeof(buffer), fh) !=
nullptr) {
85 size_t len = strlen(buffer);
89 while (i > 0 && (buffer[i - 1] ==
'\r' || buffer[i - 1] ==
'\n' || buffer[i - 1] ==
' ')) i--;
92 ret.
lines.emplace_back(buffer, i);
107 StringList::const_iterator
p;
108 StringList::const_iterator
end;
122 char *
ReadLine(
char *buffer,
const char *last)
override
124 if (this->p == this->end)
return nullptr;
126 strecpy(buffer, this->p->c_str(), last);
160 void Write(
const byte *buffer,
size_t length)
162 this->strings.emplace_back((
const char *)buffer, length);
180 if (stringid == (
int)this->strings.size()) this->strings.emplace_back(name);
204 void Scan(
const char *directory)
209 bool AddFile(
const std::string &filename,
size_t basepath_length,
const std::string &tar_filename)
override
211 if (exclude == filename)
return true;
214 if (!ls.IsValid())
return false;
229 auto e = basename.rfind(PATHSEPCHAR);
230 if (e == std::string::npos)
return nullptr;
231 basename.erase(e + 1);
233 std::string filename = basename +
"lang" PATHSEP
"english.txt";
237 if (!ls.IsValid())
return nullptr;
245 std::string ldir = basename +
"lang" PATHSEP;
247 const std::string tar_filename = info->
GetTarFile();
248 TarList::iterator iter;
249 if (!tar_filename.empty() && (iter = _tar_list[
GAME_DIR].find(tar_filename)) != _tar_list[
GAME_DIR].end()) {
252 for (
const auto &tar : _tar_filelist[
GAME_DIR]) {
254 if (tar.second.tar_filename != iter->first)
continue;
257 if (tar.first.size() <= ldir.size() || tar.first.compare(0, ldir.size(), ldir) != 0)
continue;
258 if (tar.first.compare(tar.first.size() - 4, 4,
".txt") != 0)
continue;
260 scanner.
AddFile(tar.first, 0, tar_filename);
264 scanner.
Scan(ldir.c_str());
281 if (_errors != 0)
throw std::exception();
290 StringListReader translation_reader(data, p,
false, p.language !=
"english");
292 if (_errors != 0)
throw std::exception();
324 HSQUIRRELVM vm = engine->
GetVM();
325 sq_pushroottable(vm);
326 sq_pushstring(vm,
"GSText", -1);
327 if (SQ_FAILED(sq_get(vm, -2)))
return;
331 sq_pushstring(vm, p.c_str(), -1);
332 sq_pushinteger(vm, idx);
353 char *l = strrchr(temp,
'.');
354 assert(l !=
nullptr);
358 char *language = strrchr(temp, PATHSEPCHAR);
359 assert(language !=
nullptr);
363 if (p.language == language) {
const char * _file
The filename of the input, so we can refer to it in errors/warnings.
void WriteStringID(const char *name, int stringid)
Write the string ID.
void WriteHeader(const LanguagePackHeader *header)
Write the header metadata.
const char * GetMainScript() const
Get the filename of the main.nut script.
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
uint version
The version of the language strings.
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
void Write(const byte *buffer, size_t length)
Write a number of bytes.
static class GameInfo * GetInfo()
Get the current GameInfo.
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.
Class for writing an encoded language.
HSQUIRRELVM GetVM()
Get the squirrel VM.
LanguageScanner(GameStrings *gs, const std::string &exclude)
Initialise.
int _cur_line
The current line we're parsing in the input file.
std::string GetTarFile() const
Get the filename of the tar the script is in.
virtual void WriteLang(const StringData &data)
Actually write the language.
Class for writing the string IDs.
bool master
Are we reading the master file?
StringList string_names
The names of the compiled strings.
uint Version() const
Make a hash of the file to get a unique "version number".
Auto-close a file upon scope exit.
Information about the currently known strings.
StringList::const_iterator p
The current location of the iteration.
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
const char * GetGameStringPtr(uint id)
Get the string pointer of a particular game string.
char * ReadLine(char *buffer, const char *last) override
Read a single line from the source of strings.
void Finalise()
Finalise writing the file.
Scanner to find language files in a GameScript directory.
void RegisterGameTranslation(Squirrel *engine)
Register the current translation to the Squirrel engine.
std::vector< std::string > StringList
Type for a list of strings.
const LanguageMetadata * _current_language
The currently loaded language.
@ GAME_DIR
Subdirectory for all game scripts.
void FreeTranslation()
Free all data related to the translation.
std::vector< LanguageStrings > raw_strings
The raw strings per language, first must be English/the master language!.
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
A reader that simply reads using fopen.
Container for the raw (unencoded) language strings of a language.
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
StringData & data
The data to fill during reading.
TranslationWriter(StringList &strings)
Writer for the encoded data.
All static information from an Game like name, version, etc.
void WriteLength(uint length)
Write the length as a simple gamma.
bool translation
Are we reading a translation, implies !master. However, the base translation will have this false.
virtual void ParseFile()
Start parsing the file.
LanguageStrings * cur_language
The current (compiled) language.
StringList::const_iterator end
The end of the iteration.
StringList & strings
The string names.
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
void Finalise(const StringData &data)
Finalise writing the file.
Base class for all language writers.
GameStrings * _current_data
The currently loaded game strings.
Helper for scanning for files with a given name.
StringList & strings
The encoded strings.
std::vector< LanguageStrings > compiled_strings
The compiled strings per language, first must be English/the master language!.
StringNameWriter(StringList &strings)
Writer for the string names.
StringList lines
The lines of the file to pass into the parser/encoder.
StringListReader(StringData &data, const LanguageStrings &strings, bool master, bool translation)
Create the reader.
void Compile()
Compile the language.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
void Scan(const char *directory)
Scan.
GameStrings * LoadTranslations()
Load all translations that we know of.
#define lastof(x)
Get the last element of an fixed size array.
Helper for reading strings.
Container for all the game strings.
LanguageStrings ReadRawLanguageStrings(const std::string &file)
Read all the raw language strings from the given file.