Reduced duplicated code between localize asset loaders

This commit is contained in:
Jan
2023-12-31 13:32:38 +01:00
parent 4eabf98712
commit 7c50dd84a7
11 changed files with 126 additions and 108 deletions

View File

@ -1,9 +1,6 @@
#include "AssetLoaderLocalizeEntry.h"
#include "Localize/LocalizeCommon.h"
#include "Localize/Parsing/LocalizeFileReader.h"
#include <sstream>
#include "Localize/LocalizeCommonAssetLoader.h"
using namespace IW4;
@ -25,29 +22,15 @@ bool AssetLoaderLocalizeEntry::CanLoadFromRaw() const
bool AssetLoaderLocalizeEntry::LoadFromRaw(
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
{
std::string fileName;
{
std::ostringstream str;
str << LocalizeCommon::GetNameOfLanguage(zone->m_language) << "/localizedstrings/" << assetName << ".str";
fileName = str.str();
}
LocalizeCommonAssetLoader commonLoader(
[memory, manager](const CommonLocalizeEntry& entry)
{
auto* localizeEntry = memory->Create<LocalizeEntry>();
localizeEntry->name = memory->Dup(entry.m_key.c_str());
localizeEntry->value = memory->Dup(entry.m_value.c_str());
const auto file = searchPath->Open(fileName);
if (!file.IsOpen())
return false;
manager->AddAsset(ASSET_TYPE_LOCALIZE_ENTRY, entry.m_key, localizeEntry);
});
auto* zoneState = manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<LocalizeReadingZoneState>();
LocalizeFileReader reader(*file.m_stream, assetName, zone->m_language, zoneState);
const auto localizeEntries = reader.ReadLocalizeFile();
for (const auto& [key, value] : localizeEntries)
{
auto* localizeEntry = memory->Create<LocalizeEntry>();
localizeEntry->name = memory->Dup(key.c_str());
localizeEntry->value = memory->Dup(value.c_str());
manager->AddAsset(ASSET_TYPE_LOCALIZE_ENTRY, key, localizeEntry);
}
return true;
return commonLoader.LoadLocalizeAsset(assetName, searchPath, manager, zone);
}

View File

@ -1,4 +1,5 @@
#pragma once
#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "Game/IW4/IW4.h"