Skip to content

Commit

Permalink
Merge pull request #77 from Laupetin/refactor/localize-remove-duplica…
Browse files Browse the repository at this point in the history
…tion

Remove duplicated code in localize asset loading
  • Loading branch information
Laupetin authored Dec 31, 2023
2 parents 4eabf98 + 9d0beaa commit 07266bb
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 124 deletions.
9 changes: 9 additions & 0 deletions src/ObjCommon/Localize/CommonLocalizeEntry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "CommonLocalizeEntry.h"

CommonLocalizeEntry::CommonLocalizeEntry() = default;

CommonLocalizeEntry::CommonLocalizeEntry(std::string key, std::string value)
: m_key(std::move(key)),
m_value(std::move(value))
{
}
12 changes: 12 additions & 0 deletions src/ObjCommon/Localize/CommonLocalizeEntry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <string>

class CommonLocalizeEntry
{
public:
std::string m_key;
std::string m_value;

CommonLocalizeEntry();
CommonLocalizeEntry(std::string key, std::string value);
};
39 changes: 11 additions & 28 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderLocalizeEntry.cpp
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
}

const auto file = searchPath->Open(fileName);
if (!file.IsOpen())
return false;

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);
}
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());

return true;
manager->AddAsset(ASSET_TYPE_LOCALIZE_ENTRY, entry.m_key, localizeEntry);
});

return commonLoader.LoadLocalizeAsset(assetName, searchPath, manager, zone);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "Game/IW4/IW4.h"
Expand Down
39 changes: 11 additions & 28 deletions src/ObjLoading/Game/IW5/AssetLoaders/AssetLoaderLocalizeEntry.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "AssetLoaderLocalizeEntry.h"

#include "Localize/LocalizeCommon.h"
#include "Localize/Parsing/LocalizeFileReader.h"

#include <sstream>
#include "Localize/LocalizeCommonAssetLoader.h"

using namespace IW5;

Expand All @@ -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();
}

const auto file = searchPath->Open(fileName);
if (!file.IsOpen())
return false;

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);
}
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());

return true;
manager->AddAsset(ASSET_TYPE_LOCALIZE_ENTRY, entry.m_key, localizeEntry);
});

return commonLoader.LoadLocalizeAsset(assetName, searchPath, manager, zone);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "Game/IW5/IW5.h"
Expand Down
39 changes: 11 additions & 28 deletions src/ObjLoading/Game/T5/AssetLoaders/AssetLoaderLocalizeEntry.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "AssetLoaderLocalizeEntry.h"

#include "Localize/LocalizeCommon.h"
#include "Localize/Parsing/LocalizeFileReader.h"

#include <sstream>
#include "Localize/LocalizeCommonAssetLoader.h"

using namespace T5;

Expand All @@ -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();
}

const auto file = searchPath->Open(fileName);
if (!file.IsOpen())
return false;

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);
}
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());

return true;
manager->AddAsset(ASSET_TYPE_LOCALIZE_ENTRY, entry.m_key, localizeEntry);
});

return commonLoader.LoadLocalizeAsset(assetName, searchPath, manager, zone);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "Game/T5/T5.h"
Expand Down
39 changes: 11 additions & 28 deletions src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderLocalizeEntry.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "AssetLoaderLocalizeEntry.h"

#include "Localize/LocalizeCommon.h"
#include "Localize/Parsing/LocalizeFileReader.h"

#include <sstream>
#include "Localize/LocalizeCommonAssetLoader.h"

using namespace T6;

Expand All @@ -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();
}

const auto file = searchPath->Open(fileName);
if (!file.IsOpen())
return false;

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);
}
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());

return true;
manager->AddAsset(ASSET_TYPE_LOCALIZE_ENTRY, entry.m_key, localizeEntry);
});

return commonLoader.LoadLocalizeAsset(assetName, searchPath, manager, zone);
}
42 changes: 42 additions & 0 deletions src/ObjLoading/Localize/LocalizeCommonAssetLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "LocalizeCommonAssetLoader.h"

#include "Localize/LocalizeCommon.h"
#include "Localize/LocalizeReadingZoneState.h"
#include "Localize/Parsing/LocalizeFileReader.h"

#include <sstream>

LocalizeCommonAssetLoader::LocalizeCommonAssetLoader(std::function<void(const CommonLocalizeEntry&)> entryCallback)
: m_entry_callback(std::move(entryCallback))
{
}

std::string LocalizeCommonAssetLoader::GetFileName(const std::string& assetName, Zone* zone) const
{
std::ostringstream ss;
ss << LocalizeCommon::GetNameOfLanguage(zone->m_language) << "/localizedstrings/" << assetName << ".str";
return ss.str();
}

bool LocalizeCommonAssetLoader::LoadLocalizeAsset(const std::string& assetName, ISearchPath* searchPath, IAssetLoadingManager* manager, Zone* zone) const
{
std::string fileName = GetFileName(assetName, zone);

const auto file = searchPath->Open(fileName);
if (!file.IsOpen())
return false;

auto* zoneState = manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<LocalizeReadingZoneState>();
LocalizeFileReader reader(*file.m_stream, assetName, zone->m_language, zoneState);

std::vector<CommonLocalizeEntry> localizeEntries;
if (!reader.ReadLocalizeFile(localizeEntries))
return false;

for (const auto& entry : localizeEntries)
{
m_entry_callback(entry);
}

return true;
}
22 changes: 22 additions & 0 deletions src/ObjLoading/Localize/LocalizeCommonAssetLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "AssetLoading/IAssetLoadingManager.h"
#include "Localize/CommonLocalizeEntry.h"
#include "SearchPath/ISearchPath.h"
#include "Zone/Zone.h"

#include <functional>
#include <string>

class LocalizeCommonAssetLoader
{
public:
explicit LocalizeCommonAssetLoader(std::function<void(const CommonLocalizeEntry&)> entryCallback);

bool LoadLocalizeAsset(const std::string& assetName, ISearchPath* searchPath, IAssetLoadingManager* manager, Zone* zone) const;

private:
std::string GetFileName(const std::string& assetName, Zone* zone) const;

std::function<void(const CommonLocalizeEntry&)> m_entry_callback;
};
2 changes: 1 addition & 1 deletion src/ObjLoading/Localize/Parsing/LocalizeFileParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const std::vector<AbstractParser<SimpleParserValue, LocalizeFileParserState>::se
return !m_state->m_end ? tests : noTests;
}

std::map<std::string, std::string> LocalizeFileParser::GetParsedValues()
std::vector<CommonLocalizeEntry> LocalizeFileParser::GetParsedValues()
{
return std::move(m_state->m_entries);
}
2 changes: 1 addition & 1 deletion src/ObjLoading/Localize/Parsing/LocalizeFileParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class LocalizeFileParser final : public AbstractParser<SimpleParserValue, Locali

public:
LocalizeFileParser(SimpleLexer* lexer, GameLanguage language, LocalizeReadingZoneState* zoneState);
std::map<std::string, std::string> GetParsedValues();
std::vector<CommonLocalizeEntry> GetParsedValues();
};
4 changes: 2 additions & 2 deletions src/ObjLoading/Localize/Parsing/LocalizeFileParserState.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "LocalizeFileParserState.h"

#include "Localize/LocalizeCommon.h"
#include "Utils/StringUtils.h"

LocalizeFileParserState::LocalizeFileParserState(const GameLanguage language, LocalizeReadingZoneState* zoneState)
: m_end(false),
m_language(language),
m_zone_state(zoneState)
{
m_language_name_caps = LocalizeCommon::GetNameOfLanguage(m_language);
for (auto& c : m_language_name_caps)
c = static_cast<char>(toupper(c));
utils::MakeStringUpperCase(m_language_name_caps);
}
5 changes: 3 additions & 2 deletions src/ObjLoading/Localize/Parsing/LocalizeFileParserState.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#pragma once

#include "Game/GameLanguage.h"
#include "Localize/CommonLocalizeEntry.h"
#include "Localize/LocalizeReadingZoneState.h"

#include <map>
#include <unordered_set>
#include <vector>

class LocalizeFileParserState
{
public:
bool m_end;

std::map<std::string, std::string> m_entries;
std::vector<CommonLocalizeEntry> m_entries;

GameLanguage m_language;
LocalizeReadingZoneState* m_zone_state;
Expand Down
11 changes: 7 additions & 4 deletions src/ObjLoading/Localize/Parsing/LocalizeFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void LocalizeFileReader::SetupStreamProxies()
m_stream = m_open_streams.back().get();
}

std::map<std::string, std::string> LocalizeFileReader::ReadLocalizeFile()
bool LocalizeFileReader::ReadLocalizeFile(std::vector<CommonLocalizeEntry>& entries)
{
SimpleLexer::Config lexerConfig;
lexerConfig.m_emit_new_line_tokens = true;
Expand All @@ -41,8 +41,11 @@ std::map<std::string, std::string> LocalizeFileReader::ReadLocalizeFile()
const auto parser = std::make_unique<LocalizeFileParser>(lexer.get(), m_language, m_zone_state);

if (parser->Parse())
return parser->GetParsedValues();
{
entries = parser->GetParsedValues();
return true;
}

std::cout << "Parsing localization file failed!" << std::endl;
return std::map<std::string, std::string>();
std::cerr << "Parsing localization file failed!" << std::endl;
return false;
}
Loading

0 comments on commit 07266bb

Please sign in to comment.