move PhysicsFS initialisation, search path setup and argument reading to physfsx.h
authorChris Taylor <chris@icculus.org>
Sat, 29 Jul 2006 04:02:49 +0000 (04:02 +0000)
committerChris Taylor <chris@icculus.org>
Sat, 29 Jul 2006 04:02:49 +0000 (04:02 +0000)
ChangeLog
include/physfsx.h
main/inferno.c

index fd47da3..d0c9176 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,9 @@
        * main/inferno.c, main/titles.c, main/titles.h:
        move titles, loading screen and order form showing to titles.c
 
+       * include/physfsx.h, main/inferno.c: move PhysicsFS
+       initialisation, search path setup and argument reading to physfsx.h
+
 2006-07-27  Chris Taylor  <chris@icculus.org>
 
        * main/inferno.c, main/multi.c, main/multi.h:
index b946c8d..837918d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: physfsx.h,v 1.11 2005-06-13 11:22:02 chris Exp $ */
+/* $Id: physfsx.h,v 1.12 2006-07-29 04:02:40 chris Exp $ */
 
 /*
  *
 #include "pstypes.h"
 #include "error.h"
 #include "vecmat.h"
+#include "args.h"
+
+// Initialise PhysicsFS, set up basic search paths and add arguments from .ini file(s).
+// The arguments are used to determine the search paths, so the first .ini file must be
+// in the same directory as D2X. A second one can be in the user directory.
+static inline void PHYSFSX_init(int argc, char *argv[])
+{
+       int t;
+
+       PHYSFS_init(argv[0]);
+       PHYSFS_permitSymbolicLinks(1);
+
+       PHYSFS_addToSearchPath(PHYSFS_getBaseDir(), 1);
+       InitArgs( argc,argv );
+
+       if ((t = FindArg("-userdir"))
+#ifdef __unix__
+               || 1    // or if it's a unix platform
+#endif
+               )
+       {
+               // This stuff below seems overly complicated - brad
+
+               char *path = Args[t+1];
+               char fullPath[PATH_MAX + 5];
+
+#ifdef __unix__
+               if (!t)
+                       path = "~/.d2x";
+#endif
+               PHYSFS_removeFromSearchPath(PHYSFS_getBaseDir());
+
+               if (path[0] == '~') // yes, this tilde can be put before non-unix paths.
+               {
+                       const char *home = PHYSFS_getUserDir();
+
+                       strcpy(fullPath, home); // prepend home to the path
+                       path++;
+                       if (*path == *PHYSFS_getDirSeparator())
+                               path++;
+                       strncat(fullPath, path, PATH_MAX + 5 - strlen(home));
+               }
+               else
+                       strncpy(fullPath, path, PATH_MAX + 5);
+
+               PHYSFS_setWriteDir(fullPath);
+               if (!PHYSFS_getWriteDir())
+               {                                               // need to make it
+                       char *p;
+                       char ancestor[PATH_MAX + 5];    // the directory which actually exists
+                       char child[PATH_MAX + 5];               // the directory relative to the above we're trying to make
+
+                       strcpy(ancestor, fullPath);
+                       while (!PHYSFS_getWriteDir() && ((p = strrchr(ancestor, *PHYSFS_getDirSeparator()))))
+                       {
+                               if (p[1] == 0)
+                               {                                       // separator at the end (intended here, for safety)
+                                       *p = 0;                 // kill this separator
+                                       if (!((p = strrchr(ancestor, *PHYSFS_getDirSeparator()))))
+                                               break;          // give up, this is (usually) the root directory
+                               }
+
+                               p[1] = 0;                       // go to parent
+                               PHYSFS_setWriteDir(ancestor);
+                       }
+
+                       strcpy(child, fullPath + strlen(ancestor));
+                       for (p = child; (p = strchr(p, *PHYSFS_getDirSeparator())); p++)
+                               *p = '/';
+                       PHYSFS_mkdir(child);
+                       PHYSFS_setWriteDir(fullPath);
+               }
+
+               PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 1);
+               AppendArgs();
+       }
+
+       if (!PHYSFS_getWriteDir())
+       {
+               PHYSFS_setWriteDir(PHYSFS_getBaseDir());
+               if (!PHYSFS_getWriteDir())
+                       Error("can't set write dir\n");
+               else
+                       PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 0);
+       }
+
+       //tell cfile where hogdir is
+       if ((t=FindArg("-hogdir")))
+               PHYSFS_addToSearchPath(Args[t + 1], 1);
+#ifdef __unix__
+       else if (!FindArg("-nohogdir"))
+               PHYSFS_addToSearchPath(SHAREPATH, 1);
+#endif
+}
 
 static inline int PHYSFSX_readString(PHYSFS_file *file, char *s)
 {
index d2a5509..64cdaa6 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: inferno.c,v 1.108 2006-07-29 03:49:00 chris Exp $ */
+/* $Id: inferno.c,v 1.109 2006-07-29 04:02:49 chris Exp $ */
 /*
 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
@@ -466,101 +466,17 @@ int main(int argc, char *argv[])
        int i, t;
        ubyte title_pal[768];
 
-       PHYSFS_init(argv[0]);
-       PHYSFS_permitSymbolicLinks(1);
-
        con_init();  // Initialise the console
        mem_init();
 
        error_init(NULL, NULL);
+       PHYSFSX_init(argc, argv);
 
-       PHYSFS_addToSearchPath(PHYSFS_getBaseDir(), 1);
-       InitArgs( argc,argv );
-
-       if ((t = FindArg("-userdir"))
-#ifdef __unix__
-        || 1   // or if it's a unix platform
-#endif
-        )
-       {
-               // This stuff below seems overly complicated - brad
-
-               char *path = Args[t+1];
-               char fullPath[PATH_MAX + 5];
-
-#ifdef __unix__
-               if (!t)
-                       path = "~/.d2x";
-#endif
-               PHYSFS_removeFromSearchPath(PHYSFS_getBaseDir());
-               
-               if (path[0] == '~') // yes, this tilde can be put before non-unix paths.
-               {
-                       const char *home = PHYSFS_getUserDir();
-                       
-                       strcpy(fullPath, home); // prepend home to the path
-                       path++;
-                       if (*path == *PHYSFS_getDirSeparator())
-                               path++;
-                       strncat(fullPath, path, PATH_MAX + 5 - strlen(home));
-               }
-               else
-                       strncpy(fullPath, path, PATH_MAX + 5);
-               
-               PHYSFS_setWriteDir(fullPath);
-               if (!PHYSFS_getWriteDir())
-               {                                               // need to make it
-                       char *p;
-                       char ancestor[PATH_MAX + 5];    // the directory which actually exists
-                       char child[PATH_MAX + 5];               // the directory relative to the above we're trying to make
-
-                       strcpy(ancestor, fullPath);
-                       while (!PHYSFS_getWriteDir() && ((p = strrchr(ancestor, *PHYSFS_getDirSeparator()))))
-                       {
-                               if (p[1] == 0)
-                               {                                       // separator at the end (intended here, for safety)
-                                       *p = 0;                 // kill this separator
-                                       if (!((p = strrchr(ancestor, *PHYSFS_getDirSeparator()))))
-                                               break;          // give up, this is (usually) the root directory
-                               }
-
-                               p[1] = 0;                       // go to parent
-                               PHYSFS_setWriteDir(ancestor);
-                       }
-
-                       strcpy(child, fullPath + strlen(ancestor));
-                       for (p = child; (p = strchr(p, *PHYSFS_getDirSeparator())); p++)
-                               *p = '/';
-                       PHYSFS_mkdir(child);
-                       PHYSFS_setWriteDir(fullPath);
-               }
-
-               PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 1);
-               AppendArgs();
-       }
-
-       if (!PHYSFS_getWriteDir())
-       {
-               PHYSFS_setWriteDir(PHYSFS_getBaseDir());
-               if (!PHYSFS_getWriteDir())
-                       Error("can't set write dir\n");
-               else
-                       PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 0);
-       }
-       
        if (FindArg("-debug"))
                con_threshold.value = (float)2;
        else if (FindArg("-verbose"))
                con_threshold.value = (float)1;
 
-       //tell cfile where hogdir is
-       if ((t=FindArg("-hogdir")))
-               PHYSFS_addToSearchPath(Args[t + 1], 1);
-#ifdef __unix__
-       else if (!FindArg("-nohogdir"))
-               PHYSFS_addToSearchPath(SHAREPATH, 1);
-#endif
-
        if (! cfile_init("descent2.hog"))
                if (! cfile_init("d2demo.hog"))
                        Warning("Could not find a valid hog file (descent2.hog or d2demo.hog)\nPossible locations are:\n"