-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathSubsystemGenerator.cs
More file actions
574 lines (506 loc) · 20.8 KB
/
Copy pathSubsystemGenerator.cs
File metadata and controls
574 lines (506 loc) · 20.8 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
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
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using Microsoft.CSharp;
using MixedReality.Toolkit.Editor;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace MixedReality.Toolkit.Tools
{
/// <summary>
/// Class which generates subsystem source files from templates contained within
/// the package.
/// </summary>
[Serializable]
internal class SubsystemGenerator
{
// SubsystemWizard/Templates/ApplyConfigTemplate.txt
private const string ApplyConfigTemplateGuid = "c4bce3779fa330840bc15f96d11fcec0";
// SubsystemWizard/Templates/SubsystemBaseClassTemplate.txt
private const string BaseClassTemplateGuid = "2cb3a153fb7b8b142a1dac1bcfc3b38c";
// SubsystemWizard/Templates/SubsystemConfigTemplate.txt
private const string ConfigTemplateGuid = "ab5924fb380e64d47bdbd9fed4c08910";
// SubsystemWizard/Templates/SubsystemDerivedClassTemplate.txt
private const string DerivedClassTemplateGuid = "02b67503c64cf224ca2c04a04077c0d7";
// SubsystemWizard/Templates/SubsystemDescriptorTemplate.txt
private const string DescriptorTemplateGuid = "8e1afb29fbaf1c2419511d266f49b976";
// SubsystemWizard/Templates/SubsystemInterfaceTemplate.txt
private const string InterfaceTemplateGuid = "9bac30e514266984d947e360cfd17b05";
private const bool DefaultCreateConfiguration = false;
private static readonly string DefaultBaseSubsystemName = $"NewSubsystem";
[SerializeField]
private SubsystemWizardState state = SubsystemWizardState.Start;
/// <summary>
/// The current state of the wizard.
/// </summary>
public SubsystemWizardState State
{
get => state;
set => state = value;
}
[SerializeField]
private string baseClassName = DefaultBaseSubsystemName;
/// <summary>
/// The name of the base class from which the subsystem will derive.
/// </summary>
public string BaseClassName
{
get => baseClassName;
set => baseClassName = value;
}
private bool createConfiguration = DefaultCreateConfiguration;
/// <summary>
/// Indicates whether or not the wizard should generate a subsystem configuration
/// source file.
/// </summary>
public bool CreateConfiguration
{
get => createConfiguration;
set => createConfiguration = value;
}
[SerializeField]
private string organizationName = string.Empty; // DefaultOrganizationName;
/// <summary>
/// The name of the entity which is creating / releasing the subsystem.
/// </summary>
public string OrganizationName
{
get => organizationName;
set => organizationName = value;
}
/// <summary>
/// Name of configuration class, if enabled by <see cref="CreateConfiguration"/>
/// to create for new subsystem.
/// </summary>
public string ConfigurationName => CreateConfiguration ? $"{BaseClassName}Config" : "BaseSubsystemConfig";
/// <summary>
/// Name of descriptor class to create for new subsystem.
/// </summary>
public string DescriptorName => $"{BaseClassName}Descriptor";
/// <summary>
/// The name that will be displayed in project settings.
/// </summary>
public string DisplayName => $"{OrganizationName} {BaseClassName}";
/// <summary>
/// Name of interface to create for new subsystem.
/// </summary>
public string InterfaceName => $"I{BaseClassName}";
/// <summary>
/// Indicates if the user wishes to skip the creation of the subsystem
/// descriptor source code file.
/// </summary>
public bool DontCreateDescriptor { get; set; }
/// <summary>
/// Indicates if the user wishes to skip the creation of the subsystem
/// interface source code file.
/// </summary>
public bool DontCreateInterface { get; set; }
/// <summary>
/// Indicates if the user wishes to skip the creation of the subsystem
/// class source code file.
/// </summary>
public bool DontCreateBaseClass { get; set; }
/// <summary>
/// Indicates if the user wishes to skip the creation of the subsystem
/// descriptor source code file.
/// </summary>
public bool DontCreateImplementationClass { get; set; }
/// <summary>
/// The name class to create for the new subsystem.
/// </summary>
public string SubsystemName => $"{OrganizationName}{BaseClassName}";
/// <summary>
/// The interface in which the new subsystem code will be contained.
/// </summary>
public string SubsystemNamespace => $"{OrganizationName}.MRTK3.Subsystems";
/// <summary>
/// Initializes a new instance of the <see cref="SubsystemGenerator"/> class.
/// </summary>
public SubsystemGenerator()
{
Reset();
}
/// <summary>
/// Creates the subsystem source files based off of the provided templates.
/// </summary>
public void Generate(
FileInfo descriptorTemplate,
FileInfo interfaceTemplate,
FileInfo baseClassTemplate,
FileInfo derivedClassTemplate,
FileInfo configTemplate,
FileInfo applyConfigTemplate)
{
// Make sure there is a folder in which to create the new files.
DirectoryInfo outputFolder = new DirectoryInfo(
Path.Combine(MRTKFiles.GetOrCreateGeneratedFolderPath(), SubsystemName));
if (!outputFolder.Exists)
{
outputFolder.Create();
}
// Prepare the drop-in code for applying configuration to the
// templates.
string configCode;
using (FileStream fs = applyConfigTemplate.OpenRead())
{
using (StreamReader reader = new StreamReader(fs))
{
configCode = reader.ReadToEnd();
configCode = ReplaceTokens(configCode);
}
}
// Create the source files.
if (!DontCreateDescriptor)
{
CreateFile(descriptorTemplate,
Path.Combine(outputFolder.FullName, $"{DescriptorName}.cs"));
}
if (!DontCreateInterface)
{
CreateFile(interfaceTemplate,
Path.Combine(outputFolder.FullName, $"{InterfaceName}.cs"));
}
if (!DontCreateBaseClass)
{
CreateFile(baseClassTemplate,
Path.Combine(outputFolder.FullName, $"{BaseClassName}.cs"));
}
if (!DontCreateImplementationClass)
{
string filePath = Path.Combine(outputFolder.FullName, $"{SubsystemName}.cs");
CreateFile(derivedClassTemplate, filePath);
ApplyConfigCode(CreateConfiguration ? configCode : string.Empty,
new FileInfo(filePath));
}
if (CreateConfiguration)
{
CreateFile(configTemplate,
Path.Combine(outputFolder.FullName, $"{ConfigurationName}.cs"));
}
// Update Unity's asset database to ensure the new files appear.
AssetDatabase.Refresh();
}
/// <summary>
/// Reset the state of the wizard.
/// </summary>
public void Reset()
{
CreateConfiguration = DefaultCreateConfiguration;
BaseClassName = DefaultBaseSubsystemName;
OrganizationName = PlayerSettings.companyName.Replace(" ", string.Empty); ;
DontCreateBaseClass = false;
DontCreateImplementationClass = false;
DontCreateDescriptor = false;
DontCreateInterface = false;
State = SubsystemWizardState.Start;
}
/// <summary>
/// Ensures that the value of <see cref="BaseClassName"/> is valid for the
/// C# language.
/// </summary>
/// <param name="error">String describing the encountered error.</param>
/// <returns><see langword="true"/> if successful, or <see langword="false"/>.</returns>
public bool ValidateBaseClassName(out string error)
{
// Ensure a name was provided.
if (string.IsNullOrWhiteSpace(BaseClassName))
{
error = $"Subsystem base class name: Must specify a name.";
return false;
}
bool success = ValidateName(BaseClassName);
error = success ? string.Empty :
$"Subsystem base class name: {BaseClassName} is not a valid C# identifier.";
return success;
}
/// <summary>
/// Ensures that the value of <see cref="SubsystemNamespace"/> is valid for the
/// C# language.
/// </summary>
/// <param name="error">String describing the encountered error.</param>
/// <returns><see langword="true"/> if successful, or <see langword="false"/>.</returns>
public bool ValidateNamespace(out string error)
{
// Ensure a name was provided.
if (string.IsNullOrWhiteSpace(SubsystemNamespace))
{
error = $"Subsystem namespace: Must specify a name.";
return false;
}
bool success = true;
StringBuilder sb = new StringBuilder();
string[] namespaceComponents = SubsystemNamespace.Split('.');
foreach (string s in namespaceComponents)
{
if (!ValidateName(s))
{
if (sb.Length > 0)
{
sb.Append(", ");
}
sb.Append(s);
success = false;
}
}
error = (sb.Length == 0) ? string.Empty :
$"Namespace: {sb} is/are invalid C# identifier(s)";
return success;
}
/// <summary>
/// Ensures that the value of <see cref="SubsystemName"/> is valid for the
/// C# language.
/// </summary>
/// <param name="error">String describing the encountered error.</param>
/// <returns><see langword="true"/> if successful, or <see langword="false"/>.</returns>
public bool ValidateSubsystemName(out string error)
{
// Ensure a name was provided.
if (string.IsNullOrWhiteSpace(SubsystemName))
{
error = $"Subsystem name: Must specify a name.";
return false;
}
bool success = ValidateName(SubsystemName);
error = success ? string.Empty :
$"Subsystem name: {SubsystemName} is not a valid C# identifier.";
return success;
}
/// <summary>
/// Ensures that the necessary templates can be located and successfully loaded.
/// </summary>
/// <param name="errors">List to which any encountered errors will be added.</param>
/// <param name="descriptorTemplate">
/// <see cref="FileInfo"/> object representing the subsystem descriptor template file.
/// </param>
/// <param name="interfaceTemplate">
/// <see cref="FileInfo"/> object representing the subsystem interface template file.
/// </param>
/// <param name="baseClassTemplate">
/// <see cref="FileInfo"/> object representing the subsystem class template file.
/// </param>
/// <param name="derivedClassTemplate">
/// <see cref="FileInfo"/> object representing the derived class template file.
/// </param>
/// <param name="configTemplate">
/// <see cref="FileInfo"/> object representing the subsystem configuration template file.
/// </param>
/// <param name="applyConfigTemplate">
/// <see cref="FileInfo"/> object representing the template file that contains information on
/// the code to apply when configuration is desired.
/// </param>
/// <returns><see langword="true"/> if the collection of templates can all be validated, or <see langword="false"/>.</returns>
public bool ValidateTemplates(
List<string> errors,
out FileInfo descriptorTemplate,
out FileInfo interfaceTemplate,
out FileInfo baseClassTemplate,
out FileInfo derivedClassTemplate,
out FileInfo configTemplate,
out FileInfo applyConfigTemplate)
{
bool success = true;
if (!GetAsset(
DescriptorTemplateGuid,
out descriptorTemplate,
out string error))
{
errors.Add($"Descriptor template - {error}");
success = false;
}
if (!GetAsset(
InterfaceTemplateGuid,
out interfaceTemplate,
out error))
{
errors.Add($"Interface template - {error}");
success = false;
}
if (!GetAsset(
BaseClassTemplateGuid,
out baseClassTemplate,
out error))
{
errors.Add($"Class template - {error}");
success = false;
}
if (!GetAsset(
DerivedClassTemplateGuid,
out derivedClassTemplate,
out error))
{
errors.Add($"Class template - {error}");
success = false;
}
if (!GetAsset(
ConfigTemplateGuid,
out configTemplate,
out error))
{
errors.Add($"Configuration template - {error}");
success = false;
}
if (!GetAsset(
ApplyConfigTemplateGuid,
out applyConfigTemplate,
out error))
{
errors.Add($"Apply configuration template - {error}");
success = false;
}
return success;
}
/// <summary>
/// Inserts the required code, to access a subsystem configuration object, to
/// the specified file.
/// </summary>
/// <param name="code">The code required to access the subsystem configuration.</param>
/// <param name="sourceFile">The file to which the code is to be added.</param>
/// <remarks>
/// If the subsystem does not utilize a configuration, an empty is to be provided
/// in the 'code' parameter.
/// <para/>
/// For <paramref name="sourceFile"/> to be properly updated, its contents must contain the <c>"%APPLYCONFIG%"</c>
/// token. This is used to indicate the correct location at which to place the code.
/// </remarks>
private void ApplyConfigCode(
string code,
FileInfo sourceFile)
{
if (!sourceFile.Exists)
{
throw new IOException($"{sourceFile.FullName} cannot be found.");
}
string source;
using (FileStream fs = sourceFile.OpenRead())
{
using (StreamReader reader = new StreamReader(fs))
{
source = reader.ReadToEnd();
source = source.Replace("%APPLYCONFIG%", code);
}
}
using (FileStream fs = new FileStream(sourceFile.FullName, FileMode.Truncate, FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(fs))
{
writer.AutoFlush = true;
writer.Write(source);
}
}
}
/// <summary>
/// Creates the specified subsystem source code file from the provided template.
/// </summary>
/// <param name="templateFile">The template to use for the specified file.</param>
/// <param name="outputFilePath">The fully qualified path for the resulting source code file.</param>
private void CreateFile(
FileInfo templateFile,
string outputFilePath)
{
FileInfo outputFile = new FileInfo(outputFilePath);
if (outputFile.Exists)
{
throw new IOException($"Unable to create {outputFile.FullName}, overwriting existing files is not supported.");
}
// Read the template file.
string template = null;
using (FileStream fs = templateFile.OpenRead())
{
using (StreamReader reader = new StreamReader(fs))
{
template = reader.ReadToEnd();
template = ReplaceTokens(template);
}
}
if (template == null)
{
throw new IOException($"Failed to read the contents of {templateFile.FullName}");
}
// Write the new source file.
using (FileStream fs = outputFile.OpenWrite())
{
using (StreamWriter writer = new StreamWriter(fs))
{
writer.AutoFlush = true;
writer.Write(template);
}
}
}
/// <summary>
/// Attempts to acquire a <see cref="FileInfo"/> object representing the requested asset.
/// </summary>
/// <param name="guid">The guid that represents the asset.</param>
/// <param name="fileInfo"><see cref="FileInfo"/> object representing the asset.</param>
/// <param name="error">Error message.</param>
/// <returns><see langword="true"/> if the asset was acquired, or <see langword="false"/>.</returns>
private bool GetAsset(
string guid,
out FileInfo fileInfo,
out string error)
{
fileInfo = null;
error = string.Empty;
string filePath = AssetDatabase.GUIDToAssetPath(guid);
if (string.IsNullOrWhiteSpace(filePath))
{
error = "Unable to resolve template asset, please file an MRTK3 bug.";
return false;
}
FileInfo fi = new FileInfo(filePath);
if (!fi.Exists)
{
error = $"Template file {fi.Name} could not be located, please file an MRTK3 bug.";
return false;
}
fileInfo = fi;
return true;
}
/// <summary>
/// Replaces template tokens with customer data.
/// </summary>
/// <param name="template">The contents of the template.</param>
/// <returns>The updated contents of the template.</returns>
private string ReplaceTokens(string template)
{
template = template.Replace("%NAMESPACE%", SubsystemNamespace);
template = template.Replace("%SUBSYSTEMBASECLASSNAME%", BaseClassName);
template = template.Replace("%SUBSYSTEMNAME%", SubsystemName);
template = template.Replace("%CONFIGNAME%", ConfigurationName);
template = template.Replace("%ORGANIZATION%", OrganizationName);
template = template.Replace("%DISPLAYNAME%", DisplayName);
template = template.Replace("%RUNTIMENAME%", SubsystemNamespace.ToLower());
return template;
}
/// <summary>
/// Validates that the specified name is a valid identifier for C#.
/// </summary>
/// <param name="name">The name to validate/</param>
/// <returns><see langword="true"/> if the name is a valid C# identifier, or <see langword="false"/>.</returns>
private bool ValidateName(string name)
{
// Verify that the name is valid within C#
return CSharpCodeProvider.CreateProvider("C#").IsValidIdentifier(name);
}
}
/// <summary>
/// Enumeration representing the states of the subsystem wizard.
/// </summary>
internal enum SubsystemWizardState
{
/// <summary>
/// The wizard is collecting data from the user.
/// </summary>
Start = 0,
/// <summary>
/// The wizard is ready to generate, waiting for confirmation from the user.
/// </summary>
PreGenerate,
/// <summary>
/// The wizard is complete.
/// </summary>
Complete
}
}