2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
2019-10-10 15:09:38 +08:00
|
|
|
#include <fstream>
|
2019-11-13 14:33:15 +08:00
|
|
|
#include <kiwano/core/Logger.h>
|
2020-02-10 14:41:19 +08:00
|
|
|
#include <kiwano/core/Exception.h>
|
2019-12-23 18:05:08 +08:00
|
|
|
#include <kiwano/platform/FileSystem.h>
|
2020-01-21 10:09:55 +08:00
|
|
|
#include <kiwano/utils/ResourceCache.h>
|
2019-10-12 17:37:36 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
namespace __resource_cache_01
|
|
|
|
|
{
|
|
|
|
|
bool LoadJsonData(ResourceCache* loader, Json const& json_data);
|
|
|
|
|
bool LoadXmlData(ResourceCache* loader, const pugi::xml_node& elem);
|
|
|
|
|
} // namespace __resource_cache_01
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
Map<String, Function<bool(ResourceCache*, Json const&)>> load_json_funcs = {
|
2020-02-10 13:47:00 +08:00
|
|
|
{ "latest", __resource_cache_01::LoadJsonData },
|
|
|
|
|
{ "0.1", __resource_cache_01::LoadJsonData },
|
2020-01-21 10:09:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Map<String, Function<bool(ResourceCache*, const pugi::xml_node&)>> load_xml_funcs = {
|
2020-02-10 13:47:00 +08:00
|
|
|
{ "latest", __resource_cache_01::LoadXmlData },
|
|
|
|
|
{ "0.1", __resource_cache_01::LoadXmlData },
|
2020-01-21 10:09:55 +08:00
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
ResourceCache::ResourceCache() {}
|
|
|
|
|
|
|
|
|
|
ResourceCache::~ResourceCache()
|
|
|
|
|
{
|
|
|
|
|
Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResourceCache::LoadFromJsonFile(String const& file_path)
|
|
|
|
|
{
|
2020-02-08 09:59:17 +08:00
|
|
|
if (!FileSystem::GetInstance().IsFileExists(file_path))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 14:41:19 +08:00
|
|
|
KGE_ERROR("%s failed: File not found.", __FUNCTION__);
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
Json json_data;
|
|
|
|
|
|
|
|
|
|
std::ifstream ifs;
|
2020-01-21 10:09:55 +08:00
|
|
|
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-02-08 09:59:17 +08:00
|
|
|
String full_path = FileSystem::GetInstance().GetFullPathForFile(file_path);
|
2020-01-21 10:09:55 +08:00
|
|
|
ifs.open(full_path.c_str());
|
|
|
|
|
ifs >> json_data;
|
|
|
|
|
ifs.close();
|
|
|
|
|
}
|
|
|
|
|
catch (std::wifstream::failure& e)
|
|
|
|
|
{
|
2020-02-10 14:41:19 +08:00
|
|
|
KGE_ERROR("%s failed: Cannot open file. (%s)", __FUNCTION__, e.what());
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-10 13:47:00 +08:00
|
|
|
catch (Json::exception& e)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 14:41:19 +08:00
|
|
|
KGE_ERROR("%s failed: Cannot parse to JSON. (%s)", __FUNCTION__, e.what());
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return LoadFromJson(json_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResourceCache::LoadFromJson(Json const& json_data)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
String version = json_data["version"];
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
auto load = load_json_funcs.find(version);
|
|
|
|
|
if (load != load_json_funcs.end())
|
|
|
|
|
{
|
|
|
|
|
return load->second(this, json_data);
|
|
|
|
|
}
|
|
|
|
|
else if (version.empty())
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
return load_json_funcs["latest"](this, json_data);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-02-10 14:41:19 +08:00
|
|
|
KGE_ERROR("%s failed: unknown resource data version", __FUNCTION__);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-10 13:47:00 +08:00
|
|
|
catch (Json::exception& e)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 14:41:19 +08:00
|
|
|
KGE_ERROR("%s failed: JSON data is invalid. (%s)", __FUNCTION__, e.what());
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResourceCache::LoadFromXmlFile(String const& file_path)
|
|
|
|
|
{
|
2020-02-08 09:59:17 +08:00
|
|
|
if (!FileSystem::GetInstance().IsFileExists(file_path))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 14:41:19 +08:00
|
|
|
KGE_ERROR("%s failed: File not found.", __FUNCTION__);
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-08 09:59:17 +08:00
|
|
|
String full_path = FileSystem::GetInstance().GetFullPathForFile(file_path);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
pugi::xml_parse_result result = doc.load_file(full_path.c_str(), pugi::parse_default, pugi::encoding_auto);
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
return LoadFromXml(doc);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-02-10 14:41:19 +08:00
|
|
|
KGE_ERROR("%s failed: XML [%s] parsed with errors: %s", __FUNCTION__, full_path.c_str(), result.description());
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResourceCache::LoadFromXml(const pugi::xml_document& doc)
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
if (pugi::xml_node root = doc.child("resources"))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
String version;
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto version_node = root.child("version"))
|
2020-01-21 10:09:55 +08:00
|
|
|
version = version_node.child_value();
|
|
|
|
|
|
|
|
|
|
auto load = load_xml_funcs.find(version);
|
|
|
|
|
if (load != load_xml_funcs.end())
|
|
|
|
|
{
|
|
|
|
|
return load->second(this, root);
|
|
|
|
|
}
|
|
|
|
|
else if (version.empty())
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
return load_xml_funcs["latest"](this, root);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-02-10 14:41:19 +08:00
|
|
|
KGE_ERROR("%s failed: unknown resource data version", __FUNCTION__);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ResourceCache::AddFrameSequence(String const& id, Vector<String> const& files)
|
|
|
|
|
{
|
|
|
|
|
if (files.empty())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
Vector<FramePtr> frames;
|
|
|
|
|
frames.reserve(files.size());
|
|
|
|
|
|
|
|
|
|
for (const auto& file : files)
|
|
|
|
|
{
|
|
|
|
|
FramePtr ptr = new (std::nothrow) Frame;
|
|
|
|
|
if (ptr)
|
|
|
|
|
{
|
|
|
|
|
if (ptr->Load(file))
|
|
|
|
|
{
|
|
|
|
|
frames.push_back(ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return AddFrameSequence(id, frames);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ResourceCache::AddFrameSequence(String const& id, Vector<Resource> const& resources)
|
|
|
|
|
{
|
|
|
|
|
if (resources.empty())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
Vector<FramePtr> frames;
|
|
|
|
|
frames.reserve(resources.size());
|
|
|
|
|
|
|
|
|
|
for (const auto& res : resources)
|
|
|
|
|
{
|
|
|
|
|
FramePtr ptr = new (std::nothrow) Frame;
|
|
|
|
|
if (ptr)
|
|
|
|
|
{
|
|
|
|
|
if (ptr->Load(res))
|
|
|
|
|
{
|
|
|
|
|
frames.push_back(ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return AddFrameSequence(id, frames);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ResourceCache::AddFrameSequence(String const& id, Vector<FramePtr> const& frames)
|
|
|
|
|
{
|
|
|
|
|
if (frames.empty())
|
|
|
|
|
return 0;
|
|
|
|
|
|
2020-02-06 16:54:47 +08:00
|
|
|
FrameSequencePtr fs = FrameSequence::Create(frames);
|
2020-01-21 10:09:55 +08:00
|
|
|
if (fs)
|
|
|
|
|
{
|
|
|
|
|
AddObject(id, fs);
|
|
|
|
|
return fs->GetFramesCount();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ResourceCache::AddFrameSequence(String const& id, FramePtr frame, int cols, int rows, float padding_x,
|
|
|
|
|
float padding_y)
|
|
|
|
|
{
|
|
|
|
|
if (cols <= 0 || rows <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (!frame)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
float raw_width = frame->GetWidth();
|
|
|
|
|
float raw_height = frame->GetHeight();
|
|
|
|
|
float width = (raw_width - (cols - 1) * padding_x) / cols;
|
|
|
|
|
float height = (raw_height - (rows - 1) * padding_y) / rows;
|
|
|
|
|
|
|
|
|
|
Vector<FramePtr> frames;
|
|
|
|
|
frames.reserve(rows * cols);
|
|
|
|
|
|
|
|
|
|
float dty = 0;
|
|
|
|
|
for (int i = 0; i < rows; i++)
|
|
|
|
|
{
|
|
|
|
|
float dtx = 0;
|
|
|
|
|
for (int j = 0; j < cols; j++)
|
|
|
|
|
{
|
|
|
|
|
FramePtr ptr = new (std::nothrow) Frame;
|
|
|
|
|
if (ptr)
|
|
|
|
|
{
|
|
|
|
|
ptr->SetTexture(frame->GetTexture());
|
|
|
|
|
ptr->SetCropRect(Rect{ dtx, dty, dtx + width, dty + height });
|
|
|
|
|
frames.push_back(ptr);
|
|
|
|
|
}
|
|
|
|
|
dtx += (width + padding_x);
|
|
|
|
|
}
|
|
|
|
|
dty += (height + padding_y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FrameSequencePtr fs = new (std::nothrow) FrameSequence;
|
|
|
|
|
if (fs)
|
|
|
|
|
{
|
|
|
|
|
fs->AddFrames(frames);
|
|
|
|
|
AddObject(id, fs);
|
|
|
|
|
return fs->GetFramesCount();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResourceCache::AddObject(String const& id, ObjectBasePtr obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
object_cache_.insert(std::make_pair(id, obj));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResourceCache::Remove(String const& id)
|
|
|
|
|
{
|
|
|
|
|
object_cache_.erase(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResourceCache::Clear()
|
|
|
|
|
{
|
|
|
|
|
object_cache_.clear();
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
ObjectBasePtr ResourceCache::Get(String const& id) const
|
|
|
|
|
{
|
|
|
|
|
auto iter = object_cache_.find(id);
|
|
|
|
|
if (iter == object_cache_.end())
|
|
|
|
|
return nullptr;
|
|
|
|
|
return (*iter).second;
|
2019-08-13 15:00:43 +08:00
|
|
|
}
|
2019-08-01 13:07:48 +08:00
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
} // namespace kiwano
|
|
|
|
|
|
2019-08-13 15:00:43 +08:00
|
|
|
namespace kiwano
|
|
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
namespace __resource_cache_01
|
|
|
|
|
{
|
|
|
|
|
struct GlobalData
|
|
|
|
|
{
|
|
|
|
|
String path;
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String& id, const String& type,
|
|
|
|
|
const String& file)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!gdata)
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (type == "gif")
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
// GIF image
|
|
|
|
|
GifImagePtr gif = new (std::nothrow) GifImage;
|
2020-02-10 13:47:00 +08:00
|
|
|
if (gif && gif->Load(gdata->path + file))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
return loader->AddObject(id, gif);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (file.empty())
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
// Simple image
|
|
|
|
|
FramePtr frame = new (std::nothrow) Frame;
|
2020-02-10 13:47:00 +08:00
|
|
|
if (frame && frame->Load(gdata->path + file))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
return loader->AddObject(id, frame);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String& id,
|
|
|
|
|
const Vector<String>& files)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!gdata)
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (files.empty())
|
|
|
|
|
return true;
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
// Frames
|
2020-02-10 13:47:00 +08:00
|
|
|
Vector<FramePtr> frames;
|
|
|
|
|
frames.reserve(files.size());
|
|
|
|
|
for (const auto& file : files)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
FramePtr frame = new Frame;
|
|
|
|
|
if (frame->Load(gdata->path + file))
|
2020-02-06 16:54:47 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
frames.push_back(frame);
|
2020-02-06 16:54:47 +08:00
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
2020-02-10 13:47:00 +08:00
|
|
|
FrameSequencePtr frame_seq = FrameSequence::Create(frames);
|
|
|
|
|
if (frame_seq)
|
|
|
|
|
{
|
|
|
|
|
return !!loader->AddObject(id, frame_seq);
|
|
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String& id, const String& file, int rows,
|
2020-01-21 10:09:55 +08:00
|
|
|
int cols, float padding_x, float padding_y)
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!gdata)
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!file.empty())
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
if (rows || cols)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
// Frame slices
|
|
|
|
|
FramePtr frame = new (std::nothrow) Frame;
|
|
|
|
|
if (frame && frame->Load(gdata->path + file))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
return !!loader->AddFrameSequence(id, frame, std::max(cols, 1), std::max(rows, 1), padding_x,
|
|
|
|
|
padding_y);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
2020-02-10 13:47:00 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Simple image
|
|
|
|
|
FramePtr frame = new (std::nothrow) Frame;
|
|
|
|
|
if (frame && frame->Load(gdata->path + file))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
return loader->AddObject(id, frame);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
bool LoadFontsFromData(ResourceCache* loader, GlobalData* gdata, const String& id, const String& file)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!gdata)
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
FontPtr font = new (std::nothrow) Font;
|
|
|
|
|
if (font && font->Load(gdata->path + file))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
return loader->AddObject(id, font);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LoadJsonData(ResourceCache* loader, Json const& json_data)
|
|
|
|
|
{
|
|
|
|
|
GlobalData global_data;
|
2020-02-10 13:47:00 +08:00
|
|
|
if (json_data.count("path"))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
global_data.path = json_data["path"];
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (json_data.count("images"))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
for (const auto& image : json_data["images"])
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
String id, type, file;
|
|
|
|
|
int rows = 0, cols = 0;
|
|
|
|
|
|
|
|
|
|
if (image.count("id"))
|
|
|
|
|
id = image["id"].get<String>();
|
|
|
|
|
if (image.count("type"))
|
|
|
|
|
type = image["type"].get<String>();
|
|
|
|
|
if (image.count("file"))
|
|
|
|
|
file = image["file"].get<String>();
|
|
|
|
|
if (image.count("rows"))
|
|
|
|
|
rows = image["rows"].get<int>();
|
|
|
|
|
if (image.count("cols"))
|
|
|
|
|
cols = image["cols"].get<int>();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
if (rows || cols)
|
|
|
|
|
{
|
|
|
|
|
float padding_x = 0, padding_y = 0;
|
2020-02-10 13:47:00 +08:00
|
|
|
if (image.count("padding-x"))
|
|
|
|
|
padding_x = image["padding-x"].get<float>();
|
|
|
|
|
if (image.count("padding-y"))
|
|
|
|
|
padding_y = image["padding-y"].get<float>();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
if (!LoadTexturesFromData(loader, &global_data, id, file, rows, cols, padding_x, padding_y))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (image.count("files"))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
Vector<String> files;
|
|
|
|
|
files.reserve(image["files"].size());
|
|
|
|
|
for (const auto& file : image["files"])
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
files.push_back(file.get<String>());
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!LoadTexturesFromData(loader, &global_data, id, files))
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!LoadTexturesFromData(loader, &global_data, id, type, file))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (json_data.count("fonts"))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
for (const auto& font : json_data["fonts"])
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
String id, file;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (font.count("id"))
|
|
|
|
|
id = font["id"].get<String>();
|
|
|
|
|
if (font.count("file"))
|
|
|
|
|
file = font["file"].get<String>();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
if (!LoadFontsFromData(loader, &global_data, id, file))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LoadXmlData(ResourceCache* loader, const pugi::xml_node& elem)
|
|
|
|
|
{
|
|
|
|
|
GlobalData global_data;
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto path = elem.child("path"))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
global_data.path = path.child_value();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto images = elem.child("images"))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
for (auto image : images.children())
|
|
|
|
|
{
|
|
|
|
|
String id, type, file;
|
|
|
|
|
int rows = 0, cols = 0;
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto attr = image.attribute("id"))
|
2020-01-21 10:09:55 +08:00
|
|
|
id.assign(attr.value());
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto attr = image.attribute("type"))
|
2020-01-21 10:09:55 +08:00
|
|
|
type = attr.value();
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto attr = image.attribute("file"))
|
2020-01-21 10:09:55 +08:00
|
|
|
file = attr.value();
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto attr = image.attribute("rows"))
|
2020-01-21 10:09:55 +08:00
|
|
|
rows = attr.as_int(0);
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto attr = image.attribute("cols"))
|
2020-01-21 10:09:55 +08:00
|
|
|
cols = attr.as_int(0);
|
|
|
|
|
|
|
|
|
|
if (rows || cols)
|
|
|
|
|
{
|
|
|
|
|
float padding_x = 0, padding_y = 0;
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto attr = image.attribute("padding-x"))
|
2020-01-21 10:09:55 +08:00
|
|
|
padding_x = attr.as_float(0.0f);
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto attr = image.attribute("padding-y"))
|
2020-01-21 10:09:55 +08:00
|
|
|
padding_y = attr.as_float(0.0f);
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!LoadTexturesFromData(loader, &global_data, id, file, rows, cols, padding_x, padding_y))
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (file.empty() && !image.empty())
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
Vector<String> files_arr;
|
2020-01-21 10:09:55 +08:00
|
|
|
for (auto file : image.children())
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto path = file.attribute("path"))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
files_arr.push_back(path.value());
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!LoadTexturesFromData(loader, &global_data, id, files_arr))
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!LoadTexturesFromData(loader, &global_data, id, type, file))
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto fonts = elem.child("fonts"))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
for (auto font : fonts.children())
|
|
|
|
|
{
|
|
|
|
|
String id, file;
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto attr = font.attribute("id"))
|
2020-01-21 10:09:55 +08:00
|
|
|
id.assign(attr.value());
|
2020-02-10 13:47:00 +08:00
|
|
|
if (auto attr = font.attribute("file"))
|
2020-01-21 10:09:55 +08:00
|
|
|
file = attr.value();
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
if (!LoadFontsFromData(loader, &global_data, id, file))
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2019-08-13 14:44:37 +08:00
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
} // namespace __resource_cache_01
|
|
|
|
|
} // namespace kiwano
|