blob: 9c4eec1e8ae5926092cf4c8754439e785f3085c3 [file] [log] [blame]
Matt Mowere04eee72016-12-31 00:38:57 -06001/*
bigbiffd58ba182020-03-23 10:02:29 -04002 Copyright 2012 to 2020 TeamWin
Matt Mowere04eee72016-12-31 00:38:57 -06003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
Dees_Troy51a0e822012-09-05 15:24:24 -040019// resource.cpp - Source to manage GUI resources
20
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040026
27#include <string>
28#include <sstream>
29#include <iostream>
30#include <iomanip>
Ethan Yonker3fdcda42016-11-30 12:29:37 -060031#include <fcntl.h>
bigbiff673c7ae2020-12-02 19:44:56 -050032#include <ziparchive/zip_archive.h>
33#include <android-base/unique_fd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040034
35extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "../twcommon.h"
Ethan Yonker63e414f2015-02-06 15:44:39 -060037#include "gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038}
bigbiffd58ba182020-03-23 10:02:29 -040039
40#include "../minuitwrp/truetype.hpp"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010041#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
43#include "rapidxml.hpp"
44#include "objects.hpp"
45
46#define TMP_RESOURCE_NAME "/tmp/extract.bin"
47
bigbiff673c7ae2020-12-02 19:44:56 -050048Resource::Resource(xml_node<>* node, ZipArchiveHandle pZip __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -040049{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 if (node && node->first_attribute("name"))
51 mName = node->first_attribute("name")->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040052}
53
bigbiff673c7ae2020-12-02 19:44:56 -050054int Resource::ExtractResource(ZipArchiveHandle pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile)
Dees_Troy51a0e822012-09-05 15:24:24 -040055{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020056 if (!pZip)
57 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 std::string src = folderName + "/" + fileName + fileExtn;
bigbiff673c7ae2020-12-02 19:44:56 -050060 ZipEntry binary_entry;
61 if (FindEntry(pZip, src, &binary_entry) != 0) {
62 android::base::unique_fd fd(
63 open(destFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0666));
64 if (fd == -1) {
65 return -1;
66 }
67 // if (!pZip->ExtractEntry(src, destFile, 0666))
68 int32_t err = ExtractEntryToFile(pZip, &binary_entry, fd);
69 if (err != 0)
70 return -1;
71 } else {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 return -1;
bigbiff673c7ae2020-12-02 19:44:56 -050073 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -050074 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040075}
76
bigbiff673c7ae2020-12-02 19:44:56 -050077void Resource::LoadImage(ZipArchiveHandle pZip, std::string file, gr_surface* surface)
Ethan Yonker63e414f2015-02-06 15:44:39 -060078{
thatb240f4a2016-03-14 01:21:38 +010079 int rc = 0;
Ethan Yonker63e414f2015-02-06 15:44:39 -060080 if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0)
81 {
thatb240f4a2016-03-14 01:21:38 +010082 rc = res_create_surface(TMP_RESOURCE_NAME, surface);
Ethan Yonker63e414f2015-02-06 15:44:39 -060083 unlink(TMP_RESOURCE_NAME);
84 }
85 else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0)
86 {
87 // JPG includes the .jpg extension in the filename so extension should be blank
thatb240f4a2016-03-14 01:21:38 +010088 rc = res_create_surface(TMP_RESOURCE_NAME, surface);
Ethan Yonker63e414f2015-02-06 15:44:39 -060089 unlink(TMP_RESOURCE_NAME);
90 }
91 else if (!pZip)
92 {
93 // File name in xml may have included .png so try without adding .png
thatb240f4a2016-03-14 01:21:38 +010094 rc = res_create_surface(file.c_str(), surface);
Ethan Yonker63e414f2015-02-06 15:44:39 -060095 }
thatb240f4a2016-03-14 01:21:38 +010096 if (rc != 0)
97 LOGINFO("Failed to load image from %s%s, error %d\n", file.c_str(), pZip ? " (zip)" : "", rc);
Ethan Yonker63e414f2015-02-06 15:44:39 -060098}
99
100void Resource::CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect)
101{
102 if (!source) {
103 *destination = NULL;
104 return;
105 }
106 if (get_scale_w() != 0 && get_scale_h() != 0) {
107 float scale_w = get_scale_w(), scale_h = get_scale_h();
108 if (retain_aspect) {
109 if (scale_w < scale_h)
110 scale_h = scale_w;
111 else
112 scale_w = scale_h;
113 }
114 if (res_scale_surface(source, destination, scale_w, scale_h)) {
115 LOGINFO("Error scaling image, using regular size.\n");
116 *destination = source;
117 }
118 } else {
119 *destination = source;
120 }
121}
122
bigbiff673c7ae2020-12-02 19:44:56 -0500123FontResource::FontResource(xml_node<>* node, ZipArchiveHandle pZip)
Dees_Troy51a0e822012-09-05 15:24:24 -0400124 : Resource(node, pZip)
125{
Ethan Yonker74db1572015-10-28 12:44:49 -0500126 origFontSize = 0;
127 origFont = NULL;
128 LoadFont(node, pZip);
129}
130
bigbiff673c7ae2020-12-02 19:44:56 -0500131void FontResource::LoadFont(xml_node<>* node, ZipArchiveHandle pZip)
Ethan Yonker74db1572015-10-28 12:44:49 -0500132{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 std::string file;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200134 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 mFont = NULL;
137 if (!node)
138 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200140 attr = node->first_attribute("filename");
141 if (!attr)
142 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200144 file = attr->value();
145
Matt Mowera8a89d12016-12-30 18:10:37 -0600146 if (file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500148 int font_size = 0;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200149
Ethan Yonker74db1572015-10-28 12:44:49 -0500150 if (origFontSize != 0) {
151 attr = node->first_attribute("scale");
152 if (attr == NULL)
153 return;
154 font_size = origFontSize * atoi(attr->value()) / 100;
155 } else {
156 attr = node->first_attribute("size");
157 if (attr == NULL)
158 return;
159 font_size = scale_theme_min(atoi(attr->value()));
160 origFontSize = font_size;
161 }
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200162
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200163 int dpi = 300;
164
165 attr = node->first_attribute("dpi");
Matt Mowera8a89d12016-12-30 18:10:37 -0600166 if (attr)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200167 dpi = atoi(attr->value());
168
thatb240f4a2016-03-14 01:21:38 +0100169 // we can't use TMP_RESOURCE_NAME here because the ttf subsystem is caching the name and scaling needs to reload the font
170 std::string tmpname = "/tmp/" + file;
171 if (ExtractResource(pZip, "fonts", file, "", tmpname) == 0)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200172 {
bigbiffd58ba182020-03-23 10:02:29 -0400173 mFont = twrpTruetype::gr_ttf_loadFont(tmpname.c_str(), font_size, dpi);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200174 }
175 else
176 {
Dees Troy3454ade2015-01-20 19:21:04 +0000177 file = std::string(TWRES "fonts/") + file;
bigbiffd58ba182020-03-23 10:02:29 -0400178 mFont = twrpTruetype::gr_ttf_loadFont(file.c_str(), font_size, dpi);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200179 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200180 }
181 else
182 {
Ethan Yonker88037f42015-10-04 22:09:08 -0500183 LOGERR("Non-TTF fonts are no longer supported.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400185}
186
Ethan Yonker74db1572015-10-28 12:44:49 -0500187void FontResource::DeleteFont() {
bigbiffd58ba182020-03-23 10:02:29 -0400188 if (mFont) {
189 twrpTruetype::gr_ttf_freeFont(mFont);
190 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500191 mFont = NULL;
bigbiffd58ba182020-03-23 10:02:29 -0400192 if (origFont) {
193 twrpTruetype::gr_ttf_freeFont(origFont);
194 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500195 origFont = NULL;
196}
197
bigbiff673c7ae2020-12-02 19:44:56 -0500198void FontResource::Override(xml_node<>* node, ZipArchiveHandle pZip) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500199 if (!origFont) {
200 origFont = mFont;
201 } else if (mFont) {
bigbiffd58ba182020-03-23 10:02:29 -0400202 twrpTruetype::gr_ttf_freeFont(mFont);
Ethan Yonker74db1572015-10-28 12:44:49 -0500203 mFont = NULL;
204 }
205 LoadFont(node, pZip);
206}
207
Dees_Troy51a0e822012-09-05 15:24:24 -0400208FontResource::~FontResource()
209{
Ethan Yonker74db1572015-10-28 12:44:49 -0500210 DeleteFont();
Dees_Troy51a0e822012-09-05 15:24:24 -0400211}
212
bigbiff673c7ae2020-12-02 19:44:56 -0500213ImageResource::ImageResource(xml_node<>* node, ZipArchiveHandle pZip)
Dees_Troy51a0e822012-09-05 15:24:24 -0400214 : Resource(node, pZip)
215{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 std::string file;
Ethan Yonker63e414f2015-02-06 15:44:39 -0600217 gr_surface temp_surface = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 mSurface = NULL;
Ethan Yonker619a7212014-12-03 16:47:37 -0600220 if (!node) {
221 LOGERR("ImageResource node is NULL\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200222 return;
Ethan Yonker619a7212014-12-03 16:47:37 -0600223 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400224
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200225 if (node->first_attribute("filename"))
226 file = node->first_attribute("filename")->value();
Ethan Yonker63e414f2015-02-06 15:44:39 -0600227 else {
228 LOGERR("No filename specified for image resource.\n");
229 return;
230 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400231
that5267a212015-05-06 23:45:57 +0200232 bool retain_aspect = (node->first_attribute("retainaspect") != NULL);
233 // the value does not matter, if retainaspect is present, we assume that we want to retain it
Ethan Yonker63e414f2015-02-06 15:44:39 -0600234 LoadImage(pZip, file, &temp_surface);
235 CheckAndScaleImage(temp_surface, &mSurface, retain_aspect);
Dees_Troy51a0e822012-09-05 15:24:24 -0400236}
237
238ImageResource::~ImageResource()
239{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 if (mSurface)
241 res_free_surface(mSurface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400242}
243
bigbiff673c7ae2020-12-02 19:44:56 -0500244AnimationResource::AnimationResource(xml_node<>* node, ZipArchiveHandle pZip)
Dees_Troy51a0e822012-09-05 15:24:24 -0400245 : Resource(node, pZip)
246{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 std::string file;
248 int fileNum = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 if (!node)
251 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 if (node->first_attribute("filename"))
254 file = node->first_attribute("filename")->value();
Ethan Yonker63e414f2015-02-06 15:44:39 -0600255 else {
256 LOGERR("No filename specified for image resource.\n");
257 return;
258 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400259
that5267a212015-05-06 23:45:57 +0200260 bool retain_aspect = (node->first_attribute("retainaspect") != NULL);
261 // the value does not matter, if retainaspect is present, we assume that we want to retain it
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 for (;;)
263 {
264 std::ostringstream fileName;
265 fileName << file << std::setfill ('0') << std::setw (3) << fileNum;
Dees_Troy51a0e822012-09-05 15:24:24 -0400266
Ethan Yonker63e414f2015-02-06 15:44:39 -0600267 gr_surface surface, temp_surface = NULL;
268 LoadImage(pZip, fileName.str(), &temp_surface);
269 CheckAndScaleImage(temp_surface, &surface, retain_aspect);
270 if (surface) {
271 mSurfaces.push_back(surface);
272 fileNum++;
273 } else
274 break; // Done loading animation images
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200275 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400276}
277
278AnimationResource::~AnimationResource()
279{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200280 std::vector<gr_surface>::iterator it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400281
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200282 for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it)
283 res_free_surface(*it);
284
285 mSurfaces.clear();
Dees_Troy51a0e822012-09-05 15:24:24 -0400286}
287
that74ac6062015-03-04 22:39:34 +0100288FontResource* ResourceManager::FindFont(const std::string& name) const
Dees_Troy51a0e822012-09-05 15:24:24 -0400289{
that74ac6062015-03-04 22:39:34 +0100290 for (std::vector<FontResource*>::const_iterator it = mFonts.begin(); it != mFonts.end(); ++it)
291 if (name == (*it)->GetName())
292 return *it;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400294}
295
that74ac6062015-03-04 22:39:34 +0100296ImageResource* ResourceManager::FindImage(const std::string& name) const
Dees_Troy51a0e822012-09-05 15:24:24 -0400297{
that74ac6062015-03-04 22:39:34 +0100298 for (std::vector<ImageResource*>::const_iterator it = mImages.begin(); it != mImages.end(); ++it)
299 if (name == (*it)->GetName())
300 return *it;
301 return NULL;
302}
303
304AnimationResource* ResourceManager::FindAnimation(const std::string& name) const
305{
306 for (std::vector<AnimationResource*>::const_iterator it = mAnimations.begin(); it != mAnimations.end(); ++it)
307 if (name == (*it)->GetName())
308 return *it;
309 return NULL;
310}
311
thatb2e8f672015-03-05 20:25:39 +0100312std::string ResourceManager::FindString(const std::string& name) const
313{
Ethan Yonker58f21322018-08-24 11:17:36 -0500314 //if (this != NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500315 std::map<std::string, string_resource_struct>::const_iterator it = mStrings.find(name);
316 if (it != mStrings.end())
317 return it->second.value;
318 LOGERR("String resource '%s' not found. No default value.\n", name.c_str());
319 PageManager::AddStringResource("NO DEFAULT", name, "[" + name + ("]"));
Ethan Yonker58f21322018-08-24 11:17:36 -0500320 /*} else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500321 LOGINFO("String resources not loaded when looking for '%s'. No default value.\n", name.c_str());
Ethan Yonker58f21322018-08-24 11:17:36 -0500322 }*/
thatb2e8f672015-03-05 20:25:39 +0100323 return "[" + name + ("]");
324}
325
Ethan Yonker74db1572015-10-28 12:44:49 -0500326std::string ResourceManager::FindString(const std::string& name, const std::string& default_string) const
327{
Ethan Yonker58f21322018-08-24 11:17:36 -0500328 //if (this != NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500329 std::map<std::string, string_resource_struct>::const_iterator it = mStrings.find(name);
330 if (it != mStrings.end())
331 return it->second.value;
332 LOGERR("String resource '%s' not found. Using default value.\n", name.c_str());
333 PageManager::AddStringResource("DEFAULT", name, default_string);
Ethan Yonker58f21322018-08-24 11:17:36 -0500334 /*} else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500335 LOGINFO("String resources not loaded when looking for '%s'. Using default value.\n", name.c_str());
Ethan Yonker58f21322018-08-24 11:17:36 -0500336 }*/
Ethan Yonker74db1572015-10-28 12:44:49 -0500337 return default_string;
338}
339
340void ResourceManager::DumpStrings() const
341{
Ethan Yonker58f21322018-08-24 11:17:36 -0500342 /*if (this == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500343 gui_print("No string resources\n");
344 return;
Ethan Yonker58f21322018-08-24 11:17:36 -0500345 }*/
Ethan Yonker74db1572015-10-28 12:44:49 -0500346 std::map<std::string, string_resource_struct>::const_iterator it;
347 gui_print("Dumping all strings:\n");
348 for (it = mStrings.begin(); it != mStrings.end(); it++)
349 gui_print("source: %s: '%s' = '%s'\n", it->second.source.c_str(), it->first.c_str(), it->second.value.c_str());
350 gui_print("Done dumping strings\n");
351}
352
that74ac6062015-03-04 22:39:34 +0100353ResourceManager::ResourceManager()
354{
Ethan Yonker780cd392014-07-21 15:24:39 -0500355}
356
Ethan Yonker74db1572015-10-28 12:44:49 -0500357void ResourceManager::AddStringResource(std::string resource_source, std::string resource_name, std::string value)
358{
359 string_resource_struct res;
360 res.source = resource_source;
361 res.value = value;
362 mStrings[resource_name] = res;
363}
364
bigbiff673c7ae2020-12-02 19:44:56 -0500365void ResourceManager::LoadResources(xml_node<>* resList, ZipArchiveHandle pZip, std::string resource_source)
Ethan Yonker780cd392014-07-21 15:24:39 -0500366{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200367 if (!resList)
368 return;
that0f425062015-03-04 23:05:00 +0100369
370 for (xml_node<>* child = resList->first_node(); child; child = child->next_sibling())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200371 {
that0f425062015-03-04 23:05:00 +0100372 std::string type = child->name();
373 if (type == "resource") {
374 // legacy format : <resource type="...">
375 xml_attribute<>* attr = child->first_attribute("type");
376 type = attr ? attr->value() : "*unspecified*";
377 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400378
that74ac6062015-03-04 22:39:34 +0100379 bool error = false;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200380 if (type == "font")
381 {
that74ac6062015-03-04 22:39:34 +0100382 FontResource* res = new FontResource(child, pZip);
Ethan Yonker58f21322018-08-24 11:17:36 -0500383 if (res && res->GetResource())
that74ac6062015-03-04 22:39:34 +0100384 mFonts.push_back(res);
385 else {
386 error = true;
387 delete res;
388 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200389 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500390 else if (type == "fontoverride")
391 {
392 if (mFonts.size() != 0 && child && child->first_attribute("name")) {
393 string FontName = child->first_attribute("name")->value();
394 size_t font_count = mFonts.size(), i;
395 bool found = false;
396
397 for (i = 0; i < font_count; i++) {
398 if (mFonts[i]->GetName() == FontName) {
399 mFonts[i]->Override(child, pZip);
400 found = true;
401 break;
402 }
403 }
404 if (!found) {
405 LOGERR("Unable to locate font '%s' for override.\n", FontName.c_str());
406 }
407 } else if (mFonts.size() != 0)
408 LOGERR("Unable to locate font name for type fontoverride.\n");
409 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200410 else if (type == "image")
411 {
that5267a212015-05-06 23:45:57 +0200412 ImageResource* res = new ImageResource(child, pZip);
Ethan Yonker58f21322018-08-24 11:17:36 -0500413 if (res && res->GetResource())
that74ac6062015-03-04 22:39:34 +0100414 mImages.push_back(res);
415 else {
416 error = true;
417 delete res;
418 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200419 }
420 else if (type == "animation")
421 {
that5267a212015-05-06 23:45:57 +0200422 AnimationResource* res = new AnimationResource(child, pZip);
Ethan Yonker58f21322018-08-24 11:17:36 -0500423 if (res && res->GetResourceCount())
that74ac6062015-03-04 22:39:34 +0100424 mAnimations.push_back(res);
425 else {
426 error = true;
427 delete res;
428 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200429 }
thatb2e8f672015-03-05 20:25:39 +0100430 else if (type == "string")
431 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500432 if (xml_attribute<>* attr = child->first_attribute("name")) {
433 string_resource_struct res;
434 res.source = resource_source;
435 res.value = child->value();
436 mStrings[attr->value()] = res;
437 } else
thatb2e8f672015-03-05 20:25:39 +0100438 error = true;
439 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200440 else
441 {
442 LOGERR("Resource type (%s) not supported.\n", type.c_str());
that74ac6062015-03-04 22:39:34 +0100443 error = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200444 }
thatf6ed8fc2015-02-14 20:23:16 +0100445
that74ac6062015-03-04 22:39:34 +0100446 if (error)
thatf74ac872015-01-18 12:00:02 +0100447 {
448 std::string res_name;
449 if (child->first_attribute("name"))
450 res_name = child->first_attribute("name")->value();
451 if (res_name.empty() && child->first_attribute("filename"))
452 res_name = child->first_attribute("filename")->value();
453
454 if (!res_name.empty()) {
455 LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str());
456 } else
457 LOGERR("Resource type (%s) failed to load\n", type.c_str());
thatf74ac872015-01-18 12:00:02 +0100458 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200459 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400460}
461
462ResourceManager::~ResourceManager()
463{
that74ac6062015-03-04 22:39:34 +0100464 for (std::vector<FontResource*>::iterator it = mFonts.begin(); it != mFonts.end(); ++it)
465 delete *it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400466
that74ac6062015-03-04 22:39:34 +0100467 for (std::vector<ImageResource*>::iterator it = mImages.begin(); it != mImages.end(); ++it)
468 delete *it;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200469
that74ac6062015-03-04 22:39:34 +0100470 for (std::vector<AnimationResource*>::iterator it = mAnimations.begin(); it != mAnimations.end(); ++it)
471 delete *it;
Dees_Troy51a0e822012-09-05 15:24:24 -0400472}