private bool AddTerm(Term value) { var added = true; if (_isFraction) { if (_terms.Length > 0) { value = new PrimitiveTerm(UnitType.Unknown, _terms[_terms.Length - 1] + "/" + value); } _terms.SetLastTerm(value); _isFraction = false; } else if (_functionBuffers.Count > 0) { _functionBuffers.Peek().TermList.Add(value); } else if (_terms.Length == 0) { _terms.AddTerm(value); } else if (_parsingContext == ParsingContext.InSingleValue) { _terms.AddTerm(value); } else { added = false; } return(added); }
protected void VisitUrlFunction(PrimitiveTerm term) { string path = (string)term.Value; string projectRelativePath, errorMessage; URIValidationResult result = URIHelpers.ValidAssetURL(assetPath, path, out errorMessage, out projectRelativePath); if (result != URIValidationResult.OK) { m_Errors.AddSemanticError(ConvertErrorCode(result), errorMessage); } else { UnityEngine.Object asset = DeclareDependencyAndLoad(projectRelativePath); if (asset is Texture2D || asset is Font) { m_Builder.AddValue(asset); } else { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidURIProjectAssetType, string.Format("Invalid asset type {0}, only Font and Texture2D are supported", asset.GetType().Name)); } } }
protected void VisitUrlFunction(PrimitiveTerm term) { string path = (string)term.Value; string projectRelativePath, errorMessage; URIValidationResult result = URIHelpers.ValidAssetURL(assetPath, path, out errorMessage, out projectRelativePath); if (result != URIValidationResult.OK) { m_Builder.AddValue(path, StyleValueType.MissingAssetReference); m_Errors.AddValidationWarning(errorMessage, m_Builder.currentProperty.line); } else { UnityEngine.Object asset = DeclareDependencyAndLoad(projectRelativePath); bool isTexture = asset is Texture2D; if (isTexture || asset is Font || asset is VectorImage) { // 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 DeclareDependencyAndLoad(hiResImageLocation); } m_Builder.AddValue(asset); } else { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidURIProjectAssetType, string.Format("Invalid asset type {0}, only Font, Texture2D and VectorImage are supported", asset.GetType().Name)); } } }
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)); } } }
protected void VisitUrlFunction(PrimitiveTerm term) { string path = term.Value as string; if (string.IsNullOrEmpty(path)) { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidURILocation, ""); return; } Uri absoluteUri = null; // Always treat URIs starting with "/" as implicit project schemes if (path.StartsWith("/")) { var builder = new UriBuilder(s_ProjectRootUri.Scheme, "", 0, path); absoluteUri = builder.Uri; } else if (Uri.TryCreate(path, UriKind.Absolute, out absoluteUri) == false) { // Resolve a relative URI compared to current file Uri assetPathUri = new Uri(s_ProjectRootUri, assetPath); if (Uri.TryCreate(assetPathUri, path, out absoluteUri) == false) { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidURILocation, path); return; } } else if (absoluteUri.Scheme != s_ProjectRootUri.Scheme) { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidURIScheme, absoluteUri.Scheme); return; } string projectRelativePath = absoluteUri.LocalPath; // Remove any leading "/" as this now used as a path relative to the current directory if (projectRelativePath.StartsWith("/")) { projectRelativePath = projectRelativePath.Substring(1); } if (string.IsNullOrEmpty(projectRelativePath) || !File.Exists(projectRelativePath)) { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidURIProjectAssetPath, path); } else { UnityEngine.Object asset = DeclareDependencyAndLoad(projectRelativePath); if (asset is Texture2D || asset is Font) { m_Builder.AddValue(asset); } else { m_Errors.AddSemanticError(StyleSheetImportErrorCode.InvalidURIProjectAssetType, string.Format("Invalid asset type {0}, only Font and Texture2D are supported", asset.GetType().Name)); } } }
public PrimitiveTermReference(Parser parser, PrimitiveTerm primitiveTerm) : base(new ReferenceArgs(parser, ReferenceKind.ExternalResource)) { _primitiveTerm = primitiveTerm; }
internal static void VisitValue(StyleSheetImportErrors errors, StyleSheetBuilder ssb, Term term) { PrimitiveTerm primitiveTerm = term as PrimitiveTerm; HtmlColor htmlColor = term as HtmlColor; GenericFunction genericFunction = term as GenericFunction; TermList termList = term as TermList; if (term == Term.Inherit) { ssb.AddValue(StyleValueKeyword.Inherit); } else if (primitiveTerm != null) { string text = term.ToString(); UnitType primitiveType = primitiveTerm.PrimitiveType; switch (primitiveType) { case UnitType.String: { string value = text.Trim(new char[] { '\'', '"' }); ssb.AddValue(value, StyleValueType.String); goto IL_F9; } case UnitType.Uri: IL_63: if (primitiveType != UnitType.Number && primitiveType != UnitType.Pixel) { errors.AddSemanticError(StyleSheetImportErrorCode.UnsupportedUnit, primitiveTerm.ToString()); return; } ssb.AddValue(primitiveTerm.GetFloatValue(UnitType.Pixel).Value); goto IL_F9; case UnitType.Ident: { StyleValueKeyword keyword; if (StyleSheetImporterImpl.TryParseKeyword(text, out keyword)) { ssb.AddValue(keyword); } else { ssb.AddValue(text, StyleValueType.Enum); } goto IL_F9; } } goto IL_63; IL_F9 :; } else if (htmlColor != null) { Color value2 = new Color((float)htmlColor.R / 255f, (float)htmlColor.G / 255f, (float)htmlColor.B / 255f, (float)htmlColor.A / 255f); ssb.AddValue(value2); } else if (genericFunction != null) { primitiveTerm = (genericFunction.Arguments.FirstOrDefault <Term>() as PrimitiveTerm); if (genericFunction.Name == "resource" && primitiveTerm != null) { string value3 = primitiveTerm.Value as string; ssb.AddValue(value3, StyleValueType.ResourcePath); } else { errors.AddSemanticError(StyleSheetImportErrorCode.UnsupportedFunction, genericFunction.Name); } } else if (termList != null) { foreach (Term current in termList) { StyleSheetImporterImpl.VisitValue(errors, ssb, current); } } else { errors.AddInternalError(term.GetType().Name); } }
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); } } }