Skip to content

Commit

Permalink
Issue: Move save states to Onion Saves folder #35
Browse files Browse the repository at this point in the history
  • Loading branch information
steward-fu committed Dec 8, 2023
1 parent d970699 commit ba34778
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
4 changes: 3 additions & 1 deletion detour/detour.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
typedef void (*screen_copy16)(uint16_t *dest, uint32_t screen_number);
typedef int32_t (*load_state_index)(void *system, uint32_t index, uint16_t *snapshot_top, uint16_t *snapshot_bottom, uint32_t snapshot_only);
typedef int32_t (*save_state_index)(void *system, uint32_t index, uint16_t *snapshot_top, uint16_t *snapshot_bottom);
typedef int32_t (*load_state)(void *system, const char *path, uint16_t *snapshot_top, uint16_t *snapshot_bottom, uint32_t snapshot_only);
typedef int32_t (*save_state)(void *system, const char *dir, char *filename, uint16_t *snapshot_top, uint16_t *snapshot_bottom);

void detour_init(size_t page_size);
void detour_init(size_t page_size, const char *path);
void detour_quit(void);
void detour_hook(uint32_t old_func, uint32_t new_func);

Expand Down
59 changes: 58 additions & 1 deletion detour/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,48 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <sys/mman.h>

#include "detour.h"

static size_t page_size = 0;
static char states_path[255] = {0};

int dtr_savestate(int slot)
{
#ifdef MMIYOO
char buf[255] = {0};
screen_copy16 _func0 = (screen_copy16)FUN_SCREEN_COPY16;

void *d0 = malloc(0x18000);
void *d1 = malloc(0x18000);

if ((d0 != NULL) && (d1 != NULL)) {
_func0(d0, 0);
_func0(d1, 1);

if (states_path[0] == 0) {
save_state_index _func1 = (save_state_index)FUN_SAVE_STATE_INDEX;

_func1((void*)VAR_SYSTEM, slot, d0, d1);
}
else {
save_state _func1 = (save_state)FUN_SAVE_STATE;

sprintf(buf, "%s_%d.dss", VAR_SYSTEM_GAMECARD_NAME, slot);
_func1((void*)VAR_SYSTEM, states_path, buf, d0, d1);
}
}
if (d0 != NULL) {
free(d0);
}
if (d1 != NULL) {
free(d1);
}
#endif

#ifdef TRIMUI
screen_copy16 _func0 = (screen_copy16)FUN_SCREEN_COPY16;
save_state_index _func1 = (save_state_index)FUN_SAVE_STATE_INDEX;

Expand All @@ -27,13 +61,32 @@ int dtr_savestate(int slot)
if (d1 != NULL) {
free(d1);
}
#endif
}

int dtr_loadstate(int slot)
{
#ifdef MMIYOO
char buf[255] = {0};

if (states_path[0] == 0) {
load_state_index _func = (load_state_index)FUN_LOAD_STATE_INDEX;

_func((void*)VAR_SYSTEM, slot, 0, 0, 0);
}
else {
load_state _func = (load_state)FUN_LOAD_STATE;

sprintf(buf, "%s/%s_%d.dss", states_path, VAR_SYSTEM_GAMECARD_NAME, slot);
_func((void*)VAR_SYSTEM, buf, 0, 0, 0);
}
#endif

#ifdef TRIMUI
load_state_index _func = (load_state_index)FUN_LOAD_STATE_INDEX;

_func((void*)VAR_SYSTEM, slot, 0, 0, 0);
#endif
}

int dtr_quit(void)
Expand All @@ -43,9 +96,13 @@ int dtr_quit(void)
_func((void*)VAR_SYSTEM);
}

void detour_init(size_t page)
void detour_init(size_t page, const char *path)
{
page_size = page;

if ((path != NULL) && (path[0] != 0)) {
strcpy(states_path, path);
}
}

void detour_hook(uint32_t old_func, uint32_t new_func)
Expand Down
3 changes: 2 additions & 1 deletion drastic/resources/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
"auto_slot":10,
"half_vol":0,
"splash":1,
"hotkey":0
"hotkey":0,
"states":""
}
25 changes: 24 additions & 1 deletion sdl2/src/video/mmiyoo/SDL_video_mmiyoo.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
Expand Down Expand Up @@ -1328,6 +1330,27 @@ static int read_config(void)
printf(PREFIX"[json] nds.hotkey: %d\n", nds.hotkey);
}

#ifdef MMIYOO
json_object_object_get_ex(jfile, JSON_NDS_STATES, &jval);
if (jval) {
struct stat st = {0};
const char *path = json_object_get_string(jval);

if ((path != NULL) && (path[0] != 0)) {
strcpy(nds.states.path, path);
printf(PREFIX"[json] states: %s\n", nds.states.path);

if (stat(nds.states.path, &st) == -1) {
mkdir(nds.states.path, 0755);
printf(PREFIX"create states folder under \'%s\'\n", nds.states.path);
}
}
else {
printf(PREFIX"use the default state path\n");
}
}
#endif

reload_pen();
#ifdef MMIYOO
reload_overlay();
Expand Down Expand Up @@ -3449,7 +3472,7 @@ int MMIYOO_VideoInit(_THIS)
MMIYOO_EventInit();

if (nds.cust_menu) {
detour_init(sysconf(_SC_PAGESIZE));
detour_init(sysconf(_SC_PAGESIZE), nds.states.path);
detour_hook(FUN_PRINT_STRING, (uint32_t)sdl_print_string);
detour_hook(FUN_SAVESTATE_PRE, (uint32_t)sdl_savestate_pre);
detour_hook(FUN_SAVESTATE_POST, (uint32_t)sdl_savestate_post);
Expand Down
5 changes: 5 additions & 0 deletions sdl2/src/video/mmiyoo/SDL_video_mmiyoo.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
#define JSON_NDS_AUTO_STATE "auto_state"
#define JSON_NDS_AUTO_SLOT "auto_slot"
#define JSON_NDS_HOTKEY "hotkey"
#define JSON_NDS_STATES "states"

#define GFX_ACTION_NONE 0
#define GFX_ACTION_FLIP 1
Expand Down Expand Up @@ -282,6 +283,10 @@ typedef struct _NDS {
TTF_Font *font;
uint32_t state;

struct _STATES {
char path[MAX_PATH];
} states;

struct _CFG {
char path[MAX_PATH];
} cfg;
Expand Down

0 comments on commit ba34778

Please sign in to comment.