diff mde/resource/paths.d @ 53:f000d6cd0f74

Changes to paths, command line arguments and font LCD rendering. Use "./" instead of "" as default install dir on windows. Implemented a command-line argument parser. Changes to LCD filter/render-mode option handling after testing what actually happens. Changed some FontTexture messages and internals.
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 05 Jun 2008 17:16:52 +0100
parents e0839643ff52
children
line wrap: on
line diff
--- a/mde/resource/paths.d	Mon Jun 02 14:34:24 2008 +0100
+++ b/mde/resource/paths.d	Thu Jun 05 17:16:52 2008 +0100
@@ -39,8 +39,7 @@
 
 import tango.io.Console;
 import tango.io.FilePath;
-import tango.stdc.stdlib;
-import tango.stdc.stringz;
+import tango.sys.Environment;
 //import tango.scrapple.sys.win32.Registry;     // Trouble getting this to work
 
 /** Order to read files in.
@@ -115,6 +114,15 @@
         return false;
     }
     
+    /// Print all paths found.
+    static void printPaths () {
+        Cout ("Data paths found:");
+        dataDir.coutPaths;
+        Cout ("\nConf paths found:");
+        confDir.coutPaths;
+        Cout ("\nLog file directory:\n\t")(logDir).newline;
+    }
+    
 private:
     PathView[] getFiles (char[] filename, PRIORITY readOrder)
     in {
@@ -165,6 +173,14 @@
         return false;
     }
     
+    void coutPaths () {
+        if (pathsLen) {
+            for (size_t i = 0; i < pathsLen; ++i)
+                Cout ("\n\t" ~ paths[i]);
+        } else
+            Cout ("[none]");
+    }
+    
     // Use a static array to store all possible paths with separate length counters.
     // Lowest priority paths are first.
     char[][MAX_PATHS] paths;
@@ -185,49 +201,56 @@
 * Note: the logger cannot be used yet, so only output is exception messages. */
 // FIXME: use tango/sys/Environment.d
 version (linux) {
-    void resolvePaths () {
+    // base-path not used on posix
+    void resolvePaths (char[] = null) {
         // Home directory:
-        char[] HOME = fromStringz (getenv (toStringz ("HOME")));
+        char[] HOME = Environment.get("HOME", ".");
         
         // Base paths:
         // Static data (must exist):
-        PathView staticPath = findPath (false, "/usr/share/games/mde", "/usr/local/share/games/mde", "data");
+        PathView staticPath =
+                findPath (false, "/usr/share/games/mde", "/usr/local/share/games/mde", "data");
         // Config (can just use defaults if necessary, so long as we can save afterwards):
         PathView userPath = findPath (true, HOME~"/.config/mde", HOME~"/.mde");
         
         // Static data paths:
         dataDir.addPath (staticPath.toString);      // we know this is valid anyway
         dataDir.tryPath (userPath.toString ~ DATA);
+        if (extraDataPath) dataDir.tryPath (extraDataPath);
         if (!dataDir.pathsLen) throw new mdeException ("Fatal: no data path found!");
         
         // Configuration paths:
         confDir.tryPath (staticPath.toString ~ CONF);
         confDir.tryPath ("/etc/mde");
         confDir.tryPath (userPath.toString ~ CONF, true);
+        if (extraConfPath) confDir.tryPath (extraConfPath);
         if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!");
         
         // Logging path:
         logDir = userPath.toString;
     }
 } else version (Windows) {
-    void resolvePaths () {
+    void resolvePaths (char[] base = "./") {
         //FIXME: Get path from registry
         //FIXME: Get user path (Docs&Settings/USER/Local Settings/Application data/mde)
+        //http://www.dsource.org/projects/tango/forums/topic/187
         
         // Base paths:
-        PathView installPath = findPath (false, "");;
-        PathView userPath = findPath (true, "user");   // FIXME: see above
-        PathView staticPath = findPath (false, "data");
+        PathView installPath = findPath (false, base);
+        PathView staticPath = findPath (false, installPath.append("data").toString);
+        PathView userPath = findPath (true, installPath.append("user").toString);   // FIXME: see above
         
         // Static data paths:
         dataDir.addPath (staticPath.toString);   // we know this is valid anyway
         dataDir.tryPath (userPath.toString ~ DATA);
+        if (extraDataPath) dataDir.tryPath (extraDataPath);
         if (!dataDir.pathsLen) throw new mdeException ("Fatal: no data path found!");
         
         // Configuration paths:
         confDir.tryPath (staticPath.toString ~ CONF);
-        confDir.tryPath ("conf");
+        confDir.tryPath (installPath.append("user").toString);
         confDir.tryPath (userPath.toString ~ CONF, true);
+        if (extraConfPath) confDir.tryPath (extraConfPath);
         if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!");
         
         // Logging path:
@@ -237,86 +260,98 @@
     static assert (false, "Platform is not linux or Windows: no support for paths on this platform yet!");
 }
 
-private:
-// The maximum number of paths for any one "directory".
-// There are NO CHECKS that this is not exceeded.
-const MAX_PATHS = 3;
+/// For command line args: these paths are added if non-null, with highest priority.
+char[] extraDataPath, extraConfPath;
 
-/* Try each path in succession, returning the first to exist and be a folder.
-* If none are valid and create is true, will try creating each in turn.
-* If still none are valid, throws. */
-PathView findPath (bool create, char[][] paths ...) {
-    foreach (path; paths) {
-        PathView pv = new FilePath (path);
-        if (pv.exists && pv.isFolder) return pv;    // got a valid path
-    }
-    if (create) {   // try to create a folder, using each path in turn until succesful
-        foreach (path; paths) {
-            FilePath fp = new FilePath (path);
-            try {
-                return fp.create;
-            }
-            catch (Exception e) {}
-        }
-    }
-    // no valid path...
-    char[] msg = "Unable to find"~(create ? " or create" : "")~" a required path! The following were tried:";
-    foreach (path; paths) msg ~= "  \"" ~ path ~ '\"';
-    throw new mdeException (msg);
-}
-//END Path resolution
-
-/** A special adapter for reading from multiple mergetag files with the same relative path to an
-* mdeDirectory simultaneously.
-*/
-class mdeReader : IReader
-{
-    private this (PathView[] files, DataSet ds, bool rdHeader)
-    in {
-        assert (files !is null, "mdeReader.this: files is null");
-    } body {
-        if (ds is null) ds = new DataSet;
-        
-        foreach (file; files) {
-            IReader r = makeReader (file, ds, rdHeader);
-            
-            readers[readersLen++] = r;
+private {
+    class PathException : mdeException {
+        this(char[] msg) {
+            super (msg);
         }
     }
     
-    DataSet dataset () {                /// Get the DataSet
-        return readers[0].dataset;      // all readers share the same dataset
+// The maximum number of paths for any one "directory".
+// There are NO CHECKS that this is not exceeded.
+    const MAX_PATHS = 4;
+
+    /* Try each path in succession, returning the first to exist and be a folder.
+     * If none are valid and create is true, will try creating each in turn.
+     * If still none are valid, throws. */
+    PathView findPath (bool create, char[][] paths ...) {
+        FilePath[] fps;
+        fps.length = paths.length;
+        foreach (i,path; paths) {
+            FilePath pv = new FilePath (path);
+            if (pv.exists && pv.isFolder) return pv;    // got a valid path
+            fps[i] = pv;
+        }
+        if (create) {   // try to create a folder, using each path in turn until succesful
+            foreach (fp; fps) {
+                try {
+                    return fp.create;
+                }
+                catch (Exception e) {}
+            }
+        }
+    // no valid path...
+        char[] msg = "Unable to find"~(create ? " or create" : "")~" a required path! The following were tried:";
+        foreach (path; paths) msg ~= "  \"" ~ path ~ '\"';
+        throw new PathException (msg);
     }
-    void dataset (DataSet ds) {         /// Set the DataSet
-        for (uint i = 0; i < readersLen; ++i) readers[i].dataset (ds);
-    }
+//END Path resolution
+
+    /** A special adapter for reading from multiple mergetag files with the same relative path to an
+     * mdeDirectory simultaneously.
+     */
+    class mdeReader : IReader
+    {
+        private this (PathView[] files, DataSet ds, bool rdHeader)
+        in {
+            assert (files !is null, "mdeReader.this: files is null");
+        } body {
+            if (ds is null) ds = new DataSet;
+        
+            foreach (file; files) {
+                IReader r = makeReader (file, ds, rdHeader);
+            
+                readers[readersLen++] = r;
+            }
+        }
     
-    void dataSecCreator (IDataSection delegate (ID) dsC) {  /// Set the dataSecCreator
-        for (uint i = 0; i < readersLen; ++i) readers[i].dataSecCreator = dsC;
-    }
+        DataSet dataset () {                /// Get the DataSet
+            return readers[0].dataset;      // all readers share the same dataset
+        }
+        void dataset (DataSet ds) {         /// Set the DataSet
+            for (uint i = 0; i < readersLen; ++i) readers[i].dataset (ds);
+        }
+    
+        void dataSecCreator (IDataSection delegate (ID) dsC) {  /// Set the dataSecCreator
+            for (uint i = 0; i < readersLen; ++i) readers[i].dataSecCreator = dsC;
+        }
     
     /** Get identifiers for all sections.
-    *
-    * Note: the identifiers from all sections in all files are just strung together, starting with
-    * the highest-priority file. */
+     *
+     * Note: the identifiers from all sections in all files are just strung together, starting with
+     * the highest-priority file. */
     ID[] getSectionNames () {
         ID[] names;
         for (int i = readersLen-1; i >= 0; --i) names ~= readers[i].getSectionNames;
         return names;
-    }
-    void read () {                      /// Commence reading
-        for (uint i = 0; i < readersLen; ++i) readers[i].read();
-    }
-    void read (ID[] secSet) {           /// ditto
-        for (uint i = 0; i < readersLen; ++i) readers[i].read(secSet);
+            }
+            void read () {                      /// Commence reading
+                for (uint i = 0; i < readersLen; ++i) readers[i].read();
+            }
+            void read (ID[] secSet) {           /// ditto
+                for (uint i = 0; i < readersLen; ++i) readers[i].read(secSet);
+            }
+            void read (View!(ID) secSet) {      /// ditto
+                for (uint i = 0; i < readersLen; ++i) readers[i].read(secSet);
+            }
+        
+        private:
+            IReader[MAX_PATHS] readers;
+            ubyte readersLen = 0;
+    
+            PRIORITY rdOrder;
     }
-    void read (View!(ID) secSet) {      /// ditto
-        for (uint i = 0; i < readersLen; ++i) readers[i].read(secSet);
-    }
-        
-    private:
-    IReader[MAX_PATHS] readers;
-    ubyte readersLen = 0;
-    
-    PRIORITY rdOrder;
-}
+}
\ No newline at end of file