Implement initial Unity-style asset library cache
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
#include <XCEngine/Resources/Texture/TextureLoader.h>
|
||||
#include <XCEngine/Core/Asset/ArtifactFormats.h>
|
||||
#include <XCEngine/Resources/BuiltinResources.h>
|
||||
#include <XCEngine/Core/Asset/ResourceManager.h>
|
||||
#include <stb_image.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Resources {
|
||||
@@ -29,7 +32,7 @@ LoadResult CreateTextureResource(const Containers::String& path,
|
||||
params.name = GetResourceNameFromPath(path);
|
||||
params.path = path;
|
||||
params.guid = ResourceGUID::Generate(path);
|
||||
params.memorySize = 0;
|
||||
params.memorySize = pixelDataSize;
|
||||
texture->Initialize(params);
|
||||
|
||||
if (!texture->Create(width,
|
||||
@@ -47,6 +50,40 @@ LoadResult CreateTextureResource(const Containers::String& path,
|
||||
return LoadResult(texture);
|
||||
}
|
||||
|
||||
LoadResult LoadTextureArtifact(const Containers::String& path) {
|
||||
std::ifstream input(path.CStr(), std::ios::binary);
|
||||
if (!input.is_open()) {
|
||||
return LoadResult(Containers::String("Failed to read texture artifact: ") + path);
|
||||
}
|
||||
|
||||
TextureArtifactHeader header;
|
||||
input.read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
if (!input) {
|
||||
return LoadResult(Containers::String("Failed to parse texture artifact header: ") + path);
|
||||
}
|
||||
|
||||
const std::string magic(header.magic, header.magic + 7);
|
||||
if (magic != "XCTEX01") {
|
||||
return LoadResult(Containers::String("Invalid texture artifact magic: ") + path);
|
||||
}
|
||||
|
||||
Containers::Array<Core::uint8> pixelData;
|
||||
pixelData.Resize(static_cast<size_t>(header.pixelDataSize));
|
||||
if (header.pixelDataSize > 0) {
|
||||
input.read(reinterpret_cast<char*>(pixelData.Data()), static_cast<std::streamsize>(header.pixelDataSize));
|
||||
if (!input) {
|
||||
return LoadResult(Containers::String("Failed to read texture artifact payload: ") + path);
|
||||
}
|
||||
}
|
||||
|
||||
return CreateTextureResource(path,
|
||||
static_cast<TextureFormat>(header.textureFormat),
|
||||
header.width,
|
||||
header.height,
|
||||
pixelData.Data(),
|
||||
pixelData.Size());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TextureLoader::TextureLoader() = default;
|
||||
@@ -62,19 +99,28 @@ Containers::Array<Containers::String> TextureLoader::GetSupportedExtensions() co
|
||||
extensions.PushBack(Containers::String("gif"));
|
||||
extensions.PushBack(Containers::String("hdr"));
|
||||
extensions.PushBack(Containers::String("dds"));
|
||||
extensions.PushBack(Containers::String("xctex"));
|
||||
return extensions;
|
||||
}
|
||||
|
||||
bool TextureLoader::CanLoad(const Containers::String& path) const {
|
||||
if (IsBuiltinTexturePath(path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Containers::String ext = GetExtension(path).ToLower();
|
||||
|
||||
return ext == "png" || ext == "jpg" || ext == "jpeg" ||
|
||||
ext == "tga" || ext == "bmp" || ext == "gif" ||
|
||||
ext == "hdr" || ext == "dds";
|
||||
ext == "hdr" || ext == "dds" || ext == "xctex";
|
||||
}
|
||||
|
||||
LoadResult TextureLoader::Load(const Containers::String& path, const ImportSettings* settings) {
|
||||
(void)settings;
|
||||
|
||||
if (IsBuiltinTexturePath(path)) {
|
||||
return CreateBuiltinTextureResource(path);
|
||||
}
|
||||
|
||||
Containers::String ext = GetExtension(path).ToLower();
|
||||
|
||||
@@ -82,6 +128,10 @@ LoadResult TextureLoader::Load(const Containers::String& path, const ImportSetti
|
||||
return LoadResult(Containers::String("Unsupported texture format: ") + ext);
|
||||
}
|
||||
|
||||
if (ext == "xctex") {
|
||||
return LoadTextureArtifact(path);
|
||||
}
|
||||
|
||||
if (ext == "dds") {
|
||||
return LoadResult(Containers::String("DDS texture decoding is not implemented yet: ") + path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user