2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
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:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// 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-08-13 14:44:37 +08:00
|
|
|
#include "ResourceCache.h"
|
2019-08-14 21:52:49 +08:00
|
|
|
#include "../base/Logger.h"
|
2019-08-13 14:44:37 +08:00
|
|
|
#include "../2d/Frame.h"
|
|
|
|
|
#include "../2d/FrameSequence.h"
|
|
|
|
|
#include "../renderer/GifImage.h"
|
2019-07-31 00:09:24 +08:00
|
|
|
#include <fstream>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 15:00:43 +08:00
|
|
|
namespace __resource_cache_01
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 15:00:43 +08:00
|
|
|
bool LoadJsonData(ResourceCache* loader, Json const& json_data);
|
|
|
|
|
bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem);
|
2019-07-31 00:09:24 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-04 00:11:47 +08:00
|
|
|
namespace
|
|
|
|
|
{
|
2019-08-13 21:16:38 +08:00
|
|
|
Map<String, Function<bool(ResourceCache*, Json const&)>> load_json_funcs = {
|
2019-08-13 15:00:43 +08:00
|
|
|
{ L"latest", __resource_cache_01::LoadJsonData },
|
|
|
|
|
{ L"0.1", __resource_cache_01::LoadJsonData },
|
2019-08-04 00:11:47 +08:00
|
|
|
};
|
|
|
|
|
|
2019-08-13 21:16:38 +08:00
|
|
|
Map<String, Function<bool(ResourceCache*, tinyxml2::XMLElement*)>> load_xml_funcs = {
|
2019-08-13 15:00:43 +08:00
|
|
|
{ L"latest", __resource_cache_01::LoadXmlData },
|
|
|
|
|
{ L"0.1", __resource_cache_01::LoadXmlData },
|
2019-08-04 00:11:47 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 15:00:43 +08:00
|
|
|
ResourceCache::ResourceCache()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResourceCache::~ResourceCache()
|
|
|
|
|
{
|
|
|
|
|
Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
bool ResourceCache::LoadFromJsonFile(String const& file_path)
|
2019-07-31 00:09:24 +08:00
|
|
|
{
|
|
|
|
|
Json json_data;
|
|
|
|
|
std::wifstream ifs;
|
|
|
|
|
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ifs.open(file_path.c_str());
|
|
|
|
|
ifs >> json_data;
|
|
|
|
|
ifs.close();
|
|
|
|
|
}
|
|
|
|
|
catch (std::wifstream::failure& e)
|
|
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str());
|
2019-07-31 00:09:24 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (json_exception& e)
|
|
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot parse to JSON. (%s)", string_to_wide(e.what()).c_str());
|
2019-07-31 00:09:24 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return LoadFromJson(json_data);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
bool ResourceCache::LoadFromJson(Json const& json_data)
|
2019-07-31 00:09:24 +08:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
String version = json_data[L"version"];
|
2019-08-04 00:11:47 +08:00
|
|
|
|
|
|
|
|
auto load = load_json_funcs.find(version);
|
|
|
|
|
if (load != load_json_funcs.end())
|
2019-07-31 00:09:24 +08:00
|
|
|
{
|
2019-08-04 00:11:47 +08:00
|
|
|
return load->second(this, json_data);
|
|
|
|
|
}
|
|
|
|
|
else if (version.empty())
|
|
|
|
|
{
|
|
|
|
|
return load_json_funcs[L"latest"](this, json_data);
|
2019-07-31 00:09:24 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("unknown JSON data version");
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-31 00:09:24 +08:00
|
|
|
catch (std::exception& e)
|
|
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
KGE_WARNING_LOG(L"ResourceCache::LoadFromJson failed: JSON data is invalid. (%s)", string_to_wide(e.what()).c_str());
|
2019-07-31 00:09:24 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-04 00:11:47 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
bool ResourceCache::LoadFromXmlFile(String const& file_path)
|
2019-08-04 00:11:47 +08:00
|
|
|
{
|
|
|
|
|
tinyxml2::XMLDocument doc;
|
2019-08-04 16:18:35 +08:00
|
|
|
|
|
|
|
|
std::wifstream ifs;
|
|
|
|
|
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ifs.open(file_path.c_str());
|
|
|
|
|
|
2019-08-20 19:32:36 +08:00
|
|
|
StringStream ss;
|
2019-08-04 16:18:35 +08:00
|
|
|
ss << ifs.rdbuf();
|
|
|
|
|
|
|
|
|
|
if (tinyxml2::XML_SUCCESS != doc.Parse(ss.str().c_str()))
|
|
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
KGE_WARNING_LOG(L"ResourceCache::LoadFromXmlFile failed: %s (%s)",
|
2019-08-04 16:18:35 +08:00
|
|
|
tinyxml2::XMLDocument::ErrorIDToName(doc.ErrorID()), doc.ErrorStr());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (std::wifstream::failure& e)
|
2019-08-04 00:11:47 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
KGE_WARNING_LOG(L"ResourceCache::LoadFromXmlFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str());
|
2019-08-04 00:11:47 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-04 16:18:35 +08:00
|
|
|
|
2019-08-04 00:11:47 +08:00
|
|
|
return LoadFromXml(&doc);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
bool ResourceCache::LoadFromXml(tinyxml2::XMLDocument* doc)
|
2019-08-04 00:11:47 +08:00
|
|
|
{
|
|
|
|
|
if (doc)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-08-04 16:18:35 +08:00
|
|
|
if (auto root = doc->FirstChildElement(L"resources"))
|
2019-08-04 00:11:47 +08:00
|
|
|
{
|
2019-08-04 16:18:35 +08:00
|
|
|
kiwano::wstring version;
|
|
|
|
|
if (auto ver = root->FirstChildElement(L"version")) version = ver->GetText();
|
2019-08-04 00:11:47 +08:00
|
|
|
|
2019-08-04 16:18:35 +08:00
|
|
|
auto load = load_xml_funcs.find(version);
|
2019-08-04 00:11:47 +08:00
|
|
|
if (load != load_xml_funcs.end())
|
|
|
|
|
{
|
2019-08-04 16:18:35 +08:00
|
|
|
return load->second(this, root);
|
2019-08-04 00:11:47 +08:00
|
|
|
}
|
|
|
|
|
else if (version.empty())
|
|
|
|
|
{
|
2019-08-04 16:18:35 +08:00
|
|
|
return load_xml_funcs[L"latest"](this, root);
|
2019-08-04 00:11:47 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("unknown JSON data version");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception& e)
|
|
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
KGE_WARNING_LOG(L"ResourceCache::LoadFromXml failed: %s", string_to_wide(e.what()).c_str());
|
2019-08-04 00:11:47 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-18 17:49:13 +08:00
|
|
|
bool ResourceCache::AddFrame(String const& id, String const& file_path)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
FramePtr ptr = new (std::nothrow) Frame;
|
2019-03-31 01:37:06 +08:00
|
|
|
if (ptr)
|
|
|
|
|
{
|
2019-08-18 17:49:13 +08:00
|
|
|
if (ptr->Load(file_path))
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
return AddFrame(id, ptr);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
bool ResourceCache::AddFrame(String const & id, FramePtr frame)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
if (frame)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
cache_.insert(std::make_pair(id, frame));
|
2019-03-31 01:37:06 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-18 22:49:44 +08:00
|
|
|
UInt32 ResourceCache::AddFrameSequence(String const& id, Vector<String> const& files)
|
2019-07-31 00:09:24 +08:00
|
|
|
{
|
2019-08-18 17:49:13 +08:00
|
|
|
if (files.empty())
|
2019-03-31 01:37:06 +08:00
|
|
|
return 0;
|
|
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
Vector<FramePtr> frames;
|
|
|
|
|
frames.reserve(files.size());
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-08-18 17:49:13 +08:00
|
|
|
for (const auto& file : files)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
FramePtr ptr = new (std::nothrow) Frame;
|
2019-03-31 01:37:06 +08:00
|
|
|
if (ptr)
|
|
|
|
|
{
|
2019-08-18 17:49:13 +08:00
|
|
|
if (ptr->Load(file))
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-21 16:54:51 +08:00
|
|
|
frames.push_back(ptr);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
if (!frames.empty())
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-21 16:54:51 +08:00
|
|
|
FrameSequencePtr fs = new (std::nothrow) FrameSequence(frames);
|
|
|
|
|
return AddFrameSequence(id, fs);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-18 22:49:44 +08:00
|
|
|
UInt32 ResourceCache::AddFrameSequence(String const & id, String const& file_path, Int32 cols, Int32 rows)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (cols <= 0 || rows <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
FramePtr raw = new (std::nothrow) Frame;
|
2019-08-18 17:49:13 +08:00
|
|
|
if (!raw || !raw->Load(file_path))
|
2019-08-01 13:07:48 +08:00
|
|
|
return false;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-08-18 22:49:44 +08:00
|
|
|
Float32 raw_width = raw->GetWidth();
|
|
|
|
|
Float32 raw_height = raw->GetHeight();
|
|
|
|
|
Float32 width = raw_width / cols;
|
|
|
|
|
Float32 height = raw_height / rows;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
Vector<FramePtr> frames;
|
|
|
|
|
frames.reserve(rows * cols);
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-08-18 22:49:44 +08:00
|
|
|
for (Int32 i = 0; i < rows; i++)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-18 22:49:44 +08:00
|
|
|
for (Int32 j = 0; j < cols; j++)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-21 16:33:41 +08:00
|
|
|
FramePtr ptr = new (std::nothrow) Frame(raw->GetTexture());
|
2019-03-31 01:37:06 +08:00
|
|
|
if (ptr)
|
|
|
|
|
{
|
2019-08-20 19:32:36 +08:00
|
|
|
ptr->SetCropRect(Rect{ j * width, i * height, (j + 1) * width, (i + 1) * height });
|
2019-08-21 16:54:51 +08:00
|
|
|
frames.push_back(ptr);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
FrameSequencePtr fs = new (std::nothrow) FrameSequence(frames);
|
|
|
|
|
return AddFrameSequence(id, fs);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-18 22:49:44 +08:00
|
|
|
UInt32 ResourceCache::AddFrameSequence(String const & id, FrameSequencePtr frames)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (frames)
|
|
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
cache_.insert(std::make_pair(id, frames));
|
2019-08-01 13:07:48 +08:00
|
|
|
return frames->GetFrames().size();
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2019-08-01 13:07:48 +08:00
|
|
|
return 0;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-18 10:23:54 +08:00
|
|
|
bool ResourceCache::AddObjectBase(String const& id, ObjectBasePtr obj)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
cache_.insert(std::make_pair(id, obj));
|
2019-03-31 01:37:06 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
FramePtr ResourceCache::GetFrame(String const & id) const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
return Get<Frame>(id);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
FrameSequencePtr ResourceCache::GetFrameSequence(String const & id) const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
return Get<FrameSequence>(id);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
void ResourceCache::Delete(String const & id)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
cache_.erase(id);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 15:00:43 +08:00
|
|
|
void ResourceCache::Clear()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
cache_.clear();
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 15:00:43 +08:00
|
|
|
}
|
2019-08-01 13:07:48 +08:00
|
|
|
|
2019-08-13 15:00:43 +08:00
|
|
|
namespace kiwano
|
|
|
|
|
{
|
|
|
|
|
namespace __resource_cache_01
|
2019-08-01 13:07:48 +08:00
|
|
|
{
|
2019-08-13 15:00:43 +08:00
|
|
|
struct GlobalData
|
|
|
|
|
{
|
|
|
|
|
String path;
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
bool LoadTexturesFromData(ResourceCache* loader, GlobalData* gdata, const String* id, const String* type,
|
2019-08-18 22:49:44 +08:00
|
|
|
const String* file, const Vector<const WChar*>* files, Int32 rows, Int32 cols)
|
2019-08-13 15:00:43 +08:00
|
|
|
{
|
|
|
|
|
if (!gdata || !id) return false;
|
|
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
|
{
|
|
|
|
|
if (!(*file).empty())
|
|
|
|
|
{
|
|
|
|
|
if (rows || cols)
|
|
|
|
|
{
|
|
|
|
|
// Frame slices
|
2019-08-18 17:49:13 +08:00
|
|
|
return !!loader->AddFrameSequence(*id, gdata->path + (*file), std::max(cols, 1), std::max(rows, 1));
|
2019-08-13 15:00:43 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-08-21 16:54:51 +08:00
|
|
|
// Simple image
|
2019-08-18 17:49:13 +08:00
|
|
|
return loader->AddFrame(*id, gdata->path + (*file));
|
2019-08-13 15:00:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Frames
|
|
|
|
|
if (files)
|
|
|
|
|
{
|
2019-08-13 21:16:38 +08:00
|
|
|
Vector<FramePtr> frames;
|
2019-08-13 15:00:43 +08:00
|
|
|
frames.reserve(files->size());
|
|
|
|
|
for (const auto& file : (*files))
|
|
|
|
|
{
|
|
|
|
|
FramePtr frame = new Frame;
|
|
|
|
|
if (frame->Load(gdata->path + (file)))
|
|
|
|
|
{
|
|
|
|
|
frames.push_back(frame);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-18 17:49:13 +08:00
|
|
|
FrameSequencePtr frame_seq = new FrameSequence(frames);
|
|
|
|
|
return !!loader->AddFrameSequence(*id, frame_seq);
|
2019-08-13 15:00:43 +08:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LoadJsonData(ResourceCache* loader, Json const& json_data)
|
|
|
|
|
{
|
|
|
|
|
GlobalData global_data;
|
|
|
|
|
if (json_data.count(L"path"))
|
|
|
|
|
{
|
|
|
|
|
global_data.path = json_data[L"path"];
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
if (json_data.count(L"images"))
|
2019-08-13 15:00:43 +08:00
|
|
|
{
|
2019-08-21 16:54:51 +08:00
|
|
|
for (const auto& image : json_data[L"images"])
|
2019-08-13 15:00:43 +08:00
|
|
|
{
|
|
|
|
|
const String* id = nullptr, * type = nullptr, * file = nullptr;
|
2019-08-18 22:49:44 +08:00
|
|
|
Int32 rows = 0, cols = 0;
|
2019-08-01 13:07:48 +08:00
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
if (image.count(L"id")) id = &image[L"id"].as_string();
|
|
|
|
|
if (image.count(L"type")) type = &image[L"type"].as_string();
|
|
|
|
|
if (image.count(L"file")) file = &image[L"file"].as_string();
|
|
|
|
|
if (image.count(L"rows")) rows = image[L"rows"].as_int();
|
|
|
|
|
if (image.count(L"cols")) cols = image[L"cols"].as_int();
|
2019-08-13 15:00:43 +08:00
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
if (image.count(L"files"))
|
2019-08-13 15:00:43 +08:00
|
|
|
{
|
2019-08-18 22:49:44 +08:00
|
|
|
Vector<const WChar*> files;
|
2019-08-21 16:54:51 +08:00
|
|
|
files.reserve(image[L"files"].size());
|
|
|
|
|
for (const auto& file : image[L"files"])
|
2019-08-13 15:00:43 +08:00
|
|
|
{
|
|
|
|
|
files.push_back(file.as_string().c_str());
|
|
|
|
|
}
|
2019-08-21 16:33:41 +08:00
|
|
|
if (!LoadTexturesFromData(loader, &global_data, id, type, file, &files, rows, cols))
|
2019-08-13 15:00:43 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-08-21 16:33:41 +08:00
|
|
|
if (!LoadTexturesFromData(loader, &global_data, id, type, file, nullptr, rows, cols))
|
2019-08-13 15:00:43 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem)
|
|
|
|
|
{
|
|
|
|
|
GlobalData global_data;
|
|
|
|
|
if (auto path = elem->FirstChildElement(L"path"))
|
|
|
|
|
{
|
|
|
|
|
global_data.path = path->GetText();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
if (auto images = elem->FirstChildElement(L"images"))
|
2019-08-13 15:00:43 +08:00
|
|
|
{
|
2019-08-21 16:54:51 +08:00
|
|
|
for (auto image = images->FirstChildElement(); image; image = image->NextSiblingElement())
|
2019-08-13 15:00:43 +08:00
|
|
|
{
|
|
|
|
|
String id, type, file;
|
2019-08-18 22:49:44 +08:00
|
|
|
Int32 rows = 0, cols = 0;
|
2019-08-13 15:00:43 +08:00
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
if (auto attr = image->Attribute(L"id")) id.assign(attr); // assign() copies attr content
|
|
|
|
|
if (auto attr = image->Attribute(L"type")) type = attr; // operator=() just holds attr pointer
|
|
|
|
|
if (auto attr = image->Attribute(L"file")) file = attr;
|
|
|
|
|
if (auto attr = image->IntAttribute(L"rows")) rows = attr;
|
|
|
|
|
if (auto attr = image->IntAttribute(L"cols")) cols = attr;
|
2019-08-13 15:00:43 +08:00
|
|
|
|
2019-08-21 16:54:51 +08:00
|
|
|
if (file.empty() && !image->NoChildren())
|
2019-08-13 15:00:43 +08:00
|
|
|
{
|
2019-08-18 22:49:44 +08:00
|
|
|
Vector<const WChar*> files_arr;
|
2019-08-21 16:54:51 +08:00
|
|
|
for (auto file = image->FirstChildElement(); file; file = file->NextSiblingElement())
|
2019-08-13 15:00:43 +08:00
|
|
|
{
|
|
|
|
|
if (auto path = file->Attribute(L"path"))
|
|
|
|
|
{
|
|
|
|
|
files_arr.push_back(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-21 16:33:41 +08:00
|
|
|
if (!LoadTexturesFromData(loader, &global_data, &id, &type, &file, &files_arr, rows, cols))
|
2019-08-13 15:00:43 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-08-21 16:33:41 +08:00
|
|
|
if (!LoadTexturesFromData(loader, &global_data, &id, &type, &file, nullptr, rows, cols))
|
2019-08-13 15:00:43 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-13 14:44:37 +08:00
|
|
|
}
|