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