diff options
author | Vas Crabb <vas@vastheman.com> | 2022-09-15 04:05:09 +1000 |
---|---|---|
committer | Vas Crabb <vas@vastheman.com> | 2022-09-15 04:05:09 +1000 |
commit | e06e0a9f1b9f22b37317c1d6565a066bfb855fc0 (patch) | |
tree | 2e26447fe93d1e7263b73e3de4298a12e4dd024e /src/emu | |
parent | 8f30e3071f59a2893208d4f53b40d4ffd0f93462 (diff) |
Fixes and cleanup:
* gbcolor.xml, bus/gameboy: Renamed slot option for Rocket Games
cartridges from "rom_atvrac" to "rom_rocket".
* emu/config.cpp: Made error messages more detailed when opening a
configuration file fails, and bumped error messages to warning level.
* tools/imgtool: Fixed build.
Diffstat (limited to 'src/emu')
-rw-r--r-- | src/emu/config.cpp | 118 | ||||
-rw-r--r-- | src/emu/config.h | 27 |
2 files changed, 80 insertions, 65 deletions
diff --git a/src/emu/config.cpp b/src/emu/config.cpp index c6b85bb6ae8..be9eccc8f99 100644 --- a/src/emu/config.cpp +++ b/src/emu/config.cpp @@ -15,7 +15,8 @@ #include "emuopts.h" #include "fileio.h" -#include "xmlfile.h" +#include <system_error> +#include <utility> #define DEBUG_CONFIG 0 @@ -28,8 +29,9 @@ //------------------------------------------------- -configuration_manager::configuration_manager(running_machine &machine) - : m_machine(machine) +configuration_manager::configuration_manager(running_machine &machine) : + m_machine(machine), + m_typelist() { } @@ -40,14 +42,9 @@ configuration_manager::configuration_manager(running_machine &machine) * *************************************/ -void configuration_manager::config_register(const char *nodename, load_delegate load, save_delegate save) +void configuration_manager::config_register(std::string_view name, load_delegate &&load, save_delegate &&save) { - config_element element; - element.name = nodename; - element.load = std::move(load); - element.save = std::move(save); - - m_typelist.emplace_back(std::move(element)); + m_typelist.emplace(name, config_handler{ std::move(load), std::move(save) }); } @@ -61,44 +58,31 @@ void configuration_manager::config_register(const char *nodename, load_delegate bool configuration_manager::load_settings() { // loop over all registrants and call their init function - for (const auto &type : m_typelist) - type.load(config_type::INIT, config_level::DEFAULT, nullptr); + for (auto const &type : m_typelist) + type.second.load(config_type::INIT, config_level::DEFAULT, nullptr); // now load the controller file char const *const controller = machine().options().ctrlr(); if (controller && *controller) { - // open the config file emu_file file(machine().options().ctrlr_path(), OPEN_FLAG_READ); - osd_printf_verbose("Attempting to parse: %s.cfg\n", controller); - std::error_condition const filerr = file.open(std::string(controller) + ".cfg"); - - if (filerr) - throw emu_fatalerror("Could not open controller file %s.cfg (%s:%d %s)", controller, filerr.category().name(), filerr.value(), filerr.message()); - - // load the XML - if (!load_xml(file, config_type::CONTROLLER)) - throw emu_fatalerror("Could not load controller file %s.cfg", controller); + if (!attempt_load(machine().system(), file, std::string(controller) + ".cfg", config_type::CONTROLLER)) + throw emu_fatalerror("Could not load controller configuration file %s.cfg", controller); } // next load the defaults file emu_file file(machine().options().cfg_directory(), OPEN_FLAG_READ); - std::error_condition filerr = file.open("default.cfg"); - osd_printf_verbose("Attempting to parse: default.cfg\n"); - if (!filerr) - load_xml(file, config_type::DEFAULT); + attempt_load(machine().system(), file, "default.cfg", config_type::DEFAULT); - // finally, load the game-specific file - filerr = file.open(machine().basename() + ".cfg"); - osd_printf_verbose("Attempting to parse: %s.cfg\n",machine().basename()); - const bool loaded = !filerr && load_xml(file, config_type::SYSTEM); + // load the system-specific file + bool const loaded = attempt_load(machine().system(), file, machine().basename() + ".cfg", config_type::SYSTEM); // loop over all registrants and call their final function - for (const auto &type : m_typelist) - type.load(config_type::FINAL, config_level::DEFAULT, nullptr); + for (auto const &type : m_typelist) + type.second.load(config_type::FINAL, config_level::DEFAULT, nullptr); // if we didn't find a saved config, return false so the main core knows that it - // is the first time the game is run and it should display the disclaimer. + // is the first time the system has been run return loaded; } @@ -106,8 +90,8 @@ bool configuration_manager::load_settings() void configuration_manager::save_settings() { // loop over all registrants and call their init function - for (const auto &type : m_typelist) - type.save(config_type::INIT, nullptr); + for (auto const &type : m_typelist) + type.second.save(config_type::INIT, nullptr); // save the defaults file emu_file file(machine().options().cfg_directory(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS); @@ -121,8 +105,8 @@ void configuration_manager::save_settings() save_xml(file, config_type::SYSTEM); // loop over all registrants and call their final function - for (const auto &type : m_typelist) - type.save(config_type::FINAL, nullptr); + for (auto const &type : m_typelist) + type.second.save(config_type::FINAL, nullptr); } @@ -133,13 +117,39 @@ void configuration_manager::save_settings() * *************************************/ -bool configuration_manager::load_xml(emu_file &file, config_type which_type) +bool configuration_manager::attempt_load(game_driver const &system, emu_file &file, std::string_view name, config_type which_type) +{ + std::error_condition const filerr = file.open(std::string(name)); + if (std::errc::no_such_file_or_directory == filerr) + { + osd_printf_verbose("Configuration file %s not found\n", name); + return false; + } + else if (filerr) + { + osd_printf_warning( + "Error opening configuration file %s (%s:%d %s)\n", + name, + filerr.category().name(), + filerr.value(), + filerr.message()); + return false; + } + else + { + osd_printf_verbose("Attempting to parse: %s\n", name); + return load_xml(system, file, which_type); + } +} + + +bool configuration_manager::load_xml(game_driver const &system, emu_file &file, config_type which_type) { // read the file util::xml::file::ptr const root(util::xml::file::read(file, nullptr)); if (!root) { - osd_printf_verbose("Error parsing XML configuration file %s\n", file.filename()); + osd_printf_warning("Error parsing XML configuration file %s\n", file.filename()); return false; } @@ -147,7 +157,7 @@ bool configuration_manager::load_xml(emu_file &file, config_type which_type) util::xml::data_node const *const confignode = root->get_child("mameconfig"); if (!confignode) { - osd_printf_verbose("Could not find root mameconfig element in configuration file %s\n", file.filename()); + osd_printf_warning("Could not find root mameconfig element in configuration file %s\n", file.filename()); return false; } @@ -155,18 +165,18 @@ bool configuration_manager::load_xml(emu_file &file, config_type which_type) int const version = confignode->get_attribute_int("version", 0); if (version != CONFIG_VERSION) { - osd_printf_verbose("Configuration file %s has unsupported version %d\n", file.filename(), version); + osd_printf_warning("Configuration file %s has unsupported version %d\n", file.filename(), version); return false; } // strip off all the path crap from the source filename - const char *srcfile = strrchr(machine().system().type.source(), '/'); + char const *srcfile = strrchr(system.type.source(), '/'); if (!srcfile) - srcfile = strrchr(machine().system().type.source(), '\\'); + srcfile = strrchr(system.type.source(), '\\'); if (!srcfile) - srcfile = strrchr(machine().system().type.source(), ':'); + srcfile = strrchr(system.type.source(), ':'); if (!srcfile) - srcfile = machine().system().type.source(); + srcfile = system.type.source(); else srcfile++; @@ -175,7 +185,7 @@ bool configuration_manager::load_xml(emu_file &file, config_type which_type) for (util::xml::data_node const *systemnode = confignode->get_child("system"); systemnode; systemnode = systemnode->get_next_sibling("system")) { // look up the name of the system here; skip if none - const char *name = systemnode->get_attribute_string("name", ""); + char const *name = systemnode->get_attribute_string("name", ""); // based on the file type, determine whether we have a match config_level level = config_level::DEFAULT; @@ -183,7 +193,7 @@ bool configuration_manager::load_xml(emu_file &file, config_type which_type) { case config_type::SYSTEM: // only match on the specific system name - if (strcmp(name, machine().system().name)) + if (strcmp(name, system.name)) { osd_printf_verbose("Ignoring configuration for system %s in system configuration file %s\n", name, file.filename()); continue; @@ -210,7 +220,7 @@ bool configuration_manager::load_xml(emu_file &file, config_type which_type) osd_printf_verbose("Applying default configuration from controller configuration file %s\n", file.filename()); level = config_level::DEFAULT; } - else if (!strcmp(name, machine().system().name)) + else if (!strcmp(name, system.name)) { osd_printf_verbose("Applying configuration for system %s from controller configuration file %s\n", name, file.filename()); level = config_level::SYSTEM; @@ -221,7 +231,7 @@ bool configuration_manager::load_xml(emu_file &file, config_type which_type) level = config_level::SOURCE; } else if ( - ((clone_of = driver_list::clone(machine().system())) != -1 && !strcmp(name, driver_list::driver(clone_of).name)) || + ((clone_of = driver_list::clone(system)) != -1 && !strcmp(name, driver_list::driver(clone_of).name)) || (clone_of != -1 && ((clone_of = driver_list::clone(clone_of)) != -1) && !strcmp(name, driver_list::driver(clone_of).name))) { osd_printf_verbose("Applying configuration for parent/BIOS %s from controller configuration file %s\n", name, file.filename()); @@ -243,8 +253,8 @@ bool configuration_manager::load_xml(emu_file &file, config_type which_type) osd_printf_debug("Entry: %s -- processing\n", name); // loop over all registrants and call their load function - for (const auto &type : m_typelist) - type.load(which_type, level, systemnode->get_child(type.name.c_str())); + for (auto const &type : m_typelist) + type.second.load(which_type, level, systemnode->get_child(type.first.c_str())); count++; } @@ -280,12 +290,12 @@ bool configuration_manager::save_xml(emu_file &file, config_type which_type) systemnode->set_attribute("name", (which_type == config_type::DEFAULT) ? "default" : machine().system().name); // loop over all registrants and call their save function - for (const auto &type : m_typelist) + for (auto const &type : m_typelist) { - util::xml::data_node *const curnode = systemnode->add_child(type.name.c_str(), nullptr); + util::xml::data_node *const curnode = systemnode->add_child(type.first.c_str(), nullptr); if (!curnode) return false; - type.save(which_type, curnode); + type.second.save(which_type, curnode); // if nothing was added, just nuke the node if (!curnode->get_value() && !curnode->get_first_child() && !curnode->count_attributes()) diff --git a/src/emu/config.h b/src/emu/config.h index 2a273ba248f..2ae05e0c32c 100644 --- a/src/emu/config.h +++ b/src/emu/config.h @@ -15,6 +15,10 @@ #include "xmlfile.h" +#include <map> +#include <string> +#include <string_view> + enum class config_type : int { @@ -46,27 +50,28 @@ public: // construction/destruction configuration_manager(running_machine &machine); - void config_register(const char *nodename, load_delegate load, save_delegate save); + void config_register(std::string_view name, load_delegate &&load, save_delegate &&save); + bool load_settings(); void save_settings(); - // getters - running_machine &machine() const { return m_machine; } - private: - struct config_element + struct config_handler { - std::string name; // node name - load_delegate load; // load callback - save_delegate save; // save callback + load_delegate load; + save_delegate save; }; - bool load_xml(emu_file &file, config_type which_type); + running_machine &machine() const { return m_machine; } + + bool attempt_load(game_driver const &system, emu_file &file, std::string_view name, config_type which_type); + + bool load_xml(game_driver const &system, emu_file &file, config_type which_type); bool save_xml(emu_file &file, config_type which_type); // internal state - running_machine & m_machine; // reference to our machine - std::vector<config_element> m_typelist; + running_machine &m_machine; + std::multimap<std::string, config_handler> m_typelist; }; #endif // MAME_EMU_CONFIG_H |