annotate mde/options.d @ 16:9cb7b9310168

Improvements to Options and Init. Revamped Options with sections and auto saving/loading. Moved some of init's functions outside the module. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 15 Mar 2008 11:56:13 +0000
parents 4608be19ebe2
children 5f90774ea1ef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /** This module handles stored options, currently all except input maps.
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 *
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 * The purpose of having all options centrally controlled is to allow generic handling by the GUI
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
4 * and ease saving and loading of values. The Options class is only really designed around handling
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
5 * small numbers of variables for now.
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
6 */
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
7 module mde.options;
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 import mde.exception;
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10
14
0047b364b6d9 Changed much of the mergetag structure and some functionality. First tests on windows.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 12
diff changeset
11 import mde.mergetag.Reader;
0047b364b6d9 Changed much of the mergetag structure and some functionality. First tests on windows.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 12
diff changeset
12 import mde.mergetag.Writer;
0047b364b6d9 Changed much of the mergetag structure and some functionality. First tests on windows.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 12
diff changeset
13 import mde.mergetag.DataSet;
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
14 import mde.mergetag.exception;
15
4608be19ebe2 Use OS paths (linux only for now), merging multiple paths. Init changes regarding options.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 14
diff changeset
15 import mde.resource.paths;
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
17 import tango.scrapple.text.convert.parseTo : parseTo;
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18 import tango.scrapple.text.convert.parseFrom : parseFrom;
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
19
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20 import tango.util.log.Log : Log, Logger;
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
22 /** Base class for handling options. This class itself will handle no options and should not be
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
23 * instantiated, but sub-classes handle options.
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
24 *
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
25 * Each sub-class provides named variables for maximal-speed reading & writing. Sub-class references
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
26 * are stored by name, and will be available after pre-init has run (DO NOT access before this).
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
27 *
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
28 * The static class keeps track of all Options class instances for global loading and saving.
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
29 *
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
30 * Details: Options sub-classes hold associative arrays of pointers to all option variables, with a
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
31 * char[] id. This list is used for saving, loading and to provide generic GUI options screens. The
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
32 * built-in support in Options is only for bool, int and char[] types (a float type may get added).
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33 */
14
0047b364b6d9 Changed much of the mergetag structure and some functionality. First tests on windows.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 12
diff changeset
34 class Options : IDataSection
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 {
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
36 // No actual options are stored by this class. However, much of the infrastructure is
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
37 // present since it need not be redefined in sub-classes.
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
38
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
39 // The "pointer lists":
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
40 protected bool* [ID] optsBool;
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
41 protected char[]*[ID] optsCharA;
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
42 protected int* [ID] optsInt;
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
44 //BEGIN Mergtag loading/saving code
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 void addTag (char[] tp, ID id, char[] dt) {
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
46 if (tp == "bool") {
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
47 bool** p = id in optsBool;
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
48 if (p !is null) **p = parseTo!(bool) (dt);
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
49 } else if (tp == "char[]") {
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
50 char[]** p = id in optsCharA;
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
51 if (p !is null) **p = parseTo!(char[]) (dt);
15
4608be19ebe2 Use OS paths (linux only for now), merging multiple paths. Init changes regarding options.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 14
diff changeset
52 } else if (tp == "int") {
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
53 int** p = id in optsInt;
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
54 if (p !is null) **p = parseTo!(int) (dt);
12
bff0d802cb7d Implemented some internationalization support.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 11
diff changeset
55 }
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
56 }
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 void writeAll (ItemDelg dlg) {
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
58 foreach (ID id, bool* val; optsBool) dlg ("bool" , id, parseFrom!(bool ) (*val));
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
59 foreach (ID id, char[]* val; optsCharA) dlg ("char[]", id, parseFrom!(char[]) (*val));
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
60 foreach (ID id, int* val; optsInt) dlg ("int" , id, parseFrom!(int ) (*val));
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61 }
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
62 //END Mergtag loading/saving code
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
63
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
64 //BEGIN Static
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
65 // Each individual section
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
66 static OptionsMisc misc;
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
67
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
68 /* Load/save options from file.
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
69 *
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
70 * If the file doesn't exist, no reading is attempted (options are left at default values).
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
71 */
15
4608be19ebe2 Use OS paths (linux only for now), merging multiple paths. Init changes regarding options.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 14
diff changeset
72 private static const fileName = "options";
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
73 static void load () {
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
74 // Create all uncreated sections now, so that if we return early they are still created.
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
75 if (misc is null) misc = new OptionsMisc;
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
76
14
0047b364b6d9 Changed much of the mergetag structure and some functionality. First tests on windows.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 12
diff changeset
77 // Check it exists (if not it should still be created on exit).
0047b364b6d9 Changed much of the mergetag structure and some functionality. First tests on windows.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 12
diff changeset
78 // Don't bother checking it's not a folder, because it could still be a block or something.
15
4608be19ebe2 Use OS paths (linux only for now), merging multiple paths. Init changes regarding options.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 14
diff changeset
79 if (!confDir.exists (fileName)) return;
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
80
14
0047b364b6d9 Changed much of the mergetag structure and some functionality. First tests on windows.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 12
diff changeset
81 IReader reader;
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82 try {
15
4608be19ebe2 Use OS paths (linux only for now), merging multiple paths. Init changes regarding options.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 14
diff changeset
83 reader = confDir.makeMTReader (fileName, PRIORITY.LOW_HIGH);
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
84 reader.dataSecCreator = delegate IDataSection(ID id) {
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
85 /* Recognise each defined section, and return null for unrecognised sections. */
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
86
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
87 if (id == cast(ID) "misc") return misc;
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
88 else return null;
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
89 };
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
90 reader.read;
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
91 } catch (MTException e) {
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92 logger.error ("Mergetag exception occurred:");
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
93 logger.error (e.msg);
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
94 throw new optionsLoadException ("Loading aborted: mergetag exception");
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
95 }
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
96
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
97 if (misc is null) throw new optionsLoadException ("Loading failed: section \"misc\" not found");
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
98 }
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
99 static void save () {
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
100 DataSet ds = new DataSet();
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
101 ds.sec[cast(ID) "misc"] = misc;
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
102
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
103 IWriter writer;
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
104 try {
15
4608be19ebe2 Use OS paths (linux only for now), merging multiple paths. Init changes regarding options.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 14
diff changeset
105 writer = confDir.makeMTWriter (fileName, ds);
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
106 writer.write();
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
107 } catch (MTException e) {
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
108 logger.error ("Mergetag exception occurred; saving aborted:");
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
109 logger.error (e.msg);
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
110 //FIXME: currently nothing done besides logging the error
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
111 }
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
112 }
12
bff0d802cb7d Implemented some internationalization support.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 11
diff changeset
113
bff0d802cb7d Implemented some internationalization support.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 11
diff changeset
114 private static Logger logger;
bff0d802cb7d Implemented some internationalization support.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 11
diff changeset
115 static this() {
bff0d802cb7d Implemented some internationalization support.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 11
diff changeset
116 logger = Log.getLogger ("mde.options");
bff0d802cb7d Implemented some internationalization support.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 11
diff changeset
117 }
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
118 //END Static
11
b940f267419e Options class created & changes to mergetag exception messages.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
119 }
16
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
120
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
121 /** A home for all miscellaneous options, at least for now. */
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
122 class OptionsMisc : Options {
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
123 bool useThreads; // set 0 to disable threading
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
124 char[] L10n; // locale, e.g. en-GB
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
125 int logLevel; // tango logger level
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
126
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
127 this () {
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
128 optsBool = ["useThreads":&useThreads];
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
129 optsCharA = ["L10n":&L10n];
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
130 optsInt = ["logLevel":&logLevel];
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
131 }
9cb7b9310168 Improvements to Options and Init.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 15
diff changeset
132 }