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> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/reboot.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/ioctl.h> |
| 14 | #include <time.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <string> |
| 19 | #include <sstream> |
| 20 | #include <iostream> |
| 21 | #include <iomanip> |
| 22 | |
| 23 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 24 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | #include "rapidxml.hpp" |
| 29 | #include "objects.hpp" |
| 30 | |
| 31 | #define TMP_RESOURCE_NAME "/tmp/extract.bin" |
| 32 | |
| 33 | Resource::Resource(xml_node<>* node, ZipArchive* pZip) |
| 34 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 35 | if (node && node->first_attribute("name")) |
| 36 | mName = node->first_attribute("name")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 39 | 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] | 40 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 41 | if (!pZip) |
| 42 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 44 | std::string src = folderName + "/" + fileName + fileExtn; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 45 | |
| 46 | const ZipEntry* binary = mzFindZipEntry(pZip, src.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 47 | if (binary == NULL) { |
| 48 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 51 | unlink(destFile.c_str()); |
| 52 | int fd = creat(destFile.c_str(), 0666); |
| 53 | if (fd < 0) |
| 54 | return -1; |
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 | int ret = 0; |
| 57 | if (!mzExtractZipEntryToFile(pZip, binary, fd)) |
| 58 | ret = -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 59 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 60 | close(fd); |
| 61 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | FontResource::FontResource(xml_node<>* node, ZipArchive* pZip) |
| 65 | : Resource(node, pZip) |
| 66 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 67 | std::string file; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 69 | mFont = NULL; |
| 70 | if (!node) |
| 71 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 72 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 73 | if (node->first_attribute("filename")) |
| 74 | file = node->first_attribute("filename")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 76 | if (ExtractResource(pZip, "fonts", file, ".dat", TMP_RESOURCE_NAME) == 0) |
| 77 | { |
| 78 | mFont = gr_loadFont(TMP_RESOURCE_NAME); |
| 79 | unlink(TMP_RESOURCE_NAME); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | mFont = gr_loadFont(file.c_str()); |
| 84 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | FontResource::~FontResource() |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | ImageResource::ImageResource(xml_node<>* node, ZipArchive* pZip) |
| 92 | : Resource(node, pZip) |
| 93 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 94 | std::string file; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 95 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 96 | mSurface = NULL; |
| 97 | if (!node) |
| 98 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 99 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 100 | if (node->first_attribute("filename")) |
| 101 | file = node->first_attribute("filename")->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 102 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 103 | if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0) |
| 104 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 105 | res_create_surface(TMP_RESOURCE_NAME, &mSurface); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 106 | unlink(TMP_RESOURCE_NAME); |
| 107 | } |
| 108 | else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0) |
| 109 | { |
| 110 | // JPG includes the .jpg extension in the filename so extension should be blank |
| 111 | res_create_surface(TMP_RESOURCE_NAME, &mSurface); |
| 112 | unlink(TMP_RESOURCE_NAME); |
| 113 | } |
| 114 | else |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 115 | res_create_surface(file.c_str(), &mSurface); |
| 116 | } |
| 117 | |
| 118 | ImageResource::~ImageResource() |
| 119 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | if (mSurface) |
| 121 | res_free_surface(mSurface); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | AnimationResource::AnimationResource(xml_node<>* node, ZipArchive* pZip) |
| 125 | : Resource(node, pZip) |
| 126 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 127 | std::string file; |
| 128 | int fileNum = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 129 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 130 | if (!node) |
| 131 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 132 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 133 | if (node->first_attribute("filename")) |
| 134 | file = node->first_attribute("filename")->value(); |
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 | for (;;) |
| 137 | { |
| 138 | std::ostringstream fileName; |
| 139 | fileName << file << std::setfill ('0') << std::setw (3) << fileNum; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 140 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 141 | gr_surface surface; |
| 142 | if (pZip) |
| 143 | { |
| 144 | if (ExtractResource(pZip, "images", fileName.str(), ".png", TMP_RESOURCE_NAME) != 0) |
| 145 | break; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 146 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 147 | if (res_create_surface(TMP_RESOURCE_NAME, &surface)) |
| 148 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 149 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 150 | unlink(TMP_RESOURCE_NAME); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | if (res_create_surface(fileName.str().c_str(), &surface)) |
| 155 | break; |
| 156 | } |
| 157 | mSurfaces.push_back(surface); |
| 158 | fileNum++; |
| 159 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | AnimationResource::~AnimationResource() |
| 163 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 164 | std::vector<gr_surface>::iterator it; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 166 | for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it) |
| 167 | res_free_surface(*it); |
| 168 | |
| 169 | mSurfaces.clear(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | Resource* ResourceManager::FindResource(std::string name) |
| 173 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 174 | std::vector<Resource*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 175 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 176 | for (iter = mResources.begin(); iter != mResources.end(); iter++) |
| 177 | { |
| 178 | if (name == (*iter)->GetName()) |
| 179 | return (*iter); |
| 180 | } |
| 181 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | ResourceManager::ResourceManager(xml_node<>* resList, ZipArchive* pZip) |
| 185 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 186 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 187 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 188 | if (!resList) |
| 189 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 190 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 191 | child = resList->first_node("resource"); |
| 192 | while (child != NULL) |
| 193 | { |
| 194 | xml_attribute<>* attr = child->first_attribute("type"); |
| 195 | if (!attr) |
| 196 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 197 | |
| 198 | std::string type = attr->value(); |
| 199 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 200 | if (type == "font") |
| 201 | { |
| 202 | FontResource* res = new FontResource(child, pZip); |
| 203 | if (res == NULL || res->GetResource() == NULL) |
| 204 | { |
| 205 | xml_attribute<>* attr_name = child->first_attribute("name"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 206 | |
| 207 | if (!attr_name) { |
| 208 | std::string res_name = attr_name->value(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 209 | LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 210 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 211 | LOGERR("Resource type (%s) failed to load\n", type.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 212 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 213 | delete res; |
| 214 | } |
| 215 | else |
| 216 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 217 | mResources.push_back((Resource*) res); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | else if (type == "image") |
| 221 | { |
| 222 | ImageResource* res = new ImageResource(child, pZip); |
| 223 | if (res == NULL || res->GetResource() == NULL) |
| 224 | { |
| 225 | xml_attribute<>* attr_name = child->first_attribute("name"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 226 | |
| 227 | if (!attr_name) { |
| 228 | std::string res_name = attr_name->value(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 229 | LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 230 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 231 | LOGERR("Resource type (%s) failed to load\n", type.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 232 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 233 | delete res; |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | mResources.push_back((Resource*) res); |
| 238 | } |
| 239 | } |
| 240 | else if (type == "animation") |
| 241 | { |
| 242 | AnimationResource* res = new AnimationResource(child, pZip); |
| 243 | if (res == NULL || res->GetResource() == NULL) |
| 244 | { |
| 245 | xml_attribute<>* attr_name = child->first_attribute("name"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 246 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 247 | if (!attr_name) { |
| 248 | std::string res_name = attr_name->value(); |
| 249 | LOGERR("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); |
| 250 | } else |
| 251 | LOGERR("Resource type (%s) failed to load\n", type.c_str()); |
| 252 | |
| 253 | delete res; |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | mResources.push_back((Resource*) res); |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | LOGERR("Resource type (%s) not supported.\n", type.c_str()); |
| 263 | } |
| 264 | |
| 265 | child = child->next_sibling("resource"); |
| 266 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | ResourceManager::~ResourceManager() |
| 270 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 271 | std::vector<Resource*>::iterator iter; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 272 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 273 | for (iter = mResources.begin(); iter != mResources.end(); iter++) |
| 274 | delete *iter; |
| 275 | |
| 276 | mResources.clear(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 277 | } |