-
Notifications
You must be signed in to change notification settings - Fork 51
/
uActionController.pas
571 lines (460 loc) · 13.9 KB
/
uActionController.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
unit uActionController;
interface
uses Classes, DebugInfo, DebugerTypes, XMLDoc, XMLIntf, Collections.Queues,
System.SyncObjs;
type
TacAction = (acCreateProcess, acAddThread, acUpdateInfo, acProgress, acSetProjectName, acChangeProjectSettings, acChangeDbgState);
TDbgOption = (
doDebugInfo,
doRun,
doProfiler,
doMemProfiler, doMemCallStack, doMemCheckDoubleFree,
doExceptions, doExceptionCallStack,
doCodeTracking, doTrackSystemUnits, doSamplingMethod,
doSyncObjsTracking
);
TDbgOptions = set of TDbgOption;
TArgsList = array of Variant;
TActionItem = class
private
FAction: TacAction;
FArgs: TArgsList;
public
constructor Create(const AAction: TacAction; const AArgs: TArgsList);
property Action: TacAction read FAction;
property Args: TArgsList read FArgs;
end;
TActionQueue = class(TQueue<TActionItem>)
public
constructor Create;
procedure Clear; override;
function GetAction: TActionItem;
procedure AddAction(const Action: TacAction; const Args: TArgsList);
end;
TActionThread = class(TThread)
protected
procedure Execute; override;
public
constructor Create;
destructor Destroy; override;
end;
TActionController = class
public
class procedure RunDebug(const ADbgOptions: TDbgOptions; const AProcessID: TProcessId = 0); static;
class procedure StopDebug; static;
class procedure PauseDebug; static;
class procedure TraceDebug(const TraceType: TDbgTraceState); static;
class procedure Log(const LogType: TDbgLogType; const Msg: String); overload; static;
class procedure Log(const LogType: TDbgLogType; const Msg: String; const Args: array of const); overload; static;
class procedure DoAction(const Action: TacAction; const Args: array of Variant); overload; static;
class procedure DoAction(const Action: TacAction; const Args: TArgsList); overload; static;
class procedure DoSyncAction(const Action: TacAction; const Args: array of Variant); overload; static;
class procedure DoSyncAction(const Action: TacAction; const Args: TArgsList); overload; static;
class procedure ViewDebugInfo(DebugInfo: TDebugInfo); static;
class procedure ClearDebug(const DbgFree: LongBool); static;
class procedure AppClose; static;
end;
TProjectOptions = class
private
FProjectName: String;
FProjectXML: IXMLDocument;
FUpdateCount: Integer;
function GetXMLValue(const NodeName: String): String;
procedure SetXMLValue(const NodeName, NodeValue: String);
function GetApplicationName: String;
function GetProjectName: String;
function GetProjectStorage: String;
function GetDelphiSource: String;
function GetProjectSource: String;
function GetRunParams: String;
function GetWorkingDirectory: String;
procedure SetApplicationName(const Value: String);
procedure SetProjectStorage(const Value: String);
procedure SetDelphiSource(const Value: String);
procedure SetProjectSource(const Value: String);
procedure SetRunParams(const Value: String);
procedure SetWorkingDirectory(const Value: String);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure BeginUpdate;
procedure EndUpdate;
procedure Open(const AProjectName: String);
procedure NewConfig;
procedure Save;
class function GetDefProjectSource(const ProjectName: String): String; static;
class function GetDefDelphiSource: String; static;
property ProjectName: String read GetProjectName;
property ApplicationName: String read GetApplicationName write SetApplicationName;
property ProjectStorage: String read GetProjectStorage write SetProjectStorage;
property ProjectSource: String read GetProjectSource write SetProjectSource;
property DelphiSource: String read GetDelphiSource write SetDelphiSource;
property RunParams: String read GetRunParams write SetRunParams;
property WorkingDirectory: String read GetWorkingDirectory write SetWorkingDirectory;
end;
const
_DEFAULT_PROJECT = '_default.spider';
var
_AC: TActionController = nil;
gvProjectOptions: TProjectOptions = nil;
implementation
uses SysUtils, uMain, Debuger, uDebugerThread, ClassUtils, JclIDEUtils,
System.IOUtils, System.Types, Vcl.Dialogs;
var
gvActionThread: TActionThread = nil;
gvActionQueue: TActionQueue = nil;
{ TActionController }
class procedure TActionController.Log(const LogType: TDbgLogType; const Msg: String);
begin
if Assigned(gvDebugInfo) then
gvDebugInfo.DbgLog.Add(LogType, Msg);
DoAction(acUpdateInfo, []);
end;
class procedure TActionController.AppClose;
begin
gvActionThread.Terminate;
Sleep(500);
end;
class procedure TActionController.ClearDebug(const DbgFree: LongBool);
begin
gvActionQueue.Clear;
if Assigned(gvDebugInfo) then
begin
gvDebugInfo.ClearDebugInfo;
if DbgFree then
FreeAndNil(gvDebugInfo);
end;
if Assigned(gvDebuger) then
begin
gvDebuger.ClearDbgInfo;
if DbgFree then
FreeAndNil(gvDebuger);
end;
end;
class procedure TActionController.DoAction(const Action: TacAction; const Args: TArgsList);
begin
gvActionQueue.AddAction(Action, Args);
end;
class procedure TActionController.DoSyncAction(const Action: TacAction; const Args: array of Variant);
var
_Args: TArgsList;
i: Integer;
begin
SetLength(_Args, Length(Args));
for i := 0 to High(Args) do
_Args[i] := Args[i];
TActionController.DoSyncAction(Action, _Args);
end;
class procedure TActionController.DoAction(const Action: TacAction; const Args: array of Variant);
var
_Args: TArgsList;
i: Integer;
begin
SetLength(_Args, Length(Args));
for i := 0 to High(Args) do
_Args[i] := Args[i];
TActionController.DoAction(Action, _Args);
end;
class procedure TActionController.DoSyncAction(const Action: TacAction; const Args: TArgsList);
begin
TThread.Synchronize(nil,
procedure
begin
if Assigned(MainForm) then
MainForm.DoAction(Action, Args);
end
);
end;
class procedure TActionController.Log(const LogType: TDbgLogType; const Msg: String; const Args: array of const);
begin
Log(LogType, Format(Msg, Args));
end;
class procedure TActionController.PauseDebug;
begin
TActionController.TraceDebug(dtsPause);
end;
class procedure TActionController.RunDebug(const ADbgOptions: TDbgOptions; const AProcessID: TProcessId = 0);
begin
if not Assigned(_DbgThread) then
_DbgThread := TDebugerThread.Create(ADbgOptions, AProcessID);
end;
class procedure TActionController.StopDebug;
begin
if Assigned(gvDebuger) then
gvDebuger.StopDebug;
end;
class procedure TActionController.TraceDebug(const TraceType: TDbgTraceState);
begin
if Assigned(gvDebuger) then
begin
gvDebuger.TraceDebug(TraceType);
TActionController.DoAction(acChangeDbgState, []);
end;
end;
class procedure TActionController.ViewDebugInfo(DebugInfo: TDebugInfo);
var
DI: TDebugInfo;
begin
DI := DebugInfo;
TThread.Synchronize(nil,
procedure
begin
MainForm.ViewDebugInfo(DI);
end
);
end;
{ TProjectOptions }
procedure TProjectOptions.BeginUpdate;
begin
Inc(FUpdateCount);
end;
procedure TProjectOptions.Clear;
begin
FProjectName := '';
FProjectXML := nil;
FUpdateCount := 0;
end;
constructor TProjectOptions.Create;
begin
inherited Create;
FProjectXML := nil;
FUpdateCount := 0;
end;
destructor TProjectOptions.Destroy;
begin
inherited;
end;
procedure TProjectOptions.EndUpdate;
begin
Dec(FUpdateCount);
if FUpdateCount = 0 then
Save;
end;
function TProjectOptions.GetProjectName: String;
begin
Result := FProjectName;
end;
procedure TProjectOptions.NewConfig;
var
DocNode: IXMLNode;
begin
FProjectXML := TXMLDocument.Create(nil);
FProjectXML.NodeIndentStr := ' ';
FProjectXML.Options := FProjectXML.Options + [doNodeAutoIndent, doNodeAutoCreate];
FProjectXML.Active := True;
FProjectXML.Encoding := 'utf-8';
DocNode := FProjectXML.AddChild('spider');
DocNode.Attributes['version'] := '1.0';
end;
procedure TProjectOptions.Open(const AProjectName: String);
begin
if AProjectName <> FProjectName then
Clear;
FProjectName := AProjectName;
if FileExists(FProjectName) then
FProjectXML := TXMLDocument.Create(FProjectName)
else
NewConfig;
end;
procedure TProjectOptions.Save;
begin
if Assigned(FProjectXML) and (ExtractFileName(FProjectName) <> _DEFAULT_PROJECT) then
try
FProjectXML.SaveToFile(FProjectName);
except
on E: Exception do
ShowMessageFmt('Save to "%s" fail: %s', [FProjectName, E.Message]);
end;
end;
function TProjectOptions.GetApplicationName: String;
begin
Result := GetXMLValue('application_name');
end;
procedure TProjectOptions.SetApplicationName(const Value: String);
begin
SetXMLValue('application_name', Value);
end;
class function TProjectOptions.GetDefDelphiSource: String;
var
Installations: TJclBorRADToolInstallations;
DelphiRoot: String;
begin
Result := '';
Installations := TJclBorRADToolInstallations.Create;
try
if Installations.Count > 0 then
begin
DelphiRoot := Installations.Installations[Installations.Count - 1].RootDir;
Result := IncludeTrailingPathDelimiter(DelphiRoot) + 'source';
end;
finally
FreeAndNil(Installations);
end;
end;
class function TProjectOptions.GetDefProjectSource(const ProjectName: String): String;
var
DprName: String;
DprPathName: String;
Find: TStringDynArray;
begin
Result := '';
if ProjectName <> '' then
begin
DprName := ExtractFileName(ProjectName);
DprName := ChangeFileExt(DprName, '.dpr');
Result := ExtractFileDir(ProjectName);
while (Result <> '') and TDirectory.Exists(Result) do
begin
DprPathName := Result + PathDelim + DprName;
if TFile.Exists(DprPathName) then
Break;
Find := TDirectory.GetFiles(Result, '*.dpr');
if Length(Find) > 0 then
Break;
Result := ExtractFileDir(Result);
// Åñëè â ðóòîâîì êàòàëîãå, òî âûõîäèì
if Result = IncludeTrailingPathDelimiter(ExtractFileDrive(Result)) then
begin
Result := '';
Break;
end;
end;
if Result <> '' then
begin
// TODO: Source dirs from DPR
end;
end;
end;
function TProjectOptions.GetDelphiSource: String;
begin
Result := GetXMLValue('delphi_source');
end;
procedure TProjectOptions.SetDelphiSource(const Value: String);
begin
SetXMLValue('delphi_source', Value);
end;
function TProjectOptions.GetProjectSource: String;
begin
Result := GetXMLValue('project_source');
end;
procedure TProjectOptions.SetProjectSource(const Value: String);
begin
SetXMLValue('project_source', Value);
end;
function TProjectOptions.GetProjectStorage: String;
begin
Result := GetXMLValue('project_storage');
end;
function TProjectOptions.GetRunParams: String;
begin
Result := GetXMLValue('run_parameters');
end;
function TProjectOptions.GetWorkingDirectory: String;
begin
Result := GetXMLValue('working_directory');
end;
procedure TProjectOptions.SetProjectStorage(const Value: String);
begin
SetXMLValue('project_storage', Value);
end;
procedure TProjectOptions.SetRunParams(const Value: String);
begin
SetXMLValue('run_parameters', Value);
end;
procedure TProjectOptions.SetWorkingDirectory(const Value: String);
begin
SetXMLValue('working_directory', Value);
end;
function TProjectOptions.GetXMLValue(const NodeName: String): String;
begin
Result := '';
if Assigned(FProjectXML) then
Result := ClassUtils.GetXMLValue(FProjectXML.DocumentElement, NodeName);
end;
procedure TProjectOptions.SetXMLValue(const NodeName, NodeValue: String);
begin
if Assigned(FProjectXML) then
begin
BeginUpdate;
ClassUtils.SetXMLValue(FProjectXML.DocumentElement, NodeName, NodeValue);
EndUpdate;
end;
end;
{ TActionQueue }
procedure TActionQueue.AddAction(const Action: TacAction; const Args: TArgsList);
var
ActionItem: TActionItem;
begin
ActionItem := TActionItem.Create(Action, Args);
Enqueue(ActionItem);
end;
procedure TActionQueue.Clear;
var
Action: TActionItem;
begin
while Count > 0 do
begin
Action := GetAction;
FreeAndNil(Action);
end;
inherited Clear;
end;
constructor TActionQueue.Create;
begin
inherited Create(1024, True);
end;
function TActionQueue.GetAction: TActionItem;
begin
Result := Dequeue;
end;
{ TActionThread }
constructor TActionThread.Create;
begin
inherited Create(False);
FreeOnTerminate := True;
end;
destructor TActionThread.Destroy;
begin
FreeAndNil(gvActionQueue);
gvActionThread := nil;
inherited;
end;
procedure TActionThread.Execute;
var
ActionItem: TActionItem;
begin
NameThreadForDebugging(ClassName);
while not Terminated do
begin
while not Terminated and (gvActionQueue.Count > 0) do
begin
ActionItem := gvActionQueue.GetAction;
if Assigned(ActionItem) then
try
_AC.DoSyncAction(ActionItem.Action, ActionItem.Args);
finally
FreeAndNil(ActionItem);
end;
end;
if not Terminated then
Sleep(500);
end;
end;
{ TActionItem }
constructor TActionItem.Create(const AAction: TacAction; const AArgs: TArgsList);
begin
inherited Create;
FAction := AAction;
FArgs := Copy(AArgs, 0, Length(AArgs));
end;
initialization
gvProjectOptions := TProjectOptions.Create;
gvActionQueue := TActionQueue.Create;
gvActionThread := TActionThread.Create;
finalization
// Destroys in gvActionThread
//FreeAndNil(gvActionThread);
//FreeAndNil(gvActionQueue);
FreeAndNil(gvProjectOptions);
end.