Add registering of config path

This commit is contained in:
dcvz 2024-05-28 21:55:05 +02:00 committed by Angie
parent edcb97422b
commit d38e10eac4
4 changed files with 12 additions and 39 deletions

View file

@ -5,11 +5,6 @@
#include <stdint.h> #include <stdint.h>
#include <math.h> #include <math.h>
#include <assert.h> #include <assert.h>
#include <setjmp.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
typedef uint64_t gpr; typedef uint64_t gpr;

View file

@ -30,6 +30,7 @@ namespace recomp {
IncorrectVersion, IncorrectVersion,
OtherError OtherError
}; };
void register_config_path(std::filesystem::path path);
bool register_game(const recomp::GameEntry& entry); bool register_game(const recomp::GameEntry& entry);
void register_patch(const char* patch, std::size_t size); void register_patch(const char* patch, std::size_t size);
void check_all_stored_roms(); 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 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(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); void start_game(const std::u8string& game_id);
std::filesystem::path get_app_folder_path();
std::u8string current_game_id(); std::u8string current_game_id();
} }

View file

@ -96,7 +96,7 @@ struct {
const std::u8string save_folder = u8"saves"; const std::u8string save_folder = u8"saves";
std::filesystem::path get_save_file_path() { 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() { void update_save_file() {

View file

@ -46,6 +46,7 @@ std::mutex patch_data_mutex;
std::mutex current_game_mutex; std::mutex current_game_mutex;
// Global variables // Global variables
std::filesystem::path config_path;
std::vector<char> patch_data; std::vector<char> patch_data;
std::unordered_map<std::u8string, recomp::GameEntry> game_roms {}; std::unordered_map<std::u8string, recomp::GameEntry> game_roms {};
@ -53,6 +54,10 @@ std::u8string recomp::GameEntry::stored_filename() const {
return game_id + u8".z64"; 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) { bool recomp::register_game(const recomp::GameEntry& entry) {
std::lock_guard<std::mutex> lock(game_roms_mutex); std::lock_guard<std::mutex> lock(game_roms_mutex);
game_roms.insert({ entry.game_id, entry }); game_roms.insert({ entry.game_id, entry });
@ -98,39 +103,12 @@ bool write_file(const std::filesystem::path& path, const std::vector<uint8_t>& d
return true; 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) { 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)) { if (!check_hash(stored_rom_data, game_entry.rom_hash)) {
// Incorrect hash, remove the stored ROM file if it exists. // 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; return false;
} }
@ -158,11 +136,11 @@ bool recomp::load_stored_rom(std::u8string& game_id) {
return false; return false;
} }
std::vector<uint8_t> stored_rom_data = read_file(recomp::get_app_folder_path() / find_it->second.stored_filename()); std::vector<uint8_t> stored_rom_data = read_file(config_path / find_it->second.stored_filename());
if (!check_hash(stored_rom_data, find_it->second.rom_hash)) { if (!check_hash(stored_rom_data, find_it->second.rom_hash)) {
// The ROM no longer has the right hash, delete it. // 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; 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; return recomp::RomValidationError::Good;
} }