Пример #1
0
    private static void GenOverrideFunctionBody(string name, _MethodBase methodBase)
    {
        sb.AppendLineEx("\r\n\t[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]");
        sb.AppendFormat("\tstatic int {0}(IntPtr L)\r\n", name);
        sb.AppendLineEx("\t{");

        BeginTry();
        sb.AppendLineEx("\t\t\tint count = LuaDLL.lua_gettop(L);");
        sb.AppendLineEx();

        if (HasAttribute(methodBase.Method, typeof(OverrideDefinedAttribute)))
        {
            GenOverrideDefinedFunc(methodBase.Method);
            sb.AppendFormat("\t\t\treturn 0;\r\n");
        }
        else
        {
            int count = methodBase.ProcessParams(3, false, 0);
            int ret   = methodBase.GetReturnType() == typeof(void) ? 0 : 1;
            sb.AppendFormat("\t\t\treturn {0};\r\n", ret + count);
        }

        EndTry();

        sb.AppendLineEx("\t}");
    }
Пример #2
0
    static void ProcessExtendType(Type extendType, List <_MethodBase> list)
    {
        if (extendType != null)
        {
            List <MethodInfo> list2 = new List <MethodInfo>();
            list2.AddRange(extendType.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly));

            for (int i = list2.Count - 1; i >= 0; i--)
            {
                MethodInfo md = list2[i];

                if (!md.IsDefined(typeof(ExtensionAttribute), false))
                {
                    continue;
                }

                ParameterInfo[] plist = md.GetParameters();
                Type            t     = plist[0].ParameterType;

                if (t == type || t.IsAssignableFrom(type) || (IsGenericType(md, t) && (type == t.BaseType || type.IsSubclassOf(t.BaseType))))
                {
                    if (!IsObsolete(list2[i]))
                    {
                        _MethodBase mb = new _MethodBase(md);
                        mb.BeExtend = true;
                        list.Add(mb);
                    }
                }
            }
        }
    }
Пример #3
0
        /// <summary>
        /// ctor: logs the 'enter' - message
        /// </summary>
        public FuncTrace() : base()
        {
            if (!Tracer.InstrumentationSwitch)
            {
                return;
            }

            Type callingType = null;

            {
                StackFrame fr = new StackFrame(1, true);

                _MethodBase m = fr.GetMethod();
                _name       = m.DeclaringType.FullName + "." + m.Name;
                callingType = m.DeclaringType;
            }
            Tracer.Push_MethodCallRecord(_name);

            for (int i = Tracer.m_NamespacesToLog.Length - 1; i >= 0; i--)
            {
                if (_name.StartsWith(Tracer.m_NamespacesToLog[i]))
                {
                    m_DoLogging = true;
                    break;
                }
            }

            m_Logger = LogManager.GetLogger(callingType);
            if (m_DoLogging)
            {
                m_Logger.Info("ENTERING '" + _name);
            }
        }
Пример #4
0
    static void GenRegisterFuncItems()
    {
        //bool isList = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>);
        //注册库函数
        for (int i = 0; i < methods.Count; i++)
        {
            _MethodBase m     = methods[i];
            int         count = 1;

            if (IsGenericMethod(m.Method))
            {
                continue;
            }

            string name = GetMethodName(m.Method);

            if (!nameCounter.TryGetValue(name, out count))
            {
                if (name == "get_Item" && IsThisArray(m.Method, 1))
                {
                    sb.AppendFormat("\t\tL.RegFunction(\"{0}\", get_Item);\r\n", ".geti");
                }
                else if (name == "set_Item" && IsThisArray(m.Method, 2))
                {
                    sb.AppendFormat("\t\tL.RegFunction(\"{0}\", set_Item);\r\n", ".seti");
                }

                if (!name.StartsWith("op_"))
                {
                    var list = GetOverrideMethods(name);
                    sb.AppendFormat("\t\tL.RegFunction(\"{0}\", {1});\r\n", name, name == "Register" ? "_Register" : name);
                    if (list.Count > 1)
                    {
                        for (int overrideIndex = 0; overrideIndex < list.Count; overrideIndex++)
                        {
                            var genMethodName = GenMethodName(list[overrideIndex], overrideIndex);
                            sb.AppendFormat("\t\tL.RegFunction(\"{0}\", {1});\r\n", genMethodName, genMethodName);
                        }
                    }
                }

                nameCounter[name] = 1;
            }
            else
            {
                nameCounter[name] = count + 1;
            }
        }

        if (ctorList.Count > 0 || type.IsValueType || ctorExtList.Count > 0)
        {
            sb.AppendFormat("\t\tL.RegFunction(\"New\", _Create{0});\r\n", wrapClassName);
        }

        if (getItems.Count > 0 || setItems.Count > 0)
        {
            sb.AppendLineEx("\t\tL.RegVar(\"this\", _this, null);");
        }
    }
Пример #5
0
    //-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);
    }
Пример #6
0
    static void InitCtorList()
    {
        if (isStaticClass || type.IsAbstract || typeof(MonoBehaviour).IsAssignableFrom(type))
        {
            return;
        }

        ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Instance | binding);

        if (extendType != null)
        {
            ConstructorInfo[] ctorExtends = extendType.GetConstructors(BindingFlags.Instance | binding);

            if (HasAttribute(ctorExtends[0], typeof(UseDefinedAttribute)))
            {
                ctorExtList.AddRange(ctorExtends);
            }
        }

        if (constructors.Length == 0)
        {
            return;
        }

        for (int i = 0; i < constructors.Length; i++)
        {
            if (IsObsolete(constructors[i]))
            {
                continue;
            }

            int count  = GetDefalutParamCount(constructors[i]);
            int length = constructors[i].GetParameters().Length;

            for (int j = 0; j < count + 1; j++)
            {
                _MethodBase r     = new _MethodBase(constructors[i], length - j);
                int         index = ctorList.FindIndex((p) => { return(CompareMethod(p, r) >= 0); });

                if (index >= 0)
                {
                    if (CompareMethod(ctorList[index], r) == 2)
                    {
                        ctorList.RemoveAt(index);
                        ctorList.Add(r);
                    }
                }
                else
                {
                    ctorList.Add(r);
                }
            }
        }
    }
Пример #7
0
    // 生成重载方法名
    static string GenMethodName(_MethodBase methodBase, int overrideIndex)
    {
        string name = GetMethodName(methodBase.Method);

        if (IsGenericMethod(methodBase.Method))
        {
            return(string.Format("{0}{1}T", name, overrideIndex));
        }
        else
        {
            return(string.Format("{0}{1}", name, overrideIndex));
        }
    }
Пример #8
0
    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);
    }
        public static void Print(_MethodBase Methode, String Message = "")
        {
            try
            {
                if (!(_ACTIF && _INIT))
                    return;

                StackTrace pPile = new StackTrace();

                int Pos = pPile.FrameCount - 1;
                Boolean AjouterAuFichier = true;

                if (_NB_LIGNE > _NB_LIGNE_MAX)
                {
                    AjouterAuFichier = false;
                    _NB_LIGNE = 0;
                    _NO_FICHIER++;
                    _CHEMIN_FICHIER = Path.Combine(_DOSSIER, _FICHIER_BASE + "-" + _NO_FICHIER.ToString() + ".txt");
                }

                if ((Methode != null) && (Pos < 500))
                {
                    LISTE_METHODE[Pos] = Methode.DeclaringType.ToString().Split('.')[1] + "." + Methode.Name;
                }

                if (!String.IsNullOrEmpty(Message))
                    Message = "-> " + Message;

                if (Methode != null)
                    Message = "  " + LISTE_METHODE[Pos] + " " + Message;

                Message = "  |" + Message;

                StreamWriter pFichierDebug = new StreamWriter(_CHEMIN_FICHIER, AjouterAuFichier, System.Text.Encoding.Unicode);
                pFichierDebug.WriteLine("    ".Repeter(Pos) + Message);
                pFichierDebug.Close();
                _NB_LIGNE++;
            }
            catch
            {
                StreamWriter pFichierDebug = new StreamWriter(_CHEMIN_FICHIER, true, System.Text.Encoding.Unicode);
                pFichierDebug.WriteLine("Erreur");
                pFichierDebug.Close();
            }
        }
Пример #10
0
    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);
            }
        }
    }
Пример #11
0
    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}");
    }
Пример #12
0
    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}");
    }
Пример #13
0
    static void GenNewIndexFunc()
    {
        for (int i = 0; i < fields.Length; i++)
        {
            if (fields[i].IsLiteral || fields[i].IsInitOnly || fields[i].IsPrivate)
            {
                continue;
            }

            GenSetFieldStr(fields[i].Name, fields[i].FieldType, fields[i].IsStatic);
        }

        for (int i = 0; i < props.Length; i++)
        {
            if (!props[i].CanWrite || !props[i].GetSetMethod(true).IsPublic)
            {
                continue;
            }

            bool isStatic = true;
            int  index    = propList.IndexOf(props[i]);

            if (index >= 0)
            {
                isStatic = false;
            }

            _MethodBase md = methods.Find((p) => { return(p.Name == "set_" + props[i].Name); });
            GenSetFieldStr(props[i].Name, props[i].PropertyType, isStatic, md != null);
            // 生成CSharp.lua兼容的版本
            if (isStatic)
            {
                GenSetFieldStr(props[i].Name, props[i].PropertyType, isStatic, md != null, true);
            }
        }

        for (int i = 0; i < events.Length; i++)
        {
            bool isStatic = eventList.IndexOf(events[i]) < 0;
            GenSetEventStr(events[i].Name, events[i].EventHandlerType, isStatic);
        }
    }
Пример #14
0
    static void GenFunctions()
    {
        HashSet <string> set = new HashSet <string>();

        for (int i = 0; i < methods.Count; i++)
        {
            _MethodBase m = methods[i];

            if (IsGenericMethod(m.Method))
            {
                Debugger.Log("Generic Method {0}.{1} cannot be export to lua", LuaMisc.GetTypeName(type), m.GetTotalName());
                continue;
            }

            string name = GetMethodName(m.Method);

            if (nameCounter[name] > 1)
            {
                if (!set.Contains(name))
                {
                    _MethodBase mi = GenOverrideFunc(name);

                    if (mi == null)
                    {
                        set.Add(name);
                        continue;
                    }
                    else
                    {
                        m = mi;     //非重载函数,或者折叠之后只有一个函数
                    }
                }
                else
                {
                    continue;
                }
            }

            set.Add(name);
            GenFunction(m);
        }
    }
Пример #15
0
        public int GetEqualParamsCount(_MethodBase b)
        {
            int         count = 0;
            List <Type> list1 = new List <Type>();
            List <Type> list2 = new List <Type>();

            if (!IsStatic)
            {
                list1.Add(type);
            }

            if (!b.IsStatic)
            {
                list2.Add(type);
            }

            for (int i = 0; i < args.Length; i++)
            {
                list1.Add(GetParameterType(args[i]));
            }

            ParameterInfo[] p = b.args;

            for (int i = 0; i < p.Length; i++)
            {
                list2.Add(GetParameterType(p[i]));
            }

            for (int i = 0; i < list1.Count; i++)
            {
                if (list1[i] != list2[i])
                {
                    break;
                }

                ++count;
            }

            return(count);
        }
Пример #16
0
    static void GenIndexFunc()
    {
        for (int i = 0; i < fields.Length; i++)
        {
            if (fields[i].IsLiteral && fields[i].FieldType.IsPrimitive && !fields[i].FieldType.IsEnum)
            {
                continue;
            }

            bool beBuffer = IsByteBuffer(fields[i]);
            GenGetFieldStr(fields[i].Name, fields[i].FieldType, fields[i].IsStatic, beBuffer);
        }

        for (int i = 0; i < props.Length; i++)
        {
            if (!props[i].CanRead)
            {
                continue;
            }

            bool isStatic = true;
            int  index    = propList.IndexOf(props[i]);

            if (index >= 0)
            {
                isStatic = false;
            }

            _MethodBase md       = methods.Find((p) => { return(p.Name == "get_" + props[i].Name); });
            bool        beBuffer = IsByteBuffer(props[i]);

            GenGetFieldStr(props[i].Name, props[i].PropertyType, isStatic, beBuffer, md != null);
        }

        for (int i = 0; i < events.Length; i++)
        {
            GenGetEventStr(events[i].Name, events[i].EventHandlerType);
        }
    }
Пример #17
0
    static void Push(List <_MethodBase> list, _MethodBase r)
    {
        string name  = GetMethodName(r.Method);
        int    index = list.FindIndex((p) => { return(GetMethodName(p.Method) == name && CompareMethod(p, r) >= 0); });

        if (index >= 0)
        {
            if (CompareMethod(list[index], r) == 2)
            {
                Debugger.LogWarning("{0}.{1} has been dropped as function {2} more match lua", className, list[index].GetTotalName(), r.GetTotalName());
                list.RemoveAt(index);
                list.Add(r);
                return;
            }
            else
            {
                Debugger.LogWarning("{0}.{1} has been dropped as function {2} more match lua", className, r.GetTotalName(), list[index].GetTotalName());
                return;
            }
        }

        list.Add(r);
    }
Пример #18
0
    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}");
    }
Пример #19
0
    static void InitMethods()
    {
        bool flag = false;

        if (baseType != null || isStaticClass)
        {
            binding |= BindingFlags.DeclaredOnly;
            flag     = true;
        }

        List <_MethodBase> list = new List <_MethodBase>();

        MethodInfo[] infos = type.GetMethods(BindingFlags.Instance | binding);

        for (int i = 0; i < infos.Length; i++)
        {
            list.Add(new _MethodBase(infos[i]));
        }

        for (int i = list.Count - 1; i >= 0; --i)
        {
            //去掉操作符函数
            if (list[i].Name.StartsWith("op_") || list[i].Name.StartsWith("add_") || list[i].Name.StartsWith("remove_"))
            {
                if (!IsNeedOp(list[i].Name))
                {
                    list.RemoveAt(i);
                }

                continue;
            }

            //扔掉 unity3d 废弃的函数
            if (IsObsolete(list[i].Method))
            {
                list.RemoveAt(i);
            }
        }

        PropertyInfo[] ps = type.GetProperties();

        for (int i = 0; i < ps.Length; i++)
        {
            if (IsObsolete(ps[i]))
            {
                list.RemoveAll((p) => { return(p.Method == ps[i].GetGetMethod() || p.Method == ps[i].GetSetMethod()); });
            }
            else
            {
                MethodInfo md = ps[i].GetGetMethod();

                if (md != null)
                {
                    int index = list.FindIndex((m) => { return(m.Method == md); });

                    if (index >= 0)
                    {
                        if (md.GetParameters().Length == 0)
                        {
                            list.RemoveAt(index);
                        }
                        else if (list[index].HasGetIndex())
                        {
                            getItems.Add(list[index]);
                        }
                    }
                }

                md = ps[i].GetSetMethod();

                if (md != null)
                {
                    int index = list.FindIndex((m) => { return(m.Method == md); });

                    if (index >= 0)
                    {
                        if (md.GetParameters().Length == 1)
                        {
                            list.RemoveAt(index);
                        }
                        else if (list[index].HasSetIndex())
                        {
                            setItems.Add(list[index]);
                        }
                    }
                }
            }
        }

        if (flag && !isStaticClass)
        {
            List <MethodInfo> baseList = new List <MethodInfo>(type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.IgnoreCase));

            for (int i = baseList.Count - 1; i >= 0; i--)
            {
                if (BeDropMethodType(baseList[i]))
                {
                    baseList.RemoveAt(i);
                }
            }

            HashSet <MethodInfo> addList = new HashSet <MethodInfo>();

            for (int i = 0; i < list.Count; i++)
            {
                List <MethodInfo> mds = baseList.FindAll((p) => { return(p.Name == list[i].Name); });

                for (int j = 0; j < mds.Count; j++)
                {
                    addList.Add(mds[j]);
                    baseList.Remove(mds[j]);
                }
            }

            foreach (var iter in addList)
            {
                list.Add(new _MethodBase(iter));
            }
        }

        for (int i = 0; i < list.Count; i++)
        {
            GetDelegateTypeFromMethodParams(list[i]);
        }

        ProcessExtends(list);
        GenBaseOpFunction(list);

        metaXml.AddIncludeClass(type);
        HashSet <string> names = new HashSet <string>();

        for (int i = 0; i < list.Count; i++)
        {
            var method = list[i];
            //if (IsGenericMethod(method.Method))
            //{
            //    continue;
            //}
            if (!names.Add(method.Name))
            {
                continue;
            }
            var notGenericOverrideList = GetOverrideMethods(method.Name, list, false);
            if (notGenericOverrideList.Count > 1)
            {
                for (int overrideIndex = 0; overrideIndex < notGenericOverrideList.Count; overrideIndex++)
                {
                    var overrideMethod = notGenericOverrideList[overrideIndex];
                    var name           = GenMethodName(overrideMethod, overrideIndex);
                    metaXml.AddMethod(overrideMethod.Method, name);
                }
            }
            else if (notGenericOverrideList.Count == 1)
            {
                var name = method.Name;
                metaXml.AddMethod(notGenericOverrideList[0].Method, name == "Register" ? "_Register" : name);
            }
            else
            {
                var name = method.Name;
                metaXml.AddMethod(method.Method, name == "Register" ? "_Register" : name);
            }

            var genericOverrideList = GetOverrideMethods(method.Name, list, true);
            for (int overrideIndex = 0; overrideIndex < genericOverrideList.Count; overrideIndex++)
            {
                var overrideMethod = genericOverrideList[overrideIndex];
                var name           = GenMethodName(overrideMethod, overrideIndex);
                metaXml.AddMethod(overrideMethod.Method, name);
            }
        }

        for (int i = 0; i < list.Count; i++)
        {
            int count  = GetDefalutParamCount(list[i].Method);
            int length = list[i].GetParameters().Length;

            for (int j = 0; j < count + 1; j++)
            {
                _MethodBase r = new _MethodBase(list[i].Method, length - j);
                r.BeExtend = list[i].BeExtend;
                methods.Add(r);
            }
        }
    }
Пример #20
0
    static void GenRegisterVariables()
    {
        if (fields.Length == 0 && props.Length == 0 && events.Length == 0 && isStaticClass && baseType == null)
        {
            return;
        }

        for (int i = 0; i < fields.Length; i++)
        {
            if (fields[i].IsLiteral || fields[i].IsPrivate || fields[i].IsInitOnly)
            {
                if (fields[i].IsLiteral && fields[i].FieldType.IsPrimitive && !fields[i].FieldType.IsEnum)
                {
                    double d = Convert.ToDouble(fields[i].GetValue(null));
                    sb.AppendFormat("\t\tL.RegConstant(\"{0}\", {1});\r\n", fields[i].Name, d);
                }
                else
                {
                    sb.AppendFormat("\t\tL.RegVar(\"{0}\", get_{0}, null);\r\n", fields[i].Name);
                }
            }
            else
            {
                sb.AppendFormat("\t\tL.RegVar(\"{0}\", get_{0}, set_{0});\r\n", fields[i].Name);
            }
        }

        for (int i = 0; i < props.Length; i++)
        {
            if (props[i].CanRead && props[i].CanWrite && props[i].GetSetMethod(true).IsPublic)
            {
                _MethodBase md  = methods.Find((p) => { return(p.Name == "get_" + props[i].Name); });
                string      get = md == null ? "get" : "_get";
                md = methods.Find((p) => { return(p.Name == "set_" + props[i].Name); });
                string set = md == null ? "set" : "_set";
                sb.AppendFormat("\t\tL.RegVar(\"{0}\", {1}_{0}, {2}_{0});\r\n", props[i].Name, get, set);
                sb.AppendFormat("\t\tL.RegFunction(\"get{0}\", {1}_{0});\r\n", props[i].Name, get);
                if (props[i].GetSetMethod().IsStatic)
                {
                    sb.AppendFormat("\t\tL.RegFunction(\"set{0}\", {1}_{0}ter);\r\n", props[i].Name, set);
                }
                else
                {
                    sb.AppendFormat("\t\tL.RegFunction(\"set{0}\", {1}_{0});\r\n", props[i].Name, set);
                }
            }
            else if (props[i].CanRead)
            {
                _MethodBase md = methods.Find((p) => { return(p.Name == "get_" + props[i].Name); });
                sb.AppendFormat("\t\tL.RegVar(\"{0}\", {1}_{0}, null);\r\n", props[i].Name, md == null ? "get" : "_get");
                sb.AppendFormat("\t\tL.RegFunction(\"get{0}\", {1}_{0});\r\n", props[i].Name, md == null ? "get" : "_get");
            }
            else if (props[i].CanWrite)
            {
                _MethodBase md = methods.Find((p) => { return(p.Name == "set_" + props[i].Name); });
                sb.AppendFormat("\t\tL.RegVar(\"{0}\", null, {1}_{0});\r\n", props[i].Name, md == null ? "set" : "_set");
                if (props[i].GetSetMethod().IsStatic)
                {
                    sb.AppendFormat("\t\tL.RegFunction(\"set{0}\", {1}_{0}ter);\r\n", props[i].Name, md == null ? "set" : "_set");
                }
                else
                {
                    sb.AppendFormat("\t\tL.RegFunction(\"set{0}\", {1}_{0});\r\n", props[i].Name, md == null ? "set" : "_set");
                }
            }
        }

        for (int i = 0; i < events.Length; i++)
        {
            sb.AppendFormat("\t\tL.RegVar(\"{0}\", get_{0}, set_{0});\r\n", events[i].Name);
        }
    }
Пример #21
0
        public static void Watch(MPI_Comm comm)
        {
            if (!Tracing.Tracer.InstrumentationSwitch)
            {
                // if tracing is off for performance reasons,
                // also this method should be turned of.
                return;
            }

            // init
            // ====
            string   m_functionName;
            DateTime entryTime = DateTime.Now;
            {
                StackFrame  fr = new StackFrame(1, true);
                _MethodBase m  = fr.GetMethod();
                m_functionName = m.DeclaringType.FullName + "." + m.Name;
            }

            var st = new StackTrace();
            int rank;

            csMPI.Raw.Comm_Rank(comm, out rank);

            // start watchdog ...
            // ==================
            Fire  = 100;
            Fired = 200;
            Thread wDog = (new Thread(delegate() {
                Stopwatch stw = new Stopwatch();
                stw.Start();
                double WaitTime = 10.0;

                while (true)
                {
                    if (Fire == 0)
                    {
                        Fired = 0;
                        if (stw.Elapsed.TotalSeconds > WaitTime)
                        {
                            Console.WriteLine("Returning from out-of-sync after " + stw.Elapsed.TotalSeconds + " seconds.");
                        }
                        return;
                    }
                    else if (stw.Elapsed.TotalSeconds > WaitTime)
                    {
                        Console.Error.WriteLine("WARNING: MPI out of sync on rank " + rank + " for more than " + WaitTime + " seconds.");
                        Console.Error.WriteLine("Method '" + m_functionName + "' was called at " + entryTime + " on process " + rank + " but not on all other processes.");
                        Console.Error.WriteLine("Call stack:");
                        Console.Error.WriteLine(st.ToString());
                        //System.Environment.Exit(-666);
                        //Debugger.Launch();
                        WaitTime += 10.0;
                    }
                }
            }));

            wDog.Start();


            // wait for all processors to arrive here
            // ======================================
            csMPI.Raw.Barrier(comm); // must be in the 'main' thread
            // MPI is not necessarily thread-safe!

            // ok
            Fire = 0; // quit watchdog
            while (Fired != 0)
            {
                ;
            }
        }
Пример #22
0
    //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}");
        }
    }
Пример #23
0
    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);
        }
    }