Repository navigation
Expand file tree
/
Copy pathUModularSystem.pas
More file actions
556 lines (492 loc) · 14.5 KB
/
Copy pathUModularSystem.pas
File metadata and controls
556 lines (492 loc) · 14.5 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
unit UModularSystem;
{$mode Delphi}{$H+}
interface
uses
Classes, SysUtils, URegistry, SpecializedList;
type
TModuleManager = class;
TModule = class;
TListModule = class;
TAPI = class(TComponent)
end;
TModuleCondition = (mcAll, mcEnabled, mcNotEnabled, mcInstalled, mcNotInstalled,
mcRunning, mcNotRunning);
TModuleConditions = set of TModuleCondition;
TModuleAction = (maStart, maStop, maInstall, maUninstall, maUpgrade, maEnable,
maDisable);
TModuleActions = array of TModuleAction;
{ TModule }
TModule = class(TComponent)
private
FCategory: string;
FEnabled: Boolean;
FReleaseTime: TDateTime;
FRunning: Boolean;
FInstalled: Boolean;
FManager: TModuleManager;
FVersion: string;
FIdentification: string;
FTitle: string;
FLicense: string;
FAuthor: string;
FDependencies: TListString;
FDescription: TListString;
FFileName: string;
FWebSite: string;
FStartUpTime: TDateTime;
procedure SetEnabled(AValue: Boolean);
procedure SetInstalled(AValue: Boolean);
procedure SetManager(AValue: TModuleManager);
procedure SetRunning(AValue: Boolean);
protected
procedure DoStart; virtual;
procedure DoStop; virtual;
procedure DoInstall; virtual;
procedure DoUninstall; virtual;
procedure DoUpgrade; virtual;
public
API: TAPI;
procedure Enable;
procedure Disable;
procedure Start;
procedure Stop;
procedure Restart;
procedure Install;
procedure Uninstall;
procedure Reinstall;
procedure Upgrade;
procedure EnumDependenciesCascade(ModuleList: TListModule;
Conditions: TModuleConditions = [mcAll]);
procedure EnumSuperiorDependenciesCascade(ModuleList: TListModule;
Conditions: TModuleConditions = [mcAll]);
procedure SetInstalledState(Value: Boolean);
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
property Running: Boolean read FRunning write SetRunning;
property Installed: Boolean read FInstalled write SetInstalled;
property Enabled: Boolean read FEnabled write SetEnabled;
property StartUpTime: TDateTime read FStartUpTime;
published
property Identification: string read FIdentification write FIdentification; // Unique system name
property Manager: TModuleManager read FManager write SetManager;
property Version: string read FVersion write FVersion;
property ReleaseTime: TDateTime read FReleaseTime write FReleaseTime;
property Title: string read FTitle write FTitle;
property License: string read FLicense write FLicense;
property Author: string read FAuthor write FAuthor;
property Dependencies: TListString read FDependencies write FDependencies;
property Description: TListString read FDescription write FDescription;
property FileName: string read FFileName write FFileName;
property Category: string read FCategory write FCategory;
property WebSite: string read FWebSite write FWebSite;
// Screenshots, reviews, icon, weak dependencies, ...
end;
{ TListModule }
TListModule = class(TListObject)
private
public
procedure Perform(Actions: array of TModuleAction; Conditions: TModuleConditions = [mcAll]);
function FindByName(Name: string): TModule;
end;
TModuleManagerOption = (moAutoInstallOnRun, moAuto);
TModuleManagerOptions = set of TModuleManagerOption;
{ TModuleManager }
TModuleManager = class(TComponent)
private
FAPI: TAPI;
FOnUpdate: TNotifyEvent;
FUpdateCount: Integer;
FOptions: TModuleManagerOptions;
procedure SetAPI(AValue: TAPI);
procedure DoUpdate(Sender: TObject);
public
Modules: TListModule; // TObjectList<TModule>
function ModuleRunning(Name: string): Boolean;
procedure EnumDependenciesCascade(Module: TModule; ModuleList: TListModule;
Conditions: TModuleConditions = [mcAll]);
procedure EnumSuperiorDependenciesCascade(Module: TModule;
ModuleList: TListModule; Conditions: TModuleConditions = [mcAll]);
procedure RegisterModule(Module: TModule);
procedure UnregisterModule(Module: TModule);
procedure LoadFromRegistry(Context: TRegistryContext);
procedure SaveToRegistry(Context: TRegistryContext);
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property API: TAPI read FAPI write SetAPI;
published
property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
property Options: TModuleManagerOptions read FOptions write FOptions;
end;
procedure Register;
implementation
resourcestring
SModuleNotFound = 'Module "%1:s" not found as dependency for module "%0:s"';
procedure Register;
begin
RegisterComponents('ModularSystem', [TModuleManager, TModule]);
end;
{ TListModule }
procedure TListModule.Perform(Actions: array of TModuleAction;
Conditions: TModuleConditions = [mcAll]);
var
I: Integer;
A: Integer;
begin
try
BeginUpdate;
for I := 0 to Count - 1 do
with TModule(Items[I]) do
if (mcAll in Conditions) or
(Running and (mcRunning in Conditions)) or
(not Running and (mcNotRunning in Conditions)) or
(Installed and (mcInstalled in Conditions)) or
(not Installed and (mcNotInstalled in Conditions)) or
(Enabled and (mcEnabled in Conditions)) or
(not Enabled and (mcNotEnabled in Conditions)) then
for A := 0 to High(Actions) do begin
if Actions[A] = maStart then Start;
if Actions[A] = maStop then Stop;
if Actions[A] = maInstall then Install;
if Actions[A] = maUninstall then Uninstall;
if Actions[A] = maUpgrade then Upgrade;
if Actions[A] = maEnable then Enable;
if Actions[A] = maDisable then Disable;
end;
finally
EndUpdate;
end;
end;
function TListModule.FindByName(Name: string): TModule;
var
I: Integer;
begin
I := 0;
while (I < Count) and (TModule(Items[I]).Identification <> Name) do Inc(I);
if I < Count then Result := TModule(Items[I])
else Result := nil;
end;
{ TModuleManager }
procedure TModuleManager.SetAPI(AValue: TAPI);
var
I: Integer;
begin
if FAPI = AValue then Exit;
FAPI := AValue;
for I := 0 to Modules.Count - 1 do
TModule(Modules[I]).API := FAPI;
end;
procedure TModuleManager.DoUpdate(Sender: TObject);
begin
if Assigned(FOnUpdate) then FOnUpdate(Self);
end;
function TModuleManager.ModuleRunning(Name: string): Boolean;
var
Module: TModule;
begin
Module := Modules.FindByName(Name);
if Assigned(Module) then begin
Result := Module.Running;
end else Result := False;
end;
procedure TModuleManager.EnumDependenciesCascade(Module: TModule;
ModuleList: TListModule; Conditions: TModuleConditions = [mcAll]);
var
DepModule: TModule;
I: Integer;
begin
for I := 0 to Module.Dependencies.Count - 1 do begin
DepModule := Modules.FindByName(Module.Dependencies[I]);
if Assigned(DepModule) then
with DepModule do begin
if (ModuleList.IndexOf(DepModule) = -1) and
((mcAll in Conditions) or
(Running and (mcRunning in Conditions)) or
(not Running and (mcNotRunning in Conditions)) or
(Installed and (mcInstalled in Conditions)) or
(not Installed and (mcNotInstalled in Conditions)) or
(Enabled and (mcEnabled in Conditions)) or
(not Enabled and (mcNotEnabled in Conditions))) then begin
ModuleList.Add(DepModule);
Self.EnumDependenciesCascade(DepModule, ModuleList);
end;
end else raise Exception.CreateFmt(SModuleNotFound, [DepModule.Identification]);
end;
end;
procedure TModuleManager.EnumSuperiorDependenciesCascade(Module: TModule;
ModuleList: TListModule; Conditions: TModuleConditions = [mcAll]);
var
I: Integer;
begin
for I := 0 to Modules.Count - 1 do
with TModule(Modules[I]) do begin
if (Dependencies.IndexOf(Module.Identification) <> -1) and
(ModuleList.IndexOf(TModule(Modules[I])) = -1) and
((mcAll in Conditions) or
(Running and (mcRunning in Conditions)) or
(not Running and (mcNotRunning in Conditions)) or
(Installed and (mcInstalled in Conditions)) or
(not Installed and (mcNotInstalled in Conditions)) or
(Enabled and (mcEnabled in Conditions)) or
(not Enabled and (mcNotEnabled in Conditions))) then begin
ModuleList.Add(TModule(Modules[I]));
Self.EnumSuperiorDependenciesCascade(TModule(Modules[I]), ModuleList);
end;
end;
end;
procedure TModuleManager.RegisterModule(Module: TModule);
begin
Modules.Add(Module);
Module.FManager := Self;
Module.API := API;
Modules.Update;
end;
procedure TModuleManager.UnregisterModule(Module: TModule);
begin
Modules.Remove(Module);
Modules.Update;
end;
constructor TModuleManager.Create(AOwner: TComponent);
begin
inherited;
Modules := TListModule.Create;
Modules.OwnsObjects := False;
Modules.OnUpdate := DoUpdate;
end;
destructor TModuleManager.Destroy;
begin
Modules.Perform([maStop]);
FreeAndNil(Modules);
inherited;
end;
procedure TModuleManager.LoadFromRegistry(Context: TRegistryContext);
var
I: Integer;
begin
with TRegistryEx.Create do
try
RootKey := Context.RootKey;
for I := 0 to Modules.Count - 1 do
with TModule(Modules[I]) do begin
OpenKey(Context.Key + '\' + Identification, True);
Installed := ReadBoolWithDefault('Installed', Installed);
Enabled := ReadBoolWithDefault('Enabled', Enabled);
end;
finally
Free;
end;
end;
procedure TModuleManager.SaveToRegistry(Context: TRegistryContext);
var
I: Integer;
begin
with TRegistryEx.Create do
try
RootKey := Context.RootKey;
for I := 0 to Modules.Count - 1 do
with TModule(Modules[I]) do begin
OpenKey(Context.Key + '\' + Identification, True);
WriteBool('Enabled', Enabled);
WriteBool('Installed', Installed);
end;
finally
Free;
end;
end;
{ TModule }
procedure TModule.SetRunning(AValue: Boolean);
begin
if FRunning = AValue then Exit;
if AValue then Start else Stop;
end;
procedure TModule.DoStart;
begin
end;
procedure TModule.DoStop;
begin
end;
procedure TModule.DoInstall;
begin
end;
procedure TModule.DoUninstall;
begin
end;
procedure TModule.DoUpgrade;
begin
end;
procedure TModule.Enable;
var
List: TListModule;
begin
if Enabled then Exit;
FEnabled := True;
try
List := TListModule.Create;
List.OwnsObjects := False;
EnumDependenciesCascade(List, [mcNotEnabled]);
List.Perform([maEnable], [mcNotEnabled]);
finally
List.Free;
end;
Start; // Auto start enabled modules
//Manager.Update;
end;
procedure TModule.Disable;
var
List: TListModule;
begin
if not Enabled then Exit;
if FRunning then Stop; // Auto stop running modules
FEnabled := False;
try
List := TListModule.Create;
List.OwnsObjects := False;
EnumSuperiorDependenciesCascade(List, [mcEnabled]);
List.Perform([maDisable], [mcEnabled]);
finally
List.Free;
end;
Manager.Modules.Update;
end;
procedure TModule.SetInstalled(AValue: Boolean);
begin
if FInstalled = AValue then Exit;
if AValue then Install else Uninstall;
end;
procedure TModule.SetManager(AValue: TModuleManager);
begin
if FManager = AValue then Exit;
if Assigned(FManager) then FManager.UnregisterModule(Self);
FManager := AValue;
if Assigned(FManager) then AValue.RegisterModule(Self);
end;
procedure TModule.SetEnabled(AValue: Boolean);
begin
if FEnabled = AValue then Exit;
if AValue then Enable else Disable;
end;
procedure TModule.Start;
var
List: TListModule;
StartTime: TDateTime;
begin
if not Enabled or Running then Exit;
if not Installed then Install; // Auto install not installed modules
try
List := TListModule.Create;
List.OwnsObjects := False;
EnumDependenciesCascade(List, [mcNotRunning]);
List.Perform([maStart], [mcNotRunning]);
finally
List.Free;
end;
StartTime := Now;
DoStart;
FStartUpTime := Now - StartTime;
FRunning := True;
Manager.Modules.Update;
end;
procedure TModule.Stop;
var
List: TListModule;
begin
if not Running then Exit;
FRunning := False;
try
List := TListModule.Create;
List.OwnsObjects := False;
EnumSuperiorDependenciesCascade(List, [mcRunning]);
List.Perform([maStop], [mcRunning]);
finally
List.Free;
end;
DoStop;
Manager.Modules.Update;
end;
procedure TModule.Restart;
begin
Stop;
Start;
end;
procedure TModule.Install;
var
List: TListModule;
begin
if Installed then Exit;
try
List := TListModule.Create;
List.OwnsObjects := False;
EnumDependenciesCascade(List, [mcNotInstalled]);
List.Perform([maInstall], [mcNotInstalled]);
finally
List.Free;
end;
FInstalled := True;
DoInstall;
//Enable; // Auto enable installed module
Manager.Modules.Update;
end;
procedure TModule.Uninstall;
var
List: TListModule;
begin
if not Installed then Exit;
if Enabled then Disable; // Auto disable uninstalled module
try
List := TListModule.Create;
List.OwnsObjects := False;
EnumSuperiorDependenciesCascade(List, [mcInstalled]);
List.Perform([maUninstall], [mcInstalled]);
finally
List.Free;
end;
FInstalled := False;
DoUninstall;
Manager.Modules.Update;
end;
procedure TModule.Reinstall;
begin
Uninstall;
Install;
end;
procedure TModule.Upgrade;
begin
if not Enabled or not Installed then Exit;
if Running then try
Stop;
DoUpgrade;
finally
Start;
end else DoUpgrade;
Manager.Modules.Update;
end;
procedure TModule.EnumDependenciesCascade(ModuleList: TListModule;
Conditions: TModuleConditions = [mcAll]);
begin
ModuleList.Clear;
Manager.EnumDependenciesCascade(Self, ModuleList, Conditions);
end;
procedure TModule.EnumSuperiorDependenciesCascade(ModuleList: TListModule;
Conditions: TModuleConditions = [mcAll]);
begin
ModuleList.Clear;
Manager.EnumSuperiorDependenciesCascade(Self, ModuleList, Conditions);
end;
procedure TModule.SetInstalledState(Value: Boolean);
begin
FInstalled := Value;
Manager.Modules.Update;
end;
constructor TModule.Create(Owner: TComponent);
begin
inherited;
Dependencies := TListString.Create;
Description := TListString.Create;
end;
destructor TModule.Destroy;
begin
Running := False;
FreeAndNil(FDescription);
FreeAndNil(FDependencies);
inherited;
end;
end.