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
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Dependency
{
#if TODO_CONSIDER_DECOMPILER
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using WixToolset;
using WixToolset.Data;
using WixToolset.Extensibility;
using WixToolset.Extensions.Serialize.Dependency;
using Dependency = WixToolset.Extensions.Serialize.Dependency;
using Wix = WixToolset.Data.Serialize;
/// <summary>
/// The decompiler for the WiX toolset dependency extension.
/// </summary>
public sealed class DependencyDecompiler : DecompilerExtension
{
private RegistryKeyValueCollection registryValues;
private Dictionary<string, string> keyCache;
/// <summary>
/// Creates a new instance of the <see cref="DependencyDecompiler"/> class.
/// </summary>
public DependencyDecompiler()
{
this.registryValues = new RegistryKeyValueCollection();
this.keyCache = new Dictionary<string, string>();
this.TableDefinitions = DependencyExtensionData.GetExtensionTableDefinitions();
}
/// <summary>
/// Get the extensions library to be removed.
/// </summary>
/// <param name="tableDefinitions">Table definitions for library.</param>
/// <returns>Library to remove from decompiled output.</returns>
public override Library GetLibraryToRemove(TableDefinitionCollection tableDefinitions)
{
return DependencyExtensionData.GetExtensionLibrary(tableDefinitions);
}
/// <summary>
/// Decompiles an extension table.
/// </summary>
/// <param name="table">The table to decompile.</param>
public override void DecompileTable(Table table)
{
switch (table.Name)
{
case "WixDependencyProvider":
this.DecompileWixDependencyProviderTable(table);
break;
case "WixDependency":
this.DecompileWixDependencyTable(table);
break;
case "WixDependencyRef":
this.DecompileWixDependencyRefTable(table);
break;
default:
base.DecompileTable(table);
break;
}
}
/// <summary>
/// Finalize decompilation by removing registry values that the compiler writes.
/// </summary>
/// <param name="tables">The collection of all tables.</param>
public override void Finish(TableIndexedCollection tables)
{
// Remove generated registry rows.
this.FinalizeRegistryTable(tables);
// Remove extension properties.
this.FinalizeProperties();
}
/// <summary>
/// Decompiles the WixDependencyProvider table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileWixDependencyProviderTable(Table table)
{
foreach (Row row in table.Rows)
{
Provides provides = new Provides();
provides.Id = (string)row[0];
provides.Key = (string)row[2];
if (null != row[3])
{
provides.Version = (string)row[3];
}
if (null != row[4])
{
provides.DisplayName = (string)row[4];
}
// Nothing to parse for attributes currently.
Wix.Component component = (Wix.Component)this.Core.GetIndexedElement("Component", (string)row[1]);
if (null != component)
{
component.AddChild(provides);
}
else
{
this.Core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter), "Component_", (string)row[1], "Component"));
}
// Index the provider to parent the RequiresRef elements.
this.Core.IndexElement(row, provides);
// Add the provider-specific registry keys to be removed during finalization.
// Only remove specific keys that the compiler writes.
string keyProvides = String.Concat(DependencyCommon.RegistryRoot, provides.Key);
this.registryValues.Add(keyProvides, null);
this.registryValues.Add(keyProvides, "Version");
this.registryValues.Add(keyProvides, "DisplayName");
this.registryValues.Add(keyProvides, "Attributes");
// Cache the provider key.
this.keyCache[provides.Id] = provides.Key;
}
}
/// <summary>
/// Decompiles the WixDependency table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileWixDependencyTable(Table table)
{
foreach (Row row in table.Rows)
{
Requires requires = new Requires();
requires.Id = (string)row[0];
requires.ProviderKey = (string)row[1];
if (null != row[2])
{
requires.Minimum = (string)row[2];
}
if (null != row[3])
{
requires.Maximum = (string)row[3];
}
if (null != row[4])
{
int attributes = (int)row[4];
if (0 != (attributes & DependencyCommon.RequiresAttributesMinVersionInclusive))
{
requires.IncludeMinimum = Dependency.YesNoType.yes;
}
if (0 != (attributes & DependencyCommon.RequiresAttributesMaxVersionInclusive))
{
requires.IncludeMaximum = Dependency.YesNoType.yes;
}
}
this.Core.RootElement.AddChild(requires);
// Cache the requires key.
this.keyCache[requires.Id] = requires.ProviderKey;
}
}
/// <summary>
/// Decompiles the WixDependencyRef table.
/// </summary>
/// <param name="table">The table to decompile.</param>
private void DecompileWixDependencyRefTable(Table table)
{
foreach (Row row in table.Rows)
{
RequiresRef requiresRef = new RequiresRef();
requiresRef.Id = (string)row[1];
Provides provides = (Provides)this.Core.GetIndexedElement("WixDependencyProvider", (string)row[0]);
if (null != provides)
{
provides.AddChild(requiresRef);
}
else
{
this.Core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter), "WixDependencyProvider_", (string)row[0], "WixDependencyProvider"));
}
// Get the cached keys for the provider and dependency IDs and generate registry rows.
string providesKey = null;
string requiresKey = null;
if (null != provides && this.keyCache.ContainsKey(provides.Id))
{
providesKey = this.keyCache[provides.Id];
}
else
{
this.Core.OnMessage(DependencyWarnings.ProvidesKeyNotFound(row.SourceLineNumbers, provides.Id));
}
if (this.keyCache.ContainsKey(requiresRef.Id))
{
requiresKey = this.keyCache[requiresRef.Id];
}
else
{
this.Core.OnMessage(DependencyWarnings.RequiresKeyNotFound(row.SourceLineNumbers, requiresRef.Id));
}
if (!this.Core.EncounteredError)
{
// Add the dependency-specific registry keys to be removed during finalization.
// Only remove specific keys that the compiler writes.
string keyRequires = String.Format(@"{0}{1}\{2}\{3}", DependencyCommon.RegistryRoot, requiresKey, DependencyCommon.RegistryDependents, providesKey);
this.registryValues.Add(keyRequires, "*");
this.registryValues.Add(keyRequires, "MinVersion");
this.registryValues.Add(keyRequires, "MaxVersion");
this.registryValues.Add(keyRequires, "Attributes");
}
}
}
/// <summary>
/// Removes rows from the Registry table that are generated by this extension.
/// </summary>
/// <param name="tables">The collection of tables.</param>
private void FinalizeRegistryTable(TableIndexedCollection tables)
{
Table registryTable = tables["Registry"];
if (null != registryTable)
{
foreach (Row registryRow in registryTable.Rows)
{
// Check if the compiler writes this registry value; if so, it should be removed.
if (this.registryValues.Contains(registryRow))
{
Wix.ISchemaElement elem = this.Core.GetIndexedElement(registryRow);
// If the registry row was found, remove it from its parent.
if (null != elem && null != elem.ParentElement)
{
Wix.IParentElement elemParent = elem.ParentElement as Wix.IParentElement;
if (null != elemParent)
{
elemParent.RemoveChild(elem);
}
}
}
}
}
}
/// <summary>
/// Removes properties defined by this extension.
/// </summary>
/// <param name="tables">The collection of tables.</param>
private void FinalizeProperties()
{
string[] properties = new string[] { "DISABLEDEPENDENCYCHECK", "IGNOREDEPENDENCIES" };
foreach (string property in properties)
{
Wix.Property elem = this.Core.GetIndexedElement("Property", property) as Wix.Property;
if (null != elem)
{
// If a value is defined, log a warning we're removing it.
if (!String.IsNullOrEmpty(elem.Value))
{
this.Core.OnMessage(DependencyWarnings.PropertyRemoved(elem.Id));
}
// If the property row was found, remove it from its parent.
if (null != elem.ParentElement)
{
Wix.IParentElement elemParent = elem.ParentElement as Wix.IParentElement;
if (null != elemParent)
{
elemParent.RemoveChild(elem);
}
}
}
}
}
/// <summary>
/// Provides an O(1) lookup for registry key and value name pairs for use in the decompiler.
/// </summary>
private sealed class RegistryKeyValueCollection : KeyedCollection<int, KeyValuePair<string, string>>
{
/// <summary>
/// Adds the registry key and value name pair to the collection if it doesn't already exist.
/// </summary>
/// <param name="key">The registry key to add.</param>
/// <param name="name">The registry value name to add.</param>
internal void Add(string key, string name)
{
KeyValuePair<string, string> pair = new KeyValuePair<string, string>(key, name);
if (!this.Contains(pair))
{
this.Add(pair);
}
}
/// <summary>
/// Returns whether the collection contains the registry key and value name pair from the <see cref="Row"/>.
/// </summary>
/// <param name="row">The registry <see cref="Row"/> to search for.</param>
/// <returns>True if the collection contains the registry key and value name pair from the <see cref="Row"/>; otherwise, false.</returns>
internal bool Contains(Row row)
{
if (null == row)
{
return false;
}
KeyValuePair<string, string> pair = new KeyValuePair<string, string>((string)row[2], (string)row[3]);
return this.Contains(pair);
}
/// <summary>
/// Return the hash code of the key and value pair concatenated with a colon as a delimiter.
/// </summary>
/// <param name="pair">The registry key and value name pair.</param>
/// <returns></returns>
protected override int GetKeyForItem(KeyValuePair<string, string> pair)
{
return String.Concat(pair.Key, ":", pair.Value).GetHashCode();
}
}
}
#endif
}
|