:: This installer was made for PlanB and have been given full rights and ownership.
:: This installer may only be shared by PlanB for the use of their end users, the end users does not have rights to share this installer to anyone themselves.
:: The developers of PlanB have all rights to edit this code as they see fit without the acknowledgement of any and all other 3rd parties. (in this case the end users)
:: Any and all modifcation to this code without written permission may lead to action being taken.
@echo off
IF EXIST VRChat.exe (GOTO :VRChat) ELSE GOTO :Lost

:VRChat
CLS
echo ^    -------------------------------------------------------------
echo ^    ^|	            PlanB Updater And Installer                 ^|
echo ^    -------------------------------------------------------------
echo:
echo:
echo ^    Use of clients/mods is against VRChat TOS and might get you banned.
echo ^    User discretion is advised when using our client, and users must stay vigilant at all times.
echo ^    And before we forget make sure to have fun. 
echo ^    - Plan B
echo:
pause
set tlsfix=[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
echo ---------------------- Pre-Setup -----------------------
for /f %%i in ('dir /b *_Data') do set data_folder=%%i
echo Cleaning up existing MelonLoader installation
rmdir /S /Q MelonLoader
del winmm.dll
echo:
echo Downloading 7zip...
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell -Command "%tlsfix% Invoke-WebRequest https://github.com/Slaynash/MelonLoaderAutoInstaller/raw/master/7z.exe -OutFile 7z.exe"
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell -Command "%tlsfix% Invoke-WebRequest https://github.com/Slaynash/MelonLoaderAutoInstaller/raw/master/7z.dll -OutFile 7z.dll"
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to download 7z.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	exit

)
echo:
echo ----------------- MelonLoader Install ------------------
echo Downloading MelonLoader from server
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell -Command "%tlsfix% Invoke-WebRequest 'https://drive.google.com/uc?id=1MJG5Sx5OKcl0Evh6BBU-fzVzE1D7oiei&export=download' -OutFile melonloader.zip"
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to download the MelonLoader zip file.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	exit
)
echo Decompressing MelonLoader
7z x melonloader.zip -aoa
del melonloader.zip
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to extract the MelonLoader zip file.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	del melonloader.zip
	exit
)
echo:
echo ----------------- Il2CppDumper Setup ------------------
echo Downloading Il2CppDumper from github
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell -Command "%tlsfix% Invoke-WebRequest https://github.com/Perfare/Il2CppDumper/releases/download/v6.4.12/Il2CppDumper-v6.4.12.zip -OutFile il2cppdumper.zip"
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to download the Il2CppDumper zip file.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	exit
)
echo Decompressing Il2CppDumper
7z x il2cppdumper.zip -oil2cppdumper -aoa
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to extract the Il2CppDumper zip file.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	del il2cppdumper.zip
	exit
)
del il2cppdumper.zip
echo:
echo ----------------- Il2CppAssemblyUnhollower Setup ------------------
echo Downloading Il2CppAssemblyUnhollower from github...
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell -Command "%tlsfix% Invoke-WebRequest https://github.com/knah/Il2CppAssemblyUnhollower/releases/download/v0.4.10.0/Il2CppAssemblyUnhollower.0.4.10.0.zip -OutFile il2cppassemblyunhollower.zip"
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to download the Il2CppAssemblyUnhollower zip file.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	del il2cppdumper.zip
	exit
)
echo Decompressing Il2CppAssemblyUnhollower
7z x il2cppassemblyunhollower.zip -oil2cppassemblyunhollower -aoa
del il2cppassemblyunhollower.zip
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to extract the Il2CppAssemblyUnhollower zip file.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	rmdir /Q /S il2cppdumper
	exit
)
echo:
echo --------------- Il2CppDumper Generation ----------------
echo Downloading Il2CppDumper config
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell -Command "%tlsfix% Invoke-WebRequest https://github.com/Slaynash/MelonLoaderAutoInstaller/raw/master/il2cppdumper_config.json -OutFile il2cppdumper/config.json"
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to download the Il2CppDumper config file.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	rmdir /Q /S il2cppdumper
	exit
)
echo:
echo Running Il2CppDumper
cd il2cppdumper
Il2CppDumper.exe ..\GameAssembly.dll ..\VRChat_Data\il2cpp_data\Metadata\global-metadata.dat
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to generate the assemblies using Il2CppDumper.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	rmdir /Q /S il2cppdumper
	rmdir /Q /S il2cppassemblyunhollower
	exit
)
cd ..
echo:
echo --------- Il2CppAssemblyUnhollower Generation ----------
mkdir il2cppassemblyunhollower_output
echo Running Il2CppAssemblyUnhollower
il2cppassemblyunhollower\AssemblyUnhollower.exe --input=il2cppdumper/DummyDll --output=il2cppassemblyunhollower_output --mscorlib=MelonLoader\Managed\mscorlib.dll
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to unhollow the assemblies using Il2CppAssemblyUnhollower.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	rmdir /Q /S il2cppdumper
	rmdir /Q /S il2cppassemblyunhollower
	rmdir /Q /S il2cppassemblyunhollower_output
	exit
)
echo Copying output to MelonLoader/Managed
%SYSTEMROOT%\System32\robocopy il2cppassemblyunhollower_output MelonLoader\Managed /XC /XN /XO /NFL /NDL /NJH
IF %ERRORLEVEL% LSS 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to copy the generated files to the MelonLoader assembly folder ^(robocopy returned %ERRORLEVEL%^).
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	rmdir /Q /S il2cppdumper
	rmdir /Q /S il2cppassemblyunhollower
	rmdir /Q /S il2cppassemblyunhollower_output
	exit
)
IF %ERRORLEVEL% GEQ 8 (
	echo [31m
	echo CRITICAL ERROR: Failed to copy the generated files to the MelonLoader assembly folder ^(robocopy returned %ERRORLEVEL%^).
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	rmdir /Q /S il2cppdumper
	rmdir /Q /S il2cppassemblyunhollower
	rmdir /Q /S il2cppassemblyunhollower_output
	exit
)
set %ERRORLEVEL%= 0
echo:
echo -------------------- PlanB Install ---------------------
echo Downloading PlanB from server
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell -Command "%tlsfix% Invoke-WebRequest 'https://drive.google.com/uc?id=1a3MeVkwu8RNKqXSjL1GNd49irs83PHaI&export=download' -OutFile PlanB.zip"
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to download PlanB from server.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	rmdir /Q /S il2cppdumper
	rmdir /Q /S il2cppassemblyunhollower
	rmdir /Q /S il2cppassemblyunhollower_output
	exit
)
7z x PlanB.zip -aoa	
IF %ERRORLEVEL% NEQ 0 (
	echo [31m
	echo CRITICAL ERROR: Failed to extract PlanB.
	echo [0m
	echo Press any key to exit . . .
	pause>nul
	echo Cleaning up . . .
	del 7z.exe
	del 7z.dll
	del PlanB.zip
	rmdir /Q /S il2cppdumper
	rmdir /Q /S il2cppassemblyunhollower
	rmdir /Q /S il2cppassemblyunhollower_output
	exit
)
echo:
echo:
echo:
echo:	
echo Cleaning up . . .
del 7z.exe
del 7z.dll
del PlanB.zip
rmdir /Q /S il2cppdumper
rmdir /Q /S il2cppassemblyunhollower
rmdir /Q /S il2cppassemblyunhollower_output
CLS
echo ^    -------------------------------------------------------------
echo ^    ^|                  PlanB is now installed!                   ^|
echo ^    -------------------------------------------------------------
echo:
echo:
echo Press any key to exit . . .
pause>nul
GOTO :END

:Lost
CLS
echo ^    -------------------------------------------------------------
echo ^    ^|	            PlanB Updater And Installer                 ^|
echo ^    -------------------------------------------------------------
echo:
echo:
echo Please place inside VRChat folder and run again
echo Press any key to exit . . .
pause>nul
GOTO :END

:END
DEL PlanBInstaller.bat
exit