diff --git a/src/kiwano/render/DirectX/RendererImpl.cpp b/src/kiwano/render/DirectX/RendererImpl.cpp index f11e17f7..72d3ca77 100644 --- a/src/kiwano/render/DirectX/RendererImpl.cpp +++ b/src/kiwano/render/DirectX/RendererImpl.cpp @@ -26,10 +26,10 @@ #include #include -#define KGE_SET_STATUS_IF_FAILED(ERRCODE, OBJ, MESSAGE) \ - if (FAILED(ERRCODE)) \ - { \ - OBJ.Fail(strings::Format("An exception occurred (%#x): %s", ERRCODE, MESSAGE)); \ +#define KGE_SET_STATUS_IF_FAILED(ERRCODE, OBJ, MESSAGE) \ + if (FAILED(ERRCODE)) \ + { \ + OBJ.Fail(strings::Format("%s failed (%#x): %s", __FUNCTION__, ERRCODE, MESSAGE)); \ } namespace kiwano diff --git a/src/kiwano/utils/ResourceCache.cpp b/src/kiwano/utils/ResourceCache.cpp index 4ea7c3c3..b47490c4 100644 --- a/src/kiwano/utils/ResourceCache.cpp +++ b/src/kiwano/utils/ResourceCache.cpp @@ -19,6 +19,7 @@ // THE SOFTWARE. #include +#include namespace kiwano { @@ -30,6 +31,20 @@ ResourceCache::~ResourceCache() Clear(); } +bool ResourceCache::LoadFromJsonFile(const String& file_path) +{ + ResourceLoader loader(*this); + loader.LoadFromJsonFile(file_path); + return IsValid(); +} + +bool ResourceCache::LoadFromXmlFile(const String& file_path) +{ + ResourceLoader loader(*this); + loader.LoadFromXmlFile(file_path); + return IsValid(); +} + void ResourceCache::AddObject(const String& id, ObjectBasePtr obj) { object_cache_.insert(std::make_pair(id, obj)); diff --git a/src/kiwano/utils/ResourceCache.h b/src/kiwano/utils/ResourceCache.h index 468568a8..d9b80158 100644 --- a/src/kiwano/utils/ResourceCache.h +++ b/src/kiwano/utils/ResourceCache.h @@ -37,6 +37,18 @@ class KGE_API ResourceCache final : public ObjectBase public: ResourceCache(); + virtual ~ResourceCache(); + + /// \~chinese + /// @brief 从 JSON 文件加载资源信息 + /// @param file_path JSON文件路径 + bool LoadFromJsonFile(const String& file_path); + + /// \~chinese + /// @brief 从 XML 文件加载资源信息 + /// @param file_path XML文件路径 + bool LoadFromXmlFile(const String& file_path); + /// \~chinese /// @brief 获取资源 /// @param id 对象ID @@ -68,8 +80,6 @@ public: /// @brief 清空所有资源 void Clear(); - virtual ~ResourceCache(); - private: UnorderedMap object_cache_; }; diff --git a/src/kiwano/utils/ResourceLoader.cpp b/src/kiwano/utils/ResourceLoader.cpp index 125882e3..6aa7831e 100644 --- a/src/kiwano/utils/ResourceLoader.cpp +++ b/src/kiwano/utils/ResourceLoader.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace kiwano { @@ -48,19 +49,17 @@ Map> load_xml_funcs = { } // namespace -bool ResourceLoader::LoadFromJsonFile(ResourceCachePtr cache, const String& file_path) +ResourceLoader::ResourceLoader(ResourceCache& cache) + : cache_(cache) { - if (!cache) - { - KGE_ERROR("ResourceLoader::LoadFromJsonFile failed, cache is nullptr"); - return false; - } +} +void ResourceLoader::LoadFromJsonFile(const String& file_path) +{ if (!FileSystem::GetInstance().IsFileExists(file_path)) { - cache->Fail( - strings::Format("ResourceLoader::LoadFromJsonFile failed: [%s] file not found.", file_path.c_str())); - return false; + cache_.Fail(strings::Format("ResourceLoader::LoadFromJsonFile failed: [%s] file not found.", file_path.c_str())); + return; } Json json_data; @@ -77,27 +76,22 @@ bool ResourceLoader::LoadFromJsonFile(ResourceCachePtr cache, const String& file } catch (std::ios_base::failure& e) { - cache->Fail(strings::Format("ResourceLoader::LoadFromJsonFile failed: cannot open file [%s]. %s", + cache_.Fail(strings::Format("ResourceLoader::LoadFromJsonFile failed: cannot open file [%s]. %s", file_path.c_str(), e.what())); - return false; + return; } catch (Json::exception& e) { - cache->Fail(strings::Format("ResourceLoader::LoadFromJsonFile failed: Json file [%s] parsed with errors: %s", + cache_.Fail(strings::Format("ResourceLoader::LoadFromJsonFile failed: Json file [%s] parsed with errors: %s", file_path.c_str(), e.what())); - return false; + return; } - return LoadFromJson(cache, json_data); + + LoadFromJson(json_data); } -bool ResourceLoader::LoadFromJson(ResourceCachePtr cache, const Json& json_data) +void ResourceLoader::LoadFromJson(const Json& json_data) { - if (!cache) - { - KGE_ERROR("ResourceLoader::LoadFromJson failed, cache is nullptr"); - return false; - } - try { String version = json_data["version"]; @@ -105,36 +99,29 @@ bool ResourceLoader::LoadFromJson(ResourceCachePtr cache, const Json& json_data) auto load = load_json_funcs.find(version); if (load != load_json_funcs.end()) { - load->second(cache.Get(), json_data); + load->second(&cache_, json_data); } else if (version.empty()) { - load_json_funcs["latest"](cache.Get(), json_data); + load_json_funcs["latest"](&cache_, json_data); } else { - cache->Fail("ResourceLoader::LoadFromJson failed: unknown resource data version"); + cache_.Fail("ResourceLoader::LoadFromJson failed: unknown resource data version"); } } catch (Json::exception& e) { - cache->Fail(String("ResourceLoader::LoadFromJson failed: ") + e.what()); + cache_.Fail(String("ResourceLoader::LoadFromJson failed: ") + e.what()); } - return cache->IsValid(); } -bool ResourceLoader::LoadFromXmlFile(ResourceCachePtr cache, const String& file_path) +void ResourceLoader::LoadFromXmlFile(const String& file_path) { - if (!cache) - { - KGE_ERROR("ResourceLoader::LoadFromXmlFile failed, cache is nullptr"); - return false; - } - if (!FileSystem::GetInstance().IsFileExists(file_path)) { - cache->Fail(strings::Format("ResourceLoader::LoadFromXmlFile failed: [%s] file not found.", file_path.c_str())); - return false; + cache_.Fail(strings::Format("ResourceLoader::LoadFromXmlFile failed: [%s] file not found.", file_path.c_str())); + return; } String full_path = FileSystem::GetInstance().GetFullPathForFile(file_path); @@ -144,24 +131,17 @@ bool ResourceLoader::LoadFromXmlFile(ResourceCachePtr cache, const String& file_ auto result = doc.load_file(full_path.c_str()); if (result) { - return LoadFromXml(cache, doc); + LoadFromXml(doc); } else { - cache->Fail(strings::Format("ResourceLoader::LoadFromXmlFile failed: XML file [%s] parsed with errors: %s", + cache_.Fail(strings::Format("ResourceLoader::LoadFromXmlFile failed: XML file [%s] parsed with errors: %s", file_path.c_str(), result.description())); - return false; } } -bool ResourceLoader::LoadFromXml(ResourceCachePtr cache, const XmlDocument& doc) +void ResourceLoader::LoadFromXml(const XmlDocument& doc) { - if (!cache) - { - KGE_ERROR("ResourceLoader::LoadFromXml failed, cache is nullptr"); - return false; - } - if (XmlNode root = doc.child("resources")) { String version; @@ -171,22 +151,21 @@ bool ResourceLoader::LoadFromXml(ResourceCachePtr cache, const XmlDocument& doc) auto load = load_xml_funcs.find(version); if (load != load_xml_funcs.end()) { - load->second(cache.Get(), root); + load->second(&cache_, root); } else if (version.empty()) { - load_xml_funcs["latest"](cache.Get(), root); + load_xml_funcs["latest"](&cache_, root); } else { - cache->Fail("ResourceLoader::LoadFromXml failed: unknown resource data version"); + cache_.Fail("ResourceLoader::LoadFromXml failed: unknown resource data version"); } } else { - cache->Fail("ResourceLoader::LoadFromXml failed: unknown file format"); + cache_.Fail("ResourceLoader::LoadFromXml failed: unknown file format"); } - return cache->IsValid(); } } // namespace kiwano diff --git a/src/kiwano/utils/ResourceLoader.h b/src/kiwano/utils/ResourceLoader.h index 7f701e87..0f83eab5 100644 --- a/src/kiwano/utils/ResourceLoader.h +++ b/src/kiwano/utils/ResourceLoader.h @@ -19,37 +19,44 @@ // THE SOFTWARE. #pragma once -#include +#include #include #include namespace kiwano { +class ResourceCache; + /// \~chinese /// @brief 资源加载器 -class KGE_API ResourceLoader final : Noncopyable +class KGE_API ResourceLoader final { public: + ResourceLoader(ResourceCache& cache); + /// \~chinese /// @brief 从 JSON 文件加载资源信息 /// @param file_path JSON文件路径 - static bool LoadFromJsonFile(ResourceCachePtr cache, const String& file_path); + void LoadFromJsonFile(const String& file_path); /// \~chinese /// @brief 从 JSON 加载资源信息 /// @param json_data JSON对象 - static bool LoadFromJson(ResourceCachePtr cache, const Json& json_data); + void LoadFromJson(const Json& json_data); /// \~chinese /// @brief 从 XML 文件加载资源信息 /// @param file_path XML文件路径 - static bool LoadFromXmlFile(ResourceCachePtr cache, const String& file_path); + void LoadFromXmlFile(const String& file_path); /// \~chinese /// @brief 从 XML 文档对象加载资源信息 /// @param doc XML文档对象 - static bool LoadFromXml(ResourceCachePtr cache, const XmlDocument& doc); + void LoadFromXml(const XmlDocument& doc); + +private: + ResourceCache& cache_; }; } // namespace kiwano