feat: Implement resource system Phase 4.5 - ResourceFileSystem (4 files, +305 lines)
This commit is contained in:
32
engine/include/XCEngine/Resources/FileArchive.h
Normal file
32
engine/include/XCEngine/Resources/FileArchive.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "ResourceFileSystem.h"
|
||||
#include <cstdio>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Resources {
|
||||
|
||||
class FileArchive : public IArchive {
|
||||
public:
|
||||
FileArchive();
|
||||
virtual ~FileArchive() override;
|
||||
|
||||
bool Open(const Containers::String& path) override;
|
||||
void Close() override;
|
||||
|
||||
bool Read(const Containers::String& fileName, void* buffer, size_t size, size_t offset) const override;
|
||||
size_t GetSize(const Containers::String& fileName) const override;
|
||||
bool Exists(const Containers::String& fileName) const override;
|
||||
void Enumerate(const Containers::String& pattern, Containers::Array<Containers::String>& outFiles) const override;
|
||||
|
||||
bool IsValid() const override { return m_isValid; }
|
||||
|
||||
const Containers::String& GetPath() const { return m_archivePath; }
|
||||
|
||||
private:
|
||||
Containers::String m_archivePath;
|
||||
bool m_isValid = false;
|
||||
};
|
||||
|
||||
} // namespace Resources
|
||||
} // namespace XCEngine
|
||||
75
engine/include/XCEngine/Resources/ResourceFileSystem.h
Normal file
75
engine/include/XCEngine/Resources/ResourceFileSystem.h
Normal file
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include "ResourceTypes.h"
|
||||
#include "../Containers/String.h"
|
||||
#include "../Containers/Array.h"
|
||||
#include "../Containers/HashMap.h"
|
||||
#include "../Core/SmartPtr.h"
|
||||
#include "../Threading/Mutex.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Resources {
|
||||
|
||||
class IArchive {
|
||||
public:
|
||||
virtual ~IArchive() = default;
|
||||
|
||||
virtual bool Open(const Containers::String& path) = 0;
|
||||
virtual void Close() = 0;
|
||||
|
||||
virtual bool Read(const Containers::String& fileName, void* buffer, size_t size, size_t offset) const = 0;
|
||||
virtual size_t GetSize(const Containers::String& fileName) const = 0;
|
||||
virtual bool Exists(const Containers::String& fileName) const = 0;
|
||||
virtual void Enumerate(const Containers::String& pattern, Containers::Array<Containers::String>& outFiles) const = 0;
|
||||
|
||||
virtual bool IsValid() const = 0;
|
||||
};
|
||||
|
||||
struct ResourceInfo {
|
||||
Containers::String path;
|
||||
size_t size = 0;
|
||||
Core::uint64 modifiedTime = 0;
|
||||
bool inArchive = false;
|
||||
Containers::String archivePath;
|
||||
};
|
||||
|
||||
class ResourceFileSystem {
|
||||
public:
|
||||
ResourceFileSystem();
|
||||
~ResourceFileSystem();
|
||||
|
||||
void Initialize(const Containers::String& rootPath);
|
||||
void Shutdown();
|
||||
|
||||
bool AddArchive(const Containers::String& archivePath);
|
||||
bool AddDirectory(const Containers::String& directoryPath);
|
||||
|
||||
void RemoveArchive(const Containers::String& archivePath);
|
||||
|
||||
bool FindResource(const Containers::String& relativePath, Containers::String& outAbsolutePath) const;
|
||||
|
||||
Containers::Array<Core::uint8> ReadResource(const Containers::String& relativePath) const;
|
||||
|
||||
bool Exists(const Containers::String& relativePath) const;
|
||||
|
||||
bool GetResourceInfo(const Containers::String& relativePath, ResourceInfo& outInfo) const;
|
||||
|
||||
void EnumerateResources(const Containers::String& pattern, Containers::Array<ResourceInfo>& outResources) const;
|
||||
|
||||
static ResourceFileSystem& Get();
|
||||
|
||||
private:
|
||||
IArchive* FindArchive(const Containers::String& relativePath) const;
|
||||
bool FindInDirectories(const Containers::String& relativePath, Containers::String& outAbsolutePath) const;
|
||||
bool FindInArchives(const Containers::String& relativePath, Containers::String& outArchivePath) const;
|
||||
|
||||
Containers::String m_rootPath;
|
||||
Containers::Array<Core::UniqueRef<IArchive>> m_archives;
|
||||
Containers::Array<Containers::String> m_directories;
|
||||
|
||||
mutable Containers::HashMap<Containers::String, ResourceInfo> m_infoCache;
|
||||
mutable Threading::Mutex m_mutex;
|
||||
};
|
||||
|
||||
} // namespace Resources
|
||||
} // namespace XCEngine
|
||||
@@ -20,6 +20,9 @@
|
||||
#include "AudioClip.h"
|
||||
#include "AudioLoader.h"
|
||||
|
||||
#include "ResourceFileSystem.h"
|
||||
#include "FileArchive.h"
|
||||
|
||||
// Forward declarations for concrete resource types
|
||||
namespace XCEngine {
|
||||
namespace Resources {
|
||||
|
||||
Reference in New Issue
Block a user