Example #1
0
        public TypeBindingInfo(BindingManager bindingManager, Type type, TypeBindingFlags bindingFlags)
        {
            this.bindingManager = bindingManager;
            this.type           = type;
            this.bindingFlags   = bindingFlags;
            this.transform      = bindingManager.GetTypeTransform(type);
            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);
            }
            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 ?? "";
                }

                if (type.IsGenericType)
                {
                    this.jsName = naming.Substring(0, naming.IndexOf('`'));
                    foreach (var gp in type.GetGenericArguments())
                    {
                        this.jsName += "_" + gp.Name;
                    }
                }
                else
                {
                    this.jsName = naming;
                }
            }

            this.name         = bindingManager.prefs.typeBindingPrefix + (this.jsNamespace + "_" + this.jsName).Replace('.', '_').Replace('+', '_').Replace('<', '_').Replace('>', '_');
            this.constructors = new ConstructorBindingInfo(type);
        }
        private static TypeTransform HackGetComponents(TypeTransform typeTransform)
        {
            typeTransform.AddTSMethodDeclaration("GetComponent<T extends UnityEngine.Component>(type: { new(): T }): T",
                                                 "GetComponent", typeof(Type))
            .AddTSMethodDeclaration("GetComponentInChildren<T extends UnityEngine.Component>(type: { new(): T }, includeInactive: boolean): T",
                                    "GetComponentInChildren", typeof(Type), typeof(bool))
            .AddTSMethodDeclaration("GetComponentInChildren<T extends UnityEngine.Component>(type: { new(): T }): T",
                                    "GetComponentInChildren", typeof(Type))
            .AddTSMethodDeclaration("GetComponentInParent<T extends UnityEngine.Component>(type: { new(): T }): T",
                                    "GetComponentInParent", typeof(Type))
            // .AddTSMethodDeclaration("GetComponents<T extends UnityEngine.Component>(type: { new(): T }, results: any): void",
            //     "GetComponents", typeof(Type))
            .AddTSMethodDeclaration("GetComponents<T extends UnityEngine.Component>(type: { new(): T }): T[]",
                                    "GetComponents", typeof(Type))
            .SetMethodReturnPusher("js_push_classvalue_array", "GetComponents", typeof(Type))
            .AddTSMethodDeclaration("GetComponentsInChildren<T extends UnityEngine.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 UnityEngine.Component>(type: { new(): T }): T[]",
                                    "GetComponentsInChildren", typeof(Type))
            .SetMethodReturnPusher("js_push_classvalue_array", "GetComponentsInChildren", typeof(Type))
            .AddTSMethodDeclaration("GetComponentsInParent<T extends UnityEngine.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 UnityEngine.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.Block())
                    {
                        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.Block())
                    {
                        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.Block())
                    {
                        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.Block())
                    {
                        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.Block())
                    {
                        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.Block())
                    {
                        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.Block())
                    {
                        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.Block())
                    {
                        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.Block())
                    {
                        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.Block())
                    {
                        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.Block())
                    {
                        cg.cs.AppendLine("return inject;");
                    }

                    return(true);
                }
                return(false);
            }, "GetComponents", typeof(Type), typeof(List <Component>));
            return(typeTransform);
        }