Implement initial Unity-style asset library cache
This commit is contained in:
@@ -1,28 +1,57 @@
|
||||
#include <XCEngine/Core/IO/IResourceLoader.h>
|
||||
#include <XCEngine/Core/Asset/ResourceManager.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Resources {
|
||||
|
||||
Containers::Array<Core::uint8> IResourceLoader::ReadFileData(const Containers::String& path) {
|
||||
namespace {
|
||||
|
||||
Containers::Array<Core::uint8> TryReadFileData(
|
||||
const std::filesystem::path& filePath,
|
||||
bool& opened) {
|
||||
Containers::Array<Core::uint8> data;
|
||||
|
||||
std::ifstream file(path.CStr(), std::ios::binary | std::ios::ate);
|
||||
|
||||
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
|
||||
if (!file.is_open()) {
|
||||
opened = false;
|
||||
return data;
|
||||
}
|
||||
|
||||
std::streamsize size = file.tellg();
|
||||
|
||||
opened = true;
|
||||
const std::streamsize size = file.tellg();
|
||||
if (size <= 0) {
|
||||
return data;
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
data.Resize(static_cast<size_t>(size));
|
||||
if (!file.read(reinterpret_cast<char*>(data.Data()), size)) {
|
||||
data.Clear();
|
||||
}
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Containers::Array<Core::uint8> IResourceLoader::ReadFileData(const Containers::String& path) {
|
||||
bool opened = false;
|
||||
const std::filesystem::path inputPath(path.CStr());
|
||||
Containers::Array<Core::uint8> data = TryReadFileData(inputPath, opened);
|
||||
if (opened || path.Empty() || inputPath.is_absolute()) {
|
||||
return data;
|
||||
}
|
||||
|
||||
const Containers::String& resourceRoot = ResourceManager::Get().GetResourceRoot();
|
||||
if (resourceRoot.Empty()) {
|
||||
return data;
|
||||
}
|
||||
|
||||
return TryReadFileData(std::filesystem::path(resourceRoot.CStr()) / inputPath, opened);
|
||||
}
|
||||
|
||||
Containers::String IResourceLoader::GetExtension(const Containers::String& path) {
|
||||
Containers::String ext;
|
||||
size_t dotPos = Containers::String::npos;
|
||||
|
||||
Reference in New Issue
Block a user