From bcdb6be53a44597dd7ef49846e93776cd620bc51 Mon Sep 17 00:00:00 2001 From: Agent X <44549182+AgentXLP@users.noreply.github.com> Date: Sat, 15 Jun 2024 13:39:16 -0400 Subject: [PATCH] Improve mod storage & hopefully fix Cookie mod --- docs/lua/functions-4.md | 4 ++-- src/pc/mini.h | 25 ------------------------- src/pc/mods/mod_storage.cpp | 23 +++++++++++++++++------ src/pc/mods/mod_storage.h | 6 ++++-- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/docs/lua/functions-4.md b/docs/lua/functions-4.md index 09b2d332..077f9b16 100644 --- a/docs/lua/functions-4.md +++ b/docs/lua/functions-4.md @@ -1117,7 +1117,7 @@ - `number` ### C Prototype -`double mod_storage_load_number(const char* key);` +`f32 mod_storage_load_number(const char* key);` [:arrow_up_small:](#) @@ -1200,7 +1200,7 @@ - `boolean` ### 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:](#) diff --git a/src/pc/mini.h b/src/pc/mini.h index 28a06d3c..7a7a260e 100644 --- a/src/pc/mini.h +++ b/src/pc/mini.h @@ -94,8 +94,6 @@ #include #include -#define MINI_CASE_SENSITIVE - namespace mINI { namespace INIStringUtil @@ -106,14 +104,6 @@ namespace mINI str.erase(str.find_last_not_of(whitespaceDelimiters) + 1); 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(std::tolower(c)); - }); - } -#endif inline void replace(std::string& str, std::string const& a, std::string const& b) { if (!a.empty()) @@ -173,9 +163,6 @@ namespace mINI T& operator[](std::string key) { INIStringUtil::trim(key); -#ifndef MINI_CASE_SENSITIVE - INIStringUtil::toLower(key); -#endif auto it = dataIndexMap.find(key); bool hasIt = (it != dataIndexMap.end()); std::size_t index = (hasIt) ? it->second : setEmpty(key); @@ -184,9 +171,6 @@ namespace mINI T get(std::string key) const { INIStringUtil::trim(key); -#ifndef MINI_CASE_SENSITIVE - INIStringUtil::toLower(key); -#endif auto it = dataIndexMap.find(key); if (it == dataIndexMap.end()) { @@ -197,17 +181,11 @@ namespace mINI bool has(std::string key) const { INIStringUtil::trim(key); -#ifndef MINI_CASE_SENSITIVE - INIStringUtil::toLower(key); -#endif return (dataIndexMap.count(key) == 1); } void set(std::string key, T obj) { INIStringUtil::trim(key); -#ifndef MINI_CASE_SENSITIVE - INIStringUtil::toLower(key); -#endif auto it = dataIndexMap.find(key); if (it != dataIndexMap.end()) { @@ -231,9 +209,6 @@ namespace mINI bool remove(std::string key) { INIStringUtil::trim(key); -#ifndef MINI_CASE_SENSITIVE - INIStringUtil::toLower(key); -#endif auto it = dataIndexMap.find(key); if (it != dataIndexMap.end()) { diff --git a/src/pc/mods/mod_storage.cpp b/src/pc/mods/mod_storage.cpp index 1182584f..92a98aac 100644 --- a/src/pc/mods/mod_storage.cpp +++ b/src/pc/mods/mod_storage.cpp @@ -16,7 +16,7 @@ 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 u16 i = 0; @@ -45,6 +45,7 @@ void strdelete(char string[], char substr[]) { bool char_valid(const char* buffer) { if (buffer[0] == '\0') { return false; } + while (*buffer != '\0') { if ((*buffer >= 'a' && *buffer <= 'z') || (*buffer >= 'A' && *buffer <= 'Z') || (*buffer >= '0' && *buffer <= '9') || *buffer == '_' || *buffer == '.' || *buffer == '-') { buffer++; @@ -52,13 +53,14 @@ bool char_valid(const char* buffer) { } return false; } + return true; } void mod_storage_get_filename(char* dest) { 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 - strdelete(dest, (char*)".lua"); // delete ".lua" from sav name + strdelete(dest, ".lua"); // delete ".lua" from sav name strcat(dest, SAVE_EXTENSION); // append SAVE_EXTENSION 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; } -C_FIELD bool mod_storage_save_number(const char* key, double value) { - return mod_storage_save(key, std::to_string(value).c_str()); +C_FIELD bool mod_storage_save_number(const char* key, f32 value) { + // 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) { @@ -120,11 +131,11 @@ C_FIELD const char* mod_storage_load(const char* key) { 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); 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) { diff --git a/src/pc/mods/mod_storage.h b/src/pc/mods/mod_storage.h index a5847f59..3661807b 100644 --- a/src/pc/mods/mod_storage.h +++ b/src/pc/mods/mod_storage.h @@ -1,6 +1,8 @@ #ifndef MOD_STORAGE_H #define MOD_STORAGE_H +#include + #ifdef __cplusplus extern "C" { #endif @@ -11,11 +13,11 @@ extern "C" { #define SAVE_EXTENSION ".sav" 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); 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_remove(const char* key);