forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.pas
4391 lines (4050 loc) · 158 KB
/
Main.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
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Main;
{
Inno Setup
Copyright (C) 1997-2016 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Background form
}
interface
{$I VERSION.INC}
uses
Windows, SysUtils, Messages, Classes, Graphics, Controls, Forms, Dialogs,
SetupForm, StdCtrls, Struct, DebugStruct, Int64Em, CmnFunc, CmnFunc2,
SetupTypes, ScriptRunner, BidiUtils, RestartManager;
type
TMainForm = class(TSetupForm)
procedure FormResize(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormPaint(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
IsMinimized, HideWizard: Boolean;
function MainWindowHook(var Message: TMessage): Boolean;
procedure UpdateWizardFormVisibility;
procedure WMSysCommand(var Message: TWMSysCommand); message WM_SYSCOMMAND;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
procedure WMShowWindow(var Message: TWMShowWindow); message WM_SHOWWINDOW;
public
{ Public declarations }
CurStep: TSetupStep;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Finish(const FromPreparingPage: Boolean);
procedure InitializeWizard;
function Install: Boolean;
procedure SetStep(const AStep: TSetupStep; const HandleExceptions: Boolean);
class procedure ShowException(Sender: TObject; E: Exception);
class procedure ShowExceptionMsg(const S: String);
procedure ShowAboutBox;
end;
TEntryType = (seLanguage, seCustomMessage, sePermission, seType, seComponent,
seTask, seDir, seFile, seFileLocation, seIcon, seIni, seRegistry,
seInstallDelete, seUninstallDelete, seRun, seUninstallRun);
const
EntryStrings: array[TEntryType] of Integer = (SetupLanguageEntryStrings,
SetupCustomMessageEntryStrings, SetupPermissionEntryStrings,
SetupTypeEntryStrings, SetupComponentEntryStrings, SetupTaskEntryStrings,
SetupDirEntryStrings, SetupFileEntryStrings, SetupFileLocationEntryStrings,
SetupIconEntryStrings, SetupIniEntryStrings, SetupRegistryEntryStrings,
SetupDeleteEntryStrings, SetupDeleteEntryStrings, SetupRunEntryStrings,
SetupRunEntryStrings);
EntryAnsiStrings: array[TEntryType] of Integer = (SetupLanguageEntryAnsiStrings,
SetupCustomMessageEntryAnsiStrings, SetupPermissionEntryAnsiStrings,
SetupTypeEntryAnsiStrings, SetupComponentEntryAnsiStrings, SetupTaskEntryAnsiStrings,
SetupDirEntryAnsiStrings, SetupFileEntryAnsiStrings, SetupFileLocationEntryAnsiStrings,
SetupIconEntryAnsiStrings, SetupIniEntryAnsiStrings, SetupRegistryEntryAnsiStrings,
SetupDeleteEntryAnsiStrings, SetupDeleteEntryAnsiStrings, SetupRunEntryAnsiStrings,
SetupRunEntryAnsiStrings);
{ Exit codes that are assigned to the SetupExitCode variable.
Note: SetupLdr also returns exit codes with the same numbers. }
ecInitializationError = 1; { Setup failed to initialize. }
ecCancelledBeforeInstall = 2; { User clicked Cancel before the actual
installation started. }
ecNextStepError = 3; { A fatal exception occurred while moving to
the next step. }
ecInstallationError = 4; { A fatal exception occurred during
installation. }
ecInstallationCancelled = 5; { User clicked Cancel during installation,
or clicked Abort at an Abort-Retry-Ignore
dialog. }
ecKilledByDebugger = 6; { User killed the Setup process from within
the debugger. }
ecPrepareToInstallFailed = 7; { Stopped on Preparing to Install page;
restart not needed. }
ecPrepareToInstallFailedRestartNeeded = 8;
{ Stopped on Preparing to Install page;
restart needed. }
var
MainForm: TMainForm;
{ Variables for command line parameters }
SetupLdrMode: Boolean;
SetupLdrOriginalFilename: String;
SetupLdrOffset0, SetupLdrOffset1: Longint;
SetupNotifyWndPresent: Boolean;
SetupNotifyWnd: HWND;
InitLang: String;
InitDir, InitProgramGroup: String;
InitLoadInf, InitSaveInf: String;
InitNoIcons, InitSilent, InitVerySilent, InitNoRestart, InitCloseApplications,
InitNoCloseApplications, InitForceCloseApplications, InitNoForceCloseApplications,
InitRestartApplications, InitNoRestartApplications, InitNoCancel: Boolean;
InitSetupType: String;
InitComponents, InitTasks: TStringList;
InitComponentsSpecified: Boolean;
InitDeselectAllTasks: Boolean;
InitPassword: String;
InitRestartExitCode: Integer;
InitSuppressMsgBoxes: Boolean;
DetachedUninstMsgFile: Boolean;
{ Debugger }
OriginalEntryIndexes: array[TEntryType] of TList;
{ 'Constants' }
SourceDir, TempInstallDir, WinDir, WinSystemDir, WinSysWow64Dir, SystemDrive,
ProgramFiles32Dir, CommonFiles32Dir, ProgramFiles64Dir, CommonFiles64Dir,
ProgramFilesUserDir, CommonFilesUserDir, CmdFilename, SysUserInfoName,
SysUserInfoOrg, UninstallExeFilename: String;
{ Uninstall 'constants' }
UninstallExpandedAppId, UninstallExpandedApp, UninstallExpandedGroup,
UninstallExpandedGroupName, UninstallExpandedLanguage: String;
UninstallSilent: Boolean;
{ Variables read in from the SETUP.0 file }
SetupHeader: TSetupHeader;
LangOptions: TSetupLanguageEntry;
Entries: array[TEntryType] of TList;
WizardImage: TBitmap;
WizardSmallImage: TBitmap;
CloseApplicationsFilterList: TStringList;
{ User options }
ActiveLanguage: Integer = -1;
ActiveLicenseText, ActiveInfoBeforeText, ActiveInfoAfterText: AnsiString;
WizardUserInfoName, WizardUserInfoOrg, WizardUserInfoSerial, WizardDirValue, WizardGroupValue: String;
WizardNoIcons, WizardPreparingYesRadio: Boolean;
WizardSetupType: PSetupTypeEntry;
WizardComponents, WizardDeselectedComponents, WizardTasks, WizardDeselectedTasks: TStringList;
NeedToAbortInstall: Boolean;
{ Check/BeforeInstall/AfterInstall 'contants' }
CheckOrInstallCurrentFilename, CheckOrInstallCurrentSourceFilename: String;
{ RestartManager API state.
Note: the handle and key might change while running, see TWizardForm.QueryRestartManager. }
RmSessionStarted, RmFoundApplications, RmDoRestart: Boolean;
RmSessionHandle: DWORD;
RmSessionKey: array[0..CCH_RM_SESSION_KEY] of WideChar;
{ Other }
ShowLanguageDialog: Boolean;
InstallMode: (imNormal, imSilent, imVerySilent);
HasIcons, IsNT, IsWin64, Is64BitInstallMode, IsAdmin, IsPowerUserOrAdmin,
NeedPassword, NeedSerial, NeedsRestart, RestartSystem,
IsUninstaller, AllowUninstallerShutdown, AcceptedQueryEndSessionInProgress: Boolean;
InstallDefaultDisableFsRedir, ScriptFuncDisableFsRedir: Boolean;
InstallDefaultRegView: TRegView = rvDefault;
HasCustomType, HasComponents, HasTasks: Boolean;
ProcessorArchitecture: TSetupProcessorArchitecture = paUnknown;
WindowsVersion: Cardinal;
NTServicePackLevel: Word;
WindowsProductType: Byte;
WindowsSuiteMask: Word;
MinimumSpace: Integer64;
DeleteFilesAfterInstallList, DeleteDirsAfterInstallList: TStringList;
ExpandedAppName, ExpandedAppVerName, ExpandedAppCopyright, ExpandedAppMutex: String;
DisableCodeConsts: Integer;
SetupExitCode: Integer;
CreatedIcon: Boolean;
RestartInitiatedByThisProcess: Boolean;
{$IFDEF IS_D12}
TaskbarButtonHidden: Boolean;
{$ENDIF}
CodeRunner: TScriptRunner;
function CodeRunnerOnDebug(const Position: LongInt;
var ContinueStepOver: Boolean): Boolean;
function CodeRunnerOnDebugIntermediate(const Position: LongInt;
var ContinueStepOver: Boolean): Boolean;
procedure CodeRunnerOnDllImport(var DllName: String; var ForceDelayLoad: Boolean);
procedure CodeRunnerOnException(const Exception: AnsiString; const Position: LongInt);
procedure CreateTempInstallDir;
procedure DebugNotifyEntry(EntryType: TEntryType; Number: Integer);
procedure DeinitSetup(const AllowCustomSetupExitCode: Boolean);
function ExitSetupMsgBox: Boolean;
function ExpandConst(const S: String): String;
function ExpandConstEx(const S: String; const CustomConsts: array of String): String;
function ExpandConstEx2(const S: String; const CustomConsts: array of String;
const DoExpandIndividualConst: Boolean): String;
function ExpandConstIfPrefixed(const S: String): String;
function GetCustomMessageValue(const AName: String; var AValue: String): Boolean;
function GetRealShellFolder(const Common: Boolean; const ID: TShellFolderID;
ReadOnly: Boolean): String;
function GetShellFolder(Common: Boolean; const ID: TShellFolderID;
ReadOnly: Boolean): String;
function GetShellFolderByCSIDL(Folder: Integer; const Create: Boolean): String;
function GetUninstallRegKeyBaseName(const ExpandedAppId: String): String;
function GetPreviousData(const ExpandedAppID, ValueName, DefaultValueData: String): String;
procedure Initialize64BitInstallMode(const A64Bit: Boolean);
procedure InitializeCommonVars;
procedure InitializeSetup;
procedure InitMainNonSHFolderConsts;
function InstallOnThisVersion(const MinVersion: TSetupVersionData;
const OnlyBelowVersion: TSetupVersionData): TInstallOnThisVersionResult;
function IsRecurseableDirectory(const FindData: TWin32FindData): Boolean;
procedure LoadSHFolderDLL;
function LoggedAppMessageBox(const Text, Caption: PChar; const Flags: Longint;
const Suppressible: Boolean; const Default: Integer): Integer;
function LoggedMsgBox(const Text, Caption: String; const Typ: TMsgBoxType;
const Buttons: Cardinal; const Suppressible: Boolean; const Default: Integer): Integer;
procedure LogWindowsVersion;
procedure NotifyAfterInstallEntry(const AfterInstall: String);
procedure NotifyAfterInstallFileEntry(const FileEntry: PSetupFileEntry);
procedure NotifyBeforeInstallEntry(const BeforeInstall: String);
procedure NotifyBeforeInstallFileEntry(const FileEntry: PSetupFileEntry);
function PreviousInstallCompleted(const WizardComponents, WizardTasks: TStringList): Boolean;
function CodeRegisterExtraCloseApplicationsResource(const DisableFsRedir: Boolean; const AFilename: String): Boolean;
procedure RegisterResourcesWithRestartManager(const WizardComponents, WizardTasks: TStringList);
procedure RemoveTempInstallDir;
procedure SaveResourceToTempFile(const ResName, Filename: String);
procedure SetActiveLanguage(const I: Integer);
procedure SetTaskbarButtonVisibility(const AVisible: Boolean);
function ShouldDisableFsRedirForFileEntry(const FileEntry: PSetupFileEntry): Boolean;
function ShouldDisableFsRedirForRunEntry(const RunEntry: PSetupRunEntry): Boolean;
function EvalDirectiveCheck(const Expression: String): Boolean;
function ShouldProcessEntry(const WizardComponents, WizardTasks: TStringList;
const Components, Tasks, Languages, Check: String): Boolean;
function ShouldProcessFileEntry(const WizardComponents, WizardTasks: TStringList;
const FileEntry: PSetupFileEntry; const IgnoreCheck: Boolean): Boolean;
function ShouldProcessIconEntry(const WizardComponents, WizardTasks: TStringList;
const WizardNoIcons: Boolean; const IconEntry: PSetupIconEntry): Boolean;
function ShouldProcessRunEntry(const WizardComponents, WizardTasks: TStringList;
const RunEntry: PSetupRunEntry): Boolean;
function TestPassword(const Password: String): Boolean;
procedure UnloadSHFolderDLL;
function WindowsVersionAtLeast(const AMajor, AMinor: Byte): Boolean;
implementation
uses
ShellAPI, ShlObj,
Msgs, MsgIDs, Install, InstFunc, InstFnc2, RedirFunc, PathFunc,
Compress, CompressZlib, bzlib, LZMADecomp, ArcFour, SetupEnt, SelLangForm,
Wizard, DebugClient, VerInfo, Extract, FileClass, Logging, MD5, SHA1,
{$IFNDEF Delphi3orHigher} OLE2, {$ELSE} ActiveX, {$ENDIF}
SimpleExpression, Helper, SpawnClient, SpawnServer, LibFusion, BitmapImage;
{$R *.DFM}
var
ShellFolders: array[Boolean, TShellFolderID] of String;
ShellFoldersRead: array[Boolean, TShellFolderID] of Boolean;
SHFolderDLLHandle: HMODULE;
SHGetFolderPathFunc: function(hwndOwner: HWND; nFolder: Integer;
hToken: THandle; dwFlags: DWORD; pszPath: PChar): HRESULT; stdcall;
SHGetKnownFolderPathFunc: function(const rfid: TGUID; dwFlags: DWORD; hToken: THandle;
var ppszPath: PWideChar): HRESULT; stdcall;
DecompressorDLLHandle: HMODULE;
DecryptDLLHandle: HMODULE;
type
TDummyClass = class
public
class function ExpandCheckOrInstallConstant(Sender: TSimpleExpression;
const Constant: String): String;
class function EvalInstallIdentifier(Sender: TSimpleExpression;
const Name: String; const Parameters: array of const): Boolean;
class function EvalComponentOrTaskIdentifier(Sender: TSimpleExpression;
const Name: String; const Parameters: array of const): Boolean;
class function EvalLanguageIdentifier(Sender: TSimpleExpression;
const Name: String; const Parameters: array of const): Boolean;
class function EvalCheckIdentifier(Sender: TSimpleExpression;
const Name: String; const Parameters: array of const): Boolean;
end;
{ Misc. functions }
function WindowsVersionAtLeast(const AMajor, AMinor: Byte): Boolean;
begin
Result := (WindowsVersion >= Cardinal((AMajor shl 24) or (AMinor shl 16)));
end;
function GetUninstallRegKeyBaseName(const ExpandedAppId: String): String;
{$IFDEF UNICODE}
var
UseAnsiCRC32: Boolean;
S: AnsiString;
I: Integer;
{$ENDIF}
begin
{ Set uninstall registry key base name }
Result := ExpandedAppId;
{ Uninstall registry keys can only be up to 63 characters, otherwise Win95
ignores them. Limit to 57 since Setup will add _isXXX to the end later. }
if Length(Result) > 57 then begin
{ Only keep the first 48 characters, then add an tilde and the CRC
of the original string (to make the trimmed string unique). The
resulting string is 57 characters long. On Unicode, only do this if we
can get a CRC32 compatible with ANSI versions, else there's no point
in shortening since Unicode doesn't run on Win95. }
{$IFDEF UNICODE}
UseAnsiCRC32 := True;
for I := 1 to Length(Result) do begin
if Ord(Result[I]) > 126 then begin
UseAnsiCRC32 := False;
Break;
end;
end;
if UseAnsiCRC32 then begin
S := AnsiString(Result);
FmtStr(Result, '%.48s~%.8x', [Result, GetCRC32(S[1], Length(S)*SizeOf(S[1]))]);
end;
{$ELSE}
FmtStr(Result, '%.48s~%.8x', [Result, GetCRC32(Result[1], Length(Result)*SizeOf(Result[1]))]);
{$ENDIF}
end;
end;
{ Based on FindPreviousData in Wizard.pas }
function GetPreviousData(const ExpandedAppID, ValueName, DefaultValueData: String): String;
const
RootKeys: array[0..1] of HKEY = (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE);
var
I: Integer;
H: HKEY;
UninstallRegKeyBaseName: String;
begin
Result := DefaultValueData;
if ExpandedAppId <> '' then begin
UninstallRegKeyBaseName := GetUninstallRegKeyBaseName(ExpandedAppId);
for I := 0 to 1 do begin
if RegOpenKeyExView(InstallDefaultRegView, RootKeys[I],
PChar(Format('%s\%s_is1', [NEWREGSTR_PATH_UNINSTALL, UninstallRegKeyBaseName])),
0, KEY_QUERY_VALUE, H) = ERROR_SUCCESS then begin
try
RegQueryStringValue (H, PChar(ValueName), Result);
finally
RegCloseKey (H);
end;
Break;
end;
end;
end;
end;
function TestPassword(const Password: String): Boolean;
var
Context: TSHA1Context;
Hash: TSHA1Digest;
begin
SHA1Init(Context);
SHA1Update(Context, PAnsiChar('PasswordCheckHash')^, Length('PasswordCheckHash'));
SHA1Update(Context, SetupHeader.PasswordSalt, SizeOf(SetupHeader.PasswordSalt));
SHA1Update(Context, Pointer(Password)^, Length(Password)*SizeOf(Password[1]));
Hash := SHA1Final(Context);
Result := SHA1DigestsEqual(Hash, SetupHeader.PasswordHash);
end;
class function TDummyClass.ExpandCheckOrInstallConstant(Sender: TSimpleExpression;
const Constant: String): String;
begin
Result := ExpandConst(Constant);
end;
class function TDummyClass.EvalInstallIdentifier(Sender: TSimpleExpression;
const Name: String; const Parameters: array of const): Boolean;
begin
CodeRunner.RunProcedure(AnsiString(Name), Parameters, True);
Result := True; { Result doesn't matter }
end;
procedure NotifyInstallEntry(const Install: String);
procedure EvalInstall(const Expression: String);
var
SimpleExpression: TSimpleExpression;
begin
try
SimpleExpression := TSimpleExpression.Create;
try
SimpleExpression.Expression := Expression;
SimpleExpression.OnEvalIdentifier := TDummyClass.EvalInstallIdentifier;
SimpleExpression.OnExpandConstant := TDummyClass.ExpandCheckOrInstallConstant;
SimpleExpression.ParametersAllowed := True;
SimpleExpression.SingleIdentifierMode := True;
SimpleExpression.Eval;
finally
SimpleExpression.Free;
end;
except
InternalError(Format('Expression error ''%s''', [GetExceptMessage]));
end;
end;
begin
if Install <> '' then begin
try
if CodeRunner = nil then
InternalError('"BeforeInstall" or "AfterInstall" parameter with no CodeRunner');
EvalInstall(Install);
except
{ Don't allow exceptions raised by Before/AfterInstall functions to be propagated out }
Application.HandleException(nil);
end;
end;
end;
procedure NotifyBeforeInstallEntry(const BeforeInstall: String);
begin
NotifyInstallEntry(BeforeInstall);
end;
procedure NotifyBeforeInstallFileEntry(const FileEntry: PSetupFileEntry);
begin
CheckOrInstallCurrentFilename := FileEntry.DestName;
CheckOrInstallCurrentSourceFilename := FileEntry.SourceFilename;
NotifyInstallEntry(FileEntry.BeforeInstall);
CheckOrInstallCurrentFilename := '';
CheckOrInstallCurrentSourceFilename := '';
end;
procedure NotifyAfterInstallEntry(const AfterInstall: String);
begin
NotifyInstallEntry(AfterInstall);
end;
procedure NotifyAfterInstallFileEntry(const FileEntry: PSetupFileEntry);
begin
CheckOrInstallCurrentFilename := FileEntry.DestName;
CheckOrInstallCurrentSourceFilename := FileEntry.SourceFilename;
NotifyInstallEntry(FileEntry.AfterInstall);
CheckOrInstallCurrentFilename := '';
CheckOrInstallCurrentSourceFilename := '';
end;
class function TDummyClass.EvalComponentOrTaskIdentifier(Sender: TSimpleExpression;
const Name: String; const Parameters: array of const): Boolean;
var
WizardItems: TStringList;
begin
WizardItems := TStringList(Sender.Tag);
Result := ListContains(WizardItems, Name);
end;
class function TDummyClass.EvalLanguageIdentifier(Sender: TSimpleExpression;
const Name: String; const Parameters: array of const): Boolean;
begin
Result := CompareText(PSetupLanguageEntry(Entries[seLanguage][ActiveLanguage]).Name, Name) = 0;
end;
class function TDummyClass.EvalCheckIdentifier(Sender: TSimpleExpression;
const Name: String; const Parameters: array of const): Boolean;
begin
Result := CodeRunner.RunBooleanFunction(AnsiString(Name), Parameters, True, False);
end;
function EvalCheck(const Expression: String): Boolean;
var
SimpleExpression: TSimpleExpression;
begin
try
SimpleExpression := TSimpleExpression.Create;
try
SimpleExpression.Lazy := True;
SimpleExpression.Expression := Expression;
SimpleExpression.OnEvalIdentifier := TDummyClass.EvalCheckIdentifier;
SimpleExpression.OnExpandConstant := TDummyClass.ExpandCheckOrInstallConstant;
SimpleExpression.ParametersAllowed := True;
SimpleExpression.SilentOrAllowed := False;
SimpleExpression.SingleIdentifierMode := False;
Result := SimpleExpression.Eval;
finally
SimpleExpression.Free;
end;
except
InternalError(Format('Expression error ''%s''', [GetExceptMessage]));
Result := False;
end;
end;
function EvalDirectiveCheck(const Expression: String): Boolean;
begin
if not TryStrToBoolean(Expression, Result) then
Result := EvalCheck(Expression);
end;
function ShouldProcessEntry(const WizardComponents, WizardTasks: TStringList;
const Components, Tasks, Languages, Check: String): Boolean;
function EvalExpression(const Expression: String;
OnEvalIdentifier: TSimpleExpressionOnEvalIdentifier; Tag: LongInt): Boolean;
var
SimpleExpression: TSimpleExpression;
begin
try
SimpleExpression := TSimpleExpression.Create;
try
SimpleExpression.Lazy := True;
SimpleExpression.Expression := Expression;
SimpleExpression.OnEvalIdentifier := OnEvalIdentifier;
SimpleExpression.ParametersAllowed := False;
SimpleExpression.SilentOrAllowed := True;
SimpleExpression.SingleIdentifierMode := False;
SimpleExpression.Tag := Tag;
Result := SimpleExpression.Eval;
finally
SimpleExpression.Free;
end;
except
InternalError(Format('Expression error ''%s''', [GetExceptMessage]));
Result := False;
end;
end;
var
ProcessComponent, ProcessTask, ProcessLanguage: Boolean;
begin
if (Components <> '') or (Tasks <> '') or (Languages <> '') or (Check <> '') then begin
if (Components <> '') and (WizardComponents <> nil) then
ProcessComponent := EvalExpression(Components, TDummyClass.EvalComponentOrTaskIdentifier, LongInt(WizardComponents))
else
ProcessComponent := True;
if (Tasks <> '') and (WizardTasks <> nil) then
ProcessTask := EvalExpression(Tasks, TDummyClass.EvalComponentOrTaskIdentifier, LongInt(WizardTasks))
else
ProcessTask := True;
if Languages <> '' then
ProcessLanguage := EvalExpression(Languages, TDummyClass.EvalLanguageIdentifier, 0)
else
ProcessLanguage := True;
Result := ProcessComponent and ProcessTask and ProcessLanguage;
if Result and (Check <> '') then begin
try
if CodeRunner = nil then
InternalError('"Check" parameter with no CodeRunner');
Result := EvalCheck(Check);
except
{ Don't allow exceptions raised by Check functions to be propagated out }
Application.HandleException(nil);
Result := False;
end;
end;
end else
Result := True;
end;
function ShouldProcessFileEntry(const WizardComponents, WizardTasks: TStringList;
const FileEntry: PSetupFileEntry; const IgnoreCheck: Boolean): Boolean;
begin
if foDontCopy in FileEntry.Options then begin
Result := False;
Exit;
end;
CheckOrInstallCurrentFilename := FileEntry.DestName;
CheckOrInstallCurrentSourceFilename := FileEntry.SourceFilename;
if IgnoreCheck then
Result := ShouldProcessEntry(WizardComponents, WizardTasks, FileEntry.Components, FileEntry.Tasks, FileEntry.Languages, '')
else
Result := ShouldProcessEntry(WizardComponents, WizardTasks, FileEntry.Components, FileEntry.Tasks, FileEntry.Languages, FileEntry.Check);
CheckOrInstallCurrentFilename := '';
CheckOrInstallCurrentSourceFilename := '';
end;
function ShouldProcessRunEntry(const WizardComponents, WizardTasks: TStringList;
const RunEntry: PSetupRunEntry): Boolean;
begin
if (InstallMode <> imNormal) and (roSkipIfSilent in RunEntry.Options) then
Result := False
else if (InstallMode = imNormal) and (roSkipIfNotSilent in RunEntry.Options) then
Result := False
else
Result := ShouldProcessEntry(WizardComponents, WizardTasks, RunEntry.Components, RunEntry.Tasks, RunEntry.Languages, RunEntry.Check);
end;
function ShouldProcessIconEntry(const WizardComponents, WizardTasks: TStringList;
const WizardNoIcons: Boolean; const IconEntry: PSetupIconEntry): Boolean;
begin
if WizardNoIcons and (IconEntry.Tasks = '') and
(Copy(IconEntry.IconName, 1, 8) = '{group}\') then
Result := False
else
Result := ShouldProcessEntry(WizardComponents, WizardTasks, IconEntry.Components, IconEntry.Tasks, IconEntry.Languages, IconEntry.Check);
end;
function ShouldDisableFsRedirForFileEntry(const FileEntry: PSetupFileEntry): Boolean;
begin
Result := InstallDefaultDisableFsRedir;
if fo32Bit in FileEntry.Options then
Result := False;
if fo64Bit in FileEntry.Options then begin
if not IsWin64 then
InternalError('Cannot install files to 64-bit locations on this version of Windows');
Result := True;
end;
end;
function SlashesToBackslashes(const S: String): String;
var
I: Integer;
begin
Result := S;
for I := 1 to Length(Result) do
if Result[I] = '/' then
Result[I] := '\';
end;
procedure LoadInf(const FileName: String; var WantToSuppressMsgBoxes: Boolean);
const
Section = 'Setup';
var
S: String;
begin
//saved infs
InitLang := GetIniString(Section, 'Lang', InitLang, FileName);
InitDir := GetIniString(Section, 'Dir', InitDir, FileName);
InitProgramGroup := GetIniString(Section, 'Group', InitProgramGroup, FileName);
InitNoIcons := GetIniBool(Section, 'NoIcons', InitNoIcons, FileName);
InitSetupType := GetIniString(Section, 'SetupType', InitSetupType, FileName);
S := GetIniString(Section, 'Components', '$', FileName);
if S <> '$' then begin
InitComponentsSpecified := True;
SetStringsFromCommaString(InitComponents, SlashesToBackslashes(S));
end;
S := GetIniString(Section, 'Tasks', '$', FileName);
if S <> '$' then begin
InitDeselectAllTasks := True;
SetStringsFromCommaString(InitTasks, SlashesToBackslashes(S));
end;
//non saved infs (=non user settable)
InitSilent := GetIniBool(Section, 'Silent', InitSilent, FileName);
InitVerySilent := GetIniBool(Section, 'VerySilent', InitVerySilent, FileName);
InitNoRestart := GetIniBool(Section, 'NoRestart', InitNoRestart, FileName);
InitCloseApplications := GetIniBool(Section, 'CloseApplications', InitCloseApplications, FileName);
InitNoCloseApplications := GetIniBool(Section, 'NoCloseApplications', InitNoCloseApplications, FileName);
InitForceCloseApplications := GetIniBool(Section, 'ForceCloseApplications', InitForceCloseApplications, FileName);
InitNoForceCloseApplications := GetIniBool(Section, 'NoForceCloseApplications', InitNoForceCloseApplications, FileName);
InitRestartApplications := GetIniBool(Section, 'RestartApplications', InitRestartApplications, FileName);
InitNoRestartApplications := GetIniBool(Section, 'NoRestartApplications', InitNoRestartApplications, FileName);
InitNoCancel := GetIniBool(Section, 'NoCancel', InitNoCancel, FileName);
InitPassword := GetIniString(Section, 'Password', InitPassword, FileName);
InitRestartExitCode := GetIniInt(Section, 'RestartExitCode', InitRestartExitCode, 0, 0, FileName);
WantToSuppressMsgBoxes := GetIniBool(Section, 'SuppressMsgBoxes', WantToSuppressMsgBoxes, FileName);
InitSaveInf := GetIniString(Section, 'SaveInf', InitSaveInf, FileName);
end;
procedure SaveInf(const FileName: String);
const
Section = 'Setup';
begin
SetIniString(Section, 'Lang',
PSetupLanguageEntry(Entries[seLanguage][ActiveLanguage]).Name, FileName);
SetIniString(Section, 'Dir', WizardDirValue, FileName);
SetIniString(Section, 'Group', WizardGroupValue, FileName);
SetIniBool(Section, 'NoIcons', WizardNoIcons, FileName);
if WizardSetupType <> nil then begin
SetIniString(Section, 'SetupType', WizardSetupType.Name, FileName);
SetIniString(Section, 'Components', StringsToCommaString(WizardComponents), FileName);
end
else begin
DeleteIniEntry(Section, 'SetupType', FileName);
DeleteIniEntry(Section, 'Components', FileName);
end;
SetIniString(Section, 'Tasks', StringsToCommaString(WizardTasks), FileName);
end;
function GetCustomMessageValue(const AName: String; var AValue: String): Boolean;
var
I: Integer;
begin
Result := False;
for I := 0 to Entries[seCustomMessage].Count-1 do begin
with PSetupCustomMessageEntry(Entries[seCustomMessage][I])^ do begin
if (CompareText(Name, AName) = 0) and
((LangIndex = -1) or (LangIndex = ActiveLanguage)) then begin
Result := True;
AValue := Value;
{ don't stop looping, last item counts }
end;
end;
end;
end;
function ExpandIndividualConst(const Cnst: String;
const CustomConsts: array of String): String;
{ Cnst must be the name of a single constant, without the braces.
For example: app
IsPath is set to True if the result is a path which needs special trailing-
backslash handling. }
procedure NoUninstallConstError(const C: String);
begin
InternalError(Format('Cannot evaluate "%s" constant during Uninstall', [C]));
end;
function ExpandEnvConst(C: String): String;
var
I: Integer;
VarName, Default: String;
begin
Delete(C, 1, 1);
I := ConstPos('|', C); { check for 'default' value }
if I = 0 then
I := Length(C)+1;
VarName := Copy(C, 1, I-1);
Default := Copy(C, I+1, Maxint);
Result := '';
if ConvertConstPercentStr(VarName) and ConvertConstPercentStr(Default) then begin
Result := GetEnv(ExpandConstEx(VarName, CustomConsts));
if Result = '' then
Result := ExpandConstEx(Default, CustomConsts);
end;
end;
function ExpandRegConst(C: String): String;
{ Expands a registry-value constant in the form:
reg:HKxx\SubkeyName,ValueName|DefaultValue }
type
TKeyNameConst = packed record
KeyName: String;
KeyConst: HKEY;
end;
const
KeyNameConsts: array[0..4] of TKeyNameConst = (
(KeyName: 'HKCR'; KeyConst: HKEY_CLASSES_ROOT),
(KeyName: 'HKCU'; KeyConst: HKEY_CURRENT_USER),
(KeyName: 'HKLM'; KeyConst: HKEY_LOCAL_MACHINE),
(KeyName: 'HKU'; KeyConst: HKEY_USERS),
(KeyName: 'HKCC'; KeyConst: HKEY_CURRENT_CONFIG));
var
Z, Subkey, Value, Default: String;
I, J, L: Integer;
RegView: TRegView;
RootKey: HKEY;
K: HKEY;
begin
Delete(C, 1, 4); { skip past 'reg:' }
I := ConstPos('\', C);
if I <> 0 then begin
Z := Copy(C, 1, I-1);
if Z <> '' then begin
RegView := InstallDefaultRegView;
L := Length(Z);
if L >= 2 then begin
{ Check for '32' or '64' suffix }
if (Z[L-1] = '3') and (Z[L] = '2') then begin
RegView := rv32Bit;
SetLength(Z, L-2);
end
else if (Z[L-1] = '6') and (Z[L] = '4') then begin
if not IsWin64 then
InternalError('Cannot access a 64-bit key in a "reg" constant on this version of Windows');
RegView := rv64Bit;
SetLength(Z, L-2);
end;
end;
RootKey := 0;
for J := Low(KeyNameConsts) to High(KeyNameConsts) do
if CompareText(KeyNameConsts[J].KeyName, Z) = 0 then begin
RootKey := KeyNameConsts[J].KeyConst;
Break;
end;
if RootKey <> 0 then begin
Z := Copy(C, I+1, Maxint);
I := ConstPos('|', Z); { check for a 'default' data }
if I = 0 then
I := Length(Z)+1;
Default := Copy(Z, I+1, Maxint);
SetLength(Z, I-1);
I := ConstPos(',', Z); { comma separates subkey and value }
if I <> 0 then begin
Subkey := Copy(Z, 1, I-1);
Value := Copy(Z, I+1, Maxint);
if ConvertConstPercentStr(Subkey) and ConvertConstPercentStr(Value) and
ConvertConstPercentStr(Default) then begin
Result := ExpandConstEx(Default, CustomConsts);
if RegOpenKeyExView(RegView, RootKey,
PChar(ExpandConstEx(Subkey, CustomConsts)),
0, KEY_QUERY_VALUE, K) = ERROR_SUCCESS then begin
RegQueryStringValue(K, PChar(ExpandConstEx(Value, CustomConsts)),
Result);
RegCloseKey(K);
end;
Exit;
end;
end;
end;
end;
end;
{ it will only reach here if there was a parsing error }
InternalError('Failed to parse "reg" constant');
end;
function ExpandIniConst(C: String): String;
{ Expands an INI-value constant in the form:
filename,section,key|defaultvalue }
var
Z, Filename, Section, Key, Default: String;
I: Integer;
begin
Delete(C, 1, 4); { skip past 'ini:' }
I := ConstPos(',', C);
if I <> 0 then begin
Z := Copy(C, 1, I-1);
if Z <> '' then begin
Filename := Z;
Z := Copy(C, I+1, Maxint);
I := ConstPos('|', Z); { check for a 'default' data }
if I = 0 then
I := Length(Z)+1;
Default := Copy(Z, I+1, Maxint);
SetLength(Z, I-1);
I := ConstPos(',', Z); { comma separates section and key }
if I <> 0 then begin
Section := Copy(Z, 1, I-1);
Key := Copy(Z, I+1, Maxint);
if ConvertConstPercentStr(Filename) and ConvertConstPercentStr(Section) and ConvertConstPercentStr(Key) and
ConvertConstPercentStr(Default) then begin
Filename := ExpandConstEx(Filename, CustomConsts);
Section := ExpandConstEx(Section, CustomConsts);
Key := ExpandConstEx(Key, CustomConsts);
Default := ExpandConstEx(Default, CustomConsts);
Result := GetIniString(Section, Key, Default, Filename);
Exit;
end;
end;
end;
end;
{ it will only reach here if there was a parsing error }
InternalError('Failed to parse "ini" constant');
end;
function ExpandParamConst(C: String): String;
{ Expands an commandline-parameter-value constant in the form:
parametername|defaultvalue }
function GetParamString(const Param, Default: String): String;
var
I, PCount: Integer;
Z: String;
begin
PCount := NewParamCount();
for I := 1 to PCount do begin
Z := NewParamStr(I);
if StrLIComp(PChar(Z), PChar('/'+Param+'='), Length(Param)+2) = 0 then begin
Delete(Z, 1, Length(Param)+2);
Result := Z;
Exit;
end;
end;
Result := Default;
end;
var
Z, Param, Default: String;
I: Integer;
begin
Delete(C, 1, 6); { skip past 'param:' }
Z := C;
I := ConstPos('|', Z); { check for a 'default' data }
if I = 0 then
I := Length(Z)+1;
Default := Copy(Z, I+1, Maxint);
SetLength(Z, I-1);
Param := Z;
if ConvertConstPercentStr(Param) and ConvertConstPercentStr(Default) then begin
Param := ExpandConstEx(Param, CustomConsts);
Default := ExpandConstEx(Default, CustomConsts);
Result := GetParamString(Param, Default);
Exit;
end;
{ it will only reach here if there was a parsing error }
InternalError('Failed to parse "param" constant');
end;
function ExpandCodeConst(C: String): String;
{ Expands an Pascal-script-value constant in the form:
parametername|defaultvalue }
function GetCodeString(const ScriptFunc, Default: String): String;
begin
if (CodeRunner <> nil) then
Result := CodeRunner.RunStringFunction(AnsiString(ScriptFunc), [Default], True, Default)
else begin
InternalError('"code" constant with no CodeRunner');
Result := '';
end;
end;
var
Z, ScriptFunc, Default: String;
I: Integer;
begin
if DisableCodeConsts <> 0 then
raise Exception.Create('Cannot evaluate "code" constant because of possible side effects');
Delete(C, 1, 5); { skip past 'code:' }
Z := C;
I := ConstPos('|', Z); { check for a 'default' data }
if I = 0 then
I := Length(Z)+1;
Default := Copy(Z, I+1, Maxint);
SetLength(Z, I-1);
ScriptFunc := Z;
if ConvertConstPercentStr(ScriptFunc) and ConvertConstPercentStr(Default) then begin
Default := ExpandConstEx(Default, CustomConsts);
Result := GetCodeString(ScriptFunc, Default);
Exit;
end;
{ it will only reach here if there was a parsing error }
InternalError('Failed to parse "code" constant');
end;
function ExpandDriveConst(C: String): String;
begin
Delete(C, 1, 6); { skip past 'drive:' }
if ConvertConstPercentStr(C) then begin
Result := PathExtractDrive(ExpandConstEx(C, CustomConsts));
Exit;
end;
{ it will only reach here if there was a parsing error }
InternalError('Failed to parse "drive" constant');
end;
function ExpandCustomMessageConst(C: String): String;
var
I, ArgCount: Integer;
MsgName: String;
ArgValues: array[0..8] of String; { %1 through %9 }
begin
Delete(C, 1, 3); { skip past 'cm:' }
I := ConstPos(',', C);
if I = 0 then
MsgName := C
else
MsgName := Copy(C, 1, I-1);
{ Prepare arguments. Excess arguments are ignored. }
ArgCount := 0;
while (I > 0) and (ArgCount <= High(ArgValues)) do begin
Delete(C, 1, I);
I := ConstPos(',', C);
if I = 0 then
ArgValues[ArgCount] := C
else
ArgValues[ArgCount] := Copy(C, 1, I-1);
if not ConvertConstPercentStr(ArgValues[ArgCount]) then
InternalError('Failed to parse "cm" constant');
ArgValues[ArgCount] := ExpandConstEx(ArgValues[ArgCount], CustomConsts);
Inc(ArgCount);
end;
{ Look up the message value }
if not GetCustomMessageValue(MsgName, Result) then
InternalError(Format('Unknown custom message name "%s" in "cm" constant', [MsgName]));
{ Expand the message }
Result := FmtMessage(PChar(Result), Slice(ArgValues, ArgCount));
end;
const
FolderConsts: array[Boolean, TShellFolderID] of String =
(('userdesktop', 'userstartmenu', 'userprograms', 'userstartup',
'sendto', 'fonts', 'userappdata', 'userdocs', 'usertemplates',
'userfavorites', 'localappdata'),
('commondesktop', 'commonstartmenu', 'commonprograms', 'commonstartup',
'sendto', 'fonts', 'commonappdata', 'commondocs', 'commontemplates',
'commonfavorites', 'localappdata'));
NoUninstallConsts: array[0..6] of String =
('src', 'srcexe', 'userinfoname', 'userinfoorg', 'userinfoserial', 'hwnd',
'wizardhwnd');
var
Z: String;
B: Boolean;
SF: TShellFolderID;
K: Integer;
begin
if IsUninstaller then
for K := Low(NoUninstallConsts) to High(NoUninstallConsts) do
if NoUninstallConsts[K] = Cnst then
NoUninstallConstError(NoUninstallConsts[K]);
if Cnst = '\' then Result := '\'
else if Cnst = 'app' then begin
if IsUninstaller then begin
if UninstallExpandedApp = '' then
InternalError('An attempt was made to expand the "app" constant but Setup didn''t create the "app" dir');
Result := UninstallExpandedApp;
end else begin