- 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
95 lines
2.8 KiB
C++
95 lines
2.8 KiB
C++
#pragma once
|
|
|
|
#include <XCEngine/Core/IO/IResourceLoader.h>
|
|
#include <XCEngine/Core/Asset/ImportSettings.h>
|
|
#include <XCEngine/Core/Containers/Array.h>
|
|
#include <XCEngine/Threading/Mutex.h>
|
|
#include <atomic>
|
|
#include <functional>
|
|
|
|
namespace XCEngine {
|
|
namespace Resources {
|
|
|
|
struct LoadRequest {
|
|
Containers::String path;
|
|
ResourceType type;
|
|
std::function<void(LoadResult)> callback;
|
|
ImportSettings* settings;
|
|
Core::uint64 requestId;
|
|
|
|
LoadRequest() : requestId(0), settings(nullptr) {}
|
|
|
|
LoadRequest(const Containers::String& p, ResourceType t,
|
|
std::function<void(LoadResult)> cb, ImportSettings* s = nullptr)
|
|
: path(p), type(t), callback(std::move(cb)),
|
|
settings(s), requestId(GenerateRequestId()) {}
|
|
|
|
LoadRequest(LoadRequest&& other) noexcept
|
|
: path(std::move(other.path)), type(other.type),
|
|
callback(std::move(other.callback)), settings(other.settings),
|
|
requestId(other.requestId) {
|
|
other.settings = nullptr;
|
|
}
|
|
|
|
LoadRequest& operator=(LoadRequest&& other) noexcept {
|
|
if (this != &other) {
|
|
path = std::move(other.path);
|
|
type = other.type;
|
|
callback = std::move(other.callback);
|
|
settings = other.settings;
|
|
requestId = other.requestId;
|
|
other.settings = nullptr;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
LoadRequest(const LoadRequest&) = default;
|
|
LoadRequest& operator=(const LoadRequest&) = default;
|
|
|
|
private:
|
|
static Core::uint64 GenerateRequestId();
|
|
};
|
|
|
|
class AsyncLoader {
|
|
public:
|
|
static AsyncLoader& Get();
|
|
|
|
void Initialize(Core::uint32 workerThreadCount = 2);
|
|
void Shutdown();
|
|
|
|
void Submit(const Containers::String& path, ResourceType type,
|
|
std::function<void(LoadResult)> callback);
|
|
void Submit(const Containers::String& path, ResourceType type, ImportSettings* settings,
|
|
std::function<void(LoadResult)> callback);
|
|
|
|
void Update();
|
|
bool IsLoading() const { return m_pendingCount > 0; }
|
|
Core::uint32 GetPendingCount() const { return m_pendingCount; }
|
|
float GetProgress() const;
|
|
void CancelAll();
|
|
void Cancel(Core::uint64 requestId);
|
|
|
|
~AsyncLoader() = default;
|
|
|
|
AsyncLoader() = default;
|
|
|
|
private:
|
|
void SubmitInternal(LoadRequest request);
|
|
IResourceLoader* FindLoader(ResourceType type) const;
|
|
|
|
void QueueCompleted(LoadRequest request, LoadResult result);
|
|
|
|
Threading::Mutex m_queueMutex;
|
|
Containers::Array<LoadRequest> m_pendingQueue;
|
|
|
|
Threading::Mutex m_completedMutex;
|
|
Containers::Array<LoadRequest> m_completedQueue;
|
|
|
|
std::atomic<Core::uint32> m_pendingCount{0};
|
|
std::atomic<Core::uint32> m_completedCount{0};
|
|
Core::uint32 m_totalRequested = 0;
|
|
};
|
|
|
|
} // namespace Resources
|
|
} // namespace XCEngine
|