-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSLibExport.cs
457 lines (414 loc) · 19.7 KB
/
JSLibExport.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
#nullable enable
#if UNITY_EDITOR
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Assets.UnityAngularBridge.SwaggerAttribute.Models;
using UnityEditor;
using UnityEngine;
namespace Assets.UnityAngularBridge.SwaggerAttribute
{
/// <summary>
/// Export DllImportAttribute Methods to TypeScript as some type of SwaggerClient to Plugins folder.
/// An example output is in the same directory as this file, referring to unity-jslib-exported.service.ts
/// .
/// NOTE: this method is from unity webinteractions lib, meaning it is JavaScript.
/// In Unity the method is set to the window object, which is why we first create a jslib containing that method.
/// See: https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
/// .
/// Do not put a if PLATFORM_WEBGL && !UNITY_EDITOR check around a method which should be included,
/// otherwise it will not be recognized by JSLibExport logic.
/// .
/// TODO: export JSLibClient file to frontend by placing this in a NPM package.
/// </summary>
[InitializeOnLoad]
public class JSLibExport
{
private static readonly string _jsLibFileName = "BrowserInteractions.jslib";
private static readonly string _jsLibClientFileName = "unity-jslib-exported.service.ts";
/// <summary>
/// The tabstring to use, since its for TypeScript it will be 2 spaces.
/// </summary>
private static readonly string _tabString = " ";
private static readonly List<JSLibVariable> _jSLibVariables = new();
static JSLibExport()
{
GenerateJSLib();
GenerateJSLibClient();
}
#region GenerateJSLib
/// <summary>
/// Generates JSLib File.
/// </summary>
private static void GenerateJSLib()
{
// Lines to write to file
using IndentedTextWriter writer = new(new StreamWriter(Path.Combine(GetPluginsPath(), _jsLibFileName)) { AutoFlush = true }, _tabString);
AddJsLibMergeIntoLine(writer);
AddJSLibMethodLines(writer);
AddJsLibClosingBracketsLine(writer);
}
/// <summary>
/// Adds "mergeInto(LibraryManager.library, {".
/// </summary>
private static void AddJsLibMergeIntoLine(IndentedTextWriter writer)
{
writer.WriteLine("mergeInto(LibraryManager.library, {");
writer.Indent++;
}
/// <summary>
/// Adds all JavaScript method lines.
/// </summary>
/// TODO: multi parameter support?
private static void AddJSLibMethodLines(IndentedTextWriter writer)
{
// Get Assembly
Assembly assembly = Assembly.GetExecutingAssembly();
// Get Classes Types
IEnumerable<Type>? publicClasses = assembly.GetExportedTypes().Where(p => p.IsClass);
foreach (Type? type in publicClasses)
{
// Get Class Methods with DllImportAttribute
IEnumerable<MethodInfo>? methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static)
.Where(m => m.GetCustomAttributes(typeof(DllImportAttribute), false).Length > 0);
foreach (MethodInfo? methodInfo in methodInfos)
{
// Create method data for JsLibExportService generation
JSLibVariable jslibVariable = new();
// Get methodName
string methodName = methodInfo.Name;
jslibVariable.MethodName = methodName;
// Get first parameter type and name if there is one
////string parameterType = string.Empty;
string parameterName = string.Empty;
ParameterInfo[] parameters = methodInfo.GetParameters();
if (parameters.Length > 1)
{
throw new InvalidOperationException($"Method {methodName} is only allowed to have 1 argument");
}
if (parameters.Length == 1)
{
// parameterType = ParameterTypeToTypescriptType(parameters[0].ParameterType);
parameterName = parameters[0].Name;
jslibVariable.ParameterName = parameterName;
jslibVariable.ReturnType = ReturnType.String;
jslibVariable.DefaultValue = "null";
// Get StringArrayAttribute if any
IEnumerable<MethodInfo>? methodInfosStringArrayAttribute = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static)
.Where(m => m.GetCustomAttributes(typeof(StringArrayAttribute), false).Length > 0);
foreach (MethodInfo? methodInfoStringArrayAttribute in methodInfosStringArrayAttribute)
{
jslibVariable.ReturnType = ReturnType.StringArray;
jslibVariable.DefaultValue = "[]";
}
}
else
{
jslibVariable.ReturnType = ReturnType.Void;
jslibVariable.DefaultValue = string.Empty;
}
// Add JsLibExportService variable data
_jSLibVariables.Add(jslibVariable);
AddJSLibMethodLines(writer, methodName, parameterName);
}
}
}
/// <summary>
/// [objectMethodName]: function ([ParameterName1], [ParameterName2]) {
/// window.[objectMethodName]FromUnity(UTF8ToString([ParameterName1]));
/// },.
/// </summary>
/// <param name="methodName">Method name.</param>
/// <param name="parameterName">Parameter name.</param>
/// TODO: add support for multiple params?
private static void AddJSLibMethodLines(IndentedTextWriter writer, string methodName, string? parameterName)
{
// If no parameters
if (string.IsNullOrEmpty(parameterName))
{
writer.WriteLine($"{methodName}: function ()" + " {");
writer.Indent++;
writer.WriteLine($"window.{FirstCharToLowerCase(methodName)}FromUnity();");
writer.Indent--;
writer.WriteLine("},");
writer.WriteLine();
}
else // Has 1 parameter
{
// Add Accessibility modifier and MethodName and first parameter
writer.WriteLine($"{methodName}: function ({parameterName}, size)" + " {");
writer.Indent++;
writer.WriteLine($"window.{FirstCharToLowerCase(methodName)}FromUnity(UTF8ToString({parameterName}));");
writer.Indent--;
writer.WriteLine("},");
writer.WriteLine();
}
}
/// <summary>
/// Adds end of file "});".
/// </summary>
private static void AddJsLibClosingBracketsLine(IndentedTextWriter writer)
{
writer.Indent--;
writer.WriteLine("});");
}
#endregion
#region GenerateJSLibClient
private static void GenerateJSLibClient()
{
// Lines to write to file
using IndentedTextWriter writer = new(new StreamWriter(Path.Combine(GetPluginsPath(), _jsLibClientFileName)) { AutoFlush = true }, _tabString);
AddAutoGeneratedFileCommentLines(writer);
AddImportLines(writer);
AddGlobalSubjectVariablesLines(writer);
AddCommentsAndInjectableAndClassLines(writer);
AddLocalSubjectVariablesLines(writer);
AddConstructorLines(writer);
AddSetupUnityListenersFunctionLines(writer);
AddUnityListenersFunctionLines(writer);
AddClassClosingLines(writer);
}
/// <summary>
/// Create autgenerated Text including eslint-disable text.
/// //----------------------
/// // <auto-generated>
/// // Generated using JSLibExport.cs by Jim Halewijn in UnityAngularBridge project.
/// // </auto-generated>
/// //----------------------
/// //
/// /* eslint-disable */
/// //.
/// </summary>
private static void AddAutoGeneratedFileCommentLines(IndentedTextWriter writer)
{
writer.WriteLine("//----------------------");
writer.WriteLine("// <auto-generated>");
writer.WriteLine("// Generated using JSLibExport.cs by Jim Halewijn in UnityAngularBridge project.");
writer.WriteLine("// </auto-generated>");
writer.WriteLine("//----------------------");
writer.WriteLine();
writer.WriteLine("/* eslint-disable */");
writer.WriteLine();
}
/// <summary>
/// import { Injectable } from '@angular/core';
/// import { BehaviorSubject, Observable, Subject } from "rxjs";
/// .
/// </summary>
private static void AddImportLines(IndentedTextWriter writer)
{
writer.WriteLine("import { Injectable } from \"@angular/core\";");
writer.WriteLine("import { BehaviorSubject, Observable, Subject } from \"rxjs\";");
writer.WriteLine();
}
/// <summary>
/// // NOTE: These subjects are used as a more global scope, so we can access it in JavaScript function of Unity.
/// List of: const mySubject: [Behavior?]Subject<T> = new [Behavior?]Subject<T>(optionalDefaultValue);
/// </summary>
private static void AddGlobalSubjectVariablesLines(IndentedTextWriter writer)
{
writer.WriteLine("// NOTE: These subjects are used as a more global scope, so we can access it in JavaScript function of Unity.");
// Add for each jslib variable
foreach(JSLibVariable jslibVariable in _jSLibVariables)
{
string? methodNameLowerCase = FirstCharToLowerCase(jslibVariable.MethodName);
string? returnTypeLowerCase = ConvertReturnTypeEnumToString(jslibVariable.ReturnType);
// string return type needs behaviorsubject as type
string? behaviorSubjectText = jslibVariable.ReturnType != ReturnType.Void ? "Behavior" : string.Empty;
string globalSubjectVariableLine = $"const {methodNameLowerCase}Subject: {behaviorSubjectText}Subject<{returnTypeLowerCase}> = new {behaviorSubjectText}Subject<{returnTypeLowerCase}>({jslibVariable.DefaultValue});";
writer.WriteLine(globalSubjectVariableLine);
}
writer.WriteLine();
}
/// <summary>
/// /**
/// * NOTE: these functions are from unity webinteractions lib, meaning it is JavaScript.
/// * See: https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
/// * In Unity the function is set to the window object, which is why we set it here with:
/// * window["myfunctionName"] = this.myfunctionName;
/// *
/// * 'this' does not work as it is JavaScript.
/// * As a workaround, myfunctionNameFromUnitySubject is set outside this class scope to access it and subscribe to these listeners.
/// *
/// * These names are from the jslib file in UnityAngularBridge repo '/Assets/Plugins/BrowserInteractions.jslib' file.
/// */
/// @Injectable({
/// providedIn: "root",
/// })
/// export class UnityJSLibExportedService {.
/// </summary>
private static void AddCommentsAndInjectableAndClassLines(IndentedTextWriter writer)
{
writer.WriteLine("/**");
writer.WriteLine(" * NOTE: these functions are from unity webinteractions lib, meaning it is JavaScript.");
writer.WriteLine(" * See: https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html");
writer.WriteLine(" * In Unity the function is set to the window object, which is why we set it here with:");
writer.WriteLine(" * window[\"myfunctionName\"] = this.myfunctionName;");
writer.WriteLine(" *");
writer.WriteLine(" * 'this' does not work as it is JavaScript.");
writer.WriteLine(" * As a workaround, myfunctionNameSubject is set outside this class scope to access it and subscribe to these listeners.");
writer.WriteLine(" *");
writer.WriteLine(" * These names are from the jslib file in UnityAngularBridge repo '/Assets/Plugins/BrowserInteractions.jslib' file.");
writer.WriteLine(" */");
writer.WriteLine("@Injectable({");
writer.Indent++;
writer.WriteLine("providedIn: \"root\",");
writer.Indent--;
writer.WriteLine("})");
writer.WriteLine("export class UnityJSLibExportedService {");
writer.Indent++;
}
/// <summary>
/// List of: public [methodName]$: Observable<T> = [globalSubjectVariableName].asObservable();
/// </summary>
private static void AddLocalSubjectVariablesLines(IndentedTextWriter writer)
{
// Add for each jslib variable
foreach (JSLibVariable jslibVariable in _jSLibVariables)
{
string? methodNameLowerCase = FirstCharToLowerCase(jslibVariable.MethodName);
string? returnTypeLowerCase = ConvertReturnTypeEnumToString(jslibVariable.ReturnType);
string globalSubjectVariableLine = $"public {methodNameLowerCase}$: Observable<{returnTypeLowerCase}> = {methodNameLowerCase}Subject.asObservable();";
writer.WriteLine(globalSubjectVariableLine);
}
writer.WriteLine();
}
/// <summary>
/// constructor() {
/// this.setupUnityListeners();
/// }.
/// </summary>
private static void AddConstructorLines(IndentedTextWriter writer)
{
writer.WriteLine("constructor() {");
writer.Indent++;
writer.WriteLine("this.setupUnityListeners();");
writer.Indent--;
writer.WriteLine("}");
writer.WriteLine();
}
/// <summary>
/// private setupUnityListeners(): void {
/// List of: window["[MethodName]FromUnity\"] = this.[MethodName]FromUnity;";
/// }.
/// </summary>
private static void AddSetupUnityListenersFunctionLines(IndentedTextWriter writer)
{
writer.WriteLine("private setupUnityListeners(): void {");
writer.Indent++;
// Add for each jslib variable
foreach (JSLibVariable jslibVariable in _jSLibVariables)
{
string? methodNameCamelCase = FirstCharToLowerCase(jslibVariable.MethodName);
writer.WriteLine($"window[\"{methodNameCamelCase}FromUnity\"] = this.{methodNameCamelCase}FromUnity;");
}
writer.Indent--;
writer.WriteLine("}");
writer.WriteLine();
}
/// <summary>
/// List of:
/// private [methodName]FromUnity([parameterName?]: [string | string[]): void {
/// const partKeys = [parameterName].split("|"); .<-- optional line depending on if has parameter
/// [methodName]Subject.next([parameterName?]);
/// }.
/// .
/// </summary>
private static void AddUnityListenersFunctionLines(IndentedTextWriter writer)
{
// Add for each jslib variable
foreach ((JSLibVariable jslibVariable, int index, bool isLastElement) in _jSLibVariables.Select((value, i) => (value, i, i == _jSLibVariables.Count - 1)))
{
string? methodNameCamelCase = FirstCharToLowerCase(jslibVariable.MethodName);
// Create window implementation lines
bool hasParameter = !string.IsNullOrEmpty(jslibVariable.ParameterName);
if (hasParameter && jslibVariable.ReturnType == ReturnType.StringArray)
{
string? parameterNameCamelCase = FirstCharToLowerCase(jslibVariable.ParameterName);
writer.WriteLine($"private {methodNameCamelCase}FromUnity({parameterNameCamelCase}: string): void" + " {");
writer.Indent++;
writer.WriteLine($"const split = {parameterNameCamelCase}.split(\"|\");");
writer.WriteLine($"{methodNameCamelCase}Subject.next(split);");
writer.Indent--;
writer.WriteLine("}");
}
else if (hasParameter)
{
string? parameterNameCamelCase = FirstCharToLowerCase(jslibVariable.ParameterName);
writer.WriteLine($"private {methodNameCamelCase}FromUnity({parameterNameCamelCase}: string): void" + " {");
writer.Indent++;
writer.WriteLine($"{methodNameCamelCase}Subject.next({parameterNameCamelCase});");
writer.Indent--;
writer.WriteLine("}");
}
else // has no parameter
{
writer.WriteLine($"private {methodNameCamelCase}FromUnity(): void" + " {");
writer.Indent++;
writer.WriteLine($"{methodNameCamelCase}Subject.next();");
writer.Indent--;
writer.WriteLine("}");
}
// Write newline if there is another element
if(!isLastElement)
{
writer.WriteLine();
}
}
}
/// <summary>
/// Line to close the class:
/// }.
/// </summary>
private static void AddClassClosingLines(IndentedTextWriter writer)
{
writer.Indent--;
writer.WriteLine("}");
}
/// <summary>
/// Convert value of ReturnType to JS returnType string.
/// </summary>
/// <param name="returnType">Return type enum value.</param>
/// <returns>Return type as JavaScript string.</returns>
private static string ConvertReturnTypeEnumToString(ReturnType returnType)
{
return returnType switch
{
ReturnType.Void => "void",
ReturnType.String => "string",
ReturnType.StringArray => "string[]",
_ => throw new InvalidOperationException($"ParameterType {returnType} is not supported." +
$"Only Void, String and StringArray are allowed."),
};
}
#endregion
#region pathUtilities
/// <summary>
/// Get plugins path.
/// </summary>
/// <returns>Plugins path.</returns>
private static string GetPluginsPath()
{
// Get the path of the Game data folder
// Unity Editor: <path to project folder>/Assets
// https://docs.unity3d.com/ScriptReference/Application-dataPath.html
return Application.dataPath + "/Plugins";
}
#endregion
#region stringUtilities
private static string? FirstCharToLowerCase(string? str)
{
if (!string.IsNullOrEmpty(str) && char.IsUpper(str[0]))
{
return str.Length == 1 ? char.ToLower(str[0]).ToString() : char.ToLower(str[0]) + str[1..];
}
return str;
}
#endregion
}
}
#endif