update ResourceCache & ResourceLoader
This commit is contained in:
parent
696de0326d
commit
2285449eef
|
|
@ -29,7 +29,7 @@
|
|||
#define KGE_SET_STATUS_IF_FAILED(ERRCODE, OBJ, MESSAGE) \
|
||||
if (FAILED(ERRCODE)) \
|
||||
{ \
|
||||
OBJ.Fail(strings::Format("An exception occurred (%#x): %s", ERRCODE, MESSAGE)); \
|
||||
OBJ.Fail(strings::Format("%s failed (%#x): %s", __FUNCTION__, ERRCODE, MESSAGE)); \
|
||||
}
|
||||
|
||||
namespace kiwano
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
// THE SOFTWARE.
|
||||
|
||||
#include <kiwano/utils/ResourceCache.h>
|
||||
#include <kiwano/utils/ResourceLoader.h>
|
||||
|
||||
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));
|
||||
|
|
|
|||
|
|
@ -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<String, ObjectBasePtr> object_cache_;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
#include <kiwano/platform/FileSystem.h>
|
||||
#include <kiwano/utils/Logger.h>
|
||||
#include <kiwano/utils/ResourceLoader.h>
|
||||
#include <kiwano/utils/ResourceCache.h>
|
||||
|
||||
namespace kiwano
|
||||
{
|
||||
|
|
@ -48,19 +49,17 @@ Map<String, Function<void(ResourceCache*, const XmlNode&)>> 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
|
||||
|
|
|
|||
|
|
@ -19,37 +19,44 @@
|
|||
// THE SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
#include <kiwano/utils/ResourceCache.h>
|
||||
#include <kiwano/core/Common.h>
|
||||
#include <kiwano/utils/Json.h>
|
||||
#include <kiwano/utils/Xml.h>
|
||||
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue