#
# afz 作成
#
#
#
require "perl\\tools.pl";

# ファイル名
@astrFileName	= ();
# 開始アドレス
@astrStartAdr	= ();
# ファイルサイズ
@astrFileSize	= ();

# 出力用バッファ
@astrOutIndex	= ();
@astrOutData	= ();

# 旧モーション出力数
$sOldMotionNum	= 0;

########
# main #
########
{
  print stderr "make include....\n";

  # nmsld から吐き出されたファイルを開く
  readNmsld();

  # インデックス作成
  makeIndex();

  # データ作成
  makeData();

  # ファイル出力
  output();
}


#######
# sub #
#######
# nmsld により吐き出されたファイルを読み込む
sub readNmsld
{
  open(fp,"$ARGV[0]") || die "cannot open file. $ARGV[0] ";
  @astrList = <fp>;
  close(fp);

  foreach $strList (@astrList){
    $strList =~ s/[\n\r]+//g;
    local(@astrTmp) = split(/\t/,$strList);

    local(@astrName) = split(/\//, $astrTmp[0] );
    $j = @astrName;

    # ファイル名格納
    push( @astrFileName, $astrName[$j-1] );
    # 開始アドレス格納
    push( @astrStartAdr, $astrTmp[1] );
    # ファイルサイズ
    push( @astrFileSize, $astrTmp[2]." - ".$astrTmp[1]." + 1" );
  }
}

# インデックス作成
sub makeIndex()
{
  $iNum = @astrFileName;
  # バイナリロード用
  push( @astrOutIndex, "/// モーションバイナリ情報\n");
  push( @astrOutIndex, "static motionLoadCommon::MotionBinInfo l_aMotionBinInfo[]=\n");
  push( @astrOutIndex, "{\n");

  for($i = 0; $i < $iNum; $i++){
    push( @astrOutIndex, "  \"$astrFileName[$i]\",\t" );
    push( @astrOutIndex, "$astrStartAdr[$i],\t" );
    push( @astrOutIndex, "$astrFileSize[$i],\n" );
  }
  push( @astrOutIndex, "};\n\n");
}
# データ作成
sub makeData()
{
  $iNum = @astrFileName;
  push( @astrOutData, "// ----------- 登録されているアニメーション数 -----------\n");
  push( @astrOutData, "#define kMORPH_ANIM_NUM		".$iNum."\n\n");

  # アニメーション番号
  push( @astrOutData, "// ----------- アニメーションID -----------\n");
  push( @astrOutData, "enum{\n");
  for($i = 0; $i < $iNum; $i++){
    local($strName) = split(/\./,$astrFileName[$i] );
    push( @astrOutData, "  k$strName,\n" );
  }
  push( @astrOutData, "};\n\n");
}


# ファイル出力
sub output()
{
  open(fp,"> $ARGV[1]") || die "cannot create file.";
  print fp @astrOutIndex;
  close(fp);
  open(fp,"> $ARGV[2]") || die "cannot create file.";
  print fp @astrOutData;
  close(fp);
}
