bool XML_Loader_SDL::ParseFile(FileSpecifier &file_name) { // Open file OpenedFile file; if (file_name.Open(file)) { printf ( "Parsing: %s\n", file_name.GetPath() ); // Get file size and allocate buffer file.GetLength(data_size); data = new char[data_size]; // In case there were errors... file_name.GetName(FileName); // Read and parse file if (file.Read(data_size, data)) { if (!DoParse()) { fprintf(stderr, "There were parsing errors in configuration file %s\n", FileName); } } // Delete buffer delete[] data; data = NULL; return true; } else { printf ( "Couldn't open: %s\n", file_name.GetPath() ); } return false; }
bool XML_Loader_SDL::ParseDirectory(FileSpecifier &dir) { // Get sorted list of files in directory printf ( "Looking in %s\n", dir.GetPath() ); vector<dir_entry> de; if (!dir.ReadDirectory(de)) { return false; } sort(de.begin(), de.end()); // Parse each file vector<dir_entry>::const_iterator i, end = de.end(); for (i=de.begin(); i!=end; i++) { if (i->is_directory) { continue; } if (i->name[i->name.length() - 1] == '~') { continue; } // people stick Lua scripts in Scripts/ if (boost::algorithm::ends_with(i->name, ".lua")) { continue; } // Construct full path name FileSpecifier file_name = dir + i->name; // Parse file ParseFile(file_name); } return true; }
void operator() (const std::string& arg) const { if (!NetAllowSavingLevel()) { screen_printf("Level saving disabled"); return; } std::string filename = arg; if (filename == "") { if (last_level != "") filename = last_level; else { filename = mac_roman_to_utf8(static_world->level_name); if (!boost::algorithm::ends_with(filename, ".sceA")) filename += ".sceA"; } } else { if (!boost::algorithm::ends_with(filename, ".sceA")) filename += ".sceA"; } last_level = filename; FileSpecifier fs; fs.SetToLocalDataDir(); fs += filename; if (export_level(fs)) screen_printf("Saved %s", utf8_to_mac_roman(fs.GetPath()).c_str()); else screen_printf("An error occurred while saving the level"); }
void WadImageCache::save_cache() { if (!m_cache_dirty) return; InfoTree pt; for (cache_iter_t it = m_used.begin(); it != m_used.end(); ++it) { std::string name = it->second.first; WadImageDescriptor desc = boost::tuples::get<0>(it->first); pt.put(name + ".path", desc.file.GetPath()); pt.put(name + ".checksum", desc.checksum); pt.put(name + ".index", desc.index); pt.put(name + ".tag", desc.tag); pt.put(name + ".width", boost::tuples::get<1>(it->first)); pt.put(name + ".height", boost::tuples::get<2>(it->first)); pt.put(name + ".filesize", it->second.second); } FileSpecifier info; info.SetToImageCacheDir(); info.AddPart("Cache.ini"); try { pt.save_ini(info); m_cache_dirty = false; } catch (InfoTree::ini_error e) { logError("Could not save image cache to %s (%s)", info.GetPath(), e.what()); return; } }
// Exchange two files bool FileSpecifier::Exchange(FileSpecifier &other) { // Create temporary name (this is cheap, we should make sure that the // name is not already in use...) FileSpecifier tmp; ToDirectory(tmp); tmp.AddPart("exchange_tmp_file"); err = 0; if (rename(GetPath(), tmp.GetPath()) < 0) err = errno; else rename(other.GetPath(), GetPath()); if (rename(tmp.GetPath(), other.GetPath()) < 0) err = errno; return err == 0; }
bool PluginLoader::ParseDirectory(FileSpecifier& dir) { std::vector<dir_entry> de; if (!dir.ReadDirectory(de)) { return false; } for (std::vector<dir_entry>::const_iterator it = de.begin(); it != de.end(); ++it) { FileSpecifier file = dir + it->name; if (it->name == "Plugin.xml") { ParsePlugin(file); } else if (it->is_directory && it->name[0] != '.') { ParseDirectory(file); } #ifdef HAVE_ZZIP else if (algo::ends_with(it->name, ".zip") || algo::ends_with(it->name, ".ZIP")) { // search it for a Plugin.xml file ZZIP_DIR* zzipdir = zzip_dir_open(file.GetPath(), 0); if (zzipdir) { ZZIP_DIRENT dirent; while (zzip_dir_read(zzipdir, &dirent)) { if (algo::ends_with(dirent.d_name, "Plugin.xml")) { std::string archive = file.GetPath(); FileSpecifier file_name = FileSpecifier(archive.substr(0, archive.find_last_of('.'))) + dirent.d_name; ParsePlugin(file_name); } } zzip_dir_close(zzipdir); } } #endif } return true; }
bool SndfileDecoder::Open(FileSpecifier& File) { if (sndfile) { sf_close(sndfile); sndfile = 0; } sfinfo.format = 0; sndfile = sf_open(File.GetPath(), SFM_READ, &sfinfo); return sndfile; }
std::string WadImageCache::image_to_new_name(SDL_Surface *image, int32 *filesize) const { // create name boost::uuids::random_generator gen; boost::uuids::uuid u = gen(); std::string ustr = boost::uuids::to_string(u); FileSpecifier File; File.SetToImageCacheDir(); File.AddPart(ustr); FileSpecifier TempFile; TempFile.SetTempName(File); int ret; //#if defined(HAVE_PNG) && defined(HAVE_SDL_IMAGE) // ret = aoIMG_SavePNG(TempFile.GetPath(), image, IMG_COMPRESS_DEFAULT, NULL, 0); #ifdef HAVE_SDL_IMAGE ret = IMG_SavePNG(image, TempFile.GetPath()); #else ret = SDL_SaveBMP(image, TempFile.GetPath()); #endif if (ret == 0 && TempFile.Rename(File)) { if (filesize) { OpenedFile of; if (File.Open(of)) of.GetLength(*filesize); } return ustr; } if (filesize) *filesize = 0; return ""; }
static const char *locate_font(const std::string& path) { builtin_fonts_t::iterator j = builtin_fonts.find(path); if (j != builtin_fonts.end() || path == "") { return path.c_str(); } else { static FileSpecifier file; if (file.SetNameWithPath(path.c_str())) return file.GetPath(); else return ""; } }
void WadImageCache::initialize_cache() { FileSpecifier info; info.SetToImageCacheDir(); info.AddPart("Cache.ini"); if (!info.Exists()) return; InfoTree pt; try { pt = InfoTree::load_ini(info); } catch (InfoTree::ini_error e) { logError("Could not read image cache from %s (%s)", info.GetPath(), e.what()); } for (InfoTree::iterator it = pt.begin(); it != pt.end(); ++it) { std::string name = it->first; InfoTree ptc = it->second; WadImageDescriptor desc; std::string path; ptc.read("path", path); desc.file = FileSpecifier(path); ptc.read("checksum", desc.checksum); ptc.read("index", desc.index); ptc.read("tag", desc.tag); int width = 0; ptc.read("width", width); int height = 0; ptc.read("height", height); size_t filesize = 0; ptc.read("filesize", filesize); cache_key_t key = cache_key_t(desc, width, height); cache_value_t val = cache_value_t(name, filesize); m_used.push_front(cache_pair_t(key, val)); m_cacheinfo[key] = m_used.begin(); m_cachesize += filesize; } }
bool FFmpegDecoder::Open(FileSpecifier& File) { Close(); // make sure one-time setup succeeded if (!av || !av->temp_data || !av->fifo) return false; // open the file if (avformat_open_input(&av->ctx, File.GetPath(), NULL, NULL) != 0) return false; // retrieve format info if (avformat_find_stream_info(av->ctx, NULL) < 0) { Close(); return false; } // find the audio AVCodec *codec; av->stream_idx = av_find_best_stream(av->ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0); if (av->stream_idx < 0) { Close(); return false; } av->stream = av->ctx->streams[av->stream_idx]; if (avcodec_open2(av->stream->codec, codec, NULL) < 0) { Close(); return false; } channels = av->stream->codec->channels; rate = av->stream->codec->sample_rate; return true; }
bool LoadModel_Studio(FileSpecifier& Spec, Model3D& Model) { ModelPtr = &Model; Model.Clear(); Path = Spec.GetPath(); logNote1("Loading 3D Studio Max model file %s",Path); OpenedFile OFile; if (!Spec.Open(OFile)) { logError1("ERROR opening %s",Path); return false; } ChunkHeaderData ChunkHeader; if (!ReadChunkHeader(OFile,ChunkHeader)) return false; if (ChunkHeader.ID != MASTER) { logError1("ERROR: not a 3DS Max model file: %s",Path); return false; } if (!ReadContainer(OFile,ChunkHeader,ReadMaster)) return false; if (Model.Positions.empty()) { logError1("ERROR: no vertices found in %s",Path); return false; } if (Model.VertIndices.empty()) { logError1("ERROR: no faces found in %s",Path); return false; } return true; }
bool ImageDescriptor::LoadFromFile(FileSpecifier& File, int ImgMode, int flags, int actual_width, int actual_height, int maxSize) { if (flags & ImageLoader_ImageIsAlreadyPremultiplied) PremultipliedAlpha = true; // Don't load opacity if there is no color component: switch(ImgMode) { case ImageLoader_Colors: if (LoadDDSFromFile(File, flags, actual_width, actual_height, maxSize)) return true; break; case ImageLoader_Opacity: if (!IsPresent()) return false; break; default: vassert(false, csprintf(temporary,"Bad image mode for loader: %d",ImgMode)); } // Load image to surface #ifndef SDL_RFORK_HACK #ifdef HAVE_SDL_IMAGE SDL_Surface *s = IMG_Load(File.GetPath()); #else SDL_Surface *s = SDL_LoadBMP(File.GetPath()); #endif #else SDL_Surface *s = NULL; #endif if (s == NULL) return false; // Get image dimensions and set its size int Width = s->w, Height = s->h; int OriginalWidth = (actual_width) ? actual_width : Width; int OriginalHeight = (actual_height) ? actual_height : Height; if (flags & ImageLoader_ResizeToPowersOfTwo) { Width = NextPowerOfTwo(Width); Height = NextPowerOfTwo(Height); } switch (ImgMode) { case ImageLoader_Colors: Resize(Width, Height); VScale = ((double) OriginalWidth / (double) Width); UScale = ((double) OriginalHeight / (double) Height); MipMapCount = 0; break; case ImageLoader_Opacity: // If the wrong size, then bug out if (Width != this->Width || Height != this->Height || ((double) OriginalWidth / Width != VScale || ((double) OriginalHeight / Height != UScale))) { SDL_FreeSurface(s); return false; } break; } // Convert to 32-bit OpenGL-friendly RGBA surface #ifdef ALEPHONE_LITTLE_ENDIAN SDL_Surface *rgba = SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); #else SDL_Surface *rgba = SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff); #endif if (rgba == NULL) { SDL_FreeSurface(s); return false; } SDL_SetAlpha(s, 0, 0xff); // disable SDL_SRCALPHA SDL_BlitSurface(s, NULL, rgba, NULL); SDL_FreeSurface(s); // Convert surface to RGBA texture switch (ImgMode) { case ImageLoader_Colors: memcpy(GetPixelBasePtr(), rgba->pixels, Width * Height * 4); break; case ImageLoader_Opacity: { uint8 *p = (uint8 *)rgba->pixels; uint8 *q = (uint8 *)GetPixelBasePtr(); for (int h=0; h<Height; h++) { for (int w=0; w<Width; w++) { // RGB to greyscale value, and then to the opacity float Red = float(*p++); float Green = float(*p++); float Blue = float(*p++); p++; float Opacity = (Red + Green + Blue) / 3.0F; q[3] = PIN(int(Opacity + 0.5), 0, 255); q += 4; } } break; } } SDL_FreeSurface(rgba); return true; }
bool FileSpecifier::Rename(const FileSpecifier& Destination) { return rename(GetPath(), Destination.GetPath()) == 0; }
void dump_screen(void) { // Find suitable file name FileSpecifier file; int i = 0; do { char name[256]; const char* suffix; #ifdef HAVE_PNG suffix = "png"; #else suffix = "bmp"; #endif if (get_game_state() == _game_in_progress) { sprintf(name, "%s_%04d.%s", to_alnum(static_world->level_name).c_str(), i, suffix); } else { sprintf(name, "Screenshot_%04d.%s", i, suffix); } file = screenshots_dir + name; i++; } while (file.Exists()); #ifdef HAVE_PNG // build some nice metadata std::vector<IMG_PNG_text> texts; std::map<std::string, std::string> metadata; metadata["Source"] = expand_app_variables("$appName$ $appVersion$ ($appPlatform$)"); time_t rawtime; time(&rawtime); char time_string[32]; strftime(time_string, 32,"%d %b %Y %H:%M:%S +0000", gmtime(&rawtime)); metadata["Creation Time"] = time_string; if (get_game_state() == _game_in_progress) { const float FLOAT_WORLD_ONE = float(WORLD_ONE); const float AngleConvert = 360/float(FULL_CIRCLE); metadata["Level"] = static_world->level_name; char map_file_name[256]; FileSpecifier fs = environment_preferences->map_file; fs.GetName(map_file_name); metadata["Map File"] = map_file_name; if (Scenario::instance()->GetName().size()) { metadata["Scenario"] = Scenario::instance()->GetName(); } metadata["Polygon"] = boost::lexical_cast<std::string>(world_view->origin_polygon_index); metadata["X"] = boost::lexical_cast<std::string>(world_view->origin.x / FLOAT_WORLD_ONE); metadata["Y"] = boost::lexical_cast<std::string>(world_view->origin.y / FLOAT_WORLD_ONE); metadata["Z"] = boost::lexical_cast<std::string>(world_view->origin.z / FLOAT_WORLD_ONE); metadata["Yaw"] = boost::lexical_cast<std::string>(world_view->yaw * AngleConvert); short pitch = world_view->pitch; if (pitch > HALF_CIRCLE) pitch -= HALF_CIRCLE; metadata["Pitch"] = boost::lexical_cast<std::string>(pitch * AngleConvert); } for (std::map<std::string, std::string>::const_iterator it = metadata.begin(); it != metadata.end(); ++it) { IMG_PNG_text text; text.key = const_cast<char*>(it->first.c_str()); text.value = const_cast<char*>(it->second.c_str()); texts.push_back(text); } IMG_PNG_text* textp = texts.size() ? &texts[0] : 0; #endif // Without OpenGL, dumping the screen is easy if (!MainScreenIsOpenGL()) { //#ifdef HAVE_PNG // aoIMG_SavePNG(file.GetPath(), MainScreenSurface(), IMG_COMPRESS_DEFAULT, textp, texts.size()); #ifdef HAVE_SDL_IMAGE IMG_SavePNG(MainScreenSurface(), file.GetPath()); #else SDL_SaveBMP(MainScreenSurface(), file.GetPath()); #endif return; } int video_w = MainScreenPixelWidth(); int video_h = MainScreenPixelHeight(); #ifdef HAVE_OPENGL // Otherwise, allocate temporary surface... SDL_Surface *t = SDL_CreateRGBSurface(SDL_SWSURFACE, video_w, video_h, 24, #if SDL_BYTEORDER == SDL_LIL_ENDIAN 0x000000ff, 0x0000ff00, 0x00ff0000, 0); #else 0x00ff0000, 0x0000ff00, 0x000000ff, 0); #endif if (t == NULL) return; // ...and pixel buffer void *pixels = malloc(video_w * video_h * 3); if (pixels == NULL) { SDL_FreeSurface(t); return; } // Read OpenGL frame buffer glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0, 0, video_w, video_h, GL_RGB, GL_UNSIGNED_BYTE, pixels); glPixelStorei(GL_PACK_ALIGNMENT, 4); // return to default // Copy pixel buffer (which is upside-down) to surface for (int y = 0; y < video_h; y++) memcpy((uint8 *)t->pixels + t->pitch * y, (uint8 *)pixels + video_w * 3 * (video_h - y - 1), video_w * 3); free(pixels); // Save surface //#ifdef HAVE_PNG // aoIMG_SavePNG(file.GetPath(), t, IMG_COMPRESS_DEFAULT, textp, texts.size()); #ifdef HAVE_SDL_IMAGE IMG_SavePNG(t, file.GetPath()); #else SDL_SaveBMP(t, file.GetPath()); #endif SDL_FreeSurface(t); #endif }
bool get_default_theme_spec(FileSpecifier &file) { FileSpecifier theme = "Themes"; theme += getcstr(temporary, strFILENAMES, filenameDEFAULT_THEME); return get_default_spec(file, theme.GetPath()); }