OpenTTD
game_text.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../strgen/strgen.h"
12 #include "../debug.h"
13 #include "../fileio_func.h"
14 #include "../tar_type.h"
15 #include "../script/squirrel_class.hpp"
16 #include "../strings_func.h"
17 #include "game_text.hpp"
18 #include "game.hpp"
19 #include "game_info.hpp"
20 
21 #include "table/strings.h"
22 
23 #include <stdarg.h>
24 #include <memory>
25 
26 #include "../safeguards.h"
27 
28 void CDECL strgen_warning(const char *s, ...)
29 {
30  char buf[1024];
31  va_list va;
32  va_start(va, s);
33  vseprintf(buf, lastof(buf), s, va);
34  va_end(va);
35  DEBUG(script, 0, "%s:%d: warning: %s", _file, _cur_line, buf);
36  _warnings++;
37 }
38 
39 void CDECL strgen_error(const char *s, ...)
40 {
41  char buf[1024];
42  va_list va;
43  va_start(va, s);
44  vseprintf(buf, lastof(buf), s, va);
45  va_end(va);
46  DEBUG(script, 0, "%s:%d: error: %s", _file, _cur_line, buf);
47  _errors++;
48 }
49 
50 void NORETURN CDECL strgen_fatal(const char *s, ...)
51 {
52  char buf[1024];
53  va_list va;
54  va_start(va, s);
55  vseprintf(buf, lastof(buf), s, va);
56  va_end(va);
57  DEBUG(script, 0, "%s:%d: FATAL: %s", _file, _cur_line, buf);
58  throw std::exception();
59 }
60 
66 LanguageStrings::LanguageStrings(const char *language, const char *end)
67 {
68  this->language = stredup(language, end != nullptr ? end - 1 : nullptr);
69 }
70 
73 {
74  free(this->language);
75 }
76 
82 std::unique_ptr<LanguageStrings> ReadRawLanguageStrings(const char *file)
83 {
84  try {
85  size_t to_read;
86  FILE *fh = FioFOpenFile(file, "rb", GAME_DIR, &to_read);
87  if (fh == nullptr) return nullptr;
88 
89  FileCloser fhClose(fh);
90 
91  const char *langname = strrchr(file, PATHSEPCHAR);
92  if (langname == nullptr) {
93  langname = file;
94  } else {
95  langname++;
96  }
97 
98  /* Check for invalid empty filename */
99  if (*langname == '.' || *langname == 0) return nullptr;
100 
101  std::unique_ptr<LanguageStrings> ret(new LanguageStrings(langname, strchr(langname, '.')));
102 
103  char buffer[2048];
104  while (to_read != 0 && fgets(buffer, sizeof(buffer), fh) != nullptr) {
105  size_t len = strlen(buffer);
106 
107  /* Remove trailing spaces/newlines from the string. */
108  size_t i = len;
109  while (i > 0 && (buffer[i - 1] == '\r' || buffer[i - 1] == '\n' || buffer[i - 1] == ' ')) i--;
110  buffer[i] = '\0';
111 
112  ret->lines.emplace_back(buffer, i);
113 
114  if (len > to_read) {
115  to_read = 0;
116  } else {
117  to_read -= len;
118  }
119  }
120 
121  return ret;
122  } catch (...) {
123  return nullptr;
124  }
125 }
126 
127 
130  StringList::const_iterator p;
131  StringList::const_iterator end;
132 
140  StringListReader(StringData &data, const LanguageStrings &strings, bool master, bool translation) :
141  StringReader(data, strings.language, master, translation), p(strings.lines.begin()), end(strings.lines.end())
142  {
143  }
144 
145  char *ReadLine(char *buffer, const char *last) override
146  {
147  if (this->p == this->end) return nullptr;
148 
149  strecpy(buffer, this->p->c_str(), last);
150  this->p++;
151 
152  return buffer;
153  }
154 };
155 
159 
164  TranslationWriter(StringList &strings) : strings(strings)
165  {
166  }
167 
168  void WriteHeader(const LanguagePackHeader *header)
169  {
170  /* We don't use the header. */
171  }
172 
173  void Finalise()
174  {
175  /* Nothing to do. */
176  }
177 
178  void WriteLength(uint length)
179  {
180  /* We don't write the length. */
181  }
182 
183  void Write(const byte *buffer, size_t length)
184  {
185  this->strings.emplace_back((const char *)buffer, length);
186  }
187 };
188 
192 
197  StringNameWriter(StringList &strings) : strings(strings)
198  {
199  }
200 
201  void WriteStringID(const char *name, int stringid)
202  {
203  if (stringid == (int)this->strings.size()) this->strings.emplace_back(name);
204  }
205 
206  void Finalise(const StringData &data)
207  {
208  /* Nothing to do. */
209  }
210 };
211 
215 class LanguageScanner : protected FileScanner {
216 private:
217  GameStrings *gs;
218  char *exclude;
219 
220 public:
222  LanguageScanner(GameStrings *gs, const char *exclude) : gs(gs), exclude(stredup(exclude)) {}
223  ~LanguageScanner() { free(exclude); }
224 
228  void Scan(const char *directory)
229  {
230  this->FileScanner::Scan(".txt", directory, false);
231  }
232 
233  bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
234  {
235  if (strcmp(filename, exclude) == 0) return true;
236 
237  auto ls = ReadRawLanguageStrings(filename);
238  if (ls == nullptr) return false;
239 
240  gs->raw_strings.push_back(std::move(ls));
241  return true;
242  }
243 };
244 
250 {
251  const GameInfo *info = Game::GetInfo();
252  char filename[512];
253  strecpy(filename, info->GetMainScript(), lastof(filename));
254  char *e = strrchr(filename, PATHSEPCHAR);
255  if (e == nullptr) return nullptr;
256  e++; // Make 'e' point after the PATHSEPCHAR
257 
258  strecpy(e, "lang" PATHSEP "english.txt", lastof(filename));
259  if (!FioCheckFileExists(filename, GAME_DIR)) return nullptr;
260 
261  auto ls = ReadRawLanguageStrings(filename);
262  if (ls == nullptr) return nullptr;
263 
264  GameStrings *gs = new GameStrings();
265  try {
266  gs->raw_strings.push_back(std::move(ls));
267 
268  /* Scan for other language files */
269  LanguageScanner scanner(gs, filename);
270  strecpy(e, "lang" PATHSEP, lastof(filename));
271  size_t len = strlen(filename);
272 
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()) {
276  /* The main script is in a tar file, so find all files that
277  * are in the same tar and add them to the langfile scanner. */
278  TarFileList::iterator tar;
279  FOR_ALL_TARS(tar, GAME_DIR) {
280  /* Not in the same tar. */
281  if (tar->second.tar_filename != iter->first) continue;
282 
283  /* Check the path and extension. */
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;
286 
287  scanner.AddFile(tar->first.c_str(), 0, tar_filename);
288  }
289  } else {
290  /* Scan filesystem */
291  scanner.Scan(filename);
292  }
293 
294  gs->Compile();
295  return gs;
296  } catch (...) {
297  delete gs;
298  return nullptr;
299  }
300 }
301 
304 {
305  StringData data(32);
306  StringListReader master_reader(data, *this->raw_strings[0], true, false);
307  master_reader.ParseFile();
308  if (_errors != 0) throw std::exception();
309 
310  this->version = data.Version();
311 
312  StringNameWriter id_writer(this->string_names);
313  id_writer.WriteHeader(data);
314 
315  for (const auto &p : this->raw_strings) {
316  data.FreeTranslation();
317  StringListReader translation_reader(data, *p, false, strcmp(p->language, "english") != 0);
318  translation_reader.ParseFile();
319  if (_errors != 0) throw std::exception();
320 
321  this->compiled_strings.emplace_back(new LanguageStrings(p->language));
322  TranslationWriter writer(this->compiled_strings.back()->lines);
323  writer.WriteLang(data);
324  }
325 }
326 
329 
335 const char *GetGameStringPtr(uint id)
336 {
337  if (id >= _current_data->cur_language->lines.size()) return GetStringPtr(STR_UNDEFINED);
338  return _current_data->cur_language->lines[id].c_str();
339 }
340 
346 {
347  delete _current_data;
348  _current_data = LoadTranslations();
349  if (_current_data == nullptr) return;
350 
351  HSQUIRRELVM vm = engine->GetVM();
352  sq_pushroottable(vm);
353  sq_pushstring(vm, "GSText", -1);
354  if (SQ_FAILED(sq_get(vm, -2))) return;
355 
356  int idx = 0;
357  for (const auto &p : _current_data->string_names) {
358  sq_pushstring(vm, p.c_str(), -1);
359  sq_pushinteger(vm, idx);
360  sq_rawset(vm, -3);
361  idx++;
362  }
363 
364  sq_pop(vm, 2);
365 
367 }
368 
373 {
374  if (_current_data == nullptr) return;
375 
376  char temp[MAX_PATH];
377  strecpy(temp, _current_language->file, lastof(temp));
378 
379  /* Remove the extension */
380  char *l = strrchr(temp, '.');
381  assert(l != nullptr);
382  *l = '\0';
383 
384  /* Skip the path */
385  char *language = strrchr(temp, PATHSEPCHAR);
386  assert(language != nullptr);
387  language++;
388 
389  for (auto &p : _current_data->compiled_strings) {
390  if (strcmp(p->language, language) == 0) {
391  _current_data->cur_language = p;
392  return;
393  }
394  }
395 
396  _current_data->cur_language = _current_data->compiled_strings[0];
397 }
std::vector< std::shared_ptr< LanguageStrings > > compiled_strings
The compiled strings per language, first must be English/the master language!.
Definition: game_text.hpp:34
LanguageStrings(const char *language, const char *end=nullptr)
Create a new container for language strings.
Definition: game_text.cpp:66
Class for writing an encoded language.
Definition: game_text.cpp:157
virtual void WriteLang(const StringData &data)
Actually write the language.
int _cur_line
The current line we&#39;re parsing in the input file.
Definition: strgen_base.cpp:27
Container for all the game strings.
Definition: game_text.hpp:29
void WriteStringID(const char *name, int stringid)
Write the string ID.
Definition: game_text.cpp:201
A reader that simply reads using fopen.
Definition: game_text.cpp:129
void RegisterGameTranslation(Squirrel *engine)
Register the current translation to the Squirrel engine.
Definition: game_text.cpp:345
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.
Definition: fileio.cpp:1373
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:60
const LanguageMetadata * _current_language
The currently loaded language.
Definition: strings.cpp:46
Base functions regarding game texts.
Subdirectory for all game scripts.
Definition: fileio_type.h:121
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
char * ReadLine(char *buffer, const char *last) override
Read a single line from the source of strings.
Definition: game_text.cpp:145
All static information from an Game like name, version, etc.
Definition: game_info.hpp:16
Helper for scanning for files with a given name.
Definition: fileio_func.h:70
const char * GetMainScript() const
Get the filename of the main.nut script.
Definition: script_info.hpp:92
Information about the currently known strings.
Definition: strgen.h:41
TranslationWriter(StringList &strings)
Writer for the encoded data.
Definition: game_text.cpp:164
void Scan(const char *directory)
Scan.
Definition: game_text.cpp:228
LanguageScanner(GameStrings *gs, const char *exclude)
Initialise.
Definition: game_text.cpp:222
Header of a language file.
Definition: language.h:24
Scanner to find language files in a GameScript directory.
Definition: game_text.cpp:215
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
Definition: game_text.cpp:372
void Compile()
Compile the language.
Definition: game_text.cpp:303
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:463
GameStrings * _current_data
The currently loaded game strings.
Definition: game_text.cpp:328
Container for the raw (unencoded) language strings of a language.
Definition: game_text.hpp:20
StringList lines
The lines of the file to pass into the parser/encoder.
Definition: game_text.hpp:22
Auto-close a file upon scope exit.
Definition: fileio_func.h:151
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.
Definition: string.cpp:136
StringList::const_iterator p
The current location of the iteration.
Definition: game_text.cpp:130
void WriteHeader(const LanguagePackHeader *header)
Write the header metadata.
Definition: game_text.cpp:168
GameStrings * LoadTranslations()
Load all translations that we know of.
Definition: game_text.cpp:249
void FreeTranslation()
Free all data related to the translation.
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
Check whether the given file exists.
Definition: fileio.cpp:310
static class GameInfo * GetInfo()
Get the current GameInfo.
Definition: game.hpp:80
char file[MAX_PATH]
Name of the file we read this data from.
Definition: language.h:93
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:80
void Finalise()
Finalise writing the file.
Definition: game_text.cpp:173
StringList & strings
The encoded strings.
Definition: game_text.cpp:158
virtual void ParseFile()
Start parsing the file.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
const char * _file
The filename of the input, so we can refer to it in errors/warnings.
Definition: strgen_base.cpp:26
GameInfo keeps track of all information of an Game, like Author, Description, ... ...
std::shared_ptr< LanguageStrings > cur_language
The current (compiled) language.
Definition: game_text.hpp:31
Base class for all language writers.
Definition: strgen.h:112
StringList & strings
The string names.
Definition: game_text.cpp:191
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
StringList::const_iterator end
The end of the iteration.
Definition: game_text.cpp:131
std::unique_ptr< LanguageStrings > ReadRawLanguageStrings(const char *file)
Read all the raw language strings from the given file.
Definition: game_text.cpp:82
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
StringNameWriter(StringList &strings)
Writer for the string names.
Definition: game_text.cpp:197
Base functions for all Games.
~LanguageStrings()
Free everything.
Definition: game_text.cpp:72
std::vector< std::unique_ptr< LanguageStrings > > raw_strings
The raw strings per language, first must be English/the master language!.
Definition: game_text.hpp:33
void Write(const byte *buffer, size_t length)
Write a number of bytes.
Definition: game_text.cpp:183
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
Base class for writing the header, i.e.
Definition: strgen.h:91
Class for writing the string IDs.
Definition: game_text.cpp:190
void Finalise(const StringData &data)
Finalise writing the file.
Definition: game_text.cpp:206
const char * GetGameStringPtr(uint id)
Get the string pointer of a particular game string.
Definition: game_text.cpp:335
const char * language
Name of the language (base filename).
Definition: game_text.hpp:21
StringList string_names
The names of the compiled strings.
Definition: game_text.hpp:35
const char * GetTarFile() const
Get the filename of the tar the script is in.
Definition: script_info.hpp:97
Helper for reading strings.
Definition: strgen.h:60
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
Add a file with the given filename.
Definition: game_text.cpp:233
void WriteLength(uint length)
Write the length as a simple gamma.
Definition: game_text.cpp:178
void WriteHeader(const StringData &data)
Write the header information.
StringListReader(StringData &data, const LanguageStrings &strings, bool master, bool translation)
Create the reader.
Definition: game_text.cpp:140