-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathLuaDocGenerator.cs
602 lines (490 loc) · 20.9 KB
/
LuaDocGenerator.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
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
#if UNITY_EDITOR
using System;
using UnityEditor;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.IO;
using System.Text;
using UnityEngine;
/// <summary>
/// Handles generating Lua documentation for varying sources via reflection
/// </summary>
public static class LuaDocGenerator
{
/// <summary>
/// Info about a Lua API
/// </summary>
private class LuaApiInfo
{
public LuaApi Attribute;
public Type Type;
public readonly List<LuaFunction_Info> functions = new List<LuaFunction_Info>();
public readonly List<LuaVariableInfo> variables = new List<LuaVariableInfo>();
}
/// <summary>
/// Info about a Lua function
/// </summary>
private class LuaFunction_Info
{
public LuaApiFunction Attribute;
public MethodInfo MethodInfo;
}
/// <summary>
/// Info about a Lua variable
/// </summary>
private class LuaVariableInfo
{
public LuaApiVariable Attribute;
public FieldInfo FieldInfo;
}
/// <summary>
/// Info about a Lua enum
/// </summary>
private class LuaEnumInfo
{
public LuaApiEnum Attribute;
public Type ApiType;
public readonly List<LuaEnumValueInfo> values = new List<LuaEnumValueInfo>();
}
/// <summary>
/// Info about a Lua enum value
/// </summary>
private class LuaEnumValueInfo
{
public LuaApiEnumValue Attribute;
public string StringValue;
}
/// <summary>
/// Grabs information about all the Lua Apis in the code base
/// </summary>Å
/// <returns></returns>
private static List<LuaApiInfo> GetAllApiInfo()
{
List<Type> apiTypeList = new List<Type>();
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
apiTypeList.AddRange(assembly.GetTypes()
.Where(type => type.IsSubclassOf(typeof(LuaAPIBase))));
}
List<LuaApiInfo> apiInfoArray = new List<LuaApiInfo>(apiTypeList.Count);
// Iterate all types deriving from LuaApiBase
foreach (Type apiType in apiTypeList)
{
LuaApi[] apiAttribs = apiType.GetCustomAttributes(typeof(LuaApi), false) as LuaApi[];
if (apiAttribs != null && apiAttribs.Length > 0)
{
LuaApiInfo apiInfo = new LuaApiInfo
{
Type = apiType,
Attribute = apiAttribs[0]
};
// Iterate all methods
foreach(MethodInfo methodType in apiType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static))
{
// Grab the LuaApiFunction if it exists
LuaApiFunction[] functionAttribs = methodType.GetCustomAttributes(typeof(LuaApiFunction), false) as LuaApiFunction[];
if (functionAttribs != null && functionAttribs.Length > 0)
{
apiInfo.functions.Add(new LuaFunction_Info
{
Attribute = functionAttribs[0],
MethodInfo = methodType,
});
}
}
// Iterate all variables
foreach(FieldInfo fieldInfo in apiType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Static))
{
LuaApiVariable[] varaibleAttribs =
fieldInfo.GetCustomAttributes(typeof(LuaApiVariable), false) as LuaApiVariable[];
if (varaibleAttribs != null && varaibleAttribs.Length > 0)
{
apiInfo.variables.Add(new LuaVariableInfo{
Attribute = varaibleAttribs[0],
FieldInfo = fieldInfo,
});
}
}
apiInfoArray.Add(apiInfo);
}
}
return apiInfoArray;
}
/// <summary>
/// Grabs all information about enums that will be bound to Lua
/// </summary>
/// <returns></returns>
private static List<LuaEnumInfo> GetAllEnums()
{
List<Type> luaEnumList = new List<Type>();
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
luaEnumList.AddRange((assembly.GetTypes()
.Where(luaEnumType => Attribute.IsDefined(luaEnumType, typeof(LuaApiEnum)))));
}
List<LuaEnumInfo> result = new List<LuaEnumInfo>(luaEnumList.Count);
foreach (Type enumType in luaEnumList)
{
LuaEnumInfo enumInfo = new LuaEnumInfo
{
ApiType = enumType,
Attribute = (LuaApiEnum) enumType.GetCustomAttributes(typeof(LuaApiEnum), false)[0],
};
foreach (var enumVal in Enum.GetValues(enumType))
{
var valueInfo = enumType.GetMember(enumVal.ToString());
var attribs = valueInfo[0].GetCustomAttributes(typeof(LuaApiEnumValue), false);
enumInfo.values.Add(new LuaEnumValueInfo
{
Attribute = (LuaApiEnumValue)(attribs.Length > 0 ? attribs[0] : null),
StringValue = enumVal.ToString(),
});
}
result.Add(enumInfo);
}
return result;
}
/// <summary>
/// Generate MediaWiki pages for all Lua apis and enums
/// </summary>
[MenuItem("Lua/Docs/Generate MediaWiki Docs")]
private static void GenerateMediaWikiDocs()
{
Logger.Log (Channel.Lua, "Beginning Lua doc generation for MediaWiki");
string documentationPath =
EditorUtility.OpenFolderPanel("Choose a location to place wiki files", string.Empty, string.Empty);
if (!string.IsNullOrEmpty(documentationPath))
{
// Iterate all types deriving from LuaApiBase
foreach (LuaApiInfo api in GetAllApiInfo())
{
// Write out the header for the api type
LuaApi luaApiDetails = api.Attribute;
string apiDocPath = string.Format("{0}/{1}.mediawiki", documentationPath, luaApiDetails.luaName);
StreamWriter documentation = File.CreateText(apiDocPath);
Logger.Log(Channel.Lua, "Generating documentation for api: {0}", luaApiDetails.luaName);
documentation.WriteLine("= {0} =", luaApiDetails.luaName);
if (!string.IsNullOrEmpty(luaApiDetails.description))
{
documentation.WriteLine("== Description ==");
documentation.WriteLine(luaApiDetails.description);
}
if (!string.IsNullOrEmpty(luaApiDetails.notes))
{
documentation.WriteLine("== Notes ==");
documentation.WriteLine(luaApiDetails.notes);
}
documentation.WriteLine("== Functions ==");
// Iterate all methods
foreach (LuaFunction_Info method in api.functions)
{
// Grab the LuaApiFunction if it exists
LuaApiFunction luaMethodDetails = method.Attribute;
Logger.Log(Channel.Lua, "\t {0}", luaMethodDetails.name);
documentation.WriteLine("=== {0} ===", luaMethodDetails.name);
string functionSig = GetCleanFunctionSignature(method.MethodInfo, luaMethodDetails, luaApiDetails);
documentation.WriteLine("<syntaxhighlight source lang=\"lua\">{0}</syntaxhighlight>", functionSig);
bool hasParams = method.MethodInfo.GetParameters().Length != 0;
if (hasParams)
{
documentation.WriteLine("'''Expected parameter types'''");
documentation.WriteLine("{| class=\"wikitable\"");
}
// Expected param types
foreach (ParameterInfo param in method.MethodInfo.GetParameters())
{
documentation.WriteLine("|-");
documentation.WriteLine("| {0} || {1}", param.Name, GetCleanTypeName(param.ParameterType));
}
if (hasParams)
{
documentation.WriteLine("|}");
}
documentation.WriteLine("'''Description''': {0}\n", luaMethodDetails.description);
// Use custom return description if exists, else generate one
documentation.WriteLine("'''Returns''': {0}\n",
!string.IsNullOrEmpty(luaMethodDetails.returns)
? luaMethodDetails.returns
: GetCleanTypeName(method.MethodInfo.ReturnType));
if (!string.IsNullOrEmpty(luaMethodDetails.notes))
{
documentation.WriteLine("'''Notes''': {0}",
luaMethodDetails.notes);
}
if (!string.IsNullOrEmpty(luaMethodDetails.warning))
{
documentation.WriteLine("<span style=\"color:#ff0000\">'''Warning'''</span>: {0}",
luaMethodDetails.warning);
}
if (!string.IsNullOrEmpty(luaMethodDetails.success))
{
documentation.WriteLine("<span style=\"color:#009000\">'''Tip'''</span>: {0}",
luaMethodDetails.success);
}
}
bool wroteTitle = false;
foreach (LuaVariableInfo fieldInfo in api.variables)
{
if (!wroteTitle)
{
documentation.WriteLine("== Variables ==");
wroteTitle = true;
}
LuaApiVariable luaVariableDetails = fieldInfo.Attribute;
documentation.WriteLine("=== {0} ===", luaVariableDetails.name);
string varibleSig = string.Format("{0}.{1}", luaApiDetails.luaName, luaVariableDetails.name);
documentation.WriteLine("<syntaxhighlight source lang=\"lua\">{0}</syntaxhighlight>", varibleSig);
documentation.WriteLine("'''Description''': {0}\n", luaVariableDetails.description);
}
// Add time stamp
documentation.WriteLine("\n\n'''Docs last hacked together on''': {0:dd/MM/yyyy H:mm}", DateTime.Now);
documentation.Close();
}
// TODO Enum docs
string enumDocPath = string.Format("{0}/Constants.mediawiki", documentationPath);
StreamWriter enumDocumentation = File.CreateText(enumDocPath);
enumDocumentation.WriteLine("= Constants =");
enumDocumentation.WriteLine("== Enums ==");
foreach (LuaEnumInfo enumInfo in GetAllEnums())
{
enumDocumentation.WriteLine("=== {0} ===", enumInfo.Attribute.name);
if (!string.IsNullOrEmpty(enumInfo.Attribute.description))
{
enumDocumentation.WriteLine(enumInfo.Attribute.description);
}
enumDocumentation.WriteLine("{| class=\"wikitable\"");
enumDocumentation.WriteLine("|-");
enumDocumentation.WriteLine("! Usage !! Description");
foreach (LuaEnumValueInfo value in enumInfo.values)
{
if (value.Attribute != null && value.Attribute.hidden)
{
continue;
}
enumDocumentation.WriteLine("|-");
enumDocumentation.WriteLine("| {0}.{1} || {2}",
enumInfo.Attribute.name,
value.StringValue,
value.Attribute != null ? value.Attribute.description : string.Empty);
}
enumDocumentation.WriteLine("|}");
}
enumDocumentation.Close();
}
Logger.Log (Channel.Lua, "Completed Lua doc generation");
}
private class VSCodeSnippet
{
public string prefix;
public string[] body;
public string description;
}
[MenuItem("Lua/Docs/Generate VSCode Snippets")]
public static void GenerateVSCodeSnippets()
{
StringBuilder snippets = new StringBuilder();
VSCodeSnippet snippet = new VSCodeSnippet {body = new string[1]};
foreach (LuaApiInfo api in GetAllApiInfo())
{
// Write out the header for the api type
LuaApi luaApiDetails = api.Attribute;
// Iterate all methods
foreach (LuaFunction_Info method in api.functions)
{
// Grab the LuaApiFunction if it exists
LuaApiFunction luaMethodDetails = method.Attribute;
snippet.prefix = string.Format("{0}.{1}", luaApiDetails.luaName, luaMethodDetails.name);
string paramString = string.Empty;
var paramArray = method.MethodInfo.GetParameters();
for (int i = 0; i < paramArray.Length; i++)
{
var param = paramArray[i];
paramString += string.Format("${{{0}:{1}}}, ", i, param.Name);
}
if (!string.IsNullOrEmpty(paramString))
{
paramString = paramString.Remove(paramString.Length - 2, 2);
}
snippet.body[0] = string.Format("{0}({1})", snippet.prefix, paramString);
snippet.description = luaMethodDetails.description;
string finalBlock = string.Format("\"{0}\": {1},", snippet.prefix, JsonUtility.ToJson(snippet, true));
snippets.AppendLine(finalBlock);
}
foreach (LuaVariableInfo fieldInfo in api.variables)
{
LuaApiVariable luaVariableDetails = fieldInfo.Attribute;
snippet.prefix = string.Format("{0}.{1}", luaApiDetails.luaName, luaVariableDetails.name);
snippet.body[0] = snippet.prefix;
snippet.description = luaVariableDetails.description;
string finalBlock = string.Format("\"{0}\" : {1},", snippet.prefix, JsonUtility.ToJson(snippet, true));
snippets.AppendLine(finalBlock);
}
}
EditorGUIUtility.systemCopyBuffer = snippets.ToString();
}
private class AtomSnippet
{
public string Prefix;
public string Body;
public string Description;
public string DescriptionMoreUrl;
private static string ToLiteral(string input)
{
input = input.Replace("'", @"\'");
input = input.Replace("\"", @"""");
return input;
}
public string ConstructCSONString()
{
return string.Format(@"
'{0}':
'prefix': '{0}'
'body': '{1}'
'description' : '{2}'
'rightLabelHTML': '<span style=""color:#DC9656"">Lua</span>'
'descriptionMoreURL' : '{3}'",
Prefix,
ToLiteral(Body),
ToLiteral(Description),
DescriptionMoreUrl);
}
}
[MenuItem("Lua/Docs/Generate Atom Snippets")]
public static void GenerateAtomSnippets()
{
StringBuilder snippets = new StringBuilder();
AtomSnippet snippet = new AtomSnippet();
snippets.AppendLine("'.source.lua':");
// Iterate all types deriving from LuaApiBase
foreach (LuaApiInfo api in GetAllApiInfo())
{
// Write out the header for the api type
LuaApi luaApiDetails = api.Attribute;
// Iterate all methods
foreach (LuaFunction_Info method in api.functions)
{
// Grab the LuaApiFunction if it exists
LuaApiFunction luaMethodDetails = method.Attribute;
snippet.Prefix = string.Format("{0}.{1}", luaApiDetails.luaName, luaMethodDetails.name);
string paramString = string.Empty;
var paramArray = method.MethodInfo.GetParameters();
for (int i = 0; i < paramArray.Length; i++)
{
var param = paramArray[i];
paramString += string.Format("${{{0}:{1}}}, ", i, param.Name);
}
if (!string.IsNullOrEmpty(paramString))
{
paramString = paramString.Remove(paramString.Length - 2, 2);
}
snippet.Body = string.Format("{0}({1})", snippet.Prefix, paramString);
snippet.Description = luaMethodDetails.description;
snippets.AppendLine(snippet.ConstructCSONString());
}
foreach (LuaVariableInfo fieldInfo in api.variables)
{
LuaApiVariable luaVariableDetails = fieldInfo.Attribute;
snippet.Prefix = string.Format("{0}.{1}", luaApiDetails.luaName, luaVariableDetails.name);
snippet.Body = snippet.Prefix;
snippet.Description = luaVariableDetails.description;
snippets.AppendLine(snippet.ConstructCSONString());
}
}
foreach (LuaEnumInfo enumInfo in GetAllEnums())
{
foreach (LuaEnumValueInfo enumValueInfo in enumInfo.values)
{
if (enumValueInfo.Attribute != null && enumValueInfo.Attribute.hidden)
{
continue;
}
snippet.Prefix = string.Format("{0}.{1}", enumInfo.Attribute.name, enumValueInfo.StringValue);
snippet.Body = snippet.Prefix;
snippet.Description = enumValueInfo.Attribute != null ? enumValueInfo.Attribute.description : string.Empty;
snippet.DescriptionMoreUrl = string.Empty;
snippets.AppendLine(snippet.ConstructCSONString());
}
}
EditorGUIUtility.systemCopyBuffer = snippets.ToString();
}
/// <summary>
/// Gets a clean function signature
/// </summary>
/// <returns>The clean function signature.</returns>
/// <param name="method">Method.</param>
/// <param name="functionAttrib">Function Attribute.</param>
/// <param name="apiAttrib">API Attribute.</param>
private static string GetCleanFunctionSignature(MethodInfo method, LuaApiFunction functionAttrib, LuaApi apiAttrib)
{
string apiName = apiAttrib.luaName;
string functionName = functionAttrib.name;
string paramsString = string.Empty;
foreach (ParameterInfo param in method.GetParameters())
{
paramsString += string.Format ("{0}, ", param.Name);
}
// Clean up last comma and space
if (!string.IsNullOrEmpty (paramsString))
{
paramsString = paramsString.Remove (paramsString.Length - 2, 2);
}
return string.Format ("{0}.{1}({2})", apiName, functionName, paramsString);
}
/// <summary>
/// Gets a documentation friendly name for a type.
/// </summary>
/// <returns>The clean type name.</returns>
/// <param name="type">Type.</param>
private static string GetCleanTypeName(Type type)
{
string result;
// Ideally this would be a switch, but c# won't allow system.type in a switch
if (type == typeof(void))
{
result = "Nothing";
}
else if (type == typeof(float?))
{
result = "number (optional)";
}
else if (type == typeof(float[]))
{
result = "Table of numbers";
}
else if (type == typeof(int) ||
type == typeof(uint) ||
type == typeof(float))
{
result = "number";
}
else if (type == typeof(string))
{
result = "string";
}
else if (type == typeof(bool))
{
result = "bool";
}
else if (type == typeof(MoonSharp.Interpreter.DynValue))
{
result = "Lua Type";
}
else if (type == typeof(MoonSharp.Interpreter.Table))
{
result = "Lua Table";
}
else if (type == typeof(bool?))
{
result = "bool (optional)";
}
else
{
Logger.Log (Channel.Lua, Priority.Error, "Failed to convert type {0} to cleaner name", type.ToString ());
result = type.ToString ();
}
return result;
}
}
#endif // UNITY_EDITOR