Implement initial Unity-style asset library cache

This commit is contained in:
2026-04-02 03:03:36 +08:00
parent 619856ab22
commit 4c167bec0e
29 changed files with 4818 additions and 73 deletions

View File

@@ -4,9 +4,12 @@
#include <XCEngine/Components/MeshFilterComponent.h>
#include <XCEngine/Components/MeshRendererComponent.h>
#include <XCEngine/Core/Asset/IResource.h>
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <XCEngine/Resources/Material/Material.h>
#include <XCEngine/Resources/Mesh/Mesh.h>
#include <filesystem>
#include <fstream>
#include <sstream>
using namespace XCEngine::Components;
@@ -179,4 +182,59 @@ TEST(MeshRendererComponent_Test, SetMaterialPathPreservesPathWithoutLoadedResour
EXPECT_EQ(component.GetMaterial(1), nullptr);
}
TEST(MeshRendererComponent_Test, SerializeAndDeserializeLoadsProjectMaterialByAssetRef) {
namespace fs = std::filesystem;
ResourceManager& manager = ResourceManager::Get();
manager.Initialize();
const fs::path projectRoot = fs::temp_directory_path() / "xc_mesh_renderer_asset_ref_test";
const fs::path assetsDir = projectRoot / "Assets";
const fs::path materialPath = assetsDir / "runtime.material";
fs::remove_all(projectRoot);
fs::create_directories(assetsDir);
{
std::ofstream materialFile(materialPath);
ASSERT_TRUE(materialFile.is_open());
materialFile << "{\n";
materialFile << " \"renderQueue\": \"geometry\",\n";
materialFile << " \"renderState\": {\n";
materialFile << " \"cull\": \"back\"\n";
materialFile << " }\n";
materialFile << "}";
}
manager.SetResourceRoot(projectRoot.string().c_str());
MeshRendererComponent source;
source.SetMaterialPath(0, "Assets/runtime.material");
ASSERT_EQ(source.GetMaterialCount(), 1u);
ASSERT_NE(source.GetMaterial(0), nullptr);
ASSERT_EQ(source.GetMaterialPath(0), "Assets/runtime.material");
ASSERT_EQ(source.GetMaterialAssetRefs().size(), 1u);
EXPECT_TRUE(source.GetMaterialAssetRefs()[0].IsValid());
std::stringstream stream;
source.Serialize(stream);
const std::string serialized = stream.str();
EXPECT_NE(serialized.find("materialRefs="), std::string::npos);
EXPECT_EQ(serialized.find("materialRefs=;"), std::string::npos);
std::stringstream deserializeStream(serialized);
MeshRendererComponent target;
target.Deserialize(deserializeStream);
ASSERT_EQ(target.GetMaterialCount(), 1u);
EXPECT_EQ(target.GetMaterialPath(0), "Assets/runtime.material");
ASSERT_NE(target.GetMaterial(0), nullptr);
EXPECT_TRUE(target.GetMaterialAssetRefs()[0].IsValid());
manager.SetResourceRoot("");
manager.Shutdown();
fs::remove_all(projectRoot);
}
} // namespace

View File

@@ -141,4 +141,47 @@ TEST(MaterialLoader, RejectsUnknownRenderStateEnum) {
std::remove(materialPath.string().c_str());
}
TEST(MaterialLoader, ResourceManagerLoadsRelativeMaterialFromResourceRoot) {
namespace fs = std::filesystem;
ResourceManager& manager = ResourceManager::Get();
manager.Initialize();
const fs::path previousPath = fs::current_path();
const fs::path projectRoot = fs::temp_directory_path() / "xc_material_loader_resource_root";
const fs::path assetsDir = projectRoot / "Assets";
const fs::path materialPath = assetsDir / "relative.material";
fs::remove_all(projectRoot);
fs::create_directories(assetsDir);
{
std::ofstream materialFile(materialPath);
ASSERT_TRUE(materialFile.is_open());
materialFile << "{\n";
materialFile << " \"renderQueue\": \"geometry\",\n";
materialFile << " \"renderState\": {\n";
materialFile << " \"cull\": \"back\",\n";
materialFile << " \"colorWriteMask\": 15\n";
materialFile << " }\n";
materialFile << "}";
}
manager.SetResourceRoot(projectRoot.string().c_str());
fs::current_path(projectRoot.parent_path());
{
const auto materialHandle = manager.Load<Material>("Assets/relative.material");
ASSERT_TRUE(materialHandle.IsValid());
EXPECT_EQ(materialHandle->GetRenderQueue(), static_cast<XCEngine::Core::int32>(MaterialRenderQueue::Geometry));
EXPECT_EQ(materialHandle->GetRenderState().cullMode, MaterialCullMode::Back);
EXPECT_EQ(materialHandle->GetRenderState().colorWriteMask, 15);
}
fs::current_path(previousPath);
fs::remove_all(projectRoot);
manager.SetResourceRoot("");
manager.Shutdown();
}
} // namespace

View File

@@ -1,4 +1,5 @@
#include <gtest/gtest.h>
#include <XCEngine/Core/Asset/AssetDatabase.h>
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <XCEngine/Resources/Mesh/MeshLoader.h>
#include <XCEngine/Resources/Mesh/MeshImportSettings.h>
@@ -6,7 +7,10 @@
#include <XCEngine/Resources/Texture/Texture.h>
#include <XCEngine/Core/Asset/ResourceTypes.h>
#include <XCEngine/Core/Containers/Array.h>
#include <chrono>
#include <filesystem>
#include <thread>
using namespace XCEngine::Resources;
using namespace XCEngine::Containers;
@@ -143,4 +147,102 @@ TEST(MeshLoader, ImportsMaterialTexturesFromObj) {
delete mesh;
}
TEST(MeshLoader, AssetDatabaseCreatesModelArtifactAndReusesItWithoutReimport) {
namespace fs = std::filesystem;
using namespace std::chrono_literals;
const fs::path projectRoot = fs::temp_directory_path() / "xc_mesh_library_cache_test";
const fs::path assetsDir = projectRoot / "Assets";
fs::remove_all(projectRoot);
fs::create_directories(assetsDir);
fs::copy_file(GetMeshFixturePath("textured_triangle.obj"),
assetsDir / "textured_triangle.obj",
fs::copy_options::overwrite_existing);
fs::copy_file(GetMeshFixturePath("textured_triangle.mtl"),
assetsDir / "textured_triangle.mtl",
fs::copy_options::overwrite_existing);
fs::copy_file(GetMeshFixturePath("checker.bmp"),
assetsDir / "checker.bmp",
fs::copy_options::overwrite_existing);
AssetDatabase database;
database.Initialize(projectRoot.string().c_str());
AssetDatabase::ResolvedAsset firstResolve;
ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, firstResolve));
ASSERT_TRUE(firstResolve.exists);
ASSERT_TRUE(firstResolve.artifactReady);
EXPECT_TRUE(fs::exists(projectRoot / "Assets" / "textured_triangle.obj.meta"));
EXPECT_TRUE(fs::exists(projectRoot / "Library" / "SourceAssetDB" / "assets.db"));
EXPECT_TRUE(fs::exists(projectRoot / "Library" / "ArtifactDB" / "artifacts.db"));
EXPECT_TRUE(fs::exists(firstResolve.artifactMainPath.CStr()));
EXPECT_TRUE(fs::exists((fs::path(firstResolve.artifactDirectory.CStr()) / "texture_0.xctex")));
AssetRef assetRef;
ASSERT_TRUE(database.TryGetAssetRef("Assets/textured_triangle.obj", ResourceType::Mesh, assetRef));
EXPECT_TRUE(assetRef.IsValid());
const auto originalArtifactWriteTime = fs::last_write_time(firstResolve.artifactMainPath.CStr());
std::this_thread::sleep_for(50ms);
AssetDatabase::ResolvedAsset secondResolve;
ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, secondResolve));
EXPECT_EQ(firstResolve.artifactMainPath, secondResolve.artifactMainPath);
EXPECT_EQ(originalArtifactWriteTime, fs::last_write_time(secondResolve.artifactMainPath.CStr()));
database.Shutdown();
fs::remove_all(projectRoot);
}
TEST(MeshLoader, ResourceManagerLoadsModelByAssetRefFromProjectAssets) {
namespace fs = std::filesystem;
ResourceManager& manager = ResourceManager::Get();
manager.Initialize();
const fs::path projectRoot = fs::temp_directory_path() / "xc_mesh_asset_ref_test";
const fs::path assetsDir = projectRoot / "Assets";
fs::remove_all(projectRoot);
fs::create_directories(assetsDir);
fs::copy_file(GetMeshFixturePath("textured_triangle.obj"),
assetsDir / "textured_triangle.obj",
fs::copy_options::overwrite_existing);
fs::copy_file(GetMeshFixturePath("textured_triangle.mtl"),
assetsDir / "textured_triangle.mtl",
fs::copy_options::overwrite_existing);
fs::copy_file(GetMeshFixturePath("checker.bmp"),
assetsDir / "checker.bmp",
fs::copy_options::overwrite_existing);
manager.SetResourceRoot(projectRoot.string().c_str());
const auto firstHandle = manager.Load<Mesh>("Assets/textured_triangle.obj");
ASSERT_TRUE(firstHandle.IsValid());
EXPECT_EQ(firstHandle->GetVertexCount(), 3u);
EXPECT_EQ(firstHandle->GetIndexCount(), 3u);
EXPECT_GE(firstHandle->GetMaterials().Size(), 1u);
EXPECT_EQ(firstHandle->GetTextures().Size(), 1u);
const auto initialMaterialCount = firstHandle->GetMaterials().Size();
AssetRef assetRef;
ASSERT_TRUE(manager.TryGetAssetRef("Assets/textured_triangle.obj", ResourceType::Mesh, assetRef));
EXPECT_TRUE(assetRef.IsValid());
manager.UnloadAll();
const auto secondHandle = manager.Load<Mesh>(assetRef);
ASSERT_TRUE(secondHandle.IsValid());
EXPECT_EQ(secondHandle->GetPath(), "Assets/textured_triangle.obj");
EXPECT_EQ(secondHandle->GetVertexCount(), 3u);
EXPECT_EQ(secondHandle->GetIndexCount(), 3u);
EXPECT_EQ(secondHandle->GetMaterials().Size(), initialMaterialCount);
EXPECT_EQ(secondHandle->GetTextures().Size(), 1u);
manager.SetResourceRoot("");
manager.Shutdown();
fs::remove_all(projectRoot);
}
} // namespace

View File

@@ -1,8 +1,15 @@
#include <gtest/gtest.h>
#include <XCEngine/Core/Asset/AssetDatabase.h>
#include <XCEngine/Core/Asset/AssetRef.h>
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <XCEngine/Resources/Texture/TextureLoader.h>
#include <XCEngine/Core/Asset/ResourceTypes.h>
#include <XCEngine/Core/Containers/Array.h>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <thread>
using namespace XCEngine::Resources;
using namespace XCEngine::Containers;
@@ -57,4 +64,90 @@ TEST(TextureLoader, LoadValidBmpTexture) {
delete texture;
}
TEST(TextureLoader, AssetDatabaseCreatesTextureArtifactAndReusesItWithoutReimport) {
namespace fs = std::filesystem;
using namespace std::chrono_literals;
const fs::path projectRoot = fs::temp_directory_path() / "xc_texture_library_cache_test";
const fs::path assetsDir = projectRoot / "Assets";
const fs::path texturePath = assetsDir / "checker.bmp";
fs::remove_all(projectRoot);
fs::create_directories(assetsDir);
fs::copy_file(GetTextureFixturePath("checker.bmp"), texturePath, fs::copy_options::overwrite_existing);
AssetDatabase database;
database.Initialize(projectRoot.string().c_str());
AssetDatabase::ResolvedAsset firstResolve;
ASSERT_TRUE(database.EnsureArtifact("Assets/checker.bmp", ResourceType::Texture, firstResolve));
ASSERT_TRUE(firstResolve.exists);
ASSERT_TRUE(firstResolve.artifactReady);
EXPECT_TRUE(fs::exists(projectRoot / "Assets" / "checker.bmp.meta"));
EXPECT_TRUE(fs::exists(projectRoot / "Library" / "SourceAssetDB" / "assets.db"));
EXPECT_TRUE(fs::exists(projectRoot / "Library" / "ArtifactDB" / "artifacts.db"));
EXPECT_TRUE(fs::exists(firstResolve.artifactMainPath.CStr()));
std::ifstream metaFile(projectRoot / "Assets" / "checker.bmp.meta");
ASSERT_TRUE(metaFile.is_open());
std::string metaText((std::istreambuf_iterator<char>(metaFile)), std::istreambuf_iterator<char>());
EXPECT_NE(metaText.find("guid:"), std::string::npos);
metaFile.close();
AssetRef assetRef;
ASSERT_TRUE(database.TryGetAssetRef("Assets/checker.bmp", ResourceType::Texture, assetRef));
EXPECT_TRUE(assetRef.IsValid());
const auto originalArtifactWriteTime = fs::last_write_time(firstResolve.artifactMainPath.CStr());
std::this_thread::sleep_for(50ms);
AssetDatabase::ResolvedAsset secondResolve;
ASSERT_TRUE(database.EnsureArtifact("Assets/checker.bmp", ResourceType::Texture, secondResolve));
EXPECT_EQ(firstResolve.artifactMainPath, secondResolve.artifactMainPath);
EXPECT_EQ(originalArtifactWriteTime, fs::last_write_time(secondResolve.artifactMainPath.CStr()));
database.Shutdown();
fs::remove_all(projectRoot);
}
TEST(TextureLoader, ResourceManagerLoadsTextureByAssetRefFromProjectAssets) {
namespace fs = std::filesystem;
ResourceManager& manager = ResourceManager::Get();
manager.Initialize();
const fs::path projectRoot = fs::temp_directory_path() / "xc_texture_asset_ref_test";
const fs::path assetsDir = projectRoot / "Assets";
const fs::path texturePath = assetsDir / "checker.bmp";
fs::remove_all(projectRoot);
fs::create_directories(assetsDir);
fs::copy_file(GetTextureFixturePath("checker.bmp"), texturePath, fs::copy_options::overwrite_existing);
manager.SetResourceRoot(projectRoot.string().c_str());
{
const auto firstHandle = manager.Load<Texture>("Assets/checker.bmp");
ASSERT_TRUE(firstHandle.IsValid());
EXPECT_EQ(firstHandle->GetWidth(), 2u);
EXPECT_EQ(firstHandle->GetHeight(), 2u);
AssetRef assetRef;
ASSERT_TRUE(manager.TryGetAssetRef("Assets/checker.bmp", ResourceType::Texture, assetRef));
EXPECT_TRUE(assetRef.IsValid());
manager.UnloadAll();
const auto secondHandle = manager.Load<Texture>(assetRef);
ASSERT_TRUE(secondHandle.IsValid());
EXPECT_EQ(secondHandle->GetWidth(), 2u);
EXPECT_EQ(secondHandle->GetHeight(), 2u);
EXPECT_EQ(secondHandle->GetPath(), "Assets/checker.bmp");
}
manager.SetResourceRoot("");
manager.Shutdown();
fs::remove_all(projectRoot);
}
} // namespace