Improve mod storage & hopefully fix Cookie mod

This commit is contained in:
Agent X 2024-06-15 13:39:16 -04:00
parent a15c36a968
commit bcdb6be53a
4 changed files with 23 additions and 35 deletions

View File

@ -1117,7 +1117,7 @@
- `number` - `number`
### C Prototype ### C Prototype
`double mod_storage_load_number(const char* key);` `f32 mod_storage_load_number(const char* key);`
[:arrow_up_small:](#) [:arrow_up_small:](#)
@ -1200,7 +1200,7 @@
- `boolean` - `boolean`
### C Prototype ### C Prototype
`bool mod_storage_save_number(const char* key, double value);` `bool mod_storage_save_number(const char* key, f32 value);`
[:arrow_up_small:](#) [:arrow_up_small:](#)

View File

@ -94,8 +94,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <cctype> #include <cctype>
#define MINI_CASE_SENSITIVE
namespace mINI namespace mINI
{ {
namespace INIStringUtil namespace INIStringUtil
@ -106,14 +104,6 @@ namespace mINI
str.erase(str.find_last_not_of(whitespaceDelimiters) + 1); str.erase(str.find_last_not_of(whitespaceDelimiters) + 1);
str.erase(0, str.find_first_not_of(whitespaceDelimiters)); str.erase(0, str.find_first_not_of(whitespaceDelimiters));
} }
#ifndef MINI_CASE_SENSITIVE
inline void toLower(std::string& str)
{
std::transform(str.begin(), str.end(), str.begin(), [](const char c) {
return static_cast<char>(std::tolower(c));
});
}
#endif
inline void replace(std::string& str, std::string const& a, std::string const& b) inline void replace(std::string& str, std::string const& a, std::string const& b)
{ {
if (!a.empty()) if (!a.empty())
@ -173,9 +163,6 @@ namespace mINI
T& operator[](std::string key) T& operator[](std::string key)
{ {
INIStringUtil::trim(key); INIStringUtil::trim(key);
#ifndef MINI_CASE_SENSITIVE
INIStringUtil::toLower(key);
#endif
auto it = dataIndexMap.find(key); auto it = dataIndexMap.find(key);
bool hasIt = (it != dataIndexMap.end()); bool hasIt = (it != dataIndexMap.end());
std::size_t index = (hasIt) ? it->second : setEmpty(key); std::size_t index = (hasIt) ? it->second : setEmpty(key);
@ -184,9 +171,6 @@ namespace mINI
T get(std::string key) const T get(std::string key) const
{ {
INIStringUtil::trim(key); INIStringUtil::trim(key);
#ifndef MINI_CASE_SENSITIVE
INIStringUtil::toLower(key);
#endif
auto it = dataIndexMap.find(key); auto it = dataIndexMap.find(key);
if (it == dataIndexMap.end()) if (it == dataIndexMap.end())
{ {
@ -197,17 +181,11 @@ namespace mINI
bool has(std::string key) const bool has(std::string key) const
{ {
INIStringUtil::trim(key); INIStringUtil::trim(key);
#ifndef MINI_CASE_SENSITIVE
INIStringUtil::toLower(key);
#endif
return (dataIndexMap.count(key) == 1); return (dataIndexMap.count(key) == 1);
} }
void set(std::string key, T obj) void set(std::string key, T obj)
{ {
INIStringUtil::trim(key); INIStringUtil::trim(key);
#ifndef MINI_CASE_SENSITIVE
INIStringUtil::toLower(key);
#endif
auto it = dataIndexMap.find(key); auto it = dataIndexMap.find(key);
if (it != dataIndexMap.end()) if (it != dataIndexMap.end())
{ {
@ -231,9 +209,6 @@ namespace mINI
bool remove(std::string key) bool remove(std::string key)
{ {
INIStringUtil::trim(key); INIStringUtil::trim(key);
#ifndef MINI_CASE_SENSITIVE
INIStringUtil::toLower(key);
#endif
auto it = dataIndexMap.find(key); auto it = dataIndexMap.find(key);
if (it != dataIndexMap.end()) if (it != dataIndexMap.end())
{ {

View File

@ -16,7 +16,7 @@ extern "C" {
#define C_FIELD extern "C" #define C_FIELD extern "C"
void strdelete(char string[], char substr[]) { void strdelete(char* string, const char* substr) {
// i is used to loop through the string // i is used to loop through the string
u16 i = 0; u16 i = 0;
@ -45,6 +45,7 @@ void strdelete(char string[], char substr[]) {
bool char_valid(const char* buffer) { bool char_valid(const char* buffer) {
if (buffer[0] == '\0') { return false; } if (buffer[0] == '\0') { return false; }
while (*buffer != '\0') { while (*buffer != '\0') {
if ((*buffer >= 'a' && *buffer <= 'z') || (*buffer >= 'A' && *buffer <= 'Z') || (*buffer >= '0' && *buffer <= '9') || *buffer == '_' || *buffer == '.' || *buffer == '-') { if ((*buffer >= 'a' && *buffer <= 'z') || (*buffer >= 'A' && *buffer <= 'Z') || (*buffer >= '0' && *buffer <= '9') || *buffer == '_' || *buffer == '.' || *buffer == '-') {
buffer++; buffer++;
@ -52,13 +53,14 @@ bool char_valid(const char* buffer) {
} }
return false; return false;
} }
return true; return true;
} }
void mod_storage_get_filename(char* dest) { void mod_storage_get_filename(char* dest) {
const char* path = fs_get_write_path(SAVE_DIRECTORY); // get user path const char* path = fs_get_write_path(SAVE_DIRECTORY); // get user path
snprintf(dest, SYS_MAX_PATH - 1, "%s/%s", path, gLuaActiveMod->relativePath); // append sav folder snprintf(dest, SYS_MAX_PATH - 1, "%s/%s", path, gLuaActiveMod->relativePath); // append sav folder
strdelete(dest, (char*)".lua"); // delete ".lua" from sav name strdelete(dest, ".lua"); // delete ".lua" from sav name
strcat(dest, SAVE_EXTENSION); // append SAVE_EXTENSION strcat(dest, SAVE_EXTENSION); // append SAVE_EXTENSION
normalize_path(dest); // fix any out of place slashes normalize_path(dest); // fix any out of place slashes
} }
@ -89,8 +91,17 @@ C_FIELD bool mod_storage_save(const char* key, const char* value) {
return true; return true;
} }
C_FIELD bool mod_storage_save_number(const char* key, double value) { C_FIELD bool mod_storage_save_number(const char* key, f32 value) {
return mod_storage_save(key, std::to_string(value).c_str()); // Store string results in a temporary buffer
// this assumes mod_storage_load will only ever be called by Lua
static char str[MAX_KEY_VALUE_LENGTH];
if (floor(value) == value) {
snprintf(str, MAX_KEY_VALUE_LENGTH, "%d", (s64)value);
} else {
snprintf(str, MAX_KEY_VALUE_LENGTH, "%f", value);
}
return mod_storage_save(key, str);
} }
C_FIELD bool mod_storage_save_bool(const char* key, bool value) { C_FIELD bool mod_storage_save_bool(const char* key, bool value) {
@ -120,11 +131,11 @@ C_FIELD const char* mod_storage_load(const char* key) {
return value; return value;
} }
C_FIELD double mod_storage_load_number(const char* key) { C_FIELD f32 mod_storage_load_number(const char* key) {
const char* value = mod_storage_load(key); const char* value = mod_storage_load(key);
if (value == NULL) { return 0; } if (value == NULL) { return 0; }
return std::strtod(value, nullptr); return std::strtof(value, nullptr);
} }
C_FIELD bool mod_storage_load_bool(const char* key) { C_FIELD bool mod_storage_load_bool(const char* key) {

View File

@ -1,6 +1,8 @@
#ifndef MOD_STORAGE_H #ifndef MOD_STORAGE_H
#define MOD_STORAGE_H #define MOD_STORAGE_H
#include <PR/ultratypes.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -11,11 +13,11 @@ extern "C" {
#define SAVE_EXTENSION ".sav" #define SAVE_EXTENSION ".sav"
bool mod_storage_save(const char* key, const char* value); bool mod_storage_save(const char* key, const char* value);
bool mod_storage_save_number(const char* key, double value); bool mod_storage_save_number(const char* key, f32 value);
bool mod_storage_save_bool(const char* key, bool value); bool mod_storage_save_bool(const char* key, bool value);
const char *mod_storage_load(const char* key); const char *mod_storage_load(const char* key);
double mod_storage_load_number(const char* key); f32 mod_storage_load_number(const char* key);
bool mod_storage_load_bool(const char* key); bool mod_storage_load_bool(const char* key);
bool mod_storage_remove(const char* key); bool mod_storage_remove(const char* key);