From 496639b8c1ea290d845c23353a0ef9c753e47e22 Mon Sep 17 00:00:00 2001 From: Garrett Smith Date: Sat, 17 Jan 2026 22:51:05 -0800 Subject: [PATCH] update code to reflect relocation --- librecomp/CMakeLists.txt | 1 - librecomp/include/librecomp/game.hpp | 1 + librecomp/src/eep.cpp | 30 +++++----- librecomp/src/flash.cpp | 44 +++++++------- librecomp/src/mod_manifest.cpp | 4 +- librecomp/src/mods.cpp | 12 ++-- librecomp/src/pi.cpp | 6 +- librecomp/src/recomp.cpp | 28 +-------- ultramodern/CMakeLists.txt | 2 + ultramodern/include/ultramodern/files.hpp | 11 ++-- ultramodern/include/ultramodern/save.hpp | 29 +++++++++ ultramodern/src/files.cpp | 10 ++-- ultramodern/src/save.cpp | 71 +++++++++++++++++------ 13 files changed, 142 insertions(+), 107 deletions(-) diff --git a/librecomp/CMakeLists.txt b/librecomp/CMakeLists.txt index fcc3337..60541ad 100644 --- a/librecomp/CMakeLists.txt +++ b/librecomp/CMakeLists.txt @@ -13,7 +13,6 @@ add_library(librecomp STATIC "${CMAKE_CURRENT_SOURCE_DIR}/src/eep.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/euc-jp.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/extensions.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/src/files.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/flash.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/heap.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/math_routines.cpp" diff --git a/librecomp/include/librecomp/game.hpp b/librecomp/include/librecomp/game.hpp index 9e55b72..64254a2 100644 --- a/librecomp/include/librecomp/game.hpp +++ b/librecomp/include/librecomp/game.hpp @@ -7,6 +7,7 @@ #include "recomp.h" #include "rsp.hpp" #include +#include namespace recomp { struct GameEntry { diff --git a/librecomp/src/eep.cpp b/librecomp/src/eep.cpp index 048246e..6cf55fb 100644 --- a/librecomp/src/eep.cpp +++ b/librecomp/src/eep.cpp @@ -1,20 +1,18 @@ #include "recomp.h" #include "librecomp/game.hpp" -#include "ultramodern/ultra64.h" - -void save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count); -void save_read(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count); +#include +#include constexpr int eeprom_block_size = 8; extern "C" void osEepromProbe_recomp(uint8_t* rdram, recomp_context* ctx) { - switch (recomp::get_save_type()) { - case recomp::SaveType::AllowAll: - case recomp::SaveType::Eep16k: + switch (ultramodern::get_save_type()) { + case ultramodern::SaveType::AllowAll: + case ultramodern::SaveType::Eep16k: ctx->r2 = 0x02; // EEPROM_TYPE_16K break; - case recomp::SaveType::Eep4k: + case ultramodern::SaveType::Eep4k: ctx->r2 = 0x01; // EEPROM_TYPE_4K break; default: @@ -24,7 +22,7 @@ extern "C" void osEepromProbe_recomp(uint8_t* rdram, recomp_context* ctx) { } extern "C" void osEepromWrite_recomp(uint8_t* rdram, recomp_context* ctx) { - if (!recomp::eeprom_allowed()) { + if (!ultramodern::eeprom_allowed()) { ultramodern::error_handling::message_box("Attempted to use EEPROM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -33,13 +31,13 @@ extern "C" void osEepromWrite_recomp(uint8_t* rdram, recomp_context* ctx) { gpr buffer = ctx->r6; int32_t nbytes = eeprom_block_size; - save_write(rdram, buffer, eep_address * eeprom_block_size, nbytes); + ultramodern::save_write(rdram, buffer, eep_address * eeprom_block_size, nbytes); ctx->r2 = 0; } extern "C" void osEepromLongWrite_recomp(uint8_t* rdram, recomp_context* ctx) { - if (!recomp::eeprom_allowed()) { + if (!ultramodern::eeprom_allowed()) { ultramodern::error_handling::message_box("Attempted to use EEPROM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -50,13 +48,13 @@ extern "C" void osEepromLongWrite_recomp(uint8_t* rdram, recomp_context* ctx) { assert((nbytes % eeprom_block_size) == 0); - save_write(rdram, buffer, eep_address * eeprom_block_size, nbytes); + ultramodern::save_write(rdram, buffer, eep_address * eeprom_block_size, nbytes); ctx->r2 = 0; } extern "C" void osEepromRead_recomp(uint8_t* rdram, recomp_context* ctx) { - if (!recomp::eeprom_allowed()) { + if (!ultramodern::eeprom_allowed()) { ultramodern::error_handling::message_box("Attempted to use EEPROM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -65,13 +63,13 @@ extern "C" void osEepromRead_recomp(uint8_t* rdram, recomp_context* ctx) { gpr buffer = ctx->r6; int32_t nbytes = eeprom_block_size; - save_read(rdram, buffer, eep_address * eeprom_block_size, nbytes); + ultramodern::save_read(rdram, buffer, eep_address * eeprom_block_size, nbytes); ctx->r2 = 0; } extern "C" void osEepromLongRead_recomp(uint8_t* rdram, recomp_context* ctx) { - if (!recomp::eeprom_allowed()) { + if (!ultramodern::eeprom_allowed()) { ultramodern::error_handling::message_box("Attempted to use EEPROM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -82,7 +80,7 @@ extern "C" void osEepromLongRead_recomp(uint8_t* rdram, recomp_context* ctx) { assert((nbytes % eeprom_block_size) == 0); - save_read(rdram, buffer, eep_address * eeprom_block_size, nbytes); + ultramodern::save_read(rdram, buffer, eep_address * eeprom_block_size, nbytes); ctx->r2 = 0; } diff --git a/librecomp/src/flash.cpp b/librecomp/src/flash.cpp index f46261c..e0de47c 100644 --- a/librecomp/src/flash.cpp +++ b/librecomp/src/flash.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include "recomp.h" @@ -15,15 +16,10 @@ constexpr uint32_t page_count = flash_size / page_size; constexpr uint32_t sector_size = page_size * pages_per_sector; constexpr uint32_t sector_count = flash_size / sector_size; -void save_write_ptr(const void* in, uint32_t offset, uint32_t count); -void save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count); -void save_read(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count); -void save_clear(uint32_t start, uint32_t size, char value); - std::array write_buffer; extern "C" void osFlashInit_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -32,7 +28,7 @@ extern "C" void osFlashInit_recomp(uint8_t * rdram, recomp_context * ctx) { } extern "C" void osFlashReadStatus_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -43,7 +39,7 @@ extern "C" void osFlashReadStatus_recomp(uint8_t * rdram, recomp_context * ctx) } extern "C" void osFlashReadId_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -57,7 +53,7 @@ extern "C" void osFlashReadId_recomp(uint8_t * rdram, recomp_context * ctx) { } extern "C" void osFlashClearStatus_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -66,30 +62,30 @@ extern "C" void osFlashClearStatus_recomp(uint8_t * rdram, recomp_context * ctx) } extern "C" void osFlashAllErase_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } - save_clear(0, ultramodern::save_size, 0xFF); + ultramodern::save_clear(0, ultramodern::save_size, 0xFF); ctx->r2 = 0; } extern "C" void osFlashAllEraseThrough_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } - save_clear(0, ultramodern::save_size, 0xFF); + ultramodern::save_clear(0, ultramodern::save_size, 0xFF); ctx->r2 = 0; } // This function is named sector but really means page. extern "C" void osFlashSectorErase_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -102,14 +98,14 @@ extern "C" void osFlashSectorErase_recomp(uint8_t * rdram, recomp_context * ctx) return; } - save_clear(page_num * page_size, page_size, 0xFF); + ultramodern::save_clear(page_num * page_size, page_size, 0xFF); ctx->r2 = 0; } // Same naming issue as above. extern "C" void osFlashSectorEraseThrough_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -122,13 +118,13 @@ extern "C" void osFlashSectorEraseThrough_recomp(uint8_t * rdram, recomp_context return; } - save_clear(page_num * page_size, page_size, 0xFF); + ultramodern::save_clear(page_num * page_size, page_size, 0xFF); ctx->r2 = 0; } extern "C" void osFlashCheckEraseEnd_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -138,7 +134,7 @@ extern "C" void osFlashCheckEraseEnd_recomp(uint8_t * rdram, recomp_context * ct } extern "C" void osFlashWriteBuffer_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -160,7 +156,7 @@ extern "C" void osFlashWriteBuffer_recomp(uint8_t * rdram, recomp_context * ctx) } extern "C" void osFlashWriteArray_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -168,13 +164,13 @@ extern "C" void osFlashWriteArray_recomp(uint8_t * rdram, recomp_context * ctx) uint32_t page_num = ctx->r4; // Copy the write buffer into the save file - save_write_ptr(write_buffer.data(), page_num * page_size, page_size); + ultramodern::save_write_ptr(write_buffer.data(), page_num * page_size, page_size); ctx->r2 = 0; } extern "C" void osFlashReadArray_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } @@ -190,7 +186,7 @@ extern "C" void osFlashReadArray_recomp(uint8_t * rdram, recomp_context * ctx) { uint32_t count = n_pages * page_size; // Read from the save file into the provided buffer - save_read(PASS_RDRAM dramAddr, offset, count); + ultramodern::save_read(PASS_RDRAM dramAddr, offset, count); // Send the message indicating read completion ultramodern::enqueue_external_message_src(mq, 0, false, ultramodern::EventMessageSource::Pi); @@ -199,7 +195,7 @@ extern "C" void osFlashReadArray_recomp(uint8_t * rdram, recomp_context * ctx) { } extern "C" void osFlashChange_recomp(uint8_t * rdram, recomp_context * ctx) { - if (!recomp::flashram_allowed()) { + if (!ultramodern::flashram_allowed()) { ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type"); ULTRAMODERN_QUICK_EXIT(); } diff --git a/librecomp/src/mod_manifest.cpp b/librecomp/src/mod_manifest.cpp index a75d253..b9b5134 100644 --- a/librecomp/src/mod_manifest.cpp +++ b/librecomp/src/mod_manifest.cpp @@ -3,8 +3,8 @@ #include "json/json.hpp" #include "recompiler/context.h" -#include "librecomp/files.hpp" #include "librecomp/mods.hpp" +#include static bool read_json(std::ifstream input_file, nlohmann::json &json_out) { if (!input_file.good()) { @@ -27,7 +27,7 @@ static bool read_json_with_backups(const std::filesystem::path &path, nlohmann:: } // Try reading and parsing the backup file. - if (read_json(recomp::open_input_backup_file(path), json_out)) { + if (read_json(ultramodern::open_input_backup_file(path), json_out)) { return true; } diff --git a/librecomp/src/mods.cpp b/librecomp/src/mods.cpp index ff40212..e4ef3c8 100644 --- a/librecomp/src/mods.cpp +++ b/librecomp/src/mods.cpp @@ -3,7 +3,7 @@ #include #include -#include "librecomp/files.hpp" +#include #include "librecomp/mods.hpp" #include "librecomp/overlays.hpp" #include "librecomp/game.hpp" @@ -32,7 +32,7 @@ static bool read_json_with_backups(const std::filesystem::path &path, nlohmann:: } // Try reading and parsing the backup file. - if (read_json(recomp::open_input_backup_file(path), json_out)) { + if (read_json(ultramodern::open_input_backup_file(path), json_out)) { return true; } @@ -679,7 +679,7 @@ bool save_mod_config_storage(const std::filesystem::path &path, const std::strin } } - std::ofstream output_file = recomp::open_output_file_with_backup(path); + std::ofstream output_file = ultramodern::open_output_file_with_backup(path); if (!output_file.good()) { return false; } @@ -687,7 +687,7 @@ bool save_mod_config_storage(const std::filesystem::path &path, const std::strin output_file << std::setw(4) << config_json; output_file.close(); - return recomp::finalize_output_file_with_backup(path); + return ultramodern::finalize_output_file_with_backup(path); } bool parse_mods_config(const std::filesystem::path &path, std::unordered_set &enabled_mods, std::vector &mod_order) { @@ -720,7 +720,7 @@ bool save_mods_config(const std::filesystem::path &path, const std::unordered_se config_json["enabled_mods"] = enabled_mods; config_json["mod_order"] = mod_order; - std::ofstream output_file = recomp::open_output_file_with_backup(path); + std::ofstream output_file = ultramodern::open_output_file_with_backup(path); if (!output_file.good()) { return false; } @@ -728,7 +728,7 @@ bool save_mods_config(const std::filesystem::path &path, const std::unordered_se output_file << std::setw(4) << config_json; output_file.close(); - return recomp::finalize_output_file_with_backup(path); + return ultramodern::finalize_output_file_with_backup(path); } void recomp::mods::ModContext::dirty_mod_configuration_thread_process() { diff --git a/librecomp/src/pi.cpp b/librecomp/src/pi.cpp index 546aa63..378d3d8 100644 --- a/librecomp/src/pi.cpp +++ b/librecomp/src/pi.cpp @@ -7,7 +7,7 @@ #include "recomp.h" #include "librecomp/addresses.hpp" #include "librecomp/game.hpp" -#include "librecomp/files.hpp" +#include #include #include @@ -100,7 +100,7 @@ void do_dma(RDRAM_ARG PTR(OSMesgQueue) mq, gpr rdram_address, uint32_t physical_ ULTRAMODERN_QUICK_EXIT(); } // read sram - save_read(rdram, rdram_address, physical_addr - recomp::sram_base, size); + ultramodern::save_read(rdram, rdram_address, physical_addr - recomp::sram_base, size); // Send a message to the mq to indicate that the transfer completed ultramodern::enqueue_external_message_src(mq, 0, false, ultramodern::EventMessageSource::Pi); @@ -117,7 +117,7 @@ void do_dma(RDRAM_ARG PTR(OSMesgQueue) mq, gpr rdram_address, uint32_t physical_ ULTRAMODERN_QUICK_EXIT(); } // write sram - save_write(rdram, rdram_address, physical_addr - recomp::sram_base, size); + ultramodern::save_write(rdram, rdram_address, physical_addr - recomp::sram_base, size); // Send a message to the mq to indicate that the transfer completed ultramodern::enqueue_external_message_src(mq, 0, false, ultramodern::EventMessageSource::Pi); diff --git a/librecomp/src/recomp.cpp b/librecomp/src/recomp.cpp index ea17675..e48d85e 100644 --- a/librecomp/src/recomp.cpp +++ b/librecomp/src/recomp.cpp @@ -21,6 +21,7 @@ #include "xxHash/xxh3.h" #include "ultramodern/ultramodern.hpp" #include "ultramodern/error_handling.hpp" +#include "ultramodern/save.hpp" #include "librecomp/addresses.hpp" #include "librecomp/mods.hpp" #include "recompiler/live_recompiler.h" @@ -57,8 +58,6 @@ std::unordered_map game_roms {}; std::unique_ptr mod_context = std::make_unique(); // The project's version. recomp::Version project_version; -// The current game's save type. -recomp::SaveType save_type = recomp::SaveType::None; std::u8string recomp::GameEntry::stored_filename() const { return game_id + u8".z64"; @@ -687,7 +686,7 @@ bool wait_for_game_started(uint8_t* rdram, recomp_context* context) { recomp::init_heap(rdram, recomp::mod_rdram_start + mod_ram_used); - save_type = game_entry.save_type; + ultramodern::set_save_type(game_entry.save_type); ultramodern::init_saving(rdram); try { @@ -706,29 +705,6 @@ bool wait_for_game_started(uint8_t* rdram, recomp_context* context) { } } -recomp::SaveType recomp::get_save_type() { - return save_type; -} - -bool recomp::eeprom_allowed() { - return - save_type == SaveType::Eep4k || - save_type == SaveType::Eep16k || - save_type == SaveType::AllowAll; -} - -bool recomp::sram_allowed() { - return - save_type == SaveType::Sram || - save_type == SaveType::AllowAll; -} - -bool recomp::flashram_allowed() { - return - save_type == SaveType::Flashram || - save_type == SaveType::AllowAll; -} - void recomp::start(const recomp::Configuration& cfg) { project_version = cfg.project_version; recomp::check_all_stored_roms(); diff --git a/ultramodern/CMakeLists.txt b/ultramodern/CMakeLists.txt index 5593172..0c844b4 100644 --- a/ultramodern/CMakeLists.txt +++ b/ultramodern/CMakeLists.txt @@ -10,11 +10,13 @@ add_library(ultramodern STATIC "${CMAKE_CURRENT_SOURCE_DIR}/src/error_handling.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/events.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/extensions.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/src/files.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/input.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/mesgqueue.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/misc_ultra.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/renderer_context.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/rsp.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/src/save.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/scheduling.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/task_win32.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/threadqueue.cpp" diff --git a/ultramodern/include/ultramodern/files.hpp b/ultramodern/include/ultramodern/files.hpp index 63e3e9d..497d6d9 100644 --- a/ultramodern/include/ultramodern/files.hpp +++ b/ultramodern/include/ultramodern/files.hpp @@ -1,14 +1,15 @@ -#ifndef __RECOMP_FILES_H__ -#define __RECOMP_FILES_H__ +#ifndef __ULTRAMODERN_FILES_HPP__ +#define __ULTRAMODERN_FILES_HPP__ #include #include -namespace recomp { +namespace ultramodern { std::ifstream open_input_file_with_backup(const std::filesystem::path& filepath, std::ios_base::openmode mode = std::ios_base::in); std::ifstream open_input_backup_file(const std::filesystem::path& filepath, std::ios_base::openmode mode = std::ios_base::in); std::ofstream open_output_file_with_backup(const std::filesystem::path& filepath, std::ios_base::openmode mode = std::ios_base::out); bool finalize_output_file_with_backup(const std::filesystem::path& filepath); -}; +} + +#endif // __ULTRAMODERN_FILES_HPP__ -#endif diff --git a/ultramodern/include/ultramodern/save.hpp b/ultramodern/include/ultramodern/save.hpp index d813443..c0489f1 100644 --- a/ultramodern/include/ultramodern/save.hpp +++ b/ultramodern/include/ultramodern/save.hpp @@ -1,6 +1,7 @@ #ifndef __ULTRAMODERN_SAVE_HPP__ #define __ULTRAMODERN_SAVE_HPP__ +#include #include namespace ultramodern { @@ -13,7 +14,35 @@ namespace ultramodern { AllowAll, // Allows all save types to work and reports eeprom size as 16kbit. }; + void set_save_type(SaveType type); + + void set_save_file_path(const std::u8string& subfolder, const std::u8string& name); + + void init_saving(RDRAM_ARG1); + + void change_save_file(const std::u8string& subfolder, const std::u8string& name); + + void join_saving_thread(); + + void save_write_ptr(const void* in, uint32_t offset, uint32_t count); + + void save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count); + + void save_read(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count); + + void save_clear(uint32_t start, uint32_t size, char value); + SaveType get_save_type(); + + size_t get_save_size(SaveType save_type); + + std::filesystem::path get_save_file_path(); + + bool eeprom_allowed(); + + bool sram_allowed(); + + bool flashram_allowed(); } #endif // __ULTRAMODERN_SAVE_HPP__ diff --git a/ultramodern/src/files.cpp b/ultramodern/src/files.cpp index af6f18d..0dd1a2f 100644 --- a/ultramodern/src/files.cpp +++ b/ultramodern/src/files.cpp @@ -1,15 +1,15 @@ -#include "files.hpp" +#include constexpr std::u8string_view backup_suffix = u8".bak"; constexpr std::u8string_view temp_suffix = u8".temp"; -std::ifstream recomp::open_input_backup_file(const std::filesystem::path& filepath, std::ios_base::openmode mode) { +std::ifstream ultramodern::open_input_backup_file(const std::filesystem::path& filepath, std::ios_base::openmode mode) { std::filesystem::path backup_path{filepath}; backup_path += backup_suffix; return std::ifstream{backup_path, mode}; } -std::ifstream recomp::open_input_file_with_backup(const std::filesystem::path& filepath, std::ios_base::openmode mode) { +std::ifstream ultramodern::open_input_file_with_backup(const std::filesystem::path& filepath, std::ios_base::openmode mode) { std::ifstream ret{filepath, mode}; // Check if the file failed to open and open the corresponding backup file instead if so. @@ -20,7 +20,7 @@ std::ifstream recomp::open_input_file_with_backup(const std::filesystem::path& f return ret; } -std::ofstream recomp::open_output_file_with_backup(const std::filesystem::path& filepath, std::ios_base::openmode mode) { +std::ofstream ultramodern::open_output_file_with_backup(const std::filesystem::path& filepath, std::ios_base::openmode mode) { std::filesystem::path temp_path{filepath}; temp_path += temp_suffix; std::ofstream temp_file_out{ temp_path, mode }; @@ -28,7 +28,7 @@ std::ofstream recomp::open_output_file_with_backup(const std::filesystem::path& return temp_file_out; } -bool recomp::finalize_output_file_with_backup(const std::filesystem::path& filepath) { +bool ultramodern::finalize_output_file_with_backup(const std::filesystem::path& filepath) { std::filesystem::path backup_path{filepath}; backup_path += backup_suffix; diff --git a/ultramodern/src/save.cpp b/ultramodern/src/save.cpp index c24c98b..951dc8e 100644 --- a/ultramodern/src/save.cpp +++ b/ultramodern/src/save.cpp @@ -1,4 +1,9 @@ +#include +#include +#include +#include #include +#include struct { std::vector save_buffer; @@ -12,15 +17,43 @@ struct { std::mutex save_buffer_mutex; } save_context; +// The current game's save directory within the config path. const std::u8string save_folder = u8"saves"; +// The current game's config directory path. extern std::filesystem::path config_path; +// The current game's save type. +ultramodern::SaveType save_type = ultramodern::SaveType::None; + +ultramodern::SaveType ultramodern::get_save_type() { + return save_type; +} + +bool ultramodern::eeprom_allowed() { + return + save_type == SaveType::Eep4k || + save_type == SaveType::Eep16k || + save_type == SaveType::AllowAll; +} + +bool ultramodern::sram_allowed() { + return + save_type == SaveType::Sram || + save_type == SaveType::AllowAll; +} + +bool ultramodern::flashram_allowed() { + return + save_type == SaveType::Flashram || + save_type == SaveType::AllowAll; +} + std::filesystem::path ultramodern::get_save_file_path() { return save_context.save_file_path; } -void set_save_file_path(const std::u8string& subfolder, const std::u8string& name) { +void ultramodern::set_save_file_path(const std::u8string& subfolder, const std::u8string& name) { std::filesystem::path save_folder_path = config_path / save_folder; if (!subfolder.empty()) { save_folder_path = save_folder_path / subfolder; @@ -31,7 +64,7 @@ void set_save_file_path(const std::u8string& subfolder, const std::u8string& nam void update_save_file() { bool saving_failed = false; { - std::ofstream save_file = recomp::open_output_file_with_backup(ultramodern::get_save_file_path(), std::ios_base::binary); + std::ofstream save_file = ultramodern::open_output_file_with_backup(ultramodern::get_save_file_path(), std::ios_base::binary); if (save_file.good()) { std::lock_guard lock{ save_context.save_buffer_mutex }; @@ -42,7 +75,7 @@ void update_save_file() { } } if (!saving_failed) { - saving_failed = !recomp::finalize_output_file_with_backup(ultramodern::get_save_file_path()); + saving_failed = !ultramodern::finalize_output_file_with_backup(ultramodern::get_save_file_path()); } if (saving_failed) { ultramodern::error_handling::message_box("Failed to write to the save file. Check your file permissions and whether the save folder has been moved to Dropbox or similar, as this can cause issues."); @@ -78,7 +111,7 @@ void saving_thread_func(RDRAM_ARG1) { } } -void save_write_ptr(const void* in, uint32_t offset, uint32_t count) { +void ultramodern::save_write_ptr(const void* in, uint32_t offset, uint32_t count) { assert(offset + count <= save_context.save_buffer.size()); { @@ -89,12 +122,12 @@ void save_write_ptr(const void* in, uint32_t offset, uint32_t count) { save_context.write_sempahore.signal(); } -void save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count) { +void ultramodern::save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count) { assert(offset + count <= save_context.save_buffer.size()); { std::lock_guard lock { save_context.save_buffer_mutex }; - for (gpr i = 0; i < count; i++) { + for (uint32_t i = 0; i < count; i++) { save_context.save_buffer[offset + i] = MEM_B(i, rdram_address); } } @@ -102,16 +135,16 @@ void save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t cou save_context.write_sempahore.signal(); } -void save_read(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count) { +void ultramodern::save_read(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count) { assert(offset + count <= save_context.save_buffer.size()); std::lock_guard lock { save_context.save_buffer_mutex }; - for (gpr i = 0; i < count; i++) { + for (uint32_t i = 0; i < count; i++) { MEM_B(i, rdram_address) = save_context.save_buffer[offset + i]; } } -void save_clear(uint32_t start, uint32_t size, char value) { +void ultramodern::save_clear(uint32_t start, uint32_t size, char value) { assert(start + size < save_context.save_buffer.size()); { @@ -122,18 +155,18 @@ void save_clear(uint32_t start, uint32_t size, char value) { save_context.write_sempahore.signal(); } -size_t get_save_size(recomp::SaveType save_type) { +size_t ultramodern::get_save_size(ultramodern::SaveType save_type) { switch (save_type) { - case recomp::SaveType::AllowAll: - case recomp::SaveType::Flashram: + case ultramodern::SaveType::AllowAll: + case ultramodern::SaveType::Flashram: return 0x20000; - case recomp::SaveType::Sram: + case ultramodern::SaveType::Sram: return 0x8000; - case recomp::SaveType::Eep16k: + case ultramodern::SaveType::Eep16k: return 0x800; - case recomp::SaveType::Eep4k: + case ultramodern::SaveType::Eep4k: return 0x200; - case recomp::SaveType::None: + case ultramodern::SaveType::None: return 0; } return 0; @@ -146,7 +179,7 @@ void read_save_file() { std::filesystem::create_directories(save_file_path.parent_path()); // Read the save file if it exists. - std::ifstream save_file = recomp::open_input_file_with_backup(save_file_path, std::ios_base::binary); + std::ifstream save_file = ultramodern::open_input_file_with_backup(save_file_path, std::ios_base::binary); if (save_file.good()) { save_file.read(save_context.save_buffer.data(), save_context.save_buffer.size()); } @@ -157,9 +190,9 @@ void read_save_file() { } void ultramodern::init_saving(RDRAM_ARG1) { - set_save_file_path(u8"", recomp::current_game_id()); + set_save_file_path(u8"", ultramodern::current_game_id()); - save_context.save_buffer.resize(get_save_size(recomp::get_save_type())); + save_context.save_buffer.resize(get_save_size(ultramodern::get_save_type())); read_save_file();