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"
22#include "../table/strgen_tables.h"
24#include "../safeguards.h"
26void CDECL StrgenWarningI(
const std::string &msg)
32void CDECL StrgenErrorI(
const std::string &msg)
38void CDECL StrgenFatalI(
const std::string &msg)
41 throw std::exception();
55 auto pos = file.rfind(PATHSEPCHAR);
57 std::string langname = file.substr(pos + 1);
60 if (langname.empty() || langname.front() ==
'.')
return LanguageStrings();
65 while (to_read != 0 && fgets(buffer,
sizeof(buffer), *fh) !=
nullptr) {
66 std::string_view view{buffer};
69 if (view.size() > to_read) {
72 to_read -= view.size();
82 StringList::const_iterator
p;
83 StringList::const_iterator
end;
99 if (this->p == this->end)
return std::nullopt;
131 void Write(std::string_view buffer)
override
133 this->strings.emplace_back(buffer);
131 void Write(std::string_view buffer)
override {
…}
151 if (stringid == this->strings.size()) this->strings.emplace_back(name);
165 std::weak_ptr<GameStrings> gs;
170 LanguageScanner(std::weak_ptr<GameStrings> gs,
const std::string &exclude) : gs(gs), exclude(exclude) {}
175 void Scan(
const std::string &directory)
175 void Scan(
const std::string &directory) {
…}
180 bool AddFile(
const std::string &filename,
size_t,
const std::string &)
override
182 if (exclude == filename)
return true;
185 if (!ls.IsValid())
return false;
187 if (
auto sp = this->gs.lock()) {
188 sp->raw_strings.push_back(std::move(ls));
180 bool AddFile(
const std::string &filename,
size_t,
const std::string &)
override {
…}
203 assert(info !=
nullptr);
205 auto e = basename.rfind(PATHSEPCHAR);
206 if (e == std::string::npos)
return nullptr;
207 basename.erase(e + 1);
209 std::string filename = basename +
"lang" PATHSEP
"english.txt";
213 if (!ls.IsValid())
return nullptr;
215 auto gs = std::make_shared<GameStrings>();
217 gs->raw_strings.push_back(std::move(ls));
221 std::string ldir = basename +
"lang" PATHSEP;
223 const std::string tar_filename = info->
GetTarFile();
224 TarList::iterator iter;
225 if (!tar_filename.empty() && (iter = _tar_list[
GAME_DIR].find(tar_filename)) != _tar_list[
GAME_DIR].end()) {
228 for (
const auto &tar : _tar_filelist[
GAME_DIR]) {
230 if (tar.second.tar_filename != iter->first)
continue;
233 if (tar.first.size() <= ldir.size() || tar.first.compare(0, ldir.size(), ldir) != 0)
continue;
234 if (tar.first.compare(tar.first.size() - 4, 4,
".txt") != 0)
continue;
236 scanner.
AddFile(tar.first, 0, tar_filename);
250static StringParam::ParamType GetParamType(
const CmdStruct *cs)
252 if (cs->value == SCC_RAW_STRING_POINTER)
return StringParam::RAW_STRING;
253 if (cs->value == SCC_STRING || cs != TranslateCmdForCompare(cs))
return StringParam::STRING;
254 return StringParam::OTHER;
257static void ExtractStringParams(
const StringData &data, StringParamsList ¶ms)
263 StringParams ¶m = params.emplace_back();
266 for (
auto it = pcs.consuming_commands.begin(); it != pcs.consuming_commands.end(); it++) {
267 if (*it ==
nullptr) {
269 if (std::all_of(it, pcs.consuming_commands.end(), [](
auto cs) { return cs == nullptr; }))
break;
270 param.emplace_back(StringParam::UNUSED, 1);
274 param.emplace_back(GetParamType(cs), cs->consumes, cs->cmd);
286 if (_strgen.errors != 0)
throw std::exception();
297 StringListReader translation_reader(data, p,
false, p.language !=
"english");
299 if (_strgen.errors != 0)
throw std::exception();
329 static StringParams empty;
343 static const std::string undefined =
"STR_UNDEFINED";
358 HSQUIRRELVM vm = engine.
GetVM();
359 sq_pushroottable(vm);
360 sq_pushstring(vm,
"GSText");
361 if (SQ_FAILED(sq_get(vm, -2)))
return;
365 sq_pushstring(vm, p);
366 sq_pushinteger(vm, idx);
373 ScriptText::SetPadParameterCount(vm);
387 if (p.language == language) {
Helper for scanning for files with a given name.
uint Scan(std::string_view extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
All static information from an Game like name, version, etc.
static class GameInfo * GetInfo()
Get the current GameInfo.
Scanner to find language files in a GameScript directory.
LanguageScanner(std::weak_ptr< GameStrings > gs, const std::string &exclude)
Initialise.
void Scan(const std::string &directory)
Scan.
bool AddFile(const std::string &filename, size_t, const std::string &) override
Add a file with the given filename.
const std::string & GetMainScript() const
Get the filename of the main.nut script.
const std::string & GetTarFile() const
Get the filename of the tar the script is in.
HSQUIRRELVM GetVM()
Get the squirrel VM.
static const std::string_view WHITESPACE_OR_NEWLINE
ASCII whitespace characters, including new-line.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
bool FioCheckFileExists(std::string_view filename, Subdirectory subdir)
Check whether the given file exists.
@ GAME_DIR
Subdirectory for all game scripts.
Base functions for all Games.
GameInfo keeps track of all information of an Game, like Author, Description, ...
void RegisterGameTranslation(Squirrel &engine)
Register the current translation to the Squirrel engine.
const std::string & GetGameStringName(StringIndexInTab id)
Get the name of a particular game string.
static std::shared_ptr< GameStrings > LoadTranslations()
Load all translations that we know of.
const StringParams & GetGameStringParams(StringIndexInTab id)
Get the string parameters of a particular game string.
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
LanguageStrings ReadRawLanguageStrings(const std::string &file)
Read all the raw language strings from the given file.
std::string_view GetGameStringPtr(StringIndexInTab id)
Get the string pointer of a particular game string.
std::shared_ptr< GameStrings > _current_gamestrings_data
The currently loaded game strings.
Base functions regarding game texts.
const LanguageMetadata * _current_language
The currently loaded language.
std::vector< std::string > StringList
Type for a list of strings.
ReferenceThroughBaseContainer< StringList > string_names
The names of the compiled strings.
std::vector< LanguageStrings > raw_strings
The raw strings per language, first must be English/the master language!.
std::vector< LanguageStrings > compiled_strings
The compiled strings per language, first must be English/the master language!.
uint version
The version of the language strings.
ReferenceThroughBaseContainer< StringParamsList > string_params
The parameters for the strings.
void Compile()
Compile the language.
Information about a single string.
std::string english
English text.
Container for the raw (unencoded) language strings of a language.
ReferenceThroughBaseContainer< StringList > lines
The lines of the file to pass into the parser/encoder.
Base class for all language writers.
virtual void WriteLang(const StringData &data)
Actually write the language.
std::string file
The filename of the input, so we can refer to it in errors/warnings.
size_t cur_line
The current line we're parsing in the input file.
Information about the currently known strings.
size_t max_strings
The maximum number of strings.
void FreeTranslation()
Free all data related to the translation.
std::vector< std::shared_ptr< LangString > > strings
List of all known strings.
uint32_t Version() const
Make a hash of the file to get a unique "version number".
A reader that simply reads using fopen.
StringList::const_iterator p
The current location of the iteration.
StringList::const_iterator end
The end of the iteration.
StringListReader(StringData &data, const LanguageStrings &strings, bool master, bool translation)
Create the reader.
std::optional< std::string > ReadLine() override
Read a single line from the source of strings.
Class for writing the string IDs.
void WriteStringID(const std::string &name, size_t stringid) override
Write the string ID.
void Finalise(const StringData &) override
Finalise writing the file.
StringNameWriter(StringList &strings)
Writer for the string names.
StringList & strings
The string names.
Helper for reading strings.
StringData & data
The data to fill during reading.
virtual void ParseFile()
Start parsing the file.
bool translation
Are we reading a translation, implies !master. However, the base translation will have this false.
bool master
Are we reading the master file?
Templated helper to make a type-safe 'typedef' representing a single POD value.
Class for writing an encoded language.
void Write(std::string_view buffer) override
Write a number of bytes.
void WriteLength(size_t) override
Write the length as a simple gamma.
StringList & strings
The encoded strings.
void WriteHeader(const LanguagePackHeader *) override
Write the header metadata.
TranslationWriter(StringList &strings)
Writer for the encoded data.
void Finalise() override
Finalise writing the file.
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.