Пример #1
0
    // 功能:添加函数调用的方法
    void PushMethodInfo(MethodInfo method)
    {
        if (0 != (MethodAttributes.SpecialName & method.Attributes))
        {
            return;
        }
        if (m_bPartWrap)
        {
            if (!method.IsDefined(typeof(PartWrapAttribute), false))
            {
                return;
            }
        }
        // 如果该函数有不导出的标记
        if (method.IsDefined(typeof(DontWrapAttribute), false))
        {
            return;
        }
        if (method.IsDefined(typeof(ObsoleteAttribute), false))
        {
            return;
        }
        string szMethodName  = method.ToString();
        bool   bTemplateFunc = IsTemplateFunc(method);

        // 模板函数暂时不导出吧
        if (bTemplateFunc)
        {
            return;
        }
        FCValueType ret_value = m_templateWrap.PushReturnTypeWrap(method.ReturnType);

        PushNameSpace(method.ReturnType.Namespace);

        int nSameNameCount = 0;

        if (m_CurSameName.TryGetValue(method.Name, out nSameNameCount))
        {
        }
        m_CurSameName[method.Name] = nSameNameCount + 1;
        int nFuncCount = 0;

        m_CurFuncCount.TryGetValue(method.Name, out nFuncCount);

        WrapFuncDesc func = new WrapFuncDesc();

        func.m_bAttrib = false;
        func.m_szName  = method.Name;
        if (nSameNameCount > 0)
        {
            func.m_szGetName = func.m_szSetName = string.Format("{0}{1}_wrap", method.Name, nSameNameCount);
        }
        else
        {
            func.m_szGetName = func.m_szSetName = string.Format("{0}_wrap", method.Name);
        }

        m_szTempBuilder.Length = 0;
        StringBuilder fileData = m_szTempBuilder;

        ParameterInfo[] allParams   = method.GetParameters(); // 函数参数
        Type            nRetType    = method.ReturnType;      // 返回值
        int             nParamCount = allParams != null ? allParams.Length : 0;
        bool            bEqualFunc  = func.m_szName == "Equals";

        fileData.AppendLine("    [MonoPInvokeCallbackAttribute(typeof(FCLibHelper.fc_call_back_inport_class_func))]");
        fileData.AppendFormat("    public static int {0}(long L)\r\n", func.m_szSetName);
        fileData.AppendLine("    {");
        fileData.AppendLine("        try");
        fileData.AppendLine("        {");

        fileData.AppendLine("            long nThisPtr = FCLibHelper.fc_get_inport_obj_ptr(L);");
        fileData.AppendFormat("            {0} obj = get_obj(nThisPtr);\r\n", m_szCurClassName);
        // 处理函数参数
        Type   nParamType;
        string szLeftName     = string.Empty;
        string szCallParam    = string.Empty;
        string szLeftType     = string.Empty;
        bool   bStatic        = method.IsStatic;
        string szFullFuncName = method.Name;

        for (int i = 0; i < nParamCount; ++i)
        {
            ParameterInfo param = allParams[i];
            nParamType = param.ParameterType;
            szLeftName = string.Format("arg{0}", i);
            PushNameSpace(nParamType.Namespace);
            FCValueType param_value = FCTemplateWrap.Instance.PushGetTypeWrap(nParamType);
            if (param.IsOut)
            {
                string szCSharpName = param_value.GetTypeName(true);
                fileData.AppendFormat("            {0} {1};\r\n", szCSharpName, szLeftName);
            }
            else
            {
                SetMemberValue(fileData, "            ", param_value, szLeftName, "L", i.ToString(), true, param.IsOut);
            }
            if (i > 0)
            {
                szCallParam += ',';
            }
            if (param.IsOut)
            {
                szCallParam += "out ";
            }
            else if (nParamType.IsByRef)
            {
                szCallParam += "ref ";
            }
            szCallParam   += szLeftName;
            szFullFuncName = szFullFuncName + '_' + param_value.GetTypeName(false);
        }
        // 处理返回值
        if (ret_value.m_nValueType == fc_value_type.fc_value_void)
        {
            if (bStatic)
            {
                fileData.AppendFormat("            {0}.{1}({2});\r\n", m_szCurClassName, func.m_szName, szCallParam);
            }
            else
            {
                fileData.AppendFormat("            obj.{0}({1});\r\n", func.m_szName, szCallParam);
            }
        }
        else
        {
            string szCShareRetName = ret_value.GetTypeName(true);
            if (bStatic)
            {
                fileData.AppendFormat("            {0} ret = {1}.{2}({3});\r\n", szCShareRetName, m_szCurClassName, func.m_szName, szCallParam);
            }
            else
            {
                fileData.AppendFormat("            {0} ret = obj.{1}({2});\r\n", szCShareRetName, func.m_szName, szCallParam);
            }
        }

        // 处理输出参数
        for (int i = 0; i < nParamCount; ++i)
        {
            ParameterInfo param = allParams[i];
            nParamType = param.ParameterType;
            szLeftName = string.Format("arg{0}", i);
            if (param.IsOut || nParamType.IsByRef)
            {
                FCValueType value = PushOutParamWrap(nParamType);
                fileData.Append(FCValueType.ModifyScriptCallParam("            ", value, szLeftName, "L", i.ToString(), true));
            }
        }
        // 处理返回值
        if (ret_value.m_nValueType != fc_value_type.fc_value_void)
        {
            FCValueType.PushReturnValue(fileData, "            ", ret_value, "L", "ret", false);
        }
        fileData.AppendLine("        }");
        fileData.AppendLine("        catch(Exception e)");
        fileData.AppendLine("        {");
        fileData.AppendLine("            Debug.LogException(e);");
        fileData.AppendLine("        }");
        fileData.AppendLine("        return 0;");
        fileData.AppendLine("    }");

        func.m_szContent = fileData.ToString();
        if (nFuncCount > 1)
        {
            func.m_szRegister = string.Format("FCLibHelper.fc_register_class_func(nClassName,\"{0}\",{1});", szFullFuncName, func.m_szGetName);
        }
        else
        {
            func.m_szRegister = string.Format("FCLibHelper.fc_register_class_func(nClassName,\"{0}\",{1});", func.m_szName, func.m_szGetName);
        }
        m_CurClassFunc.Add(func);
    }