Touch up mod storage and fix inconsistent logic with old mod_storage_load

This commit is contained in:
Agent X 2024-06-02 18:15:17 -04:00
parent 3f6da68615
commit bdd427c561
3 changed files with 18 additions and 19 deletions

View file

@ -73,7 +73,7 @@ static bool smlua_check_binary_header(struct ModFile *file) {
if (f) { if (f) {
// Read signature // Read signature
char signature[sizeof(LUA_SIGNATURE)] = {0}; char signature[sizeof(LUA_SIGNATURE)] = { 0 };
if (fread(signature, 1, sizeof(LUA_SIGNATURE) - 1, f) != sizeof(LUA_SIGNATURE) - 1) { if (fread(signature, 1, sizeof(LUA_SIGNATURE) - 1, f) != sizeof(LUA_SIGNATURE) - 1) {
LOG_LUA("Failed to load lua script '%s': File too short.", file->cachedPath); LOG_LUA("Failed to load lua script '%s': File too short.", file->cachedPath);
fclose(f); fclose(f);

View file

@ -43,7 +43,7 @@ void strdelete(char string[], char substr[]) {
string[i] = '\0'; string[i] = '\0';
} }
bool char_valid(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 == '-') {
@ -56,9 +56,9 @@ bool char_valid(char* buffer) {
} }
void mod_storage_get_filename(char* dest) { void mod_storage_get_filename(char* dest) {
const char* path = fs_get_write_path(""); // get user path const char* path = fs_get_write_path(SAVE_DIRECTORY); // get user path
snprintf(dest, SYS_MAX_PATH - 1, "%ssav/%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, (char*)".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
} }
@ -66,21 +66,20 @@ void mod_storage_get_filename(char* dest) {
C_FIELD bool mod_storage_save(const char* key, const char* value) { C_FIELD bool mod_storage_save(const char* key, const char* value) {
if (gLuaActiveMod == NULL) { return false; } if (gLuaActiveMod == NULL) { return false; }
if (strlen(key) > MAX_KEY_VALUE_LENGTH || strlen(value) > MAX_KEY_VALUE_LENGTH) { return false; } if (strlen(key) > MAX_KEY_VALUE_LENGTH || strlen(value) > MAX_KEY_VALUE_LENGTH) { return false; }
if (!char_valid((char *)key) || !char_valid((char *)value)) { return false; } if (!char_valid(key) || !char_valid(value)) { return false; }
char filename[SYS_MAX_PATH] = {0}; char filename[SYS_MAX_PATH] = { 0 };
mod_storage_get_filename(filename); mod_storage_get_filename(filename);
// ensure savPath exists // ensure savPath exists
char savPath[SYS_MAX_PATH] = { 0 }; const char* savPath = fs_get_write_path(SAVE_DIRECTORY);
if (snprintf(savPath, SYS_MAX_PATH - 1, "%s", fs_get_write_path(SAVE_DIRECTORY)) < 0) { return false; }
if (!fs_sys_dir_exists(savPath)) { fs_sys_mkdir(savPath); } if (!fs_sys_dir_exists(savPath)) { fs_sys_mkdir(savPath); }
mINI::INIFile file(filename); mINI::INIFile file(filename);
mINI::INIStructure ini; mINI::INIStructure ini;
file.read(ini); file.read(ini);
if (ini["storage"].size() + 1 > MAX_KEYS) { return false; } if (ini["storage"].size() > MAX_KEYS) { return false; }
ini["storage"][key] = value; ini["storage"][key] = value;
@ -101,21 +100,23 @@ C_FIELD bool mod_storage_save_bool(const char* key, bool value) {
C_FIELD const char* mod_storage_load(const char* key) { C_FIELD const char* mod_storage_load(const char* key) {
if (gLuaActiveMod == NULL) { return NULL; } if (gLuaActiveMod == NULL) { return NULL; }
if (strlen(key) > MAX_KEY_VALUE_LENGTH) { return NULL; } if (strlen(key) > MAX_KEY_VALUE_LENGTH) { return NULL; }
if (!char_valid((char *)key)) { return NULL; } if (!char_valid(key)) { return NULL; }
char filename[SYS_MAX_PATH] = {0}; char filename[SYS_MAX_PATH] = { 0 };
mod_storage_get_filename(filename); mod_storage_get_filename(filename);
if (!path_exists(filename)) { return NULL; } if (!path_exists(filename)) { return NULL; }
mINI::INIFile file(filename); mINI::INIFile file(filename);
mINI::INIStructure ini; mINI::INIStructure ini;
file.read(ini); file.read(ini);
std::string str = ini["storage"][key];
if (str.empty()) { return NULL; }
// Store string results in a temporary buffer // Store string results in a temporary buffer
// this assumes mod_storage_load will only ever be called by Lua // this assumes mod_storage_load will only ever be called by Lua
static char value[MAX_KEY_VALUE_LENGTH]; static char value[MAX_KEY_VALUE_LENGTH];
snprintf(value, MAX_KEY_VALUE_LENGTH, "%s", const_cast<char*>(ini["storage"][key].c_str())); snprintf(value, MAX_KEY_VALUE_LENGTH, "%s", str.c_str());
return value; return value;
} }
@ -138,9 +139,8 @@ C_FIELD bool mod_storage_remove(const char* key) {
if (strlen(key) > MAX_KEY_VALUE_LENGTH) { return false; } if (strlen(key) > MAX_KEY_VALUE_LENGTH) { return false; }
if (!char_valid((char *)key)) { return false; } if (!char_valid((char *)key)) { return false; }
char filename[SYS_MAX_PATH] = {0}; char filename[SYS_MAX_PATH] = { 0 };
mod_storage_get_filename(filename); mod_storage_get_filename(filename);
if (!path_exists(filename)) { return false; } if (!path_exists(filename)) { return false; }
mINI::INIFile file(filename); mINI::INIFile file(filename);
@ -159,9 +159,8 @@ C_FIELD bool mod_storage_remove(const char* key) {
C_FIELD bool mod_storage_clear(void) { C_FIELD bool mod_storage_clear(void) {
if (gLuaActiveMod == NULL) { return false; } if (gLuaActiveMod == NULL) { return false; }
char filename[SYS_MAX_PATH] = {0}; char filename[SYS_MAX_PATH] = { 0 };
mod_storage_get_filename(filename); mod_storage_get_filename(filename);
if (!path_exists(filename)) { return false; } if (!path_exists(filename)) { return false; }
mINI::INIFile file(filename); mINI::INIFile file(filename);

View file

@ -152,7 +152,7 @@ s32 osEepromLongWrite(UNUSED OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes
return 0; return 0;
} }
u8 content[512] = {0}; u8 content[512] = { 0 };
if (address != 0 || nbytes != 512) { if (address != 0 || nbytes != 512) {
osEepromLongRead(mq, 0, content, 512); osEepromLongRead(mq, 0, content, 512);
} }