2016-06-25 02:52:58 +01:00
|
|
|
/*****************************************************************************
|
2023-01-01 04:58:01 -06:00
|
|
|
* Copyright (c) 2014-2023 OpenRCT2 developers
|
2016-06-25 02:52:58 +01:00
|
|
|
*
|
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
|
2016-06-25 02:52:58 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-06-25 02:52:58 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
#include "StringTable.h"
|
|
|
|
|
|
2023-01-03 22:21:16 +01:00
|
|
|
#include "../Context.h"
|
2016-06-25 02:52:58 +01:00
|
|
|
#include "../core/IStream.hpp"
|
2020-10-02 11:31:20 +01:00
|
|
|
#include "../core/Json.hpp"
|
2016-06-25 02:52:58 +01:00
|
|
|
#include "../core/String.hpp"
|
2018-01-06 18:32:25 +01:00
|
|
|
#include "../localisation/Language.h"
|
2016-06-25 02:52:58 +01:00
|
|
|
#include "../localisation/LanguagePack.h"
|
2018-04-26 23:48:25 +01:00
|
|
|
#include "../localisation/LocalisationService.h"
|
2016-07-01 21:01:06 +01:00
|
|
|
#include "Object.h"
|
2016-06-25 02:52:58 +01:00
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
static constexpr const uint8_t RCT2ToOpenRCT2LanguageId[] = {
|
2017-12-11 19:29:25 +01:00
|
|
|
LANGUAGE_ENGLISH_UK,
|
|
|
|
|
LANGUAGE_ENGLISH_US,
|
|
|
|
|
LANGUAGE_FRENCH,
|
|
|
|
|
LANGUAGE_GERMAN,
|
|
|
|
|
LANGUAGE_SPANISH,
|
|
|
|
|
LANGUAGE_ITALIAN,
|
|
|
|
|
LANGUAGE_DUTCH,
|
|
|
|
|
LANGUAGE_SWEDISH,
|
|
|
|
|
LANGUAGE_JAPANESE,
|
|
|
|
|
LANGUAGE_KOREAN,
|
|
|
|
|
LANGUAGE_CHINESE_SIMPLIFIED,
|
|
|
|
|
LANGUAGE_CHINESE_TRADITIONAL,
|
|
|
|
|
LANGUAGE_UNDEFINED,
|
|
|
|
|
LANGUAGE_PORTUGUESE_BR,
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
static bool StringIsBlank(const utf8* str)
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2017-12-05 12:55:33 +00:00
|
|
|
for (auto ch = str; *ch != '\0'; ch++)
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
if (!isblank(static_cast<uint8_t>(*ch)))
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 04:36:58 +02:00
|
|
|
void StringTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream, ObjectStringID id)
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2016-07-01 21:01:06 +01:00
|
|
|
try
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2017-12-11 19:29:25 +01:00
|
|
|
RCT2LanguageId rct2LanguageId;
|
2020-12-23 01:43:27 -03:00
|
|
|
while ((rct2LanguageId = static_cast<RCT2LanguageId>(stream->ReadValue<uint8_t>())) != RCT2LanguageId::End)
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2020-12-23 01:43:27 -03:00
|
|
|
uint8_t languageId = (EnumValue(rct2LanguageId) <= EnumValue(RCT2LanguageId::Portuguese))
|
|
|
|
|
? RCT2ToOpenRCT2LanguageId[EnumValue(rct2LanguageId)]
|
|
|
|
|
: static_cast<uint8_t>(LANGUAGE_UNDEFINED);
|
2016-12-26 23:42:19 +01:00
|
|
|
std::string stringAsWin1252 = stream->ReadStdString();
|
2023-01-17 00:12:35 +01:00
|
|
|
auto stringAsUtf8 = RCT2StringToUTF8(stringAsWin1252, rct2LanguageId);
|
2016-07-04 19:03:30 +01:00
|
|
|
|
2020-05-05 18:49:58 +02:00
|
|
|
if (!StringIsBlank(stringAsUtf8.data()))
|
2016-07-01 21:01:06 +01:00
|
|
|
{
|
2020-05-05 18:49:58 +02:00
|
|
|
stringAsUtf8 = String::Trim(stringAsUtf8);
|
|
|
|
|
StringTableEntry entry{};
|
|
|
|
|
entry.Id = id;
|
|
|
|
|
entry.LanguageId = languageId;
|
2022-04-25 16:37:32 +03:00
|
|
|
entry.Text = std::move(stringAsUtf8);
|
2021-01-09 05:59:55 +09:00
|
|
|
_strings.push_back(std::move(entry));
|
2016-07-01 21:01:06 +01:00
|
|
|
}
|
2016-06-25 02:52:58 +01:00
|
|
|
}
|
2016-07-01 21:01:06 +01:00
|
|
|
}
|
2018-06-22 23:03:20 +02:00
|
|
|
catch (const std::exception&)
|
2016-07-01 21:01:06 +01:00
|
|
|
{
|
2020-10-10 12:21:07 -03:00
|
|
|
context->LogError(ObjectError::BadStringTable, "Bad string table.");
|
2016-07-01 21:01:06 +01:00
|
|
|
throw;
|
2016-06-25 02:52:58 +01:00
|
|
|
}
|
|
|
|
|
Sort();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 19:44:59 +01:00
|
|
|
ObjectStringID StringTable::ParseStringId(const std::string& s)
|
|
|
|
|
{
|
|
|
|
|
if (s == "name")
|
|
|
|
|
return ObjectStringID::NAME;
|
|
|
|
|
if (s == "description")
|
|
|
|
|
return ObjectStringID::DESCRIPTION;
|
|
|
|
|
if (s == "capacity")
|
|
|
|
|
return ObjectStringID::CAPACITY;
|
|
|
|
|
if (s == "vehicleName")
|
|
|
|
|
return ObjectStringID::VEHICLE_NAME;
|
|
|
|
|
return ObjectStringID::UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringTable::ReadJson(json_t& root)
|
|
|
|
|
{
|
|
|
|
|
Guard::Assert(root.is_object(), "StringTable::ReadJson expects parameter root to be object");
|
|
|
|
|
|
|
|
|
|
// We trust the JSON type of root is object
|
|
|
|
|
auto jsonStrings = root["strings"];
|
|
|
|
|
|
|
|
|
|
for (auto& [key, jsonLanguages] : jsonStrings.items())
|
|
|
|
|
{
|
|
|
|
|
auto stringId = ParseStringId(key);
|
|
|
|
|
if (stringId != ObjectStringID::UNKNOWN)
|
|
|
|
|
{
|
|
|
|
|
for (auto& [locale, jsonString] : jsonLanguages.items())
|
|
|
|
|
{
|
2023-01-17 00:12:35 +01:00
|
|
|
auto langId = LanguageGetIDFromLocale(locale.c_str());
|
2020-08-12 19:44:59 +01:00
|
|
|
if (langId != LANGUAGE_UNDEFINED)
|
|
|
|
|
{
|
|
|
|
|
auto string = Json::GetString(jsonString);
|
|
|
|
|
SetString(stringId, langId, string);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Sort();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 04:36:58 +02:00
|
|
|
std::string StringTable::GetString(ObjectStringID id) const
|
2016-06-25 14:44:04 +01:00
|
|
|
{
|
2018-06-22 23:03:20 +02:00
|
|
|
for (auto& string : _strings)
|
2016-06-25 14:44:04 +01:00
|
|
|
{
|
|
|
|
|
if (string.Id == id)
|
|
|
|
|
{
|
|
|
|
|
return string.Text;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-11 12:30:08 +00:00
|
|
|
return std::string();
|
2016-06-25 14:44:04 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-29 04:36:58 +02:00
|
|
|
std::string StringTable::GetString(uint8_t language, ObjectStringID id) const
|
2018-04-27 18:23:39 +01:00
|
|
|
{
|
2018-06-22 23:03:20 +02:00
|
|
|
for (auto& string : _strings)
|
2018-04-27 18:23:39 +01:00
|
|
|
{
|
|
|
|
|
if (string.LanguageId == language && string.Id == id)
|
|
|
|
|
{
|
|
|
|
|
return string.Text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return std::string();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 04:36:58 +02:00
|
|
|
void StringTable::SetString(ObjectStringID id, uint8_t language, const std::string& text)
|
2017-12-05 12:55:33 +00:00
|
|
|
{
|
|
|
|
|
StringTableEntry entry;
|
|
|
|
|
entry.Id = id;
|
|
|
|
|
entry.LanguageId = language;
|
2018-10-04 01:42:22 -06:00
|
|
|
entry.Text = text;
|
2021-01-09 05:59:55 +09:00
|
|
|
_strings.push_back(std::move(entry));
|
2017-12-05 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-25 02:52:58 +01:00
|
|
|
void StringTable::Sort()
|
|
|
|
|
{
|
2023-01-03 22:21:16 +01:00
|
|
|
const auto& languageOrder = OpenRCT2::GetContext()->GetLocalisationService().GetLanguageOrder();
|
|
|
|
|
std::sort(_strings.begin(), _strings.end(), [languageOrder](const StringTableEntry& a, const StringTableEntry& b) -> bool {
|
|
|
|
|
if (a.Id == b.Id)
|
|
|
|
|
{
|
|
|
|
|
if (a.LanguageId == b.LanguageId)
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2023-01-03 22:21:16 +01:00
|
|
|
return String::Compare(a.Text, b.Text, true) < 0;
|
2023-01-02 21:33:26 +01:00
|
|
|
}
|
2023-01-03 22:21:16 +01:00
|
|
|
|
|
|
|
|
for (const auto& language : languageOrder)
|
|
|
|
|
{
|
|
|
|
|
if (a.LanguageId == language)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (b.LanguageId == language)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return a.LanguageId < b.LanguageId;
|
|
|
|
|
}
|
|
|
|
|
return a.Id < b.Id;
|
|
|
|
|
});
|
2016-06-25 02:52:58 +01:00
|
|
|
}
|