@@ -57,79 +57,65 @@ static void loadRubyFile(mrb_state* mrb, char* fileName, FILE* fp) {
5757 mrbc_context_free (mrb , cxt );
5858}
5959
60- // Returns true if loaded correctly
61- static bool loadBootFile (mrb_state * mrb , int argc , char * argv []) {
62- char cwd [PATH_MAX ];
63- char fileName [] = "boot.rb" ;
64- if (getcwd (cwd , sizeof (cwd )) != NULL ) {
65- // Try to open and execute the file
66- char fileSeparator [2 ] = {FILE_SEPARATOR , '\0' }; // Make a string out of the char
67- strncat (cwd , fileSeparator , 1 );
68- strncat (cwd , fileName , sizeof (cwd ) - strlen (cwd ) - 1 );
69-
70- FILE * entryPoint = fopen (cwd , "r" );
71- if (entryPoint != NULL ) {
72- forwardArguments (mrb , argc , argv , 0 );
73- loadRubyFile (mrb , fileName , entryPoint );
74- fclose (entryPoint );
75- return true;
76- }
77- }
78-
79- return false;
80- }
81-
82- static int loadGame (mrb_state * mrb , char * path , int argc , char * argv []) {
60+ static void loadGame (mrb_state * mrb , char * path , int argc , char * argv []) {
8361 // FIXME: Currently PhysFS will return a PHYSFS_ERR_NOT_FOUND error when
8462 // mounting the executable as it is, however the zip file at the
8563 // end will be correctly mounted, I'm not sure why that's it
8664 // but also not knowledgeable enough to find a solution, I think
8765 // manually writing a PhysFS_Io that points to the zip at the end
8866 // should work, see https://icculus.org/physfs/docs/html/structPHYSFS__Io.html
8967
90- // Try to load a fused archive firstly
9168 if (isFused (path )) {
69+ // Load the Ruby code with PhysFS
9270 if (!PHYSFS_exists ("main.rb" )) {
9371 fprintf (stderr , "There's no main.rb in the fused archive!\n" );
94- return 1 ;
72+ return ;
9573 }
9674
97- // Load the Ruby code with PhysFS
9875 forwardArguments (mrb , argc , argv , 0 );
9976 initFused (mrb );
10077 loadFusedRubyFile (mrb , mrb_str_new_cstr (mrb , "main.rb" ), false);
101- return 0 ;
102- }
103-
104- // Try to load boot file in the current directory
105- if (loadBootFile (mrb , argc , argv )) {
106- return 0 ;
107- }
108-
109- // Fallback to command line input
110- if (argc > 1 ) {
111- FILE * inputFile = fopen (argv [1 ], "r" );
112- if (inputFile == NULL ) {
113- fprintf (stderr , "Path %s is invalid!\n" , argv [1 ]);
114- return 1 ;
78+ } else {
79+ // Load entrypoint file in the current directory
80+ char cwd [PATH_MAX ];
81+ char fileName [] = "boot.rb" ;
82+ if (getcwd (cwd , sizeof (cwd )) != NULL ) {
83+ // Try to open and execute the file
84+ char fileSeparator [2 ] = {FILE_SEPARATOR , '\0' }; // Make a string out of the char
85+ strncat (cwd , fileSeparator , 1 );
86+ strncat (cwd , fileName , sizeof (cwd ) - strlen (cwd ) - 1 );
87+
88+ FILE * entryPoint = fopen (cwd , "r" );
89+ if (entryPoint != NULL ) {
90+ forwardArguments (mrb , argc , argv , 0 );
91+ loadRubyFile (mrb , fileName , entryPoint );
92+ fclose (entryPoint );
93+ } else {
94+ // Fallback to command line input
95+ if (argc > 1 ) {
96+ FILE * inputFile = fopen (argv [1 ], "r" );
97+ if (inputFile == NULL ) {
98+ fprintf (stderr , "Path %s is invalid!\n" , argv [1 ]);
99+ return ;
100+ }
101+ // Skip an argument since it is the file path
102+ forwardArguments (mrb , argc , argv , 1 );
103+ loadRubyFile (mrb , argv [1 ], inputFile );
104+
105+ fclose (inputFile );
106+ } else {
107+ // Print info and usage
108+ printf ("Version: %d.%d.%d (%s)\n" ,
109+ VERSION_MAJOR , VERSION_MINOR , VERSION_PATCH ,
110+ GIT_HASH );
111+ printf ("Usage: %s [inputfile]\n" , path );
112+ printf ("\n" );
113+ printf ("[inputfile]\tEither a .rb or .mrb file\n" );
114+ return ;
115+ }
116+ }
115117 }
116-
117- // Skip an argument since it's the file path
118- forwardArguments (mrb , argc , argv , 1 );
119- loadRubyFile (mrb , argv [1 ], inputFile );
120- fclose (inputFile );
121- return 0 ;
122118 }
123-
124-
125- // If all the above fails, print info and usage
126- printf ("Version: %d.%d.%d (%s)\n" ,
127- VERSION_MAJOR , VERSION_MINOR , VERSION_PATCH ,
128- GIT_HASH );
129- printf ("Usage: %s [inputfile]\n" , path );
130- printf ("\n" );
131- printf ("[inputfile]\tEither a .rb or .mrb file\n" );
132- return 0 ;
133119}
134120
135121/* Remove the executable from the absolute path */
@@ -179,12 +165,11 @@ int main(int argc, char* argv[]) {
179165
180166 if (mrb -> exc ) {
181167 mrb_print_error (mrb );
182- exitCode = 1 ;
183168 }
184169
185170 free (dirPath );
186171 free (path );
187172 PHYSFS_deinit ();
188173 mrb_close (mrb );
189- return exitCode ;
174+ return 0 ;
190175}
0 commit comments