2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CGeneralTextHandler.h, 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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
2009-04-15 17:03:31 +03:00
|
|
|
|
2023-09-01 23:26:14 +02:00
|
|
|
#include "filesystem/ResourcePath.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
class CInputStream;
|
2023-02-12 23:00:56 +02:00
|
|
|
class JsonNode;
|
2023-09-28 04:23:14 +02:00
|
|
|
class JsonSerializeFormat;
|
2012-08-25 11:44:51 +03:00
|
|
|
|
|
|
|
/// Parser for any text files from H3
|
2013-11-09 16:49:36 +03:00
|
|
|
class DLL_LINKAGE CLegacyConfigParser
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
2023-02-24 16:15:45 +02:00
|
|
|
std::string fileEncoding;
|
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
std::unique_ptr<char[]> data;
|
|
|
|
char * curr;
|
|
|
|
char * end;
|
|
|
|
|
|
|
|
/// extracts part of quoted string.
|
|
|
|
std::string extractQuotedPart();
|
|
|
|
|
|
|
|
/// extracts quoted string. Any end of lines are ignored, double-quote is considered as "escaping"
|
|
|
|
std::string extractQuotedString();
|
|
|
|
|
|
|
|
/// extracts non-quoted string
|
|
|
|
std::string extractNormalString();
|
|
|
|
|
2013-10-26 00:45:14 +03:00
|
|
|
/// reads "raw" string without encoding conversion
|
|
|
|
std::string readRawString();
|
2023-03-05 21:50:38 +02:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
public:
|
|
|
|
/// read one entry from current line. Return ""/0 if end of line reached
|
|
|
|
std::string readString();
|
|
|
|
float readNumber();
|
|
|
|
|
2012-12-14 18:32:53 +03:00
|
|
|
template <typename numeric>
|
|
|
|
std::vector<numeric> readNumArray(size_t size)
|
|
|
|
{
|
|
|
|
std::vector<numeric> ret;
|
|
|
|
ret.reserve(size);
|
|
|
|
while (size--)
|
2020-10-01 10:38:06 +02:00
|
|
|
ret.push_back((numeric)readNumber());
|
2012-12-14 18:32:53 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
/// returns true if next entry is empty
|
2013-06-23 22:35:54 +03:00
|
|
|
bool isNextEntryEmpty() const;
|
2012-08-25 11:44:51 +03:00
|
|
|
|
|
|
|
/// end current line
|
|
|
|
bool endLine();
|
|
|
|
|
2023-09-01 23:26:14 +02:00
|
|
|
explicit CLegacyConfigParser(const TextPath & URI);
|
2012-08-25 11:44:51 +03:00
|
|
|
};
|
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
class CGeneralTextHandler;
|
|
|
|
|
2022-12-27 23:17:41 +02:00
|
|
|
/// Small wrapper that provides text access API compatible with old code
|
2022-12-27 22:19:05 +02:00
|
|
|
class DLL_LINKAGE LegacyTextContainer
|
|
|
|
{
|
|
|
|
CGeneralTextHandler & owner;
|
|
|
|
std::string basePath;
|
|
|
|
|
|
|
|
public:
|
2023-03-13 23:26:44 +02:00
|
|
|
LegacyTextContainer(CGeneralTextHandler & owner, std::string basePath);
|
2023-01-01 20:54:05 +02:00
|
|
|
std::string operator [](size_t index) const;
|
2022-12-27 22:19:05 +02:00
|
|
|
};
|
|
|
|
|
2022-12-27 23:17:41 +02:00
|
|
|
/// Small wrapper that provides help text access API compatible with old code
|
2022-12-27 22:19:05 +02:00
|
|
|
class DLL_LINKAGE LegacyHelpContainer
|
|
|
|
{
|
|
|
|
CGeneralTextHandler & owner;
|
|
|
|
std::string basePath;
|
|
|
|
|
|
|
|
public:
|
2023-03-13 23:26:44 +02:00
|
|
|
LegacyHelpContainer(CGeneralTextHandler & owner, std::string basePath);
|
2022-12-27 22:19:05 +02:00
|
|
|
std::pair<std::string, std::string> operator[](size_t index) const;
|
|
|
|
};
|
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
class TextIdentifier
|
|
|
|
{
|
|
|
|
std::string identifier;
|
|
|
|
public:
|
2023-03-13 23:26:44 +02:00
|
|
|
const std::string & get() const
|
2022-12-30 19:09:19 +02:00
|
|
|
{
|
|
|
|
return identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextIdentifier(const char * id):
|
|
|
|
identifier(id)
|
|
|
|
{}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
TextIdentifier(const std::string & id):
|
|
|
|
identifier(id)
|
2022-12-30 19:09:19 +02:00
|
|
|
{}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
template<typename... T>
|
|
|
|
TextIdentifier(const std::string & id, size_t index, T... rest):
|
|
|
|
TextIdentifier(id + '.' + std::to_string(index), rest...)
|
2022-12-30 19:09:19 +02:00
|
|
|
{}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
template<typename... T>
|
|
|
|
TextIdentifier(const std::string & id, const std::string & id2, T... rest):
|
|
|
|
TextIdentifier(id + '.' + id2, rest...)
|
2022-12-30 19:09:19 +02:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2023-09-28 02:52:49 +02:00
|
|
|
class DLL_LINKAGE TextLocalizationContainer
|
2009-01-11 00:08:18 +02:00
|
|
|
{
|
2023-09-28 02:52:49 +02:00
|
|
|
protected:
|
2023-02-09 15:03:49 +02:00
|
|
|
struct StringState
|
|
|
|
{
|
|
|
|
/// Human-readable string that was added on registration
|
|
|
|
std::string baseValue;
|
|
|
|
|
|
|
|
/// Language of base string
|
|
|
|
std::string baseLanguage;
|
2022-12-27 22:19:05 +02:00
|
|
|
|
2023-02-09 15:03:49 +02:00
|
|
|
/// Translated human-readable string
|
|
|
|
std::string overrideValue;
|
|
|
|
|
|
|
|
/// Language of the override string
|
|
|
|
std::string overrideLanguage;
|
|
|
|
|
|
|
|
/// ID of mod that created this string
|
|
|
|
std::string modContext;
|
2023-09-28 02:52:49 +02:00
|
|
|
|
|
|
|
template <typename Handler>
|
|
|
|
void serialize(Handler & h, const int Version)
|
|
|
|
{
|
|
|
|
h & baseValue;
|
|
|
|
h & baseLanguage;
|
|
|
|
h & modContext;
|
|
|
|
}
|
2023-02-09 15:03:49 +02:00
|
|
|
};
|
2023-09-28 02:52:49 +02:00
|
|
|
|
2023-02-09 15:03:49 +02:00
|
|
|
/// map identifier -> localization
|
|
|
|
std::unordered_map<std::string, StringState> stringsLocalizations;
|
2023-09-28 02:52:49 +02:00
|
|
|
|
2023-10-11 01:26:07 +02:00
|
|
|
std::vector<const TextLocalizationContainer *> subContainers;
|
2023-09-28 02:52:49 +02:00
|
|
|
|
2023-07-30 12:19:52 +02:00
|
|
|
/// add selected string to internal storage as high-priority strings
|
|
|
|
void registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized);
|
2023-09-28 02:52:49 +02:00
|
|
|
|
|
|
|
std::string getModLanguage(const std::string & modContext);
|
|
|
|
|
2023-07-30 12:19:52 +02:00
|
|
|
public:
|
2023-02-09 19:15:39 +02:00
|
|
|
/// validates translation of specified language for specified mod
|
|
|
|
/// returns true if localization is valid and complete
|
|
|
|
/// any error messages will be written to log file
|
|
|
|
bool validateTranslation(const std::string & language, const std::string & modContext, JsonNode const & file) const;
|
|
|
|
|
2023-01-16 23:20:24 +02:00
|
|
|
/// Loads translation from provided json
|
|
|
|
/// Any entries loaded by this will have priority over texts registered normally
|
2023-02-09 19:15:39 +02:00
|
|
|
void loadTranslationOverrides(const std::string & language, const std::string & modContext, JsonNode const & file);
|
2023-01-16 23:20:24 +02:00
|
|
|
|
2023-09-28 02:52:49 +02:00
|
|
|
// returns true if identifier with such name was registered, even if not translated to current language
|
|
|
|
bool identifierExists(const TextIdentifier & UID) const;
|
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
/// add selected string to internal storage
|
2023-02-09 15:03:49 +02:00
|
|
|
void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
|
2023-09-28 04:23:14 +02:00
|
|
|
void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized, const std::string & language);
|
2023-09-28 02:52:49 +02:00
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
/// returns translated version of a string that can be displayed to user
|
2022-12-30 19:09:19 +02:00
|
|
|
template<typename ... Args>
|
2023-01-01 20:54:05 +02:00
|
|
|
std::string translate(std::string arg1, Args ... args) const
|
2022-12-30 19:09:19 +02:00
|
|
|
{
|
|
|
|
TextIdentifier id(arg1, args ...);
|
|
|
|
return deserialize(id);
|
|
|
|
}
|
2022-12-27 22:19:05 +02:00
|
|
|
|
2022-12-30 19:09:19 +02:00
|
|
|
/// converts identifier into user-readable string
|
|
|
|
const std::string & deserialize(const TextIdentifier & identifier) const;
|
2023-09-28 02:52:49 +02:00
|
|
|
|
2022-12-28 00:17:16 +02:00
|
|
|
/// Debug method, dumps all currently known texts into console using Json-like format
|
2022-12-27 23:17:41 +02:00
|
|
|
void dumpAllTexts();
|
2023-09-28 02:52:49 +02:00
|
|
|
|
|
|
|
/// Add or override subcontainer which can store identifiers
|
|
|
|
void addSubContainer(const TextLocalizationContainer & container);
|
|
|
|
|
|
|
|
/// Remove subcontainer with give name
|
|
|
|
void removeSubContainer(const TextLocalizationContainer & container);
|
|
|
|
|
2023-09-28 04:23:14 +02:00
|
|
|
void jsonSerialize(JsonNode & dest) const;
|
|
|
|
|
2023-09-28 02:52:49 +02:00
|
|
|
template <typename Handler>
|
|
|
|
void serialize(Handler & h, const int Version)
|
|
|
|
{
|
|
|
|
std::string key;
|
|
|
|
auto sz = stringsLocalizations.size();
|
|
|
|
h & sz;
|
|
|
|
if(h.saving)
|
|
|
|
{
|
|
|
|
for(auto s : stringsLocalizations)
|
|
|
|
{
|
|
|
|
key = s.first;
|
|
|
|
h & key;
|
|
|
|
h & s.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(size_t i = 0; i < sz; ++i)
|
|
|
|
{
|
|
|
|
h & key;
|
|
|
|
h & stringsLocalizations[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Handles all text-related data in game
|
|
|
|
class DLL_LINKAGE CGeneralTextHandler: public TextLocalizationContainer
|
|
|
|
{
|
|
|
|
void readToVector(const std::string & sourceID, const std::string & sourceName);
|
2022-12-27 23:17:41 +02:00
|
|
|
|
2023-09-28 02:52:49 +02:00
|
|
|
/// number of scenarios in specific campaign. TODO: move to a better location
|
|
|
|
std::vector<size_t> scenariosCountPerCampaign;
|
|
|
|
|
|
|
|
public:
|
2022-12-27 23:17:41 +02:00
|
|
|
LegacyTextContainer allTexts;
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
LegacyTextContainer arraytxt;
|
|
|
|
LegacyTextContainer primarySkillNames;
|
|
|
|
LegacyTextContainer jktexts;
|
|
|
|
LegacyTextContainer heroscrn;
|
|
|
|
LegacyTextContainer overview;//text for Kingdom Overview window
|
|
|
|
LegacyTextContainer colors; //names of player colors ("red",...)
|
|
|
|
LegacyTextContainer capColors; //names of player colors with first letter capitalized ("Red",...)
|
|
|
|
LegacyTextContainer turnDurations; //turn durations for pregame (1 Minute ... Unlimited)
|
2009-01-11 00:08:18 +02:00
|
|
|
|
|
|
|
//towns
|
2022-12-27 22:19:05 +02:00
|
|
|
LegacyTextContainer tcommands, hcommands, fcommands; //texts for town screen, town hall screen and fort screen
|
|
|
|
LegacyTextContainer tavernInfo;
|
|
|
|
LegacyTextContainer tavernRumors;
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
LegacyTextContainer qeModCommands;
|
2020-04-01 00:01:57 +02:00
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
LegacyHelpContainer zelp;
|
|
|
|
LegacyTextContainer lossCondtions;
|
|
|
|
LegacyTextContainer victoryConditions;
|
2009-01-06 20:42:20 +02:00
|
|
|
|
|
|
|
//objects
|
2022-12-27 22:19:05 +02:00
|
|
|
LegacyTextContainer advobtxt;
|
|
|
|
LegacyTextContainer restypes; //names of resources
|
|
|
|
LegacyTextContainer randsign;
|
2022-12-27 23:17:41 +02:00
|
|
|
LegacyTextContainer seerEmpty;
|
|
|
|
LegacyTextContainer seerNames;
|
2022-12-27 22:19:05 +02:00
|
|
|
LegacyTextContainer tentColors;
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2009-01-25 18:40:50 +02:00
|
|
|
//sec skills
|
2022-12-27 22:19:05 +02:00
|
|
|
LegacyTextContainer levels;
|
2017-05-26 21:58:33 +02:00
|
|
|
//commanders
|
2022-12-28 00:17:16 +02:00
|
|
|
LegacyTextContainer znpc00; //more or less useful content of that file
|
2016-10-29 17:00:12 +02:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::vector<std::string> findStringsWithPrefix(const std::string & prefix);
|
2010-02-12 17:04:01 +02:00
|
|
|
|
2023-02-09 19:15:39 +02:00
|
|
|
int32_t pluralText(int32_t textIndex, int32_t count) const;
|
2017-07-04 13:24:46 +02:00
|
|
|
|
2022-12-28 00:17:16 +02:00
|
|
|
size_t getCampaignLength(size_t campaignID) const;
|
|
|
|
|
2009-01-11 00:08:18 +02:00
|
|
|
CGeneralTextHandler();
|
2016-10-29 17:00:12 +02:00
|
|
|
CGeneralTextHandler(const CGeneralTextHandler&) = delete;
|
|
|
|
CGeneralTextHandler operator=(const CGeneralTextHandler&) = delete;
|
2023-01-16 23:20:24 +02:00
|
|
|
|
2023-02-09 15:03:49 +02:00
|
|
|
/// Attempts to detect encoding & language of H3 files
|
|
|
|
static void detectInstallParameters();
|
|
|
|
|
2023-01-16 23:20:24 +02:00
|
|
|
/// Returns name of language preferred by user
|
2023-02-09 15:03:49 +02:00
|
|
|
static std::string getPreferredLanguage();
|
|
|
|
|
|
|
|
/// Returns name of language of Heroes III text files
|
2023-01-16 23:20:24 +02:00
|
|
|
static std::string getInstalledLanguage();
|
|
|
|
|
|
|
|
/// Returns name of encoding of Heroes III text files
|
|
|
|
static std::string getInstalledEncoding();
|
2009-01-11 00:08:18 +02:00
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|