libGDXのTexturePackerで作った.atlasファイルをcocos2dxで読み込める.plistに変更するperlスクリプト

第一引数：.atlasファイル、
第二引数：出力ファイルディレクトリ

```pl:convert-plist.pl
sub makeValue {
  my $arg = $_[0];
  chomp $arg;
  #print $arg . "\n";
  $arg = substr($arg, index($arg, ":")+1);
  #print $arg . "\n";
  $arg =~ s/[\s]+//g;;
  if( index($arg, ",") >= 0 ){
    $arg = "{" . $arg . "}";
  }
  #print $arg . "\n";
  return $arg;  
}


my $src = $ARGV[0];
my $outdir = $ARGV[1];
my $fhOut;
my $lastSlash=rindex($src, "/");
my $lastDot=rindex($src,".");
my $pkgname=substr($src,$lastSlash+1,$lastDot-$lastSlash-1);
$pkgname=~ s/[_]/\//g;

open(my $fh, "<", $src)
  or die "Cannot open $now: $!";

while(my $line = readline $fh){ 
    # chomp関数で、改行を取り除く
    chomp $line;
    if( 0 == length($line) ) {
      my $textureName = readline $fh;
      chomp $textureName;
      my $textureSize = readline $fh;
      my $textureFormat = readline $fh;
      my $textureFilter = readline $fh;
      my $textureRepeat = readline $fh;
      my $plistName = substr($textureName, 0, index($textureName, ".")) . ".plist";

      if( defined($fhOut) ){
        print $fhOut "    </dict>\n";
        print $fhOut "  </dict>\n";
        print $fhOut "</plist>\n";
        close($fhOut);
      }
      my $filename = $outdir . "/" . $plistName;
      print $filename . "\n";
      open $fhOut, ">", $filename;      
      
      print $fhOut "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
      print $fhOut "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
      print $fhOut "<plist version=\"1.0\">\n";
      print $fhOut "  <dict>\n";
      print $fhOut "    <key>metadata</key>\n";
      print $fhOut "    <dict>\n";
      print $fhOut "      <key>format</key>\n";
      print $fhOut "      <integer>2</integer>\n";
      print $fhOut "      <key>realTextureFileName</key>\n";
      print $fhOut "      <string>" . $textureName . "</string>\n";
      print $fhOut "      <key>size</key>\n";
      print $fhOut "      <string>" . makeValue($textureSize) ."</string>\n";
      print $fhOut "      <key>textureFileName</key>\n";
      print $fhOut "      <string>" . $textureName . "</string>\n";
      print $fhOut "    </dict>\n";
      print $fhOut "    <key>frames</key>\n";
      print $fhOut "    <dict>\n";

    # : がなければkey
    }elsif( 0 > index($line, ":") ){
      my $pngname = $pkgname . "/" . $line . ".png";
      my $rotate = readline $fh;
      my $xy = readline $fh;
      my $size = readline $fh;
      my $orig = readline $fh;
      my $offset = readline $fh;
      my $index = readline $fh;
      
      print $fhOut "      <key>" . $pngname . "</key>\n";
      print $fhOut "      <dict>\n";
      print $fhOut "        <key>frame</key>\n";
      print $fhOut "        <string>{" . makeValue($xy) . "," . makeValue($size) . "}</string>\n";
      print $fhOut "        <key>offset</key>\n";
      print $fhOut "        <string>" . makeValue($offset) . "</string>\n";
      print $fhOut "        <key>rotated</key>\n";
      print $fhOut "        <" . makeValue($rotate) . "/>\n";
      print $fhOut "        <key>sourceColorRect</key>\n";
      print $fhOut "        <string>{{0,0}," . makeValue($orig) . "}</string>\n";
      print $fhOut "        <key>sourceSize</key>\n";
      print $fhOut "        <string>" . makeValue($orig) . "</string>\n";
      print $fhOut "      </dict>\n";
    }
    else{
      # print $line . "\n";
    }
}

close $fh;
if( defined($fhOut) ){
  print $fhOut "    </dict>\n";
  print $fhOut "  </dict>\n";
  print $fhOut "</plist>\n";
  close($fhOut);
}


```