Exemple #1
0
        /// <summary>
        /// 更新viewModelScript
        /// </summary>
        /// <param name="script"></param>
        /// <param name="components"></param>
        public static void UpdateViewModelScript(Binding.ViewModel viewModel, List <ComponentItem> components)
        {
            var     monoScript   = MonoScript.FromScriptableObject(viewModel);
            var     className    = monoScript.GetClass().Name;
            UICoder oldViewModel = new UICoder(className);

            oldViewModel.Load(monoScript.text);
            var     tree      = oldViewModel.tree;
            var     classNode = tree.Descendants.OfType <TypeDeclaration>().Where(x => x.Name == className).First();
            AstNode lastNode  = classNode.Descendants.OfType <PreProcessorDirective>().FirstOrDefault();

            if (lastNode == null)
            {
                lastNode = classNode.Descendants.OfType <PropertyDeclaration>().FirstOrDefault();
            }

            foreach (var component in components)
            {
                foreach (var viewItem in component.viewItems)
                {
                    if (viewItem.bindingTargetType.type != null)
                    {
                        lastNode = InsertPropertyToClassNode(viewItem.bindingTargetType.type, viewItem.bindingSource, lastNode, classNode);
                    }
                    else
                    {
                        Debug.LogError(viewItem.bindingSource + ":type NULL");
                    }
                }

                foreach (var eventItem in component.eventItems)
                {
                    if (eventItem.type == BindingType.NoBinding)
                    {
                        continue;
                    }
                    var type = eventItem.bindingTargetType.type;
                    if (type != null)
                    {
                        Type typevalue = null;

                        if (eventItem.type == BindingType.WithTarget)
                        {
                            typevalue = typeof(Binding.PanelAction <>).MakeGenericType(component.componentType);
                        }
                        else
                        {
                            if (type.BaseType.IsGenericType)
                            {
                                var argument = type.BaseType.GetGenericArguments();
                                if (argument.Length == 1)
                                {
                                    typevalue = typeof(Binding.PanelAction <>).MakeGenericType(argument);
                                }
                                else if (argument.Length == 2)
                                {
                                    typevalue = typeof(Binding.PanelAction <,>).MakeGenericType(argument);
                                }
                                else if (argument.Length == 3)
                                {
                                    typevalue = typeof(Binding.PanelAction <, ,>).MakeGenericType(argument);
                                }
                                else if (argument.Length == 4)
                                {
                                    typevalue = typeof(Binding.PanelAction <, , ,>).MakeGenericType(argument);
                                }
                            }
                            else
                            {
                                typevalue = typeof(Binding.PanelAction);
                            }
                        }


                        lastNode = InsertPropertyToClassNode(typevalue, eventItem.bindingSource, lastNode, classNode);
                    }
                    else
                    {
                        Debug.LogError(eventItem.bindingSource + ":type NULL");
                    }
                }
            }
            var scriptValue = oldViewModel.Compile();
            var scriptPath  = AssetDatabase.GetAssetPath(monoScript);

            System.IO.File.WriteAllText(scriptPath, scriptValue);
        }
        /// <summary>
        /// 更新viewModelScript
        /// </summary>
        /// <param name="viewModel"></param>
        /// <param name="components"></param>
        public static void UpdateViewModelScript(Binding.ViewModel viewModel, List <ComponentItem> components)
        {
            var     monoScript   = MonoScript.FromScriptableObject(viewModel);
            var     classType    = monoScript.GetClass();
            var     baseType     = classType.BaseType;
            var     className    = classType.Name;
            UICoder oldViewModel = new UICoder(className);

            oldViewModel.Load(monoScript.text);
            var tree      = oldViewModel.tree;
            var classNode = tree.Descendants.OfType <TypeDeclaration>().Where(x => x.Name == className).First();

            PreProcessorDirective startRegion = classNode.Descendants.OfType <PreProcessorDirective>().Where(x => x.Type == PreProcessorDirectiveType.Region && x.Argument == "属性列表").FirstOrDefault();

            if (startRegion == null)
            {
                startRegion = new PreProcessorDirective(PreProcessorDirectiveType.Region, "属性列表");
                AstNode firstMember = classNode.GetChildByRole(Roles.TypeMemberRole);
                if (firstMember == null)
                {
                    firstMember = classNode.LastChild;
                }
                classNode.InsertChildBefore <PreProcessorDirective>(firstMember, startRegion, Roles.PreProcessorDirective);
            }

            PreProcessorDirective endRegion = classNode.Descendants.OfType <PreProcessorDirective>().Where(x => x.Type == PreProcessorDirectiveType.Endregion && x.Argument == "属性列表").FirstOrDefault();

            if (endRegion == null)
            {
                endRegion = new PreProcessorDirective(PreProcessorDirectiveType.Endregion, "属性列表");
                classNode.InsertChildAfter <PreProcessorDirective>(startRegion, endRegion, Roles.PreProcessorDirective);
            }

            #region 处理属性列表
            foreach (var component in components)
            {
                foreach (var viewItem in component.viewItems)
                {
                    //忽略已经存在于父级的属性
                    if (baseType.GetProperty(viewItem.bindingSource, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy) != null)
                    {
                        continue;
                    }

                    if (viewItem.bindingTargetType.type != null)
                    {
                        InsertPropertyToClassNode(viewItem.bindingTargetType.type, viewItem.bindingSource, endRegion, classNode);
                    }
                    else
                    {
                        Debug.LogError(viewItem.bindingSource + ":type NULL");
                    }
                }

                foreach (var eventItem in component.eventItems)
                {
                    //忽略已经存在于父级的属性
                    if (baseType.GetProperty(eventItem.bindingSource, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy) != null)
                    {
                        continue;
                    }

                    if (eventItem.type == BindingType.NoBinding)
                    {
                        continue;
                    }
                    var type = eventItem.bindingTargetType.type;

                    if (type != null)
                    {
                        Type typevalue = null;

                        if (eventItem.type == BindingType.WithTarget)
                        {
                            typevalue = typeof(Binding.PanelAction <>).MakeGenericType(component.componentType);
                        }
                        else
                        {
                            if (type.BaseType.IsGenericType)
                            {
                                var argument = type.BaseType.GetGenericArguments();
                                if (argument.Length == 1)
                                {
                                    typevalue = typeof(Binding.PanelAction <>).MakeGenericType(argument);
                                }
                                else if (argument.Length == 2)
                                {
                                    typevalue = typeof(Binding.PanelAction <,>).MakeGenericType(argument);
                                }
                                else if (argument.Length == 3)
                                {
                                    typevalue = typeof(Binding.PanelAction <, ,>).MakeGenericType(argument);
                                }
                                else if (argument.Length == 4)
                                {
                                    typevalue = typeof(Binding.PanelAction <, , ,>).MakeGenericType(argument);
                                }
                            }
                            else
                            {
                                typevalue = typeof(Binding.PanelAction);
                            }
                        }


                        InsertPropertyToClassNode(typevalue, eventItem.bindingSource, endRegion, classNode);
                    }
                    else
                    {
                        Debug.LogError(eventItem.bindingSource + ":type NULL");
                    }
                }
            }
            #endregion
            var scriptValue = oldViewModel.Compile();
            var scriptPath  = AssetDatabase.GetAssetPath(monoScript);
            System.IO.File.WriteAllText(scriptPath, scriptValue);
        }