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