mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-13 00:08:26 -05:00
chore: refactor pool allocation and add indirect references
This commit is contained in:
@ -11,6 +11,8 @@ class Zone;
|
||||
template<typename T> class AssetPool
|
||||
{
|
||||
public:
|
||||
using type = T;
|
||||
|
||||
std::map<std::string, XAssetInfo<T>*> m_asset_lookup;
|
||||
|
||||
class Iterator
|
||||
@ -46,8 +48,7 @@ public:
|
||||
|
||||
virtual ~AssetPool() = default;
|
||||
|
||||
virtual XAssetInfo<T>*
|
||||
AddAsset(std::string name, T* asset, Zone* zone, std::vector<XAssetInfoGeneric*> dependencies, std::vector<scr_string_t> usedScriptStrings) = 0;
|
||||
virtual XAssetInfo<T>* AddAsset(std::unique_ptr<XAssetInfo<T>> xAssetInfo) = 0;
|
||||
|
||||
XAssetInfo<T>* GetAsset(const std::string& name)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@ template<typename T> class AssetPoolDynamic final : public AssetPool<T>
|
||||
{
|
||||
using AssetPool<T>::m_asset_lookup;
|
||||
|
||||
std::vector<XAssetInfo<T>*> m_assets;
|
||||
std::vector<std::unique_ptr<XAssetInfo<T>>> m_assets;
|
||||
asset_type_t m_type;
|
||||
|
||||
public:
|
||||
@ -29,35 +29,27 @@ public:
|
||||
{
|
||||
GlobalAssetPool<T>::UnlinkAssetPool(this);
|
||||
|
||||
for (auto* entry : m_assets)
|
||||
for (auto& entry : m_assets)
|
||||
{
|
||||
delete entry->Asset();
|
||||
delete entry;
|
||||
}
|
||||
|
||||
m_assets.clear();
|
||||
m_asset_lookup.clear();
|
||||
}
|
||||
|
||||
XAssetInfo<T>*
|
||||
AddAsset(std::string name, T* asset, Zone* zone, std::vector<XAssetInfoGeneric*> dependencies, std::vector<scr_string_t> usedScriptStrings) override
|
||||
XAssetInfo<T>* AddAsset(std::unique_ptr<XAssetInfo<T>> xAssetInfo) override
|
||||
{
|
||||
auto* newInfo = new XAssetInfo<T>();
|
||||
newInfo->m_type = m_type;
|
||||
newInfo->m_name = std::move(name);
|
||||
newInfo->m_zone = zone;
|
||||
newInfo->m_dependencies = std::move(dependencies);
|
||||
newInfo->m_used_script_strings = std::move(usedScriptStrings);
|
||||
|
||||
T* newAsset = new T();
|
||||
memcpy(newAsset, asset, sizeof(T));
|
||||
newInfo->m_ptr = newAsset;
|
||||
memcpy(newAsset, xAssetInfo->Asset(), sizeof(T));
|
||||
xAssetInfo->m_ptr = newAsset;
|
||||
|
||||
m_assets.push_back(newInfo);
|
||||
m_asset_lookup[newInfo->m_name] = newInfo;
|
||||
auto* pAssetInfo = xAssetInfo.get();
|
||||
m_asset_lookup[xAssetInfo->m_name] = pAssetInfo;
|
||||
m_assets.emplace_back(std::move(xAssetInfo));
|
||||
|
||||
GlobalAssetPool<T>::LinkAsset(this, newInfo);
|
||||
GlobalAssetPool<T>::LinkAsset(this, pAssetInfo);
|
||||
|
||||
return newInfo;
|
||||
return pAssetInfo;
|
||||
}
|
||||
};
|
||||
|
@ -67,9 +67,7 @@ public:
|
||||
~AssetPoolStatic() override
|
||||
{
|
||||
if (m_capacity > 0)
|
||||
{
|
||||
GlobalAssetPool<T>::UnlinkAssetPool(this);
|
||||
}
|
||||
|
||||
delete[] m_pool;
|
||||
m_pool = nullptr;
|
||||
@ -81,25 +79,19 @@ public:
|
||||
m_capacity = 0;
|
||||
}
|
||||
|
||||
XAssetInfo<T>*
|
||||
AddAsset(std::string name, T* asset, Zone* zone, std::vector<XAssetInfoGeneric*> dependencies, std::vector<scr_string_t> usedScriptStrings) override
|
||||
XAssetInfo<T>* AddAsset(std::unique_ptr<XAssetInfo<T>> xAssetInfo) override
|
||||
{
|
||||
if (m_free == nullptr)
|
||||
{
|
||||
throw std::runtime_error("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));
|
||||
const T* pAsset = xAssetInfo->Asset();
|
||||
xAssetInfo->m_ptr = static_cast<void*>(&poolSlot->m_entry);
|
||||
memcpy(&poolSlot->m_entry, pAsset, sizeof(T));
|
||||
|
||||
poolSlot->m_info->m_type = m_type;
|
||||
poolSlot->m_info->m_name = std::move(name);
|
||||
poolSlot->m_info->m_ptr = &poolSlot->m_entry;
|
||||
poolSlot->m_info->m_zone = zone;
|
||||
poolSlot->m_info->m_dependencies = std::move(dependencies);
|
||||
poolSlot->m_info->m_used_script_strings = std::move(usedScriptStrings);
|
||||
*poolSlot->m_info = std::move(*xAssetInfo);
|
||||
|
||||
m_asset_lookup[poolSlot->m_info->m_name] = poolSlot->m_info;
|
||||
|
||||
|
55
src/ZoneCommon/Pool/XAssetInfo.cpp
Normal file
55
src/ZoneCommon/Pool/XAssetInfo.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include "XAssetInfo.h"
|
||||
|
||||
IndirectAssetReference::IndirectAssetReference()
|
||||
: m_type(-1)
|
||||
{
|
||||
}
|
||||
|
||||
IndirectAssetReference::IndirectAssetReference(const asset_type_t type, std::string name)
|
||||
: m_type(type),
|
||||
m_name(std::move(name))
|
||||
{
|
||||
}
|
||||
|
||||
XAssetInfoGeneric::XAssetInfoGeneric()
|
||||
: m_type(-1),
|
||||
m_ptr(nullptr),
|
||||
m_zone(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
XAssetInfoGeneric::XAssetInfoGeneric(const asset_type_t type, std::string name, void* ptr)
|
||||
: m_type(type),
|
||||
m_name(std::move(name)),
|
||||
m_ptr(ptr),
|
||||
m_zone(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
XAssetInfoGeneric::XAssetInfoGeneric(
|
||||
const asset_type_t type, std::string name, void* ptr, std::vector<XAssetInfoGeneric*> dependencies, std::vector<scr_string_t> usedScriptStrings)
|
||||
: m_type(type),
|
||||
m_name(std::move(name)),
|
||||
m_ptr(ptr),
|
||||
m_dependencies(std::move(dependencies)),
|
||||
m_used_script_strings(std::move(usedScriptStrings)),
|
||||
m_zone(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
XAssetInfoGeneric::XAssetInfoGeneric(const asset_type_t type,
|
||||
std::string name,
|
||||
void* ptr,
|
||||
std::vector<XAssetInfoGeneric*> dependencies,
|
||||
std::vector<scr_string_t> usedScriptStrings,
|
||||
std::vector<IndirectAssetReference> indirectAssetReferences,
|
||||
Zone* zone)
|
||||
: m_type(type),
|
||||
m_name(std::move(name)),
|
||||
m_ptr(ptr),
|
||||
m_dependencies(std::move(dependencies)),
|
||||
m_used_script_strings(std::move(usedScriptStrings)),
|
||||
m_indirect_asset_references(std::move(indirectAssetReferences)),
|
||||
m_zone(zone)
|
||||
{
|
||||
}
|
@ -7,20 +7,82 @@
|
||||
|
||||
class Zone;
|
||||
|
||||
// An indirect asset reference is a reference that is not done per pointer but by mentioning the asset
|
||||
// in a string or requiring the asset to exist in some other way
|
||||
class IndirectAssetReference
|
||||
{
|
||||
public:
|
||||
asset_type_t m_type;
|
||||
std::string m_name;
|
||||
|
||||
IndirectAssetReference();
|
||||
IndirectAssetReference(asset_type_t type, std::string name);
|
||||
};
|
||||
|
||||
class XAssetInfoGeneric
|
||||
{
|
||||
public:
|
||||
asset_type_t m_type = -1;
|
||||
asset_type_t m_type;
|
||||
std::string m_name;
|
||||
Zone* m_zone;
|
||||
void* m_ptr;
|
||||
std::vector<XAssetInfoGeneric*> m_dependencies;
|
||||
std::vector<scr_string_t> m_used_script_strings;
|
||||
void* m_ptr;
|
||||
std::vector<IndirectAssetReference> m_indirect_asset_references;
|
||||
Zone* m_zone;
|
||||
|
||||
XAssetInfoGeneric();
|
||||
XAssetInfoGeneric(asset_type_t type, std::string name, void* ptr);
|
||||
XAssetInfoGeneric(
|
||||
asset_type_t type, std::string name, void* ptr, std::vector<XAssetInfoGeneric*> dependencies, std::vector<scr_string_t> usedScriptStrings);
|
||||
XAssetInfoGeneric(asset_type_t type,
|
||||
std::string name,
|
||||
void* ptr,
|
||||
std::vector<XAssetInfoGeneric*> dependencies,
|
||||
std::vector<scr_string_t> usedScriptStrings,
|
||||
std::vector<IndirectAssetReference> indirectAssetReferences,
|
||||
Zone* zone);
|
||||
~XAssetInfoGeneric() = default;
|
||||
|
||||
XAssetInfoGeneric(const XAssetInfoGeneric& other) = default;
|
||||
XAssetInfoGeneric(XAssetInfoGeneric&& other) noexcept = default;
|
||||
XAssetInfoGeneric& operator=(const XAssetInfoGeneric& other) = default;
|
||||
XAssetInfoGeneric& operator=(XAssetInfoGeneric&& other) noexcept = default;
|
||||
};
|
||||
|
||||
template<typename T> class XAssetInfo : public XAssetInfoGeneric
|
||||
{
|
||||
public:
|
||||
XAssetInfo() = default;
|
||||
|
||||
XAssetInfo(const asset_type_t type, std::string name, T* ptr)
|
||||
: XAssetInfoGeneric(type, std::move(name), static_cast<void*>(ptr))
|
||||
{
|
||||
}
|
||||
|
||||
XAssetInfo(const asset_type_t type, std::string name, T* ptr, std::vector<XAssetInfoGeneric*> dependencies, std::vector<scr_string_t> usedScriptStrings)
|
||||
: XAssetInfoGeneric(type, std::move(name), static_cast<void*>(ptr), std::move(dependencies), std::move(usedScriptStrings))
|
||||
{
|
||||
}
|
||||
|
||||
XAssetInfo(const asset_type_t type,
|
||||
std::string name,
|
||||
T* ptr,
|
||||
std::vector<XAssetInfoGeneric*> dependencies,
|
||||
std::vector<scr_string_t> usedScriptStrings,
|
||||
std::vector<IndirectAssetReference> indirectAssetReferences,
|
||||
Zone* zone)
|
||||
: XAssetInfoGeneric(
|
||||
type, std::move(name), static_cast<void*>(ptr), std::move(dependencies), std::move(usedScriptStrings), std::move(indirectAssetReferences), zone)
|
||||
{
|
||||
}
|
||||
|
||||
~XAssetInfo() = default;
|
||||
|
||||
XAssetInfo(const XAssetInfo& other) = default;
|
||||
XAssetInfo(XAssetInfo&& other) noexcept = default;
|
||||
XAssetInfo& operator=(const XAssetInfo& other) = default;
|
||||
XAssetInfo& operator=(XAssetInfo&& other) noexcept = default;
|
||||
|
||||
T* Asset()
|
||||
{
|
||||
return static_cast<T*>(m_ptr);
|
||||
|
@ -5,24 +5,22 @@ ZoneAssetPools::ZoneAssetPools(Zone* zone)
|
||||
{
|
||||
}
|
||||
|
||||
XAssetInfoGeneric* ZoneAssetPools::AddAsset(
|
||||
const asset_type_t type, std::string name, void* asset, std::vector<XAssetInfoGeneric*> dependencies, std::vector<scr_string_t> usedScriptStrings)
|
||||
{
|
||||
return AddAsset(type, std::move(name), asset, std::move(dependencies), std::move(usedScriptStrings), m_zone);
|
||||
}
|
||||
|
||||
XAssetInfoGeneric* ZoneAssetPools::AddAsset(const asset_type_t type,
|
||||
std::string name,
|
||||
void* asset,
|
||||
std::vector<XAssetInfoGeneric*> dependencies,
|
||||
std::vector<scr_string_t> usedScriptStrings,
|
||||
Zone* zone)
|
||||
std::vector<IndirectAssetReference> indirectAssetReferences)
|
||||
{
|
||||
auto* assetInfo = AddAssetToPool(type, std::move(name), asset, std::move(dependencies), std::move(usedScriptStrings), zone);
|
||||
return AddAsset(std::make_unique<XAssetInfoGeneric>(
|
||||
type, std::move(name), asset, std::move(dependencies), std::move(usedScriptStrings), std::move(indirectAssetReferences), m_zone));
|
||||
}
|
||||
|
||||
XAssetInfoGeneric* ZoneAssetPools::AddAsset(std::unique_ptr<XAssetInfoGeneric> xAssetInfo)
|
||||
{
|
||||
auto* assetInfo = AddAssetToPool(std::move(xAssetInfo));
|
||||
if (assetInfo)
|
||||
{
|
||||
m_assets_in_order.push_back(assetInfo);
|
||||
}
|
||||
|
||||
return assetInfo;
|
||||
}
|
||||
|
@ -5,10 +5,12 @@
|
||||
#include "Zone/ZoneTypes.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Zone;
|
||||
class IndirectAssetReference;
|
||||
class XAssetInfoGeneric;
|
||||
|
||||
class ZoneAssetPools
|
||||
@ -17,12 +19,7 @@ protected:
|
||||
Zone* m_zone;
|
||||
std::vector<XAssetInfoGeneric*> m_assets_in_order;
|
||||
|
||||
virtual XAssetInfoGeneric* AddAssetToPool(asset_type_t type,
|
||||
std::string name,
|
||||
void* asset,
|
||||
std::vector<XAssetInfoGeneric*> dependencies,
|
||||
std::vector<scr_string_t> usedScriptStrings,
|
||||
Zone* zone) = 0;
|
||||
virtual XAssetInfoGeneric* AddAssetToPool(std::unique_ptr<XAssetInfoGeneric> xAssetInfo) = 0;
|
||||
|
||||
public:
|
||||
using iterator = std::vector<XAssetInfoGeneric*>::const_iterator;
|
||||
@ -34,14 +31,13 @@ public:
|
||||
ZoneAssetPools& operator=(const ZoneAssetPools& other) = delete;
|
||||
ZoneAssetPools& operator=(ZoneAssetPools&& other) noexcept = default;
|
||||
|
||||
XAssetInfoGeneric*
|
||||
AddAsset(asset_type_t type, std::string name, void* asset, std::vector<XAssetInfoGeneric*> dependencies, std::vector<scr_string_t> usedScriptStrings);
|
||||
XAssetInfoGeneric* AddAsset(std::unique_ptr<XAssetInfoGeneric> xAssetInfo);
|
||||
XAssetInfoGeneric* AddAsset(asset_type_t type,
|
||||
std::string name,
|
||||
void* asset,
|
||||
std::vector<XAssetInfoGeneric*> dependencies,
|
||||
std::vector<scr_string_t> usedScriptStrings,
|
||||
Zone* zone);
|
||||
std::vector<IndirectAssetReference> indirectAssetReferences);
|
||||
_NODISCARD virtual XAssetInfoGeneric* GetAsset(asset_type_t type, std::string name) const = 0;
|
||||
_NODISCARD virtual asset_type_t GetAssetTypeCount() const = 0;
|
||||
_NODISCARD virtual const char* GetAssetTypeName(asset_type_t assetType) const = 0;
|
||||
@ -51,6 +47,6 @@ public:
|
||||
|
||||
_NODISCARD size_t GetTotalAssetCount() const;
|
||||
|
||||
iterator begin() const;
|
||||
iterator end() const;
|
||||
_NODISCARD iterator begin() const;
|
||||
_NODISCARD iterator end() const;
|
||||
};
|
||||
|
Reference in New Issue
Block a user