2222
2323namespace SLua
2424{
25- using UnityEngine ;
26- using System . Collections ;
27- using System . Collections . Generic ;
25+ using UnityEngine ;
26+ using System . Collections ;
27+ using System . Collections . Generic ;
2828 using System . Linq ;
29- using System . IO ;
30- using System ;
31- using System . Reflection ;
32- using UnityEditor ;
33- using System . Text ;
34- using System . Text . RegularExpressions ;
35- using System . Runtime . CompilerServices ;
29+ using System . IO ;
30+ using System ;
31+ using System . Reflection ;
32+ using UnityEditor ;
33+ using System . Text ;
34+ using System . Text . RegularExpressions ;
35+ using System . Runtime . CompilerServices ;
3636
37- public interface ICustomExportPost { }
37+ public interface ICustomExportPost { }
3838
3939
4040 public class LuaCodeGen : MonoBehaviour
41- {
42- static public string GenPath = SLuaSetting . Instance . UnityEngineGeneratePath ;
41+ {
42+ static public string GenPath = SLuaSetting . Instance . UnityEngineGeneratePath ;
4343 static public string WrapperName = "sluaWrapper.dll" ;
4444 public delegate void ExportGenericDelegate ( Type t , string ns ) ;
45-
45+
4646 static bool autoRefresh = true ;
4747
48- static bool IsCompiling {
49- get {
50- if ( EditorApplication . isCompiling ) {
48+ static bool IsCompiling
49+ {
50+ get
51+ {
52+ if ( EditorApplication . isCompiling )
53+ {
5154 Debug . Log ( "Unity Editor is compiling, please wait." ) ;
52- }
53- return EditorApplication . isCompiling ;
54- }
55- }
56-
57- [ InitializeOnLoad ]
58- public class Startup
59- {
60- static bool isPlaying = false ;
61- static Startup ( )
62- {
63- EditorApplication . update += Update ;
64- // use this delegation to ensure dispose luavm at last
65- EditorApplication . playmodeStateChanged += ( ) => {
66-
67- if ( isPlaying == true && EditorApplication . isPlaying == false ) {
68- if ( LuaState . main != null ) LuaState . main . Dispose ( ) ;
69- }
55+ }
56+ return EditorApplication . isCompiling ;
57+ }
58+ }
7059
71- isPlaying = EditorApplication . isPlaying ;
72- } ;
73- }
60+ [ InitializeOnLoad ]
61+ public class Startup
62+ {
63+ static bool isPlaying = false ;
64+ static Startup ( )
65+ {
66+ EditorApplication . update += Update ;
67+ // use this delegation to ensure dispose luavm at last
68+ EditorApplication . playmodeStateChanged += ( ) =>
69+ {
7470
71+ if ( isPlaying == true && EditorApplication . isPlaying == false )
72+ {
73+ if ( LuaState . main != null ) LuaState . main . Dispose ( ) ;
74+ }
7575
76- static void Update ( ) {
77- EditorApplication . update -= Update ;
78- Lua3rdMeta . Instance . ReBuildTypes ( ) ;
76+ isPlaying = EditorApplication . isPlaying ;
77+ } ;
78+ }
79+
80+
81+ static void Update ( )
82+ {
83+ EditorApplication . update -= Update ;
84+ Lua3rdMeta . Instance . ReBuildTypes ( ) ;
7985
8086 // Remind user to generate lua interface code
81- var remindGenerate = ! EditorPrefs . HasKey ( "SLUA_REMIND_GENERTE_LUA_INTERFACE" ) || EditorPrefs . GetBool ( "SLUA_REMIND_GENERTE_LUA_INTERFACE" ) ;
87+ var remindGenerate = ! EditorPrefs . HasKey ( "SLUA_REMIND_GENERTE_LUA_INTERFACE" ) || EditorPrefs . GetBool ( "SLUA_REMIND_GENERTE_LUA_INTERFACE" ) ;
8288 bool ok = System . IO . Directory . Exists ( GenPath + "Unity" ) || System . IO . File . Exists ( GenPath + WrapperName ) ;
83- if ( ! ok && remindGenerate )
84- {
85- if ( EditorUtility . DisplayDialog ( "Slua" , "Not found lua interface for Unity, generate it now?" , "Generate" , "No" ) )
86- {
87- GenerateAll ( ) ;
88- }
89- else
90- {
91- if ( ! EditorUtility . DisplayDialog ( "Slua" , "Remind you next time when no lua interface found for Unity?" , "OK" ,
92- "Don't remind me next time!" ) )
93- {
89+ if ( ! ok && remindGenerate )
90+ {
91+ if ( EditorUtility . DisplayDialog ( "Slua" , "Not found lua interface for Unity, generate it now?" , "Generate" , "No" ) )
92+ {
93+ GenerateAll ( ) ;
94+ }
95+ else
96+ {
97+ if ( ! EditorUtility . DisplayDialog ( "Slua" , "Remind you next time when no lua interface found for Unity?" , "OK" ,
98+ "Don't remind me next time!" ) )
99+ {
94100 EditorPrefs . SetBool ( "SLUA_REMIND_GENERTE_LUA_INTERFACE" , false ) ;
95- }
96- else
97- {
98-
101+ }
102+ else
103+ {
104+
99105 EditorPrefs . SetBool ( "SLUA_REMIND_GENERTE_LUA_INTERFACE" , true ) ;
100- }
101-
102- }
103- }
104- }
106+ }
105107
106- }
107-
108- [ MenuItem ( "SLua/All/Make" ) ]
109- static public void GenerateAll ( )
110- {
108+ }
109+ }
110+ }
111+
112+ }
113+
114+ [ MenuItem ( "SLua/All/Make" ) ]
115+ static public void GenerateAll ( )
116+ {
111117 autoRefresh = false ;
112118 Generate ( ) ;
113- GenerateUI ( ) ;
119+ GenerateUI ( ) ;
114120 GenerateAds ( ) ;
115- Custom ( ) ;
116- Generate3rdDll ( ) ;
121+ Custom ( ) ;
122+ Generate3rdDll ( ) ;
117123 autoRefresh = true ;
118124 AssetDatabase . Refresh ( ) ;
119125 }
120126
121- static bool filterType ( Type t , List < string > noUseList , List < string > uselist )
122- {
123- if ( t . IsDefined ( typeof ( CompilerGeneratedAttribute ) , false ) ) {
124- Debug . Log ( t . Name + " is filtered out" ) ;
125- return false ;
126- }
127-
128- // check type in uselist
129- string fullName = t . FullName ;
130- if ( uselist != null && uselist . Count > 0 )
131- {
132- return uselist . Contains ( fullName ) ;
133- }
134- else
135- {
136- // check type not in nouselist
137- foreach ( string str in noUseList )
138- {
139- if ( fullName . Contains ( str ) )
140- {
141- return false ;
142- }
143- }
144- return true ;
145- }
146- }
127+ static bool filterType ( Type t , List < string > noUseList , List < string > uselist )
128+ {
129+ if ( t . IsDefined ( typeof ( CompilerGeneratedAttribute ) , false ) )
130+ {
131+ Debug . Log ( t . Name + " is filtered out" ) ;
132+ return false ;
133+ }
134+
135+ // check type in uselist
136+ string fullName = t . FullName ;
137+ if ( uselist != null && uselist . Count > 0 )
138+ {
139+ return uselist . Contains ( fullName ) ;
140+ }
141+ else
142+ {
143+ // check type not in nouselist
144+ foreach ( string str in noUseList )
145+ {
146+ if ( fullName . Contains ( str ) )
147+ {
148+ return false ;
149+ }
150+ }
151+ return true ;
152+ }
153+ }
147154
148155 [ MenuItem ( "SLua/Unity/Make UnityEngine" ) ]
149156 static public void Generate ( )
150157 {
158+ #if UNITY_2017_2_OR_NEWER
159+ GenerateFor ( "UnityEngine.CoreModule" , "Unity/" , 0 , "BindUnity" ) ;
160+ #else
151161 GenerateFor ( "UnityEngine" , "Unity/" , 0 , "BindUnity" ) ;
162+ #endif
152163 }
153164
154165 [ MenuItem ( "SLua/Unity/Make UnityEngine.UI" ) ]
@@ -428,7 +439,7 @@ static public void ClearALL()
428439 [ MenuItem ( "SLua/Compile LuaObject To DLL" ) ]
429440 static public void CompileDLL ( )
430441 {
431- #region scripts
442+ #region scripts
432443 List < string > scripts = new List < string > ( ) ;
433444 string [ ] guids = AssetDatabase . FindAssets ( "t:Script" , new string [ 1 ] { Path . GetDirectoryName ( GenPath ) } ) . Distinct ( ) . ToArray ( ) ;
434445 int guidCount = guids . Length ;
@@ -445,9 +456,9 @@ static public void CompileDLL()
445456 Debug . LogError ( "No Scripts" ) ;
446457 return ;
447458 }
448- #endregion
459+ #endregion
449460
450- #region libraries
461+ #region libraries
451462 List < string > libraries = new List < string > ( ) ;
452463 string [ ] referenced = new string [ ] { "UnityEngine" , "UnityEngine.UI" } ;
453464 string projectPath = Path . GetFullPath ( Application . dataPath + "/.." ) . Replace ( "\\ " , "/" ) ;
@@ -464,13 +475,13 @@ static public void CompileDLL()
464475 libraries . Add ( path ) ;
465476 }
466477 }
467- #endregion
478+ #endregion
468479
469480 //generate AssemblyInfo
470481 string AssemblyInfoFile = Application . dataPath + "/AssemblyInfo.cs" ;
471482 File . WriteAllText ( AssemblyInfoFile , string . Format ( "[assembly: UnityEngine.UnityAPICompatibilityVersionAttribute(\" {0}\" )]" , Application . unityVersion ) ) ;
472483
473- #region mono compile
484+ #region mono compile
474485 string editorData = EditorApplication . applicationContentsPath ;
475486#if UNITY_EDITOR_OSX && ! UNITY_5_4_OR_NEWER
476487 editorData += "/Frameworks" ;
@@ -491,9 +502,9 @@ static public void CompileDLL()
491502#if UNITY_EDITOR_WIN
492503 mcs += ".bat" ;
493504#endif
494- #endregion
505+ #endregion
495506
496- #region execute bash
507+ #region execute bash
497508 StringBuilder output = new StringBuilder ( ) ;
498509 StringBuilder error = new StringBuilder ( ) ;
499510 bool success = false ;
@@ -550,7 +561,7 @@ static public void CompileDLL()
550561 {
551562 Debug . LogError ( ex ) ;
552563 }
553- #endregion
564+ #endregion
554565
555566 Debug . Log ( output . ToString ( ) ) ;
556567 if ( success )
@@ -604,8 +615,17 @@ static bool Generate(Type t, string ns, string path)
604615
605616 static void GenerateBind ( List < Type > list , string name , int order , string path )
606617 {
607- // delete wrapper dll
608- System . IO . File . Delete ( GenPath + WrapperName ) ;
618+ // delete wrapper dll
619+ try
620+ {
621+ System . IO . File . Delete ( GenPath + WrapperName ) ;
622+ }
623+ catch { }
624+
625+ if ( ! Directory . Exists ( path ) )
626+ {
627+ Directory . CreateDirectory ( path ) ;
628+ }
609629
610630 CodeGenerator cg = new CodeGenerator ( ) ;
611631 cg . path = path ;
0 commit comments