Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 1 | /* |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 2 | Copyright 2012 to 2020 TeamWin |
Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 3 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | // 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | #include <unistd.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 26 | |
| 27 | #include <string> |
| 28 | #include <sstream> |
| 29 | #include <iostream> |
| 30 | #include <iomanip> |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 31 | #include <fcntl.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 32 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 33 | #include "zipwrap.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 35 | #include "../twcommon.h" |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 36 | #include "gui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | } |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 38 | |
| 39 | #include "../minuitwrp/truetype.hpp" |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 40 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
| 42 | #include "rapidxml.hpp" |
| 43 | #include "objects.hpp" |
| 44 | |
| 45 | #define TMP_RESOURCE_NAME "/tmp/extract.bin" |
| 46 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 47 | Resource::Resource(xml_node<>* node, ZipWrap* pZip __unused) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 48 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 49 | if (node && node->first_attribute("name")) |
| 50 | mName = node->first_attribute("name")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 53 | int Resource::ExtractResource(ZipWrap* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 55 | if (!pZip) |
| 56 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 57 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 58 | std::string src = folderName + "/" + fileName + fileExtn; |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 59 | if (!pZip->ExtractEntry(src, destFile, 0666)) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 60 | return -1; |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 61 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 64 | void Resource::LoadImage(ZipWrap* pZip, std::string file, gr_surface* surface) |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 65 | { |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 66 | int rc = 0; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 67 | if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0) |
| 68 | { |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 69 | rc = res_create_surface(TMP_RESOURCE_NAME, surface); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 70 | unlink(TMP_RESOURCE_NAME); |
| 71 | } |
| 72 | else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0) |
| 73 | { |
| 74 | // JPG includes the .jpg extension in the filename so extension should be blank |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 75 | rc = res_create_surface(TMP_RESOURCE_NAME, surface); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 76 | unlink(TMP_RESOURCE_NAME); |
| 77 | } |
| 78 | else if (!pZip) |
| 79 | { |
| 80 | // File name in xml may have included .png so try without adding .png |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 81 | rc = res_create_surface(file.c_str(), surface); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 82 | } |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 83 | if (rc != 0) |
| 84 | LOGINFO("Failed to load image from %s%s, error %d\n", file.c_str(), pZip ? " (zip)" : "", rc); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void Resource::CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect) |
| 88 | { |
| 89 | if (!source) { |
| 90 | *destination = NULL; |
| 91 | return; |
| 92 | } |
| 93 | if (get_scale_w() != 0 && get_scale_h() != 0) { |
| 94 | float scale_w = get_scale_w(), scale_h = get_scale_h(); |
| 95 | if (retain_aspect) { |
| 96 | if (scale_w < scale_h) |
| 97 | scale_h = scale_w; |
| 98 | else |
| 99 | scale_w = scale_h; |
| 100 | } |
| 101 | if (res_scale_surface(source, destination, scale_w, scale_h)) { |
| 102 | LOGINFO("Error scaling image, using regular size.\n"); |
| 103 | *destination = source; |
| 104 | } |
| 105 | } else { |
| 106 | *destination = source; |
| 107 | } |
| 108 | } |
| 109 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 110 | FontResource::FontResource(xml_node<>* node, ZipWrap* pZip) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 111 | : Resource(node, pZip) |
| 112 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 113 | origFontSize = 0; |
| 114 | origFont = NULL; |
| 115 | LoadFont(node, pZip); |
| 116 | } |
| 117 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 118 | void FontResource::LoadFont(xml_node<>* node, ZipWrap* pZip) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 119 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | std::string file; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 121 | xml_attribute<>* attr; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | mFont = NULL; |
| 124 | if (!node) |
| 125 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 126 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 127 | attr = node->first_attribute("filename"); |
| 128 | if (!attr) |
| 129 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 130 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 131 | file = attr->value(); |
| 132 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 133 | if (file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 134 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 135 | int font_size = 0; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 136 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 137 | if (origFontSize != 0) { |
| 138 | attr = node->first_attribute("scale"); |
| 139 | if (attr == NULL) |
| 140 | return; |
| 141 | font_size = origFontSize * atoi(attr->value()) / 100; |
| 142 | } else { |
| 143 | attr = node->first_attribute("size"); |
| 144 | if (attr == NULL) |
| 145 | return; |
| 146 | font_size = scale_theme_min(atoi(attr->value())); |
| 147 | origFontSize = font_size; |
| 148 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 149 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 150 | int dpi = 300; |
| 151 | |
| 152 | attr = node->first_attribute("dpi"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 153 | if (attr) |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 154 | dpi = atoi(attr->value()); |
| 155 | |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 156 | // we can't use TMP_RESOURCE_NAME here because the ttf subsystem is caching the name and scaling needs to reload the font |
| 157 | std::string tmpname = "/tmp/" + file; |
| 158 | if (ExtractResource(pZip, "fonts", file, "", tmpname) == 0) |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 159 | { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 160 | mFont = twrpTruetype::gr_ttf_loadFont(tmpname.c_str(), font_size, dpi); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 161 | } |
| 162 | else |
| 163 | { |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 164 | file = std::string(TWRES "fonts/") + file; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 165 | mFont = twrpTruetype::gr_ttf_loadFont(file.c_str(), font_size, dpi); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 166 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 167 | } |
| 168 | else |
| 169 | { |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 170 | LOGERR("Non-TTF fonts are no longer supported.\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 171 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 174 | void FontResource::DeleteFont() { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 175 | if (mFont) { |
| 176 | twrpTruetype::gr_ttf_freeFont(mFont); |
| 177 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 178 | mFont = NULL; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 179 | if (origFont) { |
| 180 | twrpTruetype::gr_ttf_freeFont(origFont); |
| 181 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 182 | origFont = NULL; |
| 183 | } |
| 184 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 185 | void FontResource::Override(xml_node<>* node, ZipWrap* pZip) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 186 | if (!origFont) { |
| 187 | origFont = mFont; |
| 188 | } else if (mFont) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 189 | twrpTruetype::gr_ttf_freeFont(mFont); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 190 | mFont = NULL; |
| 191 | } |
| 192 | LoadFont(node, pZip); |
| 193 | } |
| 194 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 195 | FontResource::~FontResource() |
| 196 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 197 | DeleteFont(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 200 | ImageResource::ImageResource(xml_node<>* node, ZipWrap* pZip) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 201 | : Resource(node, pZip) |
| 202 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 203 | std::string file; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 204 | gr_surface temp_surface = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 205 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 206 | mSurface = NULL; |
Ethan Yonker | 619a721 | 2014-12-03 16:47:37 -0600 | [diff] [blame] | 207 | if (!node) { |
| 208 | LOGERR("ImageResource node is NULL\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 209 | return; |
Ethan Yonker | 619a721 | 2014-12-03 16:47:37 -0600 | [diff] [blame] | 210 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 211 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 212 | if (node->first_attribute("filename")) |
| 213 | file = node->first_attribute("filename")->value(); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 214 | else { |
| 215 | LOGERR("No filename specified for image resource.\n"); |
| 216 | return; |
| 217 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 218 | |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 219 | bool retain_aspect = (node->first_attribute("retainaspect") != NULL); |
| 220 | // the value does not matter, if retainaspect is present, we assume that we want to retain it |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 221 | LoadImage(pZip, file, &temp_surface); |
| 222 | CheckAndScaleImage(temp_surface, &mSurface, retain_aspect); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | ImageResource::~ImageResource() |
| 226 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 227 | if (mSurface) |
| 228 | res_free_surface(mSurface); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 231 | AnimationResource::AnimationResource(xml_node<>* node, ZipWrap* pZip) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 232 | : Resource(node, pZip) |
| 233 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 234 | std::string file; |
| 235 | int fileNum = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 236 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 237 | if (!node) |
| 238 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 239 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 240 | if (node->first_attribute("filename")) |
| 241 | file = node->first_attribute("filename")->value(); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 242 | else { |
| 243 | LOGERR("No filename specified for image resource.\n"); |
| 244 | return; |
| 245 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 246 | |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 247 | bool retain_aspect = (node->first_attribute("retainaspect") != NULL); |
| 248 | // the value does not matter, if retainaspect is present, we assume that we want to retain it |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 249 | for (;;) |
| 250 | { |
| 251 | std::ostringstream fileName; |
| 252 | fileName << file << std::setfill ('0') << std::setw (3) << fileNum; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 253 | |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 254 | gr_surface surface, temp_surface = NULL; |
| 255 | LoadImage(pZip, fileName.str(), &temp_surface); |
| 256 | CheckAndScaleImage(temp_surface, &surface, retain_aspect); |
| 257 | if (surface) { |
| 258 | mSurfaces.push_back(surface); |
| 259 | fileNum++; |
| 260 | } else |
| 261 | break; // Done loading animation images |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 262 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | AnimationResource::~AnimationResource() |
| 266 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 267 | std::vector<gr_surface>::iterator it; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 268 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 269 | for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it) |
| 270 | res_free_surface(*it); |
| 271 | |
| 272 | mSurfaces.clear(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 273 | } |
| 274 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 275 | FontResource* ResourceManager::FindFont(const std::string& name) const |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 276 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 277 | for (std::vector<FontResource*>::const_iterator it = mFonts.begin(); it != mFonts.end(); ++it) |
| 278 | if (name == (*it)->GetName()) |
| 279 | return *it; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 280 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 281 | } |
| 282 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 283 | ImageResource* ResourceManager::FindImage(const std::string& name) const |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 284 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 285 | for (std::vector<ImageResource*>::const_iterator it = mImages.begin(); it != mImages.end(); ++it) |
| 286 | if (name == (*it)->GetName()) |
| 287 | return *it; |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | AnimationResource* ResourceManager::FindAnimation(const std::string& name) const |
| 292 | { |
| 293 | for (std::vector<AnimationResource*>::const_iterator it = mAnimations.begin(); it != mAnimations.end(); ++it) |
| 294 | if (name == (*it)->GetName()) |
| 295 | return *it; |
| 296 | return NULL; |
| 297 | } |
| 298 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 299 | std::string ResourceManager::FindString(const std::string& name) const |
| 300 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 301 | //if (this != NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 302 | std::map<std::string, string_resource_struct>::const_iterator it = mStrings.find(name); |
| 303 | if (it != mStrings.end()) |
| 304 | return it->second.value; |
| 305 | LOGERR("String resource '%s' not found. No default value.\n", name.c_str()); |
| 306 | PageManager::AddStringResource("NO DEFAULT", name, "[" + name + ("]")); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 307 | /*} else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 308 | LOGINFO("String resources not loaded when looking for '%s'. No default value.\n", name.c_str()); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 309 | }*/ |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 310 | return "[" + name + ("]"); |
| 311 | } |
| 312 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 313 | std::string ResourceManager::FindString(const std::string& name, const std::string& default_string) const |
| 314 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 315 | //if (this != NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 316 | std::map<std::string, string_resource_struct>::const_iterator it = mStrings.find(name); |
| 317 | if (it != mStrings.end()) |
| 318 | return it->second.value; |
| 319 | LOGERR("String resource '%s' not found. Using default value.\n", name.c_str()); |
| 320 | PageManager::AddStringResource("DEFAULT", name, default_string); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 321 | /*} else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 322 | LOGINFO("String resources not loaded when looking for '%s'. Using default value.\n", name.c_str()); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 323 | }*/ |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 324 | return default_string; |
| 325 | } |
| 326 | |
| 327 | void ResourceManager::DumpStrings() const |
| 328 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 329 | /*if (this == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 330 | gui_print("No string resources\n"); |
| 331 | return; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 332 | }*/ |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 333 | std::map<std::string, string_resource_struct>::const_iterator it; |
| 334 | gui_print("Dumping all strings:\n"); |
| 335 | for (it = mStrings.begin(); it != mStrings.end(); it++) |
| 336 | gui_print("source: %s: '%s' = '%s'\n", it->second.source.c_str(), it->first.c_str(), it->second.value.c_str()); |
| 337 | gui_print("Done dumping strings\n"); |
| 338 | } |
| 339 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 340 | ResourceManager::ResourceManager() |
| 341 | { |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 344 | void ResourceManager::AddStringResource(std::string resource_source, std::string resource_name, std::string value) |
| 345 | { |
| 346 | string_resource_struct res; |
| 347 | res.source = resource_source; |
| 348 | res.value = value; |
| 349 | mStrings[resource_name] = res; |
| 350 | } |
| 351 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 352 | void ResourceManager::LoadResources(xml_node<>* resList, ZipWrap* pZip, std::string resource_source) |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 353 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 354 | if (!resList) |
| 355 | return; |
that | 0f42506 | 2015-03-04 23:05:00 +0100 | [diff] [blame] | 356 | |
| 357 | for (xml_node<>* child = resList->first_node(); child; child = child->next_sibling()) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 358 | { |
that | 0f42506 | 2015-03-04 23:05:00 +0100 | [diff] [blame] | 359 | std::string type = child->name(); |
| 360 | if (type == "resource") { |
| 361 | // legacy format : <resource type="..."> |
| 362 | xml_attribute<>* attr = child->first_attribute("type"); |
| 363 | type = attr ? attr->value() : "*unspecified*"; |
| 364 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 365 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 366 | bool error = false; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 367 | if (type == "font") |
| 368 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 369 | FontResource* res = new FontResource(child, pZip); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 370 | if (res && res->GetResource()) |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 371 | mFonts.push_back(res); |
| 372 | else { |
| 373 | error = true; |
| 374 | delete res; |
| 375 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 376 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 377 | else if (type == "fontoverride") |
| 378 | { |
| 379 | if (mFonts.size() != 0 && child && child->first_attribute("name")) { |
| 380 | string FontName = child->first_attribute("name")->value(); |
| 381 | size_t font_count = mFonts.size(), i; |
| 382 | bool found = false; |
| 383 | |
| 384 | for (i = 0; i < font_count; i++) { |
| 385 | if (mFonts[i]->GetName() == FontName) { |
| 386 | mFonts[i]->Override(child, pZip); |
| 387 | found = true; |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | if (!found) { |
| 392 | LOGERR("Unable to locate font '%s' for override.\n", FontName.c_str()); |
| 393 | } |
| 394 | } else if (mFonts.size() != 0) |
| 395 | LOGERR("Unable to locate font name for type fontoverride.\n"); |
| 396 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 397 | else if (type == "image") |
| 398 | { |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 399 | ImageResource* res = new ImageResource(child, pZip); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 400 | if (res && res->GetResource()) |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 401 | mImages.push_back(res); |
| 402 | else { |
| 403 | error = true; |
| 404 | delete res; |
| 405 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 406 | } |
| 407 | else if (type == "animation") |
| 408 | { |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 409 | AnimationResource* res = new AnimationResource(child, pZip); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 410 | if (res && res->GetResourceCount()) |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 411 | mAnimations.push_back(res); |
| 412 | else { |
| 413 | error = true; |
| 414 | delete res; |
| 415 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 416 | } |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 417 | else if (type == "string") |
| 418 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 419 | if (xml_attribute<>* attr = child->first_attribute("name")) { |
| 420 | string_resource_struct res; |
| 421 | res.source = resource_source; |
| 422 | res.value = child->value(); |
| 423 | mStrings[attr->value()] = res; |
| 424 | } else |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 425 | error = true; |
| 426 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 427 | else |
| 428 | { |
| 429 | LOGERR("Resource type (%s) not supported.\n", type.c_str()); |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 430 | error = true; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 431 | } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 432 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 433 | if (error) |
that | f74ac87 | 2015-01-18 12:00:02 +0100 | [diff] [blame] | 434 | { |
| 435 | std::string res_name; |
| 436 | if (child->first_attribute("name")) |
| 437 | res_name = child->first_attribute("name")->value(); |
| 438 | if (res_name.empty() && child->first_attribute("filename")) |
| 439 | res_name = child->first_attribute("filename")->value(); |
| 440 | |
| 441 | if (!res_name.empty()) { |
| 442 | LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
| 443 | } else |
| 444 | LOGERR("Resource type (%s) failed to load\n", type.c_str()); |
that | f74ac87 | 2015-01-18 12:00:02 +0100 | [diff] [blame] | 445 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 446 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | ResourceManager::~ResourceManager() |
| 450 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 451 | for (std::vector<FontResource*>::iterator it = mFonts.begin(); it != mFonts.end(); ++it) |
| 452 | delete *it; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 453 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 454 | for (std::vector<ImageResource*>::iterator it = mImages.begin(); it != mImages.end(); ++it) |
| 455 | delete *it; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 456 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 457 | for (std::vector<AnimationResource*>::iterator it = mAnimations.begin(); it != mAnimations.end(); ++it) |
| 458 | delete *it; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 459 | } |