tasklist | find "Launcher.exe"
if %errorlevel% equ 0 (
	taskkill /F /IM Launcher.exe
	timeout 2
)

mkdir "%~dp0ICafeCloud\LocalAppData\4Game" 2>nul
mkdir "%~dp0ICafeCloud\LocalAppData\4Game_LLC" 2>nul
mkdir "%~dp0ICafeCloud\ProgramData\4Game" 2>nul
mkdir "%~dp0ICafeCloud\ProgramData\Innova" 2>nul
mkdir "%~dp0ICafeCloud\ProgramFiles-x86\4Game" 2>nul

rmdir "%LocalAppData%\4Game" /S /Q
rmdir "%LocalAppData%\4Game_LLC" /S /Q
rmdir "%ProgramData%\4Game" /S /Q
rmdir "%ProgramData%\Innova" /S /Q
rmdir "%ProgramFiles(x86)%\4Game" /S /Q

mklink /J "%LocalAppData%\4Game" "%~dp0ICafeCloud\LocalAppData\4Game"
mklink /J "%LocalAppData%\4Game_LLC" "%~dp0ICafeCloud\LocalAppData\4Game_LLC"
mklink /J "%ProgramData%\4Game" "%~dp0ICafeCloud\ProgramData\4Game"
mklink /J "%ProgramData%\Innova" "%~dp0ICafeCloud\ProgramData\Innova"
mklink /J "%ProgramFiles(x86)%\4Game" "%~dp0ICafeCloud\ProgramFiles-x86\4Game"

Reg.exe add "HKLM\SOFTWARE\WOW6432Node\4Game LLC\4game_cis" /f /v "OldGamesTaken" /t REG_DWORD /d 0
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\4Game LLC\4game_cis" /f /v "Path" /t REG_SZ /d "%~dp0ICafeCloud\ProgramFiles-x86\4Game\4game_cis"
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\4Game LLC\4game_cis" /f /v "LauncherRegion" /t REG_SZ /d "ru"
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\4Game LLC\4game_cis" /f /v "CodeSignRegion" /t REG_SZ /d "cis"
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\4Game LLC\4game_cis" /f /v "PassedRegionalSplitting" /t REG_DWORD /d 1
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\4Game LLC\4game_cis" /f /v "NeedOldLauncherDataTransfer" /t REG_DWORD /d 0
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\4Game LLC\4game_cis" /f /v "IsInstalledAfterPublisherSplit" /t REG_DWORD /d 1
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\4Game LLC\4game_cis" /f /v "RepairedGameRepositories" /t REG_SZ /d "live;"

Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\4game_cis" /f /v "DisplayName" /t REG_SZ /d "4Game CIS"
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\4game_cis" /f /v "Publisher" /t REG_SZ /d "4Game LLC"
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\4game_cis" /f /v "ShortcutName" /t REG_SZ /d "4Game CIS"
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\4game_cis" /f /v "InstallLocation" /t REG_SZ /d "%~dp0ICafeCloud\ProgramFiles-x86\4Game\4game_cis"
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\4game_cis" /f /v "DisplayIcon" /t REG_SZ /d "%~dp0ICafeCloud\ProgramFiles-x86\4Game\4game_cis\4Game CIS.exe"
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\4game_cis" /f /v "UninstallString" /t REG_SZ /d "\"C:\Windows\System32\cmd.exe\" /c  \"  \"%~dp0ICafeCloud\ProgramFiles-x86\4Game\4game_cis\4Game CIS.exe\" uninstall && rd /s/q \"%~dp0ICafeCloud\ProgramFiles-x86\4Game\4game_cis\" \" "

Reg.exe add "HKCR\forgame-cis" /f /ve /t REG_SZ /d "URL:forgame-cis"
Reg.exe add "HKCR\forgame-cis" /f /v "URL Protocol" /t REG_SZ /d ""
Reg.exe add "HKCR\forgame-cis\DefaultIcon" /f /ve /t REG_SZ /d "4Game CIS.exe,1"
Reg.exe add "HKCR\forgame-cis\shell\open\command" /f /ve /t REG_SZ /d "\"%~dp0ICafeCloud\ProgramFiles-x86\4Game\4game_cis\4Game CIS.exe\" \"%1\""

rem ==== export the per-game 4game_cis_ uninstall entries ====
rem reg export takes no wildcard, so every key gets its own file. Keep each ^ below single.
set "FOURGAME_REGDIR=%~dp0ICafeCloud\Registry"
rem skip when this machine has none, so a good export set is never wiped
reg query "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" 2>nul | findstr /i /r /c:"^HKEY_[^ ]*\4game_cis_" >nul
if errorlevel 1 goto fourgame_export_end
mkdir "%FOURGAME_REGDIR%" 2>nul
2>nul del /f /q "%FOURGAME_REGDIR%\*.reg"
for /f "delims=" %%K in ('reg query "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" 2^>nul ^| findstr /i /r /c:"^HKEY_[^ ]*\4game_cis_"') do (
	for %%N in ("%%K") do reg export "%%K" "%FOURGAME_REGDIR%\%%~nxN.reg" /y
)
:fourgame_export_end

set "shortcut_exe_path=%~dp0ICafeCloud\ProgramFiles-x86\4Game\4game_cis\4Game CIS.exe"
set "shortcut_work_dir=%~dp0ICafeCloud\ProgramFiles-x86\4Game\4game_cis"
set "shortcut_name=4Game CIS"
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()"
