Exemplo n.º 1
0
    static StringBuilder BuildRegisterFunction(ClassCallbackNames ccbn, JSMgr.ATypeInfo ti)
    {
        string        fmt = @"
public static void __Register()
[[
    JSMgr.CallbackInfo ci = new JSMgr.CallbackInfo();
    ci.type = typeof({0});
    ci.fields = new JSMgr.CSCallbackField[]
    [[
{1}
    ]];
    ci.properties = new JSMgr.CSCallbackProperty[]
    [[
{2}
    ]];
    ci.constructors = new JSMgr.MethodCallBackInfo[]
    [[
{3}
    ]];
    ci.methods = new JSMgr.MethodCallBackInfo[]
    [[
{4}
    ]];
    JSMgr.allCallbackInfo.Add(ci);
]]
";
        StringBuilder sb  = new StringBuilder();

        StringBuilder sbField    = new StringBuilder();
        StringBuilder sbProperty = new StringBuilder();
        StringBuilder sbCons     = new StringBuilder();
        StringBuilder sbMethod   = new StringBuilder();

        for (int i = 0; i < ccbn.fields.Count; i++)
        {
            sbField.AppendFormat("        {0},\n", ccbn.fields[i]);
        }
        for (int i = 0; i < ccbn.properties.Count; i++)
        {
            sbProperty.AppendFormat("        {0},\n", ccbn.properties[i]);
        }
        for (int i = 0; i < ccbn.constructors.Count; i++)
        {
            if (ccbn.constructors.Count == 1 && ti.constructors.Length == 0) // no constructors   add a default  so ...
            {
                sbCons.AppendFormat("        new JSMgr.MethodCallBackInfo({0}, '{2}', {1}),\n", ccbn.constructors[i], ccbn.constructorsCSParam[i], type.Name);
            }
            else
            {
                sbCons.AppendFormat("        new JSMgr.MethodCallBackInfo({0}, '{2}', {1}),\n", ccbn.constructors[i], ccbn.constructorsCSParam[i], ti.constructors[i].Name);
            }
        }
        for (int i = 0; i < ccbn.methods.Count; i++)
        {
            sbMethod.AppendFormat("        new JSMgr.MethodCallBackInfo({0}, '{2}', {1}),\n", ccbn.methods[i], ccbn.methodsCSParam[i], ti.methods[i].Name);
        }

        sb.AppendFormat(fmt, GetTypeFullName(ccbn.type), sbField, sbProperty, sbCons, sbMethod);
        return(sb);
    }
Exemplo n.º 2
0
    public static StringBuilder BuildMethods(Type type, MethodInfo[] methods, int[] olInfo, ClassCallbackNames ccbn)
    {
        /*
         * methods
         * 0 function name
         * 1 list<CSParam> generation
         * 2 function call
         */
        string        fmt = @"
static bool {0}(JSVCall vc, int start, int count)
[[
{1}
    return true;
]]
";
        StringBuilder sb  = new StringBuilder();

        for (int i = 0; i < methods.Length; i++)
        {
            MethodInfo      method = methods[i];
            ParameterInfo[] paramS = method.GetParameters();
            //静态类
            if (type.IsAbstract && type.IsSealed)
            {
                var needIgnore = NonGenerateFuncNameWhenStaticClass(method.Name);
                if (needIgnore)
                {
                    continue;
                }
            }
            int  olIndex    = olInfo[i];
            bool returnVoid = (method.ReturnType == typeof(void));

            string functionName = type.Name + "_" + method.Name + (olIndex > 0 ? olIndex.ToString() : "") + (method.IsStatic ? "_S" : "");

            sb.AppendFormat(fmt, functionName,

                            method.IsSpecialName ? BuildSpecialFunctionCall(paramS, type.Name, method.Name, method.IsStatic, returnVoid, method.ReturnType)
                : BuildNormalFunctionCall(paramS, type.Name, method.Name, method.IsStatic, returnVoid, method.ReturnType, false));

            ccbn.methods.Add(functionName);
            ccbn.methodsCSParam.Add(GenListCSParam2(paramS).ToString());
        }
        return(sb);
    }
Exemplo n.º 3
0
    public static StringBuilder BuildConstructors(Type type, ConstructorInfo[] constructors, ClassCallbackNames ccbn)
    {
        /*
         * methods
         * 0 function name
         * 1 list<CSParam> generation
         * 2 function call
         */
        string        fmt = @"
static bool {0}(JSVCall vc, int start, int count)
[[
{1}
    return true;
]]
";
        StringBuilder sb  = new StringBuilder();

        if (constructors.Length == 0 && JSBindingSettings.IsGeneratedDefaultConstructor(type) &&
            (type.IsValueType || (type.IsClass && !type.IsAbstract && !type.IsInterface)))
        {
            int    olIndex      = 1;
            bool   returnVoid   = false;
            string functionName = type.Name + "_" + type.Name +
                                  (olIndex > 0 ? olIndex.ToString() : "") + "";// (cons.IsStatic ? "_S" : "");
            sb.AppendFormat(fmt, functionName,
                            BuildNormalFunctionCall(new ParameterInfo[0], type.Name, type.Name, false, returnVoid, null, true));

            ccbn.constructors.Add(functionName);
            ccbn.constructorsCSParam.Add(GenListCSParam2(new ParameterInfo[0]).ToString());
        }

        for (int i = 0; i < constructors.Length; i++)
        {
            ConstructorInfo cons   = constructors[i];
            ParameterInfo[] paramS = cons.GetParameters();

            int  olIndex    = i + 1; // for constuctors, they are always overloaded
            bool returnVoid = false;

            string functionName = type.Name + "_" + type.Name + (olIndex > 0 ? olIndex.ToString() : "") + (cons.IsStatic ? "_S" : "");

            sb.AppendFormat(fmt, functionName,
                            BuildNormalFunctionCall(paramS, type.Name, cons.Name, cons.IsStatic, returnVoid, null, true));

            ccbn.constructors.Add(functionName);
            ccbn.constructorsCSParam.Add(GenListCSParam2(paramS).ToString());
        }
        return(sb);
    }
Exemplo n.º 4
0
    public static StringBuilder BuildFields(Type type, FieldInfo[] fields, ClassCallbackNames ccbn)
    {
        var sb = new StringBuilder();

        for (int i = 0; i < fields.Length; i++)
        {
            var sbCall = new StringBuilder();

            FieldInfo field      = fields[i];
            bool      isDelegate = (typeof(System.Delegate).IsAssignableFrom(field.FieldType));
            if (isDelegate)
            {
                sb.Append(BuildField_DelegateFunction(type, field));
            }


            sb.AppendFormat("static void {0}_{1}(JSVCall vc)\n[[\n", type.Name, field.Name);

            bool bReadOnly = (field.IsInitOnly || field.IsLiteral);
            if (!bReadOnly)
            {
                sb.Append("if (vc.bGet)\n");
            }


            //if (type.IsValueType && !field.IsStatic)
            //    sb.AppendFormat("{0} argThis = ({0})vc.csObj;", type.Name);

            // get
            if (field.IsStatic)
            {
                sbCall.AppendFormat("{0}.{1}", type.Name, field.Name);
            }
            else
            {
                sbCall.AppendFormat("(({0})vc.csObj).{1}", type.Name, field.Name);
            }
            sb.AppendFormat("    {0};\n", BuildReturnObject(field.FieldType, sbCall.ToString()));

            // set
            if (!bReadOnly)
            {
                if (!isDelegate)
                {
                    sb.Append("else\n");
                    if (field.IsStatic)
                    {
                        sb.AppendFormat("{0}.{1} = ({2}){3};\n", type.Name, field.Name, field.FieldType, BuildRetriveParam(field.FieldType));
                    }
                    else
                    {
                        if (type.IsValueType)
                        {
                            sb.AppendFormat("[[\n    {0} argThis = ({0})vc.csObj;\n", type.Name);
                            sb.AppendFormat("    argThis.{0} = ({1}){2};\n", field.Name, field.FieldType, BuildRetriveParam(field.FieldType));
                            sb.Append("    JSMgr.changeJSObj(vc.jsObj, argThis);\n]]\n");
                        }
                        else
                        {
                            sb.AppendFormat("(({0})vc.csObj).{1} = ({2});\n", GetTypeFullName(type), field.Name, BuildRetriveParam(field.FieldType));
                        }
                    }
                }
                else
                {
                    var getDelegateFuncitonName = new StringBuilder();
                    getDelegateFuncitonName.AppendFormat("{0}_{1}_GetDelegate", type.Name, field.Name);

                    sb.Append("else\n");
                    if (field.IsStatic)
                    {
                        sb.AppendFormat("{0}.{1} = {2}(vc.getJSFunction());\n", type.Name, field.Name, getDelegateFuncitonName);
                    }
                    else
                    {
                        if (type.IsValueType)
                        {
                            sb.AppendFormat("[[\n    {0} argThis = ({0})vc.csObj;\n", type.Name);
                            sb.AppendFormat("    argThis.{0} = {1}(vc.getJSFunction());\n", field.Name, getDelegateFuncitonName);
                            sb.Append("    JSMgr.changeJSObj(vc.jsObj, argThis);\n]]\n");
                        }
                        else
                        {
                            sb.AppendFormat("(({0})vc.csObj).{1} = {2}(vc.getJSFunction());\n", GetTypeFullName(type), field.Name, getDelegateFuncitonName);
                        }
                    }
                }
            }

            sb.AppendFormat("]]\n");

//             string f = fmt;
//             if (field.IsStatic) f = bReadOnly ? fmtStaticReadOnly : fmtStatic;
//             else if (bReadOnly) f = fmtReadOnly;
//             else if (type.IsValueType) f = fmtValueType;
//
//             sb.AppendFormat(f, type.Name, field.Name, field.FieldType);
            ccbn.fields.Add(type.Name + "_" + field.Name);
        }

        return(sb);
    }
Exemplo n.º 5
0
    public static void GenerateClass()
    {
        /*if (type.IsInterface)
         * {
         *  Debug.Log("Interface: " + type.ToString() + " ignored.");
         *  return;
         * }*/

        JSMgr.ATypeInfo ti;
        /*int slot = */ JSMgr.AddTypeInfo(type, out ti);
//         var sbCons = BuildConstructors(type, ti.constructors, slot);
//         var sbFields = BuildFields(type, ti.fields, slot);
//         var sbProperties = BuildProperties(type, ti.properties, slot);
//         var sbMethods = BuildMethods(type, ti.methods, slot);
//         var sbClass = BuildClass(type, sbFields, sbProperties, sbMethods, sbCons);
//         HandleStringFormat(sbClass);

        ClassCallbackNames ccbn = new ClassCallbackNames();
        {
            ccbn.type         = type;
            ccbn.fields       = new List <string>(ti.fields.Length);
            ccbn.properties   = new List <string>(ti.properties.Length);
            ccbn.constructors = new List <string>(ti.constructors.Length);
            ccbn.methods      = new List <string>(ti.methods.Length);

            ccbn.constructorsCSParam = new List <string>(ti.constructors.Length);
            ccbn.methodsCSParam      = new List <string>(ti.methods.Length);
        }

        var sbFields     = BuildFields(type, ti.fields, ccbn);
        var sbProperties = BuildProperties(type, ti.properties, ccbn);
        var sbMethods    = BuildMethods(type, ti.methods, ti.methodsOLInfo, ccbn);
        var sbCons       = BuildConstructors(type, ti.constructors, ccbn);
        var sbRegister   = BuildRegisterFunction(ccbn, ti);
        var sbClass      = BuildClass(type, sbFields, sbProperties, sbMethods, sbCons, sbRegister);

        /*
         * 0 typeName
         * 1 class contents
         * 2 type namespace
         */
        string fmtFile         = @"
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.Rendering;
{2}

public class {0}Generated
[[
{1}
]]
";
        var    sbFile          = new StringBuilder();
        string nameSpaceString = string.Empty;

        if (type.Namespace != null)
        {
            nameSpaceString = type.Namespace;
            if (nameSpaceString == "UnityEngine")
            {
                nameSpaceString = string.Empty;
            }
        }
        sbFile.AppendFormat(fmtFile, type.Name, sbClass, nameSpaceString.Length > 0?"using " + nameSpaceString + ";":"");
        HandleStringFormat(sbFile);

        sbFile.Replace("\r\n", "\n");

        string fileName = JSMgr.csGeneratedDir + "/" + type.Name + "Generated.cs";
        var    writer2  = OpenFile(fileName, false);

        writer2.Write(sbFile.ToString());
        writer2.Close();
    }
Exemplo n.º 6
0
    public static StringBuilder BuildProperties(Type type, PropertyInfo[] properties, ClassCallbackNames ccbn)
    {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < properties.Length; i++)
        {
            var sbCall = new StringBuilder();

            PropertyInfo property = properties[i];

            sb.AppendFormat("static void {0}_{1}(JSVCall vc)\n[[\n", type.Name, property.Name);

            MethodInfo[] accessors = property.GetAccessors();
            bool         isStatic  = accessors[0].IsStatic;

            bool bReadOnly = !property.CanWrite;


            if (!bReadOnly)
            {
                sb.Append("if (vc.bGet)\n");
            }


            //if (type.IsValueType && !field.IsStatic)
            //    sb.AppendFormat("{0} argThis = ({0})vc.csObj;", type.Name);

            // get
            if (isStatic)
            {
                sbCall.AppendFormat("{0}.{1}", GetTypeFullName(type), property.Name);
            }
            else
            {
                sbCall.AppendFormat("(({0})vc.csObj).{1}", GetTypeFullName(type), property.Name);
            }
            sb.AppendFormat("    {0};\n", BuildReturnObject(property.PropertyType, sbCall.ToString()));

            // set
            if (!bReadOnly)
            {
                sb.Append("else\n");
                if (isStatic)
                {
                    sb.AppendFormat("{0}.{1} = ({2}){3};", GetTypeFullName(type), property.Name, GetTypeFullName(property.PropertyType), BuildRetriveParam(property.PropertyType));
                }
                else
                {
                    if (type.IsValueType)
                    {
                        sb.AppendFormat("[[\n    {0} argThis = ({0})vc.csObj;\n", GetTypeFullName(type));
                        sb.AppendFormat("    argThis.{0} = ({1}){2};\n", property.Name, GetTypeFullName(property.PropertyType), BuildRetriveParam(property.PropertyType));
                        sb.Append("    JSMgr.changeJSObj(vc.jsObj, argThis);\n]]\n");
                    }
                    else
                    {
                        sb.AppendFormat("(({0})vc.csObj).{1} = ({2});", GetTypeFullName(type), property.Name, BuildRetriveParam(property.PropertyType));
                    }
                }
            }

            sb.AppendFormat("]]\n");

            ccbn.properties.Add(type.Name + "_" + property.Name);
        }
        return(sb);
    }
Exemplo n.º 7
0
    public static StringBuilder BuildProperties(Type type, PropertyInfo[] properties, int[] propertiesIndex, ClassCallbackNames ccbn)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < properties.Length; i++)
        {
            var sbCall = new StringBuilder();

            PropertyInfo property = properties[i];
            MethodInfo[] accessors = property.GetAccessors();
            bool isStatic = accessors[0].IsStatic;
            JSDataExchangeEditor.MemberFeature features = 0;
            if (isStatic) features |= JSDataExchangeEditor.MemberFeature.Static;

            bool bGenericT = type.IsGenericTypeDefinition;
            StringBuilder sbt = null;

            bool isDelegate = JSDataExchangeEditor.IsDelegateDerived(property.PropertyType); ;// (typeof(System.Delegate).IsAssignableFrom(property.PropertyType));
            if (isDelegate)
            {
                sb.Append(JSDataExchangeEditor.Build_DelegateFunction(type, property, property.PropertyType, i, 0));
            }

            // PropertyID
            if (bGenericT)
            {
                cg.args arg = new cg.args();
                arg.AddFormat("\"{0}\"", property.Name);

                arg.AddFormat("\"{0}\"", property.PropertyType.Name);
                if (property.PropertyType.IsGenericParameter)
                {
                    arg.Add("TypeFlag.IsT");
                }
                else
                {
                    arg.Add("TypeFlag.None");
                }

                cg.args arg1 = new cg.args();
                cg.args arg2 = new cg.args();

                foreach (ParameterInfo p in property.GetIndexParameters())
                {
                    cg.args argFlag = ParameterInfo2TypeFlag(p);

                    arg1.AddFormat("\"{0}\"", p.ParameterType.Name);
                    arg2.Add(argFlag.Format(cg.args.ArgsFormat.Flag));
                }

                if (arg1.Count > 0)
                    arg.AddFormat("new string[]{0}", arg1.Format(cg.args.ArgsFormat.Brace));
                else
                    arg.Add("null");
                if (arg2.Count > 0)
                    arg.AddFormat("new TypeFlag[]{0}", arg2.Format(cg.args.ArgsFormat.Brace));
                else
                    arg.Add("null");
                sb.AppendFormat("public static PropertyID propertyID{0} = new PropertyID({1});\n", i, arg.ToString());
            }

            if (bGenericT)
            {
                sbt = new StringBuilder();
                sbt.AppendFormat("    PropertyInfo member = GenericTypeCache.getProperty(vc.csObj.GetType(), propertyID{0}); \n", i);
                sbt.AppendFormat("    if (member == null) return;\n");
                sbt.Append("\n");
            }

            //
            // check to see if this is a indexer
            //
            ParameterInfo[] ps = property.GetIndexParameters();
            bool bIndexer = (ps.Length > 0);
            if (bIndexer) features |= JSDataExchangeEditor.MemberFeature.Indexer;
            cg.args argActual = new cg.args();
            JSDataExchangeEditor.ParamHandler[] paramHandlers = new JSDataExchangeEditor.ParamHandler[ps.Length];
            for (int j = 0; j < ps.Length; j++)
            {
                paramHandlers[j] = JSDataExchangeEditor.Get_ParamHandler(ps[j].ParameterType, j, false, false);
                argActual.Add(paramHandlers[j].argName);
            }

            string functionName = type.Name + "_" + property.Name;
            if (bIndexer)
            {
                foreach (var p in ps)
                {
                    functionName += "_" + p.ParameterType.Name;
                }
            }
            functionName = JSNameMgr.HandleFunctionName(functionName);

            sb.AppendFormat("static void {0}(JSVCall vc)\n[[\n", functionName);

            if (bGenericT)
            {
                sb.Append(sbt);
            }
            for (int j = 0; j < ps.Length; j++)
            {
                sb.Append("        " + paramHandlers[j].getter + "\n");
            }

            bool bReadOnly = (!property.CanWrite || property.GetSetMethod() == null);
            sbCall.Append(JSDataExchangeEditor.BuildCallString(type, property, argActual.Format(cg.args.ArgsFormat.OnlyList),
                                features | JSDataExchangeEditor.MemberFeature.Get));

            if (!bReadOnly)
            {
                sb.Append("    if (vc.bGet)\n");
                sb.Append("    [[ \n");
            }

            //if (type.IsValueType && !field.IsStatic)
            //    sb.AppendFormat("{0} argThis = ({0})vc.csObj;", type.Name);

            if (property.CanRead)
            {
                if (property.GetGetMethod() != null)
                {
                    sb.Append(sbCall);
                    sb.AppendFormat("        {0}\n", JSDataExchangeEditor.Get_Return(property.PropertyType, "result"));
                }
                else
                {
                    Debug.Log(type.Name + "." + property.Name + " 'get' is ignored because it's not public.");
                }
            }
            if (!bReadOnly)
            {
                sb.Append("    ]]\n");
            }

            // set
            if (!bReadOnly)
            {
                sb.Append("    else\n");
                sb.Append("    [[ \n");

                if (!isDelegate)
                {
                    int ParamIndex = ps.Length;

                    var paramHandler = JSDataExchangeEditor.Get_ParamHandler(property.PropertyType, ParamIndex, false, false);
                    sb.Append("        " + paramHandler.getter + "\n");

                    sb.Append(JSDataExchangeEditor.BuildCallString(type, property, argActual.Format(cg.args.ArgsFormat.OnlyList),
                                    features | JSDataExchangeEditor.MemberFeature.Set, paramHandler.argName));
                }
                else
                {
                    var getDelegateFuncitonName = JSDataExchangeEditor.GetMethodArg_DelegateFuncionName(type, property.Name, i, 0);

                    //                     sb.Append(JSDataExchangeEditor.BuildCallString(type, field, "" /* argList */,
                    //                                 features | JSDataExchangeEditor.MemberFeature.Set, getDelegateFuncitonName + "(vc.getJSFunctionValue())"));

                    string getDelegate = JSDataExchangeEditor.Build_GetDelegate(getDelegateFuncitonName, property.PropertyType);
                    sb.Append(JSDataExchangeEditor.BuildCallString(type, property, "" /* argList */,
                                features | JSDataExchangeEditor.MemberFeature.Set, getDelegate));
                }
                sb.Append("    ]]\n");
            }

            sb.AppendFormat("]]\n");

            ccbn.properties.Add(functionName);
        }
        return sb;
    }
Exemplo n.º 8
0
    public static StringBuilder BuildPropertiesTypeT(Type type, PropertyInfo[] properties, int[] propertiesIndex, ClassCallbackNames ccbn)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < properties.Length; i++)
        {
            var sbCall = new StringBuilder();

            PropertyInfo property = properties[i];

            bool bT = type.IsGenericTypeDefinition;
            StringBuilder sbt = null;
            if (bT)
            {
                sbt = new StringBuilder();

                sbt.AppendFormat("    PropertyInfo property = JSDataExchangeMgr.GetPropertyInfoOfGenericClass(vc.csObj.GetType(), {0}); \n",
                        propertiesIndex[i]);        // [0] methodArrIndex

                sbt.AppendFormat("    if (property == null)\n        return true;\n");
                sbt.Append("\n");

                sb.Append(sbt);
            }

            //
            // check to see if this is a indexer
            //
            ParameterInfo[] ps = property.GetIndexParameters();
            bool bIndexer = (ps.Length > 0);
            StringBuilder sbActualParam = null;
            JSDataExchangeEditor.ParamHandler[] paramHandlers = null;
            if (bIndexer)
            {
                sbActualParam = new StringBuilder();
                paramHandlers = new JSDataExchangeEditor.ParamHandler[ps.Length];
                sbActualParam.Append("[");
                for (int j = 0; j < ps.Length; j++)
                {
                    paramHandlers[j] = JSDataExchangeEditor.Get_ParamHandler(ps[j].ParameterType, j, false, false);
                    sbActualParam.AppendFormat("{0}", paramHandlers[j].argName);
                    if (j != ps.Length - 1)
                        sbActualParam.Append(", ");
                }
                sbActualParam.Append("]");
            }

            string functionName = type.Name + "_" + property.Name;
            if (bIndexer)
            {
                foreach (var p in ps)
                {
                    functionName += "_" + p.ParameterType.Name;
                }
            }
            functionName = JSNameMgr.HandleFunctionName(functionName);

            sb.AppendFormat("static void {0}(JSVCall vc)\n[[\n", functionName);

            MethodInfo[] accessors = property.GetAccessors();
            bool isStatic = accessors[0].IsStatic;

            bool bReadOnly = !property.CanWrite;
            if (bIndexer)
            {
                for (int j = 0; j < ps.Length; j++)
                {
                    sb.Append("        " + paramHandlers[j].getter + "\n");
                }
                if (bT)
                {
                    if (isStatic)
                    {
                        sbCall.AppendFormat("{0}{1}", JSNameMgr.GetTypeFullName(type), sbActualParam);
                    }
                    else
                    {
                        sbCall.AppendFormat("(({0})vc.csObj){1}", JSNameMgr.GetTypeFullName(type), sbActualParam);
                    }
                }
                else
                {
                    if (isStatic)
                    {
                        sbCall.AppendFormat("{0}{1}", JSNameMgr.GetTypeFullName(type), sbActualParam);
                    }
                    else
                    {
                        sbCall.AppendFormat("(({0})vc.csObj){1}", JSNameMgr.GetTypeFullName(type), sbActualParam);
                    }
                }
            }

            if (!bReadOnly)
            {
                sb.Append("    if (vc.bGet) [[ \n");
            }

            if (!bIndexer)
            {
                // get
                if (isStatic)
                    sbCall.AppendFormat("{0}.{1}", JSNameMgr.GetTypeFullName(type), property.Name);
                else
                    sbCall.AppendFormat("(({0})vc.csObj).{1}", JSNameMgr.GetTypeFullName(type), property.Name);
            }

            //if (type.IsValueType && !field.IsStatic)
            //    sb.AppendFormat("{0} argThis = ({0})vc.csObj;", type.Name);

            sb.AppendFormat("        {0}", JSDataExchangeEditor.Get_Return(property.PropertyType, sbCall.ToString()));
            if (!bReadOnly)
            {
                sb.Append("\n    ]]\n");
            }

            // set
            if (!bReadOnly)
            {
                sb.Append("    else [[\n");

                int ParamIndex = ps.Length;

                var paramHandler = JSDataExchangeEditor.Get_ParamHandler(property.PropertyType, ParamIndex, false, false);
                sb.Append("        " + paramHandler.getter + "\n");

                if (bIndexer)
                {
                    if (isStatic)
                        sb.AppendFormat("{0} = {1};\n", sbCall, paramHandler.argName);
                    else
                    {
                        if (type.IsValueType)
                        {
                            sb.AppendFormat("        {0} argThis = ({0})vc.csObj;\n", JSNameMgr.GetTypeFullName(type));
                            sb.AppendFormat("argThis{0} = {1};", sbActualParam, paramHandler.argName);
                            sb.Append("        JSMgr.changeJSObj(vc.jsObjID, argThis);\n");
                        }
                        else
                        {
                            sb.AppendFormat("        {0} = {1};\n", sbCall, paramHandler.argName);
                        }
                    }
                }
                else
                {
                    if (isStatic)
                        sb.AppendFormat("{0}.{1} = {2};\n", JSNameMgr.GetTypeFullName(type), property.Name, paramHandler.argName);
                    else
                    {
                        if (type.IsValueType)
                        {
                            sb.AppendFormat("        {0} argThis = ({0})vc.csObj;\n", JSNameMgr.GetTypeFullName(type));
                            sb.AppendFormat("        argThis.{0} = {1};\n", property.Name, paramHandler.argName);
                            sb.Append("        JSMgr.changeJSObj(vc.jsObjID, argThis);\n");
                        }
                        else
                        {
                            sb.AppendFormat("        (({0})vc.csObj).{1} = {2};\n", JSNameMgr.GetTypeFullName(type), property.Name, paramHandler.argName);
                        }
                    }
                }
                sb.Append("    ]]\n");
            }

            sb.AppendFormat("]]\n");

            ccbn.properties.Add(functionName);
        }
        return sb;
    }
Exemplo n.º 9
0
    public static StringBuilder BuildConstructors(Type type, ConstructorInfo[] constructors, int[] constructorsIndex, ClassCallbackNames ccbn)
    {
        /*
        * methods
        * 0 function name
        * 1 list<CSParam> generation
        * 2 function call
        */
        string fmt = @"
        static bool {0}(JSVCall vc, int argc)
        [[
        {1}
        return true;
        ]]
        ";
        StringBuilder sb = new StringBuilder();
        /*if (constructors.Length == 0 && JSBindingSettings.IsGeneratedDefaultConstructor(type) &&
            (type.IsValueType || (type.IsClass && !type.IsAbstract && !type.IsInterface)))
        {
            int olIndex = 1;
            bool returnVoid = false;
            string functionName = type.Name + "_" + type.Name +
                (olIndex > 0 ? olIndex.ToString() : "") + "";// (cons.IsStatic ? "_S" : "");
            sb.AppendFormat(fmt, functionName,
                BuildNormalFunctionCall(0, new ParameterInfo[0], type.Name, type.Name, false, returnVoid, null, true));

            ccbn.constructors.Add(functionName);
            ccbn.constructorsCSParam.Add(GenListCSParam2(new ParameterInfo[0]).ToString());
        }*/

        // increase index if adding default constructor
        //         int deltaIndex = 0;
         if (JSBindingSettings.NeedGenDefaultConstructor(type))
         {
        //             deltaIndex = 1;
         }

        for (int i = 0; i < constructors.Length; i++)
        {
            ConstructorInfo cons = constructors[i];

            if (cons == null)
            {
                sb.AppendFormat("public static ConstructorID constructorID{0} = new ConstructorID({1});\n", i, "null, null");

                // this is default constructor
                //bool returnVoid = false;
                //string functionName = type.Name + "_" + type.Name + "1";
                int olIndex = i + 1; // for constuctors, they are always overloaded
                string functionName = JSNameMgr.HandleFunctionName(type.Name + "_" + type.Name + (olIndex > 0 ? olIndex.ToString() : ""));

                sb.AppendFormat(fmt, functionName,
                    BuildNormalFunctionCall(0, new ParameterInfo[0], type.Name, false, null, true));

                ccbn.constructors.Add(functionName);
                ccbn.constructorsCSParam.Add(GenListCSParam2(new ParameterInfo[0]).ToString());
            }
            else
            {
                ParameterInfo[] paramS = cons.GetParameters();
                int olIndex = i + 1; // for constuctors, they are always overloaded
                int methodTag = i/* + deltaIndex*/;

                for (int j = 0; j < paramS.Length; j++)
                {
                    if (JSDataExchangeEditor.IsDelegateDerived(paramS[j].ParameterType))
                    {
                        StringBuilder sbD = JSDataExchangeEditor.Build_DelegateFunction(type, cons, paramS[j].ParameterType, methodTag, j);
                        sb.Append(sbD);
                    }
                }

                // ConstructorID
                if (type.IsGenericTypeDefinition)
                {
                    cg.args arg = new cg.args();
                    cg.args arg1 = new cg.args();
                    cg.args arg2 = new cg.args();

                    foreach (ParameterInfo p in cons.GetParameters())
                    {
                        cg.args argFlag = ParameterInfo2TypeFlag(p);
                        arg1.AddFormat("\"{0}\"", p.ParameterType.Name);
                        arg2.Add(argFlag.Format(cg.args.ArgsFormat.Flag));
                    }

                    if (arg1.Count > 0)
                        arg.AddFormat("new string[]{0}", arg1.Format(cg.args.ArgsFormat.Brace));
                    else
                        arg.Add("null");
                    if (arg2.Count > 0)
                        arg.AddFormat("new TypeFlag[]{0}", arg2.Format(cg.args.ArgsFormat.Brace));
                    else
                        arg.Add("null");
                    sb.AppendFormat("public static ConstructorID constructorID{0} = new ConstructorID({1});\n", i, arg.ToString());
                }

                string functionName = JSNameMgr.HandleFunctionName(type.Name + "_" + type.Name + (olIndex > 0 ? olIndex.ToString() : "") + (cons.IsStatic ? "_S" : ""));

                sb.AppendFormat(fmt, functionName,
                    BuildNormalFunctionCall(methodTag, paramS, cons.Name, cons.IsStatic, null, true, 0));

                ccbn.constructors.Add(functionName);
                ccbn.constructorsCSParam.Add(GenListCSParam2(paramS).ToString());
            }
        }
        return sb;
    }
Exemplo n.º 10
0
    public static StringBuilder BuildMethods(Type type, MethodInfo[] methods, int[] methodsIndex, int[] olInfo, ClassCallbackNames ccbn)
    {
        /*
        * methods
        * 0 function name
        * 1 list<CSParam> generation
        * 2 function call
        */
        string fmt = @"
        static bool {0}(JSVCall vc, int argc)
        [[
        {1}
        return true;
        ]]
        ";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < methods.Length; i++)
        {
            MethodInfo method = methods[i];
            ParameterInfo[] paramS = method.GetParameters();

            for (int j = 0; j < paramS.Length; j++)
            {
        //                 if (paramS[j].ParameterType == typeof(DaikonForge.Tween.TweenAssignmentCallback<Vector3>))
        //                 {
        //                     Debug.Log("yes");
        //
                //if (typeof(System.Delegate).IsAssignableFrom(paramS[j].ParameterType))
                if (JSDataExchangeEditor.IsDelegateDerived(paramS[j].ParameterType))
                {
                    // StringBuilder sbD = JSDataExchangeEditor.BuildFunctionArg_DelegateFunction(type.Name, method.Name, paramS[j].ParameterType, i, j);
                    StringBuilder sbD = JSDataExchangeEditor.Build_DelegateFunction(type, method, paramS[j].ParameterType, i, j);

                    sb.Append(sbD);
                }
            }

            // MethodID
            if (type.IsGenericTypeDefinition || method.IsGenericMethodDefinition)
            {
                cg.args arg = new cg.args();
                arg.AddFormat("\"{0}\"", method.Name);

                arg.AddFormat("\"{0}\"", method.ReturnType.Name);
                if (method.ReturnType.IsGenericParameter)
                {
                    arg.Add("TypeFlag.IsT");
                }
                else
                {
                    arg.Add("TypeFlag.None");
                }

                cg.args arg1 = new cg.args();
                cg.args arg2 = new cg.args();

                foreach (ParameterInfo p in method.GetParameters())
                {
                    // flag of a parameter
                    cg.args argFlag = ParameterInfo2TypeFlag(p);

                    arg1.AddFormat("\"{0}\"", p.ParameterType.Name);
                    arg2.Add(argFlag.Format(cg.args.ArgsFormat.Flag));
                }

                if (arg1.Count > 0)
                    arg.AddFormat("new string[]{0}", arg1.Format(cg.args.ArgsFormat.Brace));
                else
                    arg.Add("null");
                if (arg2.Count > 0)
                    arg.AddFormat("new TypeFlag[]{0}", arg2.Format(cg.args.ArgsFormat.Brace));
                else
                    arg.Add("null");
                sb.AppendFormat("public static MethodID methodID{0} = new MethodID({1});\n", i, arg.ToString());
            }

            int olIndex = olInfo[i];
            bool returnVoid = (method.ReturnType == typeof(void));

            string functionName = type.Name + "_" + method.Name + (olIndex > 0 ? olIndex.ToString() : "") + (method.IsStatic ? "_S" : "");

            int TCount = 0;
            if (method.IsGenericMethodDefinition) {
                TCount = method.GetGenericArguments().Length;
            }

            // if you change functionName
            // also have to change code in 'Manual/' folder
            functionName = JSNameMgr.HandleFunctionName(type.Name + "_" + SharpKitMethodName(method.Name, paramS, true, TCount));
            if (method.IsSpecialName && method.Name == "op_Implicit" && paramS.Length > 0)
            {
                functionName += "_to_" + method.ReturnType.Name;
            }
            if (UnityEngineManual.isManual(functionName))
            {
                sb.AppendFormat(fmt, functionName, "    UnityEngineManual." + functionName + "(vc, argc);");
            }
            else
            {
                sb.AppendFormat(fmt, functionName,

                    method.IsSpecialName ? BuildSpecialFunctionCall(paramS, type.Name, method.Name, method.IsStatic, returnVoid, method.ReturnType)
                    : BuildNormalFunctionCall(i, paramS, method.Name, method.IsStatic, method.ReturnType,
                    false/* is constructor */,
                    TCount));
            }

            ccbn.methods.Add(functionName);
            ccbn.methodsCSParam.Add(GenListCSParam2(paramS).ToString());
        }
        return sb;
    }
Exemplo n.º 11
0
    static StringBuilder BuildRegisterFunction(ClassCallbackNames ccbn, GeneratorHelp.ATypeInfo ti)
    {
        string fmt = @"
        public static void __Register()
        [[
        JSMgr.CallbackInfo ci = new JSMgr.CallbackInfo();
        ci.type = typeof({0});
        ci.fields = new JSMgr.CSCallbackField[]
        [[
        {1}
        ]];
        ci.properties = new JSMgr.CSCallbackProperty[]
        [[
        {2}
        ]];
        ci.constructors = new JSMgr.MethodCallBackInfo[]
        [[
        {3}
        ]];
        ci.methods = new JSMgr.MethodCallBackInfo[]
        [[
        {4}
        ]];
        JSMgr.allCallbackInfo.Add(ci);
        ]]
        ";
        StringBuilder sb = new StringBuilder();

        StringBuilder sbField = new StringBuilder();
        StringBuilder sbProperty = new StringBuilder();
        StringBuilder sbCons = new StringBuilder();
        StringBuilder sbMethod = new StringBuilder();

        for (int i = 0; i < ccbn.fields.Count; i++)
            sbField.AppendFormat("        {0},\n", ccbn.fields[i]);
        for (int i = 0; i < ccbn.properties.Count; i++)
            sbProperty.AppendFormat("        {0},\n", ccbn.properties[i]);
        for (int i = 0; i < ccbn.constructors.Count; i++)
        {
            if (ccbn.constructors.Count == 1 && ti.constructors.Length == 0) // no constructors   add a default  so ...
                sbCons.AppendFormat("        new JSMgr.MethodCallBackInfo({0}, '{1}'),\n",
                    ccbn.constructors[i],
                    type.Name);
            else
                sbCons.AppendFormat("        new JSMgr.MethodCallBackInfo({0}, '{1}'),\n",
                    ccbn.constructors[i],
                    ti.constructors[i] == null ? ".ctor" : ti.constructors[i].Name);
        }
        for (int i = 0; i < ccbn.methods.Count; i++)
        {
            // if method is not overloaded
            // don's save the cs param array
            sbMethod.AppendFormat("        new JSMgr.MethodCallBackInfo({0}, '{1}'),\n",
                ccbn.methods[i],
                ti.methods[i].Name);
        }

        sb.AppendFormat(fmt, JSNameMgr.GetTypeFullName(ccbn.type), sbField, sbProperty, sbCons, sbMethod);
        return sb;
    }
Exemplo n.º 12
0
    public static StringBuilder BuildFields(Type type, FieldInfo[] fields, int[] fieldsIndex, ClassCallbackNames ccbn)
    {
        var sb = new StringBuilder();
        for (int i = 0; i < fields.Length; i++)
        {
            //var sbCall = new StringBuilder();

            FieldInfo field = fields[i];
            bool isDelegate = JSDataExchangeEditor.IsDelegateDerived(field.FieldType);// (typeof(System.Delegate).IsAssignableFrom(field.FieldType));
            if (isDelegate)
            {
                sb.Append(JSDataExchangeEditor.Build_DelegateFunction(type, field, field.FieldType, i, 0));
            }
            bool bGenericT = type.IsGenericTypeDefinition;
            if (bGenericT)
            {
                sb.AppendFormat("public static FieldID fieldID{0} = new FieldID(\"{1}\");\n", i, field.Name);
            }

            JSDataExchangeEditor.MemberFeature features = 0;
            if (field.IsStatic) features |= JSDataExchangeEditor.MemberFeature.Static;

            StringBuilder sbt = null;
            if (bGenericT)
            {
                sbt = new StringBuilder();

                sbt.AppendFormat("    FieldInfo member = GenericTypeCache.getField(vc.csObj.GetType(), fieldID{0}); \n", i);
                sbt.AppendFormat("    if (member == null) return;\n");
                sbt.Append("\n");
            }

            string functionName = JSNameMgr.HandleFunctionName(type.Name + "_" + field.Name);
            sb.AppendFormat("static void {0}(JSVCall vc)\n[[\n", functionName);

            if (bGenericT)
            {
                sb.Append(sbt);
            }

            bool bReadOnly = (field.IsInitOnly || field.IsLiteral);
            if (!bReadOnly)
            {
                sb.Append("    if (vc.bGet) [[\n");
            }

            // Debug.Log("FIELD " + type.Name + "." + field.Name);

            sb.Append(JSDataExchangeEditor.BuildCallString(type, field, "" /* argList */,
                                features | JSDataExchangeEditor.MemberFeature.Get));

            sb.AppendFormat("        {0}\n", JSDataExchangeEditor.Get_Return(field.FieldType, "result"));

            // set
            if (!bReadOnly)
            {
                sb.Append("    ]]\n    else [[\n");

                if (!isDelegate)
                {
                    var paramHandler = JSDataExchangeEditor.Get_ParamHandler(field);
                    sb.Append("        " + paramHandler.getter + "\n");

                    sb.Append(JSDataExchangeEditor.BuildCallString(type, field, "" /* argList */,
                                features | JSDataExchangeEditor.MemberFeature.Set, paramHandler.argName));
                }
                else
                {
                    var getDelegateFuncitonName = JSDataExchangeEditor.GetMethodArg_DelegateFuncionName(type, field.Name, i, 0);

        //                     sb.Append(JSDataExchangeEditor.BuildCallString(type, field, "" /* argList */,
        //                                 features | JSDataExchangeEditor.MemberFeature.Set, getDelegateFuncitonName + "(vc.getJSFunctionValue())"));

                    string getDelegate = JSDataExchangeEditor.Build_GetDelegate(getDelegateFuncitonName, field.FieldType);
                    sb.Append(JSDataExchangeEditor.BuildCallString(type, field, "" /* argList */,
                                features | JSDataExchangeEditor.MemberFeature.Set, getDelegate));
                }
                sb.Append("    ]]\n");
            }

            sb.AppendFormat("]]\n");
            ccbn.fields.Add(functionName);
        }

        return sb;
    }
Exemplo n.º 13
0
    public static void GenerateClass()
    {
        /*if (type.IsInterface)
        {
            Debug.Log("Interface: " + type.ToString() + " ignored.");
            return;
        }*/

        GeneratorHelp.ATypeInfo ti;
        /*int slot = */
        GeneratorHelp.AddTypeInfo(type, out ti);
        //         var sbCons = BuildConstructors(type, ti.constructors, slot);
        //         var sbFields = BuildFields(type, ti.fields, slot);
        //         var sbProperties = BuildProperties(type, ti.properties, slot);
        //         var sbMethods = BuildMethods(type, ti.methods, slot);
        //         var sbClass = BuildClass(type, sbFields, sbProperties, sbMethods, sbCons);
        //         HandleStringFormat(sbClass);

        ClassCallbackNames ccbn = new ClassCallbackNames();
        {
            ccbn.type = type;
            ccbn.fields = new List<string>(ti.fields.Length);
            ccbn.properties = new List<string>(ti.properties.Length);
            ccbn.constructors = new List<string>(ti.constructors.Length);
            ccbn.methods = new List<string>(ti.methods.Length);

            ccbn.constructorsCSParam = new List<string>(ti.constructors.Length);
            ccbn.methodsCSParam = new List<string>(ti.methods.Length);
        }

        thisClassName = JSNameMgr.GetTypeFileName(type) + "Generated";

        var sbFields = BuildFields(type, ti.fields, ti.fieldsIndex, ccbn);
        var sbProperties = BuildProperties(type, ti.properties, ti.propertiesIndex, ccbn);
        var sbMethods = BuildMethods(type, ti.methods, ti.methodsIndex, ti.methodsOLInfo, ccbn);
        var sbCons = BuildConstructors(type, ti.constructors, ti.constructorsIndex, ccbn);
        var sbRegister = BuildRegisterFunction(ccbn, ti);
        var sbClass = BuildClass(type, sbFields, sbProperties, sbMethods, sbCons, sbRegister);

        /*
         * 0 typeName
         * 1 class contents
         * 2 type namespace
         */
        string fmtFile = @"
        using UnityEngine;
        using System;
        using System.Collections;
        using System.Collections.Generic;
        using System.IO;
        using System.Reflection;
        {2}

        using jsval = JSApi.jsval;

        public class {0}
        [[
        {1}
        ]]
        ";
        var sbFile = new StringBuilder();
        string nameSpaceString = string.Empty;
        if (type.Namespace != null)
        {
            nameSpaceString = type.Namespace;
            if (nameSpaceString == "UnityEngine"
                || nameSpaceString == "System"
                || nameSpaceString == "System.Collections"
                || nameSpaceString == "System.Collections.Generic"
                || nameSpaceString == "System.IO"
                || nameSpaceString == "System.Reflection"
                )
            {
                nameSpaceString = string.Empty;
            }
        }
        sbFile.AppendFormat(fmtFile, thisClassName, sbClass, nameSpaceString.Length > 0 ? "using " + nameSpaceString + ";" : "");
        HandleStringFormat(sbFile);

        sbFile.Replace("\r\n", "\n");

        string fileName = JSBindingSettings.csGeneratedDir + "/" +
            JSNameMgr.GetTypeFileName(type) +
            "Generated.cs";
        var writer2 = OpenFile(fileName, false);
        writer2.Write(sbFile.ToString());
        writer2.Close();
    }