11
11
public class DllToLuaLib : Editor {
12
12
13
13
private static string [ ] LUA_KEYWORDS = { "local" , "function" , "end" , "then" } ;
14
- private static string [ ] DLL_NAMES = { "mscorlib" , "UnityEngine" , "Assembly-CSharp" } ;
14
+ private static string [ ] DLL_NAMES = {
15
+ "mscorlib" ,
16
+ #if UNITY_2017_1_OR_NEWER
17
+ "UnityEngine.CoreModule" ,
18
+ #else
19
+ "UnityEngine" ,
20
+ #endif
21
+ "Assembly-CSharp"
22
+ } ;
15
23
16
24
[ MenuItem ( "Tools/DllToLuaLib" , false , - 100 ) ]
17
25
private static void GenDlls ( )
@@ -20,7 +28,13 @@ private static void GenDlls()
20
28
List < MethodInfo > allExtensionMethodList = new List < MethodInfo > ( ) ;
21
29
foreach ( string dllName in DLL_NAMES )
22
30
{
23
- Assembly assembly = Assembly . Load ( dllName ) ;
31
+ Assembly assembly = null ;
32
+ try
33
+ {
34
+ assembly = Assembly . Load ( dllName ) ;
35
+ }
36
+ catch ( FileNotFoundException ) { }
37
+
24
38
if ( assembly != null )
25
39
{
26
40
Type [ ] types = assembly . GetTypes ( ) ;
@@ -79,11 +93,17 @@ private static void GenDlls()
79
93
foreach ( string nameSpace in nameSpaceSet )
80
94
{
81
95
string fileName = nameSpace + ".ns.lua" ;
82
- string content = nameSpace + " = {}" ;
96
+ StringBuilder contentSb = new StringBuilder ( ) ;
97
+ contentSb . Append ( "---@class " ) ;
98
+ contentSb . Append ( nameSpace ) ;
99
+ contentSb . AppendLine ( ) ;
100
+ contentSb . Append ( nameSpace ) ;
101
+ contentSb . Append ( " = {}" ) ;
102
+ string content = contentSb . ToString ( ) ;
83
103
fileDict [ fileName ] = Encoding . UTF8 . GetBytes ( content ) ;
84
104
}
85
- string zipFileName = Application . dataPath + "/../" + dllName + ".zip" ;
86
- ZipDerctory ( zipFileName , fileDict ) ;
105
+ string zipFileName = Application . dataPath + "/../LuaLib/ " + dllName + ".zip" ;
106
+ WriteZip ( zipFileName , fileDict ) ;
87
107
Debug . Log ( dllName + ".zip generating is complete!" ) ;
88
108
}
89
109
}
@@ -104,15 +124,15 @@ private static List<MethodInfo> GetExtensionMethods(Type extendedType, List<Meth
104
124
105
125
private static void GenType ( Type type , List < MethodInfo > extensionMethodList , out string fileName , out string content )
106
126
{
107
- string typeName = TypeToString ( type , true ) ;
127
+ string typeName = TypeToString ( type , false , true ) ;
108
128
string typeFileName = typeName + ".lua" ;
109
129
StringBuilder typeScriptSb = new StringBuilder ( ) ;
110
130
typeScriptSb . Append ( "---@class " ) ;
111
131
typeScriptSb . Append ( typeName ) ;
112
132
typeScriptSb . Append ( " : " ) ;
113
133
if ( type . BaseType != null )
114
134
{
115
- typeScriptSb . Append ( TypeToString ( type . BaseType , true ) ) ;
135
+ typeScriptSb . Append ( TypeToString ( type . BaseType , false , true ) ) ;
116
136
}
117
137
else
118
138
{
@@ -295,14 +315,18 @@ private static void GenType(Type type, List<MethodInfo> extensionMethodList, out
295
315
{
296
316
ParameterInfo param = paramList [ paramIndex ] ;
297
317
typeScriptSb . Append ( "---@param " ) ;
298
- typeScriptSb . Append ( GetParamName ( param ) ) ;
299
- typeScriptSb . Append ( " " ) ;
300
- typeScriptSb . Append ( TypeToString ( param . ParameterType ) ) ;
301
318
if ( param . IsDefined ( typeof ( ParamArrayAttribute ) , false ) )
302
319
{
303
- typeScriptSb . Append ( "| " ) ;
320
+ typeScriptSb . Append ( "... " ) ;
304
321
typeScriptSb . Append ( TypeToString ( param . ParameterType . GetElementType ( ) ) ) ;
322
+ typeScriptSb . Append ( "|" ) ;
323
+ }
324
+ else
325
+ {
326
+ typeScriptSb . Append ( GetParamName ( param ) ) ;
327
+ typeScriptSb . Append ( " " ) ;
305
328
}
329
+ typeScriptSb . Append ( TypeToString ( param . ParameterType ) ) ;
306
330
typeScriptSb . AppendLine ( ) ;
307
331
}
308
332
if ( returnList . Count > 0 )
@@ -328,14 +352,14 @@ private static void GenType(Type type, List<MethodInfo> extensionMethodList, out
328
352
}
329
353
typeScriptSb . Append ( methodName ) ;
330
354
typeScriptSb . Append ( "(" ) ;
331
- if ( paramList . Count > 0 )
332
- {
333
- typeScriptSb . Append ( GetParamName ( paramList [ 0 ] ) ) ;
334
- }
335
- for ( int paramIndex = 1 ; paramIndex < paramList . Count ; paramIndex ++ )
355
+ for ( int paramIndex = 0 ; paramIndex < paramList . Count ; paramIndex ++ )
336
356
{
337
- typeScriptSb . Append ( ", " ) ;
338
- typeScriptSb . Append ( GetParamName ( paramList [ paramIndex ] ) ) ;
357
+ if ( paramIndex > 0 )
358
+ {
359
+ typeScriptSb . Append ( ", " ) ;
360
+ }
361
+ ParameterInfo param = paramList [ paramIndex ] ;
362
+ typeScriptSb . Append ( param . IsDefined ( typeof ( ParamArrayAttribute ) , false ) ? "..." : GetParamName ( param ) ) ;
339
363
}
340
364
typeScriptSb . Append ( ") end" ) ;
341
365
typeScriptSb . AppendLine ( ) ;
@@ -351,7 +375,7 @@ private static void GenType(Type type, List<MethodInfo> extensionMethodList, out
351
375
content = typeScriptSb . ToString ( ) ;
352
376
}
353
377
354
- private static string TypeToString ( Type type , bool classDefine = false )
378
+ private static string TypeToString ( Type type , bool inFun = false , bool classDefine = false )
355
379
{
356
380
if ( ! classDefine )
357
381
{
@@ -382,15 +406,15 @@ private static string TypeToString(Type type, bool classDefine = false)
382
406
383
407
if ( type . IsArray )
384
408
{
385
- return TypeToString ( type . GetElementType ( ) ) + "[]" ;
409
+ return TypeToString ( type . GetElementType ( ) , inFun ) + "[]" ;
386
410
}
387
411
388
412
if ( type . IsGenericType )
389
413
{
390
414
Type [ ] genericArgTypes = type . GetGenericArguments ( ) ;
391
415
if ( genericArgTypes . Length == 1 && typeof ( IList < > ) . MakeGenericType ( genericArgTypes ) . IsAssignableFrom ( type ) )
392
416
{
393
- return TypeToString ( genericArgTypes [ 0 ] ) + "[]" ;
417
+ return TypeToString ( genericArgTypes [ 0 ] , inFun ) + "[]" ;
394
418
}
395
419
396
420
if ( genericArgTypes . Length == 2 && typeof ( IDictionary < , > ) . MakeGenericType ( genericArgTypes ) . IsAssignableFrom ( type ) )
@@ -406,14 +430,14 @@ private static string TypeToString(Type type, bool classDefine = false)
406
430
{
407
431
MethodInfo method = type == typeof ( Delegate ) || type == typeof ( MulticastDelegate ) ?
408
432
type . GetMethod ( "DynamicInvoke" ) : type . GetMethod ( "Invoke" ) ;
409
- return MethodToString ( method ) ;
433
+ return MethodToString ( method , inFun ) ;
410
434
}
411
435
}
412
436
413
437
if ( type . FullName == null )
414
438
{
415
439
//GenericTypeDefinition like T
416
- return TypeToString ( type . BaseType ?? typeof ( object ) ) ;
440
+ return TypeToString ( type . BaseType ?? typeof ( object ) , inFun , classDefine ) ;
417
441
}
418
442
419
443
char [ ] typeNameChars = type . ToString ( ) . ToCharArray ( ) ;
@@ -457,7 +481,7 @@ private static string TypeToString(Type type, bool classDefine = false)
457
481
return sb . ToString ( ) ;
458
482
}
459
483
460
- private static string MethodToString ( MethodInfo method )
484
+ private static string MethodToString ( MethodInfo method , bool inFun = false )
461
485
{
462
486
if ( method == null )
463
487
{
@@ -486,58 +510,61 @@ private static string MethodToString(MethodInfo method)
486
510
}
487
511
}
488
512
489
- return MethodToString ( paramList , returnList ) ;
513
+ return MethodToString ( paramList , returnList , inFun ) ;
490
514
}
491
515
492
- private static string MethodToString ( List < ParameterInfo > paramList , List < ParameterInfo > returnList )
516
+ private static string MethodToString ( List < ParameterInfo > paramList , List < ParameterInfo > returnList , bool inFun = false )
493
517
{
494
518
StringBuilder sb = new StringBuilder ( ) ;
495
- sb . Append ( "fun(" ) ;
496
- if ( paramList . Count > 0 )
519
+ if ( inFun && returnList . Count > 0 )
497
520
{
498
- sb . Append ( GetParamName ( paramList [ 0 ] ) ) ;
499
- sb . Append ( ":" ) ;
500
- sb . Append ( ParamTypeToString ( paramList [ 0 ] . ParameterType ) ) ;
521
+ sb . Append ( "(" ) ;
501
522
}
502
- for ( int paramIndex = 1 ; paramIndex < paramList . Count ; paramIndex ++ )
503
- {
504
- ParameterInfo param = paramList [ paramIndex ] ;
505
- sb . Append ( ", " ) ;
506
- sb . Append ( GetParamName ( param ) ) ;
507
- sb . Append ( ":" ) ;
508
- sb . Append ( ParamTypeToString ( param . ParameterType ) ) ;
523
+ sb . Append ( "fun(" ) ;
524
+ for ( int paramIndex = 0 ; paramIndex < paramList . Count ; paramIndex ++ )
525
+ {
526
+ if ( paramIndex > 0 )
527
+ {
528
+ sb . Append ( ", " ) ;
529
+ }
530
+ ParameterInfo param = paramList [ paramIndex ] ;
509
531
if ( param . IsDefined ( typeof ( ParamArrayAttribute ) , false ) )
510
532
{
511
- sb . Append ( "| " ) ;
533
+ sb . Append ( "...: " ) ;
512
534
sb . Append ( TypeToString ( param . ParameterType . GetElementType ( ) ) ) ;
535
+ sb . Append ( "|" ) ;
513
536
}
537
+ else
538
+ {
539
+ sb . Append ( GetParamName ( param ) ) ;
540
+ sb . Append ( ":" ) ;
541
+ }
542
+ sb . Append ( TypeToString ( param . ParameterType ) ) ;
514
543
}
515
544
sb . Append ( ")" ) ;
516
545
if ( returnList . Count > 0 )
517
546
{
518
547
sb . Append ( ":" ) ;
519
- sb . Append ( ParamTypeToString ( returnList [ 0 ] . ParameterType ) ) ;
548
+ if ( returnList . Count > 1 )
549
+ {
550
+ sb . Append ( "(" ) ;
551
+ }
520
552
for ( int returnIndex = 1 ; returnIndex < returnList . Count ; returnIndex ++ )
521
553
{
522
- sb . Append ( ", " ) ;
523
- sb . Append ( ParamTypeToString ( returnList [ returnIndex ] . ParameterType ) ) ;
554
+ if ( returnIndex > 0 )
555
+ {
556
+ sb . Append ( ", " ) ;
557
+ }
558
+ sb . Append ( TypeToString ( returnList [ returnIndex ] . ParameterType , returnList . Count == 1 ) ) ;
559
+ }
560
+ if ( returnList . Count > 1 )
561
+ {
562
+ sb . Append ( ")" ) ;
563
+ }
564
+ if ( inFun )
565
+ {
566
+ sb . Append ( ")" ) ;
524
567
}
525
- }
526
- return sb . ToString ( ) ;
527
- }
528
-
529
- private static string ParamTypeToString ( Type paramType )
530
- {
531
- StringBuilder sb = new StringBuilder ( ) ;
532
- bool isDelegate = typeof ( Delegate ) . IsAssignableFrom ( paramType ) ;
533
- if ( isDelegate )
534
- {
535
- sb . Append ( "(" ) ;
536
- }
537
- sb . Append ( TypeToString ( paramType ) ) ;
538
- if ( isDelegate )
539
- {
540
- sb . Append ( ")" ) ;
541
568
}
542
569
return sb . ToString ( ) ;
543
570
}
@@ -556,9 +583,15 @@ private static string GetParamName(ParameterInfo param)
556
583
return paramName ;
557
584
}
558
585
559
- private static void ZipDerctory ( string zipedDirectory , Dictionary < string , byte [ ] > fileDict , int compressionLevel = 9 )
586
+ private static void WriteZip ( string zipFileName , Dictionary < string , byte [ ] > fileDict , int compressionLevel = 9 )
560
587
{
561
- FileStream fileStream = File . Create ( zipedDirectory ) ;
588
+ FileInfo zipFile = new FileInfo ( zipFileName ) ;
589
+ DirectoryInfo dir = zipFile . Directory ;
590
+ if ( ! dir . Exists )
591
+ {
592
+ dir . Create ( ) ;
593
+ }
594
+ FileStream fileStream = zipFile . Create ( ) ;
562
595
ZipOutputStream zipStream = new ZipOutputStream ( fileStream ) ;
563
596
zipStream . SetLevel ( compressionLevel ) ;
564
597
foreach ( string fileName in fileDict . Keys )
@@ -567,7 +600,6 @@ private static void ZipDerctory(string zipedDirectory, Dictionary<string, byte[]
567
600
byte [ ] buffer = fileDict [ fileName ] ;
568
601
zipStream . Write ( buffer , 0 , buffer . Length ) ;
569
602
}
570
- zipStream . Flush ( ) ;
571
603
zipStream . Close ( ) ;
572
604
fileStream . Close ( ) ;
573
605
}
0 commit comments