diff mde/init.d @ 11:b940f267419e

Options class created & changes to mergetag exception messages. Options class created (barebones). Loading/saving from Init. Init no longer runs cleanup functions after initialisation failure. Improved mergetag exception messages & error reporting. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 21 Feb 2008 09:05:33 +0000
parents 4c3575400769
children bff0d802cb7d
line wrap: on
line diff
--- a/mde/init.d	Mon Feb 18 11:54:56 2008 +0000
+++ b/mde/init.d	Thu Feb 21 09:05:33 2008 +0000
@@ -7,6 +7,7 @@
 
 import mde.exception;
 
+import mde.options;
 import mde.events;
 import global = mde.global;
 import mde.input.input;
@@ -22,8 +23,6 @@
 import derelict.sdl.sdl;
 import derelict.util.exception;
 
-private Logger logger;
-
 /**
  * Static CTOR
  *
@@ -39,8 +38,6 @@
         root.setLevel(root.Level.Trace);
         root.addAppender(new ConsoleAppender);
     }
-    
-    logger = Log.getLogger ("mde.init");
 }
 static ~this()
 {
@@ -78,13 +75,13 @@
         * These should each handle a separate area of initialisation such that these functions could
         * be run simultaneously in separate threads. */
         
-        bool initFailure = false;	// Set true if something goes wrong and we need to abort.
         void setFailure () {		// For synchronization, although shouldn't be necessary
             synchronized initFailure = true;
         }
         void delegate() [] initFuncs = [
         delegate void() {
-            logger = Log.getLogger ("mde.init.Init.SDL");
+            Logger logger = Log.getLogger ("mde.init.Init.SDL");
+            
             // Inits SDL and related stuff (joystick).
             try {
                 DerelictSDL.load();
@@ -113,6 +110,19 @@
             
             openJoysticks ();		// after SDL init
             addCleanupFct (&closeJoysticks);
+        },
+        delegate void() {
+            Logger logger = Log.getLogger ("mde.init.Init.Options");
+            
+            try {
+                Options.load();
+            } catch (optionsLoadException e) {
+                logger.fatal ("Loading options failed; message:");
+                logger.fatal (e.msg);
+                setFailure();
+                return;
+            }
+            addCleanupFct (&Options.save);  // not strictly cleanup, but needs to be called somewhere
         }
         ];
         
@@ -157,6 +167,7 @@
             // All cleanup-on-failure must be done here.
             runCleanupFcts();
             
+            logger.fatal ("Init failed");
             // Throw an exception to signal failure and prevent DTOR from also running.
             throw new initException ("Initialisation failed due to above exceptions.");
         }
@@ -167,7 +178,11 @@
     * Currently unthreaded; probably might as well stay that way. */
     ~this()
     {
-        runCleanupFcts();	// if threading, note not all functions can be called simultaeneously
+        if (!initFailure) {
+            logger.info ("Cleaning up...");
+            runCleanupFcts();	// if threading, note not all functions can be called simultaeneously
+            logger.info ("Done!");
+        }
     }
     
     /* Cleanup Functions.
@@ -188,24 +203,29 @@
             foreach_reverse (fct; cleanup) fct();
         }
     }
-}
-
-debug (mdeUnitTest) unittest {
-    /* Fake init and cleanup. This happens before the CTOR runs so the extra Init.runCleanupFcts()
-    * call isn't going to mess things up. The extra function called by runCleanupFcts won't cause
-    * any harm either. */
-    static bool initialised = false;
-    static void cleanup () {	initialised = false;	}
+    
+    /* This is set true by this() if something goes wrong and we need to abort.
+    * If it's true, no cleanup should be done by the DTOR (since the DTOR still runs after an
+    * exception has been thrown). */
+    private bool initFailure = false;
+    
+    debug (mdeUnitTest) unittest {
+        /* Fake init and cleanup. This happens before the CTOR runs so the extra Init.runCleanupFcts()
+        * call isn't going to mess things up. The extra function called by runCleanupFcts won't cause
+        * any harm either. */
+        static bool initialised = false;
+        static void cleanup () {	initialised = false;	}
         
-    static void init () {
-        initialised = true;
-        Init.addCleanupFct (&cleanup);
-    }
+        static void init () {
+            initialised = true;
+            Init.addCleanupFct (&cleanup);
+        }
         
-    init();
-    assert (initialised);
-    Init.runCleanupFcts();
-    assert (!initialised);
+        init();
+        assert (initialised);
+        Init.runCleanupFcts();
+        assert (!initialised);
 
-    logger.info ("Unittest complete.");
+        logger.info ("Unittest complete.");
+    }
 }