int stringtableLoadSection(STRINGTABLE *st, const char *section) { STRINGTABLE_FILE_ENTRY *fe; int i, sec; void *data, *zdata, *fez; char *str; if (st == NULL) return -1; if ((sec = stringtableFindSection(st, section)) == -1) return -1; if (st->section[sec].strings != 0) return 0; fsFileSeek(st->fp, st->section[sec].file_pos, SEEK_SET); zdata = malloc(st->section[sec].sec.zlen); data = malloc(st->section[sec].sec.len); fez = malloc(st->section[sec].sec.stringz); fe = malloc(st->section[sec].sec.strings * sizeof(STRINGTABLE_FILE_ENTRY)); st->section[sec].string = malloc(st->section[sec].sec.strings * sizeof(STRINGTABLE_ENTRY)); if (zdata == NULL || data == NULL || fez == NULL || fe == NULL || st->section[sec].string == NULL) { free(st->section[sec].string); free(zdata); free(data); free(fez); free(fe); return -1; } fsFileRead(zdata, st->section[sec].sec.zlen, st->fp); fsFileRead(fez, st->section[sec].sec.stringz, st->fp); stbi_zlib_decode_buffer(data, st->section[sec].sec.len, zdata, st->section[sec].sec.zlen); stbi_zlib_decode_buffer((void *) fe, st->section[sec].sec.strings * sizeof(STRINGTABLE_FILE_ENTRY), fez, st->section[sec].sec.stringz); free(zdata); free(fez); str = data; for (i = 0; i < st->section[sec].sec.strings; i++) { st->section[sec].string[i].name = &str[ntohl(fe[i].pos)]; st->section[sec].string[i].value = &str[ntohl(fe[i].val)]; st->section[sec].string[i].key = utilStringSum(st->section[sec].string[i].name); } st->section[sec].string_data = data; st->section[sec].strings = st->section[sec].sec.strings; free(fe); return 0; }
void Image::load() { flags |= USED; if (tex != 0 || image != NULL) return; if (flags & FILE) { ((FileImage*)this)->load_file(); return; } unsigned int size, out_size; unsigned char * buf; int ret; if (startup_data) { unsigned int start = AssetFile::get_offset(0, AssetFile::IMAGE_DATA); unsigned int self = AssetFile::get_offset(handle, AssetFile::IMAGE_DATA); unsigned int offset = self - start; ArrayStream stream((char*)startup_data, startup_size); stream.seek(offset); load_image_info(*this, stream, size, out_size); buf = &startup_data[stream.pos]; ret = stbi_zlib_decode_buffer((char*)image, out_size, (const char*)buf, size); } else { open_image_file(); image_file.set_item(handle, AssetFile::IMAGE_DATA); FileStream stream(image_file); load_image_info(*this, stream, size, out_size); buf = new unsigned char[size]; image_file.read(buf, size); ret = stbi_zlib_decode_buffer((char*)image, out_size, (const char*)buf, size); delete[] buf; } if (ret < 0) { std::cout << "Could not load image " << handle << std::endl; std::cout << stbi_failure_reason() << std::endl; stbi_image_free(image); image = NULL; } }
LDMZ_MAP *mapLoadReal(const char *fname, int tile_w, int tile_h) { LDMZ_MAP *map; LDMZ_FILE_MAP map_h; LDMZ_FILE_STRTABLE_REF *map_r; LDMZ_FILE_LAYER *map_l; LDMZ_FILE_OBJECT *object; FILESYSTEM_FILE *file; void *buff, *tmp; char *stringdata; int i, iso, tile_h_inc; map_h.magic = map_h.version = 0; if ((file = fsFileOpen(fname, "rb")) == NULL) return NULL; fsFileReadInts((unsigned int *) &map_h, sizeof(LDMZ_FILE_MAP) >> 2, file); if (map_h.magic != LDMZ_MAGIC) { fsFileClose(file); return NULL; } if (map_h.version != LDMZ_VERSION && map_h.version != LDMZ_VERSION_ISOM) { fsFileClose(file); return NULL; } if ((buff = malloc(map_h.strtable_zlen)) == NULL) { fsFileClose(file); return NULL; } iso = (map_h.version == LDMZ_VERSION) ? 0 : 1; stringdata = malloc(map_h.strtable_len); map = malloc(sizeof(LDMZ_MAP)); map->stringrefs = malloc(sizeof(LDMZ_REF) * (map_h.strtable_refs_len / sizeof(LDMZ_FILE_STRTABLE_REF))); map_r = malloc(map_h.strtable_refs_len); map_l = malloc(sizeof(LDMZ_FILE_LAYER) * map_h.layers); object = malloc(map_h.objects * sizeof(LDMZ_OBJECT)); map->object = malloc(sizeof(LDMZ_OBJECT) * (map_h.objects)); map->layer = malloc(sizeof(LDMZ_LAYER) * map_h.layers); map->isometric = !(map_h.version == LDMZ_VERSION); map->stringdata = stringdata; map->cam_x = map->cam_y = 0; map->layers = map_h.layers; map->objects = map_h.objects; if (!map || (!stringdata && map_h.strtable_len > 0) || (!map->stringrefs && map_h.strtable_refs_len > 0) || (!map->object && map_h.objects) || (!map->layer && map_h.layers) || (!map_r && map_h.strtable_refs_len) || (!object && map_h.objects) || (!map_l)) goto error; /* Down at the bottom of the function */ /**** STRINGS ****/ fsFileRead(buff, map_h.strtable_zlen, file); stbi_zlib_decode_buffer(stringdata, map_h.strtable_len, buff, map_h.strtable_zlen); /**** REFS ****/ if ((tmp = realloc(buff, map_h.strtable_refs_zlen)) == NULL) goto error; /* Down at the bottom of the function */ buff = tmp; fsFileRead(buff, map_h.strtable_refs_zlen, file); stbi_zlib_decode_buffer((void *) map_r, map_h.strtable_refs_len, buff, map_h.strtable_refs_zlen); utilBlockToHostEndian((unsigned int *) map_r, map_h.strtable_refs_len / 4); for (i = 0; i < (map_h.strtable_refs_len / sizeof(LDMZ_FILE_STRTABLE_REF)); i++) { if (map_r[i].key != -1) { map->stringrefs[i].key = &stringdata[map_r[i].key]; map->stringrefs[i].value = &stringdata[map_r[i].value]; } else { map->stringrefs[i].key = NULL; map->stringrefs[i].value = NULL; } } free(map_r); map_r = NULL; map->prop = &map->stringrefs[map_h.main_ref]; /**** OBJECTS ****/ if ((tmp = realloc(buff, map_h.object_zlen)) == NULL && map_h.objects > 0) { goto error; /* Down at the bottom of the function */ } buff = tmp; fsFileRead(buff, map_h.object_zlen, file); stbi_zlib_decode_buffer((void *) object, sizeof(LDMZ_OBJECT) * map_h.objects, buff, map_h.object_zlen); utilBlockToHostEndian((unsigned int *) object, (sizeof(LDMZ_OBJECT) * map_h.objects) >> 2); for (i = 0; i < map_h.objects; i++) { map->object[i].ref = &map->stringrefs[object[i].ref]; map->object[i].x = object[i].x; map->object[i].y = object[i].y; map->object[i].l = object[i].l; } free(object); object = NULL; /**** MAP LAYER ****/ if ((tmp = realloc(buff, map_h.layer_zlen)) == NULL) goto error; /* Down at the bottom of the function */ buff = tmp; fsFileRead(buff, map_h.layer_zlen, file); stbi_zlib_decode_buffer((void *)map_l, sizeof(LDMZ_FILE_LAYER) * map_h.layers, buff, map_h.layer_zlen); utilBlockToHostEndian((unsigned int *) map_l, (sizeof(LDMZ_FILE_LAYER) * map_h.layers) >> 2); for (i = 0; i < map_h.layers; i++) { map->layer[i].tilemap = NULL; map->layer[i].ts = NULL; } for (i = 0; i < map_h.layers; i++) { map->layer[i].tile_w = (tile_w > 0) ? tile_w : map_l[i].tile_w; map->layer[i].tile_h = (tile_h > 0) ? tile_h : map_l[i].tile_h; if (iso && tile_h > 0) { tile_h_inc = (tile_h << 16) / (map_l[i].tile_h); map_l[i].layer_offset_x *= tile_h_inc; map_l[i].layer_offset_x >>= 16; } map_l[i].tile_w = 0; map_l[i].tile_h = 0; map->layer[i].offset_x = map_l[i].layer_offset_x; map->layer[i].offset_y = map_l[i].layer_offset_y; map->layer[i].ref = &map->stringrefs[map_l[i].prop_ref]; if ((tmp = realloc(buff, map_l[i].layer_zlen)) == NULL) goto error; /* Down at the bottom of the function */ buff = tmp; if (strcmp(mapLayerPropGet(map, i, "tileset"), "NO SUCH KEY") == 0) { if (strcmp(mapPropGet(map, "tileset"), "NO SUCH KEY") == 0) goto error; /* Down at the bottom of the function */ else { if (iso) { if ((map->layer[i].ts = renderTilesheetLoadIsometric(mapPropGet(map, "tileset"), map->layer[i].tile_w, map->layer[i].tile_h, PFORMAT_RGB5A1)) == NULL) goto error; /* Down at the bottom of the function */ } else { if ((map->layer[i].ts = renderTilesheetLoad(mapPropGet(map, "tileset"), map->layer[i].tile_w, map->layer[i].tile_h, PFORMAT_RGB5A1)) == NULL) goto error; /* Down at the bottom of the function */ } } if (strcmp(mapPropGet(map, "animation"), "NO SUCH KEY")) renderTilesheetAnimationApply(map->layer[i].ts, mapPropGet(map, "animation")); } else { if (iso) { if ((map->layer[i].ts = renderTilesheetLoadIsometric(mapPropGet(map, "tileset"), map->layer[i].tile_w, map->layer[i].tile_h, PFORMAT_RGB5A1)) == NULL) goto error; /* Down at the bottom of the function */ } else { if ((map->layer[i].ts = renderTilesheetLoad(mapPropGet(map, "tileset"), map->layer[i].tile_w, map->layer[i].tile_h, PFORMAT_RGB5A1)) == NULL) goto error; /* Down at the bottom of the function */ } if (strcmp(mapLayerPropGet(map, i, "animation"), "NO SUCH KEY")) renderTilesheetAnimationApply(map->layer[i].ts, mapLayerPropGet(map, i, "animation")); } if (iso) { if ((map->layer[i].tilemap = tilemapNew(TILEMAP_DEFAULT_INV_DIV, map->layer[i].ts, TILEMAP_DEFAULT_INV_DIV, map_l[i].layer_w, map_l[i].layer_h, map_l[i].layer_offset_x)) == NULL) goto error; /* Down at the bottom of the function */ } else { if ((map->layer[i].tilemap = tilemapNew(TILEMAP_DEFAULT_INV_DIV, map->layer[i].ts, TILEMAP_DEFAULT_INV_DIV, map_l[i].layer_w, map_l[i].layer_h, 0)) == NULL) goto error; /* Down at the bottom of the function */ } if (!iso) renderTilemapCameraMove(map->layer[i].tilemap->render, map->cam_x + map->layer[i].offset_x, map->cam_y + map->layer[i].offset_y); else renderTilemapCameraMove(map->layer[i].tilemap->render, 0, 0); fsFileRead(buff, map_l[i].layer_zlen, file); stbi_zlib_decode_buffer((void *) map->layer[i].tilemap->data, map_l[i].layer_w * map_l[i].layer_h * 4, buff, map_l[i].layer_zlen); utilBlockToHostEndian(map->layer[i].tilemap->data, map_l[i].layer_w * map_l[i].layer_h); renderTilemapForceRecalc(map->layer[i].tilemap->render); }
LDMZ_MAP *mapLoad(FILE *fp) { LDMZ_MAP *map; LDMZ_FILE_MAP map_h; LDMZ_FILE_STRTABLE_REF *map_r; LDMZ_FILE_LAYER *map_l; LDMZ_FILE_OBJECT *object; void *buff, *tmp; char *stringdata; int i; fread((unsigned int *) &map_h, sizeof(LDMZ_FILE_MAP), 1, fp); utilBlockToHostEndian((unsigned int *) &map_h, sizeof(LDMZ_FILE_MAP) >> 2); if (map_h.magic != LDMZ_MAGIC || map_h.version != LDMZ_VERSION) { fclose(fp); return NULL; } if ((buff = malloc(map_h.strtable_zlen)) == NULL) { return NULL; } stringdata = malloc(map_h.strtable_len); map = malloc(sizeof(LDMZ_MAP)); map->stringrefs = malloc(sizeof(LDMZ_REF) * (map_h.strtable_refs_len / sizeof(LDMZ_FILE_STRTABLE_REF))); map_r = malloc(map_h.strtable_refs_len); map_l = malloc(sizeof(LDMZ_FILE_LAYER) * map_h.layers); object = malloc(map_h.objects * sizeof(LDMZ_OBJECT)); map->object = malloc(sizeof(LDMZ_OBJECT) * (map_h.objects)); map->layer = malloc(sizeof(LDMZ_LAYER) * map_h.layers); map->stringdata = stringdata; map->cam_x = map->cam_y = 0; map->layers = map_h.layers; map->objects = map_h.objects; if (!map || (!stringdata && map_h.strtable_len > 0) || (!map->stringrefs && map_h.strtable_refs_len > 0) || (!map->object && map_h.objects) || (!map->layer && map_h.layers) || (!map_r && map_h.strtable_refs_len) || (!object && map_h.objects) || (!map_l)) goto error; /* Down at the bottom of the function */ /**** STRINGS ****/ fread(buff, map_h.strtable_zlen, 1, fp); stbi_zlib_decode_buffer(stringdata, map_h.strtable_len, buff, map_h.strtable_zlen); /**** REFS ****/ if ((tmp = realloc(buff, map_h.strtable_refs_zlen)) == NULL) goto error; /* Down at the bottom of the function */ buff = tmp; fread(buff, map_h.strtable_refs_zlen, 1, fp); stbi_zlib_decode_buffer((void *) map_r, map_h.strtable_refs_len, buff, map_h.strtable_refs_zlen); utilBlockToHostEndian((unsigned int *) map_r, map_h.strtable_refs_len / 4); for (i = 0; i < (map_h.strtable_refs_len / sizeof(LDMZ_FILE_STRTABLE_REF)); i++) { if (map_r[i].key != -1) { map->stringrefs[i].key = &stringdata[map_r[i].key]; map->stringrefs[i].value = &stringdata[map_r[i].value]; } else { map->stringrefs[i].key = NULL; map->stringrefs[i].value = NULL; } } free(map_r); map_r = NULL; map->prop = &map->stringrefs[map_h.main_ref]; /**** OBJECTS ****/ if ((tmp = realloc(buff, map_h.object_zlen)) == NULL && map_h.objects > 0) { goto error; /* Down at the bottom of the function */ } buff = tmp; fread(buff, map_h.object_zlen, 1, fp); stbi_zlib_decode_buffer((void *) object, sizeof(LDMZ_OBJECT) * map_h.objects, buff, map_h.object_zlen); utilBlockToHostEndian((unsigned int *) object, (sizeof(LDMZ_OBJECT) * map_h.objects) >> 2); for (i = 0; i < map_h.objects; i++) { map->object[i].ref = &map->stringrefs[object[i].ref]; map->object[i].x = object[i].x; map->object[i].y = object[i].y; map->object[i].l = object[i].l; } free(object); object = NULL; /**** MAP LAYER ****/ if ((tmp = realloc(buff, map_h.layer_zlen)) == NULL) goto error; /* Down at the bottom of the function */ for (i = 0; i < map_h.layers; i++) map->layer[i].tilemap = NULL; buff = tmp; fread(buff, map_h.layer_zlen, 1, fp); stbi_zlib_decode_buffer((void *)map_l, sizeof(LDMZ_FILE_LAYER) * map_h.layers, buff, map_h.layer_zlen); utilBlockToHostEndian((unsigned int *) map_l, (sizeof(LDMZ_FILE_LAYER) * map_h.layers) >> 2); for (i = 0; i < map_h.layers; i++) { map->layer[i].offset_x = map_l[i].layer_offset_x; map->layer[i].offset_y = map_l[i].layer_offset_y; map->layer[i].tile_w = map_l[i].tile_w; map->layer[i].tile_h = map_l[i].tile_h; map->layer[i].ref = &map->stringrefs[map_l[i].prop_ref]; if ((tmp = realloc(buff, map_l[i].layer_zlen)) == NULL) goto error; /* Down at the bottom of the function */ buff = tmp; if ((map->layer[i].tilemap = mapNewTilemap(map_l[i].layer_w, map_l[i].layer_h)) == NULL) goto error; fread(buff, map_l[i].layer_zlen, 1, fp); stbi_zlib_decode_buffer((void *) map->layer[i].tilemap->data, map_l[i].layer_w * map_l[i].layer_h * 4, buff, map_l[i].layer_zlen); utilBlockToHostEndian(map->layer[i].tilemap->data, map_l[i].layer_w * map_l[i].layer_h); } free(map_l); free(buff); return map; error: /* I know... This is my first goto in C. Ever. <.< */ for (i = 0; i < map_h.layers; i++) mapDeleteTilemap(map->layer[i].tilemap); free(map_l); free(map_r); free(object); free(buff); mapDestroy(map); return NULL; }