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 | { |
| 35 | if (node && node->first_attribute("name")) |
| 36 | mName = node->first_attribute("name")->value(); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | int Resource::ExtractResource(ZipArchive* pZip, |
| 41 | std::string folderName, |
| 42 | std::string fileName, |
| 43 | std::string fileExtn, |
| 44 | std::string destFile) |
| 45 | { |
| 46 | if (!pZip) return -1; |
| 47 | |
| 48 | std::string src = folderName + "/" + fileName + fileExtn; |
| 49 | |
| 50 | const ZipEntry* binary = mzFindZipEntry(pZip, src.c_str()); |
| 51 | if (binary == NULL) { |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | unlink(destFile.c_str()); |
| 56 | int fd = creat(destFile.c_str(), 0666); |
| 57 | if (fd < 0) |
| 58 | return -1; |
| 59 | |
| 60 | int ret = 0; |
| 61 | if (!mzExtractZipEntryToFile(pZip, binary, fd)) |
| 62 | ret = -1; |
| 63 | |
| 64 | close(fd); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | FontResource::FontResource(xml_node<>* node, ZipArchive* pZip) |
| 69 | : Resource(node, pZip) |
| 70 | { |
| 71 | std::string file; |
| 72 | |
| 73 | mFont = NULL; |
| 74 | if (!node) return; |
| 75 | |
| 76 | if (node->first_attribute("filename")) |
| 77 | file = node->first_attribute("filename")->value(); |
| 78 | |
| 79 | if (ExtractResource(pZip, "fonts", file, ".dat", TMP_RESOURCE_NAME) == 0) |
| 80 | { |
| 81 | mFont = gr_loadFont(TMP_RESOURCE_NAME); |
| 82 | unlink(TMP_RESOURCE_NAME); |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | mFont = gr_loadFont(file.c_str()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | FontResource::~FontResource() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | ImageResource::ImageResource(xml_node<>* node, ZipArchive* pZip) |
| 95 | : Resource(node, pZip) |
| 96 | { |
| 97 | std::string file; |
| 98 | |
| 99 | mSurface = NULL; |
| 100 | if (!node) return; |
| 101 | |
| 102 | if (node->first_attribute("filename")) |
| 103 | file = node->first_attribute("filename")->value(); |
| 104 | |
| 105 | if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0) |
| 106 | { |
| 107 | res_create_surface(TMP_RESOURCE_NAME, &mSurface); |
| 108 | unlink(TMP_RESOURCE_NAME); |
| 109 | } else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0) |
| 110 | { |
| 111 | // JPG includes the .jpg extension in the filename so extension should be blank |
| 112 | res_create_surface(TMP_RESOURCE_NAME, &mSurface); |
| 113 | unlink(TMP_RESOURCE_NAME); |
| 114 | } |
| 115 | else |
| 116 | res_create_surface(file.c_str(), &mSurface); |
| 117 | } |
| 118 | |
| 119 | ImageResource::~ImageResource() |
| 120 | { |
| 121 | if (mSurface) |
| 122 | res_free_surface(mSurface); |
| 123 | } |
| 124 | |
| 125 | AnimationResource::AnimationResource(xml_node<>* node, ZipArchive* pZip) |
| 126 | : Resource(node, pZip) |
| 127 | { |
| 128 | std::string file; |
| 129 | int fileNum = 1; |
| 130 | |
| 131 | if (!node) return; |
| 132 | |
| 133 | if (node->first_attribute("filename")) |
| 134 | file = node->first_attribute("filename")->value(); |
| 135 | |
| 136 | for ( ; ; ) |
| 137 | { |
| 138 | std::ostringstream fileName; |
| 139 | fileName << file << std::setfill ('0') << std::setw (3) << fileNum; |
| 140 | |
| 141 | gr_surface surface; |
| 142 | if (pZip) |
| 143 | { |
| 144 | if (ExtractResource(pZip, "images", fileName.str(), ".png", TMP_RESOURCE_NAME) != 0) |
| 145 | break; |
| 146 | |
| 147 | if (res_create_surface(TMP_RESOURCE_NAME, &surface)) |
| 148 | break; |
| 149 | |
| 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 | } |
| 160 | } |
| 161 | |
| 162 | AnimationResource::~AnimationResource() |
| 163 | { |
| 164 | std::vector<gr_surface>::iterator it; |
| 165 | |
| 166 | for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it) |
| 167 | { |
| 168 | res_free_surface(*it); |
| 169 | } |
| 170 | mSurfaces.clear(); |
| 171 | } |
| 172 | |
| 173 | Resource* ResourceManager::FindResource(std::string name) |
| 174 | { |
| 175 | std::vector<Resource*>::iterator iter; |
| 176 | |
| 177 | for (iter = mResources.begin(); iter != mResources.end(); iter++) |
| 178 | { |
| 179 | if (name == (*iter)->GetName()) |
| 180 | return (*iter); |
| 181 | } |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | ResourceManager::ResourceManager(xml_node<>* resList, ZipArchive* pZip) |
| 186 | { |
| 187 | xml_node<>* child; |
| 188 | |
| 189 | if (!resList) return; |
| 190 | |
| 191 | child = resList->first_node("resource"); |
| 192 | while (child != NULL) |
| 193 | { |
| 194 | xml_attribute<>* attr = child->first_attribute("type"); |
| 195 | if (!attr) |
| 196 | break; |
| 197 | |
| 198 | std::string type = attr->value(); |
| 199 | |
| 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"); |
| 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 | |
| 213 | delete res; |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | mResources.push_back((Resource*) res); |
| 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"); |
| 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 | |
| 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"); |
| 246 | |
| 247 | if (!attr_name) { |
| 248 | std::string res_name = attr_name->value(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 249 | 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] | 250 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 251 | LOGERR("Resource type (%s) failed to load\n", type.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 252 | |
| 253 | delete res; |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | mResources.push_back((Resource*) res); |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 262 | LOGERR("Resource type (%s) not supported.\n", type.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | child = child->next_sibling("resource"); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | ResourceManager::~ResourceManager() |
| 270 | { |
| 271 | std::vector<Resource*>::iterator iter; |
| 272 | |
| 273 | for (iter = mResources.begin(); iter != mResources.end(); iter++) |
| 274 | { |
| 275 | delete *iter; |
| 276 | } |
| 277 | mResources.clear(); |
| 278 | } |
| 279 | |