diff --git a/src/ZoneCommon/Game/IGame.h b/src/ZoneCommon/Game/IGame.h index 2155c9f0..29c6b51f 100644 --- a/src/ZoneCommon/Game/IGame.h +++ b/src/ZoneCommon/Game/IGame.h @@ -8,5 +8,6 @@ class IGame { public: virtual void AddZone(Zone* zone) = 0; + virtual void RemoveZone(Zone* zone) = 0; virtual std::vector GetZones() = 0; }; \ No newline at end of file diff --git a/src/ZoneCommon/Game/T6/GameT6.cpp b/src/ZoneCommon/Game/T6/GameT6.cpp index 5a3e37ec..da6d3d06 100644 --- a/src/ZoneCommon/Game/T6/GameT6.cpp +++ b/src/ZoneCommon/Game/T6/GameT6.cpp @@ -10,6 +10,14 @@ void GameT6::AddZone(Zone* zone) m_zones.push_back(zone); } +void GameT6::RemoveZone(Zone* zone) +{ + const auto foundEntry = std::find(m_zones.begin(), m_zones.end(), zone); + + if (foundEntry != m_zones.end()) + m_zones.erase(foundEntry); +} + std::vector GameT6::GetZones() { return m_zones; diff --git a/src/ZoneCommon/Game/T6/GameT6.h b/src/ZoneCommon/Game/T6/GameT6.h index f5c9fe42..169300c0 100644 --- a/src/ZoneCommon/Game/T6/GameT6.h +++ b/src/ZoneCommon/Game/T6/GameT6.h @@ -7,6 +7,7 @@ class GameT6 : public IGame public: void AddZone(Zone* zone) override; + void RemoveZone(Zone* zone) override; std::vector GetZones() override; }; diff --git a/src/ZoneCommon/Pool/GlobalAssetPool.h b/src/ZoneCommon/Pool/GlobalAssetPool.h index 3649a482..4c085332 100644 --- a/src/ZoneCommon/Pool/GlobalAssetPool.h +++ b/src/ZoneCommon/Pool/GlobalAssetPool.h @@ -146,12 +146,15 @@ public: m_linked_asset_pools.erase(iLinkEntry); - for(auto iAssetEntry = m_assets.begin(); iAssetEntry != m_assets.end(); ++iAssetEntry) + for(auto iAssetEntry = m_assets.begin(); iAssetEntry != m_assets.end(); ) { auto& assetEntry = *iAssetEntry; if(assetEntry.second.m_duplicate && ReplaceAssetPoolEntry(assetEntry.second)) + { + ++iAssetEntry; continue; + } iAssetEntry = m_assets.erase(iAssetEntry); } diff --git a/src/ZoneCommon/Zone/Zone.cpp b/src/ZoneCommon/Zone/Zone.cpp index e0e900ac..101eed9a 100644 --- a/src/ZoneCommon/Zone/Zone.cpp +++ b/src/ZoneCommon/Zone/Zone.cpp @@ -7,9 +7,39 @@ Zone::Zone(std::string name, const zone_priority_t priority, IZoneAssetPools* po m_pools = pools; m_game = game; m_language = ZoneLanguage::LANGUAGE_NONE; + m_memory = new ZoneMemory(); + m_registered = false; +} + +Zone::~Zone() +{ + if(m_registered) + { + m_game->RemoveZone(this); + } + + delete m_pools; + m_pools = nullptr; + + delete m_memory; + m_memory = nullptr; +} + +void Zone::Register() +{ + if (!m_registered) + { + m_game->AddZone(this); + m_registered = true; + } } IZoneAssetPools* Zone::GetPools() const { return m_pools; } + +ZoneMemory* Zone::GetMemory() const +{ + return m_memory; +} diff --git a/src/ZoneCommon/Zone/Zone.h b/src/ZoneCommon/Zone/Zone.h index b7b5fd5f..208d0078 100644 --- a/src/ZoneCommon/Zone/Zone.h +++ b/src/ZoneCommon/Zone/Zone.h @@ -2,6 +2,8 @@ #include "ZoneTypes.h" #include "Pool/IZoneAssetPools.h" #include "Game/IGame.h" +#include "Zone/XBlock.h" +#include "ZoneMemory.h" #include class IGame; @@ -34,6 +36,10 @@ enum class ZoneLanguage class Zone { IZoneAssetPools* m_pools; + std::vector m_blocks; + ZoneMemory* m_memory; + + bool m_registered; public: std::string m_name; @@ -42,6 +48,10 @@ public: IGame* m_game; Zone(std::string name, zone_priority_t priority, IZoneAssetPools* pools, IGame* game); + ~Zone(); + + void Register(); IZoneAssetPools* GetPools() const; + ZoneMemory* GetMemory() const; }; diff --git a/src/ZoneCommon/Zone/ZoneMemory.cpp b/src/ZoneCommon/Zone/ZoneMemory.cpp new file mode 100644 index 00000000..3361f0a9 --- /dev/null +++ b/src/ZoneCommon/Zone/ZoneMemory.cpp @@ -0,0 +1,40 @@ +#include "ZoneMemory.h" + +ZoneMemory::ZoneMemory() += default; + +ZoneMemory::~ZoneMemory() +{ + for (auto block : m_blocks) + { + delete block; + } + m_blocks.clear(); + + for (auto allocation : m_allocations) + { + free(allocation); + } + m_allocations.clear(); +} + +void ZoneMemory::AddBlock(XBlock* block) +{ + m_blocks.push_back(block); +} + +void* ZoneMemory::Alloc(const size_t size) +{ + void* result = malloc(size); + m_allocations.push_back(result); + + return result; +} + +char* ZoneMemory::Dup(const char* str) +{ + char* result = _strdup(str); + m_allocations.push_back(result); + + return result; +} diff --git a/src/ZoneCommon/Zone/ZoneMemory.h b/src/ZoneCommon/Zone/ZoneMemory.h new file mode 100644 index 00000000..0b1b3e96 --- /dev/null +++ b/src/ZoneCommon/Zone/ZoneMemory.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Zone/XBlock.h" +#include + +class ZoneMemory +{ + std::vector m_blocks; + std::vector m_allocations; + +public: + ZoneMemory(); + ~ZoneMemory(); + + void AddBlock(XBlock* block); + + void* Alloc(size_t size); + char* Dup(const char* str); +}; \ No newline at end of file diff --git a/src/ZoneLoading/Loading/ZoneLoader.cpp b/src/ZoneLoading/Loading/ZoneLoader.cpp index 5a9f4967..9cb5d230 100644 --- a/src/ZoneLoading/Loading/ZoneLoader.cpp +++ b/src/ZoneLoading/Loading/ZoneLoader.cpp @@ -48,6 +48,8 @@ void ZoneLoader::AddXBlock(XBlock* block) { return b1->m_index < b2->m_index; }); + + m_zone->GetMemory()->AddBlock(block); } void ZoneLoader::AddLoadingStep(ILoadingStep* step) @@ -88,7 +90,7 @@ Zone* ZoneLoader::LoadZone(FileAPI::File* file) return nullptr; } - m_zone->m_game->AddZone(m_zone); + m_zone->Register(); return m_zone; } \ No newline at end of file