topameng / tolua Goto Github PK
View Code? Open in Web Editor NEWThe fastest unity lua binding solution
License: MIT License
The fastest unity lua binding solution
License: MIT License
我Lua中写了一个扩展c#对象的方法,希望拦截__index的调用,通过使用setpeer无效,大致代码如下:
local peer = tolua.getpeer(node)
if not peer then
peer = {}
tolua.setpeer(t, peer)
end
local mt = {}
mt.__index = function(table, key)
-- 自定义查找属性方法
end
setmetatable(peer, mt)
-- 经过测试node.test方法并不会调用我的index函数
node.test
node为c#对象
请问能否支持该特性呢?
如何能编译一个可以在mac下使用的tolua.so?毕竟还是有一些mac平台的需求.
目前版本,mac下提示tolua.so找不到
目前使用sproto rpc 客户端有些包解析不了 服务端用的是最新的sproto
Unity 2017.3.0f3 on Windows 10
System.ArgumentException: The type 'UnityEngine.RaycastHit&' may not be used as a type argument.
LuaException: cannot open tolua.lua: No such file or directory
no file './tolua.lua'
no file '/usr/local/share/luajit-2.1.0-beta3/tolua.lua'
no file '/usr/local/share/lua/5.1/tolua.lua'
no file '/usr/local/share/lua/5.1/tolua/init.lua'
no file './Resources/tolua.lua'
no file '/storage/emulated/0/Android/data/com.tolua.test/files/Android/Lua/tolua.lua'
LuaInterface.LuaState:LoadFileBuffer(String)
LuaInterface.LuaState:DoFile(String)
LuaInterface.LuaState:OpenBaseLuaLibs()
LuaInterface.LuaState:Start()
TestPerformance:Start()
(Filename: Line: -1)
新版本导出DG.Tweening.TweenParams、DG.Tweening.EaseFactory编译报错,报错如下:
ToLua/Source/Generate/DG_Tweening_TweenParamsWrap.cs(189,41): error CS0117: `LuaInterface.ToLua' does not contain a definition for `ToNullable'
发现ToLuaExport.cs
中有以下代码:
else if (varType.IsGenericType && varType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type t = TypeChecker.GetNullableType(varType);
if (beCheckTypes)
{
sb.AppendFormat("{0}{1} {2} = ToLua.ToNullable<{3}>(L, {4});\r\n", head, str, arg, GetTypeStr(t), stackPos);
}
else
{
sb.AppendFormat("{0}{1} {2} = ToLua.CheckNullable<{3}>(L, {4});\r\n", head, str, arg, GetTypeStr(t), stackPos);
}
}
但ToLua.cs
中并没有相关方法实现
测试代码片段:
self.testCo = nil;
self.testCo = coroutine.start(function()
Debug.LogWarning("99999");
coroutine.wait(0.5);
--coroutine.stop(self.testCo);--调用A:这里调用似乎无效
while (true) do
coroutine.wait(0.5);
Debug.LogWarning("8888");
end
end);
coroutine.start(function()
coroutine.wait(0.5);
coroutine.stop(self.testCo);--调用B:这里调用可以结束
end);
我们在Unity 5.5.1测试发现,lua调用已经被销毁的UnityEngine.Object的时候,捕获的Exception的Message成员为空,导致无法输出有意义的错误日志,我们的Fix如下
public static int toluaL_exception(IntPtr L, Exception e, string msg = null)
{
msg = msg == null ? e.Message : msg;
msg = string.IsNullOrEmpty(msg) ? e.GetType().ToString() : msg; //<- fixed
LuaException.luaStack = new LuaException(msg, e, 2);
return tolua_error(L, msg);
}
另外,生成的Wrap,比如Text, 所判断的o==null实际上是无法判定UnityEngine.Object被销毁(C++部分)的情况,需要cast成为UnityEngine.Object再判断。
static int set_supportRichText(IntPtr L)
{
object o = null;
try
{
#if UNITY_EDITOR
GameLogic.DebugConfig.Instance.Lua2CSharpeCallCount++;//auto_gen counting lua invoking csharp
#endif
o = ToLua.ToObject(L, 1);
UnityEngine.UI.Text obj = (UnityEngine.UI.Text)o;
bool arg0 = LuaDLL.luaL_checkboolean(L, 2);
obj.supportRichText = arg0;
return 0;
}
catch(Exception e)
{
return LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index supportRichText on a nil value" : e.Message);
}
}
crash_a7329b9945fada4fb0bf4e6f917747f1.zip
我们使用的是tolua 版本是 1.0.7,zip里面附带了tolua.dll, error.log和dump
public LuaByteBuffer(IntPtr source, int len)
: this()
{
buffer = new byte[len];
Length = len;
Marshal.Copy(source, buffer, 0, len);
}
public LuaByteBuffer(byte[] buf)
: this()
{
buffer = buf;
Length = buf.Length;
}
public LuaByteBuffer(byte[] buf, int len)
: this()
{
buffer = buf;
Length = len;
}
public LuaByteBuffer(System.IO.MemoryStream stream)
: this()
{
buffer = stream.GetBuffer();
Length = (int)stream.Length;
}
public LuaDictEntry(K key, V value)
: this()
{
Key = key;
Value = value;
}
如题,在使用zerobrane调试require进来的Lua代码时,断点无法断到
不确定lua文件返回多少个值,所以用的是luaState.DoFile<object[]>(filename); 返回了一个object[0]的数组
调试了下,在ToLua的 public static object[] CheckObjectArray(IntPtr L, int stackPos) 方法的 int len = LuaDLL.lua_objlen(L, stackPos); 里返回的长度是0。不知为何
老版的DoFile直接返回object[]的正常
在开发过程中有时候需要绑定一个lua table作为回调对象
请求在LuaFunction增加Push(LuaTable).
L.RegVar("childCount", get_childCount, null);
我用的是transform.getget_childCount;
ToLuaExport.cs 中生成代码部分有一行
fname = beCheckTypes ? "ToParamsString" : "CheckParamsString";
实际 ToLua 中只有ToParamsString, 并没有 CheckParamsString 函数.
通过
public T DoFile(string fileName),加载的文件
运行到:
protected T LuaLoadBuffer(byte[] buffer, string chunkName)
的CheckValue时抛出如下错误:
1.
LuaException: bad argument #2 (object[] or table expected, got none)
LuaInterface.LuaDLL:luaL_argerror(IntPtr, Int32, String) (at Assets/ToLua/ToLua/Core/LuaDLL.cs:674)
LuaInterface.LuaDLL:luaL_typerror(IntPtr, Int32, String, String) (at Assets/ToLua/ToLua/Core/LuaDLL.cs:688)
LuaInterface.ToLua:CheckObjectArray(IntPtr, Int32) (at Assets/ToLua/ToLua/Core/ToLua.cs:1500)
LuaInterface.LuaState:CheckValue(Int32) (at Assets/ToLua/ToLua/Core/LuaState.cs:1659)
LuaInterface.LuaState:LuaLoadBuffer(Byte[], String) (at Assets/ToLua/ToLua/Core/LuaState.cs:2129)
LuaInterface.LuaState:DoFile(String) (at Assets/ToLua/ToLua/Core/LuaState.cs:629)
20:42:56.584-5: Lua stack top is 1
UnityEngine.Debug:LogWarning(Object)
LuaInterface.Debugger:LogWarning(String)
LuaInterface.Debugger:LogWarning(String, Object)
LuaInterface.LuaState:CheckTop() (at Assets/ToLua/ToLua/Core/LuaState.cs:1135)
LuaLooper:Update() (at Assets/ToLua/ToLua/Misc/LuaLooper.cs:101)
貌似不用带返回值的DoFile不会出现此错误,这问题是我使用问题还是别的原因引起的?因为对这方面不是很了解。
我切到不带返回值会有什么影响吗?
现在reimport后会清空掉对应的preload表函数,会不会导致无法实现真正目的。因为声明在CustomSettings的dynamicList中的C#类型的唯一的require进lua脚本的路径已经被截断了,而此时重新require可能不会加载想要的C#组件的东西进来
这是我在测试tolua#中的例子:11_Delegate,时发现的。
具体看我下面的截取代码中注释说明。
static bool CompareLuaDelegate(LuaDelegate a, LuaDelegate b)
{
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
object l = a;
object r = b;
if (l == null && r != null)
{
return b.func == null && b.self == null;
}
if (l != null && r == null)
{
//return a.func == null && b.self == null; // 这句错了,说明: b也就r既然已经是一个null,怎么还能调用它下面的成员self呢。
return a.func == null && a.self == null; // 应该改成这样
}
if (a.func != b.func || a.self != b.self)
{
return false;
}
return a.func != null;
}
I want to use the LuaDLL.lua_sethook to hook some lua code, but I can not get the IntPtr L directly. I have subcall the LuaState and add a Get method to get the 'L'.
How about add a public method to get it directly?
Timer的Update里都要加个self.running判断,不然remove后还会被执行一次
使用luasocket接收数据问题,应该怎么解决?
描述:
在第一次执行luasocket.select({self._socket}, ti)和self._socket:receive()时,Unity直接卡死。
这是有一次没有卡死的时间输出,明显第一次调用时间更久,后续的就正常了。无论在lua协程还是Unity协程StartCoroutine都是一样的。
第一次执行select和receive开始的时间和结束时间
14:50:46.21-309: [LuaNetManager.lua:159]:1 receive time: 1508223046.0215
14:50:53.972-309: [LuaNetManager.lua:162]:2 receive time: 1508223053.9727
第二次执行select和receive开始的时间和结束时间
14:50:54.687-352: [LuaNetManager.lua:159]:1 receive time: 1508223054.6877
14:50:54.688-352: [LuaNetManager.lua:162]:2 receive time: 1508223054.6886
第三次执行select和receive开始的时间和结束时间
14:50:55.720-414: [LuaNetManager.lua:159]:1 receive time: 1508223055.7204
14:50:55.721-414: [LuaNetManager.lua:162]:2 receive time: 1508223055.7214
环境:
Windows 8.1
Unity 5.3.5f1
在41行代码里,输出map.name是没有值的,我通过修改下面的代码能输入map.name值,这是语言的一个bug?还是就只能这样来获取?
Debugger.Log("Read varTable from lua, default: {0} name: {1} : {2}", table["default"], ((LuaTable)table["map"])["name"], table["map.name"]);
PlayServiceResolver是Google的一个unity原生插件管理工具,集成Admob会默认安装它。安装以后,tolua的生成绑定会报出异常。原因是PlayServiceResolver添加了自己的AssemblyResolve事件,在这个事件里出现assembly找不到的情况。而tolua在ToLuaExports.ProcessExtends里用到了GetType,在绑定时有些extends要查询的类名里有“,”,比如ToLua_System_Dictionary<string,string>,GetType就把它当有assembly name的类名,触发AssemblyResolve事件,然后PlayServiceResolver就出异常了。
解决办法很简单只要把
tolua/Assets/ToLua/Editor/ToLuaExport.cs
Line 4272 in 21143b1
extendName = "ToLua_" + className.Replace(".", "_").Replace(",", "_");
使用UNITY_EDITOR宏限制函数后,build工程的时报错说找不到其中的函数。
UIPanel.cs
static public Vector2 GetMainGameViewSize ()
...
UIPanelWrap.cs
UIPanel' does not contain a definition for
GetMainGameViewSize'
这个除了取消宏,有其他方法解决吗,麻烦看下,谢谢
*环境:unity4.7.2f1,VS2015
*步骤:
.我把.cs的代码移动到我新建的VS工程,打算编译一个独立的dll,wrap的导出路径也设置到了新的vs工程中,成功编译
.手动导出wrap,把wrap导入新的VS工程,成功编译
.生成的dll放入Asset中
.测试Hellow World场景,发现tolua.lua加载失败,将ToLua/Lua目录AddSearchPath,成功加载tolua.lua
.再运行后,Unity编辑器崩溃
.跟踪后发现是最后的调用栈是在ToLua.Print(IntPtr L)调用完以后就崩溃了
请问下目前兼不兼容目前最新的Unity 2017.2.1稳定版?
Hi, I would like to know if this plugin will support Unity 2017 ?
line 772
public static void luaL_checkstack(IntPtr L, int space, string mes)
{
if (lua_checkstack(L, space) == 0)
{
throw new LuaException(string.Format("stack overflow (%s)", mes)); //<- should be {0}
}
}
事情是这样的,我写了个Compoent,在OnDestroy里暴露了一个委托,方便在lua里初始化GameObject面板时,直接Add这个Component,然后可以在这个GameObject销毁时,做一些销毁工作
然后当游戏结束的时候,因为LuaState被销毁了,导致委托里对应的LuaFunction在调用时会抛出错误,实际上应该不影响正常游戏,但开发的时候比较尴尬
我不是很清楚应该要怎么处理这个事情(目前临时写了个try-catch)
或者是否有办法判断出委托中哪些是LuaFunction?LuaFunction的是否可以调用的判断是不是可以暴露给用户去做?
实现print的地方如果將Debugger.Log()改成UnityEngine.Debug.Log()則是可以的
只有exe文件,IOS的编译命令没有
generating operator[] wrap function will cause compile error,
System.Xml.XmlAttributeCollection for example
委托的LUA_TFUNCTION的调用者
可能是LUA_TTABLE
也可能是C#的LUA_TUSERDATA
tolua/Assets/ToLua/Core/ObjectTranslator.cs
ObjectTranslator中objectsBackMap为何要使用CompareObject作为Comparer呢?用系统默认的不可以么,感谢!
I find tolua can to export the interface to lua code, I hope tolua can support it.
大神你好,我喜欢用eclipse, 刚做了一个小工具: https://github.com/ps5mh/tolua-ldtdoc 想刷下存在感.
我发现tolua require文件的时候, 跟标准lua有些出入, 导致不能兼容Eclipse的调试器.
不知能否对static int Loader(IntPtr L) ToLua.cs这个函数做一下修改:
/if (LuaDLL.luaL_loadbuffer(L, buffer, buffer.Length, fileName) != 0)/
if (LuaDLL.luaL_loadbuffer(L, buffer, buffer.Length, "@" + fileName + ".lua") != 0)
使得required之后的文件名在调试信息中变为 @module.lua 这样就像原生lua一样了.
在lua中使用Trim接口时会报
14: attempt to call method 'Trim' (a nil value)
这个错误
具体例子:
accountInput = find('AccountInput'):GetComponent('UIInput');
log("value type:"..type(accountInput.value));
accountInput.value:Trim();
其中打印的type是lua的string类型:
value type:string
而不是userdata。
这种错误是因为lua的string类型覆盖了C#的吗?
如果是的话,有办法解决吗?
我现在的办法是在lua里实现trim方法,但是感觉有点不顺手~~~
麻烦大神看下,谢谢!
C# 中代码:
lua接口:
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int GetAttr(IntPtr L)
{
try
{
ToLua.CheckArgsCount(L, 2);
Giu.Basic.DataType.Entity obj = (Giu.Basic.DataType.Entity)ToLua.CheckObject(L, 1, typeof(Giu.Basic.DataType.Entity));
string arg0 = ToLua.CheckString(L, 2);
Giu.Basic.DataType.Var o = obj.GetAttr(arg0);
if (arg0 == "captain_slot")
{
UnityEngine.Debug.LogError(string.Format("LuaGetAttr: {0}=>{1}", arg0, o.Obj));
}
ToLua.PushObject(L, o);
return 1;
}
catch(Exception e)
{
return LuaDLL.toluaL_exception(L, e);
}
}
Entity中GetAttr定义:
public Var GetAttr(string name) {
if(name == "captain_slot")
{
UnityEngine.Debug.LogError(string.Format("GetAttr: {0}=>{1}", name, _attrs[def.EntryName2Ind(name)]));
}
return _attrs[def.EntryName2Ind(name)];
}
lua中的调用
local key = entity.def.AllAttrNames:GetItem(i)
local entry = entity.def:GetEntry(key)
if table.indexof({"captain_slot"}, key) then
LogError("__Enter Attr")
end
local attr = entity:GetAttr(key);
if table.indexof({"captain_slot"}, key) then
if not tonumber(attr:ToString()) then
local attr2 = entity:GetAttr(key);
LogError(attr2)
end
LogError("__LeaveAttr")
LogError(key)
LogError(attr)
end
在c#中的输出都表明GetAttr拿到的时1,而lua中拿到的值时i:base
看了一下1.0.6的,不算是修复了。
用QQ群讨论完就找不回来了,为什么不使用issue来讨论,也方便搜索引擎查找
excuse me ? in luabyte mode , the lua exception cant locate the line number, they r always 0 line,?
LuaBeatEvent.cs文件里并没有相关的方法,运行报错。。。
今天升级了tolua,原来lua里使用的EventDelegate().Callback(cb)会报错
经查是PushObject里加入了o.Equals(null)的判断。
EventDelegate重载了Equals,传入null时,返回是true,所以就造成了lua里得不到EventDelegate
/Assets/ToLua/Examples/17_Inherit/TestInherit.cs
这里面的FindChild方法,在2017上该用Find
比如说我写了一个类
static class ExtendedIDisposable
{
public static void Test (this IDisposable disposable) {}
}
这种, 建议在 ProcessExtendType()方法中加入对接口类型扩展方法的支持,比如 t.IsInterface && t.IsAssignableFrom(type)
I am start a coroutine and resume from somewhere, but I found if there is any error in lua code in the coroutine after coroutine.yield will not print any error~
public static void Push(IntPtr L, object obj)方法的
else if (t == typeof(LayerMask))
{
Push(L, (LayerMask)obj);
}
这里并没有LayerMask的push,造成不断的递归自己,应该是
PushLayerMask(L, (LayerMask)obj);方法吧
有一个静态类:
public static class MyClass
{
public static event MyEvent;
}
在Lua中使用:
MyClass.MyEvent = MyClass.MyEvent + MyFunc
MyClass.MyEvent = MyClass.MyEvent - MyFunc
最后移除的时候会报错:
System.Action ev = (System.Action)LuaMisc.GetEventHandler(null, typeof(Scheduler), "FrameEvent");
这个函数LuaMisc.GetEventHandler通过反射GetField是获取不到静态event,每次总是返回空值。
调用 LuaFunction Call 出现 stack overflow
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.