Beispiel #1
0
        public void MakeCode(System.IO.Stream combiedcodestm, string as3apipath, string csharpcodepath, string chsharpcodenamespace,
                             string regfunctioncodenamespace, out string regfunctioncode)
        {
            Dictionary <Type, CreatorBase> creators = new Dictionary <Type, CreatorBase>();

            foreach (var item in types)
            {
                var classtype = item;

                if (!CreatorBase.IsSkipType(classtype) && !CreatorBase.IsSkipCreator(classtype)

                    && (classtype.IsClass || classtype.IsValueType) && classtype.IsPublic
                    )
                {
                    CreatorBase.MakeCreator(classtype, creators, as3apipath, csharpcodepath, chsharpcodenamespace);
                }
            }

            StringBuilder regclassSb = new StringBuilder();

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(combiedcodestm, System.Text.Encoding.UTF8))
            {
                StringBuilder usingcode = new StringBuilder();
                CreatorBase.GenNativeFuncImport(usingcode);

                sw.WriteLine(usingcode.ToString());

                regclassSb.AppendLine("using System;");
                regclassSb.AppendLine("using System.Collections.Generic;");
                regclassSb.AppendLine("using System.Text;");
                regclassSb.AppendLine("using ASBinCode;");
                //regclassSb.AppendLine("using ASCTest.regNativeFunctions;");
                regclassSb.AppendLine("using ASRuntime.nativefuncs;");

                regclassSb.AppendLine("namespace " + regfunctioncodenamespace);
                regclassSb.AppendLine("{");

                regclassSb.AppendLine("\tpartial class extFunctions : INativeFunctionRegister");
                regclassSb.AppendLine("\t{");
                regclassSb.AppendLine("\t\tprivate void regAutoCreateCodes(CSWC bin)");
                regclassSb.AppendLine("\t\t{");

                foreach (var item in creators.Values)
                {
                    Console.WriteLine("building:" + item.BuildIngType);
                    string code = item.Create().Replace("\r", "").Replace("\n", "\r\n");


                    regclassSb.AppendLine("\t\t\t" + item.linkcodenamespace + "." + CreatorBase.GetNativeFunctionClassName(item.BuildIngType) + ".regNativeFunctions(bin);");
                    sw.WriteLine(code);
                }

                regclassSb.AppendLine("\t\t}");
                regclassSb.AppendLine("\t}");
                regclassSb.AppendLine("}");
                regclassSb.AppendLine();
            }

            regfunctioncode = regclassSb.ToString().Replace("\r", "").Replace("\n", "\r\n");
        }
        public string GetCode()
        {
            string funccode = Properties.Resources.DelegateImplicitFrom;

            funccode = funccode.Replace("[classname]", classname);

            string pushparas = string.Empty;


            funccode = funccode.Replace("[delegatetype]", GetTypeFullName(type));

            var method = CreatorBase.GetDelegateMethodInfo(type);

            var param = method.GetParameters();

            if (method.ReturnType == typeof(void))
            {
                funccode = funccode.Replace("[return]", string.Empty);
            }
            else
            {
                funccode = funccode.Replace("[return]", "return (" + GetTypeFullName(method.ReturnType) + ")");
            }

            string invokeparam = string.Empty;

            for (int i = 0; i < param.Length; i++)
            {
                invokeparam += "__wapperargs__" + i;                //param[i].Name;
                if (i < param.Length - 1)
                {
                    invokeparam += ",";
                }
            }

            funccode = funccode.Replace("[invokeparam]", invokeparam);


            return(funccode);
        }
        public void MakeCode(System.IO.Stream combiedcodestm, string as3apipath, string csharpcodepath, string chsharpcodenamespace,
                             string regfunctioncodenamespace, string AssemblyCSharpCodePath, out string regfunctioncode)
        {
            Dictionary <TypeKey, CreatorBase> creators = new Dictionary <TypeKey, CreatorBase>();

            foreach (var item in types)
            {
                var classtype = item;

                if (!CreatorBase.IsSkipType(classtype) && !CreatorBase.IsSkipCreator(classtype)

                    && (classtype.IsClass || classtype.IsValueType) && classtype.IsPublic
                    )
                {
                    CreatorBase.MakeCreator(classtype, creators, as3apipath, csharpcodepath, chsharpcodenamespace);
                }
            }

            foreach (var creator in creators.Values)
            {
                //如果涉及Assembly-CSharp中声明的类型则放到AssemblyPath中去

                if (AssemblyCSharpCodePath != null && CheckAssemblyShape(creator.BuildIngType, types))
                {
                    creator.SetNativeCodePath(AssemblyCSharpCodePath);
                }
            }


            StringBuilder regclassSb = new StringBuilder();

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(combiedcodestm, System.Text.Encoding.UTF8))
            {
                StringBuilder usingcode = new StringBuilder();
                CreatorBase.GenNativeFuncImport(usingcode);

                sw.WriteLine(usingcode.ToString());

                regclassSb.AppendLine("using System;");
                regclassSb.AppendLine("using System.Collections.Generic;");
                regclassSb.AppendLine("using System.Text;");
                regclassSb.AppendLine("using ASBinCode;");
                //regclassSb.AppendLine("using ASCTest.regNativeFunctions;");
                regclassSb.AppendLine("using ASRuntime.nativefuncs;");

                regclassSb.AppendLine("namespace " + regfunctioncodenamespace);
                regclassSb.AppendLine("{");

                regclassSb.AppendLine("\tpartial class extFunctions : INativeFunctionRegister");
                regclassSb.AppendLine("\t{");
                regclassSb.AppendLine("\t\tprivate System.Collections.IEnumerator regAutoCreateCodes(CSWC bin)");
                //regclassSb.AppendLine("\t\tprivate void regAutoCreateCodes(CSWC bin)");
                regclassSb.AppendLine("\t\t{");


                int   count = creators.Values.Count;
                float c     = 0;
                foreach (var item in creators.Values)
                {
                    Console.WriteLine("building:" + item.BuildIngType);
                    string code = item.Create().Replace("\r", "").Replace("\n", "\r\n");

                    regclassSb.AppendLine("\t\t\t" + item.linkcodenamespace + "." + CreatorBase.GetNativeFunctionClassName(item.BuildIngType) + ".regNativeFunctions(bin);");
                    sw.WriteLine(code);

                    c += 1;
                    regclassSb.AppendLine("\t\t\tprogress=" + (c / count).ToString() + "f;");
                    regclassSb.AppendLine("\t\t\tyield return null;");
                }

                regclassSb.AppendLine("\t\t}");
                regclassSb.AppendLine("\t}");
                regclassSb.AppendLine("}");
                regclassSb.AppendLine();
            }

            regfunctioncode = regclassSb.ToString().Replace("\r", "").Replace("\n", "\r\n");
        }
Beispiel #4
0
        protected string GetInvokeMethodString(string storeresult, ParameterInfo[] paras)
        {
            if (method.IsSpecialName && method.Name.StartsWith("op_"))
            {
                storeresult = storeresult.Substring(0, storeresult.LastIndexOf(GetTypeFullName(methodAtType)));

                string op = method.Name.Substring(3);

                switch (op)
                {
                case "Equality":
                    //sb.AppendLine(string.Format("{0} == {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 == arg1");
                    break;

                case "Inequality":
                    //sb.AppendLine(string.Format("{0} != {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 != arg1");
                    break;

                case "Addition":
                    //sb.AppendLine(string.Format("{0} + {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 + arg1");
                    break;

                case "Subtraction":
                    //sb.AppendLine(string.Format("{0} - {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 - arg1");
                    break;

                case "Multiply":
                    //sb.AppendLine(string.Format("{0} * {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 * arg1");
                    break;

                case "Division":
                    //sb.AppendLine(string.Format("{0} / {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 / arg1");
                    break;

                case "Modulus":
                    //sb.AppendLine(string.Format("{0} % {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 % arg1");
                    break;

                case "GreaterThan":
                    //sb.AppendLine(string.Format("{0} > {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 > arg1");
                    break;

                case "GreaterThanOrEqual":
                    //sb.AppendLine(string.Format("{0} >= {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 >= arg1");
                    break;

                case "LessThan":
                    //sb.AppendLine(string.Format("{0} < {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 < arg1");
                    break;

                case "LessThanOrEqual":
                    //sb.AppendLine(string.Format("{0} <= {1};", param[0].Name, param[1].Name));
                    storeresult += string.Format("arg0 <= arg1");
                    break;

                case "LeftShift":
                    storeresult += string.Format("arg0 << arg1");
                    break;

                case "RightShift":
                    storeresult += string.Format("arg0 >> arg1");
                    break;

                case "UnaryPlus":
                    storeresult += string.Format("+arg0");
                    break;

                case "UnaryNegation":
                    //sb.AppendLine(string.Format("-{0};", param[0].Name));
                    storeresult += string.Format("-arg0");
                    break;

                case "Increment":
                    storeresult += string.Format("++arg0");
                    break;

                case "Decrement":
                    storeresult += string.Format("--arg0");
                    break;

                case "OnesComplement":
                    storeresult += string.Format("~arg0");
                    break;

                case "ExclusiveOr":
                    storeresult += string.Format("arg0 ^ arg1");
                    break;

                case "BitwiseOr":
                    storeresult += string.Format("arg0 | arg1");
                    break;

                case "BitwiseAnd":
                    storeresult += string.Format("arg0 & arg1");
                    break;

                case "Implicit":
                case "Explicit":
                {
                    //string tmp, clsName;
                    //bool isByRef;
                    //method.ReturnType.GetClassName(out tmp, out clsName, out isByRef);
                    //sb.AppendLine(string.Format("({1}){0};", param[0].Name, clsName));

                    storeresult += string.Format("({0})arg0\n", GetTypeFullName(method.ReturnType));
                }
                break;

                default:
                    throw new NotImplementedException(op);
                }

                return(storeresult);
            }
            else if (method.IsSpecialName && method.Name.StartsWith("add_") && paras.Length == 1 && CreatorBase.IsDelegate(paras[0].ParameterType))
            {
                string eventname = method.Name.Substring(4);

                storeresult += string.Format(".{0} += arg0", eventname);
                return(storeresult);
            }
            else if (method.IsSpecialName && method.Name.StartsWith("remove_") && paras.Length == 1 && CreatorBase.IsDelegate(paras[0].ParameterType))
            {
                string eventname = method.Name.Substring(7);
                storeresult += string.Format(".{0} -= arg0", eventname);
                return(storeresult);
            }

            PropertyInfo pinfo;

            if (CheckIsGetter(method, methodAtType, out pinfo))
            {
                //***访问器Getter
                storeresult += "." + pinfo.Name + "\n";
                return(storeresult);
            }
            else if (CheckIsIndexerGetter(method, methodAtType, out pinfo))
            {
                //***索引器***



                storeresult += "[";                //+ string.Format("({0})arg{1}", GetTypeFullName(paras[0].ParameterType), 0) + "]\n";

                for (int i = 0; i < paras.Length; i++)
                {
                    storeresult += string.Format("({0})arg{1}", GetTypeFullName(paras[i].ParameterType), i);
                    if (i < paras.Length - 1)
                    {
                        storeresult += ",";
                    }
                }

                storeresult += "]\n";

                return(storeresult);
            }
            else if (CheckIsSetter(method, methodAtType, out pinfo))
            {
                //***访问器Setter
                storeresult += "." + pinfo.Name + " = " + string.Format("({0})arg{1}", GetTypeFullName(paras[0].ParameterType), 0) + "\n";
                return(storeresult);
            }
            else if (CheckIsIndexerSetter(method, methodAtType, out pinfo))
            {
                //***索引器***

                if (paras.Length > 2)
                {
                    storeresult += "[";
                    for (int i = 0; i < paras.Length - 1; i++)
                    {
                        storeresult += string.Format("({0})arg{1}", GetTypeFullName(paras[i].ParameterType), i);
                        if (i < paras.Length - 2)
                        {
                            storeresult += ",";
                        }
                    }
                    storeresult += "] = "
                                   + string.Format("({0})arg{1}", GetTypeFullName(paras[paras.Length - 1].ParameterType), paras.Length - 1) + "\n";
                }
                else
                {
                    storeresult += "[" + string.Format("({0})arg{1}", GetTypeFullName(paras[1].ParameterType), 1) + "] = "
                                   + string.Format("({0})arg{1}", GetTypeFullName(paras[0].ParameterType), 0) + "\n";
                    ;
                }
                return(storeresult);
            }



            storeresult += ".";
            storeresult += method.Name;
            storeresult += "(";

            for (int i = 0; i < paras.Length; i++)
            {
                if (paras[i].IsOut)
                {
                    storeresult += string.Format(CreatorBase.GetOutKeyWord(paras[i], method) + " arg{0}", i);
                }
                else if (paras[i].ParameterType.IsByRef && !paras[i].IsIn)
                {
                    storeresult += string.Format("ref arg{0}", i);
                }
                else
                {
                    storeresult += string.Format("({0})arg{1}", GetTypeFullName(paras[i].ParameterType), i);
                }
                if (i < paras.Length - 1)
                {
                    storeresult += ",";
                }
            }

            storeresult += ")\n";

            return(storeresult);
        }
Beispiel #5
0
        public string GetCode()
        {
            var paras = method.GetParameters();

            bool ismakedelegate = true;

            if (CreatorBase.IsDelegate(methodAtType))
            {
                ismakedelegate = false;
            }

            if (method is MethodInfo)
            {
                PropertyInfo propertyInfo;
                if (MethodNativeCodeCreator.CheckIsIndexerSetter((MethodInfo)method, method.DeclaringType, out propertyInfo) && paras.Length == 2)
                {
                    var temp = paras[0];
                    paras[0] = new myparainfo(paras[1], 0);
                    paras[1] = new myparainfo(temp, 1);


                    ismakedelegate = false;
                }

                if (MethodNativeCodeCreator.CheckIsIndexerGetter((MethodInfo)method, method.DeclaringType, out propertyInfo))
                {
                    ismakedelegate = false;
                }
                if (MethodNativeCodeCreator.CheckIsSetter((MethodInfo)method, method.DeclaringType, out propertyInfo))
                {
                    ismakedelegate = false;
                }
                if (MethodNativeCodeCreator.CheckIsGetter((MethodInfo)method, method.DeclaringType, out propertyInfo))
                {
                    ismakedelegate = false;
                }

                if (method.IsSpecialName)
                {
                    ismakedelegate = false;
                }

                if (method.IsGenericMethodDefinition)
                {
                    ismakedelegate = false;
                }
            }



            int  paracount = paras.Length;
            bool hasref    = false;

            foreach (var item in paras)
            {
                if (item.ParameterType.IsByRef)
                {
                    paracount += 1;
                    hasref     = true;
                    break;
                }
            }

            string funccode;

            if (ismakedelegate)
            {
                funccode = Properties.Resources.IMethodGetterMethodFunc;
            }
            else
            {
                funccode = Properties.Resources.MethodFunc;
            }



            funccode = funccode.Replace("[classname]", classname);
            funccode = funccode.Replace("[paracount]", paracount.ToString());

            string pushparas = string.Empty;

            for (int i = 0; i < paras.Length; i++)
            {
                var para = paras[i];

                pushparas = pushparas + "\t\t\t\t" + string.Format("para.Add({0});\n", GetAS3RuntimeTypeString(para.ParameterType));
            }

            if (hasref)
            {
                pushparas = pushparas + "\t\t\t\t" + string.Format("para.Add({0});\n", GetAS3RuntimeTypeString(typeof(ASRuntime.nativefuncs.linksystem.RefOutStore)));
            }


            funccode = funccode.Replace("[pushparas]", pushparas);
            funccode = funccode.Replace("[returntype]", GetAS3RuntimeTypeString(method.ReturnType));

            if (methodAtType.IsValueType)
            {
                string loadthis = Properties.Resources.LoadStructThis;
                loadthis = loadthis.Replace("[thisObjtype]", GetTypeFullName(methodAtType));
                funccode = funccode.Replace("[loadthis]", loadthis);
            }
            else
            {
                string loadthis = Properties.Resources.LoadThis;
                loadthis = loadthis.Replace("[thisObjtype]", GetTypeFullName(methodAtType));
                funccode = funccode.Replace("[loadthis]", loadthis);
            }


            string loadargs = string.Empty;

            for (int i = 0; i < paras.Length; i++)
            {
                var argement = paras[i];

                loadargs = loadargs + GetLoadArgementString(argement);
                loadargs = loadargs + "\n";
            }
            if (hasref)
            {
                loadargs = loadargs + GetLoadArgementString(typeof(ASRuntime.nativefuncs.linksystem.RefOutStore), paras.Length);
                loadargs = loadargs + "\n";
            }


            funccode = funccode.Replace("[loadargement]", loadargs);

            var rettype = GetAS3Runtimetype(method.ReturnType);

            if (rettype == ASBinCode.RunTimeDataType.fun_void)
            {
                string storeresult = string.Empty;
                //***调用方法****
                storeresult = "_this";
                storeresult = GetInvokeMethodString(storeresult, paras);

                storeresult += "\t\t\t\t\t;\n";
                storeresult  = storeresult + "\t\t\t\t\treturnSlot.directSet(ASBinCode.rtData.rtUndefined.undefined);";
                funccode     = funccode.Replace("[storeresult]", storeresult);
            }
            else if (rettype == ASBinCode.RunTimeDataType.rt_int)
            {
                string storeresult = string.Empty;
                //***调用方法****
                storeresult = "int _result_ = (int)(_this";
                storeresult = GetInvokeMethodString(storeresult, paras);

                storeresult += "\t\t\t\t\t)\n";
                storeresult += "\t\t\t\t\t;\n";
                storeresult += "\t\t\t\t\treturnSlot.setValue(_result_);\n";
                funccode     = funccode.Replace("[storeresult]", storeresult);
            }
            else if (rettype == ASBinCode.RunTimeDataType.rt_uint)
            {
                string storeresult = string.Empty;
                //***调用方法****
                storeresult = "uint _result_ = (uint)(_this";
                storeresult = GetInvokeMethodString(storeresult, paras);

                storeresult += "\t\t\t\t\t)\n";
                storeresult += "\t\t\t\t\t;\n";
                storeresult += "\t\t\t\t\treturnSlot.setValue(_result_);\n";
                funccode     = funccode.Replace("[storeresult]", storeresult);
            }
            else if (rettype == ASBinCode.RunTimeDataType.rt_number)
            {
                string storeresult = string.Empty;
                //***调用方法****
                storeresult = "double _result_ = (double)(_this";
                storeresult = GetInvokeMethodString(storeresult, paras);

                storeresult += "\t\t\t\t\t)\n";
                storeresult += "\t\t\t\t\t;\n";
                storeresult += "\t\t\t\t\treturnSlot.setValue(_result_);\n";
                funccode     = funccode.Replace("[storeresult]", storeresult);
            }
            else if (rettype == ASBinCode.RunTimeDataType.rt_string)
            {
                string storeresult = string.Empty;
                //***调用方法****
                storeresult = "string _result_ = (string)(_this";
                storeresult = GetInvokeMethodString(storeresult, paras);

                storeresult += "\t\t\t\t\t)\n";
                storeresult += "\t\t\t\t\t;\n";
                storeresult += "\t\t\t\t\treturnSlot.setValue(_result_);\n";
                funccode     = funccode.Replace("[storeresult]", storeresult);
            }
            else if (rettype == ASBinCode.RunTimeDataType.rt_boolean)
            {
                string storeresult = string.Empty;
                //***调用方法****
                storeresult = "bool _result_ = _this";
                storeresult = GetInvokeMethodString(storeresult, paras);

                storeresult += "\t\t\t\t\t;\n";
                storeresult += "\t\t\t\t\tif(_result_)\n";
                storeresult += "\t\t\t\t\t{\n";
                storeresult += "\t\t\t\t\t\treturnSlot.setValue(ASBinCode.rtData.rtBoolean.True);\n";
                storeresult += "\t\t\t\t\t}\n";
                storeresult += "\t\t\t\t\telse\n";
                storeresult += "\t\t\t\t\t{\n";
                storeresult += "\t\t\t\t\t\treturnSlot.setValue(ASBinCode.rtData.rtBoolean.False);\n";
                storeresult += "\t\t\t\t\t}\n";


                funccode = funccode.Replace("[storeresult]", storeresult);
            }
            else if (rettype > ASBinCode.RunTimeDataType.unknown)
            {
                if (method.ReturnType.IsValueType)
                {
                    string storeresult = string.Empty;
                    //***调用方法****
                    storeresult = GetTypeFullName(method.ReturnType) + " _result_ = _this";
                    storeresult = GetInvokeMethodString(storeresult, paras);

                    storeresult += "\t\t\t\t\t;\n";
                    //storeresult += "\t\t\t\t\tstackframe.player.linktypemapper.storeLinkObject_ToSlot(_result_, functionDefine.signature.returnType, returnSlot, bin, stackframe.player);";
                    storeresult += "\t\t\t\t\t((StackSlot)returnSlot).setLinkObjectValue(bin.getClassByRunTimeDataType(functionDefine.signature.returnType), stackframe.player, _result_);";

                    funccode = funccode.Replace("[storeresult]", storeresult);
                }
                else
                {
                    string storeresult = string.Empty;
                    //***调用方法****
                    storeresult = "object _result_ = _this";
                    storeresult = GetInvokeMethodString(storeresult, paras);

                    storeresult += "\t\t\t\t\t;\n";
                    storeresult += "\t\t\t\t\tstackframe.player.linktypemapper.storeLinkObject_ToSlot(_result_, functionDefine.signature.returnType, returnSlot, bin, stackframe.player);";
                    funccode     = funccode.Replace("[storeresult]", storeresult);
                }
            }
            else
            {
                funccode = funccode.Replace("[storeresult]", "代码生成错误,不能转换返回类型");
            }

            if (methodAtType.IsValueType)             //结构体,需要重新赋值回去
            {
                string replacethis = "((LinkObj<" + GetTypeFullName(methodAtType) + ">)((ASBinCode.rtData.rtObjectBase) thisObj).value).value = _this;";
                funccode = funccode.Replace("[replacethis]", replacethis);
            }
            else
            {
                funccode = funccode.Replace("[replacethis]", string.Empty);
            }


            if (!hasref)
            {
                funccode = funccode.Replace("[storeref]", string.Empty);
            }
            else
            {
                string storetemplate = @"
					if (arg[storeidx] != null)
					{
						arg[storeidx].SetValue(functionDefine.signature.parameters[[argidx]].name, arg[argidx]);
					}
";

                string toreplace = @"
					if (arg[storeidx] != null)
					{
						arg[storeidx].Clear();
					}
";;
                toreplace = toreplace.Replace("[storeidx]", paras.Length.ToString());


                for (int i = 0; i < paras.Length; i++)
                {
                    if (paras[i].ParameterType.IsByRef)
                    {
                        toreplace += storetemplate.Replace("[storeidx]", paras.Length.ToString()).Replace("[argidx]", i.ToString());
                    }
                }

                funccode = funccode.Replace("[storeref]", toreplace);
            }


            if (ismakedelegate)
            {
                //typeof(AutoGenCodeLib.Testobj).GetMethod("TestType");

                string types = "Type.EmptyTypes";

                if (paras.Length > 0)
                {
                    types = "new Type[] {";

                    for (int i = 0; i < paras.Length; i++)
                    {
                        types += "typeof(" + GetTypeFullName(paras[i].ParameterType) + ")";
                        if (i < paras.Length - 1)
                        {
                            types += ",";
                        }
                    }

                    types += "}";
                }


                string findmethod = string.Empty;
                findmethod += "typeof(" + GetTypeFullName(methodAtType) + ")";
                findmethod += ".GetMethod(\"" + method.Name + "\"," + types + ");";


                funccode = funccode.Replace("[createmethod]", findmethod);
            }



            return(funccode);
        }