diff --git a/librecomp/include/recomp.h b/librecomp/include/recomp.h index 87bd054..2f55a04 100644 --- a/librecomp/include/recomp.h +++ b/librecomp/include/recomp.h @@ -5,11 +5,6 @@ #include #include #include -#include - -#ifdef HAVE_MALLOC_H -#include -#endif typedef uint64_t gpr; diff --git a/librecomp/include/recomp_game.h b/librecomp/include/recomp_game.h index 8a05802..a8d5b6c 100644 --- a/librecomp/include/recomp_game.h +++ b/librecomp/include/recomp_game.h @@ -30,6 +30,7 @@ namespace recomp { IncorrectVersion, OtherError }; + void register_config_path(std::filesystem::path path); bool register_game(const recomp::GameEntry& entry); void register_patch(const char* patch, std::size_t size); void check_all_stored_roms(); @@ -42,7 +43,6 @@ namespace recomp { void do_rom_pio(uint8_t* rdram, gpr ram_address, uint32_t physical_addr); void start(ultramodern::WindowHandle window_handle, const recomp::rsp::callbacks_t& rsp_callbacks, const ultramodern::audio_callbacks_t& audio_callbacks, const ultramodern::input_callbacks_t& input_callbacks, const ultramodern::gfx_callbacks_t& gfx_callbacks, const ultramodern::events::callbacks_t& thread_callbacks, const ultramodern::error_handling::callbacks_t& error_handling_callbacks_); void start_game(const std::u8string& game_id); - std::filesystem::path get_app_folder_path(); std::u8string current_game_id(); } diff --git a/librecomp/src/pi.cpp b/librecomp/src/pi.cpp index 9219f33..ea16f3b 100644 --- a/librecomp/src/pi.cpp +++ b/librecomp/src/pi.cpp @@ -96,7 +96,7 @@ struct { const std::u8string save_folder = u8"saves"; std::filesystem::path get_save_file_path() { - return recomp::get_app_folder_path() / save_folder / (std::u8string{recomp::current_game_id()} + u8".bin"); + return config_path / save_folder / (std::u8string{recomp::current_game_id()} + u8".bin"); } void update_save_file() { diff --git a/librecomp/src/recomp.cpp b/librecomp/src/recomp.cpp index 3f11ebe..139cb48 100644 --- a/librecomp/src/recomp.cpp +++ b/librecomp/src/recomp.cpp @@ -46,6 +46,7 @@ std::mutex patch_data_mutex; std::mutex current_game_mutex; // Global variables +std::filesystem::path config_path; std::vector patch_data; std::unordered_map game_roms {}; @@ -53,6 +54,10 @@ std::u8string recomp::GameEntry::stored_filename() const { return game_id + u8".z64"; } +void recomp::register_config_path(std::filesystem::path path) { + config_path = path; +} + bool recomp::register_game(const recomp::GameEntry& entry) { std::lock_guard lock(game_roms_mutex); game_roms.insert({ entry.game_id, entry }); @@ -98,39 +103,12 @@ bool write_file(const std::filesystem::path& path, const std::vector& d return true; } -std::filesystem::path recomp::get_app_folder_path() { - std::filesystem::path recomp_dir{}; - -#if defined(_WIN32) - // Deduce local app data path. - PWSTR known_path = NULL; - HRESULT result = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &known_path); - if (result == S_OK) { - recomp_dir = std::filesystem::path{known_path} / recomp::current_game_id(); - } - - CoTaskMemFree(known_path); -#elif defined(__linux__) - const char *homedir; - - if ((homedir = getenv("HOME")) == nullptr) { - homedir = getpwuid(getuid())->pw_dir; - } - - if (homedir != nullptr) { - recomp_dir = std::filesystem::path{homedir} / (std::u8string{u8".config/"} + recomp::current_game_id()); - } -#endif - - return recomp_dir; -} - bool check_stored_rom(const recomp::GameEntry& game_entry) { - std::vector stored_rom_data = read_file(recomp::get_app_folder_path() / game_entry.stored_filename()); + std::vector stored_rom_data = read_file(config_path / game_entry.stored_filename()); if (!check_hash(stored_rom_data, game_entry.rom_hash)) { // Incorrect hash, remove the stored ROM file if it exists. - std::filesystem::remove(recomp::get_app_folder_path() / game_entry.stored_filename()); + std::filesystem::remove(config_path / game_entry.stored_filename()); return false; } @@ -158,11 +136,11 @@ bool recomp::load_stored_rom(std::u8string& game_id) { return false; } - std::vector stored_rom_data = read_file(recomp::get_app_folder_path() / find_it->second.stored_filename()); + std::vector stored_rom_data = read_file(config_path / find_it->second.stored_filename()); if (!check_hash(stored_rom_data, find_it->second.rom_hash)) { // The ROM no longer has the right hash, delete it. - std::filesystem::remove(recomp::get_app_folder_path() / find_it->second.stored_filename()); + std::filesystem::remove(config_path / find_it->second.stored_filename()); return false; } @@ -273,7 +251,7 @@ recomp::RomValidationError recomp::select_rom(const std::filesystem::path& rom_p } } - write_file(recomp::get_app_folder_path() / game_entry.stored_filename(), rom_data); + write_file(config_path / game_entry.stored_filename(), rom_data); return recomp::RomValidationError::Good; }