diff mde/setup/Init.d @ 137:9f035cd139c6

BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed. Content restrutured a lot: New IContent module, Content module includes more functionality. New ContentLoader module to manage content loading/saving/translation. Translation module moved to content dir and cut down to reflect current usage. File format unchanged except renames: FontOptions -> Font, VideoOptions -> Screen. Font render mode and LCD filter options are now enums. GUI loading needs to create content (and set type for enums), but doesn't save/load value. Some setup of mainSchedule moved to mde.mainLoop. Content callbacks are called on content change now. ContentLists are set up implicitly from content symbols. Not as fast but much easier! Bug-fix in the new MTTagReader. Renamed MT *Reader maker functions to avoid confusion in paths.d. New mde.setup.logger module to allow logger setup before any other module's static this().
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 07 Feb 2009 12:46:03 +0000
parents 4084f07f2c7a
children e785e98d3b78
line wrap: on
line diff
--- a/mde/setup/Init.d	Sun Feb 01 12:36:21 2009 +0000
+++ b/mde/setup/Init.d	Sat Feb 07 12:46:03 2009 +0000
@@ -42,8 +42,10 @@
 
 import mde.setup.InitStage;     // Controls external delegates run by init
 import mde.setup.exception;
+import mde.setup.logger;
 
-import mde.lookup.Options;
+import mde.content.AStringContent;
+import mde.content.ContentLoader;
 import paths = mde.file.paths;
 import mde.exception;           // optionsLoadException
 import imde = mde.imde;
@@ -70,30 +72,31 @@
 import derelict.util.exception;
 
 
-/**************************************************************************************************
+/******************************************************************************
  * Init class
  *
- * A scope class created at beginning of the program and destroyed at the end; thus the CTOR
- * handles program initialisation and the DTOR handles program cleanup.
- *************************************************************************************************/
+ * A scope class created at beginning of the program and destroyed at the end;
+ * thus the CTOR handles program initialisation and the DTOR handles program
+ * cleanup.
+ *****************************************************************************/
 scope class Init
 {
     static this() {
-        // Set up the logger temporarily (until pre-init):
-        Logger root = Log.root;
-        debug root.level(Logger.Trace);
-        else  root.level(Logger.Info);
-        root.add(new AppendConsole);
-        
         logger = Log.getLogger ("mde.setup.Init");
+        exitImmediately = new BoolContent ("MiscOptions.exitImmediately");
+        maxThreads = new IntContent ("MiscOptions.maxThreads");
+        logLevel = new EnumContent ("MiscOptions.logLevel",
+                                    ["Trace", "Info", "Warn", "Error", "Fatal", "None"]);
+        logOutput = new EnumContent ("MiscOptions.logOutput",
+                                     ["none", "console", "file", "both"]);
     }
     
     /** this() − pre-init and init */
     this(char[][] cmdArgs)
     {
-        /******************************************************************************************
+        /**********************************************************************
          * Pre-init - init code written in this module.
-         *****************************************************************************************/
+         *********************************************************************/
         debug logger.trace ("Init: starting pre-init");
         //FIXME: warn on invalid arguments, including base-path on non-Windows
         // But Arguments doesn't support this (in tango 0.99.6 and in r3563).
@@ -142,9 +145,9 @@
         *   It enables logging to be controlled by options
         *   It's a really good idea to let the options apply to all other loading */
         try {
-            Options.load();
-        } catch (optionsLoadException e) {
-            throw new InitException ("Loading options failed: " ~ e.msg);
+            ContentLoader.load();
+        } catch (Exception e) {
+            throw new InitException ("Loading options (content values) failed: " ~ e.msg);
         }
         debug logger.trace ("Init: loaded options successfully");
         
@@ -157,15 +160,15 @@
 	    
             // Now re-set the logging level and add callback to set on change:
             setLogLevel ();
-            miscOpts.logLevel.addCallback (&setLogLevel);
+            logLevel.addCallback (&setLogLevel);
             
-            if (miscOpts.logOutput() & 2) {     // first appender so root seperator messages don't show on console
+            if (logOutput() & 2) {     // first appender so root seperator messages don't show on console
                 // Use 2 log files with a maximum size of 16kiB:
                 root.add (new AppendFiles (paths.logDir~"/log-.txt", 2, 16*1024));
                 root.append (Level.None, ""); // some kind of separation between runs
                 root.append (Level.None, "");
             }
-            if (miscOpts.logOutput() & 1)
+            if (logOutput() & 1)
                 root.add(new AppendConsole);
             logger.info ("Starting mde [no version] on " ~ TimeStamp.toString(WallClock.now));
         } catch (Exception e) {
@@ -177,7 +180,7 @@
         }
         
         // a debugging option:
-        imde.run = !args.contains("q") && !miscOpts.exitImmediately();
+        imde.run = !args.contains("q") && !exitImmediately();
         debug logger.trace ("Init: applied pre-init options");
         
         //BEGIN Load dynamic libraries
@@ -199,9 +202,9 @@
         //END Load dynamic libraries
         
         
-        /******************************************************************************************
+        /**********************************************************************
          * Init − where init code from external modules gets hooked in.
-         *****************************************************************************************/
+         *********************************************************************/
         debug logger.trace ("Init: done pre-init, starting init stages");
         
         // Calculate reverse dependencies of stages:
@@ -221,7 +224,7 @@
         
         runStages!(false);      // cleanup delegates
         
-        Options.save(); // save options... do so here for now
+        ContentLoader.save();	// save options before exiting
         
         debug logger.trace ("Cleanup: done");
     }
@@ -251,7 +254,7 @@
             }
         }
         // Counts number of active threads, and before threads are started is number to use:
-        size_t numWorking = (toRun.size < miscOpts.maxThreads()) ? toRun.size : miscOpts.maxThreads();
+        size_t numWorking = (toRun.size < maxThreads()) ? toRun.size : maxThreads();
         enum STATE {    WORKING = 0,    DONE = 1,       ABORT = 2 }
         STATE doneInit = STATE.WORKING;
         Mutex toRunM = new Mutex;       // synchronization on toRun, numWorking
@@ -377,7 +380,7 @@
         } catch (ThreadException e) {
             logger.error ("Exception while using threads: "~e.msg);
             logger.error ("Disabling threads and attempting to continue.");
-            miscOpts.maxThreads = 1;    // count includes current thread
+            maxThreads = 1;    // count includes current thread
             auto x = new InitStageThread (0);
             x.initThreadFct();                            // try with just this thread
         }       // any other exception will be caught in main() and abort program
@@ -392,10 +395,13 @@
     
     private static {
         Logger logger;
+        BoolContent exitImmediately;
+        IntContent maxThreads;
+        EnumContent logLevel, logOutput;
         
-        // Callback on miscOpts.logLevel
+        // Callback on logLevel
         void setLogLevel (Content = null) {
-            Log.root.level (miscOpts.logOutput() == 0 ? Level.None : cast(Level) miscOpts.logLevel(), true);
+            Log.root.level (logOutput() == 0 ? Level.None : cast(Level) logLevel(), true);
         }
         
         void printUsage (char[] progName) {
@@ -460,8 +466,8 @@
         foreach (key,stage_p; stages)
             foreach (name; stage_p.depends)
                 stages[name].rdepends ~= key;
-        IntContent realMaxThreads = miscOpts.maxThreads;
-        miscOpts.maxThreads = new IntContent ("maxThreads", 4);	// force up to 4 threads for unittest
+        int realMaxThreads = maxThreads();
+        maxThreads = 4;	// force up to 4 threads for unittest
         
         logger.level(Logger.Info);              // hide a lot of trace messages
         logger.info ("You should see some messages about InitStages not run/failing:");
@@ -501,8 +507,7 @@
         assert (stages[toStageName("stg3")].state == cast(StageState)7);        // set by the exception
         
         stages = realInit;      // restore the real init stages
-	delete miscOpts.maxThreads;
-        miscOpts.maxThreads = realMaxThreads;
+        maxThreads = realMaxThreads;
         logger.info ("Unittest complete.");
     }
 }