taskkill /f /im AppMarket.exe /t

rmdir "%~dp0ICafeCloud" /S /Q 2>nul
rem Strip the Android engine - AOW_Rootfs_100, ~5GB - and ui from the idc payload so they are not shipped;
rem GameLoop re-downloads the engine itself from the Tencent CDN.
rem These two dirs live at the idc root, a sibling of the AppMarket folder, not inside it - delete from the root.
rem Also sweep the AppMarket subfolder in case an older layout kept them next to AppMarket.exe.
rmdir "%~dp0AOW_Rootfs_100" /S /Q 2>nul
rmdir "%~dp0ui" /S /Q 2>nul
rmdir "%~dp0AppMarket\AOW_Rootfs_100" /S /Q 2>nul
rmdir "%~dp0AppMarket\ui" /S /Q 2>nul

rem ==== Back up live Tencent user data into the idc payload, ICafeCloud\ ====
rem Only REAL directories are copied. A source that is already a junction, reparse point, is
rem skipped - its data physically lives in the backup already; a missing source is skipped.
rem This prevents "path not found" errors on dangling junctions and "sharing violation" hangs.
call :gameloop_backup "%LocalAppData%\Tencent\TxGameAssistant" "%~dp0ICafeCloud\LocalAppData\Tencent\TxGameAssistant"
call :gameloop_backup "%LocalAppData%\Tencent\QQGuild"         "%~dp0ICafeCloud\LocalAppData\Tencent\QQGuild"
call :gameloop_backup "%ProgramData%\Tencent\qimei"            "%~dp0ICafeCloud\ProgramData\Tencent\qimei"
call :gameloop_backup "%ProgramData%\Tencent\QQPCMgr"          "%~dp0ICafeCloud\ProgramData\Tencent\QQPCMgr"
call :gameloop_backup "%ProgramData%\Tencent\TxGameAssistant"  "%~dp0ICafeCloud\ProgramData\Tencent\TxGameAssistant"
call :gameloop_backup "%AppData%\Tencent\MobileGamePC"         "%~dp0ICafeCloud\AppData\Roaming\Tencent\MobileGamePC"
call :gameloop_backup "%AppData%\Tencent\Androws"              "%~dp0ICafeCloud\AppData\Roaming\Tencent\Androws"

rmdir "%~dp0ICafeCloud\ProgramData\Tencent\TxGameAssistant\Log" /S /Q 2>nul

if not exist "pkg_match_only.dat" ( type nul > "pkg_match_only.dat" )

goto :eof

rem ---- gameloop_backup subroutine: %1 = live source dir, %2 = backup dest ----
:gameloop_backup
if not exist "%~1" goto :eof
rem skip reparse points - junction/symlink: already redirected, data lives in the backup
fsutil reparsepoint query "%~1" >nul 2>&1 && goto :eof
rem robocopy: /XJ skips nested junctions, /R:1 /W:1 tolerates locked files without hanging
robocopy "%~1" "%~2" /E /XJ /R:1 /W:1 /NFL /NDL /NJH /NJS /NP >nul 2>&1
goto :eof
