From eef63013e6f921e00f9c30b88f6113fcd740a15f Mon Sep 17 00:00:00 2001 From: Matthew Galpin Date: Wed, 5 Mar 2025 07:55:43 +0000 Subject: [PATCH] Update install.bat Refactor install.bat for improved readability and performance Simplified the structure of the batch script for easier understanding and maintenance. Consolidated related tasks (downloading, compiling, and extracting) into clearer sections. Removed redundant file handling and checks to improve readability. Streamlined the fallback mechanism for VBScript by dynamically generating the script inline. Ensured that temporary files (e.g., .cs, .vbs, .zip) are cleaned up after use. Improved overall code flow and reduced unnecessary complexity. --- install.bat | 187 ++++++++++++++++++++++++++-------------------------- 1 file changed, 92 insertions(+), 95 deletions(-) diff --git a/install.bat b/install.bat index 0758f6877a..450770ed78 100644 --- a/install.bat +++ b/install.bat @@ -1,111 +1,108 @@ @echo off -REM -REM install.bat -REM -REM Download GamePlay dependencies and binaries from github releases and extracts from ZIP -REM -REM Helps prevent repo bloat due to large binary files -REM +REM install.bat - Download and Extract GamePlay dependencies set prefix=https://github.com/gameplay3d/GamePlay/releases/download/v3.0.0 - set filename=gameplay-deps +REM Go to the script's directory +cd /d "%~dp0" + echo Downloading %filename%.zip from %prefix% -%~d0 -cd %~dp0 -> temp.cs ECHO using System; ->> temp.cs ECHO using System.Net; ->> temp.cs ECHO using System.ComponentModel; ->> temp.cs ECHO class Program ->> temp.cs ECHO { ->> temp.cs ECHO static string file = "%filename%.zip"; ->> temp.cs ECHO static string url = "%prefix%/" + file; ->> temp.cs ECHO static bool done = false; ->> temp.cs ECHO static void Main(string[] args) ->> temp.cs ECHO { ->> temp.cs ECHO try ->> temp.cs ECHO { ->> temp.cs ECHO WebClient client = new WebClient(); ->> temp.cs ECHO client.Proxy = null; ->> temp.cs ECHO client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged); ->> temp.cs ECHO client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted); ->> temp.cs ECHO Console.Write("Downloading " + file + ": 0%% "); ->> temp.cs ECHO client.DownloadFileAsync(new Uri(url), file); ->> temp.cs ECHO while (!done) System.Threading.Thread.Sleep(500); ->> temp.cs ECHO } ->> temp.cs ECHO catch (Exception x) ->> temp.cs ECHO { ->> temp.cs ECHO Console.WriteLine("Error: " + x.Message); ->> temp.cs ECHO } ->> temp.cs ECHO } ->> temp.cs ECHO static void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) ->> temp.cs ECHO { ->> temp.cs ECHO Console.Write("\rDownloading " + file + ": " + e.ProgressPercentage + "%% "); ->> temp.cs ECHO } ->> temp.cs ECHO static void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) ->> temp.cs ECHO { ->> temp.cs ECHO Console.WriteLine("\rDownloading " + file + ": Done. "); ->> temp.cs ECHO done = true; ->> temp.cs ECHO } ->> temp.cs ECHO } -> temp1.vbs ECHO WScript.Echo "Downloading using a fallback method. This might take a few minutes." ->> temp1.vbs ECHO Dim strFileURL, strHDLocation ->> temp1.vbs ECHO strFileURL = WScript.Arguments(0) ->> temp1.vbs ECHO strHDLocation = WScript.Arguments(1) ->> temp1.vbs ECHO Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") ->> temp1.vbs ECHO objXMLHTTP.open "GET", strFileURL, false ->> temp1.vbs ECHO WScript.Echo "Sending request..." ->> temp1.vbs ECHO objXMLHTTP.send() ->> temp1.vbs ECHO If objXMLHTTP.Status = 200 Then ->> temp1.vbs ECHO WScript.Echo "Got response. Processing body..." ->> temp1.vbs ECHO Set objADOStream = CreateObject("ADODB.Stream") ->> temp1.vbs ECHO objADOStream.Open ->> temp1.vbs ECHO objADOStream.Type = 1 ->> temp1.vbs ECHO objADOStream.Write objXMLHTTP.ResponseBody ->> temp1.vbs ECHO objADOStream.Position = 0 ->> temp1.vbs ECHO Set objFSO = Createobject("Scripting.FileSystemObject") ->> temp1.vbs ECHO If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation ->> temp1.vbs ECHO Set objFSO = Nothing ->> temp1.vbs ECHO WScript.Echo "Saving result to a file..." ->> temp1.vbs ECHO objADOStream.SaveToFile strHDLocation ->> temp1.vbs ECHO objADOStream.Close ->> temp1.vbs ECHO Set objADOStream = Nothing ->> temp1.vbs ECHO WScript.Echo "Success." ->> temp1.vbs ECHO End if ->> temp1.vbs ECHO Set objXMLHTTP = Nothing -if exist %windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe ( - %windir%\Microsoft.NET\Framework\v2.0.50727\csc temp.cs -) else ( -if exist %windir%\Microsoft.NET\Framework\v4.0.30319\csc.exe ( - %windir%\Microsoft.NET\Framework\v4.0.30319\csc temp.cs +REM Create a C# downloader script +( +echo using System; +echo using System.Net; +echo using System.ComponentModel; +echo class Program +echo { +echo static string file = "%filename%.zip"; +echo static string url = "%prefix%/" + file; +echo static bool done = false; +echo static void Main(string[] args) +echo { +echo try +echo { +echo WebClient client = new WebClient(); +echo client.Proxy = null; +echo client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged); +echo client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted); +echo Console.Write("Downloading " + file + ": 0%% "); +echo client.DownloadFileAsync(new Uri(url), file); +echo while (!done) System.Threading.Thread.Sleep(500); +echo } +echo catch (Exception x) +echo { +echo Console.WriteLine("Error: " + x.Message); +echo } +echo } +echo static void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) +echo { +echo Console.Write("\rDownloading " + file + ": " + e.ProgressPercentage + "%% "); +echo } +echo static void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) +echo { +echo Console.WriteLine("\rDownloading " + file + ": Done. "); +echo done = true; +echo } +echo } +) > temp.cs + +REM Compile and run C# downloader +if exist "%windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe" ( + "%windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe" temp.cs +) else if exist "%windir%\Microsoft.NET\Framework\v4.0.30319\csc.exe" ( + "%windir%\Microsoft.NET\Framework\v4.0.30319\csc.exe" temp.cs ) else ( - goto USE_VBS_AS_FALLBACK -)) -temp.exe -del temp.exe -goto :EXTRACT + call :UseVBScript +) -:USE_VBS_AS_FALLBACK +REM Clean up after C# downloader +if exist temp.exe del temp.exe +del temp.cs +goto :Extract + +:UseVBScript +REM Fallback to VBScript for downloading +echo WScript.Echo "Downloading using a fallback method. This might take a few minutes." > temp1.vbs +echo Dim strFileURL, strHDLocation >> temp1.vbs +echo strFileURL = WScript.Arguments(0) >> temp1.vbs +echo strHDLocation = WScript.Arguments(1) >> temp1.vbs +echo Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") >> temp1.vbs +echo objXMLHTTP.open "GET", strFileURL, false >> temp1.vbs +echo objXMLHTTP.send() >> temp1.vbs +echo If objXMLHTTP.Status = 200 Then >> temp1.vbs +echo Set objADOStream = CreateObject("ADODB.Stream") >> temp1.vbs +echo objADOStream.Open >> temp1.vbs +echo objADOStream.Type = 1 >> temp1.vbs +echo objADOStream.Write objXMLHTTP.ResponseBody >> temp1.vbs +echo objADOStream.Position = 0 >> temp1.vbs +echo Set objFSO = Createobject("Scripting.FileSystemObject") >> temp1.vbs +echo If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation >> temp1.vbs +echo objADOStream.SaveToFile strHDLocation >> temp1.vbs +echo objADOStream.Close >> temp1.vbs +echo WScript.Echo "Success." >> temp1.vbs +echo End If >> temp1.vbs cscript temp1.vbs %prefix%/%filename%.zip %filename%.zip +del temp1.vbs +goto :Extract -:EXTRACT +:Extract echo Extracting %filename%.zip... please standby... -%~d0 -cd %~dp0 -> temp2.vbs ECHO Dim fileName, workingDir ->> temp2.vbs ECHO fileName = WScript.Arguments(0) ->> temp2.vbs ECHO workingDir = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") ->> temp2.vbs ECHO Set objShell = CreateObject("Shell.Application") ->> temp2.vbs ECHO Set objSource = objShell.NameSpace(workingDir ^& "\" ^& fileName).Items() ->> temp2.vbs ECHO Set objTarget = objShell.NameSpace(workingDir ^& "\") ->> temp2.vbs ECHO intOptions = 256 ->> temp2.vbs ECHO objTarget.CopyHere objSource, intOptions + +REM Create VBScript for extraction +echo Dim fileName, workingDir > temp2.vbs +echo fileName = WScript.Arguments(0) >> temp2.vbs +echo workingDir = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") >> temp2.vbs +echo Set objShell = CreateObject("Shell.Application") >> temp2.vbs +echo Set objSource = objShell.NameSpace(workingDir ^& "\" ^& fileName).Items() >> temp2.vbs +echo Set objTarget = objShell.NameSpace(workingDir ^& "\") >> temp2.vbs +echo intOptions = 256 >> temp2.vbs +echo objTarget.CopyHere objSource, intOptions >> temp2.vbs cscript temp2.vbs %filename%.zip -echo Cleaning up... -del temp.cs -del temp1.vbs del temp2.vbs + +REM Clean up +echo Cleaning up... del %filename%.zip echo Done.