internal static void PopulateDependencies(string assetPath, List <string> dependencies) { var contents = File.ReadAllText(assetPath); if (string.IsNullOrEmpty(contents)) { return; } var styleSheet = s_Parser.Parse(contents); var importDirectivesCount = styleSheet.ImportDirectives.Count; s_StyleSheetProjectRelativeImportPaths.Clear(); for (var i = 0; i < importDirectivesCount; ++i) { var importedPath = styleSheet.ImportDirectives[i].Href; var importResult = URIHelpers.ValidAssetURL(assetPath, importedPath, out _, out var projectRelativePath); if (importResult == URIValidationResult.OK) { if (!s_StyleSheetProjectRelativeImportPaths.Contains(projectRelativePath)) { s_StyleSheetProjectRelativeImportPaths.Add(projectRelativePath); } } } foreach (var projectRelativeImportPath in s_StyleSheetProjectRelativeImportPaths) { if (s_StyleSheetsUnsortedDependencies.Contains(projectRelativeImportPath)) { s_StyleSheetsWithCircularImportDependencies.Add(projectRelativeImportPath); throw new InvalidDataException("Circular @import dependencies"); } s_StyleSheetsUnsortedDependencies.Add(projectRelativeImportPath); PopulateDependencies(projectRelativeImportPath, dependencies); dependencies.Add(projectRelativeImportPath); } }
protected void ImportParserStyleSheet(UnityStyleSheet asset, ParserStyleSheet styleSheet) { m_Errors.assetPath = assetPath; if (styleSheet.Errors.Count > 0) { foreach (StylesheetParseError error in styleSheet.Errors) { m_Errors.AddSyntaxError(error.ToString()); } } else { try { VisitSheet(styleSheet); } catch (Exception exc) { Debug.LogException(exc); m_Errors.AddInternalError(exc.StackTrace); } } bool success = !m_Errors.hasErrors; if (success) { m_Builder.BuildTo(asset); if (!s_StyleSheetsWithCircularImportDependencies.Contains(assetPath)) { var importDirectivesCount = styleSheet.ImportDirectives.Count; asset.imports = new UnityStyleSheet.ImportStruct[importDirectivesCount]; for (int i = 0; i < importDirectivesCount; ++i) { var importedPath = styleSheet.ImportDirectives[i].Href; string projectRelativePath, errorMessage; URIValidationResult importResult = URIHelpers.ValidAssetURL(assetPath, importedPath, out errorMessage, out projectRelativePath); UnityStyleSheet importedStyleSheet = null; if (importResult != URIValidationResult.OK) { m_Errors.AddSemanticError(ConvertErrorCode(importResult), errorMessage); } else { importedStyleSheet = DeclareDependencyAndLoad(projectRelativePath) as UnityStyleSheet; m_Context.DependsOnImportedAsset(projectRelativePath); } asset.imports[i] = new UnityStyleSheet.ImportStruct { styleSheet = importedStyleSheet, mediaQueries = styleSheet.ImportDirectives[i].Media.ToArray() }; } if (importDirectivesCount > 0) { asset.FlattenImportedStyleSheetsRecursive(); } } else { asset.imports = new UnityStyleSheet.ImportStruct[0]; var errorMsg = $"The {assetPath} contains circular @import dependencies. All @import directives will be ignored for this StyleSheet."; Debug.LogError(errorMsg); m_Errors.AddInternalError(errorMsg); } OnImportSuccess(asset); } if (!success || m_Errors.hasWarning) { OnImportError(m_Errors); } }
protected void VisitUrlFunction(PrimitiveTerm term) { string path = (string)term.Value; string projectRelativePath, subAssetPath, errorMessage; URIValidationResult result = URIHelpers.ValidAssetURL(assetPath, path, out errorMessage, out projectRelativePath, out subAssetPath); if (result != URIValidationResult.OK) { m_Builder.AddValue(path, StyleValueType.MissingAssetReference); m_Errors.AddValidationWarning(errorMessage, m_Builder.currentProperty.line); } else { UnityEngine.Object asset = DeclareDependencyAndLoad(projectRelativePath, subAssetPath); bool isTexture = asset is Texture2D; Sprite spriteAsset = asset as Sprite; if (isTexture && string.IsNullOrEmpty(subAssetPath)) { // Try to load a sprite sub-asset associated with this texture. // Sprites have extra data, such as slices and tight-meshes that // aren't stored in plain textures. spriteAsset = AssetDatabase.LoadAssetAtPath <Sprite>(projectRelativePath); } if (isTexture || spriteAsset != null || asset is Font || IsFontAssetInternal(asset) || asset is VectorImage || asset is RenderTexture) { // Looking suffixed images files only if (isTexture) { string hiResImageLocation = URIHelpers.InjectFileNameSuffix(projectRelativePath, "@2x"); if (File.Exists(hiResImageLocation)) { UnityEngine.Object hiResImage = DeclareDependencyAndLoad(hiResImageLocation); if (hiResImage is Texture2D) { m_Builder.AddValue(new ScalableImage() { normalImage = asset as Texture2D, highResolutionImage = hiResImage as Texture2D }); } else { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidHighResolutionImage, string.Format("Invalid asset type {0}, only Texture2D is supported for variants with @2x suffix", asset.GetType().Name)); } return; } // If we didn't find an high res variant, tell ADB we depend on that potential file existing if (spriteAsset != null) { DeclareDependencyAndLoad(hiResImageLocation); } } m_Builder.AddValue(spriteAsset != null ? spriteAsset : asset); } else { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidURIProjectAssetType, string.Format("Invalid asset type {0}, only Font, FontAssets, Sprite, Texture2D and VectorImage are supported", asset.GetType().Name)); } } }