2018-12-08 22:26:40 +00:00
using RecrownedAthenaeum.Tools.CommandProcessor ;
2018-12-08 23:06:11 +00:00
using RecrownedAthenaeum.Tools.TextureAtlas ;
2018-12-08 22:26:40 +00:00
using System ;
using System.Collections.Generic ;
2018-12-08 23:06:11 +00:00
using System.IO ;
2018-12-08 22:26:40 +00:00
using System.Text ;
namespace RecrownedAthenaeum.Tools.TextureAtlasTools
{
2018-12-09 06:51:33 +00:00
class TexturePackerCommand : EngineCommand
2018-12-08 22:26:40 +00:00
{
2018-12-08 23:06:11 +00:00
TexturePacker texturePacker ;
2018-12-09 03:28:58 +00:00
2018-12-09 06:51:33 +00:00
public TexturePackerCommand ( ) : base ( "texturepacker" ) { }
public override string Help ( string argument )
2018-12-09 03:28:58 +00:00
{
2018-12-09 07:10:18 +00:00
switch ( argument )
{
case null :
2018-12-09 08:46:54 +00:00
return "Packs a given directory composed of png and jpg files into an atlas. Can also add 9patch properties. Possible arguments are \"-i\", \"-o\", \"-mp\", \"-dau\", and \"-9p\". Refer to \"help\" for more info." ;
2018-12-09 07:10:18 +00:00
case "-i" :
2018-12-09 08:46:54 +00:00
return "-i : Path for input directory containing the textures. Required." ;
2018-12-09 07:10:18 +00:00
case "-o" :
2018-12-09 08:46:54 +00:00
return "-o : Path for output files. Points to non-existent file. Will create texture and definitions file with name. Required." ;
2018-12-09 07:10:18 +00:00
case "-9p" :
2018-12-09 08:46:54 +00:00
return "-9p : Can be used multiple times for defining a 9patch. This parameter requires a name, left patch, right patch, top patch, and bottom patch in the format name,a,b,c,d. Optional." ;
case "-sp" :
return "-sp : starting power for one side of the texture. Default is 8." ;
case "-mp" :
return "-mp : Maximum power for one side of the texture. Default is 8." ;
case "-dau" :
return "-dau : disables automatically upscaling the texture." ;
2018-12-09 07:10:18 +00:00
default :
return argument + " is not a valid argument. Type \"help texturepacker to see general help and list of arguments.\"" ;
}
2018-12-09 03:28:58 +00:00
}
2018-12-09 06:51:33 +00:00
public override void Run ( string [ ] arguments )
2018-12-08 22:26:40 +00:00
{
2018-12-09 08:46:54 +00:00
string path = null ;
int mp = 8 ;
int sp = 8 ;
2018-12-09 06:51:33 +00:00
if ( arguments = = null ) throw new ArgumentException ( "Requires arguments. Type \"help texturepacker\" for more info." ) ;
2018-12-09 08:46:54 +00:00
2018-12-08 23:06:11 +00:00
for ( int i = 0 ; i < arguments . Length ; i + + )
{
if ( arguments [ i ] = = "-i" )
{
2018-12-09 08:46:54 +00:00
if ( i + 1 = = arguments . Length ) throw new ArgumentException ( "-i is not followed by path for input directory." ) ;
path = arguments [ i + 1 ] ;
}
if ( arguments [ i ] = = "-mp" )
{
if ( i + 1 > = arguments . Length | | ! int . TryParse ( arguments [ i + 1 ] , out mp ) ) throw new ArgumentException ( "mp is not followed by maximum power." ) ;
}
if ( arguments [ i ] = = "-sp" )
{
if ( i + 1 > = arguments . Length | | ! int . TryParse ( arguments [ i + 1 ] , out sp ) ) throw new ArgumentException ( "sp is not followed by maximum power." ) ;
2018-12-08 23:06:11 +00:00
}
}
2018-12-09 08:46:54 +00:00
texturePacker = new TexturePacker ( path , mp , sp ) ;
2018-12-09 07:22:23 +00:00
if ( texturePacker ! = null )
2018-12-08 23:06:11 +00:00
{
2018-12-09 08:46:54 +00:00
bool dau = false ;
for ( int i = 0 ; i < arguments . Length ; i + + )
{
if ( arguments [ i ] = = "-dau" ) dau = true ;
}
try
{
texturePacker . Build ( ! dau ) ;
} catch ( InvalidOperationException e )
{
throw new ArgumentException ( e . Message ) ;
}
2018-12-09 07:22:23 +00:00
for ( int i = 0 ; i < arguments . Length ; i + + )
2018-12-08 23:06:11 +00:00
{
2018-12-09 07:22:23 +00:00
if ( arguments [ i ] = = "-9p" )
2018-12-08 23:06:11 +00:00
{
2018-12-09 07:22:23 +00:00
if ( i + 5 > = arguments . Length ) throw new ArgumentException ( "-9p is not followed by proper specifiers for a 9Patch (format: \"-9p textureName,a,b,c,d\" where a, b, c, and d are integers definining the border regions for the 9patch.)" ) ;
string [ ] nPatchArgs = arguments [ i + 1 ] . Split ( ',' ) ;
try
{
texturePacker . SetNinePatch ( nPatchArgs [ 0 ] , int . Parse ( nPatchArgs [ 1 ] ) , int . Parse ( nPatchArgs [ 2 ] ) , int . Parse ( nPatchArgs [ 3 ] ) , int . Parse ( nPatchArgs [ 4 ] ) ) ;
}
catch ( FormatException )
{
throw new ArgumentException ( "-9p argument parameters must be in the format \"-9p textureName,a,b,c,d\" where a, b, c, and d are integers definining the border regions for the 9patch." ) ;
}
2018-12-08 23:06:11 +00:00
}
}
2018-12-09 07:22:23 +00:00
for ( int i = 0 ; i < arguments . Length ; i + + )
2018-12-08 23:06:11 +00:00
{
2018-12-09 07:22:23 +00:00
if ( arguments [ i ] = = "-o" )
{
if ( i + 1 > = arguments . Length ) throw new ArgumentException ( "-o is not followed by path for output files. (eg. path/to/file where file is the name for the atlas.)" ) ;
texturePacker . Save ( Path . GetDirectoryName ( arguments [ i + 1 ] ) , Path . GetFileName ( arguments [ i + 1 ] ) ) ;
return ;
}
2018-12-08 23:06:11 +00:00
2018-12-09 07:22:23 +00:00
if ( i = = arguments . Length - 1 )
{
throw new ArgumentException ( "no -o argument found to specify output." ) ;
}
2018-12-08 23:06:11 +00:00
}
2018-12-09 08:46:54 +00:00
}
else
2018-12-09 07:22:23 +00:00
{
throw new ArgumentException ( "-i not specified." ) ;
2018-12-08 23:06:11 +00:00
}
2018-12-08 22:26:40 +00:00
}
}
}