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; |
bigbiff | 83298f5 | 2021-10-13 19:24:42 -0400 | [diff] [blame] | 61 | if (FindEntry(pZip, src, &binary_entry) == 0) { |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 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 | } |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 67 | int32_t err = ExtractEntryToFile(pZip, &binary_entry, fd); |
| 68 | if (err != 0) |
| 69 | return -1; |
| 70 | } else { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 71 | return -1; |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 72 | } |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 73 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 74 | } |
| 75 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 76 | void Resource::LoadImage(ZipArchiveHandle pZip, std::string file, gr_surface* surface) |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 77 | { |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 78 | int rc = 0; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 79 | if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0) |
| 80 | { |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 81 | rc = res_create_surface(TMP_RESOURCE_NAME, surface); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 82 | unlink(TMP_RESOURCE_NAME); |
| 83 | } |
| 84 | else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0) |
| 85 | { |
| 86 | // JPG includes the .jpg extension in the filename so extension should be blank |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 87 | rc = res_create_surface(TMP_RESOURCE_NAME, surface); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 88 | unlink(TMP_RESOURCE_NAME); |
| 89 | } |
| 90 | else if (!pZip) |
| 91 | { |
| 92 | // File name in xml may have included .png so try without adding .png |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 93 | rc = res_create_surface(file.c_str(), surface); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 94 | } |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 95 | if (rc != 0) |
| 96 | 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] | 97 | } |
| 98 | |
| 99 | void Resource::CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect) |
| 100 | { |
| 101 | if (!source) { |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 102 | *destination = nullptr; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | if (get_scale_w() != 0 && get_scale_h() != 0) { |
| 106 | float scale_w = get_scale_w(), scale_h = get_scale_h(); |
| 107 | if (retain_aspect) { |
| 108 | if (scale_w < scale_h) |
| 109 | scale_h = scale_w; |
| 110 | else |
| 111 | scale_w = scale_h; |
| 112 | } |
| 113 | if (res_scale_surface(source, destination, scale_w, scale_h)) { |
| 114 | LOGINFO("Error scaling image, using regular size.\n"); |
| 115 | *destination = source; |
| 116 | } |
| 117 | } else { |
| 118 | *destination = source; |
| 119 | } |
| 120 | } |
| 121 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 122 | FontResource::FontResource(xml_node<>* node, ZipArchiveHandle pZip) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 123 | : Resource(node, pZip) |
| 124 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 125 | origFontSize = 0; |
| 126 | origFont = NULL; |
| 127 | LoadFont(node, pZip); |
| 128 | } |
| 129 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 130 | void FontResource::LoadFont(xml_node<>* node, ZipArchiveHandle pZip) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 131 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | std::string file; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 133 | xml_attribute<>* attr; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 134 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 135 | mFont = NULL; |
| 136 | if (!node) |
| 137 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 138 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 139 | attr = node->first_attribute("filename"); |
| 140 | if (!attr) |
| 141 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 142 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 143 | file = attr->value(); |
| 144 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 145 | if (file.size() >= 4 && file.compare(file.size()-4, 4, ".ttf") == 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 146 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 147 | int font_size = 0; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 148 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 149 | if (origFontSize != 0) { |
| 150 | attr = node->first_attribute("scale"); |
| 151 | if (attr == NULL) |
| 152 | return; |
| 153 | font_size = origFontSize * atoi(attr->value()) / 100; |
| 154 | } else { |
| 155 | attr = node->first_attribute("size"); |
| 156 | if (attr == NULL) |
| 157 | return; |
| 158 | font_size = scale_theme_min(atoi(attr->value())); |
| 159 | origFontSize = font_size; |
| 160 | } |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 161 | |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 162 | int dpi = 300; |
| 163 | |
| 164 | attr = node->first_attribute("dpi"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 165 | if (attr) |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 166 | dpi = atoi(attr->value()); |
| 167 | |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 168 | // we can't use TMP_RESOURCE_NAME here because the ttf subsystem is caching the name and scaling needs to reload the font |
| 169 | std::string tmpname = "/tmp/" + file; |
| 170 | if (ExtractResource(pZip, "fonts", file, "", tmpname) == 0) |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 171 | { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 172 | mFont = twrpTruetype::gr_ttf_loadFont(tmpname.c_str(), font_size, dpi); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 173 | } |
| 174 | else |
| 175 | { |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 176 | file = std::string(TWRES "fonts/") + file; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 177 | mFont = twrpTruetype::gr_ttf_loadFont(file.c_str(), font_size, dpi); |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 178 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 179 | } |
| 180 | else |
| 181 | { |
Ethan Yonker | 88037f4 | 2015-10-04 22:09:08 -0500 | [diff] [blame] | 182 | LOGERR("Non-TTF fonts are no longer supported.\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 183 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 186 | void FontResource::DeleteFont() { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 187 | if (mFont) { |
| 188 | twrpTruetype::gr_ttf_freeFont(mFont); |
| 189 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 190 | mFont = NULL; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 191 | if (origFont) { |
| 192 | twrpTruetype::gr_ttf_freeFont(origFont); |
| 193 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 194 | origFont = NULL; |
| 195 | } |
| 196 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 197 | void FontResource::Override(xml_node<>* node, ZipArchiveHandle pZip) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 198 | if (!origFont) { |
| 199 | origFont = mFont; |
| 200 | } else if (mFont) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 201 | twrpTruetype::gr_ttf_freeFont(mFont); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 202 | mFont = NULL; |
| 203 | } |
| 204 | LoadFont(node, pZip); |
| 205 | } |
| 206 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 207 | FontResource::~FontResource() |
| 208 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 209 | DeleteFont(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 210 | } |
| 211 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 212 | ImageResource::ImageResource(xml_node<>* node, ZipArchiveHandle pZip) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 213 | : Resource(node, pZip) |
| 214 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 215 | std::string file; |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 216 | gr_surface temp_surface = nullptr; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 217 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 218 | mSurface = NULL; |
Ethan Yonker | 619a721 | 2014-12-03 16:47:37 -0600 | [diff] [blame] | 219 | if (!node) { |
| 220 | LOGERR("ImageResource node is NULL\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 221 | return; |
Ethan Yonker | 619a721 | 2014-12-03 16:47:37 -0600 | [diff] [blame] | 222 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 223 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 224 | if (node->first_attribute("filename")) |
| 225 | file = node->first_attribute("filename")->value(); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 226 | else { |
| 227 | LOGERR("No filename specified for image resource.\n"); |
| 228 | return; |
| 229 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 230 | |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 231 | bool retain_aspect = (node->first_attribute("retainaspect") != NULL); |
| 232 | // 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] | 233 | LoadImage(pZip, file, &temp_surface); |
| 234 | CheckAndScaleImage(temp_surface, &mSurface, retain_aspect); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | ImageResource::~ImageResource() |
| 238 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 239 | if (mSurface) |
| 240 | res_free_surface(mSurface); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 241 | } |
| 242 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 243 | AnimationResource::AnimationResource(xml_node<>* node, ZipArchiveHandle pZip) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 244 | : Resource(node, pZip) |
| 245 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 246 | std::string file; |
| 247 | int fileNum = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 248 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 249 | if (!node) |
| 250 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 251 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 252 | if (node->first_attribute("filename")) |
| 253 | file = node->first_attribute("filename")->value(); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 254 | else { |
| 255 | LOGERR("No filename specified for image resource.\n"); |
| 256 | return; |
| 257 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 258 | |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 259 | bool retain_aspect = (node->first_attribute("retainaspect") != NULL); |
| 260 | // 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] | 261 | for (;;) |
| 262 | { |
| 263 | std::ostringstream fileName; |
| 264 | fileName << file << std::setfill ('0') << std::setw (3) << fileNum; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 265 | |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 266 | gr_surface surface = nullptr; |
| 267 | gr_surface temp_surface = nullptr; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 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 Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 275 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | AnimationResource::~AnimationResource() |
| 279 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 280 | std::vector<gr_surface>::iterator it; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 281 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 282 | for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it) |
| 283 | res_free_surface(*it); |
| 284 | |
| 285 | mSurfaces.clear(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 286 | } |
| 287 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 288 | FontResource* ResourceManager::FindFont(const std::string& name) const |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 289 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 290 | for (std::vector<FontResource*>::const_iterator it = mFonts.begin(); it != mFonts.end(); ++it) |
| 291 | if (name == (*it)->GetName()) |
| 292 | return *it; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 293 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 294 | } |
| 295 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 296 | ImageResource* ResourceManager::FindImage(const std::string& name) const |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 297 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 298 | 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 | |
| 304 | AnimationResource* 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 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 312 | std::string ResourceManager::FindString(const std::string& name) const |
| 313 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 314 | //if (this != NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 315 | 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 Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 320 | /*} else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 321 | 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] | 322 | }*/ |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 323 | return "[" + name + ("]"); |
| 324 | } |
| 325 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 326 | std::string ResourceManager::FindString(const std::string& name, const std::string& default_string) const |
| 327 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 328 | //if (this != NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 329 | 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 Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 334 | /*} else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 335 | 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] | 336 | }*/ |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 337 | return default_string; |
| 338 | } |
| 339 | |
| 340 | void ResourceManager::DumpStrings() const |
| 341 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 342 | /*if (this == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 343 | gui_print("No string resources\n"); |
| 344 | return; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 345 | }*/ |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 346 | 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 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 353 | ResourceManager::ResourceManager() |
| 354 | { |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 355 | } |
| 356 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 357 | void 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 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 365 | void ResourceManager::LoadResources(xml_node<>* resList, ZipArchiveHandle pZip, std::string resource_source) |
Ethan Yonker | 780cd39 | 2014-07-21 15:24:39 -0500 | [diff] [blame] | 366 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 367 | if (!resList) |
| 368 | return; |
that | 0f42506 | 2015-03-04 23:05:00 +0100 | [diff] [blame] | 369 | |
| 370 | for (xml_node<>* child = resList->first_node(); child; child = child->next_sibling()) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 371 | { |
that | 0f42506 | 2015-03-04 23:05:00 +0100 | [diff] [blame] | 372 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 378 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 379 | bool error = false; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 380 | if (type == "font") |
| 381 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 382 | FontResource* res = new FontResource(child, pZip); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 383 | if (res && res->GetResource()) |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 384 | mFonts.push_back(res); |
| 385 | else { |
| 386 | error = true; |
| 387 | delete res; |
| 388 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 389 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 390 | 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 Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 410 | else if (type == "image") |
| 411 | { |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 412 | ImageResource* res = new ImageResource(child, pZip); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 413 | if (res && res->GetResource()) |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 414 | mImages.push_back(res); |
| 415 | else { |
| 416 | error = true; |
| 417 | delete res; |
| 418 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 419 | } |
| 420 | else if (type == "animation") |
| 421 | { |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 422 | AnimationResource* res = new AnimationResource(child, pZip); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 423 | if (res && res->GetResourceCount()) |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 424 | mAnimations.push_back(res); |
| 425 | else { |
| 426 | error = true; |
| 427 | delete res; |
| 428 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 429 | } |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 430 | else if (type == "string") |
| 431 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 432 | 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 |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 438 | error = true; |
| 439 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 440 | else |
| 441 | { |
| 442 | LOGERR("Resource type (%s) not supported.\n", type.c_str()); |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 443 | error = true; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 444 | } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 445 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 446 | if (error) |
that | f74ac87 | 2015-01-18 12:00:02 +0100 | [diff] [blame] | 447 | { |
| 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()); |
that | f74ac87 | 2015-01-18 12:00:02 +0100 | [diff] [blame] | 458 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 459 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | ResourceManager::~ResourceManager() |
| 463 | { |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 464 | for (std::vector<FontResource*>::iterator it = mFonts.begin(); it != mFonts.end(); ++it) |
| 465 | delete *it; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 466 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 467 | for (std::vector<ImageResource*>::iterator it = mImages.begin(); it != mImages.end(); ++it) |
| 468 | delete *it; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 469 | |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 470 | for (std::vector<AnimationResource*>::iterator it = mAnimations.begin(); it != mAnimations.end(); ++it) |
| 471 | delete *it; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 472 | } |