2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CGeneralTextHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2007-07-08 20:28:08 +03:00
|
|
|
#include "CGeneralTextHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2013-10-26 00:45:14 +03:00
|
|
|
#include <boost/locale.hpp>
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "filesystem/Filesystem.h"
|
2013-10-26 00:45:14 +03:00
|
|
|
#include "CConfigHandler.h"
|
2013-04-26 00:50:55 +03:00
|
|
|
#include "CModHandler.h"
|
2013-10-26 00:45:14 +03:00
|
|
|
#include "GameConstants.h"
|
2022-12-27 23:17:41 +02:00
|
|
|
#include "mapObjects/CQuest.h"
|
2013-04-26 00:50:55 +03:00
|
|
|
#include "VCMI_Lib.h"
|
2007-07-08 20:28:08 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-05-23 19:46:54 +03:00
|
|
|
size_t Unicode::getCharacterSize(char firstByte)
|
2013-10-26 00:45:14 +03:00
|
|
|
{
|
|
|
|
// length of utf-8 character can be determined from 1st byte by counting number of highest bits set to 1:
|
|
|
|
// 0xxxxxxx -> 1 - ASCII chars
|
|
|
|
// 110xxxxx -> 2
|
|
|
|
// 11110xxx -> 4 - last allowed in current standard
|
|
|
|
// 1111110x -> 6 - last allowed in original standard
|
|
|
|
|
2014-05-23 19:46:54 +03:00
|
|
|
if ((ui8)firstByte < 0x80)
|
2013-10-26 00:45:14 +03:00
|
|
|
return 1; // ASCII
|
|
|
|
|
|
|
|
size_t ret = 0;
|
|
|
|
|
|
|
|
for (size_t i=0; i<8; i++)
|
|
|
|
{
|
2014-05-23 19:46:54 +03:00
|
|
|
if (((ui8)firstByte & (0x80 >> i)) != 0)
|
2013-10-26 00:45:14 +03:00
|
|
|
ret++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-23 19:46:54 +03:00
|
|
|
bool Unicode::isValidCharacter(const char * character, size_t maxSize)
|
2013-10-26 00:45:14 +03:00
|
|
|
{
|
2014-05-23 19:46:54 +03:00
|
|
|
// can't be first byte in UTF8
|
|
|
|
if ((ui8)character[0] >= 0x80 && (ui8)character[0] < 0xC0)
|
|
|
|
return false;
|
2013-10-26 00:45:14 +03:00
|
|
|
// first character must follow rules checked in getCharacterSize
|
2014-05-23 19:46:54 +03:00
|
|
|
size_t size = getCharacterSize((ui8)character[0]);
|
2013-10-26 00:45:14 +03:00
|
|
|
|
2014-05-23 19:46:54 +03:00
|
|
|
if ((ui8)character[0] > 0xF4)
|
2013-10-26 00:45:14 +03:00
|
|
|
return false; // above maximum allowed in standard (UTF codepoints are capped at 0x0010FFFF)
|
|
|
|
|
|
|
|
if (size > maxSize)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// remaining characters must have highest bit set to 1
|
|
|
|
for (size_t i = 1; i < size; i++)
|
|
|
|
{
|
2014-05-23 19:46:54 +03:00
|
|
|
if (((ui8)character[i] & 0x80) == 0)
|
2013-10-26 00:45:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Unicode::isValidASCII(const std::string & text)
|
|
|
|
{
|
|
|
|
for (const char & ch : text)
|
|
|
|
if (ui8(ch) >= 0x80 )
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Unicode::isValidASCII(const char * data, size_t size)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<size; i++)
|
|
|
|
if (ui8(data[i]) >= 0x80 )
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Unicode::isValidString(const std::string & text)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<text.size(); i += getCharacterSize(text[i]))
|
|
|
|
{
|
2014-05-23 19:46:54 +03:00
|
|
|
if (!isValidCharacter(text.data() + i, text.size() - i))
|
2013-10-26 00:45:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Unicode::isValidString(const char * data, size_t size)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<size; i += getCharacterSize(data[i]))
|
|
|
|
{
|
2014-05-23 19:46:54 +03:00
|
|
|
if (!isValidCharacter(data + i, size - i))
|
2013-10-26 00:45:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getSelectedEncoding()
|
|
|
|
{
|
2023-01-09 00:24:11 +02:00
|
|
|
auto explicitSetting = settings["general"]["encoding"].String();
|
|
|
|
if (explicitSetting != "auto")
|
|
|
|
return explicitSetting;
|
|
|
|
return settings["session"]["encoding"].String();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Detects encoding of H3 text files based on matching against pregenerated footprints of H3 file
|
|
|
|
/// Can also detect language of H3 install, however right now this is not necessary
|
|
|
|
static void detectEncoding()
|
|
|
|
{
|
|
|
|
static const size_t knownCount = 6;
|
|
|
|
|
|
|
|
// "footprints" of data collected from known versions of H3
|
|
|
|
static const std::array<std::array<double, 16>, knownCount> knownFootprints =
|
|
|
|
{ {
|
|
|
|
{ { 0.0559, 0.0000, 0.1983, 0.0051, 0.0222, 0.0183, 0.4596, 0.2405, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000 } },
|
|
|
|
{ { 0.0493, 0.0000, 0.1926, 0.0047, 0.0230, 0.0121, 0.4133, 0.2780, 0.0002, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0259, 0.0008 } },
|
|
|
|
{ { 0.0534, 0.0000, 0.1705, 0.0047, 0.0418, 0.0208, 0.4775, 0.2191, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0005, 0.0036, 0.0080 } },
|
|
|
|
{ { 0.0534, 0.0000, 0.1701, 0.0067, 0.0157, 0.0133, 0.4328, 0.2540, 0.0001, 0.0043, 0.0000, 0.0244, 0.0000, 0.0000, 0.0181, 0.0071 } },
|
|
|
|
{ { 0.0548, 0.0000, 0.1744, 0.0061, 0.0031, 0.0009, 0.0046, 0.0136, 0.0000, 0.0004, 0.0000, 0.0000, 0.0227, 0.0061, 0.4882, 0.2252 } },
|
|
|
|
{ { 0.0559, 0.0000, 0.1807, 0.0059, 0.0036, 0.0013, 0.0046, 0.0134, 0.0000, 0.0004, 0.0000, 0.0487, 0.0209, 0.0060, 0.4615, 0.1972 } }
|
|
|
|
} };
|
|
|
|
|
|
|
|
// languages of known footprints
|
|
|
|
static const std::array<std::string, knownCount> knownLanguages =
|
|
|
|
{ {
|
|
|
|
"English", "French", "German", "Polish", "Russian", "Ukrainian"
|
|
|
|
} };
|
|
|
|
|
|
|
|
// encoding that should be used for known footprints
|
|
|
|
static const std::array<std::string, knownCount> knownEncodings =
|
|
|
|
{ {
|
|
|
|
"CP1252", "CP1252", "CP1252", "CP1250", "CP1251", "CP1251"
|
|
|
|
} };
|
|
|
|
|
|
|
|
// load file that will be used for footprint generation
|
|
|
|
// this is one of the most text-heavy files in game and consists solely from translated texts
|
|
|
|
auto resource = CResourceHandler::get()->load(ResourceID("DATA/GENRLTXT.TXT", EResType::TEXT));
|
|
|
|
|
|
|
|
std::array<size_t, 256> charCount;
|
|
|
|
std::array<double, 16> footprint;
|
|
|
|
std::array<double, knownCount> deviations;
|
|
|
|
|
|
|
|
boost::range::fill(charCount, 0);
|
|
|
|
boost::range::fill(footprint, 0.0);
|
|
|
|
boost::range::fill(deviations, 0.0);
|
|
|
|
|
|
|
|
auto data = resource->readAll();
|
|
|
|
|
|
|
|
// compute how often each character occurs in input file
|
2023-01-15 23:55:24 +02:00
|
|
|
for (si64 i = 0; i < data.second; ++i)
|
2023-01-09 00:24:11 +02:00
|
|
|
charCount[data.first[i]] += 1;
|
|
|
|
|
|
|
|
// and convert computed data into weights
|
|
|
|
// to reduce amount of data, group footprint data into 16-char blocks.
|
|
|
|
// While this will reduce precision, it should not affect output
|
|
|
|
// since we expect only tiny differences compared to reference footprints
|
|
|
|
for (size_t i = 0; i < 256; ++i)
|
|
|
|
footprint[i/16] += double(charCount[i]) / data.second;
|
|
|
|
|
|
|
|
logGlobal->debug("Language footprint: %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
|
|
|
|
footprint[0], footprint[1], footprint[2], footprint[3], footprint[4], footprint[5], footprint[6], footprint[7],
|
|
|
|
footprint[8], footprint[9], footprint[10], footprint[11], footprint[12], footprint[13], footprint[14], footprint[15]
|
|
|
|
);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < deviations.size(); ++i)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < footprint.size(); ++j)
|
|
|
|
deviations[i] += std::abs((footprint[j] - knownFootprints[i][j]));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bestIndex = boost::range::min_element(deviations) - deviations.begin();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < deviations.size(); ++i)
|
|
|
|
logGlobal->debug("Comparing to %s: %f", knownLanguages[i], deviations[i]);
|
|
|
|
|
|
|
|
Settings s = settings.write["session"]["encoding"];
|
|
|
|
s->String() = knownEncodings[bestIndex];
|
2013-10-26 00:45:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Unicode::toUnicode(const std::string &text)
|
|
|
|
{
|
|
|
|
return toUnicode(text, getSelectedEncoding());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Unicode::toUnicode(const std::string &text, const std::string &encoding)
|
|
|
|
{
|
|
|
|
return boost::locale::conv::to_utf<char>(text, encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Unicode::fromUnicode(const std::string & text)
|
|
|
|
{
|
|
|
|
return fromUnicode(text, getSelectedEncoding());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Unicode::fromUnicode(const std::string &text, const std::string &encoding)
|
|
|
|
{
|
|
|
|
return boost::locale::conv::from_utf<char>(text, encoding);
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
void Unicode::trimRight(std::string & text, const size_t amount)
|
2014-07-02 18:16:05 +03:00
|
|
|
{
|
|
|
|
if(text.empty())
|
|
|
|
return;
|
|
|
|
//todo: more efficient algorithm
|
|
|
|
for(int i = 0; i< amount; i++){
|
2016-08-12 11:10:27 +02:00
|
|
|
auto b = text.begin();
|
2014-07-02 18:16:05 +03:00
|
|
|
auto e = text.end();
|
|
|
|
size_t lastLen = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
while (b != e) {
|
|
|
|
lastLen = len;
|
|
|
|
size_t n = getCharacterSize(*b);
|
|
|
|
|
|
|
|
if(!isValidCharacter(&(*b),e-b))
|
2016-08-12 11:10:27 +02:00
|
|
|
{
|
|
|
|
logGlobal->error("Invalid UTF8 sequence");
|
2014-07-02 18:16:05 +03:00
|
|
|
break;//invalid sequence will be trimmed
|
|
|
|
}
|
|
|
|
|
|
|
|
len += n;
|
|
|
|
b += n;
|
2016-08-12 11:10:27 +02:00
|
|
|
}
|
2014-07-02 18:16:05 +03:00
|
|
|
|
|
|
|
text.resize(lastLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
//Helper for string -> float conversion
|
|
|
|
class LocaleWithComma: public std::numpunct<char>
|
|
|
|
{
|
|
|
|
protected:
|
2015-10-12 15:47:10 +02:00
|
|
|
char do_decimal_point() const override
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
|
|
|
return ',';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CLegacyConfigParser::CLegacyConfigParser(std::string URI)
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
init(CResourceHandler::get()->load(ResourceID(URI, EResType::TEXT)));
|
2008-12-22 19:48:41 +02:00
|
|
|
}
|
2009-07-03 22:57:14 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
CLegacyConfigParser::CLegacyConfigParser(const std::unique_ptr<CInputStream> & input)
|
2009-07-03 22:57:14 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
init(input);
|
2009-07-03 22:57:14 +03:00
|
|
|
}
|
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
void CLegacyConfigParser::init(const std::unique_ptr<CInputStream> & input)
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
data.reset(new char[input->getSize()]);
|
|
|
|
input->read((ui8*)data.get(), input->getSize());
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
curr = data.get();
|
|
|
|
end = curr + input->getSize();
|
2012-08-01 15:02:54 +03:00
|
|
|
}
|
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
std::string CLegacyConfigParser::extractQuotedPart()
|
2008-11-28 03:36:34 +02:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
assert(*curr == '\"');
|
2008-11-28 03:36:34 +02:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
curr++; // skip quote
|
|
|
|
char * begin = curr;
|
2008-11-28 03:36:34 +02:00
|
|
|
|
2013-01-26 22:39:54 +03:00
|
|
|
while (curr != end && *curr != '\"' && *curr != '\t')
|
2012-08-25 11:44:51 +03:00
|
|
|
curr++;
|
2008-11-28 03:36:34 +02:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
return std::string(begin, curr++); //increment curr to close quote
|
|
|
|
}
|
2008-12-22 19:48:41 +02:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
std::string CLegacyConfigParser::extractQuotedString()
|
|
|
|
{
|
|
|
|
assert(*curr == '\"');
|
2008-12-22 19:48:41 +02:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
std::string ret;
|
|
|
|
while (true)
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
ret += extractQuotedPart();
|
2008-12-22 19:48:41 +02:00
|
|
|
|
2015-09-12 22:03:10 +02:00
|
|
|
// double quote - add it to string and continue quoted part
|
|
|
|
if (curr < end && *curr == '\"')
|
2013-01-26 22:39:54 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
ret += '\"';
|
2013-01-26 22:39:54 +03:00
|
|
|
}
|
2015-09-12 22:03:10 +02:00
|
|
|
//extract normal part
|
|
|
|
else if(curr < end && *curr != '\t' && *curr != '\r')
|
|
|
|
{
|
|
|
|
char * begin = curr;
|
|
|
|
|
|
|
|
while (curr < end && *curr != '\t' && *curr != '\r' && *curr != '\"')//find end of string or next quoted part start
|
2016-08-12 11:10:27 +02:00
|
|
|
curr++;
|
|
|
|
|
2015-09-12 22:03:10 +02:00
|
|
|
ret += std::string(begin, curr);
|
2016-08-12 11:10:27 +02:00
|
|
|
|
2015-09-12 22:03:10 +02:00
|
|
|
if(curr>=end || *curr != '\"')
|
|
|
|
return ret;
|
2016-08-12 11:10:27 +02:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
else // end of string
|
|
|
|
return ret;
|
2008-12-22 19:48:41 +02:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CLegacyConfigParser::extractNormalString()
|
|
|
|
{
|
|
|
|
char * begin = curr;
|
|
|
|
|
|
|
|
while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
|
|
|
|
curr++;
|
|
|
|
|
|
|
|
return std::string(begin, curr);
|
|
|
|
}
|
|
|
|
|
2013-10-26 00:45:14 +03:00
|
|
|
std::string CLegacyConfigParser::readRawString()
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
|
|
|
if (curr >= end || *curr == '\n')
|
|
|
|
return "";
|
|
|
|
|
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
if (*curr == '\"')
|
|
|
|
ret = extractQuotedString();// quoted text - find closing quote
|
|
|
|
else
|
|
|
|
ret = extractNormalString();//string without quotes - copy till \t or \r
|
|
|
|
|
2018-04-19 15:12:18 +02:00
|
|
|
curr++;
|
2012-08-25 11:44:51 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-26 00:45:14 +03:00
|
|
|
std::string CLegacyConfigParser::readString()
|
|
|
|
{
|
|
|
|
// do not convert strings that are already in ASCII - this will only slow down loading process
|
|
|
|
std::string str = readRawString();
|
|
|
|
if (Unicode::isValidASCII(str))
|
|
|
|
return str;
|
|
|
|
return Unicode::toUnicode(str);
|
|
|
|
}
|
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
float CLegacyConfigParser::readNumber()
|
|
|
|
{
|
2013-10-26 00:45:14 +03:00
|
|
|
std::string input = readRawString();
|
2012-08-25 11:44:51 +03:00
|
|
|
|
|
|
|
std::istringstream stream(input);
|
|
|
|
|
2017-07-16 11:58:05 +02:00
|
|
|
if(input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
|
|
|
|
stream.imbue(std::locale(std::locale(), new LocaleWithComma()));
|
2012-08-25 11:44:51 +03:00
|
|
|
|
2013-06-09 13:09:28 +03:00
|
|
|
float result;
|
2012-08-25 11:44:51 +03:00
|
|
|
if ( !(stream >> result) )
|
|
|
|
return 0;
|
|
|
|
return result;
|
|
|
|
}
|
2008-12-22 19:48:41 +02:00
|
|
|
|
2013-06-23 22:35:54 +03:00
|
|
|
bool CLegacyConfigParser::isNextEntryEmpty() const
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
2012-09-26 16:13:39 +03:00
|
|
|
char * nextSymbol = curr;
|
|
|
|
while (nextSymbol < end && *nextSymbol == ' ')
|
|
|
|
nextSymbol++; //find next meaningfull symbol
|
|
|
|
|
|
|
|
return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
|
2012-08-25 11:44:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CLegacyConfigParser::endLine()
|
|
|
|
{
|
|
|
|
while (curr < end && *curr != '\n')
|
|
|
|
readString();
|
|
|
|
|
2018-04-19 15:12:18 +02:00
|
|
|
curr++;
|
2012-08-25 11:44:51 +03:00
|
|
|
|
|
|
|
return curr < end;
|
|
|
|
}
|
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
void CGeneralTextHandler::readToVector(std::string const & sourceID, std::string const & sourceName)
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
|
|
|
CLegacyConfigParser parser(sourceName);
|
2022-12-27 22:19:05 +02:00
|
|
|
size_t index = 0;
|
2012-08-25 11:44:51 +03:00
|
|
|
do
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({sourceID, index}, parser.readString());
|
2022-12-27 22:19:05 +02:00
|
|
|
index += 1;
|
2008-12-22 19:48:41 +02:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
while (parser.endLine());
|
|
|
|
}
|
2008-12-22 19:48:41 +02:00
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
const std::string & CGeneralTextHandler::serialize(const std::string & identifier) const
|
|
|
|
{
|
|
|
|
assert(stringsIdentifiers.count(identifier));
|
|
|
|
return stringsIdentifiers.at(identifier);
|
|
|
|
}
|
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
const std::string & CGeneralTextHandler::deserialize(const TextIdentifier & identifier) const
|
2022-12-27 22:19:05 +02:00
|
|
|
{
|
2023-01-01 22:50:38 +02:00
|
|
|
if(stringsLocalizations.count(identifier.get()))
|
2022-12-30 19:09:19 +02:00
|
|
|
return stringsLocalizations.at(identifier.get());
|
|
|
|
logGlobal->error("Unable to find localization for string '%s'", identifier.get());
|
|
|
|
return identifier.get();
|
2022-12-27 22:19:05 +02:00
|
|
|
}
|
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
void CGeneralTextHandler::registerString(const TextIdentifier & UID, const std::string & localized)
|
2022-12-27 22:19:05 +02:00
|
|
|
{
|
2023-01-18 23:56:01 +02:00
|
|
|
assert(UID.get().find("..") == std::string::npos);
|
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
stringsIdentifiers[localized] = UID.get();
|
|
|
|
stringsLocalizations[UID.get()] = localized;
|
2022-12-27 22:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CGeneralTextHandler::CGeneralTextHandler():
|
|
|
|
victoryConditions(*this, "core.vcdesc" ),
|
|
|
|
lossCondtions (*this, "core.lcdesc" ),
|
|
|
|
colors (*this, "core.plcolors" ),
|
|
|
|
tcommands (*this, "core.tcommand" ),
|
|
|
|
hcommands (*this, "core.hallinfo" ),
|
|
|
|
fcommands (*this, "core.castinfo" ),
|
|
|
|
advobtxt (*this, "core.advevent" ),
|
|
|
|
xtrainfo (*this, "core.xtrainfo" ),
|
|
|
|
restypes (*this, "core.restypes" ),
|
|
|
|
randsign (*this, "core.randsign" ),
|
|
|
|
overview (*this, "core.overview" ),
|
|
|
|
arraytxt (*this, "core.arraytxt" ),
|
|
|
|
primarySkillNames(*this, "core.priskill" ),
|
|
|
|
jktexts (*this, "core.jktext" ),
|
|
|
|
tavernInfo (*this, "core.tvrninfo" ),
|
|
|
|
tavernRumors (*this, "core.randtvrn" ),
|
|
|
|
turnDurations (*this, "core.turndur" ),
|
|
|
|
heroscrn (*this, "core.heroscrn" ),
|
|
|
|
tentColors (*this, "core.tentcolr" ),
|
|
|
|
levels (*this, "core.skilllev" ),
|
|
|
|
zelp (*this, "core.help" ),
|
2022-12-27 23:17:41 +02:00
|
|
|
allTexts (*this, "core.genrltxt" ),
|
2022-12-27 22:19:05 +02:00
|
|
|
// pseudo-array, that don't have H3 file with same name
|
2022-12-27 23:17:41 +02:00
|
|
|
seerEmpty (*this, "core.seerhut.empty" ),
|
|
|
|
seerNames (*this, "core.seerhut.names" ),
|
2022-12-27 22:19:05 +02:00
|
|
|
capColors (*this, "vcmi.capitalColors" ),
|
2022-12-28 00:17:16 +02:00
|
|
|
znpc00 (*this, "vcmi.znpc00" ), // technically - wog
|
2022-12-27 22:19:05 +02:00
|
|
|
qeModCommands (*this, "vcmi.quickExchange" )
|
|
|
|
{
|
2023-01-09 00:24:11 +02:00
|
|
|
if (getSelectedEncoding().empty())
|
|
|
|
detectEncoding();
|
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
readToVector("core.vcdesc", "DATA/VCDESC.TXT" );
|
|
|
|
readToVector("core.lcdesc", "DATA/LCDESC.TXT" );
|
|
|
|
readToVector("core.tcommand", "DATA/TCOMMAND.TXT" );
|
|
|
|
readToVector("core.hallinfo", "DATA/HALLINFO.TXT" );
|
|
|
|
readToVector("core.castinfo", "DATA/CASTINFO.TXT" );
|
|
|
|
readToVector("core.advevent", "DATA/ADVEVENT.TXT" );
|
|
|
|
readToVector("core.xtrainfo", "DATA/XTRAINFO.TXT" );
|
|
|
|
readToVector("core.restypes", "DATA/RESTYPES.TXT" );
|
|
|
|
readToVector("core.randsign", "DATA/RANDSIGN.TXT" );
|
2023-01-10 12:25:23 +02:00
|
|
|
readToVector("core.crgen1", "DATA/CRGEN1.TXT" );
|
|
|
|
readToVector("core.crgen4", "DATA/CRGEN4.TXT" );
|
2022-12-27 22:19:05 +02:00
|
|
|
readToVector("core.overview", "DATA/OVERVIEW.TXT" );
|
|
|
|
readToVector("core.arraytxt", "DATA/ARRAYTXT.TXT" );
|
|
|
|
readToVector("core.priskill", "DATA/PRISKILL.TXT" );
|
|
|
|
readToVector("core.jktext", "DATA/JKTEXT.TXT" );
|
|
|
|
readToVector("core.tvrninfo", "DATA/TVRNINFO.TXT" );
|
|
|
|
readToVector("core.turndur", "DATA/TURNDUR.TXT" );
|
|
|
|
readToVector("core.heroscrn", "DATA/HEROSCRN.TXT" );
|
|
|
|
readToVector("core.tentcolr", "DATA/TENTCOLR.TXT" );
|
|
|
|
readToVector("core.skilllev", "DATA/SKILLLEV.TXT" );
|
|
|
|
readToVector("core.cmpmusic", "DATA/CMPMUSIC.TXT" );
|
|
|
|
readToVector("core.minename", "DATA/MINENAME.TXT" );
|
|
|
|
readToVector("core.mineevnt", "DATA/MINEEVNT.TXT" );
|
2012-08-25 11:44:51 +03:00
|
|
|
|
2021-11-28 21:48:22 +02:00
|
|
|
static const char * QE_MOD_COMMANDS = "DATA/QECOMMANDS.TXT";
|
|
|
|
if (CResourceHandler::get()->existsResource(ResourceID(QE_MOD_COMMANDS, EResType::TEXT)))
|
2022-12-27 22:19:05 +02:00
|
|
|
readToVector("vcmi.quickExchange", QE_MOD_COMMANDS);
|
|
|
|
|
|
|
|
auto vcmiTexts = JsonNode(ResourceID("config/translate.json", EResType::TEXT));
|
2020-04-01 00:01:57 +02:00
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
for ( auto const & node : vcmiTexts.Struct())
|
|
|
|
registerString(node.first, node.second.String());
|
2013-10-27 16:05:01 +03:00
|
|
|
|
2022-12-28 01:14:19 +02:00
|
|
|
{
|
|
|
|
CLegacyConfigParser parser("DATA/RANDTVRN.TXT");
|
|
|
|
parser.endLine();
|
|
|
|
size_t index = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::string line = parser.readString();
|
2023-01-01 22:50:38 +02:00
|
|
|
if(!line.empty())
|
2022-12-28 01:14:19 +02:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"core.randtvrn", index}, line);
|
2022-12-28 01:14:19 +02:00
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (parser.endLine());
|
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
|
|
|
CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
|
|
|
|
parser.endLine();
|
2022-12-27 23:17:41 +02:00
|
|
|
size_t index = 0;
|
2012-08-25 11:44:51 +03:00
|
|
|
do
|
2008-12-22 19:48:41 +02:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"core.genrltxt", index}, parser.readString());
|
2022-12-27 23:17:41 +02:00
|
|
|
index += 1;
|
2008-12-22 19:48:41 +02:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
while (parser.endLine());
|
2008-12-22 19:48:41 +02:00
|
|
|
}
|
2009-05-22 02:50:45 +03:00
|
|
|
{
|
2013-05-04 16:14:23 +03:00
|
|
|
CLegacyConfigParser parser("DATA/HELP.TXT");
|
2022-12-27 22:19:05 +02:00
|
|
|
size_t index = 0;
|
2012-08-25 11:44:51 +03:00
|
|
|
do
|
2009-05-22 02:50:45 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
std::string first = parser.readString();
|
|
|
|
std::string second = parser.readString();
|
2022-12-28 23:23:11 +02:00
|
|
|
registerString("core.help." + std::to_string(index) + ".hover", first);
|
2022-12-27 22:19:05 +02:00
|
|
|
registerString("core.help." + std::to_string(index) + ".help", second);
|
|
|
|
index += 1;
|
2009-05-22 02:50:45 +03:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
while (parser.endLine());
|
2009-05-22 02:50:45 +03:00
|
|
|
}
|
2009-01-11 00:08:18 +02:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
|
2022-12-27 22:19:05 +02:00
|
|
|
size_t index = 0;
|
2012-08-25 11:44:51 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
std::string color = parser.readString();
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"core.plcolors", index}, color);
|
2012-08-25 11:44:51 +03:00
|
|
|
color[0] = toupper(color[0]);
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"vcmi.capitalColors", index}, color);
|
2022-12-27 22:19:05 +02:00
|
|
|
index += 1;
|
2012-08-25 11:44:51 +03:00
|
|
|
}
|
|
|
|
while (parser.endLine());
|
2009-01-11 00:08:18 +02:00
|
|
|
}
|
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
CLegacyConfigParser parser("DATA/SEERHUT.TXT");
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
//skip header
|
|
|
|
parser.endLine();
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
for (size_t i = 0; i < 6; ++i)
|
2022-12-27 22:19:05 +02:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"core.seerhut.empty", i}, parser.readString());
|
2022-12-27 22:19:05 +02:00
|
|
|
}
|
2013-07-23 18:03:01 +03:00
|
|
|
parser.endLine();
|
2010-01-30 22:53:47 +02:00
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
for (size_t i = 0; i < 9; ++i) //9 types of quests
|
2010-01-26 21:43:15 +02:00
|
|
|
{
|
2022-12-27 23:17:41 +02:00
|
|
|
std::string questName = CQuest::missionName(CQuest::Emission(1+i));
|
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
for (size_t j = 0; j < 5; ++j)
|
2010-01-26 21:43:15 +02:00
|
|
|
{
|
2022-12-27 23:17:41 +02:00
|
|
|
std::string questState = CQuest::missionState(j);
|
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
parser.readString(); //front description
|
2022-12-30 19:09:19 +02:00
|
|
|
for (size_t k = 0; k < 6; ++k)
|
2022-12-27 22:19:05 +02:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"core.seerhut.quest", questName, questState, k}, parser.readString());
|
2022-12-27 22:19:05 +02:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
parser.endLine();
|
2010-01-26 21:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
2010-01-30 22:53:47 +02:00
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
for (size_t k = 0; k < 6; ++k) //Time limit
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"core.seerhut.time", k}, parser.readString());
|
2012-08-25 11:44:51 +03:00
|
|
|
}
|
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
parser.endLine(); // empty line
|
|
|
|
parser.endLine(); // header
|
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
for (size_t i = 0; i < 48; ++i)
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"core.seerhut.names", i}, parser.readString());
|
2012-08-25 11:44:51 +03:00
|
|
|
parser.endLine();
|
|
|
|
}
|
2010-01-30 22:53:47 +02:00
|
|
|
}
|
2012-07-08 19:36:20 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
|
2012-07-08 19:36:20 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
//skip header
|
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
std::string text;
|
2022-12-28 00:17:16 +02:00
|
|
|
size_t campaignsCount = 0;
|
2012-08-25 11:44:51 +03:00
|
|
|
do
|
2010-02-12 17:04:01 +02:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
text = parser.readString();
|
|
|
|
if (!text.empty())
|
2022-12-28 00:17:16 +02:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"core.camptext.names", campaignsCount}, text);
|
2022-12-28 00:17:16 +02:00
|
|
|
campaignsCount += 1;
|
|
|
|
}
|
2010-02-12 17:04:01 +02:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
while (parser.endLine() && !text.empty());
|
2010-02-12 17:04:01 +02:00
|
|
|
|
2022-12-28 00:32:44 +02:00
|
|
|
for (size_t campaign=0; campaign<campaignsCount; campaign++)
|
2010-02-12 17:04:01 +02:00
|
|
|
{
|
2022-12-28 00:17:16 +02:00
|
|
|
size_t region = 0;
|
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
do // skip empty space and header
|
2010-02-12 17:04:01 +02:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
text = parser.readString();
|
2010-02-12 17:04:01 +02:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
while (parser.endLine() && text.empty());
|
2011-02-07 16:39:17 +02:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
text = parser.readString();
|
|
|
|
if (!text.empty())
|
2022-12-28 00:17:16 +02:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
registerString({"core.camptext.regions", std::to_string(campaign), region}, text);
|
2022-12-28 00:17:16 +02:00
|
|
|
region += 1;
|
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
}
|
|
|
|
while (parser.endLine() && !text.empty());
|
2014-07-04 12:48:09 +03:00
|
|
|
|
2022-12-28 00:17:16 +02:00
|
|
|
scenariosCountPerCampaign.push_back(region);
|
2012-08-25 11:44:51 +03:00
|
|
|
}
|
2011-03-21 10:14:23 +02:00
|
|
|
}
|
2017-05-26 21:58:33 +02:00
|
|
|
if (VLC->modh->modules.COMMANDERS)
|
|
|
|
{
|
2023-01-01 22:50:38 +02:00
|
|
|
if(CResourceHandler::get()->existsResource(ResourceID("DATA/ZNPC00.TXT", EResType::TEXT)))
|
2022-12-28 01:14:19 +02:00
|
|
|
readToVector("vcmi.znpc00", "DATA/ZNPC00.TXT" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t CGeneralTextHandler::pluralText(const int32_t textIndex, const int32_t count) const
|
2017-07-04 13:24:46 +02:00
|
|
|
{
|
|
|
|
if(textIndex == 0)
|
|
|
|
return 0;
|
|
|
|
else if(textIndex < 0)
|
|
|
|
return -textIndex;
|
2017-07-08 20:20:38 +02:00
|
|
|
else if(count == 1)
|
2017-07-04 13:24:46 +02:00
|
|
|
return textIndex;
|
2022-12-28 01:14:19 +02:00
|
|
|
else
|
|
|
|
return textIndex + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGeneralTextHandler::dumpAllTexts()
|
|
|
|
{
|
|
|
|
logGlobal->info("BEGIN TEXT EXPORT");
|
|
|
|
for ( auto const & entry : stringsLocalizations)
|
|
|
|
{
|
|
|
|
auto cleanString = entry.second;
|
|
|
|
boost::replace_all(cleanString, "\\", "\\\\");
|
|
|
|
boost::replace_all(cleanString, "\n", "\\n");
|
|
|
|
boost::replace_all(cleanString, "\r", "\\r");
|
|
|
|
boost::replace_all(cleanString, "\t", "\\t");
|
|
|
|
boost::replace_all(cleanString, "\"", "\\\"");
|
|
|
|
|
|
|
|
logGlobal->info("\"%s\" : \"%s\",", entry.first, cleanString);
|
|
|
|
}
|
|
|
|
logGlobal->info("END TEXT EXPORT");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CGeneralTextHandler::getCampaignLength(size_t campaignID) const
|
2022-12-28 00:17:16 +02:00
|
|
|
{
|
|
|
|
assert(campaignID < scenariosCountPerCampaign.size());
|
|
|
|
|
2023-01-01 22:50:38 +02:00
|
|
|
if(campaignID < scenariosCountPerCampaign.size())
|
2022-12-28 00:17:16 +02:00
|
|
|
return scenariosCountPerCampaign[campaignID];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
std::vector<std::string> CGeneralTextHandler::findStringsWithPrefix(std::string const & prefix)
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
for (auto const & entry : stringsLocalizations)
|
|
|
|
{
|
2023-01-01 22:50:38 +02:00
|
|
|
if(boost::algorithm::starts_with(entry.first, prefix))
|
2022-12-27 22:19:05 +02:00
|
|
|
result.push_back(entry.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
LegacyTextContainer::LegacyTextContainer(CGeneralTextHandler & owner, std::string const & basePath):
|
|
|
|
owner(owner),
|
|
|
|
basePath(basePath)
|
|
|
|
{}
|
|
|
|
|
2023-01-01 20:54:05 +02:00
|
|
|
std::string LegacyTextContainer::operator[](size_t index) const
|
2022-12-27 22:19:05 +02:00
|
|
|
{
|
2022-12-30 19:09:19 +02:00
|
|
|
return owner.translate(basePath, index);
|
2022-12-27 22:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LegacyHelpContainer::LegacyHelpContainer(CGeneralTextHandler & owner, std::string const & basePath):
|
|
|
|
owner(owner),
|
|
|
|
basePath(basePath)
|
|
|
|
{}
|
|
|
|
|
|
|
|
std::pair<std::string, std::string> LegacyHelpContainer::operator[](size_t index) const
|
|
|
|
{
|
|
|
|
return {
|
2022-12-28 23:23:11 +02:00
|
|
|
owner.translate(basePath + "." + std::to_string(index) + ".hover"),
|
2022-12-27 22:19:05 +02:00
|
|
|
owner.translate(basePath + "." + std::to_string(index) + ".help")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|