Files
OpenRCT2-Web/src/openrct2/object/StringTable.cpp
T

147 lines
4.0 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 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:03:20 +02:00
#include "StringTable.h"
#include "../core/IStream.hpp"
#include "../core/String.hpp"
2018-01-06 18:32:25 +01:00
#include "../localisation/Language.h"
#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"
2018-06-22 23:03:20 +02:00
#include <algorithm>
static constexpr const uint8_t RCT2ToOpenRCT2LanguageId[] = {
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)
{
2017-12-05 12:55:33 +00:00
for (auto ch = str; *ch != '\0'; ch++)
{
2020-04-22 17:09:29 +02:00
if (!isblank(static_cast<uint8_t>(*ch)))
{
return false;
}
}
return true;
}
void StringTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream, uint8_t id)
{
2016-07-01 21:01:06 +01:00
try
{
RCT2LanguageId rct2LanguageId;
2020-04-22 17:09:29 +02:00
while ((rct2LanguageId = static_cast<RCT2LanguageId>(stream->ReadValue<uint8_t>())) != RCT2_LANGUAGE_ID_END)
{
2018-06-22 23:03:20 +02:00
uint8_t languageId = (rct2LanguageId <= RCT2_LANGUAGE_ID_PORTUGUESE) ? RCT2ToOpenRCT2LanguageId[rct2LanguageId]
2020-04-22 17:09:29 +02:00
: static_cast<uint8_t>(LANGUAGE_UNDEFINED);
2016-12-26 23:42:19 +01:00
std::string stringAsWin1252 = stream->ReadStdString();
auto stringAsUtf8 = rct2_to_utf8(stringAsWin1252, rct2LanguageId);
2016-07-04 19:03:30 +01:00
if (!StringIsBlank(stringAsUtf8.data()))
2016-07-01 21:01:06 +01:00
{
stringAsUtf8 = String::Trim(stringAsUtf8);
StringTableEntry entry{};
entry.Id = id;
entry.LanguageId = languageId;
entry.Text = stringAsUtf8;
_strings.push_back(entry);
2016-07-01 21:01:06 +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;
}
Sort();
}
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
}
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);
}
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 {
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 12:30:20 +01:00
2018-04-26 23:48:25 +01:00
if (a.LanguageId == targetLanguage)
{
2016-06-25 12:30:20 +01:00
return true;
}
2018-04-26 23:48:25 +01:00
if (b.LanguageId == targetLanguage)
2016-06-25 12:30:20 +01:00
{
return false;
}
if (a.LanguageId == LANGUAGE_ENGLISH_UK)
{
2016-06-25 12:30:20 +01:00
return true;
}
if (b.LanguageId == LANGUAGE_ENGLISH_UK)
2016-06-25 12:30:20 +01:00
{
return false;
}
return a.LanguageId < b.LanguageId;
}
2016-06-25 12:30:20 +01:00
return a.Id < b.Id;
});
}