Skip to content

Commit

Permalink
experimenting w/ loading + saving mp-profile assocated values
Browse files Browse the repository at this point in the history
Initial commit: saving / loading props associated with mp profile works

initial commit: setup extended profiles right away in case they're

loaded from pak and so they can be saved to the ini

blank profile gets set correctly, but not associated yet

sane defaults, auto-inits on load

TODO:
- handle save-as-copy
- handie save new file
  • Loading branch information
cylonicboom committed Dec 20, 2024
1 parent 26b3b91 commit a19b035
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 34 deletions.
42 changes: 42 additions & 0 deletions port/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,41 @@
#define CONFIG_FNAME "pd.ini"
#define CONFIG_PATH "$S/" CONFIG_FNAME


#define CONFIG_MAX_SECNAME 128
#define CONFIG_MAX_KEYNAME 256
#define CONFIG_MAX_SETTINGS 256 * 8

typedef enum {
CFG_NONE,
CFG_S32,
CFG_F32,
CFG_U32,
CFG_STR,
CFG_U8
} configtype;

struct configentry {
char key[CONFIG_MAX_KEYNAME + 1];
s32 seclen;
configtype type;
void *ptr;
union {
struct { f32 min_f32, max_f32; };
struct { s32 min_s32, max_s32; };
struct { u32 min_u32, max_u32; };
struct { u8 min_u8, max_u8; };
u32 max_str;
};
};

extern struct configentry settings[CONFIG_MAX_SETTINGS];

void configInit(void);

// loads config from file (path extensions such as ! apply)
s32 configLoad(const char *fname);
s32 configLoadKey(const char *fname, char *key);

// saves config to file (path extensions such as ! apply)
s32 configSave(const char *fname);
Expand All @@ -19,3 +50,14 @@ void configRegisterInt(const char *key, s32 *var, s32 min, s32 max);
void configRegisterUInt(const char* key, u32* var, u32 min, u32 max);
void configRegisterFloat(const char *key, f32 *var, f32 min, f32 max);
void configRegisterString(const char *key, char *var, u32 maxstr);

// player save stuff
struct configentry *configFindPlayerEntry(s32 player, const char *key);
struct configentry *configFindOrAddPlayerEntry(s32 player, const char *key);
void configSetPlayerEntry(s32 player, const char *key, const char *val);
s32 getPlayerConfigSlug(s32 playernum, char *out, char* key);
void configRegisterU8Int(const char *key, u8 *var, u32 min, u32 max);

struct configentry *configFindEntryByPtr(void *ptr);

s32 getConfigIndexFromDB(u16 deviceserial, s32 fileid);
143 changes: 111 additions & 32 deletions port/src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,10 @@
#include "config.h"
#include "system.h"
#include "utils.h"
#include "types.h"
#include "bss.h"

#define CONFIG_MAX_SECNAME 128
#define CONFIG_MAX_KEYNAME 256
#define CONFIG_MAX_SETTINGS 256

typedef enum {
CFG_NONE,
CFG_S32,
CFG_F32,
CFG_U32,
CFG_STR
} configtype;

struct configentry {
char key[CONFIG_MAX_KEYNAME + 1];
s32 seclen;
configtype type;
void *ptr;
union {
struct { f32 min_f32, max_f32; };
struct { s32 min_s32, max_s32; };
struct { u32 min_u32, max_u32; };
u32 max_str;
};
} settings[CONFIG_MAX_SETTINGS];

struct configentry settings[CONFIG_MAX_SETTINGS];
static s32 numSettings = 0;

static inline s32 configClampInt(s32 val, s32 min, s32 max)
Expand Down Expand Up @@ -69,6 +47,7 @@ static inline struct configentry *configAddEntry(const char *key)
cfg->seclen = delim ? (delim - cfg->key) : 0;
return cfg;
}
printf("configAddEntry: too many settings: %s\n", key);
return NULL;
}

Expand Down Expand Up @@ -118,6 +97,17 @@ void configRegisterUInt(const char* key, u32* var, u32 min, u32 max)
}
}

void configRegisterU8Int(const char* key, u8* var, u32 min, u32 max)
{
struct configentry* cfg = configFindOrAddEntry(key);
if (cfg) {
cfg->type = CFG_U8;
cfg->ptr = var;
cfg->min_u32 = min;
cfg->max_u32 = max;
}
}

void configRegisterFloat(const char *key, f32 *var, f32 min, f32 max)
{
struct configentry *cfg = configFindOrAddEntry(key);
Expand All @@ -139,14 +129,14 @@ void configRegisterString(const char *key, char *var, u32 maxstr)
}
}

static void configSetFromString(const char *key, const char *val)
{
struct configentry *cfg = configFindEntry(key);
if (!cfg) return;

void configSet(struct configentry *cfg, const char *val) {
s32 tmp_s32;
f32 tmp_f32;
u32 tmp_u32;
u8 tmp_u8;
if (!cfg->ptr) {
return;
}
switch (cfg->type) {
case CFG_S32:
tmp_s32 = strtol(val, NULL, 0);
Expand All @@ -169,6 +159,13 @@ static void configSetFromString(const char *key, const char *val)
}
*(u32*)cfg->ptr = tmp_u32;
break;
case CFG_U8:
tmp_u8 = strtoul(val, NULL, 0);
if (cfg->min_u8 < cfg->max_u8) {
tmp_u8 = configClampUInt(tmp_u8, cfg->min_u8, cfg->max_u8);
}
*(u8*)cfg->ptr = tmp_u8;
break;
case CFG_STR:
strncpy(cfg->ptr, val, cfg->max_str ? cfg->max_str - 1 : 4096);
break;
Expand All @@ -177,8 +174,21 @@ static void configSetFromString(const char *key, const char *val)
}
}

static inline void configSetFromString(const char *key, const char *val)
{
struct configentry *cfg = configFindOrAddEntry(key);
if (!cfg) {
return;
}

configSet(cfg, val);
}

static void configSaveEntry(struct configentry *cfg, FILE *f)
{
if (!cfg->ptr) {
return;
}
switch (cfg->type) {
case CFG_S32:
if (cfg->min_s32 < cfg->max_s32) {
Expand All @@ -198,6 +208,12 @@ static void configSaveEntry(struct configentry *cfg, FILE *f)
}
fprintf(f, "%s=%u\n", cfg->key + cfg->seclen + 1, *(u32 *)cfg->ptr);
break;
case CFG_U8:
if (cfg->min_u8 < cfg->max_u8) {
*(u8*)cfg->ptr = configClampUInt(*(u8*)cfg->ptr, cfg->min_u8, cfg->max_u8);
}
fprintf(f, "%s=%u\n", cfg->key + cfg->seclen + 1, *(u8 *)cfg->ptr);
break;
case CFG_STR:
fprintf(f, "%s=%s\n", cfg->key + cfg->seclen + 1, (char *)cfg->ptr);
break;
Expand All @@ -206,6 +222,16 @@ static void configSaveEntry(struct configentry *cfg, FILE *f)
}
}

struct configentry *configFindEntryByPtr(void *ptr)
{
for (s32 i = 0; i < numSettings; ++i) {
if (settings[i].ptr == ptr) {
return &settings[i];
}
}
return NULL;
}

s32 configSave(const char *fname)
{
FILE *f = fsFileOpenWrite(fname);
Expand All @@ -232,7 +258,23 @@ s32 configSave(const char *fname)
return 1;
}

s32 configLoad(const char *fname)
static inline s32 configLoadFileIdFromSection(const char *sec, u16 *deviceserial, s32 *fileid)
{
u32 tmp_deviceserial = 0;
s32 tmp_fileid = 0;

// Ensure sscanf matches both fields
if (sscanf(sec, "MpPlayer.%x-%x", &tmp_deviceserial, &tmp_fileid) == 2) {
*deviceserial = tmp_deviceserial;
*fileid = tmp_fileid;
return 1;
}

// If sscanf did not match both fields, return 0
return 0;
}

s32 configLoadKey(const char *fname, char *key)
{
FILE *f = fsFileOpenRead(fname);
if (!f) {
Expand All @@ -259,6 +301,13 @@ s32 configLoad(const char *fname)
continue;
}
strncpy(curSec, token, CONFIG_MAX_SECNAME);
// detect if curSec has a fileguid in it
u16 deviceserial = 0;
s32 fileid = 0;
s32 configindex = -1;
if (!key && configLoadFileIdFromSection(curSec, &deviceserial, &fileid)) {
g_GuidsToProcess[g_NumGuidsToProcess++] = (struct fileguid) { fileid, deviceserial };
}
// eat ]
line = strParseToken(line, token, NULL);
if (token[0] != ']' || token[1] != '\0') {
Expand All @@ -278,13 +327,20 @@ s32 configLoad(const char *fname)
if (line[0] == '"') {
line = strUnquote(line);
}
configSetFromString(keyBuf, line);
if (!key || (key && strcmp(keyBuf, key) == 0)) {
configSetFromString(keyBuf, line);
}
}
}

fsFileFree(f);

return 1;

}
s32 configLoad(const char *fname)
{
return configLoadKey(fname, 0);
}

void configInit(void)
Expand All @@ -293,3 +349,26 @@ void configInit(void)
configLoad(CONFIG_PATH);
}
}

s32 getPlayerConfigSlug(s32 playernum, char *out, char *key)
{
struct fileguid *guid = &(g_PlayerConfigsArray[playernum].fileguid);
if (guid && key) {
sprintf(out, "MpPlayer.%x-%x.%s\0", guid->deviceserial, guid->fileid, key);
return 1;
} else if (guid) {
sprintf(out, "MpPlayer.%x-%x\0", guid->deviceserial, guid->fileid);
return 1;
}
return 0;
}

s32 getConfigIndexFromDB(u16 deviceserial, s32 fileid) {
s32 retval = -1;
for (s32 i = 0; i < numSettings; ++i) {
if (strncmp(settings[i].key, "MpPlayer.", 9) != 0) {
continue;
}
}
return retval;
}
2 changes: 2 additions & 0 deletions src/game/filemgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -871,10 +871,12 @@ bool filemgrAttemptOperation(s32 device, bool closeonsuccess)
filemgrHandleSuccess();
}

// Success dialog: Cool!
if (showfilesaved && errnum == 0) {
menuPushDialog(&g_FilemgrFileSavedMenuDialog);
}
} else {
// file load
if (errnum == 0) {
filemgrHandleSuccess();
}
Expand Down
2 changes: 2 additions & 0 deletions src/game/lv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2443,6 +2443,8 @@ void lvStop(void)
#if VERSION >= VERSION_NTSC_1_0
menuStop();
#endif
updateGuids();
mpExtendedProfileRegisterBlank();
}

void lvCheckPauseStateChanged(void)
Expand Down
Loading

0 comments on commit a19b035

Please sign in to comment.