/* Archive::open * Reads an archive from disk * Returns true if successful, false otherwise *******************************************************************/ bool Archive::open(string filename) { // Read the file into a MemChunk MemChunk mc; if (!mc.importFile(filename)) { Global::error = "Unable to open file. Make sure it isn't in use by another program."; return false; } // Update filename before opening string backupname = this->filename; this->filename = filename; // Load from MemChunk sf::Clock timer; if (open(mc)) { LOG_MESSAGE(2, "Archive::open took %dms", timer.getElapsedTime().asMilliseconds()); this->on_disk = true; return true; } else { this->filename = backupname; return false; } }
void updateEntry() { // Read file MemChunk data; data.importFile(filename); // Read image SImage image; image.open(data, 0, "png"); image.convertPaletted(&palette); // Convert image to entry gfx format SIFormat* format = SIFormat::getFormat(gfx_format); if (format) { MemChunk conv_data; if (format->saveImage(image, conv_data, &palette)) { // Update entry data entry->importMemChunk(conv_data); EntryOperations::setGfxOffsets(entry, offsets.x, offsets.y); } else { LOG_MESSAGE(1, "Unable to convert external png to %s", format->getName()); } } }
/* PaletteManager::loadCustomPalettes * Loads any files in the '<userdir>/palettes' directory as palettes, * with names from the files (minus the file extension) *******************************************************************/ bool PaletteManager::loadCustomPalettes() { // If the directory doesn't exist create it if (!wxDirExists(appPath("palettes", DIR_USER))) wxMkdir(appPath("palettes", DIR_USER)); // Open the custom palettes directory wxDir res_dir; res_dir.Open(appPath("palettes", DIR_USER)); // Go through each file in the directory string filename = wxEmptyString; bool files = res_dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES); while (files) { // Load palette data Palette8bit* pal = new Palette8bit(); MemChunk mc; mc.importFile(res_dir.GetName() + "/" + filename); pal->loadMem(mc); // Add the palette wxFileName fn(filename); addPalette(pal, fn.GetName()); // Next file files = res_dir.GetNext(&filename); } return true; }
// ----------------------------------------------------------------------------- // Reads an archive from disk // Returns true if successful, false otherwise // ----------------------------------------------------------------------------- bool Archive::open(string_view filename) { // Read the file into a MemChunk MemChunk mc; if (!mc.importFile(filename)) { Global::error = "Unable to open file. Make sure it isn't in use by another program."; return false; } // Update filename before opening auto backupname = filename_; filename_ = filename; // Load from MemChunk sf::Clock timer; if (open(mc)) { Log::info(2, "Archive::open took {}ms", timer.getElapsedTime().asMilliseconds()); on_disk_ = true; return true; } else { filename_ = backupname; return false; } }
/* ColourConfiguration::init * Initialises the colour configuration *******************************************************************/ bool ColourConfiguration::init() { // Load default configuration loadDefaults(); // Check for saved colour configuration if (wxFileExists(appPath("colours.cfg", DIR_USER))) { MemChunk ccfg; ccfg.importFile(appPath("colours.cfg", DIR_USER)); readConfiguration(ccfg); } return true; }
// ----------------------------------------------------------------------------- // Initialises the colour configuration // ----------------------------------------------------------------------------- bool ColourConfiguration::init() { // Load default configuration loadDefaults(); // Check for saved colour configuration if (wxFileExists(App::path("colours.cfg", App::Dir::User))) { MemChunk ccfg; ccfg.importFile(App::path("colours.cfg", App::Dir::User)); readConfiguration(ccfg); } return true; }
/* ZipArchive::write * Writes the zip archive to a MemChunk * Returns true if successful, false otherwise *******************************************************************/ bool ZipArchive::write(MemChunk& mc, bool update) { bool success = false; // Write to a temporary file string tempfile = appPath("slade-temp-write.zip", DIR_TEMP); if (write(tempfile, true)) { // Load file into MemChunk success = mc.importFile(tempfile); } // Clean up wxRemoveFile(tempfile); return success; }
/* Executables::init * Reads all executable definitions from the program resource * and user dir *******************************************************************/ void Executables::init() { // Load from pk3 Archive* res_archive = theArchiveManager->programResourceArchive(); ArchiveEntry* entry = res_archive->entryAtPath("config/executables.cfg"); if (!entry) return; // Parse base executables config Parser p; p.parseText(entry->getMCData(), "slade.pk3 - executables.cfg"); parse(&p, false); // Parse user executables config Parser p2; MemChunk mc; if (mc.importFile(App::path("executables.cfg", App::Dir::User))) { p2.parseText(mc, "user execuatbles.cfg"); parse(&p2, true); } }
/* EntryType::loadEntryTypes * Loads all built-in and custom user entry types *******************************************************************/ bool EntryType::loadEntryTypes() { EntryDataFormat* fmt_any = EntryDataFormat::anyFormat(); // Setup unknown type etype_unknown.format = fmt_any; etype_unknown.icon = "e_unknown"; etype_unknown.detectable = false; etype_unknown.reliability = 0; etype_unknown.addToList(); // Setup folder type etype_folder.format = fmt_any; etype_folder.icon = "e_folder"; etype_folder.name = "Folder"; etype_folder.detectable = false; etype_folder.addToList(); // Setup marker type etype_marker.format = fmt_any; etype_marker.icon = "e_marker"; etype_marker.name = "Marker"; etype_marker.detectable = false; etype_marker.category = ""; // No category, markers only appear when 'All' categories shown etype_marker.addToList(); // Setup map marker type etype_map.format = fmt_any; etype_map.icon = "e_map"; etype_map.name = "Map Marker"; etype_map.category = "Maps"; // Should appear with maps etype_map.detectable = false; etype_map.colour = rgba_t(0, 255, 0); etype_map.addToList(); // -------- READ BUILT-IN TYPES --------- // Get builtin entry types from resource archive Archive* res_archive = theArchiveManager->programResourceArchive(); // Check resource archive exists if (!res_archive) { wxLogMessage("Error: No resource archive open!"); return false; } // Get entry types directory ArchiveTreeNode* et_dir = res_archive->getDir("config/entry_types/"); // Check it exists if (!et_dir) { wxLogMessage("Error: config/entry_types does not exist in slade.pk3"); return false; } // Read in each file in the directory bool etypes_read = false; for (unsigned a = 0; a < et_dir->numEntries(); a++) { if (readEntryTypeDefinition(et_dir->getEntry(a)->getMCData())) etypes_read = true; } // Warn if no types were read (this shouldn't happen unless the resource archive is corrupted) if (!etypes_read) wxLogMessage("Warning: No built-in entry types could be loaded from slade.pk3"); // -------- READ CUSTOM TYPES --------- // If the directory doesn't exist create it if (!wxDirExists(appPath("entry_types", DIR_USER))) wxMkdir(appPath("entry_types", DIR_USER)); // Open the custom palettes directory wxDir res_dir; res_dir.Open(appPath("entry_types", DIR_USER)); // Go through each file in the directory string filename = wxEmptyString; bool files = res_dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES); while (files) { // Load file data MemChunk mc; mc.importFile(res_dir.GetName() + "/" + filename); // Parse file readEntryTypeDefinition(mc); // Next file files = res_dir.GetNext(&filename); } return true; }
// ----------------------------------------------------------------------------- // Game related initialisation (read basic definitions, etc.) // ----------------------------------------------------------------------------- void Game::init() { // Init static ThingTypes ThingType::initGlobal(); // Init static ActionSpecials ActionSpecial::initGlobal(); // Add game configurations from user dir wxArrayString allfiles; wxDir::GetAllFiles(App::path("games", App::Dir::User), &allfiles); for (unsigned a = 0; a < allfiles.size(); a++) { // Read config info MemChunk mc; mc.importFile(allfiles[a]); // Add to list if valid GameDef gdef; if (gdef.parse(mc)) { gdef.filename = wxFileName(allfiles[a]).GetName(); gdef.user = true; game_defs[gdef.name] = gdef; } } // Add port configurations from user dir allfiles.clear(); wxDir::GetAllFiles(App::path("ports", App::Dir::User), &allfiles); for (unsigned a = 0; a < allfiles.size(); a++) { // Read config info MemChunk mc; mc.importFile(allfiles[a]); // Add to list if valid PortDef pdef; if (pdef.parse(mc)) { pdef.filename = wxFileName(allfiles[a]).GetName(); pdef.user = true; port_defs[pdef.name] = pdef; } } // Add game configurations from program resource auto dir = App::archiveManager().programResourceArchive()->getDir("config/games"); if (dir) { for (auto& entry : dir->entries()) { // Read config info GameDef conf; if (!conf.parse(entry.get()->getMCData())) continue; // Ignore if invalid // Add to list if it doesn't already exist if (game_defs.find(conf.name) == game_defs.end()) { conf.filename = entry.get()->getName(true); conf.user = false; game_defs[conf.name] = conf; } } } // Add port configurations from program resource dir = App::archiveManager().programResourceArchive()->getDir("config/ports"); if (dir) { for (auto& entry : dir->entries()) { // Read config info PortDef conf; if (!conf.parse(entry.get()->getMCData())) continue; // Ignore if invalid // Add to list if it doesn't already exist if (port_defs.find(conf.name) == port_defs.end()) { conf.filename = entry.get()->getName(true); conf.user = false; port_defs[conf.name] = conf; } } } // Load last configuration if any if (game_configuration != "") config_current.openConfig(game_configuration, port_configuration); // Load custom special presets if (!loadCustomSpecialPresets()) Log::warning("An error occurred loading user special_presets.cfg"); // Load zdoom.pk3 stuff if (wxFileExists(zdoom_pk3_path)) { std::thread thread([=]() { ZipArchive zdoom_pk3; if (!zdoom_pk3.open(zdoom_pk3_path)) return; // ZScript auto zscript_entry = zdoom_pk3.entryAtPath("zscript.txt"); if (!zscript_entry) { // Bail out if no entry is found. Log::warning(1, "Could not find \'zscript.txt\' in " + zdoom_pk3_path); } else { zscript_base.parseZScript(zscript_entry); auto lang = TextLanguage::fromId("zscript"); if (lang) lang->loadZScript(zscript_base); // MapInfo auto mapinfo_entry = zdoom_pk3.entryAtPath("zmapinfo.txt"); config_current.parseMapInfo(&zdoom_pk3); } }); thread.detach(); } // Init game listener listener = std::make_unique<GameListener>(); }