示例#1
0
 public TypeBindingInfo(BindingManager bindingManager, Type type, TypeTransform typeTransform)
 {
     this.bindingManager = bindingManager;
     this.type           = type;
     this.transform      = typeTransform;
     this._omit          = type.IsDefined(typeof(JSOmitAttribute));
 }
示例#2
0
        private static TypeTransform HackGetComponents(TypeTransform typeTransform)
        {
            if (typeTransform.type == typeof(GameObject))
            {
                typeTransform.AddTSMethodDeclaration($"AddComponent<T extends Component>(type: {{ new(): T }}): T",
                                                     "AddComponent", typeof(Type));

                typeTransform.WriteCSMethodOverrideBinding("AddComponent", GameObjectFix.Bind_AddComponent);

                typeTransform.WriteCSMethodOverrideBinding("GetComponent", GameObjectFix.Bind_GetComponent);
                typeTransform.WriteCSMethodOverrideBinding("GetComponentInChildren", GameObjectFix.Bind_GetComponentInChildren);
                typeTransform.WriteCSMethodOverrideBinding("GetComponentInParent", GameObjectFix.Bind_GetComponentInParent);
                typeTransform.WriteCSMethodOverrideBinding("GetComponentsInChildren", GameObjectFix.Bind_GetComponentsInChildren);
                typeTransform.WriteCSMethodOverrideBinding("GetComponentsInParent", GameObjectFix.Bind_GetComponentsInParent);
                typeTransform.WriteCSMethodOverrideBinding("GetComponents", GameObjectFix.Bind_GetComponents);
            }
            else
            {
                typeTransform.WriteCSMethodOverrideBinding("GetComponent", ComponentFix.Bind_GetComponent);
                typeTransform.WriteCSMethodOverrideBinding("GetComponentInChildren", ComponentFix.Bind_GetComponentInChildren);
                typeTransform.WriteCSMethodOverrideBinding("GetComponentInParent", ComponentFix.Bind_GetComponentInParent);
                typeTransform.WriteCSMethodOverrideBinding("GetComponentsInChildren", ComponentFix.Bind_GetComponentsInChildren);
                typeTransform.WriteCSMethodOverrideBinding("GetComponentsInParent", ComponentFix.Bind_GetComponentsInParent);
                typeTransform.WriteCSMethodOverrideBinding("GetComponents", ComponentFix.Bind_GetComponents);
            }

            typeTransform.AddTSMethodDeclaration($"GetComponent<T extends Component>(type: {{ new(): T }}): T",
                                                 "GetComponent", typeof(Type))
            .AddTSMethodDeclaration($"GetComponentInChildren<T extends Component>(type: {{ new(): T }}, includeInactive: boolean): T",
                                    "GetComponentInChildren", typeof(Type), typeof(bool))
            .AddTSMethodDeclaration($"GetComponentInChildren<T extends Component>(type: {{ new(): T }}): T",
                                    "GetComponentInChildren", typeof(Type))
            .AddTSMethodDeclaration($"GetComponentInParent<T extends Component>(type: {{ new(): T }}): T",
                                    "GetComponentInParent", typeof(Type))
            .AddTSMethodDeclaration($"GetComponents<T extends Component>(type: {{ new(): T }}): T[]",
                                    "GetComponents", typeof(Type))
            .AddTSMethodDeclaration($"GetComponentsInChildren<T extends Component>(type: {{ new(): T }}, includeInactive: boolean): T[]",
                                    "GetComponentsInChildren", typeof(Type), typeof(bool))
            .AddTSMethodDeclaration($"GetComponentsInChildren<T extends Component>(type: {{ new(): T }}): T[]",
                                    "GetComponentsInChildren", typeof(Type))
            .AddTSMethodDeclaration($"GetComponentsInParent<T extends Component>(type: {{ new(): T }}, includeInactive: boolean): T[]",
                                    "GetComponentsInParent", typeof(Type), typeof(bool))
            .AddTSMethodDeclaration($"GetComponentsInParent<T extends Component>(type: {{ new(): T }}): T[]",
                                    "GetComponentsInParent", typeof(Type))
            ;
            return(typeTransform);
        }
示例#3
0
        public TSTypeNaming(BindingManager bindingManager, Type type, TypeTransform typeTransform)
        {
            this.type = type;

            var naming          = typeTransform?.GetTypeNaming() ?? type.Name;
            var indexOfTypeName = naming.LastIndexOf('.');

            if (indexOfTypeName >= 0)
            {
                // 指定的命名中已经携带了"."
                var indexOfInnerTypeName = naming.IndexOf('+');
                if (indexOfInnerTypeName >= 0)
                {
                    this.jsModule = naming.Substring(0, indexOfInnerTypeName);
                    var rightName = naming.Substring(indexOfInnerTypeName + 1);
                    var lastIndexOfInnerTypeName = rightName.LastIndexOf('+');
                    if (lastIndexOfInnerTypeName >= 0)
                    {
                        this.jsNamespace = rightName.Substring(0, lastIndexOfInnerTypeName);
                        this.jsName      = rightName.Substring(lastIndexOfInnerTypeName + 1);
                    }
                    else
                    {
                        this.jsNamespace = "";
                        this.jsName      = rightName;
                    }
                }
                else
                {
                    this.jsModule    = naming.Substring(0, indexOfTypeName);
                    this.jsNamespace = "";
                    this.jsName      = naming.Substring(indexOfTypeName + 1);
                }

                var gArgIndex = this.jsName.IndexOf("<");
                if (gArgIndex < 0)
                {
                    this.jsPureName = this.jsName;
                }
                else
                {
                    this.jsPureName = this.jsName.Substring(0, gArgIndex);
                }
            }
            else
            {
                this.jsModule    = type.Namespace ?? "";
                this.jsNamespace = "";

                // 处理内部类层级
                var declaringType = type.DeclaringType;
                while (declaringType != null)
                {
                    this.jsNamespace = string.IsNullOrEmpty(this.jsNamespace) ? declaringType.Name : $"{declaringType.Name}.{this.jsNamespace}";
                    declaringType    = declaringType.DeclaringType;
                }

                if (type.IsGenericType)
                {
                    this.jsName     = naming.Contains("`") ? naming.Substring(0, naming.IndexOf('`')) : naming;
                    this.jsPureName = this.jsName;

                    if (type.IsGenericTypeDefinition)
                    {
                        if (!naming.Contains("<"))
                        {
                            this.jsName += "<";
                            var gArgs = type.GetGenericArguments();

                            for (var i = 0; i < gArgs.Length; i++)
                            {
                                this.jsName += gArgs[i].Name;
                                if (i != gArgs.Length - 1)
                                {
                                    this.jsName += ", ";
                                }
                            }
                            this.jsName += ">";
                        }
                    }
                    else
                    {
                        foreach (var gp in type.GetGenericArguments())
                        {
                            this.jsName += "_" + gp.Name;
                        }
                    }
                }
                else
                {
                    this.jsName = naming;

                    //TODO: 整理 jsPureName 的取值流程 (对于泛型中的嵌套的处理等)
                    var gArgIndex = this.jsName.IndexOf("<");
                    if (gArgIndex < 0)
                    {
                        this.jsPureName = this.jsName;
                    }
                    else
                    {
                        this.jsPureName = this.jsName.Substring(0, gArgIndex);
                    }
                }
            }

            if (string.IsNullOrEmpty(this.jsNamespace))
            {
                this.jsModuleAccess       = this.jsName;
                this.jsModuleImportAccess = this.jsPureName;
                this.jsLocalName          = "";
            }
            else
            {
                var i = this.jsNamespace.IndexOf('.');
                this.jsModuleAccess       = i < 0 ? this.jsNamespace : this.jsNamespace.Substring(0, i);
                this.jsModuleImportAccess = this.jsModuleAccess;
                this.jsLocalName          = CodeGenUtils.Concat(".", i < 0 ? "" : this.jsNamespace.Substring(i + 1), this.jsName);
            }

            if (this.jsModuleAccess.EndsWith("[]"))
            {
                this.jsModuleAccess = this.jsModuleAccess.Substring(0, this.jsModuleAccess.Length - 2);
            }

            this.jsDepth    = this.jsModuleAccess.Split('.').Length;
            this.jsFullName = CodeGenUtils.Concat(".", jsModule, jsNamespace, jsName);
        }
示例#4
0
        private static TypeTransform HackGetComponents(TypeTransform typeTransform)
        {
            var moduleNameWithDot = "";

            // var moduleNameWithDot = "UnityEngine.";

            if (typeTransform.type == typeof(GameObject))
            {
                typeTransform.AddTSMethodDeclaration($"AddComponent<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}): T",
                                                     "AddComponent", typeof(Type));
            }

            typeTransform.AddTSMethodDeclaration($"GetComponent<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}): T",
                                                 "GetComponent", typeof(Type))
            .AddTSMethodDeclaration($"GetComponentInChildren<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}, includeInactive: boolean): T",
                                    "GetComponentInChildren", typeof(Type), typeof(bool))
            .AddTSMethodDeclaration($"GetComponentInChildren<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}): T",
                                    "GetComponentInChildren", typeof(Type))
            .AddTSMethodDeclaration($"GetComponentInParent<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}): T",
                                    "GetComponentInParent", typeof(Type))
            // .AddTSMethodDeclaration($"GetComponents<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}, results: any): void",
            //     "GetComponents", typeof(Type))
            .AddTSMethodDeclaration($"GetComponents<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}): T[]",
                                    "GetComponents", typeof(Type))
            .SetMethodReturnPusher("js_push_classvalue_array", "GetComponents", typeof(Type))
            .AddTSMethodDeclaration($"GetComponentsInChildren<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}, includeInactive: boolean): T[]",
                                    "GetComponentsInChildren", typeof(Type), typeof(bool))
            .SetMethodReturnPusher("js_push_classvalue_array", "GetComponentsInChildren", typeof(Type), typeof(bool))
            .AddTSMethodDeclaration($"GetComponentsInChildren<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}): T[]",
                                    "GetComponentsInChildren", typeof(Type))
            .SetMethodReturnPusher("js_push_classvalue_array", "GetComponentsInChildren", typeof(Type))
            .AddTSMethodDeclaration($"GetComponentsInParent<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}, includeInactive: boolean): T[]",
                                    "GetComponentsInParent", typeof(Type), typeof(bool))
            .SetMethodReturnPusher("js_push_classvalue_array", "GetComponentsInParent", typeof(Type), typeof(bool))
            .AddTSMethodDeclaration($"GetComponentsInParent<T extends {moduleNameWithDot}Component>(type: {{ new(): T }}): T[]",
                                    "GetComponentsInParent", typeof(Type))
            .SetMethodReturnPusher("js_push_classvalue_array", "GetComponentsInParent", typeof(Type))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_component(ctx, argv[0], self, arg0);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponent", typeof(Type))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_component_in_children(ctx, argv[0], self, arg0, false);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponentInChildren", typeof(Type))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_component_in_children(ctx, argv[0], self, arg0, arg1);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponentInChildren", typeof(Type), typeof(bool))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_component_in_parent(ctx, argv[0], self, arg0, false);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponentInParent", typeof(Type))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_component_in_parent(ctx, argv[0], self, arg0, arg1);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponentInParent", typeof(Type), typeof(bool))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_components_in_children(ctx, argv[0], self, arg0, false);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponentsInChildren", typeof(Type))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_components_in_children(ctx, argv[0], self, arg0, arg1);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponentsInChildren", typeof(Type), typeof(bool))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_components_in_parent(ctx, argv[0], self, arg0, false);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponentsInParent", typeof(Type))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_components_in_parent(ctx, argv[0], self, arg0, arg1);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponentsInParent", typeof(Type), typeof(bool))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_components(ctx, argv[0], self, arg0);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponents", typeof(Type))
            .WriteCSMethodBinding((bindPoint, cg, info) =>
            {
                if (bindPoint == BindingPoints.METHOD_BINDING_BEFORE_INVOKE)
                {
                    cg.cs.AppendLine("var inject = _js_game_object_get_components(ctx, argv[0], self, arg0, arg1);");
                    cg.cs.AppendLine("if (!inject.IsUndefined())");
                    using (cg.cs.CodeBlockScope())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponents", typeof(Type), typeof(List <Component>));
            return(typeTransform);
        }
示例#5
0
        public TypeBindingInfo(BindingManager bindingManager, Type type, TypeTransform typeTransform)
        {
            this.bindingManager = bindingManager;
            this.type           = type;
            this.transform      = typeTransform;
            var naming          = this.transform.GetTypeNaming() ?? GetNamingAttribute(type);
            var indexOfTypeName = naming.LastIndexOf('.');

            if (indexOfTypeName >= 0) // 内部类
            {
                this.jsNamespace = naming.Substring(0, indexOfTypeName);
                this.jsName      = naming.Substring(indexOfTypeName + 1);
                this.jsTypeName  = this.jsName;
            }
            else
            {
                if (type.DeclaringType != null)
                {
                    if (string.IsNullOrEmpty(type.Namespace))
                    {
                        this.jsNamespace = type.DeclaringType.Name;
                    }
                    else
                    {
                        this.jsNamespace = $"{type.Namespace}.{type.DeclaringType.Name}";
                    }
                }
                else
                {
                    this.jsNamespace = type.Namespace ?? "";
                }

                do
                {
                    if (type.IsGenericType)
                    {
                        if (type.IsGenericTypeDefinition)
                        {
                            this.jsName     = naming.Contains("`") ? naming.Substring(0, naming.IndexOf('`')) : naming;
                            this.jsTypeName = this.jsName;

                            if (!naming.Contains("<"))
                            {
                                this.jsName += "<";
                                var gArgs = type.GetGenericArguments();

                                for (var i = 0; i < gArgs.Length; i++)
                                {
                                    this.jsName += gArgs[i].Name;
                                    if (i != gArgs.Length - 1)
                                    {
                                        this.jsName += ", ";
                                    }
                                }
                                this.jsName += ">";
                            }
                            break;
                        }
                        else
                        {
                            this.jsName     = naming.Contains("`") ? naming.Substring(0, naming.IndexOf('`')) : naming;
                            this.jsTypeName = this.jsName;

                            foreach (var gp in type.GetGenericArguments())
                            {
                                this.jsName += "_" + gp.Name;
                            }
                            break;
                        }
                    }

                    this.jsName     = naming;
                    this.jsTypeName = this.jsName;
                } while (false);
            }

            this.csBindingName = bindingManager.prefs.typeBindingPrefix + (this.jsNamespace + "_" + this.jsName).Replace('.', '_').Replace('+', '_').Replace('<', '_').Replace('>', '_');
            this.constructors  = new ConstructorBindingInfo(type);
        }