From 5f7830d09ec7e8921b27f525d1763e90daf32f3c Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 8 Mar 2021 11:33:19 +0100 Subject: [PATCH] Fixes and code cleanup for Flax Storage types --- .../Content/Factories/BinaryAssetFactory.cpp | 3 + .../Content/Storage/ContentStorageManager.cpp | 8 +- .../Content/Storage/ContentStorageManager.h | 5 +- Source/Engine/Content/Storage/FlaxFile.h | 93 ++--------- Source/Engine/Content/Storage/FlaxPackage.cpp | 31 ---- Source/Engine/Content/Storage/FlaxPackage.h | 64 +------- Source/Engine/Content/Storage/FlaxStorage.cpp | 151 ++++++++++++++++++ Source/Engine/Content/Storage/FlaxStorage.h | 2 +- 8 files changed, 182 insertions(+), 175 deletions(-) delete mode 100644 Source/Engine/Content/Storage/FlaxPackage.cpp diff --git a/Source/Engine/Content/Factories/BinaryAssetFactory.cpp b/Source/Engine/Content/Factories/BinaryAssetFactory.cpp index 6eee4eaa2..c3c4e8178 100644 --- a/Source/Engine/Content/Factories/BinaryAssetFactory.cpp +++ b/Source/Engine/Content/Factories/BinaryAssetFactory.cpp @@ -6,6 +6,9 @@ #include "Engine/Core/Math/Math.h" #include "Engine/Platform/FileSystem.h" #include "Engine/Content/Storage/ContentStorageManager.h" +#if USE_EDITOR +#include "Engine/Content/Storage/FlaxFile.h" +#endif #include "Engine/Content/Upgraders/BinaryAssetUpgrader.h" bool BinaryAssetFactoryBase::Init(BinaryAsset* asset) diff --git a/Source/Engine/Content/Storage/ContentStorageManager.cpp b/Source/Engine/Content/Storage/ContentStorageManager.cpp index 1b830458d..d272dda57 100644 --- a/Source/Engine/Content/Storage/ContentStorageManager.cpp +++ b/Source/Engine/Content/Storage/ContentStorageManager.cpp @@ -1,12 +1,14 @@ // Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #include "ContentStorageManager.h" +#include "FlaxFile.h" +#include "FlaxPackage.h" #include "Engine/Core/Log.h" #include "Engine/Engine/EngineService.h" #include "Engine/Platform/FileSystem.h" #include "Engine/Profiler/ProfilerCPU.h" -namespace ContentStorageManagerImpl +namespace { CriticalSection Locker; #if USE_EDITOR @@ -14,13 +16,11 @@ namespace ContentStorageManagerImpl Array Packages; #else Array Files; - Array Packages(32); + Array Packages(64); #endif Dictionary StorageMap(2048); } -using namespace ContentStorageManagerImpl; - class ContentStorageManagerService : public EngineService { public: diff --git a/Source/Engine/Content/Storage/ContentStorageManager.h b/Source/Engine/Content/Storage/ContentStorageManager.h index 91b0522d0..2d7ce5fd7 100644 --- a/Source/Engine/Content/Storage/ContentStorageManager.h +++ b/Source/Engine/Content/Storage/ContentStorageManager.h @@ -2,10 +2,11 @@ #pragma once -#include "FlaxFile.h" -#include "FlaxPackage.h" #include "FlaxStorageReference.h" +class FlaxFile; +class FlaxPackage; + /// /// Content Storage Manager is responsible for content data management /// diff --git a/Source/Engine/Content/Storage/FlaxFile.h b/Source/Engine/Content/Storage/FlaxFile.h index 0f7cae45c..a50a36515 100644 --- a/Source/Engine/Content/Storage/FlaxFile.h +++ b/Source/Engine/Content/Storage/FlaxFile.h @@ -9,8 +9,6 @@ /// class FLAXENGINE_API FlaxFile : public FlaxStorage { - friend ContentStorageManager; - protected: Entry _asset; @@ -21,91 +19,24 @@ public: /// Initializes a new instance of the class. /// /// The path. - FlaxFile(const StringView& path) - : FlaxStorage(path) - { - _asset.ID = Guid::Empty; - } - -public: - - /// - /// Finalizes an instance of the class. - /// - ~FlaxFile() - { - } + FlaxFile(const StringView& path); public: // [FlaxStorage] - String ToString() const override - { - return String::Format(TEXT("Asset \'{0}\'"), _path); - } - - bool IsPackage() const override - { - return false; - } - - bool AllowDataModifications() const override - { - return true; - } - - bool HasAsset(const Guid& id) const override - { - return _asset.ID == id; - } - - bool HasAsset(const AssetInfo& info) const override - { -#if USE_EDITOR - if (_path != info.Path) - return false; -#endif - return _asset.ID == info.ID && _asset.TypeName == info.TypeName; - } - - int32 GetEntriesCount() const override - { - return _asset.ID.IsValid() ? 1 : 0; - } - - void GetEntry(int32 index, Entry& output) const override - { - ASSERT(index == 0); - output = _asset; - } - - void GetEntries(Array& output) const override - { - if (_asset.ID.IsValid()) - output.Add(_asset); - } - - void Dispose() override - { - // Base - FlaxStorage::Dispose(); - - // Clean - _asset.ID = Guid::Empty; - } + String ToString() const override; + bool IsPackage() const override; + bool AllowDataModifications() const override; + bool HasAsset(const Guid& id) const override; + bool HasAsset(const AssetInfo& info) const override; + int32 GetEntriesCount() const override; + void GetEntry(int32 index, Entry& output) const override; + void GetEntries(Array& output) const override; + void Dispose() override; protected: // [FlaxStorage] - bool GetEntry(const Guid& id, Entry& e) override - { - e = _asset; - return id != _asset.ID; - } - - void AddEntry(Entry& e) override - { - ASSERT(_asset.ID.IsValid() == false); - _asset = e; - } + bool GetEntry(const Guid& id, Entry& e) override; + void AddEntry(Entry& e) override; }; diff --git a/Source/Engine/Content/Storage/FlaxPackage.cpp b/Source/Engine/Content/Storage/FlaxPackage.cpp deleted file mode 100644 index 4ec920339..000000000 --- a/Source/Engine/Content/Storage/FlaxPackage.cpp +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -#include "FlaxPackage.h" - -bool FlaxPackage::HasAsset(const AssetInfo& info) const -{ - ASSERT(_path == info.Path); - - Entry* e = _entries.TryGet(info.ID); - return e && e->TypeName == info.TypeName; -} - -bool FlaxPackage::GetEntry(const Guid& id, Entry& e) -{ - return !_entries.TryGet(id, e); -} - -void FlaxPackage::AddEntry(Entry& e) -{ - ASSERT(HasAsset(e.ID) == false); - _entries.Add(e.ID, e); -} - -void FlaxPackage::Dispose() -{ - // Base - FlaxStorage::Dispose(); - - // Clean - _entries.Clear(); -} diff --git a/Source/Engine/Content/Storage/FlaxPackage.h b/Source/Engine/Content/Storage/FlaxPackage.h index db61426c2..adf33ed28 100644 --- a/Source/Engine/Content/Storage/FlaxPackage.h +++ b/Source/Engine/Content/Storage/FlaxPackage.h @@ -20,67 +20,19 @@ public: /// Initializes a new instance of the class. /// /// The path. - FlaxPackage(const StringView& path) - : FlaxStorage(path) - , _entries(256) - { - } - - /// - /// Finalizes an instance of the class. - /// - ~FlaxPackage() - { - } + FlaxPackage(const StringView& path); public: // [FlaxStorage] - String ToString() const override - { - return String::Format(TEXT("Package \'{0}\'"), _path); - } - - bool IsPackage() const override - { - return true; - } - - bool AllowDataModifications() const override - { - return false; - } - - bool HasAsset(const Guid& id) const override - { - return _entries.ContainsKey(id); - } - + String ToString() const override; + bool IsPackage() const override; + bool AllowDataModifications() const override; + bool HasAsset(const Guid& id) const override; bool HasAsset(const AssetInfo& info) const override; - - int32 GetEntriesCount() const override - { - return _entries.Count(); - } - - void GetEntry(int32 index, Entry& output) const override - { - ASSERT(index >= 0 && index < _entries.Count()); - for (auto i = _entries.Begin(); i.IsNotEnd(); ++i) - { - if (index-- <= 0) - { - output = i->Value; - return; - } - } - } - - void GetEntries(Array& output) const override - { - _entries.GetValues(output); - } - + int32 GetEntriesCount() const override; + void GetEntry(int32 index, Entry& output) const override; + void GetEntries(Array& output) const override; void Dispose() override; protected: diff --git a/Source/Engine/Content/Storage/FlaxStorage.cpp b/Source/Engine/Content/Storage/FlaxStorage.cpp index 99c229544..d4da1d951 100644 --- a/Source/Engine/Content/Storage/FlaxStorage.cpp +++ b/Source/Engine/Content/Storage/FlaxStorage.cpp @@ -1,6 +1,8 @@ // Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #include "FlaxStorage.h" +#include "FlaxFile.h" +#include "FlaxPackage.h" #include "ContentStorageManager.h" #include "Engine/Core/Log.h" #include "Engine/Platform/File.h" @@ -1319,3 +1321,152 @@ void FlaxStorage::OnRename(const StringView& newPath) } #endif + +FlaxFile::FlaxFile(const StringView& path) + : FlaxStorage(path) +{ + _asset.ID = Guid::Empty; +} + +String FlaxFile::ToString() const +{ + return String::Format(TEXT("Asset \'{0}\'"), _path); +} + +bool FlaxFile::IsPackage() const +{ + return false; +} + +bool FlaxFile::AllowDataModifications() const +{ + return true; +} + +bool FlaxFile::HasAsset(const Guid& id) const +{ + return _asset.ID == id; +} + +bool FlaxFile::HasAsset(const AssetInfo& info) const +{ +#if USE_EDITOR + if (_path != info.Path) + return false; +#endif + return _asset.ID == info.ID && _asset.TypeName == info.TypeName; +} + +int32 FlaxFile::GetEntriesCount() const +{ + return _asset.ID.IsValid() ? 1 : 0; +} + +void FlaxFile::GetEntry(int32 index, Entry& output) const +{ + ASSERT(index == 0); + output = _asset; +} + +void FlaxFile::GetEntries(Array& output) const +{ + if (_asset.ID.IsValid()) + output.Add(_asset); +} + +void FlaxFile::Dispose() +{ + // Base + FlaxStorage::Dispose(); + + // Clean + _asset.ID = Guid::Empty; +} + +bool FlaxFile::GetEntry(const Guid& id, Entry& e) +{ + e = _asset; + return id != _asset.ID; +} + +void FlaxFile::AddEntry(Entry& e) +{ + ASSERT(_asset.ID.IsValid() == false); + _asset = e; +} + +FlaxPackage::FlaxPackage(const StringView& path) + : FlaxStorage(path) + , _entries(256) +{ +} + +String FlaxPackage::ToString() const +{ + return String::Format(TEXT("Package \'{0}\'"), _path); +} + +bool FlaxPackage::IsPackage() const +{ + return true; +} + +bool FlaxPackage::AllowDataModifications() const +{ + return false; +} + +bool FlaxPackage::HasAsset(const Guid& id) const +{ + return _entries.ContainsKey(id); +} + +bool FlaxPackage::HasAsset(const AssetInfo& info) const +{ + ASSERT(_path == info.Path); + Entry* e = _entries.TryGet(info.ID); + return e && e->TypeName == info.TypeName; +} + +int32 FlaxPackage::GetEntriesCount() const +{ + return _entries.Count(); +} + +void FlaxPackage::GetEntry(int32 index, Entry& output) const +{ + ASSERT(index >= 0 && index < _entries.Count()); + for (auto i = _entries.Begin(); i.IsNotEnd(); ++i) + { + if (index-- <= 0) + { + output = i->Value; + return; + } + } +} + +void FlaxPackage::GetEntries(Array& output) const +{ + _entries.GetValues(output); +} + +void FlaxPackage::Dispose() +{ + // Base + FlaxStorage::Dispose(); + + // Clean + _entries.Clear(); +} + +bool FlaxPackage::GetEntry(const Guid& id, Entry& e) +{ + return !_entries.TryGet(id, e); +} + +void FlaxPackage::AddEntry(Entry& e) +{ + ASSERT(HasAsset(e.ID) == false); + _entries.Add(e.ID, e); +} diff --git a/Source/Engine/Content/Storage/FlaxStorage.h b/Source/Engine/Content/Storage/FlaxStorage.h index 185cc81ae..55eadaa42 100644 --- a/Source/Engine/Content/Storage/FlaxStorage.h +++ b/Source/Engine/Content/Storage/FlaxStorage.h @@ -116,7 +116,7 @@ public: /// /// Finalizes an instance of the class. /// - ~FlaxStorage(); + virtual ~FlaxStorage(); public: