SpritePacker/SpritePacker-CLI/Program.cs

155 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
namespace SpritePacker
{
enum ProgramMode
{
Normal,
DisplayHelpText,
DisplayVersionText
}
enum OutputFormat
{
Text,
CSV
}
class Program
{
public static string Version = Utilities.GetEmbeddedResourceContent("SpritePacker.Version.txt").Trim();
private static ProgramMode programMode = ProgramMode.Normal;
private static string helpTextFilename = "SpritePacker.Resources.HelpText.txt";
private static string versionTextFilename = "SpritePacker.Resources.VersionText.txt";
private static string commitHashFilename = "SpritePacker.latest-commit-hash.txt";
private static SpritePacker spritePacker;
private static OutputFormat outputFormat = OutputFormat.Text;
private static string outputFilename = "spritesheet.png";
private static List<string> values = new List<string>();
public static bool Verbose = false;
public static int Main(string[] args)
{
string commitHash = Utilities.GetEmbeddedResourceContent(commitHashFilename).Trim();
Version += "-" + commitHash.Substring(0, 7);
for(int i = 0; i < args.Length; i++)
{
switch(args[i])
{
case "--help":
programMode = ProgramMode.DisplayHelpText;
break;
case "--version":
programMode = ProgramMode.DisplayVersionText;
break;
case "--verbose":
Verbose = true;
break;
case "--csv":
outputFormat = OutputFormat.CSV;
break;
default:
if(args[i].StartsWith("-"))
{
Console.Error.WriteLine("Error: Unrecognised flag '{0}'.", args[i]);
return 1;
}
values.Add(args[i]);
break;
}
}
switch(programMode)
{
case ProgramMode.Normal:
if (values.Count == 0) {
Console.Error.WriteLine("Error: No filenames specified!");
return 1;
}
// Set the output filename to be equal to the first filename in the list
outputFilename = values[0];
values.RemoveAt(0);
RunNormal();
break;
case ProgramMode.DisplayHelpText:
case ProgramMode.DisplayVersionText:
string textFileName = programMode == ProgramMode.DisplayHelpText ? helpTextFilename : versionTextFilename;
string helpText = Utilities.GetEmbeddedResourceContent(textFileName);
Dictionary<string, string> templateValues = new Dictionary<string, string>() {
{ "version", Version },
{ "build-date", Utilities.GetProgramBuildDate().ToString("dd/MM/yyyy h:mmtt") },
{ "commit-hash", commitHash.Substring(commitHash.Length - 7) },
{ "commit-url", string.Format("https://git.starbeamrainbowlabs.com/sbrl/SpritePacker/commit/{0}", commitHash) }
};
foreach (KeyValuePair<string, string> replacePair in templateValues)
helpText = helpText.Replace(string.Format("{{{0}}}", replacePair.Key), replacePair.Value);
Console.WriteLine(helpText);
break;
}
return 0;
}
public static void RunNormal()
{
spritePacker = new SpritePacker(Verbose);
addSprites(values);
spritePacker.Arrange();
spritePacker.Output(outputFilename);
switch(outputFormat)
{
case OutputFormat.CSV:
Console.WriteLine(spritePacker.GetSpritePositionsCSV().Trim());
break;
case OutputFormat.Text:
Console.WriteLine(spritePacker.ToString());
break;
default:
Console.Error.WriteLine("Error: Invalid output format!");
break;
}
}
private static void addSprites(List<string> filenames)
{
foreach(string filename in values)
{
// Make sure that the file actually exists (we don't want the packer to throw a tantrum)
if (File.Exists(filename))
{
try
{
spritePacker.Add(new Sprite(filename));
}
catch(Exception error)
{
Console.Error.WriteLine("Error reading {0}: {1}", filename, error.Message);
}
if ((File.GetAttributes(filename) & FileAttributes.Directory) == FileAttributes.Directory)
{
// This filename is a directory - recurse over all the files in the directory
string[] dirFiles = Directory.GetFiles(filename);
addSprites(new List<string>(dirFiles));
}
}
else
{
Console.Error.WriteLine("Warning: Ignoring non-existent file '{0}'.", filename);
}
}
}
}
}