Skip to content

Commit 5e2f733

Browse files
committed
Support XLua
1 parent 8142877 commit 5e2f733

File tree

2 files changed

+118
-39
lines changed

2 files changed

+118
-39
lines changed

Editor/PreferencesGUI.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43
using UnityEditor;
54
using UnityEngine;
6-
using UnityScript.Lang;
75

86

97
namespace litefeel.LuaInteractive.Editor
@@ -76,7 +74,7 @@ private static void CreateDefaultScript()
7674
if (File.Exists(path))
7775
{
7876
if (!EditorUtility.DisplayDialog("Save File",
79-
$"the file already exists, do you want to overwrite it?\n{path}",
77+
"the file already exists, do you want to overwrite it?\n" + path,
8078
"Overwrite", "Cancel"))
8179
{
8280
return;

Runtime/LuaRunner.cs

Lines changed: 117 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,64 @@ namespace litefeel.LuaInteractive
1212
{
1313
public class LuaRunner : MonoBehaviour
1414
{
15+
enum LuaType
16+
{
17+
None,
18+
ToLua,
19+
XLua,
20+
}
1521
private static Type LuaState;
1622
private static MethodInfo GetLuaState;
1723
private static MethodInfo DoString;
1824
private object luaState;
25+
private static LuaType luaType;
26+
1927

2028
[RuntimeInitializeOnLoadMethod]
2129
private static void Init()
2230
{
23-
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
24-
foreach (var assembly in assemblies)
25-
{
26-
LuaState = assembly.GetType("LuaInterface.LuaState");
27-
if (LuaState != null)
28-
break;
29-
}
30-
31-
if (LuaState != null)
32-
{
33-
GetLuaState = GetMethod(LuaState, "Get", new object[] { typeof(IntPtr) });
34-
DoString = GetMethod(LuaState, "DoString", new object[] { typeof(string), typeof(string) });
35-
}
36-
if (LuaState == null)
37-
Debug.LogError($"Cannot get type LuaInterface.LuaState");
38-
if (GetLuaState == null)
39-
Debug.LogError($"Cannot get method LuaInterface.LuaState:Get(IntPtr)");
40-
if (DoString == null)
41-
Debug.LogError($"Cannot get method LuaInterface.LuaState:DoString(string, string)");
31+
luaType = LuaType.None;
32+
if (IsToLua(out GetLuaState, out DoString))
33+
luaType = LuaType.ToLua;
34+
else if (IsXLua(out GetLuaState, out DoString))
35+
luaType = LuaType.XLua;
4236

4337
var debuger = new GameObject("_LuaRunner");
38+
debuger.hideFlags = HideFlags.DontSave;
4439
DontDestroyOnLoad(debuger);
4540
debuger.AddComponent<LuaRunner>();
4641
}
4742

48-
private static MethodInfo GetMethod(Type type, string name, object[] args)
49-
{
50-
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
51-
{
52-
if (!method.IsGenericMethod && method.Name == name)
53-
return method;
54-
}
55-
return null;
56-
}
43+
5744

5845
void Start()
5946
{
60-
if (GetLuaState != null && DoString != null)
47+
if (luaType != LuaType.None)
6148
StartCoroutine(WaitLuaState());
6249
}
6350

6451
private IEnumerator WaitLuaState()
6552
{
66-
var args = new object[] { IntPtr.Zero };
67-
while (GetLuaState.Invoke(null, args) == null)
68-
yield return null;
53+
if (luaType == LuaType.ToLua)
54+
{
55+
var args = new object[] { IntPtr.Zero };
56+
while (GetLuaState.Invoke(null, args) == null)
57+
yield return null;
58+
59+
yield return new WaitForSeconds(1);
6960

70-
yield return new WaitForSeconds(1);
61+
luaState = GetLuaState.Invoke(null, args);
62+
}
63+
else if (luaType == LuaType.XLua)
64+
{
65+
var args = new object[] {};
66+
while (GetLuaState.Invoke(null, args) == null)
67+
yield return null;
7168

72-
luaState = GetLuaState.Invoke(null, args);
69+
yield return new WaitForSeconds(1);
70+
71+
luaState = GetLuaState.Invoke(null, args);
72+
}
7373
}
7474

7575
private void Update()
@@ -79,10 +79,18 @@ private void Update()
7979
if (Settings.AutoClearLog == ClearLogMode.Previous)
8080
ClearLog();
8181
var path = Settings.ScriptPath;
82-
if (!string.IsNullOrEmpty(path) && File.Exists(path))
82+
if (luaState != null && !string.IsNullOrEmpty(path) && File.Exists(path))
8383
{
8484
var content = File.ReadAllText(path);
85-
DoString?.Invoke(luaState, new object[] { content, null });
85+
switch(luaType)
86+
{
87+
case LuaType.ToLua:
88+
DoString.Invoke(luaState, new object[] { content, null });
89+
break;
90+
case LuaType.XLua:
91+
DoString.Invoke(luaState, new object[] { content, null, null });
92+
break;
93+
}
8694
}
8795

8896
if (Settings.AutoClearLog == ClearLogMode.All)
@@ -97,6 +105,79 @@ private void ClearLog()
97105
MethodInfo method = type.GetMethod("Clear");
98106
method.Invoke(new object(), null);
99107
}
108+
109+
private static bool IsToLua(out MethodInfo GetLuaState, out MethodInfo DoString)
110+
{
111+
GetLuaState = null;
112+
DoString = null;
113+
Type LuaState = TryGetType("LuaInterface.LuaState");
114+
if (LuaState == null)
115+
return false;
116+
117+
GetLuaState = GetMethod(LuaState, "Get", new Type[] { typeof(IntPtr) });
118+
if (GetLuaState == null)
119+
return false;
120+
121+
DoString = GetMethod(LuaState, "DoString", new Type[] { typeof(string), typeof(string) });
122+
if (DoString == null)
123+
return false;
124+
125+
return true;
126+
}
127+
128+
private static bool IsXLua(out MethodInfo GetLuaState, out MethodInfo DoString)
129+
{
130+
GetLuaState = null;
131+
DoString = null;
132+
Type LuaManager = TryGetType("LuaManager");
133+
if (LuaManager == null)
134+
return false;
135+
136+
GetLuaState = GetMethod(LuaManager, "GetLuaEnv", new Type[0]);
137+
if (GetLuaState == null)
138+
return false;
139+
140+
var LuaTable = TryGetType("XLua.LuaTable");
141+
if (LuaTable == null)
142+
return false;
143+
var LuaEnv = TryGetType("XLua.LuaEnv");
144+
if (LuaEnv == null)
145+
return false;
146+
DoString = GetMethod(LuaEnv, "DoString", new Type[] { typeof(string), typeof(string), LuaTable });
147+
if (DoString == null)
148+
return false;
149+
150+
return true;
151+
}
152+
153+
154+
private static Type TryGetType(string fullname)
155+
{
156+
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
157+
foreach (var assembly in assemblies)
158+
{
159+
var type = assembly.GetType(fullname);
160+
if (type != null)
161+
return type;
162+
}
163+
return null;
164+
}
165+
166+
private static MethodInfo GetMethod(Type type, string name, Type[] args)
167+
{
168+
return type.GetMethod(name, args);
169+
170+
//foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
171+
//{
172+
// if (method.IsGenericMethod)
173+
// continue;
174+
// if (method.Name != name)
175+
// continue;
176+
// var margs = method.GetParameters();
177+
// return method;
178+
//}
179+
//return null;
180+
}
100181
}
101182
}
102183
#endif

0 commit comments

Comments
 (0)