예제 #1
0
 private Parser(Schema schema, XmlReader reader, Type elementBaseType)
 {
     this.schema          = schema;
     this.reader          = reader;
     this.elementBaseType = elementBaseType;
     this.document        = ParseRoot();
 }
예제 #2
0
        public static GameObject Spawn(Data.Document data, GameObject go = null)
        {
            if (!go)
            {
                go = new GameObject();
            }

            var document = CreateElement(go, data.root, go.scene, data.schema);

            document.ShowSelf = false;

            return(go);
        }
예제 #3
0
        public static bool Generate(Data.Document document, string assetPath)
        {
            var classFullName = document.root.className;

            var idx       = classFullName.LastIndexOf('.');
            var classNs   = idx != -1 ? classFullName.Substring(0, idx) : null;
            var className = idx != 1 ? classFullName.Substring(idx + 1) : classFullName;

            var fileName = Path.GetFileNameWithoutExtension(assetPath);

            if (className != fileName)
            {
                // TODO: if x:Class specified, class name must be same as file
            }

            return(Generate(document, assetPath, classNs, className));
        }
예제 #4
0
        private static bool Generate(Data.Document document, string assetPath, string classNs, string className)
        {
            if (!document.schema.TryGetElement(document.root.name, out var rootElement) || string.IsNullOrEmpty(rootElement.className))
            {
                throw new Exception("Invalid root element");
            }

            var rootElementClassName = rootElement.className;
            var assetDir             = Path.GetDirectoryName(assetPath);
            var assetFilename        = Path.GetFileNameWithoutExtension(assetPath);
            var assetExt             = Path.GetExtension(assetPath);

            if (className != assetFilename)
            {
                throw new Exception($"Classname must match filename ({className} != {assetFilename})");
            }

            if (string.IsNullOrWhiteSpace(document.schema.namespaces))
            {
                throw new Exception("Schema namespace can't be null");
            }

            var userPath    = GetPath(assetDir, assetFilename, ".cs");
            var hasUserPath = File.Exists(userPath);

            if (!hasUserPath)
            {
                WriteFile(userPath, u =>
                {
                    u.AppendLine($"using UnityEngine;");
                    u.AppendLine($"using {document.schema.namespaces};");
                    u.AppendLine();
                    using (u.ScopeLine($"namespace {classNs}", classNs != null))
                    {
                        using (u.ScopeLine($"public partial class {className} : {rootElementClassName}"))
                        {
                            using (u.ScopeLine($"protected override void Awake()"))
                            {
                                u.AppendLine($"base.Awake();");
                                u.AppendLine($"InitializeComponent();");
                            }
                        }
                    }
                });
            }

            var genPath = GetPath(assetDir, assetFilename, ".g.cs");

            WriteFile(genPath, u =>
            {
                u.AppendLine($"using UnityEngine;");
                u.AppendLine($"using {document.schema.namespaces};");
                u.AppendLine();
                using (u.ScopeLine($"namespace {classNs}", classNs != null))
                {
                    using (u.ScopeLine($"public partial class {className} : {rootElementClassName}"))
                    {
                        u.AppendLine($"private bool _contentLoaded;");
                        u.AppendLine();
                        using (u.ScopeLine($"public void InitializeComponent()"))
                        {
                            using (u.ScopeLine("if (_contentLoaded)"))
                            {
                                u.AppendLine("return;");
                            }
                            u.AppendLine("_contentLoaded = true;");
                            u.AppendLine();
                            u.AppendLine("Uaml.Core.Application.LoadComponent(this);");
                            u.AppendLine($"Debug.Log(\"Loaded component {className}\");");
                        }
                    }
                }
            });

            return(hasUserPath);
        }