shithub: choc

Download patch

ref: c06b2c5f8e22d7cf54a502df5042de87d6d2218e
parent: 0121b8c02bd1746767e17fa41b848a11744f593e
author: Simon Howard <[email protected]>
date: Fri Nov 2 18:24:18 EDT 2018

Add WAD file autoloading.

Implements most of #1052, adding a new config file variable named
`autoload_path` that is auto-configured on first run. The actual
files are autoloaded from subdirectories named by IWAD file name that
are automatically created. It's sufficient to just drop .WAD and .DEH
files into the appropriate directory to have them automatically load
on startup.

Also add a -noautoload command line parameter to disable the autoload
functionality on occasion if desired.

--- a/src/deh_main.c
+++ b/src/deh_main.c
@@ -21,6 +21,7 @@
 #include <ctype.h>
 
 #include "doomtype.h"
+#include "i_glob.h"
 #include "i_system.h"
 #include "d_iwad.h"
 #include "m_argv.h"
@@ -404,6 +405,27 @@
     }
 
     return 1;
+}
+
+// Load all dehacked patches from the given directory.
+void DEH_AutoLoadPatches(const char *path)
+{
+    const char *filename;
+    glob_t *glob;
+
+    glob = I_StartGlob(path, "*.deh", GLOB_FLAG_NOCASE|GLOB_FLAG_SORTED);
+    for (;;)
+    {
+        filename = I_NextGlob(glob);
+        if (filename == NULL)
+        {
+            break;
+        }
+        printf(" [autoload]");
+        DEH_LoadFile(filename);
+    }
+
+    I_EndGlob(glob);
 }
 
 // Load dehacked file from WAD lump.
--- a/src/deh_main.h
+++ b/src/deh_main.h
@@ -31,6 +31,7 @@
 
 void DEH_ParseCommandLine(void);
 int DEH_LoadFile(const char *filename);
+void DEH_AutoLoadPatches(const char *path);
 int DEH_LoadLump(int lumpnum, boolean allow_long, boolean allow_error);
 int DEH_LoadLumpByName(const char *name, boolean allow_long, boolean allow_error);
 
--- a/src/doom/d_main.c
+++ b/src/doom/d_main.c
@@ -1506,6 +1506,20 @@
         DEH_AddStringReplacement("M_SCRNSZ", "M_DISP");
     }
 
+    //!
+    // @category mod
+    //
+    // Disable auto-loading of .wad and .deh files.
+    //
+    if (!M_ParmExists("-noautoload") && gamemode != shareware)
+    {
+        char *autoload_dir;
+        autoload_dir = M_GetAutoloadDir(D_SaveGameIWADName(gamemission));
+        DEH_AutoLoadPatches(autoload_dir);
+        W_AutoLoadWADs(autoload_dir);
+        free(autoload_dir);
+    }
+
     // Load Dehacked patches specified on the command line with -deh.
     // Note that there's a very careful and deliberate ordering to how
     // Dehacked patches are loaded. The order we use is:
--- a/src/heretic/d_main.c
+++ b/src/heretic/d_main.c
@@ -901,6 +901,20 @@
     D_AddFile(iwadfile);
     W_CheckCorrectIWAD(heretic);
 
+    //!
+    // @category mod
+    //
+    // Disable auto-loading of .wad files.
+    //
+    if (!M_ParmExists("-noautoload"))
+    {
+        char *autoload_dir;
+        autoload_dir = M_GetAutoloadDir("heretic.wad");
+        // TODO? DEH_AutoLoadPatches(autoload_dir);
+        W_AutoLoadWADs(autoload_dir);
+        free(autoload_dir);
+    }
+
     // Load dehacked patches specified on the command line.
     DEH_ParseCommandLine();
 
--- a/src/hexen/h2_main.c
+++ b/src/hexen/h2_main.c
@@ -422,6 +422,20 @@
     D_SetGameDescription();
     AdjustForMacIWAD();
 
+    //!
+    // @category mod
+    //
+    // Disable auto-loading of .wad files.
+    //
+    if (!M_ParmExists("-noautoload"))
+    {
+        char *autoload_dir;
+        autoload_dir = M_GetAutoloadDir("hexen.wad");
+        // TODO? DEH_AutoLoadPatches(autoload_dir);
+        W_AutoLoadWADs(autoload_dir);
+        free(autoload_dir);
+    }
+
     HandleArgs();
 
     I_PrintStartupBanner(gamedescription);
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -33,6 +33,7 @@
 #include "doomkeys.h"
 #include "i_system.h"
 #include "m_argv.h"
+#include "m_config.h"
 #include "m_misc.h"
 
 #include "z_zone.h"
@@ -46,6 +47,8 @@
 
 const char *configdir;
 
+static char *autoload_path = "";
+
 // Default filenames for configuration files.
 
 static const char *default_main_config;
@@ -909,6 +912,14 @@
     CONFIG_VARIABLE_FLOAT(libsamplerate_scale),
 
     //!
+    // Full path to a directory in which WAD files and dehacked patches
+    // can be placed to be automatically loaded on startup. A subdirectory
+    // of this directory matching the IWAD name is checked to find the
+    // files to load.
+
+    CONFIG_VARIABLE_STRING(autoload_path),
+
+    //!
     // Full path to a directory containing configuration files for
     // substitute music packs. These packs contain high quality renderings
     // of game music to be played instead of using the system's built-in
@@ -1977,7 +1988,10 @@
 void M_LoadDefaults (void)
 {
     int i;
- 
+
+    // This variable is a special snowflake for no good reason.
+    M_BindStringVariable("autoload_path", &autoload_path);
+
     // check for a custom default file
 
     //!
@@ -2315,5 +2329,31 @@
     }
 
     return savegamedir;
+}
+
+//
+// Calculate the path to the directory for autoloaded WADs/DEHs.
+// Creates the directory as necessary.
+//
+char *M_GetAutoloadDir(const char *iwadname)
+{
+    char *result;
+
+    if (autoload_path == NULL || strlen(autoload_path) == 0)
+    {
+        char *prefdir;
+        prefdir = SDL_GetPrefPath("", PACKAGE_TARNAME);
+        autoload_path = M_StringJoin(prefdir, "autoload", NULL);
+        free(prefdir);
+    }
+
+    M_MakeDirectory(autoload_path);
+
+    result = M_StringJoin(autoload_path, DIR_SEPARATOR_S, iwadname, NULL);
+    M_MakeDirectory(result);
+
+    // TODO: Add README file
+
+    return result;
 }
 
--- a/src/m_config.h
+++ b/src/m_config.h
@@ -36,6 +36,7 @@
 float M_GetFloatVariable(const char *name);
 void M_SetConfigFilenames(const char *main_config, const char *extra_config);
 char *M_GetSaveGameDir(const char *iwadname);
+char *M_GetAutoloadDir(const char *iwadname);
 
 extern const char *configdir;
 
--- a/src/strife/d_main.c
+++ b/src/strife/d_main.c
@@ -1736,6 +1736,20 @@
     W_CheckCorrectIWAD(strife);
     D_IdentifyVersion();
 
+    //!
+    // @category mod
+    //
+    // Disable auto-loading of .wad files.
+    //
+    if (!M_ParmExists("-noautoload"))
+    {
+        char *autoload_dir;
+        autoload_dir = M_GetAutoloadDir("strife1.wad");
+        // TODO? DEH_AutoLoadPatches(autoload_dir);
+        W_AutoLoadWADs(autoload_dir);
+        free(autoload_dir);
+    }
+
     // Load dehacked patches specified on the command line.
     DEH_ParseCommandLine();
 
--- a/src/w_main.c
+++ b/src/w_main.c
@@ -20,6 +20,7 @@
 
 #include "config.h"
 #include "d_iwad.h"
+#include "i_glob.h"
 #include "i_system.h"
 #include "m_argv.h"
 #include "w_main.h"
@@ -198,6 +199,27 @@
 //    W_PrintDirectory();
 
     return modifiedgame;
+}
+
+// Load all WAD files from the given directory.
+void W_AutoLoadWADs(const char *path)
+{
+    glob_t *glob;
+    const char *filename;
+
+    glob = I_StartGlob(path, "*.wad", GLOB_FLAG_NOCASE|GLOB_FLAG_SORTED);
+    for (;;)
+    {
+        filename = I_NextGlob(glob);
+        if (filename == NULL)
+        {
+            break;
+        }
+        printf(" [autoload] merging %s\n", filename);
+        W_MergeFile(filename);
+    }
+
+    I_EndGlob(glob);
 }
 
 // Lump names that are unique to particular game types. This lets us check
--- a/src/w_main.h
+++ b/src/w_main.h
@@ -23,5 +23,8 @@
 boolean W_ParseCommandLine(void);
 void W_CheckCorrectIWAD(GameMission_t mission);
 
+// Autoload all .wad files from the given directory:
+void W_AutoLoadWADs(const char *path);
+
 #endif /* #ifndef W_MAIN_H */