public override bool Resolve(IFileProperties fileProperties) { TextureFileProperties textureFileProperties = fileProperties as TextureFileProperties; if (textureFileProperties == null) { throw new RuleException($"Resolve unable to cast {nameof(fileProperties)} arg to {nameof(TextureFileProperties)}, {nameof(fileProperties)}.Type = {fileProperties.GetType()}"); } bool resolveWidth = ResolveNumberValue(textureFileProperties.Width); bool resolveHeight = ResolveNumberValue(textureFileProperties.Height); //TODO consider supporting different width and height return(resolveWidth && resolveHeight); }
/// <summary> /// Resolve a tokenized string based on a file's properties /// </summary> /// <param name="tokenString">the value of either "output_path" or "output_file"</param> /// <param name="fileProperties">the file property object of the file being checked</param> /// <param name="matchingFileCount">the current number of files that match this <see cref="ValidationConfig"/>, including the current fileProperties</param> /// <param name="jsonPropertyName">literal either "output_path" or "output_file" (used for error output)</param> /// <returns>the resolved string with the tokens substituted</returns> private string ResolveTokenString(string tokenString, IFileProperties fileProperties, uint matchingFileCount, string jsonPropertyName) { string originalFileNameWithoutExtension = fileProperties.FileNameWithoutExtension; string[] fileNameParts = originalFileNameWithoutExtension.Split(_nameSeperator); string resolvedTokenString = tokenString; foreach (Match match in tokenRegexPattern.Matches(tokenString)) { string token = match.Value; string partPrefix = "{part"; string partSuffix = "}"; if (token.StartsWith(partPrefix) && token.EndsWith(partSuffix)) { string partIndexStr = token.Substring(partPrefix.Length, token.Length - (partPrefix.Length + partSuffix.Length)); if (Int32.TryParse(partIndexStr, out int partIndex)) { if (partIndex < 0 || partIndex >= fileNameParts.Length) { throw new IndexOutOfRangeException($"'part' index: {partIndex} out of range: {fileNameParts.Length} in validation config: {Name} for file: {originalFileNameWithoutExtension}"); } resolvedTokenString = resolvedTokenString.Replace(token, fileNameParts[partIndex]); } else { throw new Exception($"Failed to parse 'part' token for file: {originalFileNameWithoutExtension} in validation cofig: {Name}"); } continue; } switch (token) { case "{current_name}": { resolvedTokenString = resolvedTokenString.Replace(token, fileProperties.FileNameWithoutExtension); break; } case "{current_extension}": { BaseFileProperties baseFileProperties = fileProperties as BaseFileProperties; string extensionWithoutDot = baseFileProperties.FileExtension.Substring(1); resolvedTokenString = resolvedTokenString.Replace(token, extensionWithoutDot); break; } case "{width}": { TextureFileProperties textureFileProperties = fileProperties as TextureFileProperties; if (textureFileProperties == null) { throw new Exception($"file: {fileProperties.FileName} is not a texture file and thus doesn't have a 'width' property"); } resolvedTokenString = resolvedTokenString.Replace(token, textureFileProperties.Width.ToString()); break; } case "{height}": { TextureFileProperties textureFileProperties = fileProperties as TextureFileProperties; if (textureFileProperties == null) { throw new Exception($"file: {fileProperties.FileName} is not a texture file and thus doesn't have a 'height' property"); } resolvedTokenString = resolvedTokenString.Replace(token, textureFileProperties.Height.ToString()); break; } case "{file_count}": { resolvedTokenString = resolvedTokenString.Replace(token, matchingFileCount.ToString("D3")); break; } default: { throw new Exception($"Unknown token: {token} found in '{jsonPropertyName}' of validation_config: {Name}"); } } } return(resolvedTokenString); }