Performed basic tests and fixed 9patch and texturepacker.

This commit is contained in:
Harrison Deng 2019-01-13 13:55:08 -06:00
parent 9ab70fd291
commit 570fe7d7ba
5 changed files with 55 additions and 47 deletions

View File

@ -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
{

View File

@ -18,7 +18,7 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor
/// <summary>
/// Arguments that this command should accept and take into account.
/// </summary>
protected EngineCommandArgument[] arguments;
protected EngineCommandArgument[] commandArguments;
protected string help;
public EngineCommand(params string[] invokeStrings)
@ -71,11 +71,11 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor
/// <returns>null if not viable or the <see cref="EngineCommandArgumentOf(argument)"/> if viable.</returns>
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
/// <returns>The string for the help.</returns>
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(", ");
}

View File

@ -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<SupportedExtensions>(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<SupportedExtensions>(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 + "\"");
}
}
}

View File

@ -10,6 +10,7 @@ using SixLabors.Primitives;
using System.Linq;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using System.Security;
namespace RecrownedAthenaeum.Tools.TextureAtlas
{
@ -42,10 +43,10 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
try
{
paths = Directory.GetFiles(rootDirectoryPath);
} catch (DirectoryNotFoundException)
}
catch (IOException)
{
throw new ArgumentException("Path " + rootDirectoryPath + " couldn't be found.");
throw new ArgumentException("Path " + rootDirectoryPath + " couldn't be resolved.");
}
TexturePowerLength = startingPower;
List<ImageHandler> imageHandlers = new List<ImageHandler>();
@ -66,13 +67,12 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
else if (Path.GetExtension(paths[pathID]).ToLower() == ".9p")
{
if (ninePatchDictionary == null) ninePatchDictionary = new Dictionary<string, NinePatchData>();
using (StreamReader streamReader = new StreamReader(paths[pathID]))
{
NinePatchData npData = JsonConvert.DeserializeObject<NinePatchData>(streamReader.ReadLine());
ConsoleUtilities.WriteWrappedLine("Reading ninepatch data for: " + paths[pathID]);
string serialized = File.ReadAllText(paths[pathID]);
NinePatchData npData = JsonConvert.DeserializeObject<NinePatchData>(serialized);
ninePatchDictionary.Add(npData.textureName, npData);
}
}
}
imageHandlers.Sort();
this.imageHandlersDictionary = new Dictionary<string, ImageHandler>();
foreach (ImageHandler imageHandler in imageHandlers)
@ -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,10 +257,16 @@ namespace RecrownedAthenaeum.Tools.TextureAtlas
internal ImageHandler(String path)
{
this.path = path;
try
{
using (FileStream stream = new FileStream(path, FileMode.Open))
{
image = Image.Identify(stream);
}
} catch (SecurityException)
{
throw new ArgumentException("Security exception occurred for image: " + path);
}
}
public int CompareTo(ImageHandler tImage)

View File

@ -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)];
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];