//-1 不存在替换, 1 保留左面, 2 保留右面 static int CompareMethod(_MethodBase l, _MethodBase r) { int s = 0; if (!CompareParmsCount(l, r)) { return(-1); } else { ParameterInfo[] lp = l.GetParameters(); ParameterInfo[] rp = r.GetParameters(); List <Type> ll = new List <Type>(); List <Type> lr = new List <Type>(); if (!l.IsStatic) { ll.Add(type); } if (!r.IsStatic) { lr.Add(type); } for (int i = 0; i < lp.Length; i++) { ll.Add(GetParameterType(lp[i])); } for (int i = 0; i < rp.Length; i++) { lr.Add(GetParameterType(rp[i])); } for (int i = 0; i < ll.Count; i++) { if (ll[i] == lr[i]) { continue; } else { return(-1); } } if (s == 0 && l.IsStatic) { s = 2; } } return(s); }
static void GenOverrideFuncBody(_MethodBase md, bool beIf, int checkTypeOffset) { int offset = md.IsStatic ? 0 : 1; int ret = md.GetReturnType() == typeof(void) ? 0 : 1; string strIf = beIf ? "if " : "else if "; if (HasOptionalParam(md.GetParameters())) { ParameterInfo[] paramInfos = md.GetParameters(); ParameterInfo param = paramInfos[paramInfos.Length - 1]; string str = GetTypeStr(param.ParameterType.GetElementType()); if (paramInfos.Length + offset > 1) { string strParams = md.GenParamTypes(0); sb.AppendFormat("\t\t\t{0}(TypeChecker.CheckTypes<{1}>(L, 1) && TypeChecker.CheckParamsType<{2}>(L, {3}, {4}))\r\n", strIf, strParams, str, paramInfos.Length + offset, GetCountStr(paramInfos.Length + offset - 1)); } else { sb.AppendFormat("\t\t\t{0}(TypeChecker.CheckParamsType<{1}>(L, {2}, {3}))\r\n", strIf, str, paramInfos.Length + offset, GetCountStr(paramInfos.Length + offset - 1)); } } else { ParameterInfo[] paramInfos = md.GetParameters(); if (paramInfos.Length + offset > checkTypeOffset) { string strParams = md.GenParamTypes(checkTypeOffset); sb.AppendFormat("\t\t\t{0}(count == {1} && TypeChecker.CheckTypes<{2}>(L, {3}))\r\n", strIf, paramInfos.Length + offset, strParams, checkTypeOffset + 1); } else { sb.AppendFormat("\t\t\t{0}(count == {1})\r\n", strIf, paramInfos.Length + offset); } } sb.AppendLineEx("\t\t\t{"); int count = md.ProcessParams(4, false, checkTypeOffset); sb.AppendFormat("\t\t\t\treturn {0};\r\n", ret + count); sb.AppendLineEx("\t\t\t}"); }
static bool CompareParmsCount(_MethodBase l, _MethodBase r) { if (l == r) { return(false); } int c1 = l.IsStatic ? 0 : 1; int c2 = r.IsStatic ? 0 : 1; c1 += l.GetParameters().Length; c2 += r.GetParameters().Length; return(c1 == c2); }
static void GetDelegateTypeFromMethodParams(_MethodBase m) { if (m.IsGenericMethod) { return; } ParameterInfo[] pifs = m.GetParameters(); for (int k = 0; k < pifs.Length; k++) { Type t = pifs[k].ParameterType; if (IsDelegateType(t)) { eventSet.Add(t); } } }
static void GenFunction(_MethodBase m) { string name = GetMethodName(m.Method); sb.AppendLineEx("\r\n\t[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]"); sb.AppendFormat("\tstatic int {0}(IntPtr L)\r\n", name == "Register" ? "_Register" : name); sb.AppendLineEx("\t{"); if (HasAttribute(m.Method, typeof(UseDefinedAttribute))) { FieldInfo field = extendType.GetField(name + "Defined"); string strfun = field.GetValue(null) as string; sb.AppendLineEx(strfun); sb.AppendLineEx("\t}"); return; } ParameterInfo[] paramInfos = m.GetParameters(); int offset = m.IsStatic ? 0 : 1; bool haveParams = HasOptionalParam(paramInfos); int rc = m.GetReturnType() == typeof(void) ? 0 : 1; BeginTry(); if (!haveParams) { int count = paramInfos.Length + offset; if (m.Name == "op_UnaryNegation") { count = 2; } sb.AppendFormat("\t\t\tToLua.CheckArgsCount(L, {0});\r\n", count); } else { sb.AppendLineEx("\t\t\tint count = LuaDLL.lua_gettop(L);"); } rc += m.ProcessParams(3, false, int.MaxValue); sb.AppendFormat("\t\t\treturn {0};\r\n", rc); EndTry(); sb.AppendLineEx("\t}"); }
static void GenConstructFunction() { if (ctorExtList.Count > 0) { if (HasAttribute(ctorExtList[0], typeof(UseDefinedAttribute))) { sb.AppendLineEx("\r\n\t[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]"); sb.AppendFormat("\tstatic int _Create{0}(IntPtr L)\r\n", wrapClassName); sb.AppendLineEx("\t{"); FieldInfo field = extendType.GetField(extendName + "Defined"); string strfun = field.GetValue(null) as string; sb.AppendLineEx(strfun); sb.AppendLineEx("\t}"); return; } } if (ctorList.Count == 0) { if (type.IsValueType) { DefaultConstruct(); } return; } ctorList.Sort(Compare); int[] checkTypeMap = CheckCheckTypePos(ctorList); sb.AppendLineEx("\r\n\t[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]"); sb.AppendFormat("\tstatic int _Create{0}(IntPtr L)\r\n", wrapClassName); sb.AppendLineEx("\t{"); BeginTry(); sb.AppendLineEx("\t\t\tint count = LuaDLL.lua_gettop(L);"); sb.AppendLineEx(); _MethodBase md = ctorList[0]; bool hasEmptyCon = ctorList[0].GetParameters().Length == 0 ? true : false; //处理重载构造函数 if (HasOptionalParam(md.GetParameters())) { ParameterInfo[] paramInfos = md.GetParameters(); ParameterInfo param = paramInfos[paramInfos.Length - 1]; string str = GetTypeStr(param.ParameterType.GetElementType()); if (paramInfos.Length > 1) { string strParams = md.GenParamTypes(1); sb.AppendFormat("\t\t\tif (TypeChecker.CheckTypes<{0}>(L, 1) && TypeChecker.CheckParamsType<{1}>(L, {2}, {3}))\r\n", strParams, str, paramInfos.Length, GetCountStr(paramInfos.Length - 1)); } else { sb.AppendFormat("\t\t\tif (TypeChecker.CheckParamsType<{0}>(L, {1}, {2}))\r\n", str, paramInfos.Length, GetCountStr(paramInfos.Length - 1)); } } else { ParameterInfo[] paramInfos = md.GetParameters(); if (ctorList.Count == 1 || paramInfos.Length == 0 || paramInfos.Length + 1 <= checkTypeMap[0]) { sb.AppendFormat("\t\t\tif (count == {0})\r\n", paramInfos.Length); } else { string strParams = md.GenParamTypes(checkTypeMap[0]); sb.AppendFormat("\t\t\tif (count == {0} && TypeChecker.CheckTypes<{1}>(L, {2}))\r\n", paramInfos.Length, strParams, checkTypeMap[0]); } } sb.AppendLineEx("\t\t\t{"); int rc = md.ProcessParams(4, true, checkTypeMap[0] - 1); sb.AppendFormat("\t\t\t\treturn {0};\r\n", rc); sb.AppendLineEx("\t\t\t}"); for (int i = 1; i < ctorList.Count; i++) { hasEmptyCon = ctorList[i].GetParameters().Length == 0 ? true : hasEmptyCon; md = ctorList[i]; ParameterInfo[] paramInfos = md.GetParameters(); if (!HasOptionalParam(md.GetParameters())) { string strParams = md.GenParamTypes(checkTypeMap[i]); if (paramInfos.Length + 1 > checkTypeMap[i]) { sb.AppendFormat("\t\t\telse if (count == {0} && TypeChecker.CheckTypes<{1}>(L, {2}))\r\n", paramInfos.Length, strParams, checkTypeMap[i]); } else { sb.AppendFormat("\t\t\telse if (count == {0})\r\n", paramInfos.Length); } } else { ParameterInfo param = paramInfos[paramInfos.Length - 1]; string str = GetTypeStr(param.ParameterType.GetElementType()); if (paramInfos.Length > 1) { string strParams = md.GenParamTypes(1); sb.AppendFormat("\t\t\telse if (TypeChecker.CheckTypes<{0}>(L, 1) && TypeChecker.CheckParamsType<{1}>(L, {2}, {3}))\r\n", strParams, str, paramInfos.Length, GetCountStr(paramInfos.Length - 1)); } else { sb.AppendFormat("\t\t\telse if (TypeChecker.CheckParamsType<{0}>(L, {1}, {2}))\r\n", str, paramInfos.Length, GetCountStr(paramInfos.Length - 1)); } } sb.AppendLineEx("\t\t\t{"); rc = md.ProcessParams(4, true, checkTypeMap[i] - 1); sb.AppendFormat("\t\t\t\treturn {0};\r\n", rc); sb.AppendLineEx("\t\t\t}"); } if (type.IsValueType && !hasEmptyCon) { sb.AppendLineEx("\t\t\telse if (count == 0)"); sb.AppendLineEx("\t\t\t{"); sb.AppendFormat("\t\t\t\t{0} obj = new {0}();\r\n", className); GenPushStr(type, "obj", "\t\t\t\t"); sb.AppendLineEx("\t\t\t\treturn 1;"); sb.AppendLineEx("\t\t\t}"); } sb.AppendLineEx("\t\t\telse"); sb.AppendLineEx("\t\t\t{"); sb.AppendFormat("\t\t\t\treturn LuaDLL.luaL_throw(L, \"invalid arguments to ctor method: {0}.New\");\r\n", className); sb.AppendLineEx("\t\t\t}"); EndTry(); sb.AppendLineEx("\t}"); }
static int Compare(_MethodBase lhs, _MethodBase rhs) { int off1 = lhs.IsStatic ? 0 : 1; int off2 = rhs.IsStatic ? 0 : 1; ParameterInfo[] lp = lhs.GetParameters(); ParameterInfo[] rp = rhs.GetParameters(); int pos1 = GetOptionalParamPos(lp); int pos2 = GetOptionalParamPos(rp); if (pos1 >= 0 && pos2 < 0) { return(1); } else if (pos1 < 0 && pos2 >= 0) { return(-1); } else if (pos1 >= 0 && pos2 >= 0) { pos1 += off1; pos2 += off2; if (pos1 != pos2) { return(pos1 > pos2 ? -1 : 1); } else { pos1 -= off1; pos2 -= off2; if (lp[pos1].ParameterType.GetElementType() == typeof(object) && rp[pos2].ParameterType.GetElementType() != typeof(object)) { return(1); } else if (lp[pos1].ParameterType.GetElementType() != typeof(object) && rp[pos2].ParameterType.GetElementType() == typeof(object)) { return(-1); } } } int c1 = off1 + lp.Length; int c2 = off2 + rp.Length; if (c1 > c2) { return(1); } else if (c1 == c2) { List <ParameterInfo> list1 = new List <ParameterInfo>(lp); List <ParameterInfo> list2 = new List <ParameterInfo>(rp); if (list1.Count > list2.Count) { if (list1[0].ParameterType == typeof(object)) { return(1); } list1.RemoveAt(0); } else if (list2.Count > list1.Count) { if (list2[0].ParameterType == typeof(object)) { return(-1); } list2.RemoveAt(0); } for (int i = 0; i < list1.Count; i++) { if (list1[i].ParameterType == typeof(object) && list2[i].ParameterType != typeof(object)) { return(1); } else if (list1[i].ParameterType != typeof(object) && list2[i].ParameterType == typeof(object)) { return(-1); } else if (list1[i].ParameterType.IsPrimitive && list2[i].ParameterType.IsPrimitive) { if (Is64bit(list1[i].ParameterType) && !Is64bit(list2[i].ParameterType)) { return(1); } else if (!Is64bit(list1[i].ParameterType) && Is64bit(list2[i].ParameterType)) { return(-1); } else if (Is64bit(list1[i].ParameterType) && Is64bit(list2[i].ParameterType) && list1[i].ParameterType != list2[i].ParameterType) { if (list1[i].ParameterType == typeof(ulong)) { return(1); } return(-1); } } } return(0); } else { return(-1); } }
//this[] 非静态函数 static void GenItemPropertyFunction() { int flag = 0; if (getItems.Count > 0) { sb.AppendLineEx("\r\n\t[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]"); sb.AppendLineEx("\tstatic int _get_this(IntPtr L)"); sb.AppendLineEx("\t{"); BeginTry(); if (getItems.Count == 1) { _MethodBase m = getItems[0]; int count = m.GetParameters().Length + 1; sb.AppendFormat("\t\t\tToLua.CheckArgsCount(L, {0});\r\n", count); m.ProcessParams(3, false, int.MaxValue); sb.AppendLineEx("\t\t\treturn 1;\r\n"); } else { getItems.Sort(Compare); int[] checkTypeMap = CheckCheckTypePos(getItems); sb.AppendLineEx("\t\t\tint count = LuaDLL.lua_gettop(L);"); sb.AppendLineEx(); for (int i = 0; i < getItems.Count; i++) { GenOverrideFuncBody(getItems[i], i == 0, checkTypeMap[i]); } sb.AppendLineEx("\t\t\telse"); sb.AppendLineEx("\t\t\t{"); sb.AppendFormat("\t\t\t\treturn LuaDLL.luaL_throw(L, \"invalid arguments to operator method: {0}.this\");\r\n", className); sb.AppendLineEx("\t\t\t}"); } EndTry(); sb.AppendLineEx("\t}"); flag |= 1; } if (setItems.Count > 0) { sb.AppendLineEx("\r\n\t[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]"); sb.AppendLineEx("\tstatic int _set_this(IntPtr L)"); sb.AppendLineEx("\t{"); BeginTry(); if (setItems.Count == 1) { _MethodBase m = setItems[0]; int count = m.GetParameters().Length + 1; sb.AppendFormat("\t\t\tToLua.CheckArgsCount(L, {0});\r\n", count); m.ProcessParams(3, false, int.MaxValue); sb.AppendLineEx("\t\t\treturn 0;\r\n"); } else { setItems.Sort(Compare); int[] checkTypeMap = CheckCheckTypePos(setItems); sb.AppendLineEx("\t\t\tint count = LuaDLL.lua_gettop(L);"); sb.AppendLineEx(); for (int i = 0; i < setItems.Count; i++) { GenOverrideFuncBody(setItems[i], i == 0, checkTypeMap[i]); } sb.AppendLineEx("\t\t\telse"); sb.AppendLineEx("\t\t\t{"); sb.AppendFormat("\t\t\t\treturn LuaDLL.luaL_throw(L, \"invalid arguments to operator method: {0}.this\");\r\n", className); sb.AppendLineEx("\t\t\t}"); } EndTry(); sb.AppendLineEx("\t}"); flag |= 2; } if (flag != 0) { sb.AppendLineEx("\r\n\t[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]"); sb.AppendLineEx("\tstatic int _this(IntPtr L)"); sb.AppendLineEx("\t{"); BeginTry(); sb.AppendLineEx("\t\t\tLuaDLL.lua_pushvalue(L, 1);"); sb.AppendFormat("\t\t\tLuaDLL.tolua_bindthis(L, {0}, {1});\r\n", (flag & 1) == 1 ? "_get_this" : "null", (flag & 2) == 2 ? "_set_this" : "null"); sb.AppendLineEx("\t\t\treturn 1;"); EndTry(); sb.AppendLineEx("\t}"); } }