2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-09-05 16:27:27 +00:00

chore: implement obj loading skeleton with localize asset

This commit is contained in:
Jan
2024-12-23 22:55:58 +01:00
parent 673db0592f
commit c524cb007a
64 changed files with 1238 additions and 1038 deletions

View File

@@ -0,0 +1,52 @@
#include "CommonLocalizeLoader.h"
#include "Localize/LocalizeCommon.h"
#include "Localize/Parsing/LocalizeFileReader.h"
#include <format>
CommonLocalizeLoader::CommonLocalizeLoader(ISearchPath& searchPath, Zone& zone)
: m_search_path(searchPath),
m_zone(zone)
{
}
std::string CommonLocalizeLoader::GetFileName(const std::string& assetName) const
{
return std::format("{}/localizedstrings/{}.str", LocalizeCommon::GetNameOfLanguage(m_zone.m_language), assetName);
}
AssetCreationResult CommonLocalizeLoader::CreateLocalizeAsset(const std::string& assetName, AssetCreationContext& context)
{
std::string fileName = GetFileName(assetName);
const auto file = m_search_path.Open(fileName);
if (!file.IsOpen())
return AssetCreationResult::NoAction();
LocalizeFileReader reader(*file.m_stream, assetName, m_zone.m_language, *this);
std::vector<CommonLocalizeEntry> localizeEntries;
if (!reader.ReadLocalizeFile(localizeEntries))
return AssetCreationResult::Failure();
auto lastResult = AssetCreationResult::Failure();
for (const auto& entry : localizeEntries)
{
lastResult = CreateAssetFromCommonAsset(entry, context);
if (!lastResult.HasBeenSuccessful())
return lastResult;
}
return lastResult;
}
bool CommonLocalizeLoader::CheckLocalizeEntryForDuplicates(const std::string& key)
{
const auto existingEntry = m_keys.find(key);
if (existingEntry != m_keys.end())
return false;
m_keys.emplace(key);
return true;
}

View File

@@ -0,0 +1,32 @@
#pragma once
#include "Asset/IAssetCreator.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "Localize/CommonLocalizeEntry.h"
#include "Localize/Parsing/ILocalizeFileDuplicationChecker.h"
#include "SearchPath/ISearchPath.h"
#include "Zone/Zone.h"
#include <string>
#include <unordered_set>
class CommonLocalizeLoader : ILocalizeFileDuplicationChecker
{
public:
CommonLocalizeLoader(ISearchPath& searchPath, Zone& zone);
AssetCreationResult CreateLocalizeAsset(const std::string& assetName, AssetCreationContext& context);
protected:
virtual AssetCreationResult CreateAssetFromCommonAsset(const CommonLocalizeEntry& localizeEntry, AssetCreationContext& context) = 0;
private:
std::string GetFileName(const std::string& assetName) const;
bool CheckLocalizeEntryForDuplicates(const std::string& key) override;
ISearchPath& m_search_path;
Zone& m_zone;
std::unordered_set<std::string> m_keys;
};

View File

@@ -1,42 +0,0 @@
#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;
}

View File

@@ -1,22 +0,0 @@
#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;
};

View File

@@ -1,11 +0,0 @@
#include "LocalizeReadingZoneState.h"
bool LocalizeReadingZoneState::DoLocalizeEntryDuplicateCheck(const std::string& key)
{
const auto existingEntry = m_keys.find(key);
if (existingEntry != m_keys.end())
return false;
m_keys.emplace(key);
return true;
}

View File

@@ -1,13 +1,12 @@
#pragma once
#include "AssetLoading/IZoneAssetLoaderState.h"
#include <string>
#include <unordered_set>
class LocalizeReadingZoneState final : public IZoneAssetLoaderState
class ILocalizeFileDuplicationChecker
{
public:
virtual ~ILocalizeFileDuplicationChecker() = default;
/**
* Checks whether a localize key was already added.
* Inserts key if it was not added yet.
@@ -15,8 +14,5 @@ public:
* \param key The key to check
* \returns \c true if key was not duplicated yet, \c false otherwise
*/
bool DoLocalizeEntryDuplicateCheck(const std::string& key);
private:
std::unordered_set<std::string> m_keys;
virtual bool CheckLocalizeEntryForDuplicates(const std::string& key) = 0;
};

View File

@@ -8,8 +8,8 @@
#include "Sequence/SequenceLocalizeFileReference.h"
#include "Sequence/SequenceLocalizeFileVersion.h"
LocalizeFileParser::LocalizeFileParser(SimpleLexer* lexer, GameLanguage language, LocalizeReadingZoneState* zoneState)
: AbstractParser(lexer, std::make_unique<LocalizeFileParserState>(language, zoneState))
LocalizeFileParser::LocalizeFileParser(SimpleLexer* lexer, GameLanguage language, ILocalizeFileDuplicationChecker& duplicationChecker)
: AbstractParser(lexer, std::make_unique<LocalizeFileParserState>(language, duplicationChecker))
{
}

View File

@@ -12,6 +12,6 @@ protected:
const std::vector<sequence_t*>& GetTestsForState() override;
public:
LocalizeFileParser(SimpleLexer* lexer, GameLanguage language, LocalizeReadingZoneState* zoneState);
LocalizeFileParser(SimpleLexer* lexer, GameLanguage language, ILocalizeFileDuplicationChecker& duplicationChecker);
std::vector<CommonLocalizeEntry> GetParsedValues();
};

View File

@@ -3,10 +3,10 @@
#include "Localize/LocalizeCommon.h"
#include "Utils/StringUtils.h"
LocalizeFileParserState::LocalizeFileParserState(const GameLanguage language, LocalizeReadingZoneState* zoneState)
LocalizeFileParserState::LocalizeFileParserState(const GameLanguage language, ILocalizeFileDuplicationChecker& duplicationChecker)
: m_end(false),
m_language(language),
m_zone_state(zoneState)
m_duplication_checker(duplicationChecker)
{
m_language_name_caps = LocalizeCommon::GetNameOfLanguage(m_language);
utils::MakeStringUpperCase(m_language_name_caps);

View File

@@ -1,8 +1,8 @@
#pragma once
#include "Game/GameLanguage.h"
#include "ILocalizeFileDuplicationChecker.h"
#include "Localize/CommonLocalizeEntry.h"
#include "Localize/LocalizeReadingZoneState.h"
#include <unordered_set>
#include <vector>
@@ -10,16 +10,16 @@
class LocalizeFileParserState
{
public:
LocalizeFileParserState(GameLanguage language, ILocalizeFileDuplicationChecker& duplicationChecker);
bool m_end;
std::vector<CommonLocalizeEntry> m_entries;
GameLanguage m_language;
LocalizeReadingZoneState* m_zone_state;
ILocalizeFileDuplicationChecker& m_duplication_checker;
std::string m_language_name_caps;
std::string m_current_reference;
std::unordered_set<std::string> m_current_reference_languages;
LocalizeFileParserState(GameLanguage language, LocalizeReadingZoneState* zoneState);
};

View File

@@ -4,11 +4,11 @@
#include "Parsing/Impl/CommentRemovingStreamProxy.h"
#include "Parsing/Impl/ParserSingleInputStream.h"
LocalizeFileReader::LocalizeFileReader(std::istream& stream, std::string fileName, GameLanguage language, LocalizeReadingZoneState* zoneState)
LocalizeFileReader::LocalizeFileReader(std::istream& stream, std::string fileName, GameLanguage language, ILocalizeFileDuplicationChecker& duplicationChecker)
: m_file_name(std::move(fileName)),
m_stream(nullptr),
m_language(language),
m_zone_state(zoneState)
m_duplication_checker(duplicationChecker)
{
OpenBaseStream(stream);
SetupStreamProxies();
@@ -38,7 +38,7 @@ bool LocalizeFileReader::ReadLocalizeFile(std::vector<CommonLocalizeEntry>& entr
lexerConfig.m_read_floating_point_numbers = false;
const auto lexer = std::make_unique<SimpleLexer>(m_stream, std::move(lexerConfig));
const auto parser = std::make_unique<LocalizeFileParser>(lexer.get(), m_language, m_zone_state);
const auto parser = std::make_unique<LocalizeFileParser>(lexer.get(), m_language, m_duplication_checker);
if (parser->Parse())
{

View File

@@ -1,8 +1,8 @@
#pragma once
#include "Game/GameLanguage.h"
#include "ILocalizeFileDuplicationChecker.h"
#include "Localize/CommonLocalizeEntry.h"
#include "Localize/LocalizeReadingZoneState.h"
#include "Parsing/IParserLineStream.h"
#include <map>
@@ -16,13 +16,13 @@ class LocalizeFileReader
IParserLineStream* m_stream;
std::vector<std::unique_ptr<IParserLineStream>> m_open_streams;
GameLanguage m_language;
LocalizeReadingZoneState* m_zone_state;
ILocalizeFileDuplicationChecker& m_duplication_checker;
bool OpenBaseStream(std::istream& stream);
void SetupStreamProxies();
public:
LocalizeFileReader(std::istream& stream, std::string fileName, GameLanguage language, LocalizeReadingZoneState* zoneState);
LocalizeFileReader(std::istream& stream, std::string fileName, GameLanguage language, ILocalizeFileDuplicationChecker& duplicationChecker);
bool ReadLocalizeFile(std::vector<CommonLocalizeEntry>& entries);
};

View File

@@ -33,7 +33,7 @@ void SequenceLocalizeFileLanguageValue::ProcessMatch(LocalizeFileParserState* st
if (langName == state->m_language_name_caps)
{
const auto& currentReference = state->m_current_reference;
if (!state->m_zone_state->DoLocalizeEntryDuplicateCheck(currentReference))
if (!state->m_duplication_checker.CheckLocalizeEntryForDuplicates(currentReference))
{
std::cout << "Localize: a value for reference \"" << currentReference << "\" was already defined\n";
}