annotate mde/setup/Init.d @ 63:66d555da083e

Moved many modules/packages to better reflect usage.
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 27 Jun 2008 18:35:33 +0100
parents mde/scheduler/Init.d@f9f5e04f20b2
children cc3763817b8a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /* LICENSE BLOCK
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 Part of mde: a Modular D game-oriented Engine
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
4
26
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
5 This program is free software: you can redistribute it and/or modify it under the terms
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
6 of the GNU General Public License as published by the Free Software Foundation, either
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
7 version 2 of the License, or (at your option) any later version.
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
11 See the GNU General Public License for more details.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
12
26
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
13 You should have received a copy of the GNU General Public License
611f7b9063c6 Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 25
diff changeset
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
15
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16 /**************************************************************************************************
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
17 * Initialisation setup and exit cleanup module.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18 *
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
19 * This module provides an infrastructure for handling much of the initialisation and
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20 * deinitialisation of the program. It does not, however, provide much of the (de)initialisation
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21 * code; with the exception of that for the logger.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
22 *************************************************************************************************/
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 56
diff changeset
23 module mde.setup.Init;
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 56
diff changeset
25 import mde.setup.init2; // This module is responsible for setting up some init functions.
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 56
diff changeset
26 import mde.setup.initFunctions;
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 56
diff changeset
27 import mde.setup.exception;
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 56
diff changeset
29 import mde.lookup.Options;
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 56
diff changeset
30 import paths = mde.setup.paths;
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31 import mde.exception;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33 // tango imports
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34 import tango.core.Thread;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 import tango.core.Exception;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 import tango.stdc.stringz : fromStringz;
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
37 import tango.io.Console; // for printing command-line usage
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
38 import TimeStamp = tango.text.convert.TimeStamp, tango.time.WallClock; // output date in log file
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
39 import tango.util.Arguments;
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 import tango.util.log.Log : Log, Logger;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 import tango.util.log.ConsoleAppender : ConsoleAppender;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
42
25
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
43 //version = SwitchAppender;
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44 version (SwitchAppender) { // My own variation, currently just a test
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 import tango.util.log.SwitchingFileAppender : SwitchingFileAppender;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 } else {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
47 import tango.util.log.RollingFileAppender : RollingFileAppender;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
48 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
50 // Derelict imports
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
51 import derelict.opengl.gl;
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
52 import derelict.sdl.sdl;
56
f9f5e04f20b2 Bit of polish for a public release, including some more documentation.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 55
diff changeset
53 //import derelict.sdl.image; Was to be used... for now isn't
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 29
diff changeset
54 import derelict.freetype.ft;
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
55 import derelict.util.exception;
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
56
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 /**
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
58 * Static CTOR
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
59 *
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
60 * This should handle a minimal amount of functionality where useful. For instance, configuring the
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61 * logger here and not in Init allows unittests to use the logger.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62 */
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
63 static this()
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
64 {
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
65 Logger root = Log.getRootLogger();
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
66 // Set the level here, but set it again once options have been loaded:
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
67 debug root.setLevel(root.Level.Trace);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
68 else root.setLevel(root.Level.Info);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
69 // Temporarily log to the console (until we've found paths and loaded options):
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
70 root.addAppender(new ConsoleAppender);
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
71 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
72 static ~this()
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
73 {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
74 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
75
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
76 /**
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77 * Init class
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
78 *
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
79 * A scope class created at beginning of the program and destroyed at the end; thus the CTOR
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
80 * handles program initialisation and the DTOR handles program cleanup.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
81 */
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82 scope class Init
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83 {
25
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
84 //private bool failure = false; // set true on failure during init, so that clean
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
85 private static Logger logger;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
86 static this() {
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 56
diff changeset
87 logger = Log.getLogger ("mde.setup.Init");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
88 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
89
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
90 /** this() − initialisation
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
91 *
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92 * Runs general initialisation code, in a threaded manner where this isn't difficult.
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
93 * If any init fails, cleanup is still handled by ~this().
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
94 *
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
95 * Init order: 1. Pre-init (loads components needed by most init functions). 2. Dynamic library
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
96 * loading (load any dynamic libraries first, so that if loading fails nothing else need be
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
97 * done). 3. Init functions (threaded functions handling the rest of initialisation).
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
98 */
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
99 /* In a single-threaded function this could be done with:
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
100 * scope(failure) cleanup;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
101 * This won't work with a threaded init function since any threads completing succesfully will
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
102 * not clean-up, and a fixed list of clean-up functions cannot be used since clean-up functions
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
103 * must not run where the initialisation functions have failed.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
104 * Hence a list of clean-up functions is built similarly to scope(failure) --- see addCleanupFct.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
105 */
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
106 this(char[][] cmdArgs)
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
107 {
24
32eff0e01c05 Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 21
diff changeset
108 debug logger.trace ("Init: starting");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
109
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
110 //BEGIN Pre-init (stage init0)
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
111 //FIXME: warn on invalid arguments, including base-path on non-Windows
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
112 // But Arguments doesn't support this (in tango 0.99.6 and in r3563).
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
113 Arguments args;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
114 try {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
115 args = new Arguments();
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
116 args.define("base-path").parameters(1);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
117 args.define("data-path").parameters(1,-1);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
118 args.define("conf-path").parameters(1,-1);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
119 args.define("paths");
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
120 args.define("q").aliases(["quick-exit"]);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
121 args.define("help").aliases(["h"]);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
122 args.parse(cmdArgs);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
123 if (args.contains("help")) // lazy way to print help
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
124 throw new InitException ("Help requested"); // and stop
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
125 } catch (Exception e) {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
126 printUsage(cmdArgs[0]);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
127 throw new InitException ("Parsing arguments failed: "~e.msg);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
128 }
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
129
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
130 // Find/create paths:
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
131 try {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
132 if (args.contains("data-path"))
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
133 paths.extraDataPath = args["data-path"];
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
134 if (args.contains("conf-path"))
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
135 paths.extraConfPath = args["conf-path"];
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
136 if (args.contains("base-path"))
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
137 paths.resolvePaths (args["base-path"]);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
138 else
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
139 paths.resolvePaths();
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
140 } catch (Exception e) {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
141 throw new InitException ("Resolving paths failed: " ~ e.msg);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
142 }
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
143 if (args.contains("paths")) {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
144 paths.mdeDirectory.printPaths;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
145 throw new InitException ("Paths requested"); // lazy way to stop
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
146 }
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
147 debug logger.trace ("Init: resolved paths successfully");
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
148
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
149 /* Load options now. Don't load in a thread since:
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
150 * Loading should be fast & most work is probably disk access
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
151 * It enables logging to be controlled by options
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
152 * It's a really good idea to let the options apply to all other loading */
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
153 try {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
154 Options.load();
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
155 } catch (optionsLoadException e) {
25
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
156 throw new InitException ("Loading options failed: " ~ e.msg);
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
157 }
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
158 debug logger.trace ("Init: loaded options successfully");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
159
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
160 // Set up the logger:
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
161 Logger root;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
162 try {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
163 enum LOG {
55
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
164 LEVEL = 0xF, // mask for log level
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
165 CONSOLE = 0x1000, // log to console?
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
166 ROLLFILE= 0x2000 // use Rolling/Switching File Appender?
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
167 }
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
168
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
169 // Where logging is done to is determined at compile-time, currently just via static ifs.
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
170 root = Log.getRootLogger();
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
171 root.clearAppenders; // we may no longer want to log to the console
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
172
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
173 // Now re-set the logging level, using the value from the config file:
55
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 53
diff changeset
174 root.setLevel (cast(Log.Level) (miscOpts.logOptions & LOG.LEVEL), true);
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
175
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
176 // Log to a file (first appender so root seperator messages don't show on console):
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
177 if (miscOpts.logOptions & LOG.ROLLFILE) {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
178 version (SwitchAppender) {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
179 root.addAppender (new SwitchingFileAppender (paths.logDir~"/log-.txt", 5));
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
180 } else {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
181 // Use 2 log files with a maximum size of 1 MB:
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
182 root.addAppender (new RollingFileAppender (paths.logDir~"/log-.txt", 2, 1024*1024));
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
183 root.info (""); // some kind of separation between runs
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
184 root.info ("");
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
185 }
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
186 } else if (!(miscOpts.logOptions & LOG.CONSOLE)) {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
187 // make sure at least one logger is enabled
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
188 Options.setInt ("misc", "logOptions", miscOpts.logOptions | LOG.CONSOLE);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
189 }
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
190 if (miscOpts.logOptions & LOG.CONSOLE) { // Log to the console
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
191 root.addAppender(new ConsoleAppender);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
192 }
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
193 logger.info ("Starting mde [no version] on " ~ TimeStamp.toString(WallClock.now));
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
194 } catch (Exception e) {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
195 // Presumably it was only adding a file appender which failed; set up a new console
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
196 // logger and if that fails let the exception kill the program.
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
197 root.clearAppenders;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
198 root.addAppender (new ConsoleAppender);
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
199 logger.warn ("Exception while setting up the logger; logging to the console instead.");
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
200 }
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
201
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
202 // a debugging option:
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
203 imde.run = !args.contains("q") && !miscOpts.exitImmediately;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
204 debug logger.trace ("Init: applied pre-init options");
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
205
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
206 //BEGIN Load dynamic libraries
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
207 /* Can be done by init functions but much neater to do here.
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
208 * Also means that init functions aren't run if a library fails to load. */
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
209 try {
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
210 DerelictSDL.load();
56
f9f5e04f20b2 Bit of polish for a public release, including some more documentation.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 55
diff changeset
211 // SDLImage was going to be used... for now it isn't because of gl texturing problems
f9f5e04f20b2 Bit of polish for a public release, including some more documentation.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 55
diff changeset
212 //DerelictSDLImage.load();
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
213 DerelictGL.load();
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 29
diff changeset
214 DerelictFT.load();
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
215 } catch (DerelictException de) {
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
216 logger.fatal ("Loading dynamic library failed:");
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
217 logger.fatal (de.msg);
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
218
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
219 throw new InitException ("Loading dynamic libraries failed (see above).");
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
220 }
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
221 debug logger.trace ("Init: dynamic libraries loaded");
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
222 //END Load dynamic libraries
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
223 //END Pre-init
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
224
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
225
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
226 //BEGIN Init (stages init2, init4)
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
227 /* Call init functions.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
228 *
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
229 * Current method is to try using threads, and on failure assume no threads were actually
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
230 * created and run functions in a non-threaded manner. */
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
231
21
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
232 try {
25
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
233 if (runStageThreaded (init)) runStageForward (init);
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
234 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
235 catch (InitStageException) { // This init stage failed.
25
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
236 // FIXME: check DTOR still runs
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
237 throw new InitException ("An init function failed (see above message(s))");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
238 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
239 //END Init
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
240
24
32eff0e01c05 Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 21
diff changeset
241 debug logger.trace ("Init: done");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
242 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
243
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
244 /** DTOR - runs cleanup functions. */
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
245 ~this()
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
246 {
24
32eff0e01c05 Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 21
diff changeset
247 debug logger.trace ("Cleanup: starting");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
248
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
249 Options.save(); // save options... do so here for now
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
250
25
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
251 // General cleanup:
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
252 try {
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
253 if (runStageThreaded (cleanup)) runStageReverse (cleanup);
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
254 }
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
255 catch (InitStageException) {
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
256 // Nothing else to do but report:
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
257 logger.error ("One or more cleanup functions failed!");
2c28ee04a4ed Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 24
diff changeset
258 }
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
259
24
32eff0e01c05 Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 21
diff changeset
260 debug logger.trace ("Cleanup: done");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
261 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
262
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
263
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
264 //BEGIN runStage...
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
265 private static {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
266 /* The following three functions, runStage*, each run all functions in a stage in some order,
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
267 * catching any exceptions thrown by the functions (although this isn't guaranteed for threads),
21
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
268 * and throw an InitStageException on initFailure. */
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
269
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
270 const LOG_IF_MSG = "Init function ";
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
271 const LOG_CF_MSG = "Cleanup function ";
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
272 const LOG_F_START = " - running";
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
273 const LOG_F_END = " - completed";
45
0fd51d2c6c8a Several changes to resising windows and layout widgets. This commit still has some bugs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 43
diff changeset
274 const LOG_F_BAD = " - failed";
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
275 const LOG_F_FAIL = " - failed: ";
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
276 /* Runs all functions consecutively, first-to-last.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
277 * If any function fails, halts immediately. */
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
278 void runStageForward (InitStage s) {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
279 foreach (func; s.funcs) {
21
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
280 if (initFailure) break;
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
281 try {
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
282 debug logger.trace (LOG_IF_MSG ~ func.name ~ LOG_F_START);
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
283 func.func();
45
0fd51d2c6c8a Several changes to resising windows and layout widgets. This commit still has some bugs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 43
diff changeset
284 debug logger.trace (LOG_IF_MSG ~ func.name ~ (initFailure ? LOG_F_BAD : LOG_F_END));
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
285 } catch (Exception e) {
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
286 logger.fatal (LOG_IF_MSG ~ func.name ~ LOG_F_FAIL ~
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
287 ((e.msg is null || e.msg == "") ? "(no failure message)" : e.msg) );
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
288
21
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
289 setInitFailure();
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
290 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
291 }
21
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
292
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
293 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here.
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
294 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
295 /* Runs all functions consecutively, last-to-first.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
296 * If any function fails, continue until all have been run. */
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
297 void runStageReverse (InitStage s) {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
298 foreach_reverse (func; s.funcs) {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
299 try {
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
300 debug logger.trace (LOG_CF_MSG ~ func.name ~ LOG_F_START);
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
301 func.func();
45
0fd51d2c6c8a Several changes to resising windows and layout widgets. This commit still has some bugs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 43
diff changeset
302 debug logger.trace (LOG_CF_MSG ~ func.name ~ (initFailure ? LOG_F_BAD : LOG_F_END));
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
303 } catch (Exception e) {
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
304 logger.fatal (LOG_CF_MSG ~ func.name ~ LOG_F_FAIL ~
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
305 ((e.msg is null || e.msg == "") ? "(no failure message)" : e.msg) );
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
306
21
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
307 setInitFailure();
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
308 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
309 }
21
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
310 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here.
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
311 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
312 /* Tries running functions in a threaded way. Returns false if successful, true if not but
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
313 * functions should be run without threads. */
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
314 bool runStageThreaded (InitStage s) {
24
32eff0e01c05 Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 21
diff changeset
315 if (!miscOpts.useThreads) return true; // Use unthreaded route instead
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
316
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
317 ThreadGroup tg;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
318 try { // creating/starting threads could fail
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
319 tg = new ThreadGroup;
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
320 foreach (func; s.funcs) { // Start all threads
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
321 debug logger.trace (LOG_IF_MSG ~ func.name ~ LOG_F_START);
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
322 tg.create(func.func);
45
0fd51d2c6c8a Several changes to resising windows and layout widgets. This commit still has some bugs.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 43
diff changeset
323 debug logger.trace (LOG_IF_MSG ~ func.name ~ (initFailure ? LOG_F_BAD : LOG_F_END));
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
324 }
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
325 } catch (ThreadException e) { // Problem with threading; try without threads
24
32eff0e01c05 Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 21
diff changeset
326 logger.error ("Caught ThreadException while trying to create threads:");
32eff0e01c05 Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 21
diff changeset
327 logger.error (e.msg);
29
f985c28c0ec9 A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 28
diff changeset
328 logger.info ("Will disable threads and continue, assuming no threads were created.");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
329
24
32eff0e01c05 Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 21
diff changeset
330 Options.setBool("misc", "useThreads", false); // Disable threads entirely
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
331 return true; // Try again without threads
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
332 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
333
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
334 /* Wait for all threads to complete.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
335 *
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
336 * If something went wrong, we still need to do this before cleaning up.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
337 */
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
338 foreach (t; tg) {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
339 try {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
340 t.join (true);
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
341 } catch (Exception e) {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
342 // Relying on catching exceptions thrown by other threads is a bad idea.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
343 // Hence all threads should catch their own exceptions and return safely.
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
344
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
345 logger.fatal ("Unhandled exception from Init function:");
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
346 logger.fatal (e.msg);
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
347
21
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
348 setInitFailure (); // abort (but join other threads first)
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
349 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
350 }
21
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
351
a60cbb7359dd Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents: 20
diff changeset
352 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here.
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
353 return false; // Done successfully
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
354 }
53
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
355 //END runStage...
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
356
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
357 void printUsage (char[] progName) {
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
358 Cout ("mde [no version]").newline;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
359 Cout ("Usage:").newline;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
360 Cout (progName ~ ` [options]`).newline;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
361 version(Windows)
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
362 Cout (
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
363 ` --base-path path Use path as the base (install) path (Windows only). It
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
364 should contain the "data" directory.`).newline;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
365 Cout (
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
366 ` --data-path path(s) Add path(s) as a potential location for data files.
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
367 First path argument becomes the preffered location to
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
368 load data files from.
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
369 --conf-path path(s) Add path(s) as a potential location for config files.
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
370 Configuration in the first path given take highest
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
371 priority.
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
372 --paths Print all paths found and exit.
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
373 --quick-exit, -q Exit immediately, without entering main loop.
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
374 --help, -h Print this message.`).newline;
f000d6cd0f74 Changes to paths, command line arguments and font LCD rendering.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 45
diff changeset
375 }
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
376 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
377
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
378 debug (mdeUnitTest) unittest {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
379 /* Fake init and cleanup. Use unittest-specific init and cleanup InitStages to avoid
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
380 * messing other init/cleanup up. */
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
381 static InitStage initUT, cleanupUT;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
382
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
383 static bool initialised = false;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
384 static void cleanupFunc1 () {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
385 initialised = false;
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
386 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
387 static void cleanupFunc2 () {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
388 assert (initialised == true);
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
389 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
390
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
391 static void initFunc () {
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
392 initialised = true;
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 29
diff changeset
393 cleanupUT.addFunc (&cleanupFunc1, "UT cleanup 1");
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 29
diff changeset
394 cleanupUT.addFunc (&cleanupFunc2, "UT cleanup 2");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
395 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
396
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 29
diff changeset
397 initUT.addFunc (&initFunc, "UT init");
20
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
398
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
399 runStageForward (initUT);
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
400 assert (initialised);
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
401
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
402 runStageReverse (cleanupUT);
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
403 assert (!initialised);
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
404
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
405 logger.info ("Unittest complete.");
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
406 }
838577503598 Reworked much of Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
407 }