Files
OpenRCT2-Web/src/openrct2/localisation/LanguagePack.cpp
T

695 lines
20 KiB
C++
Raw Normal View History

/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 23:01:56 +02:00
#include "LanguagePack.h"
#include "../common.h"
2015-09-02 22:00:02 +01:00
#include "../core/FileStream.hpp"
#include "../core/Memory.hpp"
2016-09-19 19:42:17 +01:00
#include "../core/String.hpp"
2015-09-02 22:00:02 +01:00
#include "../core/StringBuilder.hpp"
#include "../core/StringReader.hpp"
2018-07-26 14:26:43 +02:00
#include "Language.h"
2018-06-22 23:01:56 +02:00
#include "Localisation.h"
#include <algorithm>
#include <string>
#include <vector>
#ifndef _WIN32
2018-07-26 15:02:55 +02:00
# include <unicode/ubidi.h>
# include <unicode/unistr.h>
# include <unicode/ushape.h>
# include <unicode/ustring.h>
# include <unicode/utf.h>
# include <unicode/utypes.h>
#endif
2015-08-31 23:06:11 +01:00
2016-01-23 18:58:31 +00:00
// Don't try to load more than language files that exceed 64 MiB
constexpr uint64_t MAX_LANGUAGE_SIZE = 64 * 1024 * 1024;
constexpr uint64_t MAX_OBJECT_OVERRIDES = 4096;
constexpr uint64_t MAX_SCENARIO_OVERRIDES = 4096;
2018-06-22 23:01:56 +02:00
constexpr rct_string_id ObjectOverrideBase = 0x6000;
constexpr int32_t ObjectOverrideMaxStringCount = 3;
2015-09-08 18:46:22 +01:00
2018-06-22 23:01:56 +02:00
constexpr rct_string_id ScenarioOverrideBase = 0x7000;
constexpr int32_t ScenarioOverrideMaxStringCount = 3;
2016-01-23 18:58:31 +00:00
struct ObjectOverride
2015-08-31 23:06:11 +01:00
{
2018-06-22 23:01:56 +02:00
char name[8] = { 0 };
2018-01-02 18:51:29 +01:00
std::string strings[ObjectOverrideMaxStringCount];
};
2015-08-31 23:06:11 +01:00
struct ScenarioOverride
{
std::string filename;
2018-01-02 18:51:29 +01:00
std::string strings[ScenarioOverrideMaxStringCount];
};
2016-10-04 19:14:29 +02:00
class LanguagePack final : public ILanguagePack
{
private:
uint16_t const _id;
2018-06-22 23:01:56 +02:00
std::vector<std::string> _strings;
std::vector<ObjectOverride> _objectOverrides;
std::vector<ScenarioOverride> _scenarioOverrides;
///////////////////////////////////////////////////////////////////////////
// Parsing work data
///////////////////////////////////////////////////////////////////////////
2018-06-22 23:01:56 +02:00
std::string _currentGroup;
ObjectOverride* _currentObjectOverride = nullptr;
ScenarioOverride* _currentScenarioOverride = nullptr;
public:
2018-06-22 23:01:56 +02:00
static LanguagePack* FromFile(uint16_t id, const utf8* path)
2016-01-23 18:58:31 +00:00
{
Guard::ArgumentNotNull(path);
// Load file directly into memory
2018-06-22 23:01:56 +02:00
utf8* fileData = nullptr;
try
{
FileStream fs = FileStream(path, FILE_MODE_OPEN);
2016-11-13 20:17:49 +01:00
size_t fileLength = (size_t)fs.GetLength();
if (fileLength > MAX_LANGUAGE_SIZE)
{
throw IOException("Language file too large.");
}
fileData = Memory::Allocate<utf8>(fileLength + 1);
fs.Read(fileData, fileLength);
fileData[fileLength] = '\0';
}
2018-06-22 23:01:56 +02:00
catch (const std::exception& ex)
2016-01-23 18:58:31 +00:00
{
Memory::Free(fileData);
2018-01-02 19:23:22 +00:00
log_error("Unable to open %s: %s", path, ex.what());
return nullptr;
2016-01-23 18:58:31 +00:00
}
2015-08-31 23:06:11 +01:00
// Parse the memory as text
2018-06-22 23:01:56 +02:00
LanguagePack* result = FromText(id, fileData);
2016-01-23 18:58:31 +00:00
Memory::Free(fileData);
return result;
2016-01-23 18:58:31 +00:00
}
2018-06-22 23:01:56 +02:00
static LanguagePack* FromText(uint16_t id, const utf8* text)
2016-01-23 18:58:31 +00:00
{
return new LanguagePack(id, text);
2016-01-23 18:58:31 +00:00
}
2015-08-31 23:06:11 +01:00
2018-06-22 23:01:56 +02:00
LanguagePack(uint16_t id, const utf8* text)
2017-12-05 17:42:04 +00:00
: _id(id)
2016-01-23 18:58:31 +00:00
{
Guard::ArgumentNotNull(text);
auto reader = UTF8StringReader(text);
while (reader.CanRead())
2016-01-23 18:58:31 +00:00
{
ParseLine(&reader);
}
2016-09-20 12:48:35 +01:00
// Clean up the parsing work data
2017-12-05 17:42:04 +00:00
_currentGroup = std::string();
2016-09-20 12:48:35 +01:00
_currentObjectOverride = nullptr;
_currentScenarioOverride = nullptr;
}
uint16_t GetId() const override
{
return _id;
}
uint32_t GetCount() const override
{
return (uint32_t)_strings.size();
}
2017-12-05 17:42:04 +00:00
void RemoveString(rct_string_id stringId) override
{
if (_strings.size() >= (size_t)stringId)
{
_strings[stringId] = std::string();
}
}
2018-06-22 23:01:56 +02:00
void SetString(rct_string_id stringId, const std::string& str) override
{
if (_strings.size() >= (size_t)stringId)
{
_strings[stringId] = str;
}
}
2018-06-22 23:01:56 +02:00
const utf8* GetString(rct_string_id stringId) const override
{
if (stringId >= ScenarioOverrideBase)
{
int32_t offset = stringId - ScenarioOverrideBase;
int32_t ooIndex = offset / ScenarioOverrideMaxStringCount;
int32_t ooStringIndex = offset % ScenarioOverrideMaxStringCount;
2017-12-16 12:06:00 +00:00
if (_scenarioOverrides.size() > (size_t)ooIndex && !_scenarioOverrides[ooIndex].strings[ooStringIndex].empty())
{
2017-12-05 17:42:04 +00:00
return _scenarioOverrides[ooIndex].strings[ooStringIndex].c_str();
}
2016-01-23 18:58:31 +00:00
else
{
return nullptr;
}
}
else if (stringId >= ObjectOverrideBase)
{
int32_t offset = stringId - ObjectOverrideBase;
int32_t ooIndex = offset / ObjectOverrideMaxStringCount;
int32_t ooStringIndex = offset % ObjectOverrideMaxStringCount;
2017-12-16 12:06:00 +00:00
if (_objectOverrides.size() > (size_t)ooIndex && !_objectOverrides[ooIndex].strings[ooStringIndex].empty())
{
2017-12-05 17:42:04 +00:00
return _objectOverrides[ooIndex].strings[ooStringIndex].c_str();
}
else
{
return nullptr;
}
}
else
{
2017-12-16 12:06:00 +00:00
if ((_strings.size() > (size_t)stringId) && !_strings[stringId].empty())
{
2017-12-05 17:42:04 +00:00
return _strings[stringId].c_str();
}
else
{
return nullptr;
}
}
}
2018-06-22 23:01:56 +02:00
rct_string_id GetObjectOverrideStringId(const char* objectIdentifier, uint8_t index) override
{
Guard::ArgumentNotNull(objectIdentifier);
Guard::Assert(index < ObjectOverrideMaxStringCount);
int32_t ooIndex = 0;
2018-06-22 23:01:56 +02:00
for (const ObjectOverride& objectOverride : _objectOverrides)
{
if (strncmp(objectOverride.name, objectIdentifier, 8) == 0)
{
2017-12-05 17:42:04 +00:00
if (objectOverride.strings[index].empty())
{
return STR_NONE;
}
return ObjectOverrideBase + (ooIndex * ObjectOverrideMaxStringCount) + index;
}
ooIndex++;
}
return STR_NONE;
}
2018-06-22 23:01:56 +02:00
rct_string_id GetScenarioOverrideStringId(const utf8* scenarioFilename, uint8_t index) override
{
Guard::ArgumentNotNull(scenarioFilename);
Guard::Assert(index < ScenarioOverrideMaxStringCount);
int32_t ooIndex = 0;
2018-06-22 23:01:56 +02:00
for (const ScenarioOverride& scenarioOverride : _scenarioOverrides)
{
if (String::Equals(scenarioOverride.filename.c_str(), scenarioFilename, true))
{
2017-12-05 17:42:04 +00:00
if (scenarioOverride.strings[index].empty())
{
return STR_NONE;
}
return ScenarioOverrideBase + (ooIndex * ScenarioOverrideMaxStringCount) + index;
}
ooIndex++;
}
return STR_NONE;
}
2017-12-05 17:42:04 +00:00
private:
2018-06-22 23:01:56 +02:00
ObjectOverride* GetObjectOverride(const std::string& objectIdentifier)
{
2018-06-22 23:01:56 +02:00
for (auto& oo : _objectOverrides)
{
2017-12-05 17:42:04 +00:00
if (strncmp(oo.name, objectIdentifier.c_str(), 8) == 0)
{
2017-12-06 00:12:36 +01:00
return &oo;
}
}
return nullptr;
}
2018-06-22 23:01:56 +02:00
ScenarioOverride* GetScenarioOverride(const std::string& scenarioIdentifier)
{
2018-06-22 23:01:56 +02:00
for (auto& so : _scenarioOverrides)
{
2017-12-05 17:42:04 +00:00
if (String::Equals(so.strings[0], scenarioIdentifier.c_str(), true))
{
2017-12-06 00:12:36 +01:00
return &so;
}
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Parsing
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2017-05-11 04:45:59 -05:00
// Partial support to open an uncompiled language file which parses tokens and converts them to the corresponding character
// code. Due to resource strings (strings in scenarios and objects) being written to the original game's string table,
// get_string will use those if the same entry in the loaded language is empty.
//
// Unsure at how the original game decides which entries to write resource strings to, but this could affect adding new
// strings for the time being. Further investigation is required.
//
2018-06-22 23:01:56 +02:00
// When reading the language files, the STR_XXXX part is read and XXXX becomes the string id number. Everything after the
// colon and before the new line will be saved as the string. Tokens are written with inside curly braces {TOKEN}. Use # at
// the beginning of a line to leave a comment.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static bool IsWhitespace(codepoint_t codepoint)
{
return codepoint == '\t' || codepoint == ' ' || codepoint == '\r' || codepoint == '\n';
}
static bool IsNewLine(codepoint_t codepoint)
{
return codepoint == '\r' || codepoint == '\n';
}
2018-06-22 23:01:56 +02:00
static void SkipWhitespace(IStringReader* reader)
{
codepoint_t codepoint;
while (reader->TryPeek(&codepoint))
{
if (IsWhitespace(codepoint))
{
reader->Skip();
}
else
{
break;
}
}
}
2018-06-22 23:01:56 +02:00
static void SkipNewLine(IStringReader* reader)
{
codepoint_t codepoint;
while (reader->TryPeek(&codepoint))
{
if (IsNewLine(codepoint))
{
reader->Skip();
}
else
{
break;
}
}
}
2018-06-22 23:01:56 +02:00
static void SkipToEndOfLine(IStringReader* reader)
{
codepoint_t codepoint;
2018-06-22 23:01:56 +02:00
while (reader->TryPeek(&codepoint))
{
if (codepoint != '\r' && codepoint != '\n')
{
reader->Skip();
}
else
{
break;
}
}
}
2018-06-22 23:01:56 +02:00
void ParseLine(IStringReader* reader)
{
SkipWhitespace(reader);
codepoint_t codepoint;
if (reader->TryPeek(&codepoint))
{
2018-06-22 23:01:56 +02:00
switch (codepoint)
{
case '#':
SkipToEndOfLine(reader);
break;
case '[':
ParseGroupObject(reader);
break;
case '<':
ParseGroupScenario(reader);
break;
case '\r':
case '\n':
break;
default:
ParseString(reader);
break;
}
SkipToEndOfLine(reader);
SkipNewLine(reader);
}
}
2018-06-22 23:01:56 +02:00
void ParseGroupObject(IStringReader* reader)
{
auto sb = StringBuilder();
codepoint_t codepoint;
// Should have already deduced that the next codepoint is a [
reader->Skip();
// Read string up to ] or line end
bool closedCorrectly = false;
while (reader->TryPeek(&codepoint))
{
2018-06-22 23:01:56 +02:00
if (IsNewLine(codepoint))
break;
reader->Skip();
if (codepoint == ']')
{
closedCorrectly = true;
break;
}
sb.Append(codepoint);
}
if (closedCorrectly)
{
while (sb.GetLength() < 8)
{
sb.Append(' ');
}
if (sb.GetLength() == 8)
{
2018-07-17 23:37:56 +02:00
_currentGroup = sb.GetStdString();
_currentObjectOverride = GetObjectOverride(_currentGroup);
_currentScenarioOverride = nullptr;
if (_currentObjectOverride == nullptr)
{
if (_objectOverrides.size() == MAX_OBJECT_OVERRIDES)
{
log_warning("Maximum number of localised object strings exceeded.");
}
_objectOverrides.push_back(ObjectOverride());
_currentObjectOverride = &_objectOverrides[_objectOverrides.size() - 1];
2018-02-01 12:52:39 +00:00
std::copy_n(_currentGroup.c_str(), 8, _currentObjectOverride->name);
}
}
}
}
2018-06-22 23:01:56 +02:00
void ParseGroupScenario(IStringReader* reader)
{
auto sb = StringBuilder();
codepoint_t codepoint;
// Should have already deduced that the next codepoint is a <
reader->Skip();
// Read string up to > or line end
bool closedCorrectly = false;
while (reader->TryPeek(&codepoint))
{
2018-06-22 23:01:56 +02:00
if (IsNewLine(codepoint))
break;
reader->Skip();
if (codepoint == '>')
{
closedCorrectly = true;
break;
}
sb.Append(codepoint);
}
if (closedCorrectly)
{
2018-07-17 23:37:56 +02:00
_currentGroup = sb.GetStdString();
_currentObjectOverride = nullptr;
_currentScenarioOverride = GetScenarioOverride(_currentGroup);
if (_currentScenarioOverride == nullptr)
{
if (_scenarioOverrides.size() == MAX_SCENARIO_OVERRIDES)
{
log_warning("Maximum number of scenario strings exceeded.");
}
_scenarioOverrides.push_back(ScenarioOverride());
_currentScenarioOverride = &_scenarioOverrides[_scenarioOverrides.size() - 1];
_currentScenarioOverride->filename = std::string(sb.GetBuffer());
}
}
}
2018-06-22 23:01:56 +02:00
void ParseString(IStringReader* reader)
{
auto sb = StringBuilder();
codepoint_t codepoint;
// Parse string identifier
while (reader->TryPeek(&codepoint))
{
if (IsNewLine(codepoint))
{
// Unexpected new line, ignore line entirely
return;
}
else if (!IsWhitespace(codepoint) && codepoint != ':')
{
reader->Skip();
sb.Append(codepoint);
}
else
{
break;
}
}
SkipWhitespace(reader);
// Parse a colon
if (!reader->TryPeek(&codepoint) || codepoint != ':')
{
// Expected a colon, ignore line entirely
return;
}
reader->Skip();
// Validate identifier
2018-06-22 23:01:56 +02:00
const utf8* identifier = sb.GetBuffer();
int32_t stringId;
2017-12-05 17:42:04 +00:00
if (_currentGroup.empty())
{
if (sscanf(identifier, "STR_%4d", &stringId) != 1)
{
// Ignore line entirely
2016-01-23 18:58:31 +00:00
return;
}
}
else
{
2018-06-22 23:01:56 +02:00
if (String::Equals(identifier, "STR_NAME"))
{
stringId = 0;
}
else if (String::Equals(identifier, "STR_DESC"))
{
stringId = 1;
}
else if (String::Equals(identifier, "STR_CPTY"))
{
stringId = 2;
}
2018-06-22 23:01:56 +02:00
else if (String::Equals(identifier, "STR_SCNR"))
{
stringId = 0;
}
else if (String::Equals(identifier, "STR_PARK"))
{
stringId = 1;
}
else if (String::Equals(identifier, "STR_DTLS"))
{
stringId = 2;
}
else
{
// Ignore line entirely
return;
}
2016-01-23 18:58:31 +00:00
}
// Rest of the line is the actual string
sb.Clear();
while (reader->TryPeek(&codepoint) && !IsNewLine(codepoint))
2016-01-23 18:58:31 +00:00
{
if (codepoint == '{')
{
uint32_t token;
bool isByte;
if (ParseToken(reader, &token, &isByte))
{
if (isByte)
{
sb.Append((const utf8*)&token, 1);
}
else
{
sb.Append((int32_t)token);
}
}
else
{
// Syntax error or unknown token, ignore line entirely
return;
}
}
else
{
reader->Skip();
sb.Append(codepoint);
}
}
std::string s;
if (LanguagesDescriptors[_id].isRtl)
{
auto ts = std::string(sb.GetBuffer(), sb.GetLength());
s = FixRTL(ts);
}
else
{
s = std::string(sb.GetBuffer(), sb.GetLength());
}
2017-12-05 17:42:04 +00:00
if (_currentGroup.empty())
{
// Make sure the list is big enough to contain this string id
2017-12-06 00:12:36 +01:00
if ((size_t)stringId >= _strings.size())
{
2017-12-06 00:12:36 +01:00
_strings.resize(stringId + 1);
}
2017-12-05 17:42:04 +00:00
_strings[stringId] = s;
2016-01-23 18:58:31 +00:00
}
else
{
if (_currentObjectOverride != nullptr)
{
2017-12-05 17:42:04 +00:00
_currentObjectOverride->strings[stringId] = s;
}
else
{
2017-12-05 17:42:04 +00:00
_currentScenarioOverride->strings[stringId] = s;
}
2016-01-23 18:58:31 +00:00
}
}
2018-06-22 23:01:56 +02:00
bool ParseToken(IStringReader* reader, uint32_t* token, bool* isByte)
2016-01-23 18:58:31 +00:00
{
auto sb = StringBuilder();
codepoint_t codepoint;
// Skip open brace
2016-01-23 18:58:31 +00:00
reader->Skip();
while (reader->TryPeek(&codepoint))
2016-01-23 18:58:31 +00:00
{
2018-06-22 23:01:56 +02:00
if (IsNewLine(codepoint))
return false;
if (IsWhitespace(codepoint))
return false;
reader->Skip();
2018-06-22 23:01:56 +02:00
if (codepoint == '}')
break;
sb.Append(codepoint);
2016-01-23 18:58:31 +00:00
}
2018-06-22 23:01:56 +02:00
const utf8* tokenName = sb.GetBuffer();
*token = format_get_code(tokenName);
*isByte = false;
// Handle explicit byte values
if (*token == 0)
{
int32_t number;
if (sscanf(tokenName, "%d", &number) == 1)
{
2018-08-12 12:50:40 +01:00
*token = std::clamp(number, 0, 255);
*isByte = true;
}
}
return true;
}
std::string FixRTL(std::string& input)
{
#ifdef _WIN32
return input;
#else
UErrorCode err = (UErrorCode)0;
// Force a hard left-to-right at the beginning (will mess up mixed strings' word order otherwise)
std::string text2 = std::string(u8"\xE2\x80\xAA") + input;
icu::UnicodeString ustr = icu::UnicodeString::fromUTF8(icu::StringPiece(text2));
int32_t length = ustr.length();
icu::UnicodeString reordered;
icu::UnicodeString shaped;
UBiDi* bidi = ubidi_openSized(length, 0, &err);
// UBIDI_DEFAULT_LTR preserves formatting codes.
ubidi_setPara(bidi, ustr.getBuffer(), length, UBIDI_DEFAULT_LTR, nullptr, &err);
ubidi_writeReordered(bidi, reordered.getBuffer(length), length, UBIDI_DO_MIRRORING | UBIDI_REMOVE_BIDI_CONTROLS, &err);
ubidi_close(bidi);
reordered.releaseBuffer(length);
u_shapeArabic(
2018-07-26 15:02:55 +02:00
reordered.getBuffer(), length, shaped.getBuffer(length), length,
U_SHAPE_LETTERS_SHAPE | U_SHAPE_LENGTH_FIXED_SPACES_NEAR | U_SHAPE_TEXT_DIRECTION_VISUAL_LTR, &err);
shaped.releaseBuffer(length);
std::string cppstring;
shaped.toUTF8String(cppstring);
return cppstring;
#endif
}
};
namespace LanguagePackFactory
{
2018-06-22 23:01:56 +02:00
ILanguagePack* FromFile(uint16_t id, const utf8* path)
{
auto languagePack = LanguagePack::FromFile(id, path);
return languagePack;
2016-01-23 18:58:31 +00:00
}
2015-09-09 19:22:41 +01:00
2018-06-22 23:01:56 +02:00
ILanguagePack* FromText(uint16_t id, const utf8* text)
{
auto languagePack = LanguagePack::FromText(id, text);
return languagePack;
}
2018-05-04 22:40:09 +02:00
} // namespace LanguagePackFactory