mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-11 23:38:09 -05:00
Import code from previous AssetBuilder version
This commit is contained in:
67
src/ZoneCommon/Pool/AssetPool.h
Normal file
67
src/ZoneCommon/Pool/AssetPool.h
Normal file
@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include "XAssetInfo.h"
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
template<typename T>
|
||||
class AssetPool
|
||||
{
|
||||
public:
|
||||
std::map<std::string, XAssetInfo<T>*> m_asset_lookup;
|
||||
|
||||
class Iterator
|
||||
{
|
||||
typename std::map<std::string, XAssetInfo<T>*>::iterator m_iterator;
|
||||
|
||||
public:
|
||||
explicit Iterator(typename std::map<std::string, XAssetInfo<T>*>::iterator i)
|
||||
{
|
||||
m_iterator = i;
|
||||
}
|
||||
|
||||
bool operator!=(Iterator rhs)
|
||||
{
|
||||
return m_iterator != rhs.m_iterator;
|
||||
}
|
||||
|
||||
XAssetInfo<T>* operator*()
|
||||
{
|
||||
return m_iterator.operator*().second;
|
||||
}
|
||||
|
||||
void operator++()
|
||||
{
|
||||
++m_iterator;
|
||||
}
|
||||
};
|
||||
|
||||
AssetPool()
|
||||
{
|
||||
m_asset_lookup = std::map<std::string, XAssetInfo<T>*>();
|
||||
}
|
||||
|
||||
virtual ~AssetPool() = default;
|
||||
|
||||
virtual XAssetInfo<T>* AddAsset(std::string name, T* asset, std::vector<std::string>& scriptStrings, std::vector<XAssetDependency>& dependencies) = 0;
|
||||
|
||||
XAssetInfo<T>* GetAsset(const std::string& name)
|
||||
{
|
||||
auto foundAsset = m_asset_lookup.find(name);
|
||||
|
||||
if(foundAsset == m_asset_lookup.end())
|
||||
return nullptr;
|
||||
|
||||
return foundAsset->second;
|
||||
}
|
||||
|
||||
Iterator begin()
|
||||
{
|
||||
return Iterator(m_asset_lookup.begin());
|
||||
}
|
||||
|
||||
Iterator end()
|
||||
{
|
||||
return Iterator(m_asset_lookup.end());
|
||||
}
|
||||
};
|
59
src/ZoneCommon/Pool/AssetPoolDynamic.h
Normal file
59
src/ZoneCommon/Pool/AssetPoolDynamic.h
Normal file
@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include "AssetPool.h"
|
||||
#include "GlobalAssetPool.h"
|
||||
#include "XAssetInfo.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
template <typename T>
|
||||
class AssetPoolDynamic final : public AssetPool<T>
|
||||
{
|
||||
using AssetPool<T>::m_asset_lookup;
|
||||
|
||||
std::vector<XAssetInfo<T>*> m_assets;
|
||||
|
||||
public:
|
||||
explicit AssetPoolDynamic(const int priority)
|
||||
{
|
||||
GlobalAssetPool<T>::LinkAssetPool(this, priority);
|
||||
}
|
||||
|
||||
AssetPoolDynamic(AssetPoolDynamic<T>&) = delete;
|
||||
AssetPoolDynamic(AssetPoolDynamic<T>&&) = delete;
|
||||
AssetPoolDynamic<T>& operator =(AssetPoolDynamic<T>&) = delete;
|
||||
AssetPoolDynamic<T>& operator =(AssetPoolDynamic<T>&&) = default;
|
||||
|
||||
~AssetPoolDynamic() override
|
||||
{
|
||||
GlobalAssetPool<T>::UnlinkAssetPool(this);
|
||||
|
||||
for(auto* entry : m_assets)
|
||||
{
|
||||
delete entry->m_asset;
|
||||
delete entry;
|
||||
}
|
||||
|
||||
m_assets.clear();
|
||||
m_asset_lookup.clear();
|
||||
}
|
||||
|
||||
XAssetInfo<T>* AddAsset(std::string name, T* asset, std::vector<std::string>& scriptStrings, std::vector<XAssetDependency>& dependencies) override
|
||||
{
|
||||
auto* newInfo = new XAssetInfo<T>();
|
||||
newInfo->m_name = std::move(name);
|
||||
newInfo->m_script_strings = std::move(scriptStrings);
|
||||
newInfo->m_dependencies = std::move(dependencies);
|
||||
|
||||
T* newAsset = new T();
|
||||
memcpy(newAsset, asset, sizeof(T));
|
||||
newInfo->m_asset = newAsset;
|
||||
|
||||
m_assets.push_back(newInfo);
|
||||
m_asset_lookup[newInfo->m_name] = newInfo;
|
||||
|
||||
GlobalAssetPool<T>::LinkAsset(this, newInfo);
|
||||
|
||||
return newInfo;
|
||||
}
|
||||
};
|
104
src/ZoneCommon/Pool/AssetPoolStatic.h
Normal file
104
src/ZoneCommon/Pool/AssetPoolStatic.h
Normal file
@ -0,0 +1,104 @@
|
||||
#pragma once
|
||||
|
||||
#include "GlobalAssetPool.h"
|
||||
#include "AssetPool.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
|
||||
template <typename T>
|
||||
class AssetPoolStatic final : public AssetPool<T>
|
||||
{
|
||||
using AssetPool<T>::m_asset_lookup;
|
||||
|
||||
struct AssetPoolEntry
|
||||
{
|
||||
XAssetInfo<T>* m_info;
|
||||
|
||||
union
|
||||
{
|
||||
T m_entry;
|
||||
AssetPoolEntry* m_next;
|
||||
};
|
||||
};
|
||||
|
||||
AssetPoolEntry* m_free;
|
||||
AssetPoolEntry* m_pool;
|
||||
XAssetInfo<T>* m_info_pool;
|
||||
size_t m_capacity;
|
||||
|
||||
public:
|
||||
AssetPoolStatic(const size_t capacity, const int priority)
|
||||
{
|
||||
m_capacity = capacity;
|
||||
|
||||
if (m_capacity > 0)
|
||||
{
|
||||
m_pool = new AssetPoolEntry[m_capacity];
|
||||
m_info_pool = new XAssetInfo<T>[m_capacity];
|
||||
|
||||
for(size_t i = 0; i < m_capacity - 1; i++)
|
||||
{
|
||||
m_pool[i].m_info = &m_info_pool[i];
|
||||
m_pool[i].m_next = &m_pool[i + 1];
|
||||
}
|
||||
m_pool[m_capacity - 1].m_info = &m_info_pool[m_capacity - 1];
|
||||
m_pool[m_capacity - 1].m_next = nullptr;
|
||||
|
||||
m_free = m_pool;
|
||||
|
||||
GlobalAssetPool<T>::LinkAssetPool(this, priority);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_pool = nullptr;
|
||||
m_free = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
AssetPoolStatic(AssetPoolStatic<T>&) = delete;
|
||||
AssetPoolStatic(AssetPoolStatic<T>&&) = delete;
|
||||
AssetPoolStatic<T>& operator =(AssetPoolStatic<T>&) = delete;
|
||||
AssetPoolStatic<T>& operator =(AssetPoolStatic<T>&&) = default;
|
||||
|
||||
~AssetPoolStatic() override
|
||||
{
|
||||
if(m_capacity > 0)
|
||||
{
|
||||
GlobalAssetPool<T>::UnlinkAssetPool(this);
|
||||
}
|
||||
|
||||
delete[] m_pool;
|
||||
m_pool = nullptr;
|
||||
|
||||
delete[] m_info_pool;
|
||||
m_info_pool = nullptr;
|
||||
|
||||
m_free = nullptr;
|
||||
m_capacity = 0;
|
||||
}
|
||||
|
||||
XAssetInfo<T>* AddAsset(std::string name, T* asset, std::vector<std::string>& scriptStrings, std::vector<XAssetDependency>& dependencies) override
|
||||
{
|
||||
if(m_free == nullptr)
|
||||
{
|
||||
throw std::exception("Could not add asset to static asset pool: capacity exhausted.");
|
||||
}
|
||||
|
||||
AssetPoolEntry* poolSlot = m_free;
|
||||
m_free = m_free->m_next;
|
||||
|
||||
memcpy(&poolSlot->m_entry, asset, sizeof(T));
|
||||
|
||||
poolSlot->m_info->m_name = std::move(name);
|
||||
poolSlot->m_info->m_asset = &poolSlot->m_entry;
|
||||
poolSlot->m_info->m_script_strings = std::move(scriptStrings);
|
||||
poolSlot->m_info->m_dependencies = std::move(dependencies);
|
||||
|
||||
m_asset_lookup[poolSlot->m_info->m_name] = poolSlot->m_info;
|
||||
|
||||
GlobalAssetPool<T>::LinkAsset(this, poolSlot->m_info);
|
||||
|
||||
return poolSlot->m_info;
|
||||
}
|
||||
};
|
2
src/ZoneCommon/Pool/GlobalAssetPool.cpp
Normal file
2
src/ZoneCommon/Pool/GlobalAssetPool.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "GlobalAssetPool.h"
|
||||
|
165
src/ZoneCommon/Pool/GlobalAssetPool.h
Normal file
165
src/ZoneCommon/Pool/GlobalAssetPool.h
Normal file
@ -0,0 +1,165 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
#include "AssetPool.h"
|
||||
|
||||
template<typename T>
|
||||
class GlobalAssetPool
|
||||
{
|
||||
struct LinkedAssetPool
|
||||
{
|
||||
AssetPool<T>* m_asset_pool;
|
||||
int m_priority;
|
||||
};
|
||||
|
||||
struct GameAssetPoolEntry
|
||||
{
|
||||
XAssetInfo<T>* m_asset;
|
||||
bool m_duplicate;
|
||||
LinkedAssetPool* m_asset_pool;
|
||||
};
|
||||
|
||||
static std::vector<LinkedAssetPool*> m_linked_asset_pools;
|
||||
static std::map<std::string, GameAssetPoolEntry> m_assets;
|
||||
|
||||
static void SortLinkedAssetPools()
|
||||
{
|
||||
std::sort(m_linked_asset_pools.begin(), m_linked_asset_pools.end(), [](const LinkedAssetPool* a, const LinkedAssetPool* b) -> bool
|
||||
{
|
||||
return a->m_priority < b->m_priority;
|
||||
});
|
||||
}
|
||||
|
||||
static bool ReplaceAssetPoolEntry(GameAssetPoolEntry& assetEntry)
|
||||
{
|
||||
int occurrences = 0;
|
||||
|
||||
for(auto linkedAssetPool : m_linked_asset_pools)
|
||||
{
|
||||
XAssetInfo<T>* foundAsset = linkedAssetPool->m_asset_pool->GetAsset(assetEntry.m_asset->m_name);
|
||||
|
||||
if(foundAsset != nullptr)
|
||||
{
|
||||
if(++occurrences == 1)
|
||||
{
|
||||
assetEntry.m_asset = foundAsset;
|
||||
assetEntry.m_duplicate = false;
|
||||
assetEntry.m_asset_pool = linkedAssetPool;
|
||||
}
|
||||
else
|
||||
{
|
||||
assetEntry.m_duplicate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return occurrences > 0;
|
||||
}
|
||||
|
||||
static void LinkAsset(LinkedAssetPool* link, XAssetInfo<T>* asset)
|
||||
{
|
||||
std::string assetName = std::string(asset->m_name);
|
||||
|
||||
auto existingAsset = m_assets.find(assetName);
|
||||
|
||||
if(existingAsset == m_assets.end())
|
||||
{
|
||||
GameAssetPoolEntry entry{};
|
||||
entry.m_asset = asset;
|
||||
entry.m_asset_pool = link;
|
||||
entry.m_duplicate = false;
|
||||
|
||||
m_assets[assetName] = entry;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto& existingEntry = existingAsset->second;
|
||||
|
||||
existingEntry.m_duplicate = true;
|
||||
|
||||
if(existingEntry.m_asset_pool->m_priority < link->m_priority)
|
||||
{
|
||||
existingEntry.m_asset_pool = link;
|
||||
existingEntry.m_asset = asset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
static void LinkAssetPool(AssetPool<T>* assetPool, const int priority)
|
||||
{
|
||||
auto* newLink = new LinkedAssetPool();
|
||||
newLink->m_asset_pool = assetPool;
|
||||
newLink->m_priority = priority;
|
||||
|
||||
m_linked_asset_pools.push_back(newLink);
|
||||
SortLinkedAssetPools();
|
||||
|
||||
for(auto asset : *assetPool)
|
||||
{
|
||||
LinkAsset(newLink, asset);
|
||||
}
|
||||
}
|
||||
|
||||
static void LinkAsset(AssetPool<T>* assetPool, XAssetInfo<T>* asset)
|
||||
{
|
||||
LinkedAssetPool* link = nullptr;
|
||||
|
||||
for(auto existingLink : m_linked_asset_pools)
|
||||
{
|
||||
if(existingLink->m_asset_pool == assetPool)
|
||||
{
|
||||
link = existingLink;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert(link != nullptr);
|
||||
if(link == nullptr)
|
||||
return;
|
||||
|
||||
LinkAsset(link, asset);
|
||||
}
|
||||
|
||||
static void UnlinkAssetPool(AssetPool<T>* assetPool)
|
||||
{
|
||||
auto iLinkEntry = m_linked_asset_pools.begin();
|
||||
|
||||
for(; iLinkEntry != m_linked_asset_pools.end(); ++iLinkEntry)
|
||||
{
|
||||
LinkedAssetPool* linkEntry = *iLinkEntry;
|
||||
if(linkEntry->m_asset_pool == assetPool)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert(iLinkEntry != m_linked_asset_pools.end());
|
||||
if(iLinkEntry == m_linked_asset_pools.end())
|
||||
return;
|
||||
|
||||
m_linked_asset_pools.erase(iLinkEntry);
|
||||
|
||||
for(auto iAssetEntry = m_assets.begin(); iAssetEntry != m_assets.end(); ++iAssetEntry)
|
||||
{
|
||||
auto& assetEntry = *iAssetEntry;
|
||||
|
||||
if(assetEntry.second.m_duplicate && ReplaceAssetPoolEntry(assetEntry.second))
|
||||
continue;
|
||||
|
||||
iAssetEntry = m_assets.erase(iAssetEntry);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
std::vector<typename GlobalAssetPool<T>::LinkedAssetPool*> GlobalAssetPool<T>::m_linked_asset_pools = std::vector<LinkedAssetPool*>();
|
||||
|
||||
template<typename T>
|
||||
std::map<std::string, typename GlobalAssetPool<T>::GameAssetPoolEntry> GlobalAssetPool<T>::m_assets = std::map<std::string, GameAssetPoolEntry>();
|
14
src/ZoneCommon/Pool/IZoneAssetPools.h
Normal file
14
src/ZoneCommon/Pool/IZoneAssetPools.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "XAssetInfo.h"
|
||||
#include "Zone/ZoneTypes.h"
|
||||
|
||||
class IZoneAssetPools
|
||||
{
|
||||
public:
|
||||
virtual ~IZoneAssetPools() = default;
|
||||
|
||||
virtual void* AddAsset(asset_type_t type, std::string name, void* asset, std::vector<std::string>& scriptStrings, std::vector<XAssetDependency>& dependencies) = 0;
|
||||
virtual void InitPoolStatic(asset_type_t type, size_t capacity) = 0;
|
||||
virtual void InitPoolDynamic(asset_type_t type) = 0;
|
||||
};
|
13
src/ZoneCommon/Pool/XAssetInfo.h
Normal file
13
src/ZoneCommon/Pool/XAssetInfo.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "Zone/XAssetDependency.h"
|
||||
|
||||
template<typename T>
|
||||
class XAssetInfo
|
||||
{
|
||||
public:
|
||||
std::string m_name;
|
||||
T* m_asset;
|
||||
std::vector<std::string> m_script_strings;
|
||||
std::vector<XAssetDependency> m_dependencies;
|
||||
};
|
Reference in New Issue
Block a user