@echo off
if "%~dpnx1"=="" goto error1
echo _____________________________________________________________________
echo Copy a set of working title files...
if exist "title.tmd" if exist "title.tik" if exist "title.cert" goto copyTitleFiles1
goto copyTitleFiles1End
:copyTitleFiles1
copy /Y /b title.tik _title.tik
copy /Y /b title.tmd _title.tmd
copy /Y /b title.cert _title.cert
goto copyTitleFilesEnd
:copyTitleFiles1End
if exist "%~nx1.tmd" if exist "%~nx1.tik" if exist "%~nx1.cert" goto copyTitleFiles2
goto copyTitleFiles2End
:copyTitleFiles2
copy /Y /b "%~nx1.tik" _title.tik
copy /Y /b "%~nx1.tmd" _title.tmd
copy /Y /b "%~nx1.cert" _title.cert
if exist "%~nx1.trailer" copy /Y /b "%~nx1.trailer" _title.trailer
goto copyTitleFilesEnd
:copyTitleFiles2End
set wwpacker_titleFile=:
for %%k in ("*.tik") do if not "%%k"=="_title.tik" set wwpacker_titleFile=%%k
if not "%wwpacker_titleFile%"==":" copy /Y /b "%wwpacker_titleFile%" _title.tik
set wwpacker_titleFile=:
for %%k in ("*.tmd") do if not "%%k"=="_title.tmd" set wwpacker_titleFile=%%k
if not "%wwpacker_titleFile%"==":" copy /Y /b "%wwpacker_titleFile%" _title.tmd
set wwpacker_titleFile=:
for %%k in ("*.cert") do if not "%%k"=="_title.cert" set wwpacker_titleFile=%%k
if not "%wwpacker_titleFile%"==":" copy /Y /b "%wwpacker_titleFile%" _title.cert
set wwpacker_titleFile=:
for %%k in ("*.trailer") do if not "%%k"=="_title.trailer" set wwpacker_titleFile=%%k
if not "%wwpacker_titleFile%"==":" copy /Y /b "%wwpacker_titleFile%" _title.trailer
set wwpacker_titleFile=
:copyTitleFilesEnd

for %%k in ("%~dp0htool.exe") do set wwpacker_htcmd=%%~fsk
for %%k in ("%ComSpec%") do set wwpacker_findcmd=%%~dpk
for %%k in ("%wwpacker_findcmd%find.exe") do set wwpacker_findcmd=%%~fsk
set HToolFile=%wwpacker_htcmd%
set FindCommand=%wwpacker_findcmd%
for %%k in ("_title.tik") do set /A wwpacker_tikFileSize=%%~zk
if %wwpacker_tikFileSize% EQU 676 goto copyTikEndDP
if exist "_title_t.tik" del "_title_t.tik"
ren _title.tik _title_t.tik
set /A wwpacker_tikType=%wwpacker_tikFileSize% %% 0x2a4
set dumpSource=_title_t.tik
if %wwpacker_tikType% EQU 0 (
set /A dumpSourceStart=%wwpacker_tikFileSize% - 0x2a4
set /A dumpSourceEnd=%wwpacker_tikFileSize%
) else (
set /A dumpSourceStart=0
set /A dumpSourceEnd=0x2a4
)
set dumpTarget=_title.tik
set dumpTargetStart=0
set returnLabel=copyTikEndDP
goto dumpPart
:copyTikEndDP
if exist "_title_t.tik" del "_title_t.tik"
for %%k in ("_title.tmd") do set /A wwpacker_tmdFileSize=%%~zk
for /F "tokens=3" %%k in ('%wwpacker_htcmd% /d /b0x1de /e0x1e0 "_title.tmd" ^| %wwpacker_findcmd% " : "') do set /A wwpacker_tmdSize=0x1e4 + 0x%%k * 0x24
if %wwpacker_tmdSize% EQU %wwpacker_tmdFileSize% goto copyTmdEndDP
if exist "_title_t.tmd" del "_title_t.tmd"
ren _title.tmd _title_t.tmd
set dumpSource=_title_t.tmd
set dumpSourceStart=0
set dumpSourceEnd=%wwpacker_tmdSize%
set dumpTarget=_title.tmd
set dumpTargetStart=0
set returnLabel=copyTmdEndDP
goto dumpPart
:copyTmdEndDP
if exist "_title_t.tmd" del "_title_t.tmd"
set wwpacker_tikFileSize=
set wwpacker_tikType=
set wwpacker_tmdFileSize=
set wwpacker_tmdSize=
set HToolFile=
set FindCommand=
set dumpSource=
set dumpSourceStart=
set dumpSourceEnd=
set dumpTarget=
set dumpTargetStart=
set returnLabel=
goto end


REM dumpPart
REM [IN] %HToolFile% - the short-named full path string of HTool application
REM [IN] %FindCommand% - the short-named full path string of find command
REM [IN] %dumpSource% - the source file to be dumped (no quotes)
REM [IN] %dumpSourceStart% - the start offset of source file
REM [IN] %dumpSourceEnd% - the end offset of source file (exclusive)
REM [IN] %dumpTarget% - the target file having the dumped data written in (no quotes)
REM [IN] %dumpTargetStart% - the start offset of target file
REM [IN] %returnLabel% - the label to return when this function ends
:dumpPart
set /A dumpSourceSize=%dumpSourceEnd% - %dumpSourceStart%
:copyingDumpedPartStart
if %dumpSourceSize% LEQ 0 goto copyingDumpedPartEnd
if %dumpSourceSize% GTR 506 ( set /A copyingDumpSize=506
) else set /A copyingDumpSize=%dumpSourceSize%
set /A dumpSourceEnd=%dumpSourceStart% + %copyingDumpSize%
for /F "tokens=3" %%k in ('%HToolFile% /d /b%dumpSourceStart% /e%dumpSourceEnd% "%dumpSource%" ^| %FindCommand% " : "') do set dumpedDataString=%%k
if not exist "%dumpTarget%" echo.> "%dumpTarget%"
%HToolFile% /p /b%dumpTargetStart% "%dumpTarget%" %dumpedDataString% > NUL
set /A dumpTargetStart+=%copyingDumpSize%
set /A dumpSourceStart=%dumpSourceEnd%
set /A dumpSourceSize-=%copyingDumpSize%
goto copyingDumpedPartStart
:copyingDumpedPartEnd
set dumpSourceSize=
set copyingDumpSize=
set dumpedDataString=
goto %returnLabel%


:error1
echo Error! Not enough parameters.
echo You should use the _wwpacker scripts instead of this one.
echo.
pause
goto end


:end
echo.
