aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Classes/UCore.pas
blob: 990ee7abaa56b0b21a46f703bcc8c990f9daa337 (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
unit UCore;

interface
uses uPluginDefs, uCoreModule, UHooks, UServices, uModules;
{*********************
  TCore
  Class manages all CoreModules, teh StartUp, teh MainLoop and the shutdown process
  Also it does some Error Handling, and maybe sometime multithreaded Loading ;)
*********************}

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

{$I switches.inc}

type
  TModuleListItem = record
    Module:       TCoreModule; //Instance of the Modules Class
    Info:         TModuleInfo; //ModuleInfo returned by Modules Modulinfo Proc
    NeedsDeInit:  Boolean;     //True if Module was succesful inited
  end;
  
  TCore = class
    private
      //Some Hook Handles. See Plugin SDKs Hooks.txt for Infos
      hLoadingFinished: THandle;
      hMainLoop:        THandle;
      hLoadTextures:    THandle;
      hExitQuery:       THandle;
      hExit:            THandle;
      hDebug:           THandle;
      hError:           THandle;
      sReportError:       THandle;
      sReportDebug:       THandle;

      Modules:          Array [0..High(CORE_MODULES_TO_LOAD)] of TModuleListItem;

      //Function Get all Modules and Creates them
      Function GetModules: Boolean;

      //Loads Core and all Modules
      Function Load: Boolean;

      //Inits Core and all Modules
      Function Init: Boolean;

      //DeInits Core and all Modules
      Function DeInit: Boolean;

      //Load the Core
      Function LoadCore: Boolean;

      //Init the Core
      Function InitCore: Boolean;

      //DeInit the Core
      Function DeInitCore: Boolean;

      //Called one Time per Frame
      Function MainLoop: Boolean;

    public
      Hooks:            THookManager;   //Teh Hook Manager ;)
      Services:         TServiceManager;//The Service Manager

      CurExecuted:      Integer;        //ID of Plugin or Module curently Executed

      Name:             String;         //Name of this Application
      Version:          LongWord;       //Version of this ". For Info Look PluginDefs Functions

      //---------------
      //Main Methods to control the Core:
      //---------------
      Constructor Create(const cName: String; const cVersion: LongWord);

      //Starts Loading and Init Process. Then Runs MainLoop. DeInits on Shutdown
      Procedure Run;

      //--------------
      // Hook and Service Procs:
      //--------------
      Function ShowMessage(wParam, lParam: DWord): integer; //Shows a Message (lParam: PChar Text, wParam: Symbol)
      {Function ShowMessage(wParam, lParam: DWord): integer; //Shows a Message (lParam: PChar Text, wParam: Symbol)
      Function ShowMessage(wParam, lParam: DWord): integer; //Shows a Message (lParam: PChar Text, wParam: Symbol)}
  end;

var
  Core: TCore; 

implementation
{$IFDEF win32}
uses Windows;
{$ENDIF}

//-------------
// Create - Creates Class + Hook and Service Manager
//-------------
Constructor TCore.Create(const cName: String; const cVersion: LongWord);
begin
  Name := cName;
  Version := cVersion;
  CurExecuted := 0;

  Hooks := THookManager.Create(50);
  Services := TServiceManager.Create;
end;

//-------------
//Starts Loading and Init Process. Then Runs MainLoop. DeInits on Shutdown
//-------------
Procedure TCore.Run;
var
  noError: Boolean;
begin
  //Get Modules
  Try
    noError := GetModules;
  Except
    noError := False;
  end;

  //Loading
  if (noError) then
  begin
    Try
      noError := Load;
    Except
      noError := False;
    end;
  end
  else
    Self.ShowMessage(CORE_SM_ERROR, Integer(PChar('')));
end;

//-------------
//Called one Time per Frame
//-------------
Function TCore.MainLoop: Boolean;
begin
  Result := True;

end;

//-------------
//Function Get all Modules and Creates them
//-------------
Function TCore.GetModules: Boolean;
var
  I: Integer;
begin
  For I := 0 to high(Modules) do
  begin
    Modules[I].NeedsDeInit := False;
    Modules[I].Module := CORE_MODULES_TO_LOAD[I].Create;
    Modules[I].Module.Info(@Modules[I].Info);
  end;
end;

//-------------
//Loads Core and all Modules
//-------------
Function TCore.Load: Boolean;
var
  I: Integer;
begin
  Result := LoadCore;

  I := 0;
  While ((Result = True) AND (I <= High(CORE_MODULES_TO_LOAD))) do
  begin
    Result := Modules[I].Module.Load;
    Inc(I);
  end;
end;

//-------------
//Inits Core and all Modules
//-------------
Function TCore.Init: Boolean;
var
  I: Integer;
begin
  Result := InitCore;

  I := 0;
  While ((Result = True) AND (I <= High(CORE_MODULES_TO_LOAD))) do
  begin
    Result := Modules[I].Module.Init;
    Inc(I);
  end;
end;

//-------------
//DeInits Core and all Modules
//-------------
Function TCore.DeInit: Boolean;
var
  I: Integer;
label Continue;
begin
  I := High(CORE_MODULES_TO_LOAD);

  Continue:
  Try
    While (I >= 0) do
    begin
      If (Modules[I].NeedsDeInit) then
        Modules[I].Module.DeInit;

      Dec(I);
    end;
  Except


  end;
  If (I >= 0) then
    GoTo Continue;

  DeInitCore;
end;

//-------------
//Load the Core
//-------------
Function TCore.LoadCore: Boolean;
begin
  hLoadingFinished := Hooks.AddEvent('Core/LoadingFinished');
  hMainLoop        := Hooks.AddEvent('Core/MainLoop');
  hLoadTextures    := Hooks.AddEvent('Core/LoadTextures');
  hExitQuery       := Hooks.AddEvent('Core/ExitQuery');
  hExit            := Hooks.AddEvent('Core/Exit');
  hDebug           := Hooks.AddEvent('Core/NewDebugInfo');
  hError           := Hooks.AddEvent('Core/NewError');
  sReportError     := Services.AddService('Core/ReportError');
  sReportDebug     := Services.AddService('Core/ReportDebug');
end;

//-------------
//Init the Core
//-------------
Function TCore.InitCore: Boolean;
begin
  //Dont Init s.th. atm.
end;

//-------------
//DeInit the Core
//-------------
Function TCore.DeInitCore: Boolean;
begin


  // to-do : write TService-/HookManager.Free and call it here
end;

//-------------
//Shows a MessageDialog (lParam: PChar Text, wParam: Symbol)
//-------------
Function TCore.ShowMessage(wParam, lParam: DWord): integer;
var Params: Cardinal; 
begin
  Result := -1;

  {$IFDEF win32}
  If (ptr(lParam)<>nil) then
  begin
    Params := MB_OK;
    Case wParam of
      CORE_SM_ERROR: Params := Params or MB_ICONERROR;
      CORE_SM_WARNING: Params := Params or MB_ICONWARNING;
      CORE_SM_INFO: Params := Params or MB_ICONINFORMATION;
    end;

    //Anzeigen:
    Result := Messagebox(0, ptr(lParam), PChar(Name), Params);
  end;
  {$ENDIF}

  // to-do : write ShowMessage for other OSes
end;

end.