Skip to content

Commit 3f23cca

Browse files
committed
quick save at exit and at poweroff, save files in rom folder
Signed-off-by: Vincent-FK <[email protected]>
1 parent daee88a commit 3f23cca

File tree

5 files changed

+422
-45
lines changed

5 files changed

+422
-45
lines changed

platform/common/emu.c

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ int need_screen_cleared = 0;
5858
static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
5959

6060
/* tmp buff to reduce stack usage for plats with small stack */
61-
static char static_buff[512];
61+
static char static_buff[1024];
6262
const char *rom_fname_reload;
63-
char rom_fname_loaded[512];
63+
char rom_fname_loaded[1024];
6464
int reset_timing = 0;
6565
static unsigned int notice_msg_time; /* when started showing */
6666
static char noticeMsg[40];
@@ -113,9 +113,13 @@ static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext
113113

114114
*dst = 0;
115115
if (prefix) {
116-
int len = plat_get_root_dir(dst, dstlen);
116+
/*int len = plat_get_root_dir(dst, dstlen);
117117
strcpy(dst + len, prefix);
118-
prefix_len = len + strlen(prefix);
118+
prefix_len = len + strlen(prefix);*/
119+
120+
/* Saves are in ROM folder */
121+
prefix_len = strlen(mRomPath)+1;
122+
sprintf(dst, "%s/", mRomPath);
119123
}
120124

121125
p = fname + strlen(fname) - 1;
@@ -879,6 +883,22 @@ int emu_check_save_file(int slot, int *time)
879883
return emu_get_save_fname(1, 0, slot, time) ? 1 : 0;
880884
}
881885

886+
int emu_save_load_game_from_file(int load, char *saveFname){
887+
int ret = PicoState(saveFname, !load);
888+
if (!ret) {
889+
#ifdef __GP2X__
890+
if (!load) sync();
891+
#endif
892+
//emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");
893+
} else {
894+
//emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");
895+
ret = -1;
896+
}
897+
898+
return ret;
899+
}
900+
901+
882902
int emu_save_load_game(int load, int sram)
883903
{
884904
int ret = 0;
@@ -1387,6 +1407,47 @@ void emu_cmn_forced_frame(int no_scale, int do_emu)
13871407
PicoIn.opt = po_old;
13881408
}
13891409

1410+
1411+
1412+
1413+
/* Quick save and turn off the console */
1414+
void quick_save_and_poweroff()
1415+
{
1416+
/* Vars */
1417+
char shell_cmd[1024];
1418+
FILE *fp;
1419+
1420+
/* Send command to kill any previously scheduled shutdown */
1421+
sprintf(shell_cmd, "pkill %s", SHELL_CMD_SCHEDULE_POWERDOWN);
1422+
fp = popen(shell_cmd, "r");
1423+
if (fp == NULL) {
1424+
printf("Failed to run command %s\n", shell_cmd);
1425+
}
1426+
1427+
/* Save */
1428+
emu_save_load_game_from_file(0, quick_save_file);
1429+
1430+
/* Write quick load file */
1431+
sprintf(shell_cmd, "%s SDL_NOMOUSE=1 \"%s\" -loadStateFile \"%s\" \"%s\"",
1432+
SHELL_CMD_WRITE_QUICK_LOAD_CMD, prog_name, quick_save_file, mRomName);
1433+
printf("Cmd write quick load file:\n %s\n", shell_cmd);
1434+
fp = popen(shell_cmd, "r");
1435+
if (fp == NULL) {
1436+
printf("Failed to run command %s\n", shell_cmd);
1437+
}
1438+
1439+
/* Clean Poweroff */
1440+
sprintf(shell_cmd, "%s", SHELL_CMD_POWERDOWN);
1441+
fp = popen(shell_cmd, "r");
1442+
if (fp == NULL) {
1443+
printf("Failed to run command %s\n", shell_cmd);
1444+
}
1445+
1446+
/* Exit Emulator */
1447+
engineState = PGS_Quit;
1448+
}
1449+
1450+
13901451
void emu_init(void)
13911452
{
13921453
char path[512];
@@ -1643,6 +1704,12 @@ void emu_loop(void)
16431704
diff = timestamp_aim_x3 - timestamp_x3;
16441705
}
16451706

1707+
/* Quick save and poweroff */
1708+
if(mQuickSaveAndPoweroff){
1709+
quick_save_and_poweroff();
1710+
mQuickSaveAndPoweroff = 0;
1711+
}
1712+
16461713
emu_update_input();
16471714
if (skip) {
16481715
int do_audio = diff > -target_frametime_x3 * 2;

platform/common/emu.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,20 @@ extern int reset_timing;
8787
extern int flip_after_sync;
8888
extern int show_fps_bypass;
8989
extern int need_screen_cleared;
90+
extern int mQuickSaveAndPoweroff;
91+
92+
extern char *prog_name;
93+
extern char *mRomName;
94+
extern char *mRomPath;
95+
extern char *quick_save_file;
9096

9197
#define PICO_PEN_ADJUST_X 4
9298
#define PICO_PEN_ADJUST_Y 2
9399
extern int pico_pen_x, pico_pen_y;
94100
extern int pico_inp_mode;
95101

96102
extern const char *rom_fname_reload; // ROM to try loading on next PGS_ReloadRom
97-
extern char rom_fname_loaded[512]; // currently loaded ROM filename
103+
extern char rom_fname_loaded[1024]; // currently loaded ROM filename
98104

99105
// engine states
100106
extern int engineState;
@@ -118,6 +124,7 @@ void emu_loop(void);
118124
int emu_reload_rom(const char *rom_fname_in);
119125
int emu_swap_cd(const char *fname);
120126
int emu_save_load_game(int load, int sram);
127+
int emu_save_load_game_from_file(int load, char *saveFname);
121128
void emu_reset_game(void);
122129

123130
void emu_prep_defconfig(void);

platform/common/main.c

Lines changed: 119 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
#include <stdio.h>
1010
#include <stdlib.h>
11+
#include <unistd.h>
1112
#include <string.h>
1213
#include <strings.h>
14+
#include <signal.h>
1315

1416
#include "../libpicofe/input.h"
1517
#include "../libpicofe/plat.h"
@@ -19,8 +21,39 @@
1921
#include <cpu/debug.h>
2022

2123

22-
static int load_state_slot = -1;
2324
char **g_argv;
25+
char *prog_name;
26+
static char *load_state_file = NULL;
27+
static int load_state_slot = -1;
28+
static char *quick_save_file_extension = "quicksave";
29+
char *mRomName = NULL;
30+
char *mRomPath = NULL;
31+
char *quick_save_file = NULL;
32+
int mQuickSaveAndPoweroff=0;
33+
34+
35+
void usage(){
36+
printf("\n\n\nPicoDrive v" VERSION " (c) notaz, 2006-2009,2013\n");
37+
printf("usage: PicoDriveBin [options] [romfile]\n");
38+
printf("options:\n"
39+
" -config <file> use specified config file instead of default 'config.cfg'\n"
40+
" -fps use to show fps\n"
41+
" -loadStateSlot <num> if ROM is specified, try loading savestate slot <num>\n"
42+
" -loadStateFile <filePath> if ROM is specified, try loading savestate file <filePath>\n");
43+
exit(1);
44+
}
45+
46+
/* Handler for SIGUSR1, caused by closing the console */
47+
void handle_sigusr1(int sig)
48+
{
49+
//printf("Caught signal USR1 %d\n", sig);
50+
51+
/* Exit menu if it was launched */
52+
stop_menu_loop = 1;
53+
54+
/* Signal to quick save and poweoff after next loop */
55+
mQuickSaveAndPoweroff = 1;
56+
}
2457

2558
void parse_cmd_line(int argc, char *argv[])
2659
{
@@ -33,11 +66,15 @@ void parse_cmd_line(int argc, char *argv[])
3366
if (strcasecmp(argv[x], "-config") == 0) {
3467
if (x+1 < argc) { ++x; PicoConfigFile = argv[x]; }
3568
}
36-
else if (strcasecmp(argv[x], "-loadstate") == 0
69+
else if (strcasecmp(argv[x], "-loadStateSlot") == 0
3770
|| strcasecmp(argv[x], "-load") == 0)
3871
{
3972
if (x+1 < argc) { ++x; load_state_slot = atoi(argv[x]); }
4073
}
74+
else if (strcasecmp(argv[x], "-loadStateFile") == 0)
75+
{
76+
if (x+1 < argc) { ++x; load_state_file = argv[x]; }
77+
}
4178
else if (strcasecmp(argv[x], "-fps") == 0) {
4279
currentConfig.EmuOpt |= EOPT_SHOW_FPS;
4380
show_fps_bypass = 1;
@@ -52,27 +89,44 @@ void parse_cmd_line(int argc, char *argv[])
5289
unrecognized = 1;
5390
break;
5491
}
55-
} else {
92+
}
93+
/* Check if file exists, Save ROM name, and ROM path */
94+
else {
95+
mRomName = argv[x];
5696
FILE *f = fopen(argv[x], "rb");
5797
if (f) {
98+
/* Save Rom path */
99+
mRomPath = (char *)malloc(strlen(mRomName)+1);
100+
strcpy(mRomPath, mRomName);
101+
char *slash = strrchr ((char*)mRomPath, '/');
102+
*slash = 0;
103+
104+
/* Rom name without extension */
105+
char *point = strrchr ((char*)slash+1, '.');
106+
*point = 0;
107+
108+
/* Set quicksave filename */
109+
quick_save_file = (char *)malloc(strlen(mRomPath) + strlen(slash+1) +
110+
strlen(quick_save_file_extension) + 2 + 1);
111+
sprintf(quick_save_file, "%s/%s.%s",
112+
mRomPath, slash+1, quick_save_file_extension);
113+
printf("Quick_save_file: %s\n", quick_save_file);
114+
115+
/* Close file*/
58116
fclose(f);
59117
rom_fname_reload = argv[x];
60118
engineState = PGS_ReloadRom;
61119
}
62-
else
120+
else{
121+
printf("Rom %s not found \n", mRomName);
63122
unrecognized = 1;
123+
}
64124
break;
65125
}
66126
}
67127

68128
if (unrecognized) {
69-
printf("\n\n\nPicoDrive v" VERSION " (c) notaz, 2006-2009,2013\n");
70-
printf("usage: %s [options] [romfile]\n", argv[0]);
71-
printf("options:\n"
72-
" -config <file> use specified config file instead of default 'config.cfg'\n"
73-
" -fps use to show fps\n"
74-
" -loadstate <num> if ROM is specified, try loading savestate slot <num>\n");
75-
exit(1);
129+
usage();
76130
}
77131
}
78132

@@ -81,6 +135,23 @@ int main(int argc, char *argv[])
81135
{
82136
g_argv = argv;
83137

138+
/* Save program name */
139+
prog_name = argv[0];
140+
141+
/* Engine initial state */
142+
engineState = PGS_Menu;
143+
144+
/* Parse arguments */
145+
if (argc > 1){
146+
parse_cmd_line(argc, argv);
147+
}
148+
else{
149+
usage();
150+
}
151+
152+
/* Init Signals */
153+
signal(SIGUSR1, handle_sigusr1);
154+
84155
plat_early_init();
85156

86157
in_init();
@@ -95,19 +166,47 @@ int main(int argc, char *argv[])
95166
emu_init();
96167
menu_init();
97168

98-
engineState = PGS_Menu;
99-
100-
if (argc > 1)
101-
parse_cmd_line(argc, argv);
102-
103169
if (engineState == PGS_ReloadRom)
104170
{
105171
if (emu_reload_rom(rom_fname_reload)) {
106172
engineState = PGS_Running;
107-
if (load_state_slot >= 0) {
108-
state_slot = load_state_slot;
109-
emu_save_load_game(1, 0);
110-
}
173+
174+
/* Load slot */
175+
if(load_state_slot != -1){
176+
printf("LOADING FROM SLOT %d...\n", load_state_slot+1);
177+
char fname[1024];
178+
emu_save_load_game(1, 0);
179+
printf("LOADED FROM SLOT %d\n", load_state_slot+1);
180+
load_state_slot = -1;
181+
}
182+
/* Load file */
183+
else if(load_state_file != NULL){
184+
printf("LOADING FROM FILE %s...\n", load_state_file);
185+
emu_save_load_game_from_file(1, load_state_file);
186+
printf("LOADED FROM SLOT %s\n", load_state_file);
187+
load_state_file = NULL;
188+
}
189+
/* Load quick save file */
190+
else if(access( quick_save_file, F_OK ) != -1){
191+
printf("Found quick save file: %s\n", quick_save_file);
192+
193+
int resume = launch_resume_menu_loop();
194+
if(resume == RESUME_YES){
195+
printf("Resume game from quick save file: %s\n", quick_save_file);
196+
emu_save_load_game_from_file(1, quick_save_file);
197+
}
198+
else{
199+
printf("Reset game\n");
200+
}
201+
}
202+
203+
/* Remove quicksave file if present */
204+
if (remove(quick_save_file) == 0){
205+
printf("Deleted successfully: %s\n", quick_save_file);
206+
}
207+
else{
208+
printf("Unable to delete the file: %s\n", quick_save_file);
209+
}
111210
}
112211
}
113212

0 commit comments

Comments
 (0)