2018-12-29 06:30:09 +00:00
using Newtonsoft.Json ;
2019-01-20 07:07:52 +00:00
using RecrownedAthenaeum.Data ;
2018-12-29 06:30:09 +00:00
using RecrownedAthenaeum.Tools.CommandProcessor ;
using System ;
using System.IO ;
namespace RecrownedAthenaeum.Tools.NinePatchTools
{
public class NinePatchCommand : EngineCommand
{
2019-01-04 18:23:42 +00:00
2018-12-29 06:30:09 +00:00
public NinePatchCommand ( ) : base ( "9p" , "ninepatch" , "9patch" )
{
help = "Generates a 9 patch file for a given image." ;
2019-01-13 19:55:08 +00:00
commandArguments = new EngineCommandArgument [ 6 ] ;
2018-12-29 06:30:09 +00:00
2019-01-13 19:55:08 +00:00
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 ) ;
2019-03-08 05:08:15 +00:00
commandArguments [ 4 ] = new EngineCommandArgument ( "-t" , "top patch." , true ) ;
commandArguments [ 5 ] = new EngineCommandArgument ( "-b" , "bottom patch." , true ) ;
2018-12-29 06:30:09 +00:00
}
public override void Run ( string [ ] arguments = null )
{
if ( arguments = = null ) throw new ArgumentException ( "Missing arguments. Type \"help 9p\" for more information." ) ;
int leftBound = 0 , rightBound = 0 , topBound = 0 , bottomBound = 0 ;
string imagePath , outPath ;
2019-01-13 23:12:37 +00:00
if ( IndexOfArgument ( "-i" , arguments ) + 1 > = arguments . Length ) throw new ArgumentException ( "Missing -i path after argument." ) ;
imagePath = arguments [ IndexOfArgument ( "-i" , arguments ) + 1 ] ;
2019-03-21 00:28:16 +00:00
if ( ! File . Exists ( imagePath ) ) throw new ArgumentException ( "Input file does not exist at " + imagePath + "." ) ;
2018-12-29 06:30:09 +00:00
2019-01-13 19:55:08 +00:00
if ( HasArgument ( commandArguments [ 1 ] , arguments ) )
2019-01-13 06:23:44 +00:00
{
2019-01-13 23:12:37 +00:00
if ( IndexOfArgument ( "-o" , arguments ) + 1 > = arguments . Length ) throw new ArgumentException ( "Missing -o path after argument." ) ;
outPath = arguments [ IndexOfArgument ( "-o" , arguments ) + 1 ] ;
2019-01-13 06:23:44 +00:00
} else
{
2019-01-13 19:55:08 +00:00
outPath = imagePath . Substring ( 0 , imagePath . Length - Path . GetExtension ( imagePath ) . Length ) ;
2019-01-13 06:23:44 +00:00
}
2019-01-13 23:12:37 +00:00
if ( IndexOfArgument ( "-l" , arguments ) + 1 > = arguments . Length | | ! int . TryParse ( arguments [ IndexOfArgument ( "-l" , arguments ) + 1 ] , out leftBound ) ) throw new ArgumentException ( "Missing -l argument bound." ) ;
2019-03-08 05:08:15 +00:00
if ( IndexOfArgument ( "-r" , arguments ) + 1 > = arguments . Length | | ! int . TryParse ( arguments [ IndexOfArgument ( "-r" , arguments ) + 1 ] , out rightBound ) ) throw new ArgumentException ( "Missing -r argument bound." ) ;
if ( IndexOfArgument ( "-t" , arguments ) + 1 > = arguments . Length | | ! int . TryParse ( arguments [ IndexOfArgument ( "-t" , arguments ) + 1 ] , out topBound ) ) throw new ArgumentException ( "Missing -u argument bound." ) ;
if ( IndexOfArgument ( "-b" , arguments ) + 1 > = arguments . Length | | ! int . TryParse ( arguments [ IndexOfArgument ( "-b" , arguments ) + 1 ] , out bottomBound ) ) throw new ArgumentException ( "Missing -d argument bound." ) ;
2018-12-29 06:30:09 +00:00
NinePatchData npData = new NinePatchData ( Path . GetFileName ( imagePath ) , leftBound , rightBound , bottomBound , topBound ) ;
string serialized = JsonConvert . SerializeObject ( npData , Formatting . Indented ) ;
2019-03-21 00:28:16 +00:00
File . Move ( imagePath , Path . GetFileNameWithoutExtension ( imagePath ) + "-texture" + Path . GetExtension ( imagePath ) ) ;
2019-01-13 19:55:08 +00:00
File . WriteAllText ( outPath + ".9p" , serialized ) ;
2019-01-13 23:12:37 +00:00
ConsoleUtilities . WriteWrappedLine ( "Done. Written to \"" + outPath + "\" with values: left = " + leftBound + " right = " + rightBound + " top = " + topBound + " bottom = " + bottomBound ) ;
2018-12-29 06:30:09 +00:00
}
}
}