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"
1921#include <cpu/debug.h>
2022
2123
22- static int load_state_slot = -1 ;
2324char * * 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
2558void 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