1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Add mutex as a quick fix for concurrent access to text handler by client

and server
This commit is contained in:
Ivan Savenko 2024-05-18 09:02:21 +00:00
parent 004e6d1fcb
commit 0dfa781655
2 changed files with 28 additions and 3 deletions

View File

@ -23,6 +23,8 @@
VCMI_LIB_NAMESPACE_BEGIN
std::recursive_mutex TextLocalizationContainer::globalTextMutex;
/// Detects language and encoding of H3 text files based on matching against pregenerated footprints of H3 file
void CGeneralTextHandler::detectInstallParameters()
{
@ -251,6 +253,8 @@ bool CLegacyConfigParser::endLine()
void TextLocalizationContainer::registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
assert(!modContext.empty());
assert(!language.empty());
@ -265,12 +269,16 @@ void TextLocalizationContainer::registerStringOverride(const std::string & modCo
void TextLocalizationContainer::addSubContainer(const TextLocalizationContainer & container)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
assert(!vstd::contains(subContainers, &container));
subContainers.push_back(&container);
}
void TextLocalizationContainer::removeSubContainer(const TextLocalizationContainer & container)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
assert(vstd::contains(subContainers, &container));
subContainers.erase(std::remove(subContainers.begin(), subContainers.end(), &container), subContainers.end());
@ -278,6 +286,8 @@ void TextLocalizationContainer::removeSubContainer(const TextLocalizationContain
const std::string & TextLocalizationContainer::deserialize(const TextIdentifier & identifier) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
if(stringsLocalizations.count(identifier.get()) == 0)
{
for(auto containerIter = subContainers.rbegin(); containerIter != subContainers.rend(); ++containerIter)
@ -297,6 +307,8 @@ const std::string & TextLocalizationContainer::deserialize(const TextIdentifier
void TextLocalizationContainer::registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized, const std::string & language)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
assert(!modContext.empty());
assert(!Languages::getLanguageOptions(language).identifier.empty());
assert(UID.get().find("..") == std::string::npos); // invalid identifier - there is section that was evaluated to empty string
@ -327,6 +339,8 @@ void TextLocalizationContainer::registerString(const std::string & modContext, c
bool TextLocalizationContainer::validateTranslation(const std::string & language, const std::string & modContext, const JsonNode & config) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
bool allPresent = true;
for(const auto & string : stringsLocalizations)
@ -384,11 +398,15 @@ void TextLocalizationContainer::loadTranslationOverrides(const std::string & lan
bool TextLocalizationContainer::identifierExists(const TextIdentifier & UID) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
return stringsLocalizations.count(UID.get());
}
void TextLocalizationContainer::exportAllTexts(std::map<std::string, std::map<std::string, std::string>> & storage) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
for (auto const & subContainer : subContainers)
subContainer->exportAllTexts(storage);
@ -418,6 +436,8 @@ std::string TextLocalizationContainer::getModLanguage(const std::string & modCon
void TextLocalizationContainer::jsonSerialize(JsonNode & dest) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
for(auto & s : stringsLocalizations)
{
dest.Struct()[s.first].String() = s.second.baseValue;
@ -692,6 +712,7 @@ std::string CGeneralTextHandler::getInstalledEncoding()
std::vector<std::string> CGeneralTextHandler::findStringsWithPrefix(const std::string & prefix)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::vector<std::string> result;
for(const auto & entry : stringsLocalizations)

View File

@ -117,6 +117,8 @@ public:
class DLL_LINKAGE TextLocalizationContainer
{
protected:
static std::recursive_mutex globalTextMutex;
struct StringState
{
/// Human-readable string that was added on registration
@ -153,6 +155,9 @@ protected:
std::string getModLanguage(const std::string & modContext);
// returns true if identifier with such name was registered, even if not translated to current language
bool identifierExists(const TextIdentifier & UID) const;
public:
/// validates translation of specified language for specified mod
/// returns true if localization is valid and complete
@ -163,9 +168,6 @@ public:
/// Any entries loaded by this will have priority over texts registered normally
void loadTranslationOverrides(const std::string & language, const std::string & modContext, JsonNode const & file);
// returns true if identifier with such name was registered, even if not translated to current language
bool identifierExists(const TextIdentifier & UID) const;
/// add selected string to internal storage
void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized, const std::string & language);
@ -196,6 +198,8 @@ public:
template <typename Handler>
void serialize(Handler & h)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::string key;
auto sz = stringsLocalizations.size();
h & sz;