From 72460032aeb34cee479c6c0118ae49c63abdf438 Mon Sep 17 00:00:00 2001 From: Edo Date: Sun, 4 Dec 2022 00:15:46 +0000 Subject: [PATCH] [Zone:b:uilder]: Port over json parsing of loc strings (#621) --- .../AssetInterfaces/ILocalizeEntry.cpp | 47 +++++++++++++++++++ .../AssetInterfaces/ILocalizeEntry.hpp | 1 + src/Components/Modules/FileSystem.hpp | 5 ++ src/Components/Modules/ZoneBuilder.cpp | 9 +++- src/Utils/Memory.cpp | 2 +- 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/ILocalizeEntry.cpp b/src/Components/Modules/AssetInterfaces/ILocalizeEntry.cpp index 5b5eb82a..243c0958 100644 --- a/src/Components/Modules/AssetInterfaces/ILocalizeEntry.cpp +++ b/src/Components/Modules/AssetInterfaces/ILocalizeEntry.cpp @@ -81,4 +81,51 @@ namespace Assets builder->addRawAsset(type, entry); } } + + void ILocalizeEntry::ParseLocalizedStringsJson(Components::ZoneBuilder::Zone* builder, Components::FileSystem::File& file) + { + nlohmann::json localize; + try + { + Components::Logger::Debug("Parsing localized string \"{}\"...", file.getName()); + localize = nlohmann::json::parse(file.getBuffer()); + } + catch (const std::exception& ex) + { + Components::Logger::PrintError(Game::CON_CHANNEL_ERROR, "{}\n", ex.what()); + return; + } + + if (!localize.is_object()) + { + Components::Logger::PrintError(Game::CON_CHANNEL_ERROR, "Localized strings json file '{}' should be an object!", file.getName()); + return; + } + + std::vector assets; + + try + { + for (const auto& [key, value] : localize.items()) + { + const auto valueStr = value.get(); + + auto* entry = builder->getAllocator()->allocate(); + entry->name = builder->getAllocator()->duplicateString(key); + entry->value = builder->getAllocator()->duplicateString(valueStr); + + assets.emplace_back(entry); + } + } + catch (const std::exception& ex) + { + Components::Logger::PrintError(Game::CON_CHANNEL_ERROR, "{}: Localized strings json file '{}' contains invalid data!", ex.what(), file.getName()); + } + + auto type = Game::DB_GetXAssetNameType("localize"); + for (const auto& entry : assets) + { + builder->addRawAsset(type, entry); + } + } } diff --git a/src/Components/Modules/AssetInterfaces/ILocalizeEntry.hpp b/src/Components/Modules/AssetInterfaces/ILocalizeEntry.hpp index b01b9c81..3818e1d4 100644 --- a/src/Components/Modules/AssetInterfaces/ILocalizeEntry.hpp +++ b/src/Components/Modules/AssetInterfaces/ILocalizeEntry.hpp @@ -11,5 +11,6 @@ namespace Assets void save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; static void ParseLocalizedStringsFile(Components::ZoneBuilder::Zone* builder, const std::string& name, const std::string& filename); + static void ParseLocalizedStringsJson(Components::ZoneBuilder::Zone* builder, Components::FileSystem::File& file); }; } diff --git a/src/Components/Modules/FileSystem.hpp b/src/Components/Modules/FileSystem.hpp index 50573b40..bcdbd0e1 100644 --- a/src/Components/Modules/FileSystem.hpp +++ b/src/Components/Modules/FileSystem.hpp @@ -13,6 +13,11 @@ namespace Components virtual bool exists() = 0; virtual std::string getName() = 0; virtual std::string& getBuffer() = 0; + + virtual explicit operator bool() + { + return this->exists(); + } }; class File : public AbstractFile diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index 2ac3d780..00aa122a 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -158,12 +158,17 @@ namespace Components if (this->dataMap.getElementAt(i, 0) == "localize"s) { const auto filename = this->dataMap.getElementAt(i, 1); - FileSystem::File file(std::format("localizedstrings/{}.str", filename)); - if (file.exists()) + if (FileSystem::File file = std::format("localizedstrings/{}.str", filename)) { Assets::ILocalizeEntry::ParseLocalizedStringsFile(this, filename, file.getName()); continue; } + + if (FileSystem::File file = std::format("localizedstrings/{}.json", filename)) + { + Assets::ILocalizeEntry::ParseLocalizedStringsJson(this, file); + continue; + } } if (this->dataMap.getColumns(i) > 2) diff --git a/src/Utils/Memory.cpp b/src/Utils/Memory.cpp index ceab4894..cf26b3ca 100644 --- a/src/Utils/Memory.cpp +++ b/src/Utils/Memory.cpp @@ -21,7 +21,7 @@ namespace Utils char* Memory::DuplicateString(const std::string& string) { - auto* newString = Memory::AllocateArray(string.size() + 1); + auto* newString = AllocateArray(string.size() + 1); std::memcpy(newString, string.data(), string.size()); return newString; }