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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
unit GS5_Ext;
{

  GS5 Extension Development

}
interface

uses Windows, Graphics, GS5_Intf, GS5;

type
{
     Custom License Model Development
}

//---- Application Monitor -----------
TGSApp = class
  private
    _core: TGSCore;

    _icon: TIcon;
    function getGameTitle: string; //Game Icon

    //Initialize for every game passes
    procedure init;
    procedure registerLicenseModels;

    //[INTERNAL]
    procedure OnPassBegin(ring: Integer);
    procedure OnPassEnd(ring: Integer);

  protected

    //
    function OnAppInit: Boolean; virtual;

    //Event Category handlers
    procedure OnAppEvent(evtId: Integer); virtual;
    procedure OnLicenseEvent( evtId: Integer); virtual;
    procedure OnEntityEvent(evtId: Integer; entity: TGSEntity); virtual;

    //--- App Specific Event Handlers
    procedure OnAppBegin; virtual;   //EVENT_APP_BEGIN
    procedure OnAppRun; virtual;     //EVENT_APP_RUN
    procedure OnAppEnd; virtual;     //EVENT_APP_END
    procedure OnClockRolledBack; virtual; //EVENT_APP_CLOCK_ROLLBACK
    procedure OnIntegrityCorrupted; virtual; //EVENT_APP_INTEGRITY_CORRUPT
    //---- License Specific Event Handlers
    procedure OnNewInstall; virtual; //EVENT_LICENSE_NEWINSTALL
    procedure OnLicenseLoading; virtual; //EVENT_LICENSE_LOADING
    procedure OnLicenseLoaded; virtual; //EVENT_LICENSE_READY
    procedure OnLicenseFail; virtual;  //EVENT_LICENSE_FAIL
    //----- Entity Specific Event Handlers
    procedure OnEntityAccessStarting(entity: TGSEntity); virtual;  //EVENT_ENTITY_TRY_ACCESS
    procedure OnEntityAccessStarted(entity: TGSEntity); virtual;    //EVENT_ENTITY_ACCESS_STARTED
    procedure OnEntityAccessEnding(entity: TGSEntity); virtual;    //EVENT_ENTITY_ACCESS_ENDING
    procedure OnEntityAccessEnded(entity: TGSEntity); virtual;      //EVENT_ENTITY_ACCESS_ENDED

    procedure OnEntityAccessInvalid(entity: TGSEntity); virtual; //EVENT_ENTITY_ACCESS_INVALID
    procedure OnEntityHeartBeat(entity: TGSEntity); virtual;  //EVENT_ENTITY_ACCESS_HEARTBEAT
    //------ Action Applied Event ------
    procedure OnEntityActionApplied(entity: TGSEntity); virtual; //EVENT_ENTITY_ACTION_APPLIED

    //
  public
    constructor Create;
    destructor Destroy; override;

    //------- App Control --------
    procedure exitApp(rc: Integer);
    procedure terminateApp(rc: Integer);
    procedure playApp;
    procedure restartApp;
    function isRestartedApp: Boolean;

    //----- App Running Context -----
    function isFirstLaunched: Boolean;

    function isFirstPass: Boolean;
    function isGamePass: Boolean;
    function isLastPass: Boolean;

    function isFirstGameExe: Boolean;
    function isLastGameExe: Boolean;

    function isMainThread: Boolean;

    function getAppRootPath: String;
    function getAppCommandLine: String;
    function getAppMainExe: String;
    //App Session Variables
    procedure setSessionVar(const name, val: AnsiString);
    function getSessionVar(const name: AnsiString): AnsiString;


    //Properties
    property GameTitle: string read getGameTitle;
    property GameIcon: TIcon read _icon;

    property RootPath: String read getAppRootPath;
    property CommandLine: String read getAppCommandLine;
    property MainExe: String read getAppMainExe;

    property SessionVars[ const name: AnsiString ]: AnsiString read getSessionVar write setSessionVar;
    property Core: TGSCore read _core;
end;

//--------- Custom License Model -----------
TGS_LM_OnIsValid = function: Boolean of object;
TGS_LM_OnStartAccess = procedure of object;
TGS_LM_OnFinishAccess = procedure of object;
TGS_LM_OnApplyAction = procedure(act: TGSAction) of object;

TGSDynamicLM = class
  private
    _lic: TGSLicense;

    //Event Handlers
    _onIsValid: TGS_LM_OnIsValid;
    _onStart: TGS_LM_OnStartAccess;
    _onFinish: TGS_LM_OnFinishAccess;
    _onApplyAction: TGS_LM_OnApplyAction;

    function getDescription: AnsiString;
    function getId: AnsiString;
    function getName: AnsiString;

    function isValid_: Boolean;
    procedure startAccess_;
    procedure finishAccess_;
    procedure onAction_(hAct: TActionHandle);


  protected
    //Initialize the instance (init properties, etc.)
    procedure init; virtual;

    procedure defineParamStr(const paramName,  paramInitValue: String; permission: Cardinal);
    procedure defineParamInt(const paramName: String; const paramInitValue: Integer; permission: Cardinal);
    procedure defineParamInt64(const paramName: String; const paramInitValue: Int64; permission: Cardinal);
    procedure defineParamBool(const paramName: String; const paramInitValue: Boolean; permission: Cardinal);
    procedure defineParamDouble(const paramName: String; const paramInitValue: Double; permission: Cardinal);
    procedure defineParamFloat(const paramName: String; const paramInitValue: Single; permission: Cardinal);
    procedure defineParamTime(const paramName: String; const paramInitValue: TDateTime; permission: Cardinal);

    //LM handlers
    //Sub-class should override these handlers or uses event properties
    function isValid: Boolean; virtual;
    procedure startAccess; virtual;
    procedure finishAccess; virtual;
    procedure onAction(act: TGSAction); virtual;

    constructor Create;
    destructor Destroy; override;

  public
    //Property
    property Id: AnsiString read getId;
    property Name: AnsiString read getName;
    property Description: AnsiString read getDescription;

    property License: TGSLicense read _lic;

    //Event Handlers
    property OnIsValid: TGS_LM_OnIsValid write _onIsValid;
    property OnStartAccess: TGS_LM_OnStartAccess write _onStart;
    property OnFinishAccess: TGS_LM_OnFinishAccess write _onFinish;
    property OnApplyAction: TGS_LM_OnApplyAction write _onApplyAction;
end;

TGSAppCls = class of TGSApp;
TGSLMCls =  class of TGSDynamicLM;

procedure registerApp(clsApp: TGSAppCls);
procedure registerLM(clsLM: TClass; const licId, licName, description: AnsiString);

function getGSApp: TGSApp;

implementation

uses ShellAPI, Classes
{$ifdef DEBUG}
  ,unDebugHelper
{$endif};

type
  PCustomLMInfo = ^TCustomLMInfo;
  TCustomLMInfo = record
    _cls: TClass;
    _id, _name, _description: AnsiString;
  end;

var
  s_clsApp: TGSAppCls = TGSApp;
  s_app: TGSApp;

  s_lms: TList = nil;

procedure registerApp(clsApp: TGSAppCls);
begin
  s_clsApp := clsApp;
end;

procedure registerLM(clsLM: TClass; const licId, licName, description: AnsiString);
var
  p : PCustomLMInfo;
begin
  if s_lms = nil then begin
    s_lms := TList.Create;
  end;

  New(p);
  p^._cls := clsLM;
  p^._id := licId;
  p^._name := licName;
  p^._description := description;
  s_lms.Add(p);
end;

function getGSApp: TGSApp;
begin
  if s_app = nil then s_app := s_clsApp.Create;
  Result := s_app;
end;

procedure GS5_Entry(srv: Pointer);stdcall;
begin
  {$IFDEF DEBUG}
  DebugMsg('GS5_Entry: Initialize GSApp...');
  {$ENDIF}
  //Application launching, initializes my LM class instance.
  getGSApp;
end;

exports GS5_Entry;


{ TGSDynamicLM }

function fcb_isValid(usrData: Pointer): Boolean; stdcall;
begin
  Result := TGSDynamicLM(usrData).IsValid_();
end;

procedure fcb_startAccess(usrData: Pointer); stdcall;
begin
  TGSDynamicLM(usrData).startAccess_;
end;

procedure fcb_finishAccess(usrData: Pointer); stdcall;
begin
  TGSDynamicLM(usrData).finishAccess_;
end;

procedure fcb_onAction(hAct: TActionHandle; usrData: Pointer); stdcall;
begin
  TGSDynamicLM(usrData).onAction_(hAct);
end;

procedure fcb_onDestroy(usrData: Pointer); stdcall;
begin
  TGSDynamicLM(usrData).Destroy;
end;

constructor TGSDynamicLM.Create;
begin
end;

procedure TGSDynamicLM.defineParamBool(const paramName: String;
  const paramInitValue: Boolean; permission: Cardinal);
begin
  gsAddLicenseParamBool(_lic.Handle, PAnsiChar(paramName), paramInitValue, permission);
end;

procedure TGSDynamicLM.defineParamDouble(const paramName: String;
  const paramInitValue: Double; permission: Cardinal);
begin
  gsAddLicenseParamDouble(_lic.Handle, PAnsiChar(paramName), paramInitValue, permission);
end;

procedure TGSDynamicLM.defineParamFloat(const paramName: String;
  const paramInitValue: Single; permission: Cardinal);
begin
  gsAddLicenseParamFloat(_lic.Handle, PAnsiChar(paramName), paramInitValue, permission);
end;

procedure TGSDynamicLM.defineParamInt(const paramName: String;
  const paramInitValue: Integer; permission: Cardinal);
begin
  gsAddLicenseParamInt(_lic.Handle, PAnsiChar(paramName), paramInitValue, permission);
end;

procedure TGSDynamicLM.defineParamInt64(const paramName: String;
  const paramInitValue: Int64; permission: Cardinal);
begin
  gsAddLicenseParamInt64(_lic.Handle, PAnsiChar(paramName), paramInitValue, permission);
end;

procedure TGSDynamicLM.defineParamStr(const paramName,
  paramInitValue: String; permission: Cardinal);
begin
  gsAddLicenseParamStr(_lic.Handle, PAnsiChar(paramName), PAnsiChar(paramInitValue), permission);
end;

procedure TGSDynamicLM.defineParamTime(const paramName: String;
  const paramInitValue: TDateTime; permission: Cardinal);
begin
  gsAddLicenseParamTime(_lic.Handle, PAnsiChar(paramName), UTCToUnixTime(paramInitValue), permission);
end;

destructor TGSDynamicLM.Destroy;
begin
  _lic.Free;
  inherited;
end;


procedure TGSDynamicLM.finishAccess;
begin
end;

procedure TGSDynamicLM.finishAccess_;
begin
  if Assigned(_onFinish) then _onFinish;
  finishAccess;
end;

function TGSDynamicLM.getDescription: AnsiString;
begin
  Result := _lic.Description;
end;

function TGSDynamicLM.getId: AnsiString;
begin
  Result := _lic.Id;
end;

function TGSDynamicLM.getName: AnsiString;
begin
  Result := _lic.Name;
end;

procedure TGSDynamicLM.init;
begin

end;

function TGSDynamicLM.isValid: Boolean;
begin
  Result := False;
end;

function TGSDynamicLM.isValid_: Boolean;
begin
  if Assigned(_onIsValid) then Result := _onIsValid()
  else Result := self.isValid;
end;

procedure TGSDynamicLM.onAction(act: TGSAction);
begin
end;

procedure TGSDynamicLM.onAction_(hAct: TActionHandle);
var
  act: TGSAction;
begin
  act := TGSAction.Create(hAct);
  try
    if Assigned(_onApplyAction) then _onApplyAction(act);
    onAction(act);
  finally
    act.Free;
  end;
end;

procedure TGSDynamicLM.startAccess;
begin
end;

procedure TGSDynamicLM.startAccess_;
begin
  if Assigned(_onStart) then _onStart;
  startAccess;
end;

{ TGSApp }

constructor TGSApp.Create;
begin
  //Initializes the gsCore
  _core := TGSCore.getInstance;
  //register my event handlers
  _core.OnAppEvent := OnAppEvent;
  _core.OnLicenseEvent := OnLicenseEvent;
  _core.OnEntityEvent := OnEntityEvent;

  _icon := TIcon.Create;
  _icon.Handle := ExtractIcon(GetModuleHandle(nil), PAnsiChar(MainExe), 0);
end;

destructor TGSApp.Destroy;
begin
  _core.cleanUp;
  _icon.Free;

  inherited;
end;

function TGSApp.getGameTitle: string;
begin
  Result := _core.ProductName;
end;

procedure TGSApp.init;
begin
  if not OnAppInit then begin
    {$ifdef DEBUG}
    DebugMsg('TGSApp.init >> terminating...');
    {$ENDIF}
    terminateApp(-1);
  end;
end;

function TGSApp.OnAppInit: Boolean;
begin
  Result := True;
end;

procedure TGSApp.OnAppBegin;
begin

end;

procedure TGSApp.OnAppEnd;
begin

end;

procedure TGSApp.OnAppEvent(evtId: Integer);
begin
  {$IFDEF DEBUG}
  DebugMsg('OnAppEvent >> Event [%s]', [_core.getEventName(evtId)]);
  {$ENDIF}
  case evtId of
    EVENT_PASS_BEGIN_RING1: OnPassBegin(1);
    EVENT_PASS_BEGIN_RING2: OnPassBegin(2);
    EVENT_PASS_END_RING1: OnPassEnd(1);
    EVENT_PASS_END_RING2: OnPassEnd(2);

    EVENT_APP_BEGIN: OnAppBegin;
    EVENT_APP_END:  OnAppEnd;
    EVENT_APP_RUN: OnAppRun;
    EVENT_APP_CLOCK_ROLLBACK: OnClockRolledBack;
    EVENT_APP_INTEGRITY_CORRUPT: OnIntegrityCorrupted;
  else
    {$IFDEF DEBUG}
    DebugMsg('Unknown Application Event [%s]', [_core.getEventName(evtId)]);
    {$ENDIF}
  end;
  {$IFDEF DEBUG}
  DebugMsg('OnAppEvent << Event [%s]', [_core.getEventName(evtId)]);
  {$ENDIF}
end;

procedure TGSApp.OnAppRun;
begin

end;

procedure TGSApp.OnClockRolledBack;
begin

end;

procedure TGSApp.OnEntityAccessEnded(entity: TGSEntity);
begin

end;

procedure TGSApp.OnEntityAccessInvalid(entity: TGSEntity);
begin

end;

procedure TGSApp.OnEntityAccessStarted(entity: TGSEntity);
begin

end;

procedure TGSApp.OnEntityActionApplied(entity: TGSEntity);
begin

end;

procedure TGSApp.OnEntityEvent(evtId: Integer; entity: TGSEntity);
begin
  case evtId of
    EVENT_ENTITY_TRY_ACCESS: OnEntityAccessStarting(entity);
    EVENT_ENTITY_ACCESS_STARTED: OnEntityAccessStarted(entity);
    EVENT_ENTITY_ACCESS_ENDING: OnEntityAccessStarting(entity);
    EVENT_ENTITY_ACCESS_ENDED: OnEntityAccessEnded(entity);
    EVENT_ENTITY_ACCESS_INVALID: OnEntityAccessInvalid(entity);
    EVENT_ENTITY_ACCESS_HEARTBEAT: OnEntityHeartBeat(entity);
    EVENT_ENTITY_ACTION_APPLIED: OnEntityActionApplied(entity);
  end;
end;

procedure TGSApp.OnEntityHeartBeat(entity: TGSEntity);
begin

end;

procedure TGSApp.OnIntegrityCorrupted;
begin

end;

procedure TGSApp.OnLicenseEvent(evtId: Integer);
begin
  case evtId of
    EVENT_LICENSE_NEWINSTALL:
    begin
      OnNewInstall();
    end;

    EVENT_LICENSE_LOADING:
    begin
      registerLicenseModels;
      OnLicenseLoading();
    end;

    EVENT_LICENSE_READY:
    begin
      OnLicenseLoaded();
    end;
    EVENT_LICENSE_FAIL:
    begin
      OnLicenseFail();
    end;
  end;
end;


procedure TGSApp.OnLicenseFail;
begin

end;

procedure TGSApp.OnLicenseLoaded;
begin
end;

procedure TGSApp.OnNewInstall;
begin

end;

procedure TGSApp.OnPassBegin(ring: Integer);
begin
  case ring of
    1:
    begin

    end;
    2:
    begin
      init;
    end;
  end;
end;

procedure TGSApp.OnPassEnd(ring: Integer);
begin

end;

function s_createLM(usrData: Pointer): TLicenseHandle; stdcall;
var
  p : PCustomLMInfo;
  lm: TObject;
begin
  p := PCustomLMInfo(usrData);
  lm := p^._cls.Create;  //freed on fcb_onDestroy

  Result := gsCreateCustomLicense(PAnsiChar(p^._id), PAnsiChar(p^._name), PAnsiChar(p^._description), Pointer(lm),
    fcb_isValid, fcb_startAccess, fcb_finishAccess, fcb_onAction, fcb_onDestroy);

  TGSDynamicLM(lm)._lic := TGSLicense.Create(Result);
  TGSDynamicLM(lm).init;
end;

procedure TGSApp.OnEntityAccessEnding(entity: TGSEntity);
begin

end;

procedure TGSApp.OnEntityAccessStarting(entity: TGSEntity);
begin

end;

procedure TGSApp.registerLicenseModels;
var
  i : Integer;
  p : PCustomLMInfo;
begin
  {$ifdef DEBUG}
  DebugMsg('registerLicenseModels >>');
  {$endif}
  if s_lms <> nil then begin
    for i := 0 to s_lms.Count-1 do begin
      p := PCustomLMInfo(s_lms[i]);
      gsRegisterCustomLicense(PAnsiChar(p^._id), s_createLM, p);
    end;
  end;
  {$ifdef DEBUG}
  DebugMsg('registerLicenseModels <<');
  {$endif}
end;

procedure TGSApp.OnLicenseLoading;
begin

end;

procedure TGSApp.exitApp(rc: Integer);
begin
  gsExitApp(rc);
end;

function TGSApp.getAppCommandLine: String;
begin
  Result := gsGetAppCommandLine;
end;

function TGSApp.getAppMainExe: String;
begin
  Result := gsGetAppMainExe;
end;

function TGSApp.getAppRootPath: String;
begin
  Result := gsGetAppRootPath;
end;

function TGSApp.getSessionVar(const name: AnsiString): AnsiString;
begin
  Result := gsGetAppVar(PAnsiChar(name));
end;

function TGSApp.isFirstGameExe: Boolean;
begin
  Result := gsIsFirstGameExe;
end;

function TGSApp.isFirstPass: Boolean;
begin
  Result := gsIsFirstPass;
end;

function TGSApp.isGamePass: Boolean;
begin
  Result := gsIsGamePass;
end;

function TGSApp.isLastGameExe: Boolean;
begin
  Result := gsIsLastGameExe;
end;

function TGSApp.isLastPass: Boolean;
begin
  Result := gsIsLastPass;
end;

function TGSApp.isMainThread: Boolean;
begin
  Result := gsIsMainThread;
end;

function TGSApp.isRestartedApp: Boolean;
begin
  Result := gsIsRestartedApp;
end;

procedure TGSApp.playApp;
begin
  gsPlayApp;
end;

procedure TGSApp.restartApp;
begin
  gsRestartApp;
end;

procedure TGSApp.setSessionVar(const name, val: AnsiString);
begin
  gsSetAppVar(PAnsiChar(name), PAnsiChar(val));
end;

procedure TGSApp.terminateApp(rc: Integer);
begin
  gsTerminateApp(rc);
end;

function TGSApp.isFirstLaunched: Boolean;
begin
  Result := gsIsAppFirstLaunched;
end;

end.