Skip to content

Commit f9094fd

Browse files
committed
Go back to the commit where everything was done except we don't use the
`fprintf()`s now
1 parent 2030b97 commit f9094fd

1 file changed

Lines changed: 59 additions & 44 deletions

File tree

src/main.c

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -57,65 +57,79 @@ static void loadRubyFile(mrb_state* mrb, char* fileName, FILE* fp) {
5757
mrbc_context_free(mrb, cxt);
5858
}
5959

60-
static void loadGame(mrb_state* mrb, char* path, int argc, char* argv[]) {
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[]) {
6183
// FIXME: Currently PhysFS will return a PHYSFS_ERR_NOT_FOUND error when
6284
// mounting the executable as it is, however the zip file at the
6385
// end will be correctly mounted, I'm not sure why that's it
6486
// but also not knowledgeable enough to find a solution, I think
6587
// manually writing a PhysFS_Io that points to the zip at the end
6688
// should work, see https://icculus.org/physfs/docs/html/structPHYSFS__Io.html
6789

90+
// Try to load a fused archive firstly
6891
if(isFused(path)) {
69-
// Load the Ruby code with PhysFS
7092
if(!PHYSFS_exists("main.rb")) {
7193
printf("There's no main.rb in the fused archive!\n");
72-
return;
94+
return 1;
7395
}
7496

97+
// Load the Ruby code with PhysFS
7598
forwardArguments(mrb, argc, argv, 0);
7699
initFused(mrb);
77100
loadFusedRubyFile(mrb, mrb_str_new_cstr(mrb, "main.rb"), false);
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-
printf("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-
}
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;
117115
}
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;
118122
}
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;
119133
}
120134

121135
/* Remove the executable from the absolute path */
@@ -161,15 +175,16 @@ int main(int argc, char* argv[]) {
161175
mrb_warn(mrb, "Couldn't change working directory to %s", dirPath);
162176
}
163177

164-
loadGame(mrb, path, argc, argv);
178+
int exitCode = loadGame(mrb, path, argc, argv);
165179

166180
if(mrb->exc) {
167181
mrb_print_error(mrb);
182+
exitCode = 1;
168183
}
169184

170185
free(dirPath);
171186
free(path);
172187
PHYSFS_deinit();
173188
mrb_close(mrb);
174-
return 0;
189+
return exitCode;
175190
}

0 commit comments

Comments
 (0)