void QuickSaves::delete_surplus_saves(size_t max_saves) { if (max_saves < 1) return; // unlimited saves, no need to prune clear(); // Check the directory to count the saves. If there // are fewer than the max, no need to go further. vector<dir_entry> entries; DirectorySpecifier path; path.SetToQuickSavesDir(); if (path.ReadDirectory(entries)) { if (entries.size() <= max_saves) return; } // We might have too many unnamed saves; load and // count them, deleting any extras. enumerate(); size_t unnamed_saves = 0; for (std::vector<QuickSave>::iterator it = begin(); it != end(); ++it) { if (it->name.length()) continue; if (++unnamed_saves > max_saves) delete_quick_save(*it); } clear(); }
void QuickSaves::enumerate() { clear(); logContext("parsing quick saves"); QuickSaveLoader loader; DirectorySpecifier path; path.SetToQuickSavesDir(); loader.ParseDirectory(path); clear_game_error(); std::sort(m_saves.begin(), m_saves.end()); std::reverse(m_saves.begin(), m_saves.end()); }
bool PluginLoader::ParsePlugin(FileSpecifier& file_name) { OpenedFile file; if (file_name.Open(file)) { int32 data_size; file.GetLength(data_size); m_data.resize(data_size); if (file.Read(data_size, &m_data[0])) { file_name.ToDirectory(current_plugin_directory); char name[256]; current_plugin_directory.GetName(name); m_name = name; if (!DoParse()) { logError1("There were parsing errors in %s Plugin.xml\n", m_name.c_str()); } } m_data.clear(); return true; } return false; }
static bool _ParseMMLDirectory(DirectorySpecifier& dir) { // Get sorted list of files in directory 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 ParseMMLFromFile(file_name); } return true; }
bool FileFinder::Find(DirectorySpecifier &dir, Typecode type, bool recursive) { // Get list of entries in directory vector<dir_entry> entries; if (!dir.ReadDirectory(entries)) return false; sort(entries.begin(), entries.end()); // Iterate through entries vector<dir_entry>::const_iterator i, end = entries.end(); for (i = entries.begin(); i != end; i++) { // Construct full specifier of file/dir FileSpecifier file = dir + i->name; if (i->is_directory) { // Recurse into directory if (recursive) if (Find(file, type, recursive)) return true; } else { // Check file type and call found() function if (type == WILDCARD_TYPE || type == file.GetType()) if (found(file)) return true; } } return false; }
bool FileFinder::Find(DirectorySpecifier &dir, Typecode type, bool recursive) { // Get list of entries in directory vector<dir_entry> entries; if (!dir.ReadDirectory(entries)) { return false; } sort(entries.begin(), entries.end()); // Iterate through entries vector<dir_entry>::const_iterator i, end = entries.end(); for (i = entries.begin(); i != end; i++) { // Construct full specifier of file/dir FileSpecifier file = dir + i->name; // DJB Skip our texture directories if ( dir.GetPathString().find ( "SpriteTextures" ) != string::npos || dir.GetPathString().find ( "TTEP" ) != string::npos ) { // Don't go down these paths return false; } if (i->is_directory) { // Recurse into directory if (recursive) { if (Find(file, type, recursive)) { return true; } } } else { // Check file type and call found() function if (type == WILDCARD_TYPE || type == file.GetType()) { if (found(file)) { return true; } } } } return false; }
bool expand_symbolic_paths_helper(char *dest, const char *src, int maxlen, const char *symbol, DirectorySpecifier& dir) { int symlen = strlen(symbol); if (!strncmp(src, symbol, symlen)) { strncpy(dest, dir.GetPath(), maxlen); dest[maxlen] = '\0'; strncat(dest, &src[symlen], maxlen-strlen(dest)); return true; } return false; }
bool contract_symbolic_paths_helper(char *dest, const char *src, int maxlen, const char *symbol, DirectorySpecifier &dir) { const char *dpath = dir.GetPath(); int dirlen = strlen(dpath); if (!strncmp(src, dpath, dirlen)) { strncpy(dest, symbol, maxlen); dest[maxlen] = '\0'; strncat(dest, &src[dirlen], maxlen-strlen(dest)); return true; } return false; }
/* --------- Local Code --------- */ // static OSErr enumerate_files( // struct find_file_pb *param_block, // long directory_id) /* Because it is recursive.. */ bool FileFinder::Enumerate(DirectorySpecifier& Dir) { // Set up the temporary file TempFile.FromDirectory(Dir); // Kludge to make the FSSpec always available FSSpec& temp_file = TempFile.GetSpec(); // Local variable short index; obj_clear(pb); pb.hFileInfo.ioVRefNum= Dir.Get_vRefNum(); pb.hFileInfo.ioNamePtr= temp_file.name; // ZZZ addition bool KeepGoing = true; for(Err= noErr, index=1; count<max && Err==noErr && KeepGoing; index++) { pb.hFileInfo.ioDirID= Dir.Get_parID(); pb.hFileInfo.ioFDirIndex= index; Err= PBGetCatInfo( &pb, false); if(Err == noErr) { if ((pb.hFileInfo.ioFlAttrib & 16) && (flags & _ff_recurse)) { /* Recurse, if you really want to... */ // ZZZ addition: callback on directory change if desired if(directory_change_callback != NULL) KeepGoing = directory_change_callback(TempFile, true, (flags & _ff_callback_with_catinfo) ? &pb : user_data); if(KeepGoing) { // Set up volume info DirectorySpecifier SubDir = Dir; // Change the directory SubDir.Set_parID(pb.dirInfo.ioDrDirID); // Go! Enumerate(SubDir); // Reset the temporary file's directory to what's appropriate here // (it's global to this object) TempFile.FromDirectory(Dir); // ZZZ addition: callback on directory change if desired if(directory_change_callback != NULL) KeepGoing = directory_change_callback(TempFile, false, (flags & _ff_callback_with_catinfo) ? &pb : user_data); } } else { /* Add.. */ if(Type==WILDCARD_TYPE || Type == TempFile.GetType()) { /* Only add if there isn't a callback or it returns true */ switch(search_type) { case _fill_buffer: if(!callback || callback(TempFile, user_data)) { /* Copy it in.. */ buffer[count++] = TempFile; } break; case _callback_only: assert(callback); KeepGoing = callback(TempFile, (flags & _ff_callback_with_catinfo) ? &pb : user_data); break; default: assert(false); break; } } } } else { /* We ran out of files.. */ } } /* If we got a fnfErr, it was because we indexed too far. */ if (Err == fnfErr) Err = noErr; return (Err == noErr); }
static void initialize_application(void) { #if defined(__WIN32__) && defined(__MINGW32__) if (LoadLibrary("exchndl.dll")) option_debug = true; #endif // SDL_putenv(const_cast<char*>("SDL_VIDEO_ALLOW_SCREENSAVER=1")); // Initialize SDL int retval = SDL_Init(SDL_INIT_VIDEO | (option_nosound ? 0 : SDL_INIT_AUDIO) | (option_nojoystick ? 0 : SDL_INIT_JOYSTICK) | (option_debug ? SDL_INIT_NOPARACHUTE : 0)); if (retval < 0) { const char *sdl_err = SDL_GetError(); if (sdl_err) fprintf(stderr, "Couldn't initialize SDL (%s)\n", sdl_err); else fprintf(stderr, "Couldn't initialize SDL\n"); exit(1); } #if defined(HAVE_SDL_IMAGE) IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG); #endif // We only want text input events at specific times SDL_StopTextInput(); // Find data directories, construct search path InitDefaultStringSets(); #if defined(unix) || defined(__NetBSD__) || defined(__OpenBSD__) || (defined(__APPLE__) && defined(__MACH__) && !defined(HAVE_BUNDLE_NAME)) default_data_dir = PKGDATADIR; const char *home = getenv("HOME"); if (home) local_data_dir = home; local_data_dir += ".alephone"; log_dir = local_data_dir; #elif defined(__APPLE__) && defined(__MACH__) bundle_data_dir = bundle_resource_path; bundle_data_dir += "DataFiles"; data_search_path.push_back(bundle_data_dir); #ifndef SCENARIO_IS_BUNDLED { char* buf = getcwd(0, 0); default_data_dir = buf; free(buf); } #endif log_dir = app_log_directory; preferences_dir = app_preferences_directory; local_data_dir = app_support_directory; #elif defined(__WIN32__) char file_name[MAX_PATH]; GetModuleFileName(NULL, file_name, sizeof(file_name)); char *sep = strrchr(file_name, '\\'); *sep = '\0'; default_data_dir = file_name; char login[17]; DWORD len = 17; bool hasName = (GetUserName((LPSTR) login, &len) == TRUE); if (!hasName || strpbrk(login, "\\/:*?\"<>|") != NULL) strcpy(login, "Bob User"); DirectorySpecifier legacy_data_dir = file_name; legacy_data_dir += "Prefs"; legacy_data_dir += login; SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, file_name); local_data_dir = file_name; local_data_dir += "AlephOne"; log_dir = local_data_dir; #else default_data_dir = ""; local_data_dir = ""; //#error Data file paths must be set for this platform. #endif #if defined(__WIN32__) #define LIST_SEP ';' #else #define LIST_SEP ':' #endif // in case we need to redo search path later: size_t dsp_insert_pos = data_search_path.size(); size_t dsp_delete_pos = (size_t)-1; if (arg_directory != "") { default_data_dir = arg_directory; dsp_delete_pos = data_search_path.size(); data_search_path.push_back(arg_directory); } const char *data_env = getenv("ALEPHONE_DATA"); if (data_env) { // Read colon-separated list of directories string path = data_env; string::size_type pos; while ((pos = path.find(LIST_SEP)) != string::npos) { if (pos) { string element = path.substr(0, pos); data_search_path.push_back(element); } path.erase(0, pos + 1); } if (!path.empty()) data_search_path.push_back(path); } else { if (arg_directory == "") { dsp_delete_pos = data_search_path.size(); data_search_path.push_back(default_data_dir); } #if defined(__WIN32__) data_search_path.push_back(legacy_data_dir); #endif data_search_path.push_back(local_data_dir); } // Subdirectories #if defined(__MACH__) && defined(__APPLE__) DirectorySpecifier legacy_preferences_dir = local_data_dir; #elif defined(__WIN32__) DirectorySpecifier legacy_preferences_dir = legacy_data_dir; SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, file_name); preferences_dir = file_name; preferences_dir += "AlephOne"; #else preferences_dir = local_data_dir; #endif saved_games_dir = local_data_dir + "Saved Games"; quick_saves_dir = local_data_dir + "Quick Saves"; image_cache_dir = local_data_dir + "Image Cache"; recordings_dir = local_data_dir + "Recordings"; screenshots_dir = local_data_dir + "Screenshots"; #if defined(__APPLE__) && defined(__MACH__) if (app_screenshots_directory) screenshots_dir = app_screenshots_directory; #endif DirectorySpecifier local_mml_dir = local_data_dir + "MML"; DirectorySpecifier local_themes_dir = local_data_dir + "Themes"; // Setup resource manager initialize_resources(); init_physics_wad_data(); initialize_fonts(false); load_film_profile(FILM_PROFILE_DEFAULT, false); // Parse MML files LoadBaseMMLScripts(); // Check for presence of strings if (!TS_IsPresent(strERRORS) || !TS_IsPresent(strFILENAMES)) { fprintf(stderr, "Can't find required text strings (missing MML?).\n"); exit(1); } // Check for presence of files (one last chance to change data_search_path) if (!have_default_files()) { char chosen_dir[256]; if (alert_choose_scenario(chosen_dir)) { // remove original argument (or fallback) from search path if (dsp_delete_pos < data_search_path.size()) data_search_path.erase(data_search_path.begin() + dsp_delete_pos); // add selected directory where command-line argument would go data_search_path.insert(data_search_path.begin() + dsp_insert_pos, chosen_dir); default_data_dir = chosen_dir; // Parse MML files again, now that we have a new dir to search initialize_fonts(false); LoadBaseMMLScripts(); } } initialize_fonts(true); Plugins::instance()->enumerate(); #if defined(__WIN32__) || (defined(__MACH__) && defined(__APPLE__)) preferences_dir.CreateDirectory(); transition_preferences(legacy_preferences_dir); #endif // Load preferences initialize_preferences(); local_data_dir.CreateDirectory(); saved_games_dir.CreateDirectory(); quick_saves_dir.CreateDirectory(); { std::string scen = Scenario::instance()->GetName(); if (scen.length()) scen.erase(std::remove_if(scen.begin(), scen.end(), char_is_not_filesafe), scen.end()); if (!scen.length()) scen = "Unknown"; quick_saves_dir += scen; quick_saves_dir.CreateDirectory(); } image_cache_dir.CreateDirectory(); recordings_dir.CreateDirectory(); screenshots_dir.CreateDirectory(); local_mml_dir.CreateDirectory(); local_themes_dir.CreateDirectory(); WadImageCache::instance()->initialize_cache(); #ifndef HAVE_OPENGL graphics_preferences->screen_mode.acceleration = _no_acceleration; #endif if (force_fullscreen) graphics_preferences->screen_mode.fullscreen = true; if (force_windowed) // takes precedence over fullscreen because windowed is safer graphics_preferences->screen_mode.fullscreen = false; write_preferences(); Plugins::instance()->load_mml(); // SDL_WM_SetCaption(application_name, application_name); // #if defined(HAVE_SDL_IMAGE) && !(defined(__APPLE__) && defined(__MACH__)) // SDL_WM_SetIcon(IMG_ReadXPMFromArray(const_cast<char**>(alephone_xpm)), 0); // #endif atexit(shutdown_application); #if !defined(DISABLE_NETWORKING) // Initialize SDL_net if (SDLNet_Init () < 0) { fprintf (stderr, "Couldn't initialize SDL_net (%s)\n", SDLNet_GetError()); exit(1); } #endif if (TTF_Init() < 0) { fprintf (stderr, "Couldn't initialize SDL_ttf (%s)\n", TTF_GetError()); exit(1); } HTTPClient::Init(); // Initialize everything mytm_initialize(); // initialize_fonts(); SoundManager::instance()->Initialize(*sound_preferences); initialize_marathon_music_handler(); initialize_keyboard_controller(); initialize_gamma(); alephone::Screen::instance()->Initialize(&graphics_preferences->screen_mode); initialize_marathon(); initialize_screen_drawing(); initialize_dialogs(); initialize_terminal_manager(); initialize_shape_handler(); initialize_fades(); initialize_images_manager(); load_environment_from_preferences(); initialize_game_state(); }