From 570fe7d7ba2136dc2af22898e0d2f849a634b1d1 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Sun, 13 Jan 2019 13:55:08 -0600 Subject: [PATCH] Performed basic tests and fixed 9patch and texturepacker. --- .../CommandProcessor/CommandEngine.cs | 2 +- .../CommandProcessor/EngineCommand.cs | 20 +++++------ .../NinePatchTools/NinePatchCommand.cs | 30 ++++++++-------- .../TextureAtlasTools/TexturePacker.cs | 34 ++++++++++--------- .../TextureAtlasTools/TexturePackerCommand.cs | 16 ++++++--- 5 files changed, 55 insertions(+), 47 deletions(-) diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs index 379d289..88c5c62 100644 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs @@ -62,7 +62,7 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor } while (!argumentsSplit[i].Contains('"')); quoteBuilder.Append(' '); quoteBuilder.Append(argumentsSplit[i]); - argumentsList.Add(quoteBuilder.ToString().Trim()); + argumentsList.Add(quoteBuilder.ToString().Replace("\"", "").Trim()); } else { diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs index 4f4bbe7..a7cca1b 100644 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs @@ -18,7 +18,7 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor /// /// Arguments that this command should accept and take into account. /// - protected EngineCommandArgument[] arguments; + protected EngineCommandArgument[] commandArguments; protected string help; public EngineCommand(params string[] invokeStrings) @@ -71,11 +71,11 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor /// null if not viable or the if viable. public EngineCommandArgument EngineCommandArgumentOf(string argument) { - for (int i = 0; i < arguments.Length; i++) + for (int i = 0; i < commandArguments.Length; i++) { - if (arguments[i].invokeString == argument) + if (commandArguments[i].invokeString == argument) { - return arguments[i]; + return commandArguments[i]; } } return null; @@ -138,7 +138,7 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor /// The string for the help. public string Help(string argument = null) { - if (argument != null && arguments != null) + if (argument != null && commandArguments != null) { if (Validate(argument)) { @@ -156,18 +156,18 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor { StringBuilder helpBuilder = new StringBuilder(); helpBuilder.Append(help); - if (arguments != null) + if (commandArguments != null) { helpBuilder.AppendLine(); helpBuilder.Append("Possible arguments are: "); - for (int i = 0; i < arguments.Length; i++) + for (int i = 0; i < commandArguments.Length; i++) { - helpBuilder.Append(arguments[i].invokeString); - if (i == arguments.Length - 2) + helpBuilder.Append(commandArguments[i].invokeString); + if (i == commandArguments.Length - 2) { helpBuilder.Append(", and "); } - else if (i < arguments.Length - 2) + else if (i < commandArguments.Length - 2) { helpBuilder.Append(", "); } diff --git a/RecrownedAthenaeum.ConsoleTools/NinePatchTools/NinePatchCommand.cs b/RecrownedAthenaeum.ConsoleTools/NinePatchTools/NinePatchCommand.cs index 413770c..b3c221d 100644 --- a/RecrownedAthenaeum.ConsoleTools/NinePatchTools/NinePatchCommand.cs +++ b/RecrownedAthenaeum.ConsoleTools/NinePatchTools/NinePatchCommand.cs @@ -17,14 +17,14 @@ namespace RecrownedAthenaeum.Tools.NinePatchTools { help = "Generates a 9 patch file for a given image."; - arguments = new EngineCommandArgument[6]; + commandArguments = new EngineCommandArgument[6]; - arguments[0] = new EngineCommandArgument("-i", "defines the path to the texture to be used for the nine patch.", true); - arguments[1] = new EngineCommandArgument("-o", "defines path of output file."); - arguments[2] = new EngineCommandArgument("-l", "left patch.", true); - arguments[3] = new EngineCommandArgument("-r", "right patch.", true); - arguments[4] = new EngineCommandArgument("-u", "up patch.", true); - arguments[5] = new EngineCommandArgument("-d", "down patch.", true); + commandArguments[0] = new EngineCommandArgument("-i", "defines the path to the texture to be used for the nine patch.", true); + commandArguments[1] = new EngineCommandArgument("-o", "defines path of output file."); + commandArguments[2] = new EngineCommandArgument("-l", "left patch.", true); + commandArguments[3] = new EngineCommandArgument("-r", "right patch.", true); + commandArguments[4] = new EngineCommandArgument("-u", "up patch.", true); + commandArguments[5] = new EngineCommandArgument("-d", "down patch.", true); } public override void Run(string[] arguments = null) @@ -35,13 +35,13 @@ namespace RecrownedAthenaeum.Tools.NinePatchTools if (IndexOfArgumentIn("-i", arguments) + 1 >= arguments.Length) throw new ArgumentException("Missing -i path after argument."); imagePath = arguments[IndexOfArgumentIn("-i", arguments) + 1]; - if (HasArgument(arguments[1], arguments)) + if (HasArgument(commandArguments[1], arguments)) { if (IndexOfArgumentIn("-o", arguments) + 1 >= arguments.Length) throw new ArgumentException("Missing -o path after argument."); outPath = arguments[IndexOfArgumentIn("-o", arguments) + 1]; } else { - outPath = Path.GetFileNameWithoutExtension(imagePath); + outPath = imagePath.Substring(0, imagePath.Length - Path.GetExtension(imagePath).Length); } if (IndexOfArgumentIn("-l", arguments) + 1 >= arguments.Length && !int.TryParse(arguments[IndexOfArgumentIn("-l", arguments) + 1], out leftBound)) throw new ArgumentException("Missing -l argument bound."); @@ -52,13 +52,13 @@ namespace RecrownedAthenaeum.Tools.NinePatchTools NinePatchData npData = new NinePatchData(Path.GetFileName(imagePath), leftBound, rightBound, bottomBound, topBound); string serialized = JsonConvert.SerializeObject(npData, Formatting.Indented); - if (!File.Exists(imagePath)) throw new ArgumentException("Input file does not exist."); + if (!File.Exists(imagePath)) throw new ArgumentException("Input file does not exist at " + imagePath + "."); SupportedExtensions extension; - if (!Enum.TryParse(Path.GetExtension(imagePath).ToLower().Substring(1), out extension)) throw new ArgumentException("Input file extension not supported."); - using (StreamWriter stringWriter = new StreamWriter(outPath + ".9p")) - { - stringWriter.WriteLine(serialized); - } + if (!Enum.TryParse(Path.GetExtension(imagePath).ToLower().Substring(1), out extension)) throw new ArgumentException("Input file extension \"" + Path.GetExtension(imagePath).ToLower().Substring(1) + "\" not supported."); + + File.WriteAllText(outPath + ".9p", serialized); + + ConsoleUtilities.WriteWrappedLine("Done. Written to \"" + outPath + "\""); } } } diff --git a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePacker.cs b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePacker.cs index fcba488..ae07723 100644 --- a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePacker.cs +++ b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePacker.cs @@ -10,6 +10,7 @@ using SixLabors.Primitives; using System.Linq; using Newtonsoft.Json; using System.Runtime.InteropServices; +using System.Security; namespace RecrownedAthenaeum.Tools.TextureAtlas { @@ -41,11 +42,11 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas string[] paths; try { - paths = Directory.GetFiles(rootDirectoryPath); - - } catch (DirectoryNotFoundException) + paths = Directory.GetFiles(rootDirectoryPath); + } + catch (IOException) { - throw new ArgumentException("Path " + rootDirectoryPath + " couldn't be found."); + throw new ArgumentException("Path " + rootDirectoryPath + " couldn't be resolved."); } TexturePowerLength = startingPower; List imageHandlers = new List(); @@ -66,11 +67,10 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas else if (Path.GetExtension(paths[pathID]).ToLower() == ".9p") { if (ninePatchDictionary == null) ninePatchDictionary = new Dictionary(); - using (StreamReader streamReader = new StreamReader(paths[pathID])) - { - NinePatchData npData = JsonConvert.DeserializeObject(streamReader.ReadLine()); - ninePatchDictionary.Add(npData.textureName, npData); - } + ConsoleUtilities.WriteWrappedLine("Reading ninepatch data for: " + paths[pathID]); + string serialized = File.ReadAllText(paths[pathID]); + NinePatchData npData = JsonConvert.DeserializeObject(serialized); + ninePatchDictionary.Add(npData.textureName, npData); } } imageHandlers.Sort(); @@ -148,11 +148,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas } } string serialized = JsonConvert.SerializeObject(new TextureAtlasData(atlasName + ".png", regions), Formatting.Indented); - - using (StreamWriter stream = new StreamWriter(output + "/" + atlasName + ".tatlas")) - { - stream.WriteLine(serialized); - } + File.WriteAllText(output + "/" + atlasName + ".tatlas", serialized); } public void SetNinePatch(string fileName, int a, int b, int c, int d) @@ -261,9 +257,15 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas internal ImageHandler(String path) { this.path = path; - using (FileStream stream = new FileStream(path, FileMode.Open)) + try { - image = Image.Identify(stream); + using (FileStream stream = new FileStream(path, FileMode.Open)) + { + image = Image.Identify(stream); + } + } catch (SecurityException) + { + throw new ArgumentException("Security exception occurred for image: " + path); } } diff --git a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs index fa6f6ef..1959a2d 100644 --- a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs +++ b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs @@ -13,7 +13,7 @@ namespace RecrownedAthenaeum.Tools.TextureAtlasTools public TexturePackerCommand() : base("texturepacker") { help = "Packs a given directory composed of png and jpg files into an atlas. Can also add 9patch properties. Images with the associated \".9p\" files will automatically be defined in the resulting .tatlas file, but can be overwritten by use of the \"-9p\" argument."; - arguments = new[] { + commandArguments = new[] { new EngineCommandArgument("-interactive", "runs in interactive mode. Ninepatches must still be defined with arguments or previously defined with associated \".9p\" files. Other arguments will be ignored."), new EngineCommandArgument("-i", "for input directory containing the textures.", true), new EngineCommandArgument("-o", "Path for output files. Points to non-existent file. Will create texture and definitions file with name.", true), @@ -41,12 +41,12 @@ namespace RecrownedAthenaeum.Tools.TextureAtlasTools ConsoleUtilities.WriteWrappedLine("Please enter path of folder containing the textures to be packed."); input = Console.ReadLine(); if (input == "q") return; - path = input; + path = input.Replace("\"", ""); ConsoleUtilities.WriteWrappedLine("Please enter output path of file name."); input = Console.ReadLine(); if (input == "q") return; - output = input; + output = input.Replace("\"", ""); do { @@ -70,8 +70,14 @@ namespace RecrownedAthenaeum.Tools.TextureAtlasTools int indexOfInputArg = IndexOfArgumentIn("-i", arguments); if (indexOfInputArg + 1 >= arguments.Length) throw new ArgumentException("-i is not followed by input path."); path = arguments[1 + IndexOfArgumentIn("-i", arguments)]; - int.TryParse(arguments[IndexOfArgumentIn("-mp", arguments) + 1], out mp); - int.TryParse(arguments[IndexOfArgumentIn("-sp", arguments) + 1], out sp); + if (HasArgument("-mp", arguments)) + { + int.TryParse(arguments[IndexOfArgumentIn("-mp", arguments) + 1], out mp); + } + if (HasArgument("-sp", arguments)) + { + int.TryParse(arguments[IndexOfArgumentIn("-sp", arguments) + 1], out sp); + } int indexOfOutputArg = IndexOfArgumentIn("-o", arguments); if (indexOfOutputArg + 1 >= arguments.Length) throw new ArgumentException("-o is not followed by input path."); output = arguments[IndexOfArgumentIn("-o", arguments) + 1];