taskkill /f /im EpicGamesLauncher.exe /t

rmdir "%LocalAppData%\EpicGamesLauncher" /S /Q
rmdir "%LocalAppData%\Epic Games" /S /Q
rmdir "%LocalAppData%\EpicOnlineServicesUIHelper" /S /Q
rmdir "%LocalAppData%\FortniteGame" /S /Q
rmdir "%ProgramData%\Epic" /S /Q
rmdir "%ProgramFiles(x86)%\Epic Games" /S /Q
rmdir "%ProgramFiles%\Epic Games" /S /Q
mkdir "%~dp0ICafeCloud\LocalAppData\FortniteGame" 2>nul
mkdir "%~dp0ICafeCloud\LocalAppData\EpicGamesLauncher" 2>nul
mkdir "%~dp0ICafeCloud\LocalAppData\Epic Games" 2>nul
mkdir "%~dp0ICafeCloud\LocalAppData\EpicOnlineServicesUIHelper" 2>nul
mkdir "%~dp0ICafeCloud\ProgramFiles\Epic Games" 2>nul

mklink /J "%LocalAppData%\EpicGamesLauncher" "%~dp0ICafeCloud\LocalAppData\EpicGamesLauncher"
mklink /J  "%LocalAppData%\Epic Games" "%~dp0ICafeCloud\LocalAppData\Epic Games"
mklink /J "%LocalAppData%\EpicOnlineServicesUIHelper" "%~dp0ICafeCloud\LocalAppData\EpicOnlineServicesUIHelper"
mklink /J  "%LocalAppData%\FortniteGame" "%~dp0ICafeCloud\LocalAppData\FortniteGame"
mklink /J  "%ProgramData%\Epic" "%~dp0ICafeCloud\ProgramData\Epic"
mklink /J  "%ProgramFiles(x86)%\Epic Games" "%~dp0ICafeCloud\ProgramFiles(x86)\Epic Games"
REM New Epic launcher installs itself and all games into "%ProgramFiles%\Epic Games" - path is not selectable
mklink /J  "%ProgramFiles%\Epic Games" "%~dp0ICafeCloud\ProgramFiles\Epic Games"

REM New layout keeps the launcher in ICafeCloud\ProgramFiles; fall back to the old package layout - Launcher at package root
REM EPIC_LAUNCHER_DIR  = physical path - used for file-existence checks
REM EPIC_LAUNCHER_CANON = canonical path Epic expects - %ProgramFiles%\Epic Games junction -> same payload.
REM   Writing the canonical path into the protocol handler and the updater service makes the registered
REM   state byte-for-byte match a real install, so the launcher does not try to "repair" it on login.
set "EPIC_LAUNCHER_DIR=%~dp0Launcher"
set "EPIC_LAUNCHER_CANON=%~dp0Launcher"
if exist "%~dp0ICafeCloud\ProgramFiles\Epic Games\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe" (
	set "EPIC_LAUNCHER_DIR=%~dp0ICafeCloud\ProgramFiles\Epic Games\Launcher"
	set "EPIC_LAUNCHER_CANON=%ProgramFiles%\Epic Games\Launcher"
)

REM Epic Games Launcher's protocol
Reg.exe add "HKCR\com.epicgames.launcher" /f /ve /t REG_SZ /d "Epic Games Launcher Link"
Reg.exe add "HKCR\com.epicgames.launcher" /f /v "URL Protocol" /t REG_SZ /d ""
Reg.exe add "HKCR\com.epicgames.launcher\DefaultIcon" /f /ve /t REG_SZ /d "%EPIC_LAUNCHER_CANON%\Portal\Binaries\Win64\EpicGamesLauncher.exe,0"
Reg.exe add "HKCR\com.epicgames.launcher\shell" /f /ve /t REG_SZ /d "open"
Reg.exe add "HKCR\com.epicgames.launcher\shell\open\command" /f /ve /t REG_SZ /d "\"%EPIC_LAUNCHER_CANON%\Portal\Binaries\Win64\EpicGamesLauncher.exe\" %%1"

Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Epic Games" /f
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Epic Games\EpicGamesLauncher" /f
Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Epic Games\EpicGamesLauncher" /v "AppDataPath" /t REG_SZ /d "%~dp0ICafeCloud\ProgramData\Epic\EpicGamesLauncher\Data\\" /f

REM Import EOS registry captured on the IDC server - includes the Version values the new launcher checks;
REM fall back to writing the minimal Path keys for old packages without eos.reg
if exist "%~dp0ICafeCloud\eos.reg" (
	reg import "%~dp0ICafeCloud\eos.reg"
) else (
	Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Epic Games\EOS" /f
	Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Epic Games\EOS\InstallHelper" /v "Path" /t REG_SZ /d "%~dp0ICafeCloud\ProgramFiles(x86)\Epic Games\Epic Online Services\EpicOnlineServicesInstallHelper.exe" /f
	Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Epic Games\EOS\MainService" /v "Path" /t REG_SZ /d "%~dp0ICafeCloud\ProgramFiles(x86)\Epic Games\Epic Online Services\service\EpicOnlineServicesHost.exe" /f
	Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Epic Games\EOS\UIHelper" /v "Path" /t REG_SZ /d "%~dp0ICafeCloud\ProgramFiles(x86)\Epic Games\Epic Online Services\EpicOnlineServicesUIHelper.exe" /f
	Reg.exe add "HKLM\SOFTWARE\WOW6432Node\Epic Games\EOS\UserHelper" /v "Path" /t REG_SZ /d "%~dp0ICafeCloud\ProgramFiles(x86)\Epic Games\Epic Online Services\EpicOnlineServicesUserHelper.exe" /f
)

REM Register real Windows services via sc create so SCM recognizes them - Type/Start/ErrorControl/Security
REM are written by SCM itself. Bare "Reg.exe add" only drops loose values and leaves an incomplete key that
REM SCM does NOT enumerate as an installed service, which makes the launcher pop the "needs admin to enable
REM startup functionality" dialog on login to re-install EpicGamesUpdater. sc delete + reg delete first keeps
REM this idempotent and clears any old reg-only residue from earlier runs.
sc stop   EpicOnlineServices >nul 2>&1
sc delete EpicOnlineServices >nul 2>&1
Reg.exe delete "HKLM\SYSTEM\CurrentControlSet\Services\EpicOnlineServices" /f >nul 2>&1
sc create EpicOnlineServices binPath= "\"%ProgramFiles(x86)%\Epic Games\Epic Online Services\service\EpicOnlineServicesHost.exe\"" start= demand obj= LocalSystem DisplayName= "Epic Online Services"

sc stop   EpicGamesUpdater >nul 2>&1
sc delete EpicGamesUpdater >nul 2>&1
Reg.exe delete "HKLM\SYSTEM\CurrentControlSet\Services\EpicGamesUpdater" /f >nul 2>&1
sc create EpicGamesUpdater binPath= "\"%EPIC_LAUNCHER_CANON%\Portal\Binaries\Win64\EpicGamesUpdater.exe\"" start= demand obj= LocalSystem DisplayName= "Epic Games Updater"
sc description EpicGamesUpdater "Runs the software update installation for Epic Games Launcher"

set "EPIC_GAME_USER_SETTINGS=%~dp0ICafeCloud\LocalAppData\EpicGamesLauncher\Saved\Config\WindowsEditor\GameUserSettings.ini"
if exist "%EPIC_GAME_USER_SETTINGS%" (
	powershell -Command "$content = Get-Content -LiteralPath '%EPIC_GAME_USER_SETTINGS%' -Raw; $newContent = $content -replace 'Enable=True', 'Enable=False'; if ($content -ne $newContent) { Set-Content -LiteralPath '%EPIC_GAME_USER_SETTINGS%' -Value $newContent -NoNewline }"
)

REM old version
set "EPIC_GAME_USER_SETTINGS=%~dp0ICafeCloud\LocalAppData\EpicGamesLauncher\Saved\Config\Windows\GameUserSettings.ini"
if exist "%EPIC_GAME_USER_SETTINGS%" (
	powershell -Command "$content = Get-Content -LiteralPath '%EPIC_GAME_USER_SETTINGS%' -Raw; $newContent = $content -replace 'Enable=True', 'Enable=False'; if ($content -ne $newContent) { Set-Content -LiteralPath '%EPIC_GAME_USER_SETTINGS%' -Value $newContent -NoNewline }"
)
