summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
diff options
context:
space:
mode:
authorMai <mai.iam2048@gmail.com>2024-02-13 07:42:57 -0500
committerGitHub <noreply@github.com>2024-02-13 07:42:57 -0500
commit9e6d701fbce863e9dddb8d4c11327508098d64fc (patch)
tree71e9c0c70e85d1819abf4fa99a10a62cf3c644a6 /Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
parent38696949449058f3aa8b456259f06d50f2a9c1d1 (diff)
parent2ab877586def383692c893e3948ccc6865966f3c (diff)
Merge pull request #12577 from iwubcode/mesh_for_asset_loader
VideoCommon: make mesh asset data loadable by asset loader
Diffstat (limited to 'Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp')
-rw-r--r--Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp b/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
index ff20d117c2..be5159b448 100644
--- a/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
+++ b/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
@@ -4,13 +4,16 @@
#include "VideoCommon/Assets/DirectFilesystemAssetLibrary.h"
#include <algorithm>
+#include <vector>
#include <fmt/std.h>
#include "Common/FileUtil.h"
+#include "Common/IOFile.h"
#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"
#include "VideoCommon/Assets/MaterialAsset.h"
+#include "VideoCommon/Assets/MeshAsset.h"
#include "VideoCommon/Assets/ShaderAsset.h"
#include "VideoCommon/Assets/TextureAsset.h"
#include "VideoCommon/RenderState.h"
@@ -220,6 +223,110 @@ CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadMaterial(const As
return LoadInfo{json_data.size(), GetLastAssetWriteTime(asset_id)};
}
+CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadMesh(const AssetID& asset_id,
+ MeshData* data)
+{
+ const auto asset_map = GetAssetMapForID(asset_id);
+
+ // Asset map for a mesh is the mesh and some metadata
+ if (asset_map.size() != 2)
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have two files mapped!", asset_id);
+ return {};
+ }
+
+ const auto metadata = asset_map.find("metadata");
+ const auto mesh = asset_map.find("mesh");
+ if (metadata == asset_map.end())
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a metadata entry mapped!", asset_id);
+ return {};
+ }
+
+ if (mesh == asset_map.end())
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a mesh entry mapped!", asset_id);
+ return {};
+ }
+
+ std::size_t metadata_size;
+ {
+ std::error_code ec;
+ metadata_size = std::filesystem::file_size(metadata->second, ec);
+ if (ec)
+ {
+ ERROR_LOG_FMT(VIDEO,
+ "Asset '{}' error - failed to get mesh metadata file size with error '{}'!",
+ asset_id, ec);
+ return {};
+ }
+ }
+ std::size_t mesh_size;
+ {
+ std::error_code ec;
+ mesh_size = std::filesystem::file_size(mesh->second, ec);
+ if (ec)
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to get mesh file size with error '{}'!",
+ asset_id, ec);
+ return {};
+ }
+ }
+ const auto approx_mem_size = metadata_size + mesh_size;
+
+ File::IOFile file(PathToString(mesh->second), "rb");
+ if (!file.IsOpen())
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to open mesh file '{}'!", asset_id,
+ PathToString(mesh->second));
+ return {};
+ }
+
+ std::vector<u8> bytes;
+ bytes.reserve(file.GetSize());
+ file.ReadBytes(bytes.data(), file.GetSize());
+ if (!MeshData::FromDolphinMesh(bytes, data))
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the mesh file '{}'!", asset_id,
+ PathToString(mesh->second));
+ return {};
+ }
+
+ std::string json_data;
+ if (!File::ReadFileToString(PathToString(metadata->second), json_data))
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the json file '{}'!", asset_id,
+ PathToString(metadata->second));
+ return {};
+ }
+
+ picojson::value root;
+ const auto error = picojson::parse(root, json_data);
+
+ if (!error.empty())
+ {
+ ERROR_LOG_FMT(VIDEO,
+ "Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
+ asset_id, PathToString(metadata->second), error);
+ return {};
+ }
+ if (!root.is<picojson::object>())
+ {
+ ERROR_LOG_FMT(
+ VIDEO,
+ "Asset '{}' error - failed to load the json file '{}', due to root not being an object!",
+ asset_id, PathToString(metadata->second));
+ return {};
+ }
+
+ const auto& root_obj = root.get<picojson::object>();
+
+ if (!MeshData::FromJson(asset_id, root_obj, data))
+ return {};
+
+ return LoadInfo{approx_mem_size, GetLastAssetWriteTime(asset_id)};
+}
+
CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadTexture(const AssetID& asset_id,
TextureData* data)
{