net stop GameLoopService /y >nul 2>&1
taskkill /f /im GameLoop.exe /t

REM Link GameLoop to the recorded version folder so the game exe path stays fixed
set "gl_rec_file=%~dp0GameLoop\Application\gl_version.txt"
set "gl_rec="
if exist "%gl_rec_file%" set /p gl_rec=<"%gl_rec_file%"

rmdir "%~dp0GameLoop\Application\GameLoop" /S /Q 2>nul
if defined gl_rec mklink /J "%~dp0GameLoop\Application\GameLoop" "%~dp0GameLoop\Application\%gl_rec%"

mkdir "%LocalAppData%\Temp\Tencent" 2>nul
mkdir "%ProgramData%\Tencent" 2>nul
mkdir "%AppData%\Tencent" 2>nul
mkdir "%~dp0GameLoopData" 2>nul
mkdir "%~dp0ICafeCloud\LocalAppData\Temp\Tencent\GameLoop" 2>nul
mkdir "%~dp0ICafeCloud\AppData\Roaming\Tencent\GameLoop" 2>nul

rmdir "%ProgramData%\Tencent\qimei" /S /Q 2>nul
rmdir "%LocalAppData%\Temp\Tencent\GameLoop" /S /Q 2>nul
rmdir "%AppData%\Tencent\GameLoop" /S /Q 2>nul

mklink /J "%ProgramData%\Tencent\qimei" "%~dp0ICafeCloud\ProgramData\Tencent\qimei"
mklink /J "%LocalAppData%\Temp\Tencent\GameLoop" "%~dp0ICafeCloud\LocalAppData\Temp\Tencent\GameLoop"
mklink /J "%AppData%\Tencent\GameLoop" "%~dp0ICafeCloud\AppData\Roaming\Tencent\GameLoop"

Reg.exe add "HKLM\SOFTWARE\Tencent\GameLoop" /f /v "InstallPath" /t REG_SZ /d "%~dp0GameLoop"
Reg.exe add "HKLM\SOFTWARE\Tencent\GameLoop" /f /v "GameLoopData" /t REG_SZ /d "%~dp0GameLoopData"
Reg.exe add "HKLM\SOFTWARE\Tencent\GameLoop" /f /v "ExperimentId" /t REG_SZ /d "Empty"

rem register the protocol handler and uninstall entry first - the export below then carries them
Reg.exe add "HKLM\SOFTWARE\Classes\GameLoop" /f /ve /t REG_SZ /d "Androws class"
Reg.exe add "HKLM\SOFTWARE\Classes\GameLoop" /f /v "URL Protocol" /t REG_SZ /d "%~dp0GameLoop\Application\GameLoopLauncher.exe"
Reg.exe add "HKLM\SOFTWARE\Classes\GameLoop\DefaultIcon" /f /v "DefaultIcon" /t REG_SZ /d "%~dp0GameLoop\Application\GameLoopLauncher.exe,1"
Reg.exe add "HKLM\SOFTWARE\Classes\GameLoop\shell" /f
Reg.exe add "HKLM\SOFTWARE\Classes\GameLoop\shell\open" /f
Reg.exe add "HKLM\SOFTWARE\Classes\GameLoop\shell\open\command" /f /ve /t REG_SZ /d "\"%~dp0GameLoop\Application\GameLoopLauncher.exe\" \"%%1\""

Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\GameLoop" /f /v "DisplayIcon" /t REG_SZ /d "%~dp0GameLoop\Application\Uninstall.exe"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\GameLoop" /f /v "DisplayName" /t REG_SZ /d "GameLoop"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\GameLoop" /f /v "Publisher" /t REG_SZ /d "Hong Kong Gathering Media Limited"
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\GameLoop" /f /v "UninstallString" /t REG_SZ /d "\"%~dp0GameLoop\Application\Uninstall.exe\" --oem-uninstall=0 --uninstall-entry=2"


Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop" /f /v "ClientType" /t REG_QWORD /d 0
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop" /f /v "CanSkipClientType" /t REG_QWORD /d 0
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop" /f /v "Version" /t REG_SZ /d "7.0.19.05"
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\Env" /f /v "installer_locale" /t REG_SZ /d "en"
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\Env\Values" /f /v "LastLnk" /t REG_SZ /d "GameLoop"
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\GameLoop" /f /v "BrandCode" /t REG_QWORD /d 0
set "gl_ver=7.0.167.0"
if defined gl_rec set "gl_ver=%gl_rec%"
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\GameLoop" /f /v "Version" /t REG_SZ /d "%gl_ver%"
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\GameLoop" /f /v "VirtualEngineType" /t REG_QWORD /d 2
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\GameLoop" /f /v "DevicePerfTier" /t REG_SZ /d "0"
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\GameLoop" /f /v "CurrentDpi" /t REG_SZ /d "1.00"
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\GameLoop" /f /v "is_system_tray_mode" /t REG_QWORD /d 0
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\GameLoop" /f /v "StoreWndInScreenIndex" /t REG_QWORD /d 0
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\GameLoop" /f /v "SimulatorHardwareConfig" /t REG_SZ /d "{\"customize_max\":{\"cpu_core\":3,\"memory_size_gb\":2},\"default_tier\":1,\"device_performance_tier\":0,\"high\":{\"cpu_core\":4,\"memory_size_gb\":1},\"low\":{\"cpu_core\":2,\"memory_size_gb\":1},\"mid\":{\"cpu_core\":2,\"memory_size_gb\":1},\"recommend\":{\"cpu_core\":4,\"memory_size_gb\":1}}"
Reg.exe add "HKCU\SOFTWARE\Tencent\GameLoop\Settings\GameLoopSettings" /f /v "InstallerDetectorRulesJson" /t REG_SZ /d ""

rem export the registry snapshot into the payload - ICafeMenuReady.bat imports it back to restore
mkdir "%~dp0Registry" 2>nul
reg export HKLM\SOFTWARE\Tencent\GameLoop "%~dp0Registry\Tencent.reg" /y 2>nul
reg export HKCU\SOFTWARE\Tencent\GameLoop "%~dp0Registry\TencentUser.reg" /y 2>nul
reg export HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\GameLoop "%~dp0Registry\Uninstall.reg" /y 2>nul
reg export HKLM\SOFTWARE\Classes\GameLoop "%~dp0Registry\Classes.reg" /y 2>nul

set "shortcut_exe_path=%~dp0GameLoop\Application\GameLoop\GameLoop.exe"
set "shortcut_work_dir=%~dp0GameLoop\Application\GameLoop"
set "shortcut_name=Gameloop 7"
set "desktop_folder=%USERPROFILE%\Desktop"

powershell -Command "$s=(New-Object -ComObject WScript.Shell).CreateShortcut('%desktop_folder%\%shortcut_name%.lnk'); $s.TargetPath='%shortcut_exe_path%'; $s.WorkingDirectory='%shortcut_work_dir%'; $s.Save()"
