2022-02-27 07:53:44 -05:00
|
|
|
#include <STDInclude.hpp>
|
2022-06-22 11:36:06 -04:00
|
|
|
#include "ILocalizeEntry.hpp"
|
2016-01-12 13:08:26 -05:00
|
|
|
|
|
|
|
namespace Assets
|
|
|
|
{
|
2022-11-08 20:31:46 -05:00
|
|
|
void ILocalizeEntry::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
|
|
|
|
{
|
|
|
|
const auto path = "localizedstrings/" + name;
|
|
|
|
|
|
|
|
Components::FileSystem::File rawFile(path);
|
|
|
|
if (!rawFile.exists())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Components::Logger::Debug("Parsing localized string \"{}\"...", path);
|
|
|
|
|
|
|
|
auto* asset = builder->getAllocator()->allocate<Game::LocalizeEntry>();
|
|
|
|
if (!asset)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
asset->name = builder->getAllocator()->duplicateString(name);
|
|
|
|
asset->value = builder->getAllocator()->duplicateString(rawFile.getBuffer());
|
|
|
|
|
|
|
|
header->localize = asset;
|
|
|
|
}
|
|
|
|
|
2016-12-21 11:26:16 -05:00
|
|
|
void ILocalizeEntry::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
|
2016-01-12 13:08:26 -05:00
|
|
|
{
|
2016-12-21 11:26:16 -05:00
|
|
|
AssertSize(Game::LocalizeEntry, 8);
|
2016-01-22 05:59:43 -05:00
|
|
|
|
2022-11-08 20:31:46 -05:00
|
|
|
auto* buffer = builder->getBuffer();
|
|
|
|
auto* asset = header.localize;
|
|
|
|
auto* dest = buffer->dest<Game::LocalizeEntry>();
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->save(asset);
|
2016-01-12 13:08:26 -05:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL);
|
2016-01-12 13:08:26 -05:00
|
|
|
|
|
|
|
if (asset->value)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->saveString(asset->value);
|
2016-06-10 07:52:55 -04:00
|
|
|
Utils::Stream::ClearPointer(&dest->value);
|
2016-01-12 13:08:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (asset->name)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->saveString(builder->getAssetName(this->getType(), asset->name));
|
2016-06-10 07:52:55 -04:00
|
|
|
Utils::Stream::ClearPointer(&dest->name);
|
2016-01-12 13:08:26 -05:00
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->popBlock();
|
2016-01-12 13:08:26 -05:00
|
|
|
}
|
2022-11-16 12:25:21 -05:00
|
|
|
|
|
|
|
void ILocalizeEntry::ParseLocalizedStringsFile(Components::ZoneBuilder::Zone* builder, const std::string& name, const std::string& filename)
|
|
|
|
{
|
|
|
|
std::vector<Game::LocalizeEntry*> assets;
|
|
|
|
const auto _0 = gsl::finally([]
|
|
|
|
{
|
|
|
|
Components::Localization::ParseOutput(nullptr);
|
|
|
|
Components::Localization::PrefixOverride = {};
|
|
|
|
});
|
|
|
|
|
|
|
|
Components::Localization::PrefixOverride = Utils::String::ToUpper(name) + "_";
|
|
|
|
Components::Localization::ParseOutput([&assets](Game::LocalizeEntry* asset)
|
|
|
|
{
|
|
|
|
assets.push_back(asset);
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto* psLoadedFile = Game::SE_Load(filename.data(), false);
|
|
|
|
if (psLoadedFile)
|
|
|
|
{
|
|
|
|
Game::Com_PrintError(Game::CON_CHANNEL_SYSTEM, "^1Localization ERROR: %s\n", psLoadedFile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto type = Game::DB_GetXAssetNameType("localize");
|
|
|
|
for (const auto& entry : assets)
|
|
|
|
{
|
|
|
|
builder->addRawAsset(type, entry);
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 19:15:46 -05:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2016-01-12 13:08:26 -05:00
|
|
|
}
|