2016-06-25 02:52:58 +01:00
|
|
|
/*****************************************************************************
|
2018-06-15 14:07:34 +02:00
|
|
|
* Copyright (c) 2014-2018 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"
|
|
|
|
|
|
2016-06-25 02:52:58 +01:00
|
|
|
#include "../core/IStream.hpp"
|
|
|
|
|
#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
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
if (!isblank((uint8_t)*ch))
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
void StringTable::Read(IReadObjectContext* context, IStream* stream, uint8_t 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;
|
2018-06-20 17:28:51 +02:00
|
|
|
while ((rct2LanguageId = (RCT2LanguageId)stream->ReadValue<uint8_t>()) != RCT2_LANGUAGE_ID_END)
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2018-06-22 23:03:20 +02:00
|
|
|
uint8_t languageId = (rct2LanguageId <= RCT2_LANGUAGE_ID_PORTUGUESE) ? RCT2ToOpenRCT2LanguageId[rct2LanguageId]
|
|
|
|
|
: LANGUAGE_UNDEFINED;
|
|
|
|
|
StringTableEntry entry{};
|
2016-07-01 21:01:06 +01:00
|
|
|
entry.Id = id;
|
|
|
|
|
entry.LanguageId = languageId;
|
|
|
|
|
|
2016-12-26 23:42:19 +01:00
|
|
|
std::string stringAsWin1252 = stream->ReadStdString();
|
2018-04-14 18:06:49 +01:00
|
|
|
auto stringAsUtf8 = rct2_to_utf8(stringAsWin1252, rct2LanguageId);
|
2016-07-04 19:03:30 +01:00
|
|
|
|
2018-04-14 18:06:49 +01:00
|
|
|
if (StringIsBlank(stringAsUtf8.data()))
|
2016-07-01 21:01:06 +01:00
|
|
|
{
|
2017-12-11 19:29:25 +01:00
|
|
|
entry.LanguageId = LANGUAGE_UNDEFINED;
|
2016-07-01 21:01:06 +01:00
|
|
|
}
|
2018-04-14 18:06:49 +01:00
|
|
|
stringAsUtf8 = String::Trim(stringAsUtf8);
|
2016-07-01 21:01:06 +01:00
|
|
|
|
2016-07-04 19:03:30 +01:00
|
|
|
entry.Text = stringAsUtf8;
|
2016-07-01 21:01:06 +01:00
|
|
|
_strings.push_back(entry);
|
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
|
|
|
{
|
|
|
|
|
context->LogError(OBJECT_ERROR_BAD_STRING_TABLE, "Bad string table.");
|
|
|
|
|
throw;
|
2016-06-25 02:52:58 +01:00
|
|
|
}
|
|
|
|
|
Sort();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
std::string StringTable::GetString(uint8_t 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
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
std::string StringTable::GetString(uint8_t language, uint8_t 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();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
void StringTable::SetString(uint8_t 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;
|
2017-12-05 12:55:33 +00:00
|
|
|
_strings.push_back(entry);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-25 02:52:58 +01:00
|
|
|
void StringTable::Sort()
|
|
|
|
|
{
|
2018-04-26 23:48:25 +01:00
|
|
|
auto targetLanguage = LocalisationService_GetCurrentLanguage();
|
2018-06-22 23:03:20 +02:00
|
|
|
std::sort(_strings.begin(), _strings.end(), [targetLanguage](const StringTableEntry& a, const StringTableEntry& b) -> bool {
|
2016-06-25 02:52:58 +01:00
|
|
|
if (a.Id == b.Id)
|
|
|
|
|
{
|
|
|
|
|
if (a.LanguageId == b.LanguageId)
|
|
|
|
|
{
|
2017-12-05 12:55:33 +00:00
|
|
|
return String::Compare(a.Text, b.Text, true) < 0;
|
2016-06-25 02:52:58 +01:00
|
|
|
}
|
2016-06-25 12:30:20 +01:00
|
|
|
|
2018-04-26 23:48:25 +01:00
|
|
|
if (a.LanguageId == targetLanguage)
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2016-06-25 12:30:20 +01:00
|
|
|
return true;
|
2016-06-25 02:52:58 +01:00
|
|
|
}
|
2018-04-26 23:48:25 +01:00
|
|
|
if (b.LanguageId == targetLanguage)
|
2016-06-25 12:30:20 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-11 19:29:25 +01:00
|
|
|
if (a.LanguageId == LANGUAGE_ENGLISH_UK)
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2016-06-25 12:30:20 +01:00
|
|
|
return true;
|
2016-06-25 02:52:58 +01:00
|
|
|
}
|
2017-12-11 19:29:25 +01:00
|
|
|
if (b.LanguageId == LANGUAGE_ENGLISH_UK)
|
2016-06-25 12:30:20 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return a.LanguageId < b.LanguageId;
|
2016-06-25 02:52:58 +01:00
|
|
|
}
|
2016-06-25 12:30:20 +01:00
|
|
|
return a.Id < b.Id;
|
2016-06-25 02:52:58 +01:00
|
|
|
});
|
|
|
|
|
}
|