-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommonModelsWin32.cs
463 lines (407 loc) · 14.8 KB
/
CommonModelsWin32.cs
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
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using WDCMLSDKBase;
using WDCMLSDKDerived;
namespace WDCMLSDK
{
/// <summary>
/// An object model representing the Win32 API ref docs.
/// </summary>
internal class ApiRefModelWin32
{
/// <summary>
/// Gets an object model representing the Win32 API ref docs.
/// </summary>
public static ApiRefModelWin32 GetApiRefModelWin32(bool desktop, bool wsua = false, bool wdk = false)
{
string platformDesc = string.Empty;
string projectFilename = string.Empty;
var sortedProjectList = new SortedList<string, string>();
if (desktop)
{
platformDesc = "desktop";
projectFilename = "desktop.txt";
ApiRefModelWin32.AppendToProjectList(ref sortedProjectList, "desktop.txt");
}
if (wsua)
{
if (platformDesc.Length == 0) platformDesc = "server";
else platformDesc += " and server";
if (projectFilename.Length == 0) projectFilename = "wsua.txt";
else projectFilename += " and wsua.txt";
ApiRefModelWin32.AppendToProjectList(ref sortedProjectList, "wsua.txt");
}
if (wdk)
{
if (platformDesc.Length == 0) platformDesc = "wdk";
else platformDesc += " and wdk";
if (projectFilename.Length == 0) projectFilename = "wdk.txt";
else projectFilename += " and wdk.txt";
ApiRefModelWin32.AppendToProjectList(ref sortedProjectList, "wdk.txt");
}
List<DirectoryInfo> projectDirectoryInfos = new List<DirectoryInfo>();
foreach (string eachProjectName in sortedProjectList.Keys)
{
projectDirectoryInfos.AddRange(ProgramBase.EnlistmentDirectoryInfo.GetDirectories(eachProjectName, SearchOption.TopDirectoryOnly).ToList());
}
ProgramBase.ConsoleWrite("Creating Win32 " + platformDesc + " docset", ConsoleWriteStyle.Success);
string projectListIntro = "These are the shipping projects that document Win32 " + platformDesc + " functions (they're in " + projectFilename + "). The app only processes topics in these projects that are represented by an unfiltered TOC entry (that is, no MSDN build condition).";
ProgramBase.ConsoleWrite(projectListIntro, ConsoleWriteStyle.Highlight);
ApiRefModelWin32 apiRefModelWin32 = new ApiRefModelWin32();
foreach (DirectoryInfo eachProjectDirectoryInfo in projectDirectoryInfos)
{
ProgramBase.ConsoleWrite(eachProjectDirectoryInfo.Name, ConsoleWriteStyle.Default, false);
if (eachProjectDirectoryInfo != projectDirectoryInfos[projectDirectoryInfos.Count - 1])
{
ProgramBase.ConsoleWrite(", ", ConsoleWriteStyle.Default, false);
}
else
{
ProgramBase.ConsoleWrite(".\n\n");
}
// For reference projects, the topic files should be in a folder with the same name as the project folder. But use the standard algorithm just to be sure.
apiRefModelWin32.ProcessProject(eachProjectDirectoryInfo.Name, EditorBase.GetEditorsForTopicsInProject(eachProjectDirectoryInfo));
}
return apiRefModelWin32;
}
private static void AppendToProjectList(ref SortedList<string, string> sortedProjectList, string dotTxtFilename)
{
List<string> projectList = null;
ProgramBase.LoadTextFileIntoStringList(dotTxtFilename, ref projectList, "MISSING " + dotTxtFilename + ". This file could not be found in your enlistment folder path. Your configuration.txt contains something like: my_enlistment_folder D:\\Source_Depot\\devdocmain. This should be the folder that contains the dev_*, m_*, w_* folders, BuildX, desktop.txt, etc.");
foreach (string eachProjectName in projectList)
{
if (eachProjectName == "buses") continue;
if (!sortedProjectList.Keys.Contains(eachProjectName)) sortedProjectList.Add(eachProjectName, null);
}
}
// Note: the key here is the ToLower version but the FunctionWin32 contains the original case from the topic's title.
public Dictionary<string, FunctionWin32InDocs> FunctionWin32InDocses = new Dictionary<string, FunctionWin32InDocs>();
private void ProcessProject(string projectName, List<Editor> topicEditors)
{
foreach (Editor eachTopicEditor in topicEditors)
{
string eachTopicMetadataAtId = eachTopicEditor.GetMetadataAtId();
string eachTopicTitle = eachTopicEditor.GetMetadataAtTitle();
FileInfo eachTopicFileInfo = eachTopicEditor.FileInfo;
if (eachTopicEditor.GetMetadataAtTypeAsString() == "function")
{
bool functionAlreadyExists = false;
FunctionWin32InDocs functionWin32 = null;
this.EnsureFunction(eachTopicEditor, projectName, eachTopicMetadataAtId, eachTopicTitle, eachTopicFileInfo, ref functionAlreadyExists, ref functionWin32);
if (functionAlreadyExists)
{
ProgramBase.DupedWin32ApiNamesLog.AddEntry(string.Format("{0} and {1}", eachTopicFileInfo.FullName, functionWin32.FileInfo.FullName));
}
}
}
}
public void EnsureFunction(Editor eachTopicEditor, string projectName, string id, string name, FileInfo fileInfo, ref bool functionAlreadyExists, ref FunctionWin32InDocs functionWin32)
{
if (this.FunctionWin32InDocses.ContainsKey(name.ToLower()))
{
functionAlreadyExists = true;
functionWin32 = this.FunctionWin32InDocses[name.ToLower()];
}
else
{
functionAlreadyExists = false;
this.FunctionWin32InDocses[name.ToLower()] = new FunctionWin32InDocs(projectName, id, name, fileInfo, eachTopicEditor.GetLibraryFilenames());
}
}
public bool HasFunctionWin32s
{
get
{
return (FunctionWin32InDocses.Count != 0);
}
}
public bool GetFunctionWin32ByName(string name, ref FunctionWin32InDocs foundFunctionWin32)
{
if (this.FunctionWin32InDocses.ContainsKey(name.ToLower()))
{
foundFunctionWin32 = this.FunctionWin32InDocses[name.ToLower()];
return true;
}
return false;
}
}
/// <summary>
/// A class representing a Win32 function.
/// </summary>
internal class FunctionWin32
{
public string Name { get; set; }
public string ModuleName { get; set; }
public List<string> LibraryNames { get; set; }
public FunctionWin32(string name, string moduleName, List<string> libraryNames)
{
this.Name = name;
this.ModuleName = moduleName;
this.LibraryNames = libraryNames;
}
}
/// <summary>
/// Utility class used for sorting FunctionWin32s.
/// </summary>
internal class FunctionWin32Comparer : Comparer<FunctionWin32>
{
public override int Compare(FunctionWin32 lhs, FunctionWin32 rhs)
{
return lhs.Name.CompareTo(rhs.Name);
}
}
/// <summary>
/// Utility class used for sorting FunctionWin32GroupedByInitialChars.
/// </summary>
internal class FunctionWin32GroupedByInitialCharComparer : Comparer<FunctionWin32GroupedByInitialChar>
{
public override int Compare(FunctionWin32GroupedByInitialChar lhs, FunctionWin32GroupedByInitialChar rhs)
{
return lhs.Name.CompareTo(rhs.Name);
}
}
/// <summary>
/// Utility class used for sorting InitialCharGroups.
/// </summary>
internal class InitialCharGroupComparer : Comparer<InitialCharGroup>
{
public override int Compare(InitialCharGroup lhs, InitialCharGroup rhs)
{
return lhs.Name.CompareTo(rhs.Name);
}
}
/// <summary>
/// A class representing a Win32 function in the docs.
/// </summary>
internal class FunctionWin32InDocs : FunctionWin32
{
public FunctionWin32InDocs(string projectName, string id, string name, FileInfo fileInfo, List<string> libraryNames)
: base(name, null, libraryNames)
{
this.ProjectName = projectName;
this.Id = id;
this.Name = name;
this.FileInfo = fileInfo;
}
public FunctionWin32InDocs(string projectName, string id, string name, FileInfo fileInfo)
: this(projectName, id, name, fileInfo, null)
{
}
public string ProjectName = string.Empty;
public string Id = string.Empty;
public FileInfo FileInfo;
}
/// <summary>
/// A class representing an api grouped by some key.
/// </summary>
internal class FunctionWin32Grouped : FunctionWin32
{
public string SdkVersionIntroducedIn { get; set; }
public string SdkVersionRemovedIn { get; set; }
public string FunctionWin32Id { get; set; }
public Module ModuleMovedTo { get; set; }
public FunctionWin32Grouped(string moduleName, string name, string sdkVersionIntroducedIn, string functionWin32Id)
: base(name, moduleName, null)
{
this.SdkVersionIntroducedIn = sdkVersionIntroducedIn;
this.FunctionWin32Id = functionWin32Id;
}
}
internal class FunctionWin32GroupedByModule : FunctionWin32Grouped
{
public string Requirements
{
get
{
string requirements = "Introduced in Windows " + this.SdkVersionIntroducedIn;
if (this.SdkVersionRemovedIn != null)
{
if (this.ModuleMovedTo != null)
{
requirements += ". Moved to " + this.ModuleMovedTo.Name + " in Windows " + this.SdkVersionRemovedIn;
}
else
{
requirements += ". Removed in Windows " + this.SdkVersionRemovedIn;
}
}
return requirements;
}
}
public FunctionWin32GroupedByModule(string moduleName, string name, string sdkVersionIntroducedIn, string functionWin32Id)
: base(moduleName, name, sdkVersionIntroducedIn, functionWin32Id)
{
}
}
internal class FunctionWin32GroupedByInitialChar : FunctionWin32Grouped
{
public string Module
{
get
{
string requirements = "Introduced into " + this.ModuleName + " in Windows " + this.SdkVersionIntroducedIn;
if (this.SdkVersionRemovedIn != null)
{
if (this.ModuleMovedTo != null)
{
requirements += ". Moved to " + this.ModuleMovedTo.Name + " in Windows " + this.SdkVersionRemovedIn;
}
else
{
requirements += ". Removed in Windows " + this.SdkVersionRemovedIn;
}
}
return requirements;
}
}
public FunctionWin32GroupedByInitialChar(string moduleName, string name, string sdkVersionIntroducedIn, string functionWin32Id, string sdkVersionRemovedIn, Module moduleMovedTo)
: base(moduleName, name, sdkVersionIntroducedIn, functionWin32Id)
{
this.SdkVersionRemovedIn = sdkVersionRemovedIn;
this.ModuleMovedTo = moduleMovedTo;
}
}
/// <summary>
/// A class representing an api set or dll.
/// </summary>
internal class Module
{
public string Name { get; set; }
public bool IsApiSet { get; set; }
private List<FunctionWin32GroupedByModule> apis = new List<FunctionWin32GroupedByModule>();
public System.Collections.ObjectModel.ReadOnlyCollection<FunctionWin32GroupedByModule> Apis
{
get { return new System.Collections.ObjectModel.ReadOnlyCollection<FunctionWin32GroupedByModule>(this.apis); }
}
public Module(string name, bool isApiSet)
{
this.Name = name;
this.IsApiSet = isApiSet;
this.apis = new List<FunctionWin32GroupedByModule>();
}
public void AddApi(string name, string sdkVersionIntroducedIn, string functionWin32Id)
{
this.apis.Add(new FunctionWin32GroupedByModule(this.Name, name, sdkVersionIntroducedIn, functionWin32Id));
}
public FunctionWin32GroupedByModule FindApi(string name)
{
return this.apis.Find(found => found.Name == name);
}
}
/// <summary>
/// A class representing apis grouped by initial char.
/// </summary>
internal class InitialCharGroup
{
public string Name { get; set; }
private List<FunctionWin32GroupedByInitialChar> apis = new List<FunctionWin32GroupedByInitialChar>();
public System.Collections.ObjectModel.ReadOnlyCollection<FunctionWin32GroupedByInitialChar> Apis
{
get { return new System.Collections.ObjectModel.ReadOnlyCollection<FunctionWin32GroupedByInitialChar>(this.apis); }
}
public InitialCharGroup(string name)
{
this.Name = name;
this.apis = new List<FunctionWin32GroupedByInitialChar>();
}
public void AddApi(string moduleName, string name, string sdkVersionIntroducedIn, string functionWin32Id, string sdkVersionRemovedIn, Module moduleMovedTo)
{
this.apis.Add(new FunctionWin32GroupedByInitialChar(moduleName, name, sdkVersionIntroducedIn, functionWin32Id, sdkVersionRemovedIn, moduleMovedTo));
}
public FunctionWin32GroupedByInitialChar FindApi(string name)
{
return this.apis.Find(found => found.Name == name);
}
public void Sort()
{
this.apis.Sort(new FunctionWin32GroupedByInitialCharComparer());
}
public static List<InitialCharGroup> ListOfInitialCharGroupFromListOfModule(List<Module> modules)
{
List<InitialCharGroup> initialCharGroups = new List<InitialCharGroup>();
foreach (Module module in modules)
{
foreach (FunctionWin32GroupedByModule functionWin32GroupedByModule in module.Apis)
{
string initialCharGroupKey = functionWin32GroupedByModule.Name.Substring(0, 1);
if (char.IsLetter(initialCharGroupKey[0]))
{
initialCharGroupKey = initialCharGroupKey.ToUpper();
}
else
{
initialCharGroupKey = "_";
}
InitialCharGroup initialCharGroup = initialCharGroups.Find(found => found.Name == initialCharGroupKey);
if (initialCharGroup == null)
{
initialCharGroup = new InitialCharGroup(initialCharGroupKey);
initialCharGroups.Add(initialCharGroup);
}
initialCharGroup.AddApi(functionWin32GroupedByModule.ModuleName, functionWin32GroupedByModule.Name, functionWin32GroupedByModule.SdkVersionIntroducedIn, functionWin32GroupedByModule.FunctionWin32Id, functionWin32GroupedByModule.SdkVersionRemovedIn, functionWin32GroupedByModule.ModuleMovedTo);
}
}
foreach (InitialCharGroup initialCharGroup in initialCharGroups)
{
initialCharGroup.Sort();
}
initialCharGroups.Sort(new InitialCharGroupComparer());
return initialCharGroups;
}
}
/// <summary>
/// A class representing the API Sets in an umbrella lib.
/// </summary>
internal class UmbrellaLib
{
public string Name { get; set; }
public List<Module> Modules = new List<Module>();
public List<InitialCharGroup> InitialCharGroups = null;
public Dictionary<string, List<string>> setToApiMappings = new Dictionary<string, List<string>>();
public UmbrellaLib(string name)
{
this.Name = name;
}
public Module GetModuleForApiName(string apiName)
{
foreach (Module module in this.Modules)
{
FunctionWin32Grouped apiToAdd = module.FindApi(apiName);
if (apiToAdd != null)
{
return module;
}
}
return null;
}
#if (USING_WCD)
public void AddApi(Microsoft.CoreSystem.WindowsCompositionDatabase.Database.Api api, string sdkVersion, string functionWin32Id)
{
bool isApiSet = false;
foreach (Microsoft.CoreSystem.WindowsCompositionDatabase.Database.Apiset apiset in api.Apisets) // api.Apisets can contain many entries: 6 is not unusual.
{
isApiSet = true; break;
}
Module module = this.Modules.Find(found => found.Name == api.Binary.Name);
if (module == null)
{
module = new Module(api.Binary.Name, isApiSet);
this.Modules.Add(module);
}
FunctionWin32Grouped apiToAdd = module.FindApi(api.Name);
if (apiToAdd == null)
{
module.AddApi(api.Name, sdkVersion, functionWin32Id);
}
}
#endif
public void SortAlphabetically()
{
this.InitialCharGroups = InitialCharGroup.ListOfInitialCharGroupFromListOfModule(this.Modules);
}
}
}