-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuFileAssociations.pas
More file actions
203 lines (165 loc) · 5.72 KB
/
uFileAssociations.pas
File metadata and controls
203 lines (165 loc) · 5.72 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
{
AE BDS Launcher © 2022 by Akos Eigler is licensed under CC BY 4.0.
To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt,
and build upon the material in any medium or format, even for commercial purposes.
}
Unit uFileAssociations;
Interface
Const
KNOWNEXTENSIONS : Array[0..9] Of String = ('.pas', '.dpr', '.dproj', '.dfm', '.groupproj', '.dpk', '.bdsproj', '.dpkw', '.fmx', '.bdsgroup');
Procedure GiveBackFileAssociations;
Procedure TakeOverFileAssociations;
Implementation
Uses Win.Registry, WinApi.Windows, WinApi.ShlObj, System.SysUtils;
Const
CLASSESROOT = '\SOFTWARE\Classes\';
AEBDSLAUNCHERROOT = CLASSESROOT + 'AEBDSLauncher';
BDSLAUNCHERBACKUP = 'AEBDSLauncherBackup';
Procedure NotifyAssociationsChange;
Begin
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
End;
//
// Giving back file associations
//
Procedure InternalGiveBackFileAssociations(Const inRegistry: TRegistry; Const inPath: String);
Var
rootkeyfound, delkey: Boolean;
Begin
// Revert taken over associations back to their previous ones (if there were any). If there was no association before taking over
// we have to delete the keys, but reverting twice in a row should not do anything!
delkey := False;
rootkeyfound := inRegistry.KeyExists(AEBDSLAUNCHERROOT + inPath);
// If our key exists, delete it
If rootkeyfound Then
inRegistry.DeleteKey(AEBDSLAUNCHERROOT + inPath);
// Does this association exist at the moment?
If inRegistry.OpenKey(CLASSESROOT + inPath, False) Then
Try
// Is the file extension currently associated to AE BDSLauncher?
If inRegistry.ReadString('') = 'AEBDSLauncher' + inPath Then
Begin
If inRegistry.ValueExists(BDSLAUNCHERBACKUP) Then
Begin
inRegistry.WriteString('', inRegistry.ReadString(BDSLAUNCHERBACKUP));
// If backup value exists there was an association for sure, therefore the association must not be deleted, no matter what
delkey := False;
End
Else
// No backup key was found. This can mean there was no previous association OR we are reverting from a non-taken-over
// state. Therefore, association only has to be deleted if our key was found!
delkey := rootkeyfound;
End;
If inRegistry.ValueExists(BDSLAUNCHERBACKUP) Then
inRegistry.DeleteValue(BDSLAUNCHERBACKUP);
Finally
inRegistry.CloseKey;
End;
If delkey And inRegistry.KeyExists(CLASSESROOT + inPath) Then
inRegistry.DeleteKey(CLASSESROOT + inPath);
End;
Procedure GiveBackFileAssociations;
Var
reg: TRegistry;
extension: String;
Begin
reg := TRegistry.Create;
Try
reg.RootKey := HKEY_CURRENT_USER;
For extension In KNOWNEXTENSIONS Do
InternalGiveBackFileAssociations(reg, extension);
NotifyAssociationsChange;
Finally
FreeAndNil(reg);
End;
End;
//
// Taking over file associations
//
Procedure InternalTakeOverFileAssociations(Const inRegistry: TRegistry; Const inPath: String);
Var
oldtarget, description, friendlytname, icon: String;
Begin
// There are only two things we should consider here: taking over the second time should have zero effect whatsoever
// and maybe there are no associations for this file type yet.
// Open the current file extension assignment, back up the old association and change it to the new one
If inRegistry.OpenKey(CLASSESROOT + inPath, True) Then
Try
oldtarget := '';
If Not inRegistry.ValueExists('') Or (inRegistry.ReadString('') <> 'AEBDSLauncher' + inPath) Then
Begin
If inRegistry.ValueExists('') Then
Begin
oldtarget := inRegistry.ReadString('');
inRegistry.WriteString(BDSLAUNCHERBACKUP, oldtarget);
End;
inRegistry.WriteString('', 'AEBDSLauncher' + inPath);
End;
Finally
inRegistry.CloseKey;
End;
If inRegistry.KeyExists(AEBDSLAUNCHERROOT + inPath) Then
Exit;
// Look for the old association and extract description, friendly type name and icon to use
description := '';
friendlytname := '';
If inRegistry.OpenKey(CLASSESROOT + oldtarget, False) Then
Try
If inRegistry.ValueExists('') Then
description := inRegistry.ReadString('');
If inRegistry.ValueExists('FriendlyTypeName') Then
friendlytname := inRegistry.ReadString('FriendlyTypeName');
Finally
inRegistry.CloseKey;
End;
icon := '';
If inRegistry.OpenKey(CLASSESROOT + oldtarget + '\DefaultIcon', False) Then
Try
If inRegistry.ValueExists('') Then
icon := inRegistry.ReadString('');
Finally
inRegistry.CloseKey;
End;
// Now, create our own association with the previously collected information
If description.IsEmpty Then
description := 'AE BDSLauncher file';
If inRegistry.OpenKey(AEBDSLAUNCHERROOT + inPath, True) Then
Try
inRegistry.WriteString('', description);
If Not friendlytname.IsEmpty Then
inRegistry.WriteString('FriendlyTypeName', friendlytname);
Finally
inRegistry.CloseKey;
End;
If inRegistry.OpenKey(AEBDSLAUNCHERROOT + inPath + '\Shell\Open\Command', True) Then
Try
inRegistry.WriteString('', ParamStr(0) + ' "%1"');
Finally
inRegistry.CloseKey;
End;
If icon.IsEmpty Then
icon := ParamStr(0);
If inRegistry.OpenKey(AEBDSLAUNCHERROOT + inPath + '\DefaultIcon', True) Then
Try
inRegistry.WriteString('', icon);
Finally
inRegistry.CloseKey;
End;
End;
Procedure TakeOverFileAssociations;
Var
reg: TRegistry;
extension: String;
Begin
reg := TRegistry.Create;
Try
reg.RootKey := HKEY_CURRENT_USER;
For extension In KNOWNEXTENSIONS Do
InternalTakeOverFileAssociations(reg, extension);
NotifyAssociationsChange;
Finally
FreeAndNil(reg);
End;
End;
End.