-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEraSettings.pas
136 lines (102 loc) · 3.45 KB
/
EraSettings.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
unit EraSettings;
(*
Description: Settings management
Author: Alexander Shostak aka Berserker
*)
(***) interface (***)
uses
Math,
SysUtils,
Debug,
Ini,
Log;
const
(* Globally used common directories and files *)
DEBUG_DIR = 'Debug\Era';
LOG_FILE_NAME = 'log.txt';
(* Game settings *)
DEFAULT_GAME_SETTINGS_FILE = 'default heroes3.ini';
GAME_SETTINGS_FILE = 'heroes3.ini';
ERA_SETTINGS_SECTION = 'Era';
GAME_SETTINGS_SECTION = 'Settings';
type
TOption = record
Value: string;
Dummy: integer; // This is field is necessary to prevent Delphi 2009 bug, when it tries to optimize 4 byte structures
// Memory corruption occurs quite often, while 8 byte structure will be passed as var-parameter and is thus safe
function Str (const Default: string = ''): string;
function Int (Default: integer = 0): integer;
function Bool (Default: boolean = false): boolean;
end;
(* Load Era settings *)
procedure LoadSettings (const GameDir: string);
(* Returns Era settings option by name *)
function GetOpt (const OptionName: string): TOption;
function IsDebug: boolean;
(* Returns Era debug option by name. The result is influenced by 'Debug' and 'Debug.Evenything' options *)
function GetDebugBoolOpt (const OptionName: string; Default: boolean = false): boolean;
(***) implementation (***)
var
DebugOpt: boolean;
DebugEverythingOpt: boolean;
GameSettingsFilePath: string;
function TOption.Str (const Default: string = ''): string;
begin
result := Self.Value;
if Self.Value = '' then begin
result := Default;
end;
end;
function TOption.Int (Default: integer = 0): integer;
begin
if (Self.Value = '') or not SysUtils.TryStrToInt(Self.Value, result) then begin
result := Default;
end;
end;
function TOption.Bool (Default: boolean = false): boolean;
begin
result := Default;
if Self.Value <> '' then begin
result := Self.Value <> '0';
end;
end;
function GetOpt (const OptionName: string): TOption;
begin
result.Value := '';
if Ini.ReadStrFromIni(OptionName, ERA_SETTINGS_SECTION, GameSettingsFilePath, result.Value) then begin
result.Value := SysUtils.Trim(result.Value);
end;
end;
function IsDebug: boolean;
begin
result := DebugOpt;
end;
function GetDebugBoolOpt (const OptionName: string; Default: boolean = false): boolean;
begin
result := DebugOpt and (DebugEverythingOpt or GetOpt(OptionName).Bool(Default));
end;
procedure InstallLogger (Logger: Log.TLogger);
var
LogRec: TLogRec;
begin
{!} Assert(Logger <> nil);
Log.Seek(0);
while Log.Read(LogRec) do begin
Logger.Write(LogRec.EventSource, LogRec.Operation, LogRec.Description);
end;
Log.InstallLogger(Logger, Log.FREE_OLD_LOGGER);
end; // .procedure InstallLogger
procedure LoadSettings (const GameDir: string);
var
DefaultGameSettingsPath: string;
begin
GameSettingsFilePath := GameDir + '\' + GAME_SETTINGS_FILE;
DefaultGameSettingsPath := GameDir + '\' + DEFAULT_GAME_SETTINGS_FILE;
Ini.LoadIni(GameSettingsFilePath);
Ini.LoadIni(DefaultGameSettingsPath);
Ini.MergeIniWithDefault(GameSettingsFilePath, DefaultGameSettingsPath);
DebugOpt := GetOpt('Debug').Bool(true);
DebugEverythingOpt := GetOpt('Debug.Everything').Bool(false);
Debug.AbortOnError := GetDebugBoolOpt('Debug.AbortOnError', false);
end;
end.