- TEST_SPEC.md: Updated test directory structure to reflect Core/Asset, Core/IO, and Resources/<Type> subdirectories - TEST_SPEC.md: Updated module names and test counts (852 total) - TEST_SPEC.md: Updated build commands for new Resources subdirectories - README.md: Updated engine structure with Core/Asset/ and Core/IO/ - README.md: Updated Resources section with layered architecture - README.md: Updated test coverage table with accurate counts
133 lines
4.6 KiB
C++
133 lines
4.6 KiB
C++
#pragma once
|
|
|
|
#include <XCEngine/Core/IO/IResourceLoader.h>
|
|
#include "ResourceCache.h"
|
|
#include "AsyncLoader.h"
|
|
#include "ResourceHandle.h"
|
|
#include <XCEngine/Core/Asset/ImportSettings.h>
|
|
#include <XCEngine/Core/Containers/String.h>
|
|
#include <XCEngine/Core/Containers/Array.h>
|
|
#include <XCEngine/Core/Containers/HashMap.h>
|
|
#include <XCEngine/Threading/Mutex.h>
|
|
#include <XCEngine/Debug/Logger.h>
|
|
#include <type_traits>
|
|
|
|
namespace XCEngine {
|
|
namespace Resources {
|
|
|
|
class ResourceManager {
|
|
public:
|
|
static ResourceManager& Get();
|
|
|
|
void Initialize();
|
|
void Shutdown();
|
|
|
|
void SetResourceRoot(const Containers::String& rootPath);
|
|
const Containers::String& GetResourceRoot() const;
|
|
|
|
template<typename T>
|
|
ResourceHandle<T> Load(const Containers::String& path, ImportSettings* settings = nullptr) {
|
|
static_assert(std::is_base_of_v<IResource, T>, "T must derive from IResource");
|
|
|
|
ResourceGUID guid = ResourceGUID::Generate(path);
|
|
|
|
IResource* cached = FindInCache(guid);
|
|
if (cached) {
|
|
return ResourceHandle<T>(static_cast<T*>(cached));
|
|
}
|
|
|
|
IResourceLoader* loader = FindLoader(GetResourceType<T>());
|
|
if (!loader) {
|
|
Debug::Logger::Get().Warning(Debug::LogCategory::FileSystem,
|
|
Containers::String("No loader found for resource type: ") +
|
|
GetResourceTypeName(GetResourceType<T>()));
|
|
return ResourceHandle<T>();
|
|
}
|
|
|
|
LoadResult result = loader->Load(path, settings);
|
|
if (!result) {
|
|
Debug::Logger::Get().Error(Debug::LogCategory::FileSystem,
|
|
Containers::String("Failed to load resource: ") + path + " - " + result.errorMessage);
|
|
return ResourceHandle<T>();
|
|
}
|
|
|
|
AddToCache(guid, result.resource);
|
|
|
|
return ResourceHandle<T>(static_cast<T*>(result.resource));
|
|
}
|
|
|
|
void LoadAsync(const Containers::String& path, ResourceType type,
|
|
std::function<void(LoadResult)> callback);
|
|
void LoadAsync(const Containers::String& path, ResourceType type, ImportSettings* settings,
|
|
std::function<void(LoadResult)> callback);
|
|
|
|
void Unload(const Containers::String& path);
|
|
void Unload(ResourceGUID guid);
|
|
void UnloadUnused();
|
|
void UnloadAll();
|
|
|
|
void AddRef(ResourceGUID guid);
|
|
void Release(ResourceGUID guid);
|
|
Core::uint32 GetRefCount(ResourceGUID guid) const;
|
|
|
|
void RegisterLoader(IResourceLoader* loader);
|
|
void UnregisterLoader(ResourceType type);
|
|
IResourceLoader* GetLoader(ResourceType type) const;
|
|
|
|
void SetMemoryBudget(size_t bytes);
|
|
size_t GetMemoryUsage() const;
|
|
size_t GetMemoryBudget() const;
|
|
void FlushCache();
|
|
|
|
IResource* Find(const Containers::String& path);
|
|
IResource* Find(ResourceGUID guid);
|
|
bool Exists(const Containers::String& path) const;
|
|
bool Exists(ResourceGUID guid) const;
|
|
|
|
Containers::String ResolvePath(const Containers::String& relativePath) const;
|
|
|
|
template<typename T>
|
|
void LoadGroup(const Containers::Array<Containers::String>& paths,
|
|
std::function<void(ResourceHandle<T>)> callback) {
|
|
for (const auto& path : paths) {
|
|
LoadAsync(path, GetResourceType<T>(), [callback](LoadResult result) {
|
|
if (result && result.resource) {
|
|
callback(ResourceHandle<T>(static_cast<T*>(result.resource)));
|
|
} else {
|
|
callback(ResourceHandle<T>());
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
Containers::Array<Containers::String> GetResourcePaths() const;
|
|
void UnloadGroup(const Containers::Array<ResourceGUID>& guids);
|
|
|
|
private:
|
|
ResourceManager() = default;
|
|
~ResourceManager() = default;
|
|
|
|
IResource* FindInCache(ResourceGUID guid);
|
|
void AddToCache(ResourceGUID guid, IResource* resource);
|
|
IResourceLoader* FindLoader(ResourceType type);
|
|
void ReloadResource(ResourceGUID guid);
|
|
|
|
Containers::String m_resourceRoot;
|
|
Containers::HashMap<ResourceGUID, IResource*> m_resourceCache;
|
|
Containers::HashMap<ResourceGUID, Core::uint32> m_refCounts;
|
|
Containers::HashMap<ResourceGUID, Containers::String> m_guidToPath;
|
|
Containers::HashMap<ResourceType, IResourceLoader*> m_loaders;
|
|
|
|
size_t m_memoryUsage = 0;
|
|
size_t m_memoryBudget = 512 * 1024 * 1024;
|
|
|
|
ResourceCache m_cache;
|
|
Core::UniqueRef<AsyncLoader> m_asyncLoader;
|
|
Threading::Mutex m_mutex;
|
|
|
|
friend class ResourceHandleBase;
|
|
};
|
|
|
|
} // namespace Resources
|
|
} // namespace XCEngine
|