2016-05-16 19:55:11 +00:00
|
|
|
|
using System;
|
2016-05-17 20:08:07 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2016-05-16 19:55:11 +00:00
|
|
|
|
|
|
|
|
|
namespace SpritePacker
|
|
|
|
|
{
|
2016-05-19 19:31:48 +00:00
|
|
|
|
enum ProgramMode
|
|
|
|
|
{
|
|
|
|
|
Normal,
|
|
|
|
|
DisplayHelpText,
|
|
|
|
|
DisplayVersionText
|
2016-05-21 13:35:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum OutputFormat
|
|
|
|
|
{
|
|
|
|
|
Text,
|
|
|
|
|
CSV
|
|
|
|
|
}
|
2016-05-19 19:31:48 +00:00
|
|
|
|
|
2016-05-16 19:55:11 +00:00
|
|
|
|
class Program
|
|
|
|
|
{
|
2016-05-19 19:47:26 +00:00
|
|
|
|
public static string Version = Utilities.GetEmbeddedResourceContent("SpritePacker.Version.txt").Trim();
|
|
|
|
|
|
2016-05-19 19:31:48 +00:00
|
|
|
|
private static ProgramMode programMode = ProgramMode.Normal;
|
2016-05-20 20:02:42 +00:00
|
|
|
|
|
2016-05-19 19:31:48 +00:00
|
|
|
|
private static string helpTextFilename = "SpritePacker.Resources.HelpText.txt";
|
|
|
|
|
private static string versionTextFilename = "SpritePacker.Resources.VersionText.txt";
|
2016-05-19 19:47:26 +00:00
|
|
|
|
private static string commitHashFilename = "SpritePacker.latest-commit-hash.txt";
|
2016-05-20 20:02:42 +00:00
|
|
|
|
|
2016-05-21 13:13:05 +00:00
|
|
|
|
private static SpritePacker spritePacker;
|
|
|
|
|
|
2016-05-21 13:35:24 +00:00
|
|
|
|
private static OutputFormat outputFormat = OutputFormat.Text;
|
2016-05-21 13:13:05 +00:00
|
|
|
|
private static string outputFilename = "spritesheet.png";
|
2016-05-19 19:31:48 +00:00
|
|
|
|
private static List<string> values = new List<string>();
|
|
|
|
|
|
2016-05-20 20:02:42 +00:00
|
|
|
|
public static bool Verbose = false;
|
|
|
|
|
|
2016-05-19 19:59:57 +00:00
|
|
|
|
public static int Main(string[] args)
|
2016-05-16 19:55:11 +00:00
|
|
|
|
{
|
2016-05-19 19:47:26 +00:00
|
|
|
|
string commitHash = Utilities.GetEmbeddedResourceContent(commitHashFilename).Trim();
|
2016-05-25 11:06:17 +00:00
|
|
|
|
Version += "-" + commitHash.Substring(0, 7);
|
2016-05-19 19:47:26 +00:00
|
|
|
|
|
2016-05-17 20:08:07 +00:00
|
|
|
|
for(int i = 0; i < args.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
switch(args[i])
|
|
|
|
|
{
|
|
|
|
|
case "--help":
|
2016-05-19 19:31:48 +00:00
|
|
|
|
programMode = ProgramMode.DisplayHelpText;
|
|
|
|
|
break;
|
2016-05-17 20:08:07 +00:00
|
|
|
|
case "--version":
|
2016-05-19 19:31:48 +00:00
|
|
|
|
programMode = ProgramMode.DisplayVersionText;
|
|
|
|
|
break;
|
2016-05-20 20:02:42 +00:00
|
|
|
|
case "--verbose":
|
|
|
|
|
Verbose = true;
|
|
|
|
|
break;
|
2016-05-21 13:35:24 +00:00
|
|
|
|
case "--csv":
|
|
|
|
|
outputFormat = OutputFormat.CSV;
|
|
|
|
|
break;
|
2016-05-17 20:08:07 +00:00
|
|
|
|
default:
|
2016-05-19 19:59:57 +00:00
|
|
|
|
if(args[i].StartsWith("-"))
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("Error: Unrecognised flag '{0}'.", args[i]);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2016-05-17 20:08:07 +00:00
|
|
|
|
values.Add(args[i]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 19:31:48 +00:00
|
|
|
|
switch(programMode)
|
|
|
|
|
{
|
|
|
|
|
case ProgramMode.Normal:
|
2016-05-21 13:13:05 +00:00
|
|
|
|
if (values.Count == 0) {
|
2016-05-19 20:03:35 +00:00
|
|
|
|
Console.Error.WriteLine("Error: No filenames specified!");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2016-05-21 13:13:05 +00:00
|
|
|
|
|
|
|
|
|
// Set the output filename to be equal to the first filename in the list
|
|
|
|
|
outputFilename = values[0];
|
|
|
|
|
values.RemoveAt(0);
|
|
|
|
|
|
2016-05-19 19:31:48 +00:00
|
|
|
|
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>() {
|
2016-05-19 19:47:26 +00:00
|
|
|
|
{ "version", Version },
|
2016-05-19 19:31:48 +00:00
|
|
|
|
{ "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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 19:59:57 +00:00
|
|
|
|
return 0;
|
2016-05-19 19:31:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RunNormal()
|
|
|
|
|
{
|
2016-05-21 13:13:05 +00:00
|
|
|
|
spritePacker = new SpritePacker(Verbose);
|
2016-05-20 20:02:42 +00:00
|
|
|
|
addSprites(values);
|
|
|
|
|
spritePacker.Arrange();
|
2016-05-21 13:13:05 +00:00
|
|
|
|
spritePacker.Output(outputFilename);
|
2016-05-21 13:35:24 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2016-05-20 20:02:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void addSprites(List<string> filenames)
|
|
|
|
|
{
|
2016-05-17 20:08:07 +00:00
|
|
|
|
foreach(string filename in values)
|
|
|
|
|
{
|
2016-05-20 20:02:42 +00:00
|
|
|
|
// Make sure that the file actually exists (we don't want the packer to throw a tantrum)
|
2016-05-17 20:08:07 +00:00
|
|
|
|
if (File.Exists(filename))
|
2016-05-20 20:02:42 +00:00
|
|
|
|
{
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-17 20:08:07 +00:00
|
|
|
|
else
|
2016-05-20 20:02:42 +00:00
|
|
|
|
{
|
2016-05-17 20:08:07 +00:00
|
|
|
|
Console.Error.WriteLine("Warning: Ignoring non-existent file '{0}'.", filename);
|
2016-05-20 20:02:42 +00:00
|
|
|
|
}
|
2016-05-17 20:08:07 +00:00
|
|
|
|
}
|
2016-05-20 20:02:42 +00:00
|
|
|
|
|
2016-05-16 19:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|