Skip to content

Commit bb6b3b1

Browse files
authored
Add mod API export to get the save file path (#102)
1 parent 1f2a583 commit bb6b3b1

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

librecomp/src/mod_config_api.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,40 @@ void recomp_get_config_double(uint8_t* rdram, recomp_context* ctx, size_t mod_in
2828
}
2929
}
3030

31+
template <typename StringType>
32+
void return_string(uint8_t* rdram, recomp_context* ctx, const StringType& str) {
33+
// Allocate space in the recomp heap to hold the string, including the null terminator.
34+
size_t alloc_size = (str.size() + 1 + 15) & ~15;
35+
gpr offset = reinterpret_cast<uint8_t*>(recomp::alloc(rdram, alloc_size)) - rdram;
36+
gpr addr = offset + 0xFFFFFFFF80000000ULL;
37+
38+
// Copy the string's data into the allocated memory and null terminate it.
39+
for (size_t i = 0; i < str.size(); i++) {
40+
MEM_B(i, addr) = str[i];
41+
}
42+
MEM_B(str.size(), addr) = 0;
43+
44+
// Return the allocated memory.
45+
ctx->r2 = addr;
46+
}
47+
3148
void recomp_get_config_string(uint8_t* rdram, recomp_context* ctx, size_t mod_index) {
3249
recomp::mods::ConfigValueVariant val = recomp::mods::get_mod_config_value(mod_index, _arg_string<0>(rdram, ctx));
3350
if (std::string* as_string = std::get_if<std::string>(&val)) {
34-
const std::string& str = *as_string;
35-
// Allocate space in the recomp heap to hold the string, including the null terminator.
36-
size_t alloc_size = (str.size() + 1 + 15) & ~15;
37-
gpr offset = reinterpret_cast<uint8_t*>(recomp::alloc(rdram, alloc_size)) - rdram;
38-
gpr addr = offset + 0xFFFFFFFF80000000ULL;
39-
40-
// Copy the string's data into the allocated memory and null terminate it.
41-
for (size_t i = 0; i < str.size(); i++) {
42-
MEM_B(i, addr) = str[i];
43-
}
44-
MEM_B(str.size(), addr) = 0;
45-
46-
// Return the allocated memory.
47-
ctx->r2 = addr;
51+
return_string(rdram, ctx, *as_string);
4852
}
4953
else {
5054
_return(ctx, NULLPTR);
5155
}
5256
}
5357

58+
void recomp_free_config_string(uint8_t* rdram, recomp_context* ctx) {
59+
gpr str_rdram = (gpr)_arg<0, PTR(char)>(rdram, ctx);
60+
gpr offset = str_rdram - 0xFFFFFFFF80000000ULL;
61+
62+
recomp::free(rdram, rdram + offset);
63+
}
64+
5465
void recomp_get_mod_version(uint8_t* rdram, recomp_context* ctx, size_t mod_index) {
5566
uint32_t* major_out = _arg<0, uint32_t*>(rdram, ctx);
5667
uint32_t* minor_out = _arg<1, uint32_t*>(rdram, ctx);
@@ -73,18 +84,18 @@ void recomp_change_save_file(uint8_t* rdram, recomp_context* ctx, size_t mod_ind
7384
ultramodern::change_save_file(mod_id_u8, name_u8);
7485
}
7586

76-
void recomp_free_config_string(uint8_t* rdram, recomp_context* ctx) {
77-
gpr str_rdram = (gpr)_arg<0, PTR(char)>(rdram, ctx);
78-
gpr offset = str_rdram - 0xFFFFFFFF80000000ULL;
87+
void recomp_get_save_file_path(uint8_t* rdram, recomp_context* ctx) {
88+
std::filesystem::path save_file_path = ultramodern::get_save_file_path();
7989

80-
recomp::free(rdram, rdram + offset);
90+
return_string(rdram, ctx, std::filesystem::absolute(save_file_path).u8string());
8191
}
8292

8393
void recomp::mods::register_config_exports() {
8494
recomp::overlays::register_ext_base_export("recomp_get_config_u32", recomp_get_config_u32);
8595
recomp::overlays::register_ext_base_export("recomp_get_config_double", recomp_get_config_double);
8696
recomp::overlays::register_ext_base_export("recomp_get_config_string", recomp_get_config_string);
97+
recomp::overlays::register_base_export("recomp_free_config_string", recomp_free_config_string);
8798
recomp::overlays::register_ext_base_export("recomp_get_mod_version", recomp_get_mod_version);
8899
recomp::overlays::register_ext_base_export("recomp_change_save_file", recomp_change_save_file);
89-
recomp::overlays::register_base_export("recomp_free_config_string", recomp_free_config_string);
100+
recomp::overlays::register_base_export("recomp_get_save_file_path", recomp_get_save_file_path);
90101
}

librecomp/src/pi.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const std::u8string save_folder = u8"saves";
101101

102102
extern std::filesystem::path config_path;
103103

104-
std::filesystem::path get_save_file_path() {
104+
std::filesystem::path ultramodern::get_save_file_path() {
105105
return save_context.save_file_path;
106106
}
107107

@@ -116,7 +116,7 @@ void set_save_file_path(const std::u8string& subfolder, const std::u8string& nam
116116
void update_save_file() {
117117
bool saving_failed = false;
118118
{
119-
std::ofstream save_file = recomp::open_output_file_with_backup(get_save_file_path(), std::ios_base::binary);
119+
std::ofstream save_file = recomp::open_output_file_with_backup(ultramodern::get_save_file_path(), std::ios_base::binary);
120120

121121
if (save_file.good()) {
122122
std::lock_guard lock{ save_context.save_buffer_mutex };
@@ -127,7 +127,7 @@ void update_save_file() {
127127
}
128128
}
129129
if (!saving_failed) {
130-
saving_failed = !recomp::finalize_output_file_with_backup(get_save_file_path());
130+
saving_failed = !recomp::finalize_output_file_with_backup(ultramodern::get_save_file_path());
131131
}
132132
if (saving_failed) {
133133
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.");
@@ -225,7 +225,7 @@ size_t get_save_size(recomp::SaveType save_type) {
225225
}
226226

227227
void read_save_file() {
228-
std::filesystem::path save_file_path = get_save_file_path();
228+
std::filesystem::path save_file_path = ultramodern::get_save_file_path();
229229

230230
// Ensure the save file directory exists.
231231
std::filesystem::create_directories(save_file_path.parent_path());

ultramodern/include/ultramodern/ultramodern.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <stdexcept>
77
#include <span>
88
#include <chrono>
9+
#include <filesystem>
910

1011
#undef MOODYCAMEL_DELETE_FUNCTION
1112
#define MOODYCAMEL_DELETE_FUNCTION = delete
@@ -39,6 +40,7 @@ void init_thread_cleanup();
3940

4041
// Saving
4142
void change_save_file(const std::u8string& subfolder, const std::u8string& name);
43+
std::filesystem::path get_save_file_path();
4244

4345
// Thread queues.
4446
constexpr PTR(PTR(OSThread)) running_queue = (PTR(PTR(OSThread)))-1;

0 commit comments

Comments
 (0)