shithub: zelda3

Download patch

ref: 9d083a7864e2ad20b620c9309f899f63d2fda3e8
parent: 3be6ece9f9af832950699fb1c448d6eec8ebc47a
author: Snesrev <[email protected]>
date: Fri Oct 14 19:10:42 EDT 2022

Allow --config to be specified to switch config file

--- a/.gitignore
+++ b/.gitignore
@@ -25,4 +25,5 @@
 /msu/alttp_msu-*.pcm
 /tmp/
 /tables/zelda3_assets.dat
-/SDL2.dll
\ No newline at end of file
+/SDL2.dll
+/zelda3.user.ini
--- a/config.c
+++ b/config.c
@@ -332,14 +332,11 @@
   return false;
 }
 
-void ParseConfigFile() {
-  char *filedata = (char*)ReadWholeFile("zelda3.user.ini", NULL), *p;
-  if (!filedata) {
-    filedata = (char *)ReadWholeFile("zelda3.ini", NULL);
-    if (!filedata)
-      return;
-  }
-  fprintf(stderr, "Loading zelda3.ini\n");
+static bool ParseOneConfigFile(const char *filename, int depth) {
+  char *filedata = (char*)ReadWholeFile(filename, NULL), *p;
+  if (!filedata)
+    return false;
+  
   int section = -2;
   g_config.memory_buffer = filedata;
 
@@ -349,21 +346,34 @@
     if (*p == '[') {
       section = GetIniSection(p);
       if (section < 0)
-        fprintf(stderr, "zelda3.ini:%d: Invalid .ini section %s\n", lineno, p);
+        fprintf(stderr, "%s:%d: Invalid .ini section %s\n", filename, lineno, p);
+    } else if (*p == '!' && SkipPrefix(p + 1, "include ")) {
+      char *tt = p + 8;
+      char *new_filename = ReplaceFilenameWithNewPath(filename, NextPossiblyQuotedString(&tt));
+      if (depth > 10 || !ParseOneConfigFile(new_filename, depth + 1))
+        fprintf(stderr, "Warning: Unable to read %s\n", new_filename);
+      free(new_filename);
     } else if (section == -2) {
-      fprintf(stderr, "zelda3.ini:%d: Expecting [section]\n", lineno);
+      fprintf(stderr, "%s:%d: Expecting [section]\n", filename, lineno);
     } else {
       char *v = SplitKeyValue(p);
       if (v == NULL) {
-        fprintf(stderr, "zelda3.ini:%d: Expecting 'key=value'\n", lineno);
+        fprintf(stderr, "%s:%d: Expecting 'key=value'\n", filename, lineno);
         continue;
       }
       if (section >= 0 && !HandleIniConfig(section, p, v))
-        fprintf(stderr, "zelda3.ini:%d: Can't parse '%s'\n", lineno, p);
+        fprintf(stderr, "%s:%d: Can't parse '%s'\n", filename, lineno, p);
     }
   }
+  return true;
 }
 
-void AfterConfigParse() {
+void ParseConfigFile(const char *filename) {
+  if (filename != NULL || !ParseOneConfigFile("zelda3.user.ini", 0)) {
+    if (filename == NULL)
+      filename = "zelda3.ini";
+    if (!ParseOneConfigFile(filename, 0))
+      fprintf(stderr, "Warning: Unable to read config file %s\n", filename);
+  }
   RegisterDefaultKeys();
 }
--- a/config.h
+++ b/config.h
@@ -72,6 +72,5 @@
 
 extern Config g_config;
 
-void ParseConfigFile();
-void AfterConfigParse();
+void ParseConfigFile(const char *filename);
 int FindCmdForSdlKey(SDL_Keycode code, SDL_Keymod mod);
--- a/main.c
+++ b/main.c
@@ -263,9 +263,15 @@
 
 #undef main
 int main(int argc, char** argv) {
-  SwitchDirectory();
-  ParseConfigFile();
-  AfterConfigParse();
+  argc--, argv++;
+  const char *config_file = NULL;
+  if (argc >= 2 && strcmp(argv[0], "--config") == 0) {
+    config_file = argv[1];
+    argc -= 2, argv += 2;
+  } else {
+    SwitchDirectory();
+  }
+  ParseConfigFile(config_file);
   LoadAssets();
   LoadLinkGraphics();
 
@@ -353,8 +359,8 @@
     g_audiobuffer = malloc(g_frames_per_block * have.channels * sizeof(int16));
   }
 
-  if (argc >= 2 && !g_run_without_emu)
-    LoadRom(argv[1]);
+  if (argc >= 1 && !g_run_without_emu)
+    LoadRom(argv[0]);
 
 #if defined(_WIN32)
   _mkdir("saves");