Skip to content

Commit

Permalink
chore: WIP refactor IW4 asset loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Laupetin committed Dec 25, 2024
1 parent e1cd546 commit 8cc4147
Show file tree
Hide file tree
Showing 128 changed files with 2,070 additions and 2,773 deletions.
19 changes: 18 additions & 1 deletion src/Linker/ZoneCreation/ZoneCreator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ZoneCreator.h"

#include "AssetLoading/AssetLoadingContext.h"
#include "Gdt/GdtLookup.h"
#include "IObjCompiler.h"
#include "IObjLoader.h"

Expand Down Expand Up @@ -45,6 +46,19 @@ namespace

namespace zone_creator
{
void InitLookup(const ZoneCreationContext& context, GdtLookup& lookup)
{
std::vector<const Gdt*> gdtFiles;
gdtFiles.reserve(context.m_gdt_files.size());

for (const auto& gdt : context.m_gdt_files)
{
gdtFiles.emplace_back(gdt.get());
}

lookup.Initialize(gdtFiles);
}

std::unique_ptr<Zone> CreateZoneForDefinition(GameId gameId, ZoneCreationContext& context)
{
auto zone = CreateZone(context, gameId);
Expand All @@ -53,12 +67,15 @@ namespace zone_creator
IgnoreReferencesFromAssets(context);
IgnoredAssetLookup ignoredAssetLookup(context.m_ignored_assets);

GdtLookup lookup;
InitLookup(context, lookup);

const auto* objCompiler = IObjCompiler::GetObjCompilerForGame(gameId);
const auto* objLoader = IObjLoader::GetObjLoaderForGame(gameId);

AssetCreatorCollection creatorCollection(*zone);
objCompiler->ConfigureCreatorCollection(creatorCollection, *zone, *context.m_definition);
objLoader->ConfigureCreatorCollection(creatorCollection, *zone, *context.m_asset_search_path);
objLoader->ConfigureCreatorCollection(creatorCollection, *zone, *context.m_asset_search_path, gdtLookup);

AssetCreationContext creationContext(zone.get(), &creatorCollection, &ignoredAssetLookup);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "Game/IW4/IW4.h"

namespace IW4
Expand Down
3 changes: 3 additions & 0 deletions src/ObjLoading/Asset/AssetCreationContext.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "AssetCreationContext.h"

#include <cassert>
#include <format>
#include <iostream>

Expand Down Expand Up @@ -51,6 +52,8 @@ void GenericAssetRegistration::AddIndirectAssetReference(IndirectAssetReference

std::unique_ptr<XAssetInfoGeneric> GenericAssetRegistration::CreateXAssetInfo()
{
assert(m_asset);

std::vector<XAssetInfoGeneric*> dependencies(m_dependencies.begin(), m_dependencies.end());
std::vector<scr_string_t> scriptStrings(m_used_script_strings.begin(), m_used_script_strings.end());
std::vector<IndirectAssetReference> indirectAssetReferences(m_indirect_asset_references.begin(), m_indirect_asset_references.end());
Expand Down
4 changes: 2 additions & 2 deletions src/ObjLoading/Asset/AssetCreationContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AssetCreationContext
{
static_assert(std::is_base_of_v<IAssetBase, AssetType>);

return static_cast<XAssetInfo<typename AssetType::Type>*>(LoadDependencyInternal(AssetType::EnumEntry, assetName));
return static_cast<XAssetInfo<typename AssetType::Type>*>(LoadDependencyGeneric(AssetType::EnumEntry, assetName));
}

XAssetInfoGeneric* LoadDependencyGeneric(asset_type_t assetType, const std::string& assetName);
Expand All @@ -76,7 +76,7 @@ class AssetCreationContext
return dynamic_cast<T*>(foundEntry->second.get());

auto newState = std::make_unique<T>();
newState->SetZone(&m_zone);
newState->SetZone(m_zone);
auto* newStatePtr = newState.get();
m_zone_asset_loader_states.emplace(std::make_pair<std::type_index, std::unique_ptr<IZoneAssetLoaderState>>(typeid(T), std::move(newState)));
return newStatePtr;
Expand Down
12 changes: 11 additions & 1 deletion src/ObjLoading/Asset/AssetRegistration.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GenericAssetRegistration

std::unique_ptr<XAssetInfoGeneric> CreateXAssetInfo();

private:
protected:
asset_type_t m_type;
std::string m_name;
void* m_asset;
Expand All @@ -39,6 +39,11 @@ template<typename AssetType> class AssetRegistration : public GenericAssetRegist
static_assert(std::is_base_of_v<IAssetBase, AssetType>);

public:
AssetRegistration(std::string assetName)
: GenericAssetRegistration(AssetType::EnumEntry, std::move(assetName), nullptr)
{
}

AssetRegistration(std::string assetName, typename AssetType::Type* asset)
: GenericAssetRegistration(AssetType::EnumEntry, std::move(assetName), asset)
{
Expand All @@ -48,4 +53,9 @@ template<typename AssetType> class AssetRegistration : public GenericAssetRegist
AssetRegistration(AssetRegistration&& other) = default;
AssetRegistration& operator=(const AssetRegistration& other) = delete;
AssetRegistration& operator=(AssetRegistration&& other) noexcept = default;

void SetAsset(typename AssetType::Type* asset)
{
m_asset = asset;
}
};
45 changes: 1 addition & 44 deletions src/ObjLoading/AssetLoading/AssetLoadingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,6 @@

AssetLoadingContext::AssetLoadingContext(Zone& zone, ISearchPath& rawSearchPath, std::vector<Gdt*> gdtFiles)
: m_zone(zone),
m_raw_search_path(rawSearchPath),
m_gdt_files(std::move(gdtFiles))
m_raw_search_path(rawSearchPath)
{
BuildGdtEntryCache();
}

void AssetLoadingContext::BuildGdtEntryCache()
{
for (const auto* gdt : m_gdt_files)
{
for (const auto& entry : gdt->m_entries)
{
auto gdfMapEntry = m_entries_by_gdf_and_by_name.find(entry->m_gdf_name);
if (gdfMapEntry == m_entries_by_gdf_and_by_name.end())
{
std::unordered_map<std::string, GdtEntry*> entryMap;
entryMap.emplace(std::make_pair(entry->m_name, entry.get()));
m_entries_by_gdf_and_by_name.emplace(std::make_pair(entry->m_gdf_name, std::move(entryMap)));
}
else
{
auto entryMapEntry = gdfMapEntry->second.find(entry->m_name);

if (entryMapEntry == gdfMapEntry->second.end())
gdfMapEntry->second.emplace(std::make_pair(entry->m_name, entry.get()));
else
entryMapEntry->second = entry.get();
}
}
}
}

GdtEntry* AssetLoadingContext::GetGdtEntryByGdfAndName(const std::string& gdfName, const std::string& entryName)
{
const auto foundGdtMap = m_entries_by_gdf_and_by_name.find(gdfName);

if (foundGdtMap == m_entries_by_gdf_and_by_name.end())
return nullptr;

const auto foundGdtEntry = foundGdtMap->second.find(entryName);

if (foundGdtEntry == foundGdtMap->second.end())
return nullptr;

return foundGdtEntry->second;
}
7 changes: 1 addition & 6 deletions src/ObjLoading/AssetLoading/AssetLoadingContext.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "IGdtQueryable.h"
#include "Gdt/IGdtQueryable.h"
#include "IZoneAssetLoaderState.h"
#include "Obj/Gdt/Gdt.h"
#include "SearchPath/ISearchPath.h"
Expand Down Expand Up @@ -32,15 +32,10 @@ class AssetLoadingContext final : public IGdtQueryable
return newStatePtr;
}

private:
void BuildGdtEntryCache();

public:
Zone& m_zone;
ISearchPath& m_raw_search_path;
const std::vector<Gdt*> m_gdt_files;
std::unordered_map<std::string, asset_type_t> m_ignored_asset_map;

std::unordered_map<std::string, std::unordered_map<std::string, GdtEntry*>> m_entries_by_gdf_and_by_name;
std::unordered_map<std::type_index, std::unique_ptr<IZoneAssetLoaderState>> m_zone_asset_loader_states;
};
3 changes: 2 additions & 1 deletion src/ObjLoading/AssetLoading/IAssetLoader.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Gdt/IGdtQueryable.h"
#include "IAssetLoadingManager.h"
#include "IGdtQueryable.h"
#include "SearchPath/ISearchPath.h"
#include "Utils/ClassUtils.h"
#include "Zone/ZoneTypes.h"
Expand Down
1 change: 0 additions & 1 deletion src/ObjLoading/Game/IW3/RawFile/AssetLoaderRawFileIW3.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "AssetLoaderRawFileIW3.h"

#include "Game/IW3/IW3.h"
#include "Pool/GlobalAssetPool.h"

#include <cstring>

Expand Down
17 changes: 0 additions & 17 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderAddonMapEnts.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderAddonMapEnts.h

This file was deleted.

27 changes: 0 additions & 27 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderClipMap.cpp

This file was deleted.

25 changes: 0 additions & 25 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderClipMap.h

This file was deleted.

17 changes: 0 additions & 17 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderComWorld.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderComWorld.h

This file was deleted.

17 changes: 0 additions & 17 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderFont.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderFont.h

This file was deleted.

17 changes: 0 additions & 17 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderFx.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderFx.h

This file was deleted.

Loading

0 comments on commit 8cc4147

Please sign in to comment.