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.
|
|
|
|
|
|
|
|
|
|
#include "ResLoader.h"
|
2019-07-31 00:09:24 +08:00
|
|
|
#include "../base/logs.h"
|
2019-03-31 01:37:06 +08:00
|
|
|
#include "../2d/Image.h"
|
|
|
|
|
#include "../2d/Frames.h"
|
2019-07-31 00:09:24 +08:00
|
|
|
#include "../2d/GifImage.h"
|
|
|
|
|
#include "FileUtil.h"
|
|
|
|
|
#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-01 13:07:48 +08:00
|
|
|
namespace __res_loader_09
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-01 13:07:48 +08:00
|
|
|
struct GlobalData
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-07-31 00:09:24 +08:00
|
|
|
String path;
|
2019-08-01 13:07:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void LoadImageFromData(ResLoader* loader, GlobalData const& global_data, Json const& image_data);
|
|
|
|
|
bool CompareJsonWithString(Json const& data, const wchar_t* key, const wchar_t* value);
|
|
|
|
|
|
|
|
|
|
void LoadJsonData(ResLoader* loader, Json const& json_data)
|
|
|
|
|
{
|
|
|
|
|
GlobalData global_data;
|
2019-07-31 00:09:24 +08:00
|
|
|
if (json_data.count(L"path"))
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-01 13:07:48 +08:00
|
|
|
global_data.path = json_data[L"path"];
|
2019-07-31 00:09:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (json_data.count(L"images"))
|
|
|
|
|
{
|
|
|
|
|
for (const auto& image : json_data[L"images"])
|
|
|
|
|
{
|
2019-08-01 13:07:48 +08:00
|
|
|
LoadImageFromData(loader, global_data, image);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LoadImageFromData(ResLoader* loader, GlobalData const& global_data, Json const& image_data)
|
|
|
|
|
{
|
|
|
|
|
String id = image_data[L"id"];
|
|
|
|
|
|
|
|
|
|
if (image_data.count(L"file"))
|
|
|
|
|
{
|
|
|
|
|
String file = image_data[L"file"];
|
|
|
|
|
|
|
|
|
|
// Gif image
|
|
|
|
|
if (CompareJsonWithString(image_data, L"type", L"gif"))
|
|
|
|
|
{
|
|
|
|
|
loader->AddGifImage(id, Resource(global_data.path + file));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-31 00:09:24 +08:00
|
|
|
|
2019-08-01 13:07:48 +08:00
|
|
|
if (!file.empty())
|
|
|
|
|
{
|
|
|
|
|
if (image_data.count(L"rows") || image_data.count(L"cols"))
|
|
|
|
|
{
|
|
|
|
|
// Image slices
|
|
|
|
|
int rows = 1, cols = 1;
|
|
|
|
|
if (image_data.count(L"rows")) rows = image_data[L"rows"];
|
|
|
|
|
if (image_data.count(L"cols")) cols = image_data[L"cols"];
|
|
|
|
|
loader->AddFrames(id, Resource(global_data.path + file), cols, rows);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-31 00:09:24 +08:00
|
|
|
else
|
2019-08-01 13:07:48 +08:00
|
|
|
{
|
|
|
|
|
// Simple image
|
|
|
|
|
loader->AddImage(id, Resource(global_data.path + file));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-31 00:09:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-01 13:07:48 +08:00
|
|
|
|
|
|
|
|
// Frames
|
|
|
|
|
if (image_data.count(L"files"))
|
2019-07-31 00:09:24 +08:00
|
|
|
{
|
2019-08-01 13:07:48 +08:00
|
|
|
Array<ImagePtr> images;
|
|
|
|
|
images.reserve(image_data[L"files"].size());
|
|
|
|
|
for (const auto& file : image_data[L"files"])
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-01 13:07:48 +08:00
|
|
|
auto filePath = file.as_string();
|
|
|
|
|
ImagePtr image = new Image(global_data.path + file.as_string());
|
|
|
|
|
if (image->IsValid())
|
|
|
|
|
{
|
|
|
|
|
images.push_back(image);
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2019-08-01 13:07:48 +08:00
|
|
|
loader->AddFrames(id, images);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CompareJsonWithString(Json const& data, const wchar_t* key, const wchar_t* value)
|
|
|
|
|
{
|
|
|
|
|
return data.count(key) && data[key].as_string() == value;
|
2019-07-31 00:09:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResLoader::LoadFromJsonFile(String const& file_path)
|
|
|
|
|
{
|
|
|
|
|
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-02 13:57:17 +08:00
|
|
|
KGE_WARNING_LOG(L"ResLoader::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-02 13:57:17 +08:00
|
|
|
KGE_WARNING_LOG(L"ResLoader::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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResLoader::LoadFromJson(Json const& json_data)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
String version = json_data[L"version"];
|
|
|
|
|
if (version.empty() || version == L"0.9")
|
|
|
|
|
{
|
2019-08-01 13:07:48 +08:00
|
|
|
__res_loader_09::LoadJsonData(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-02 13:57:17 +08:00
|
|
|
KGE_WARNING_LOG(L"ResLoader::LoadFromJson failed: JSON data is invalid. (%s)", string_to_wide(e.what()).c_str());
|
2019-07-31 00:09:24 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResLoader::AddImage(String const& id, Resource const& image)
|
|
|
|
|
{
|
|
|
|
|
ImagePtr ptr = new (std::nothrow) Image;
|
|
|
|
|
if (ptr)
|
|
|
|
|
{
|
2019-07-31 00:09:24 +08:00
|
|
|
if (ptr->Load(image))
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-07-31 00:09:24 +08:00
|
|
|
return AddImage(id, ptr);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 15:27:18 +08:00
|
|
|
bool ResLoader::AddImage(String const & id, ImagePtr image)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (image)
|
|
|
|
|
{
|
|
|
|
|
res_.insert(std::make_pair(id, image));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 00:09:24 +08:00
|
|
|
bool ResLoader::AddGifImage(String const& id, Resource const& image)
|
|
|
|
|
{
|
|
|
|
|
GifImagePtr ptr = new (std::nothrow) GifImage;
|
|
|
|
|
if (ptr)
|
|
|
|
|
{
|
|
|
|
|
if (ptr->Load(image))
|
|
|
|
|
{
|
|
|
|
|
return AddGifImage(id, ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResLoader::AddGifImage(String const& id, GifImagePtr image)
|
|
|
|
|
{
|
|
|
|
|
if (image)
|
|
|
|
|
{
|
|
|
|
|
res_.insert(std::make_pair(id, image));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 13:07:48 +08:00
|
|
|
bool ResLoader::AddFrames(String const& id, Array<Resource> const& images)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (images.empty())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
Array<ImagePtr> image_arr;
|
|
|
|
|
image_arr.reserve(images.size());
|
|
|
|
|
|
|
|
|
|
for (const auto& image : images)
|
|
|
|
|
{
|
|
|
|
|
ImagePtr ptr = new (std::nothrow) Image;
|
|
|
|
|
if (ptr)
|
|
|
|
|
{
|
2019-07-31 00:09:24 +08:00
|
|
|
if (ptr->Load(image))
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
image_arr.push_back(ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!image_arr.empty())
|
|
|
|
|
{
|
|
|
|
|
FramesPtr frames = new (std::nothrow) Frames(image_arr);
|
2019-08-01 13:07:48 +08:00
|
|
|
return AddFrames(id, frames);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 15:27:18 +08:00
|
|
|
size_t ResLoader::AddFrames(String const& id, Array<ImagePtr> const& images)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (images.empty())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
FramesPtr frames = new (std::nothrow) Frames(images);
|
2019-08-01 13:07:48 +08:00
|
|
|
return AddFrames(id, frames);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-05-03 15:27:18 +08:00
|
|
|
size_t ResLoader::AddFrames(String const & id, Resource const & image, int cols, int rows)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (cols <= 0 || rows <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
ImagePtr raw = new (std::nothrow) Image;
|
2019-07-31 00:09:24 +08:00
|
|
|
if (!raw || !raw->Load(image))
|
2019-08-01 13:07:48 +08:00
|
|
|
return false;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
|
|
|
float raw_width = raw->GetSourceWidth();
|
|
|
|
|
float raw_height = raw->GetSourceHeight();
|
|
|
|
|
float width = raw_width / cols;
|
|
|
|
|
float height = raw_height / rows;
|
|
|
|
|
|
|
|
|
|
Array<ImagePtr> image_arr;
|
|
|
|
|
image_arr.reserve(rows * cols);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < rows; i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j = 0; j < cols; j++)
|
|
|
|
|
{
|
|
|
|
|
ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap());
|
|
|
|
|
if (ptr)
|
|
|
|
|
{
|
2019-08-01 13:07:48 +08:00
|
|
|
ptr->Crop(Rect{ j * width, i * height, width, height });
|
2019-03-31 01:37:06 +08:00
|
|
|
image_arr.push_back(ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FramesPtr frames = new (std::nothrow) Frames(image_arr);
|
2019-08-01 13:07:48 +08:00
|
|
|
return AddFrames(id, frames);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-05-03 15:27:18 +08:00
|
|
|
size_t ResLoader::AddFrames(String const & id, Resource const & image, Array<Rect> const & crop_rects)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
ImagePtr raw = new (std::nothrow) Image;
|
2019-07-31 00:09:24 +08:00
|
|
|
if (!raw || !raw->Load(image))
|
2019-03-31 01:37:06 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
Array<ImagePtr> image_arr;
|
|
|
|
|
image_arr.reserve(crop_rects.size());
|
|
|
|
|
|
|
|
|
|
for (const auto& rect : crop_rects)
|
|
|
|
|
{
|
|
|
|
|
ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap());
|
|
|
|
|
if (ptr)
|
|
|
|
|
{
|
|
|
|
|
ptr->Crop(rect);
|
|
|
|
|
image_arr.push_back(ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FramesPtr frames = new (std::nothrow) Frames(image_arr);
|
2019-08-01 13:07:48 +08:00
|
|
|
return AddFrames(id, frames);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-01 13:07:48 +08:00
|
|
|
size_t ResLoader::AddFrames(String const & id, FramesPtr frames)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (frames)
|
|
|
|
|
{
|
|
|
|
|
res_.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-05-03 15:27:18 +08:00
|
|
|
bool ResLoader::AddObj(String const& id, ObjectPtr obj)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
res_.insert(std::make_pair(id, obj));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImagePtr ResLoader::GetImage(String const & id) const
|
|
|
|
|
{
|
|
|
|
|
return Get<Image>(id);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 00:09:24 +08:00
|
|
|
GifImagePtr ResLoader::GetGifImage(String const& id) const
|
|
|
|
|
{
|
|
|
|
|
return Get<GifImage>(id);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 01:37:06 +08:00
|
|
|
FramesPtr ResLoader::GetFrames(String const & id) const
|
|
|
|
|
{
|
|
|
|
|
return Get<Frames>(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ObjectPtr ResLoader::GetObj(String const & id) const
|
|
|
|
|
{
|
|
|
|
|
return Get<Object>(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResLoader::Delete(String const & id)
|
|
|
|
|
{
|
|
|
|
|
res_.erase(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResLoader::Destroy()
|
|
|
|
|
{
|
|
|
|
|
res_.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 13:07:48 +08:00
|
|
|
ResLoader::ResLoader()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResLoader::~ResLoader()
|
|
|
|
|
{
|
|
|
|
|
Destroy();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|