Exemplo n.º 1
0
        /// <summary>
        /// 创建ViewModelScript
        /// </summary>
        /// <param name="classname"></param>
        /// <param name="scriptPath"></param>
        /// <param name="components"></param>
        public static void CreateNewViewModelScript(string className, string scriptPath, List <ComponentItem> components)
        {
            UICoder oldViewModel = new UICoder(className);
            var     tree         = oldViewModel.tree;

            #region MakeClassScript
            TypeDeclaration classNode = null;
            if (tree.Descendants.OfType <TypeDeclaration>().Where(x => x.Name == className).Count() == 0)
            {
                classNode            = new TypeDeclaration();
                classNode.Name       = className;
                classNode.Modifiers |= Modifiers.Public;
                classNode.Modifiers |= Modifiers.Abstract;

                var comment = new Comment("<summary>", CommentType.Documentation);
                tree.AddChild(comment, Roles.Comment);
                comment = new Comment("[代码说明信息]", CommentType.Documentation);
                tree.AddChild(comment, Roles.Comment);
                comment = new Comment("<summary>", CommentType.Documentation);
                tree.AddChild(comment, Roles.Comment);
                tree.AddChild(classNode, Roles.TypeMemberRole);

                classNode = tree.Descendants.OfType <TypeDeclaration>().Where(x => x.Name == className).First();
                var basePanels = LoadAllBasePanels();
                var bs         = classNode.BaseTypes.Where(x => Array.Find(basePanels, y => y.Contains(x.ToString())) != null).FirstOrDefault();
                if (bs != null)
                {
                    classNode.BaseTypes.Remove(bs);
                }
                classNode.BaseTypes.Add(new SimpleType(typeof(Binding.ViewModelObject).FullName));
            }
            #endregion
            var scriptValue = GenerateViewModelScript(className, oldViewModel.Compile(), typeof(Binding.ViewModelObject), components);
            System.IO.File.WriteAllText(scriptPath, scriptValue);
        }
Exemplo n.º 2
0
 private static void LoadOldScript(GameObject prefab, Action <UICoder> onGet)
 {
     ChoiseAnUserMonobehiver(prefab, (x) =>
     {
         if (x != null)
         {
             UICoder coder = new UICoder(x.GetType().Name);
             var path      = AssetDatabase.GetAssetPath(MonoScript.FromMonoBehaviour(x));
             coder.Load(System.IO.File.ReadAllText(path, System.Text.Encoding.UTF8));
             onGet(coder);
         }
         else
         {
             UICoder coder = new UICoder(prefab.name);
             onGet(coder);
         }
     });
 }
Exemplo n.º 3
0
        private static string GenerateViewModelScript(string className, string oldScript, Type baseType, List <ComponentItem> components)
        {
            UICoder oldViewModel = new UICoder(className);

            oldViewModel.Load(oldScript);
            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)
            {
                var flag = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy;

                foreach (var viewItem in component.viewItems)
                {
                    //忽略已经存在于父级的属性
                    if (baseType.GetProperty(viewItem.bindingSource, flag) != null)
                    {
                        continue;
                    }

                    //Debug.Log(baseType);

                    CompleteKeyField(viewItem.bindingSource, classNode);

                    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, flag) != null)
                    {
                        continue;
                    }

                    CompleteKeyField(eventItem.bindingSource, classNode);

                    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();
            return(scriptValue);
        }
Exemplo n.º 4
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);
        }