void DataStore::setString(const char* value, const char *key) { fzStoreEntry entry; entry.key = fzStrcpy(key); entry.hash = fzHash(key); entry.type = kFZData_string; entry.data = fzBuffer(fzStrcpy(value), strlen(value)+1); setEntry(entry); }
void ResourcesManager::checkFile(const char* filename) const { if(filename == NULL) return; // REMOVING FORCED FLAGS char *filenameCpy = fzStrcpy(filename); IO::removeFileSuffix(filenameCpy); // LOOK FOR TEXTURE char absolutePath[STRING_MAX_SIZE]; fzUInt factor, priority = 0; FZLog("ResourcesManager:"); while (getPath(filenameCpy, priority, absolutePath, &factor)) { if(IO::checkFile(absolutePath)) printf(" - FOUND: %s\n", absolutePath); else printf(" - NOT FOUND: %s\n", absolutePath); ++priority; } delete filenameCpy; }
fzBuffer ResourcesManager::loadResource(const char *filename, fzUInt *outFactor) const { FZ_ASSERT(filename != NULL, "Filename can not be NULL."); FZ_ASSERT(outFactor != NULL, "outFactor can not be NULL."); // REMOVING FORCED FLAGS char *filenameCpy = fzStrcpy(filename); IO::removeFileSuffix(filenameCpy); // LOOK FOR TEXTURE *outFactor = 0; char absolutePath[STRING_MAX_SIZE]; fzUInt factor, priority = 0; while (getPath(filenameCpy, priority, absolutePath, &factor)) { fzBuffer buffer = IO::loadFile(absolutePath); if(!buffer.isEmpty()) { *outFactor = factor; delete filenameCpy; return buffer; } ++priority; } FZLOGERROR("IO: \"%s\" not found.", filename); delete filenameCpy; return fzBuffer::empty(); }
void ResourcesManager::_generateAbsolutePath(const char *filename, const char *suffix, char *absolutePath) const { FZ_ASSERT(filename != NULL, "Filename can not be NULL."); FZ_ASSERT(absolutePath != NULL, "AbsolutePath must be a valid pointer."); if(suffix == NULL) { if(filename[0] == '/') strcpy(absolutePath, filename); else IO::appendPaths(p_resourcesPath, filename, absolutePath); } else { // GET EXTENSION const char *extension = strchr(filename, '.'); if(extension == NULL) extension = filename + strlen(filename); // GET NAME size_t nameLength = extension - filename; char *name = fzStrcpy(filename, nameLength); // BUILD if(filename[0] == '/') sprintf(absolutePath, "%s%c%s%s", name, FZ_IO_SUBFIX_CHAR, suffix, extension); else { char relativePath[STRING_MAX_SIZE]; sprintf(relativePath, "%s%c%s%s", name, FZ_IO_SUBFIX_CHAR, suffix, extension); IO::appendPaths(p_resourcesPath, relativePath, absolutePath); } delete [] name; } }
char* fzStrcpy(const char *str) { if(str == NULL) return NULL; return fzStrcpy(str, strlen(str)); }
void DataStore::setData(const fzBuffer& data, const char *key) { fzStoreEntry entry; entry.key = fzStrcpy(key); entry.hash = fzHash(key); entry.type = kFZData_data; entry.data = data; setEntry(entry); }
void DataStore::setInteger(int32_t value, const char *key) { fzStoreEntry entry; entry.key = fzStrcpy(key); entry.hash = fzHash(key); entry.type = kFZData_integer; entry.integerValue = value; setEntry(entry); }
void DataStore::setFloat(double value, const char *key) { fzStoreEntry entry; entry.key = fzStrcpy(key); entry.hash = fzHash(key); entry.type = kFZData_float; entry.floatValue = value; setEntry(entry); }
ResourcesManager::ResourcesManager() : m_nuRules(0) { // GET RESOURCES PATH char tmp[STRING_MAX_SIZE]; if(!fzOSW_getResourcesPath(tmp, STRING_MAX_SIZE)) FZ_RAISE_STOP("ResourcesManager: Buffer too small, impossible to get the resources path."); p_resourcesPath = fzStrcpy(tmp); setupDefaultRules(); }
DataStore::DataStore() : p_store(NULL) , p_path(NULL) , m_num(0) , m_capacity(0) , m_dirty(false) { { char buffer[1024]; fzDevice_getPersistentPath(XML_FILENAME, buffer, 1024); p_path = fzStrcpy(buffer); } // READ DATA try { readFromMemory(); } catch (std::exception& error) { fzDevice_removePath(p_path); } }
Font* FontCache::addFont(const char* filename, fzFloat lineHeight) { FZ_ASSERT(filename != NULL, "Filename argument must be non-NULL."); // Make string mutable char *filenameCpy = fzStrcpy(filename); // Remove "-x" suffix IO::removeFileSuffix(filenameCpy); uint32_t hash = fzHash(filenameCpy); Font *font = getFontForHash(hash); if(font != NULL && strcmp(IO::getExtension(filenameCpy), "ttf") == 0) { // Rewritting FZ_ASSERT(lineHeight > 0, "Line height must me positive."); FZLog("NOT IMPLEMENTED"); } if(font == NULL) { try { font = new Font(filenameCpy, lineHeight); font->retain(); m_fonts.insert(fontsPair(hash, font)); } catch(std::exception& error) { delete filenameCpy; FZLOGERROR("%s", error.what()); return NULL; } } delete filenameCpy; return font; }
void DataStore::readFromMemory() { FZ_ASSERT(p_path, "Path cannot be NULL."); if(m_dirty) return; fzBuffer buffer = IO::loadFile(p_path); if(!buffer.isEmpty()) { xml_document<> doc; doc.parse<parse_fastest | parse_validate_closing_tags>(buffer.getPointer()); xml_node<> *node = doc.first_node(); if(strncmp(node->name(), XML_SIZE_TAG, node->name_size()) != 0) FZ_RAISE("DataStore: XML is corrupted."); // RESERVE CAPACITY reserveCapacity(atoi(node->value())+1); // ITERATE XML m_num = 0; node = node->next_sibling(); for(; node; node = node->next_sibling()) { if(strncmp(node->name(), XML_ENTRY_TAG, node->name_size()) != 0) { FZLOGERROR("DataStore: XML entry is corrupted."); continue; } fzStoreEntry &entry = p_store[m_num]; // KEY xml_attribute<> *attribute = node->first_attribute(XML_KEY_ATTRIBUTE); if(attribute == NULL) { FZLOGERROR("DataStore: Key attribute is missing."); continue; } entry.key = fzStrcpy(attribute->value(), attribute->value_size()); entry.hash = fzHash(attribute->value(), attribute->value_size()); // ENTRY TYPE attribute = node->last_attribute(XML_TYPE_ATTRIBUTE); if(attribute == NULL) { FZLOGERROR("DataStore: Type attribute is missing."); continue; } int type = atoi(attribute->value()); // DATA const char *data = node->value(); if(data) { switch (type) { case kFZData_string: case kFZData_data: { fzBuffer decoded = Data::B64Decode(data, node->value_size()); entry.data = decoded; break; } case kFZData_integer: entry.integerValue = atoi(data); break; case kFZData_float: entry.floatValue = atof(data); break; default: FZ_RAISE("DataStore: Entry type is invalid."); break; } } entry.type = static_cast<unsigned char>(type); ++m_num; } buffer.free(); FZ_ASSERT(m_num <= m_capacity, "Memory overflow."); } }
void SpriteFrameCache::addSpriteFrames(const char* coordsFilename, Texture2D *texture) { fzUInt factor; fzBuffer data = ResourcesManager::Instance().loadResource(coordsFilename, &factor); if(data.isEmpty()) return; xml_document<> doc; try { doc.parse<parse_fastest | parse_validate_closing_tags>(data.getPointer()); } catch(std::exception &error) { data.free(); FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". rapidXML exception: %s", coordsFilename, error.what()); return; } xml_node<> *rootNode, *subNode, *node; rootNode = doc.first_node("metadata"); if(texture == NULL) { subNode = rootNode->first_node("textureFileName"); if(subNode) { char *filename = fzStrcpy(subNode->value(), subNode->value_size()); texture = TextureCache::Instance().addImage(filename); delete filename; } } // Parse metadata if(texture == NULL) { data.free(); FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". Error pTexture could not be loaded.", coordsFilename); return; } // Parse frames rootNode = doc.first_node("frames"); if(rootNode == NULL) { data.free(); FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". <frames> tag wasn't found.", coordsFilename); return; } for(subNode = rootNode->first_node(); subNode; subNode = subNode->next_sibling()) { if(strncmp(subNode->name(), "frame", subNode->name_size()) != 0) { FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". XML format is not correct.", coordsFilename); continue; } fzRect rect; fzPoint offset = FZPointZero; fzSize originalSize = FZSizeZero; bool rotated = false; // NAME xml_attribute<> *attribute = subNode->first_attribute("name"); if(attribute == NULL) { FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". <node name> attribute wasn't found.", coordsFilename); continue; } uint32_t hash = fzHash(attribute->value(), attribute->value_size()); if(getSpriteFrameByHash(hash).isValid()) continue; // ORIGIN node = subNode->first_node("p"); if(node == NULL) { FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". <p> tag wasn't found.", coordsFilename); continue; } attribute = node->first_attribute("x"); if(attribute == NULL) { FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". <p x> attribute wasn't found.", coordsFilename); continue; } rect.origin.x = atof(attribute->value()) / factor; attribute = node->first_attribute("y"); if(attribute == NULL) { FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". <p y> attribute wasn't found.", coordsFilename); continue; } rect.origin.y = atof(attribute->value()) / factor; // SIZE node = subNode->first_node("s"); if(node == NULL) { FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". <s> tag wasn't found.", coordsFilename); continue; } attribute = node->first_attribute("x"); if(attribute == NULL) { FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". <s x> attribute wasn't found.", coordsFilename); continue; } rect.size.width = atof(attribute->value()) / factor; attribute = node->first_attribute("y"); if(attribute == NULL) { FZLOGERROR("SpriteFrameCache: Error parsing \"%s\". <s y> attribute wasn't found.", coordsFilename); continue; } rect.size.height = atof(attribute->value()) / factor; // OFFSET POSITION node = subNode->first_node("o"); if(node) { attribute = node->first_attribute("x"); if(attribute) offset.x = atof(attribute->value()) / factor; attribute = node->first_attribute("y"); if(attribute) offset.y = atof(attribute->value()) / factor; } // UNTRIMMED SIZE (ORIGINAL) node = subNode->first_node("u"); if(node) { attribute = node->first_attribute("x"); if(attribute) originalSize.width = atof(attribute->value()) / factor; attribute = node->first_attribute("y"); if(attribute) originalSize.height = atof(attribute->value()) / factor; } // IS ROTATED node = subNode->last_node("r"); if(node) { attribute = node->first_attribute("x"); if(attribute) rotated = atoi(attribute->value()); } if(originalSize == FZSizeZero) originalSize = rect.size; // create frame fzSpriteFrame spriteFrame(texture, rect, offset, originalSize, rotated); m_frames.insert(framesPair(hash, spriteFrame)); } data.free(); }