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(string.Format(glossary.ussParsingError, error.Message), error.Line); } } else { try { VisitSheet(styleSheet); } catch (System.Exception exc) { m_Errors.AddInternalError(string.Format(glossary.internalErrorWithStackTrace, exc.Message, exc.StackTrace), m_CurrentLine); } } bool hasErrors = m_Errors.hasErrors; if (!hasErrors) { 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; var response = URIHelpers.ValidateAssetURL(assetPath, importedPath); var importResult = response.result; var errorToken = response.errorToken; var projectRelativePath = response.resolvedProjectRelativePath; if (response.hasWarningMessage) { m_Errors.AddValidationWarning(response.warningMessage, m_CurrentLine); } UnityStyleSheet importedStyleSheet = null; if (importResult != URIValidationResult.OK) { var(code, message) = ConvertErrorCode(importResult); m_Errors.AddSemanticWarning(code, string.Format(message, errorToken), m_CurrentLine); } else { importedStyleSheet = response.resolvedQueryAsset as UnityStyleSheet; if (importedStyleSheet) { m_Context.DependsOnSourceAsset(projectRelativePath); } else { importedStyleSheet = DeclareDependencyAndLoad(projectRelativePath) as UnityStyleSheet; } if (!response.isLibraryAsset) { 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]; m_Errors.AddValidationWarning(glossary.circularImport, -1); } OnImportSuccess(asset); } bool hasWarnings = m_Errors.hasWarning; asset.importedWithErrors = hasErrors; asset.importedWithWarnings = hasWarnings; if (hasErrors || hasWarnings) { OnImportError(m_Errors); } }
protected void VisitUrlFunction(PrimitiveTerm term) { string path = (string)term.Value; var response = URIHelpers.ValidateAssetURL(assetPath, path); if (response.hasWarningMessage) { m_Errors.AddValidationWarning(response.warningMessage, m_CurrentLine); } if (response.result != URIValidationResult.OK) { var(_, message) = ConvertErrorCode(response.result); m_Builder.AddValue(path, StyleValueType.MissingAssetReference); m_Errors.AddValidationWarning(string.Format(message, response.errorToken), m_CurrentLine); } else { var projectRelativePath = response.resolvedProjectRelativePath; var subAssetPath = response.resolvedSubAssetPath; var asset = response.resolvedQueryAsset; if (asset) { if (response.isLibraryAsset) { // do not add path dependencies on assets in the Library folder (e.g. built-in resources) m_Builder.AddValue(asset); return; } // explicit asset reference already loaded m_Context?.DependsOnSourceAsset(projectRelativePath); } else { 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 (asset != null) { // 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(glossary.invalidHighResAssetType, asset.GetType().Name, projectRelativePath), m_CurrentLine); } return; } // If we didn't find an high res variant, tell ADB we depend on that potential file existing if (spriteAsset != null) { DeclareDependencyAndLoad(hiResImageLocation); } } Object assetToStore = spriteAsset != null ? spriteAsset : asset; m_Builder.AddValue(assetToStore); if (!disableValidation) { var propertyName = new StylePropertyName(m_Builder.currentProperty.name); // Unknown properties (not custom) should beforehand if (propertyName.id == StylePropertyId.Unknown) { return; } var allowed = StylePropertyUtil.GetAllowedAssetTypesForProperty(propertyName.id); // If no types were returned, it means this property doesn't support assets. // Normal syntax validation should cover this. if (!allowed.Any()) { return; } Type assetType = assetToStore.GetType(); // If none of the allowed types are compatible with the asset type, output a warning if (!allowed.Any(t => t.IsAssignableFrom(assetType))) { string allowedTypes = string.Join(", ", allowed.Select(t => t.Name)); m_Errors.AddValidationWarning( string.Format(glossary.invalidAssetType, assetType.Name, projectRelativePath, allowedTypes), m_CurrentLine); } } } else { // Asset is actually missing OR we couldn't load it for some reason; this should result in // response.result != URIValidationResult.OK (above) but if assets are deleted while Unity is // already open, we fall in here instead. var(_, message) = ConvertErrorCode(URIValidationResult.InvalidURIProjectAssetPath); // In case of error, we still want to call AddValue, with parameters to indicate the problem, in order // to keep the full layout from being discarded. We also add appropriate warnings to explain to the // user what is wrong. m_Builder.AddValue(path, StyleValueType.MissingAssetReference); m_Errors.AddValidationWarning(string.Format(message, path), m_CurrentLine); } } }