[Zone🅱️uilder]: Port over json parsing of loc strings (#621)

This commit is contained in:
Edo 2022-12-04 00:15:46 +00:00 committed by GitHub
parent 5eec7687be
commit 72460032ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 3 deletions

View File

@ -81,4 +81,51 @@ namespace Assets
builder->addRawAsset(type, entry); 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<Game::LocalizeEntry*> assets;
try
{
for (const auto& [key, value] : localize.items())
{
const auto valueStr = value.get<std::string>();
auto* entry = builder->getAllocator()->allocate<Game::LocalizeEntry>();
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);
}
}
} }

View File

@ -11,5 +11,6 @@ namespace Assets
void save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; 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 ParseLocalizedStringsFile(Components::ZoneBuilder::Zone* builder, const std::string& name, const std::string& filename);
static void ParseLocalizedStringsJson(Components::ZoneBuilder::Zone* builder, Components::FileSystem::File& file);
}; };
} }

View File

@ -13,6 +13,11 @@ namespace Components
virtual bool exists() = 0; virtual bool exists() = 0;
virtual std::string getName() = 0; virtual std::string getName() = 0;
virtual std::string& getBuffer() = 0; virtual std::string& getBuffer() = 0;
virtual explicit operator bool()
{
return this->exists();
}
}; };
class File : public AbstractFile class File : public AbstractFile

View File

@ -158,12 +158,17 @@ namespace Components
if (this->dataMap.getElementAt(i, 0) == "localize"s) if (this->dataMap.getElementAt(i, 0) == "localize"s)
{ {
const auto filename = this->dataMap.getElementAt(i, 1); const auto filename = this->dataMap.getElementAt(i, 1);
FileSystem::File file(std::format("localizedstrings/{}.str", filename)); if (FileSystem::File file = std::format("localizedstrings/{}.str", filename))
if (file.exists())
{ {
Assets::ILocalizeEntry::ParseLocalizedStringsFile(this, filename, file.getName()); Assets::ILocalizeEntry::ParseLocalizedStringsFile(this, filename, file.getName());
continue; continue;
} }
if (FileSystem::File file = std::format("localizedstrings/{}.json", filename))
{
Assets::ILocalizeEntry::ParseLocalizedStringsJson(this, file);
continue;
}
} }
if (this->dataMap.getColumns(i) > 2) if (this->dataMap.getColumns(i) > 2)

View File

@ -21,7 +21,7 @@ namespace Utils
char* Memory::DuplicateString(const std::string& string) char* Memory::DuplicateString(const std::string& string)
{ {
auto* newString = Memory::AllocateArray<char>(string.size() + 1); auto* newString = AllocateArray<char>(string.size() + 1);
std::memcpy(newString, string.data(), string.size()); std::memcpy(newString, string.data(), string.size());
return newString; return newString;
} }