fix: restore backpack material import output

This commit is contained in:
2026-04-02 22:34:25 +08:00
parent 71923267e9
commit 5ff97b437a
13 changed files with 212 additions and 23 deletions

View File

@@ -11,6 +11,7 @@
#include <chrono>
#include <filesystem>
#include <fstream>
#include <thread>
using namespace XCEngine::Resources;
@@ -51,6 +52,23 @@ bool PumpAsyncLoadsUntilIdle(ResourceManager& manager,
return !manager.IsAsyncLoading();
}
void FlipLastByte(const std::filesystem::path& path) {
std::ifstream input(path, std::ios::binary);
ASSERT_TRUE(input.is_open());
std::vector<char> bytes(
(std::istreambuf_iterator<char>(input)),
std::istreambuf_iterator<char>());
ASSERT_FALSE(bytes.empty());
bytes.back() ^= 0x01;
std::ofstream output(path, std::ios::binary | std::ios::trunc);
ASSERT_TRUE(output.is_open());
output.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
ASSERT_TRUE(static_cast<bool>(output));
}
TEST(MeshLoader, GetResourceType) {
MeshLoader loader;
EXPECT_EQ(loader.GetResourceType(), ResourceType::Mesh);
@@ -172,6 +190,7 @@ TEST(MeshLoader, ImportsMaterialTexturesFromObj) {
EXPECT_EQ(diffuseTexture->GetWidth(), 2u);
EXPECT_EQ(diffuseTexture->GetHeight(), 2u);
EXPECT_EQ(diffuseTexture->GetPixelDataSize(), 16u);
EXPECT_EQ(diffuseTexture->GetFormat(), TextureFormat::RGBA8_UNORM);
EXPECT_EQ(mesh->GetTextures().Size(), 1u);
delete mesh;
@@ -291,6 +310,64 @@ TEST(MeshLoader, AssetDatabaseCreatesModelArtifactAndReusesItWithoutReimport) {
fs::remove_all(projectRoot);
}
TEST(MeshLoader, AssetDatabaseReimportsModelWhenDependencyChanges) {
namespace fs = std::filesystem;
using namespace std::chrono_literals;
const fs::path projectRoot = fs::temp_directory_path() / "xc_mesh_dependency_reimport_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.artifactReady);
const String firstArtifactPath = firstResolve.artifactMainPath;
database.Shutdown();
std::this_thread::sleep_for(50ms);
{
std::ofstream mtlOutput(assetsDir / "textured_triangle.mtl", std::ios::app);
ASSERT_TRUE(mtlOutput.is_open());
mtlOutput << "\n# force dependency reimport\n";
ASSERT_TRUE(static_cast<bool>(mtlOutput));
}
database.Initialize(projectRoot.string().c_str());
AssetDatabase::ResolvedAsset secondResolve;
ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, secondResolve));
ASSERT_TRUE(secondResolve.artifactReady);
EXPECT_NE(firstArtifactPath, secondResolve.artifactMainPath);
const String secondArtifactPath = secondResolve.artifactMainPath;
database.Shutdown();
std::this_thread::sleep_for(50ms);
FlipLastByte(assetsDir / "checker.bmp");
database.Initialize(projectRoot.string().c_str());
AssetDatabase::ResolvedAsset thirdResolve;
ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, thirdResolve));
ASSERT_TRUE(thirdResolve.artifactReady);
EXPECT_NE(secondArtifactPath, thirdResolve.artifactMainPath);
EXPECT_TRUE(fs::exists(thirdResolve.artifactMainPath.CStr()));
database.Shutdown();
fs::remove_all(projectRoot);
}
TEST(MeshLoader, ResourceManagerLoadsModelByAssetRefFromProjectAssets) {
namespace fs = std::filesystem;

View File

@@ -2,6 +2,7 @@
#include <XCEngine/Core/Asset/AssetDatabase.h>
#include <XCEngine/Core/Asset/AssetRef.h>
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <XCEngine/Resources/Texture/TextureImportSettings.h>
#include <XCEngine/Resources/Texture/TextureLoader.h>
#include <XCEngine/Core/Asset/ResourceTypes.h>
#include <XCEngine/Core/Containers/Array.h>
@@ -64,6 +65,24 @@ TEST(TextureLoader, LoadValidBmpTexture) {
delete texture;
}
TEST(TextureLoader, LoadValidBmpTextureAsSRGBWhenRequested) {
TextureLoader loader;
TextureImportSettings settings;
settings.SetSRGB(true);
const std::string path = GetTextureFixturePath("checker.bmp");
LoadResult result = loader.Load(path.c_str(), &settings);
ASSERT_TRUE(result);
ASSERT_NE(result.resource, nullptr);
auto* texture = static_cast<Texture*>(result.resource);
EXPECT_EQ(texture->GetWidth(), 2u);
EXPECT_EQ(texture->GetHeight(), 2u);
EXPECT_EQ(texture->GetFormat(), TextureFormat::RGBA8_SRGB);
delete texture;
}
TEST(TextureLoader, AssetDatabaseCreatesTextureArtifactAndReusesItWithoutReimport) {
namespace fs = std::filesystem;
using namespace std::chrono_literals;