static int op_Subtraction(IntPtr L) { try { Delegate arg0 = (Delegate)ToLua.ToObject(L, 1); //不能增加引用计数,否则自身包含的都会增加 LuaTypes type = LuaDLL.lua_type(L, 2); if (type == LuaTypes.LUA_TFUNCTION) { LuaFunction func = ToLua.ToLuaFunction(L, 2); Delegate[] ds = arg0.GetInvocationList(); for (int i = 0; i < ds.Length; i++) { LuaDelegate target = ds[i].Target as LuaDelegate; if (target != null && target.func == func && target.self == null) { arg0 = Delegate.Remove(arg0, ds[i]); if (arg0 != null) { arg0.AddRef(); //产生新的委托需要增加计数 } break; } } func.Dispose(); ToLua.Push(L, arg0); return(1); } else { Delegate arg1 = (Delegate)ToLua.CheckObject(L, 2); arg0 = DelegateFactory.RemoveDelegate(arg0, arg1); ToLua.Push(L, arg0); return(1); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int op_Subtraction(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); Delegate arg0 = (Delegate)ToLua.CheckObject(L, 1, typeof(Delegate)); LuaTypes type = LuaDLL.lua_type(L, 2); if (type == LuaTypes.LUA_TFUNCTION) { LuaState state = LuaState.Get(L); LuaFunction func = ToLua.ToLuaFunction(L, 2); Delegate[] ds = arg0.GetInvocationList(); for (int i = 0; i < ds.Length; i++) { LuaDelegate ld = ds[i].Target as LuaDelegate; if (ld != null && ld.func == func && ld.self == null) { arg0 = Delegate.Remove(arg0, ds[i]); state.DelayDispose(ld.func); break; } } func.Dispose(); ToLua.Push(L, arg0); return(1); } else { Delegate arg1 = (Delegate)ToLua.CheckObject(L, 2, typeof(Delegate)); arg0 = DelegateFactory.RemoveDelegate(arg0, arg1); ToLua.Push(L, arg0); return(1); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
private static int op_Subtraction(IntPtr L) { int result; try { ToLua.CheckArgsCount(L, 2); Delegate @delegate = (Delegate)ToLua.CheckObject(L, 1, typeof(Delegate)); LuaTypes luaTypes = LuaDLL.lua_type(L, 2); if (luaTypes == LuaTypes.LUA_TFUNCTION) { LuaFunction luaFunction = ToLua.ToLuaFunction(L, 2); LuaState luaState = LuaState.Get(L); Delegate[] invocationList = @delegate.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { LuaDelegate luaDelegate = invocationList[i].Target as LuaDelegate; if (luaDelegate != null && luaDelegate.func == luaFunction) { @delegate = Delegate.Remove(@delegate, invocationList[i]); luaState.DelayDispose(luaDelegate.func); break; } } luaFunction.Dispose(); ToLua.Push(L, @delegate); result = 1; } else { Delegate dg = (Delegate)ToLua.CheckObject(L, 2, typeof(Delegate)); @delegate = DelegateFactory.RemoveDelegate(@delegate, dg); ToLua.Push(L, @delegate); result = 1; } } catch (Exception e) { result = LuaDLL.toluaL_exception(L, e, null); } return(result); }
void OnGUI() { GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 200, 600, 400), tips); if (GUI.Button(new Rect(10, 10, 120, 40), " = OnClick1")) { CallLuaFunction(SetClick1); } else if (GUI.Button(new Rect(10, 60, 120, 40), " + Click1")) { CallLuaFunction(AddClick1); } else if (GUI.Button(new Rect(10, 110, 120, 40), " + Click2")) { CallLuaFunction(AddClick2); } else if (GUI.Button(new Rect(10, 160, 120, 40), " - Click1")) { CallLuaFunction(RemoveClick1); } else if (GUI.Button(new Rect(10, 210, 120, 40), " - Click2")) { CallLuaFunction(RemoveClick2); } else if (GUI.Button(new Rect(10, 260, 120, 40), "+ Click1 in C#")) { tips = ""; LuaFunction func = state.GetFunction("DoClick1"); TestEventListener.OnClick onClick = (TestEventListener.OnClick) DelegateTraits <TestEventListener.OnClick> .Create(func); listener.onClick += onClick; } else if (GUI.Button(new Rect(10, 310, 120, 40), " - Click1 in C#")) { tips = ""; LuaFunction func = state.GetFunction("DoClick1"); listener.onClick = (TestEventListener.OnClick)DelegateFactory.RemoveDelegate(listener.onClick, func); func.Dispose(); func = null; } else if (GUI.Button(new Rect(10, 360, 120, 40), "OnClick")) { if (listener.onClick != null) { listener.onClick(gameObject); } else { Debug.Log("empty delegate!!"); } } else if (GUI.Button(new Rect(10, 410, 120, 40), "Override")) { CallLuaFunction(TestOverride); } else if (GUI.Button(new Rect(10, 460, 120, 40), "Force GC")) { //自动gc log: collect lua reference name , id xxx in thread state.LuaGC(LuaGCOptions.LUA_GCCOLLECT, 0); GC.Collect(); } else if (GUI.Button(new Rect(10, 510, 120, 40), "event +")) { CallLuaFunction(AddEvent); } else if (GUI.Button(new Rect(10, 560, 120, 40), "event -")) { CallLuaFunction(RemoveEvent); } else if (GUI.Button(new Rect(10, 610, 120, 40), "event call")) { listener.OnClickEvent(gameObject); } else if (GUI.Button(new Rect(200, 10, 120, 40), "+self call")) { CallLuaFunction(AddSelfClick); } else if (GUI.Button(new Rect(200, 60, 120, 40), "-self call")) { CallLuaFunction(RemoveSelfClick); } }
public void removeListener(TableEvent te, Callback callback) { listeners [(int)te] -= callback; DelegateFactory.RemoveDelegate(callback); }
static public bool CheckDelegateType(Type type, IntPtr L, int pos) { LuaTypes luaType = LuaDLL.lua_type(L, pos); switch (luaType) { case LuaTypes.LUA_TNIL: return(true); case LuaTypes.LUA_TUSERDATA: int udata = LuaDLL.tolua_rawnetobj(L, pos); if (udata != -1) { ObjectTranslator translator = ObjectTranslator.Get(L); object obj = translator.GetObject(udata); return(obj == null ? true : type == obj.GetType()); } return(false); case LuaTypes.LUA_TFUNCTION: /*int udata1 = LuaDLL.tolua_rawnetobj(L, pos); * * if (udata1 != -1) * { * ObjectTranslator translator = ObjectTranslator.Get(L); * object obj = translator.GetObject(udata1); * //return obj == null ? true : type == obj.GetType(); * * if (obj != null) * { * if (obj is Delegate dd) * { * int a = dd.Method.GetHashCode(); * int ddc = dd.GetHashCode(); * int tc = type.GetHashCode(); * * * // dd.Target * // type.IsEquivalentTo(dd.GetType()); * * return ddc == tc ? true : false; * //return obj == type; * //return (Delegate)obj; * } * } * else * { * return true; * } * } * return false;*/ //return LuaDLL.luaL_checktype LuaFunction func = ToLua.ToLuaFunction(L, pos); Delegate del = DelegateFactory.CreateDelegate(type, func); DelegateFactory.RemoveDelegate(del, func); return(true); //LuaFunction func = ToLua.ToLuaFunction(L, pos); //object oo = ToLua.ToObject(L, pos); //ToLua.CheckDelegate<OnSceneNameChanged>(L, pos); //Delegate.Equals(); //return oo.GetType() == type; //ToLua.CheckDelegate //DelegateTraits<type>.Create(func); //OnSceneNameChanged arg1 = (OnSceneNameChanged)ToLua.CheckDelegate<OnSceneNameChanged>(L, pos); //Type t = GetNullableType(type); //return t == typeof(LuaFunction); default: return(false); } }
private void OnGUI() { if (GUI.Button(new Rect(10f, 10f, 120f, 40f), " = OnClick1")) { this.CallLuaFunction(this.SetClick1); } else if (GUI.Button(new Rect(10f, 60f, 120f, 40f), " + Click1")) { this.CallLuaFunction(this.AddClick1); } else if (GUI.Button(new Rect(10f, 110f, 120f, 40f), " + Click2")) { this.CallLuaFunction(this.AddClick2); } else if (GUI.Button(new Rect(10f, 160f, 120f, 40f), " - Click1")) { this.CallLuaFunction(this.RemoveClick1); } else if (GUI.Button(new Rect(10f, 210f, 120f, 40f), " - Click2")) { this.CallLuaFunction(this.RemoveClick2); } else if (GUI.Button(new Rect(10f, 260f, 120f, 40f), "+ Click1 in C#")) { LuaFunction function = this.state.GetFunction("DoClick1", true); TestEventListener.OnClick b = (TestEventListener.OnClick)DelegateFactory.CreateDelegate(typeof(TestEventListener.OnClick), function); TestEventListener expr_173 = this.listener; expr_173.onClick = (TestEventListener.OnClick)Delegate.Combine(expr_173.onClick, b); } else if (GUI.Button(new Rect(10f, 310f, 120f, 40f), " - Click1 in C#")) { LuaFunction function2 = this.state.GetFunction("DoClick1", true); this.listener.onClick = (TestEventListener.OnClick)DelegateFactory.RemoveDelegate(this.listener.onClick, function2); function2.Dispose(); } else if (GUI.Button(new Rect(10f, 360f, 120f, 40f), "OnClick")) { if (this.listener.onClick != null) { this.listener.onClick(base.gameObject); } else { Debug.Log("empty delegate!!"); } } else if (GUI.Button(new Rect(10f, 410f, 120f, 40f), "Override")) { this.CallLuaFunction(this.TestOverride); } else if (GUI.Button(new Rect(10f, 460f, 120f, 40f), "Force GC")) { this.state.LuaGC(LuaGCOptions.LUA_GCCOLLECT, 0); GC.Collect(); } else if (GUI.Button(new Rect(10f, 510f, 120f, 40f), "event +")) { this.CallLuaFunction(this.AddEvent); } else if (GUI.Button(new Rect(10f, 560f, 120f, 40f), "event -")) { this.CallLuaFunction(this.RemoveEvent); } else if (GUI.Button(new Rect(10f, 610f, 120f, 40f), "event call")) { this.listener.OnClickEvent(base.gameObject); } else if (GUI.Button(new Rect(200f, 10f, 120f, 40f), "+self call")) { this.CallLuaFunction(this.AddSelfClick); } else if (GUI.Button(new Rect(200f, 60f, 120f, 40f), "-self call")) { this.CallLuaFunction(this.RemoveSelfClick); } }
public void removeListener(int code, Callback callback) { listeners[code] -= callback; DelegateFactory.RemoveDelegate(callback); }
void OnGUI() { if (GUI.Button(new Rect(10, 10, 120, 40), " = OnClick1")) { CallLuaFunction(SetClick1); } else if (GUI.Button(new Rect(10, 60, 120, 40), " + Click1")) { CallLuaFunction(AddClick1); } else if (GUI.Button(new Rect(10, 160, 120, 40), " - Click1")) { CallLuaFunction(RemoveClick1); } else if (GUI.Button(new Rect(10, 260, 120, 40), "+ Click1 in C#")) { LuaFunction func = state.GetFunction("DoClick1"); TestEventListener.OnClick onClick = (TestEventListener.OnClick) DelegateTraits <TestEventListener.OnClick> .Create(func); listener.onClick += onClick; } else if (GUI.Button(new Rect(10, 310, 120, 40), " - Click1 in C#")) { LuaFunction func = state.GetFunction("DoClick1"); listener.onClick = (TestEventListener.OnClick)DelegateFactory.RemoveDelegate(listener.onClick, func); func.Dispose(); func = null; } else if (GUI.Button(new Rect(10, 360, 120, 40), "OnClick")) { if (listener.onClick != null) { listener.onClick(gameObject); } else { Debug.Log("empty delegate!!"); } } else if (GUI.Button(new Rect(10, 410, 120, 40), "Override")) { CallLuaFunction(TestOverride); } else if (GUI.Button(new Rect(10, 510, 120, 40), "event +")) { CallLuaFunction(AddEvent); } else if (GUI.Button(new Rect(10, 560, 120, 40), "event -")) { CallLuaFunction(RemoveEvent); } else if (GUI.Button(new Rect(10, 610, 120, 40), "event call")) { listener.OnClickEvent(gameObject); } else if (GUI.Button(new Rect(200, 10, 120, 40), "+self call")) { CallLuaFunction(AddSelfClick); } else if (GUI.Button(new Rect(200, 60, 120, 40), "-self call")) { CallLuaFunction(RemoveSelfClick); } }
void OnGUI() { if (GUI.Button(new Rect(10, 10, 240, 80), " = OnClick1")) { CallLuaFunction(setClick1); } else if (GUI.Button(new Rect(10, 90, 240, 80), " + Click1")) { CallLuaFunction(addClick1); } else if (GUI.Button(new Rect(10, 170, 240, 80), " + Click2")) { CallLuaFunction(addClick2); } else if (GUI.Button(new Rect(10, 260, 240, 80), " - Click1")) { CallLuaFunction(removeClick1); } else if (GUI.Button(new Rect(10, 350, 240, 80), " - Click2")) { CallLuaFunction(removeClick2); } else if (GUI.Button(new Rect(10, 440, 240, 80), "+ Click1 in C#")) { LuaFunction func = luaState.GetFunction("DoClick1"); TestEventListener.OnClick onClick = (TestEventListener.OnClick) DelegateTraits <TestEventListener.OnClick> .Create(func); listener.onClick += onClick; } else if (GUI.Button(new Rect(10, 530, 240, 80), " - Click1 in C#")) { LuaFunction func = luaState.GetFunction("DoClick1"); listener.onClick = (TestEventListener.OnClick)DelegateFactory.RemoveDelegate(listener.onClick, func); func.Dispose(); func = null; } else if (GUI.Button(new Rect(10, 620, 240, 80), "OnClick")) { if (listener.onClick != null) { listener.onClick(gameObject); } else { Debug.Log("empty delegate!!"); } } else if (GUI.Button(new Rect(10, 710, 240, 80), "Override")) { CallLuaFunction(testOverride); } else if (GUI.Button(new Rect(10, 810, 240, 80), "Force GC")) { //自动gc log: collect lua reference name , id xxx in thread luaState.LuaGC(LuaGCOptions.LUA_GCCOLLECT, 0); GC.Collect(); } else if (GUI.Button(new Rect(10, 900, 240, 80), "event +")) { CallLuaFunction(addEvent); } else if (GUI.Button(new Rect(10, 990, 240, 80), "event -")) { CallLuaFunction(removeEvent); } else if (GUI.Button(new Rect(260, 10, 240, 80), "event call")) { listener.OnClickEvent(gameObject); } else if (GUI.Button(new Rect(260, 90, 240, 80), "+self call")) { CallLuaFunction(addSelfClick); } else if (GUI.Button(new Rect(260, 170, 240, 80), "-self call")) { CallLuaFunction(removeSelfClick); } else if (GUI.Button(new Rect(260, 250, 240, 80), "call onclick")) { CallLuaFunction(callOnClick); //listener.onAct(); } }
public void RemoveListener(GameEventCall call) { this.call -= call; DelegateFactory.RemoveDelegate(call); }