Exemple #1
0
        static void AddDependency(string assetPath, XElement templateNode, List <string> dependencies)
        {
            bool   hasSrc = false;
            string src    = null;

            foreach (var xAttribute in templateNode.Attributes())
            {
                switch (xAttribute.Name.LocalName)
                {
                case k_GenericSrcAttr:
                    hasSrc = true;
                    src    = xAttribute.Value;
                    break;
                }
            }

            if (hasSrc)
            {
                string errorMessage, projectRelativePath;

                URIValidationResult result = URIHelpers.ValidAssetURL(assetPath, src, out errorMessage, out projectRelativePath);

                if (result == URIValidationResult.OK)
                {
                    dependencies.Add(projectRelativePath);
                }
            }
        }
        internal void CreateNewTemplatesFiles()
        {
            if (IsInputValid())
            {
                m_Root.SetEnabled(false);

                if (!Directory.Exists(m_Folder))
                {
                    Directory.CreateDirectory(m_Folder);
                }

                StyleSheet      styleSheet      = null;
                VisualTreeAsset visualTreeAsset = null;

                if (m_IsUssEnable)
                {
                    File.WriteAllText(ussPath, GetUssTemplateContent());
                    AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);

                    styleSheet = AssetDatabase.LoadAssetAtPath <StyleSheet>(ussPath);
                }

                if (m_IsUxmlEnable)
                {
                    var stringBuilder = new StringBuilder();

                    if (m_IsUssEnable)
                    {
                        var assetUri   = URIHelpers.MakeAssetUri(styleSheet);
                        var encodedUri = URIHelpers.EncodeUri(assetUri);
                        stringBuilder.AppendLine(string.Format(@"<Style src=""{0}"" />", encodedUri));
                        stringBuilder.Append('\t');
                    }

                    stringBuilder.AppendLine(@"<engine:Label text=""Hello World! From UXML"" />");

                    if (m_IsUssEnable)
                    {
                        stringBuilder.Append('\t');
                        stringBuilder.AppendLine(@"<engine:Label class=""custom-label"" text=""Hello World! With Style"" />");
                    }

                    File.WriteAllText(uxmlPath, UIElementsTemplate.CreateUXMLTemplate(m_Folder, stringBuilder.ToString()));
                    AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
                    visualTreeAsset = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(uxmlPath);
                }

                File.WriteAllText(cSharpPath, UIElementsTemplate.CreateCSharpTemplate(m_CSharpName, m_IsUxmlEnable, m_IsUssEnable && !m_IsUxmlEnable));
                AssetDatabase.Refresh();
            }
            else
            {
                ShowErrorMessage();
            }
        }
Exemple #3
0
        static void AddDependency(string assetPath, XElement templateNode, List <string> dependencies)
        {
            bool   hasSrc = false;
            string src    = null;

            foreach (var xAttribute in templateNode.Attributes())
            {
                switch (xAttribute.Name.LocalName)
                {
                case k_GenericSrcAttr:
                    hasSrc = true;
                    src    = xAttribute.Value;
                    break;
                }
            }

            if (!hasSrc)
            {
                return;
            }

            URIValidationResult result = URIHelpers.ValidAssetURL(assetPath, src, out string errorMessage, out string projectRelativePath);

            if (result != URIValidationResult.OK)
            {
                return;
            }

            switch (templateNode.Name.LocalName)
            {
            case k_TemplateNode:
                var templateDependencies = new HashSet <string>();
                templateDependencies.Add(assetPath);

                if (!HasTemplateCircularDependencies(projectRelativePath, templateDependencies))
                {
                    dependencies.Add(projectRelativePath);
                }
                else
                {
                    logger.LogError(ImportErrorType.Semantic, ImportErrorCode.TemplateHasCircularDependency, projectRelativePath, Error.Level.Warning, templateNode);
                }
                break;

            default:
                dependencies.Add(projectRelativePath);
                break;
            }
        }
        static void AddAssetDependency(string assetPath, string src, List <string> dependencies)
        {
            if (string.IsNullOrEmpty(src))
            {
                return;
            }

            var result = URIHelpers.ValidAssetURL(assetPath, src, out var _, out var projectRelativePath);

            if (result != URIValidationResult.OK)
            {
                return;
            }

            dependencies.Add(projectRelativePath);
        }
Exemple #5
0
        void LoadStyleReferenceNode(VisualElementAsset vea, XElement styleElt, VisualTreeAsset vta)
        {
            XAttribute pathAttr = styleElt.Attribute(k_GenericPathAttr);
            bool       hasPath  = pathAttr != null && !String.IsNullOrEmpty(pathAttr.Value);

            XAttribute srcAttr = styleElt.Attribute(k_GenericSrcAttr);
            bool       hasSrc  = srcAttr != null && !String.IsNullOrEmpty(srcAttr.Value);

            if (hasPath == hasSrc)
            {
                LogWarning(vta,
                           ImportErrorType.Semantic,
                           hasPath ? ImportErrorCode.StyleReferenceSrcAndPathBothSpecified : ImportErrorCode.StyleReferenceEmptyOrMissingPathOrSrcAttr,
                           null,
                           styleElt);
                return;
            }

            if (hasPath)
            {
                vea.stylesheetPaths.Add(pathAttr.Value);
            }
            else if (hasSrc)
            {
                string errorMessage, projectRelativePath;

                URIValidationResult result = URIHelpers.ValidAssetURL(assetPath, srcAttr.Value, out errorMessage, out projectRelativePath);

                if (result != URIValidationResult.OK)
                {
                    LogError(vta, ImportErrorType.Semantic, ConvertErrorCode(result), errorMessage, styleElt);
                }
                else
                {
                    Object asset = DeclareDependencyAndLoad(projectRelativePath);

                    if (asset is StyleSheet)
                    {
                        vea.stylesheets.Add(asset as StyleSheet);
                    }
                    else
                    {
                        LogError(vta, ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidAssetType, projectRelativePath, styleElt);
                    }
                }
            }
        }
Exemple #6
0
        internal static bool HasTemplateCircularDependencies(XElement templateElement, HashSet <string> templateDependencies, string rootAssetPath)
        {
            bool hasCircularDependencies = false;

            var elements = templateElement.Elements();

            foreach (var child in elements)
            {
                switch (child.Name.LocalName)
                {
                case k_TemplateNode:
                    var attributes = child.Attributes();

                    foreach (var xAttribute in attributes)
                    {
                        if (xAttribute.Name.LocalName != k_GenericSrcAttr)
                        {
                            continue;
                        }

                        var src = xAttribute.Value;
                        URIValidationResult result = URIHelpers.ValidAssetURL(rootAssetPath, src, out string errorMessage, out string projectRelativePath);
                        hasCircularDependencies = HasTemplateCircularDependencies(projectRelativePath, templateDependencies);

                        if (!hasCircularDependencies)
                        {
                            templateDependencies.Remove(projectRelativePath);
                        }
                    }

                    break;

                default:
                    hasCircularDependencies = HasTemplateCircularDependencies(child, templateDependencies, rootAssetPath);
                    break;
                }

                if (hasCircularDependencies)
                {
                    break;
                }
            }

            return(hasCircularDependencies);
        }
        static void AppendElementAttribute(string name, string value, StringBuilder stringBuilder)
        {
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            if (name == "picking-mode" && value == "Position")
            {
                return;
            }

            // Clean up value and make it ready for XML.
            value = URIHelpers.EncodeUri(value);

            stringBuilder.Append(" ");
            stringBuilder.Append(name);
            stringBuilder.Append("=\"");
            stringBuilder.Append(value);
            stringBuilder.Append("\"");
        }
        public static string GetProcessedPathForSrcAttribute(Object asset, string vtaPath, string assetPath)
        {
            if (asset)
            {
                return(URIHelpers.MakeAssetUri(asset));
            }

            if (string.IsNullOrEmpty(assetPath))
            {
                return(assetPath);
            }

            var result = string.Empty;

            if (!string.IsNullOrEmpty(vtaPath))
            {
                var vtaDir = Path.GetDirectoryName(vtaPath);
                vtaDir  = vtaDir.Replace('\\', '/');
                vtaDir += "/";

                var assetPathDir = Path.GetDirectoryName(assetPath);
                assetPathDir  = assetPathDir.Replace('\\', '/');
                assetPathDir += "/";

                if (assetPathDir.StartsWith(vtaDir))
                {
                    result = assetPath.Substring(vtaDir.Length); // +1 for the /
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                result = "/" + assetPath;
            }

            return(result);
        }
        private static string GetPathValueFromAssetRef(UnityEngine.Object assetRef)
        {
            var assetPath = URIHelpers.MakeAssetUri(assetRef);

            return(assetRef == null ? "none" : $"url('{assetPath}')");
        }
Exemple #10
0
        void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child)
        {
            bool   hasPath = false;
            bool   hasSrc  = false;
            string name    = null;
            string path    = null;
            string src     = null;

            foreach (var xAttribute in child.Attributes())
            {
                switch (xAttribute.Name.LocalName)
                {
                case k_GenericPathAttr:
                    hasPath = true;
                    path    = xAttribute.Value;
                    break;

                case k_GenericSrcAttr:
                    hasSrc = true;
                    src    = xAttribute.Value;
                    break;

                case k_TemplateNameAttr:
                    name = xAttribute.Value;
                    if (String.IsNullOrEmpty(name))
                    {
                        logger.LogError(ImportErrorType.Semantic,
                                        ImportErrorCode.TemplateHasEmptyName,
                                        child,
                                        Error.Level.Fatal,
                                        child
                                        );
                    }
                    break;

                default:
                    logger.LogError(ImportErrorType.Semantic,
                                    ImportErrorCode.UnknownAttribute,
                                    xAttribute.Name.LocalName,
                                    Error.Level.Fatal,
                                    child
                                    );
                    break;
                }
            }

            if (hasPath == hasSrc)
            {
                logger.LogError(ImportErrorType.Semantic,
                                hasPath ? ImportErrorCode.TemplateSrcAndPathBothSpecified : ImportErrorCode.TemplateMissingPathOrSrcAttribute,
                                null,
                                Error.Level.Fatal,
                                elt
                                );
                return;
            }

            if (String.IsNullOrEmpty(name))
            {
                name = Path.GetFileNameWithoutExtension(path);
            }

            if (vta.TemplateExists(name))
            {
                logger.LogError(ImportErrorType.Semantic,
                                ImportErrorCode.DuplicateTemplateName,
                                name,
                                Error.Level.Fatal,
                                elt
                                );
                return;
            }

            if (hasPath)
            {
                vta.RegisterTemplate(name, path);
            }
            else if (hasSrc)
            {
                string errorMessage, projectRelativePath;

                URIValidationResult result = URIHelpers.ValidAssetURL(assetPath, src, out errorMessage, out projectRelativePath);

                if (result != URIValidationResult.OK)
                {
                    logger.LogError(ImportErrorType.Semantic, ConvertErrorCode(result), errorMessage, Error.Level.Fatal, elt);
                }
                else
                {
                    Object asset = DeclareDependencyAndLoad(projectRelativePath);

                    if (asset is VisualTreeAsset)
                    {
                        vta.RegisterTemplate(name, asset as VisualTreeAsset);
                    }
                    else
                    {
                        logger.LogError(ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidAssetType, projectRelativePath, Error.Level.Fatal, elt);
                    }
                }
            }
        }
Exemple #11
0
        void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child)
        {
            bool   hasPath = false;
            bool   hasSrc  = false;
            string name    = null;
            string path    = null;
            string src     = null;

            foreach (var xAttribute in child.Attributes())
            {
                switch (xAttribute.Name.LocalName)
                {
                case k_GenericPathAttr:
                    hasPath = true;
                    path    = xAttribute.Value;
                    break;

                case k_GenericSrcAttr:
                    hasSrc = true;
                    src    = xAttribute.Value;
                    break;

                case k_TemplateNameAttr:
                    name = xAttribute.Value;
                    if (String.IsNullOrEmpty(name))
                    {
                        LogError(vta,
                                 ImportErrorType.Semantic,
                                 ImportErrorCode.TemplateHasEmptyName,
                                 child,
                                 child
                                 );
                    }
                    break;

                default:
                    LogError(vta,
                             ImportErrorType.Semantic,
                             ImportErrorCode.UnknownAttribute,
                             xAttribute.Name.LocalName,
                             child
                             );
                    break;
                }
            }

            if (hasPath == hasSrc)
            {
                LogError(vta,
                         ImportErrorType.Semantic,
                         hasPath ? ImportErrorCode.TemplateSrcAndPathBothSpecified : ImportErrorCode.TemplateMissingPathOrSrcAttribute,
                         null,
                         elt
                         );
                return;
            }

            if (String.IsNullOrEmpty(name))
            {
                name = Path.GetFileNameWithoutExtension(path);
            }

            if (vta.TemplateExists(name))
            {
                LogError(vta,
                         ImportErrorType.Semantic,
                         ImportErrorCode.DuplicateTemplateName,
                         name,
                         elt
                         );
                return;
            }

            if (hasPath)
            {
                vta.RegisterTemplate(name, path);
            }
            else if (hasSrc)
            {
                var response            = URIHelpers.ValidateAssetURL(assetPath, src);
                var result              = response.result;
                var projectRelativePath = response.resolvedProjectRelativePath;

                if (response.hasWarningMessage)
                {
                    logger.LogError(ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidURIProjectAssetPath,
                                    response.warningMessage, Error.Level.Warning, elt);
                }

                if (result != URIValidationResult.OK)
                {
                    LogError(vta, ImportErrorType.Semantic, ConvertErrorCode(result), response.errorToken, elt);
                }
                else
                {
                    var asset = response.resolvedQueryAsset;
                    if (asset && m_Context != null)
                    {
                        m_Context.DependsOnSourceAsset(projectRelativePath);
                    }
                    else
                    {
                        asset = DeclareDependencyAndLoad(projectRelativePath);
                    }

                    if (asset is VisualTreeAsset treeAsset)
                    {
                        vta.RegisterTemplate(name, treeAsset);
                    }
                    else
                    {
                        LogError(vta, ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidAssetType, projectRelativePath, elt);
                    }
                }
            }
        }