rewrite install script (again)

This commit is contained in:
Jonny_Bro (Nikita) 2024-09-14 22:58:28 +05:00
parent 1d1856d5cc
commit 177fa833a5
No known key found for this signature in database
GPG key ID: 3F1ECC04147E9BD8

View file

@ -25,10 +25,14 @@ if (-not $steam_dir) {
# Fallback to searching for Steam.exe on all drives # Fallback to searching for Steam.exe on all drives
if (-not $steam_dir) { if (-not $steam_dir) {
Write-Host "No registry key found. Searching for Steam executable on all drives..." Write-Host "No registry key found. Searching for Steam executable on all drives..."
$drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Name -ge 'C' }
# Get all filesystem drives
$drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Name -match '^[A-Z]$' }
foreach ($drive in $drives) { foreach ($drive in $drives) {
$steam_exe = Get-ChildItem -Path "$($drive.Name):\" -Recurse -Include "Steam.exe" -ErrorAction SilentlyContinue | Select-Object -First 1 # Properly format drive path, avoid additional quotes
$drive_letter = $drive.Name
$steam_exe = Get-ChildItem -Path "$($drive_letter):\" -Recurse -Include "Steam.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($steam_exe) { if ($steam_exe) {
$steam_dir = Split-Path $steam_exe.FullName -Parent $steam_dir = Split-Path $steam_exe.FullName -Parent
@ -61,12 +65,16 @@ if (-not $gmod_dir) {
if (Test-Path $library_folders_path) { if (Test-Path $library_folders_path) {
$library_folders = (Get-Content $library_folders_path) -join "`n" $library_folders = (Get-Content $library_folders_path) -join "`n"
$library_paths = $library_folders -split '(?=\s*"\d+"\s*{)' # Split by each library entry $library_paths = $library_folders -split '(?=\s*"\d+"\s*{)' # Split by each library entry
foreach ($library in $library_paths) { foreach ($library in $library_paths) {
# Extract the library path from the line that contains "path"
if ($library -match '"path"\s*"\s*(.+)\s*"') { if ($library -match '"path"\s*"\s*(.+)\s*"') {
$library_path = $matches[1] # Clean the extracted path to remove extra backslashes or quotes
$library_path = $matches[1] -replace "\\{2,}", "\" # Replace any multiple backslashes with a single backslash
$library_path = $library_path.Trim('"') # Remove any surrounding quotes
} }
# If "4000" (Garry's Mod ID) is found, set the GMod directory
if ($library -match '"4000"') { if ($library -match '"4000"') {
$gmod_dir = Join-Path $library_path "steamapps\common\GarrysMod" $gmod_dir = Join-Path $library_path "steamapps\common\GarrysMod"
break break
@ -85,35 +93,65 @@ Write-Host "GMod installation path: $gmod_dir"
# Confirm Garry's Mod installation path # Confirm Garry's Mod installation path
$GMODPath = Join-Path $gmod_dir "garrysmod" $GMODPath = Join-Path $gmod_dir "garrysmod"
$AddonsPath = Join-Path $GMODPath "addons"
$_text = "Do you want to install in this path? It will completely wipe the previous Beatrun install if you had one!`n$GMODPath (y/n)" $_text = "Do you want to install in this path? It will completely wipe the previous Beatrun install if you had one!`n$GMODPath (y/n)"
$GMODConfirmation = Read-Host $_text $GMODConfirmation = Read-Host $_text
if ($GMODConfirmation -eq "y") { if ($GMODConfirmation -eq "y") {
# Define paths
$TempPath = Join-Path $GMODPath "temp" $TempPath = Join-Path $GMODPath "temp"
$ZIPPath = Join-Path $TempPath "beatrun.zip" $ZIPPath = Join-Path $TempPath "beatrun.zip"
$FolderPath = Join-Path $TempPath "beatrun"
$FolderMainPath = Join-Path $TempPath "beatrun-main" $FolderMainPath = Join-Path $TempPath "beatrun-main"
New-Item -ItemType Directory -Path $TempPath -Force | Out-Null $FolderPath = Join-Path $TempPath "beatrun"
Write-Host "Downloading the archive..."
(New-Object Net.WebClient).DownloadFile("https://github.com/JonnyBro/beatrun/archive/refs/heads/main.zip", $ZIPPath)
Write-Host "Downloaded! Unpacking..."
Expand-Archive $ZIPPath $TempPath -Force
Write-Host "Unpacked successfully!"
Rename-Item $FolderMainPath $FolderPath
Remove-Item $ZIPPath
$confirmation = Read-Host "Do you want to install modules? (Discord + Steam Presence) (y/n)"
if ($confirmation -eq "y") {
$ModulesPath = Join-Path $FolderPath "lua\*"
$NewModulesPath = Join-Path $GMODPath "lua"
Copy-Item -Path $ModulesPath -Destination $NewModulesPath -Force -Recurse
}
$AddonPath = Join-Path $FolderPath "beatrun" $AddonPath = Join-Path $FolderPath "beatrun"
$NewAddonPath = Join-Path $AddonsPath "beatrun" $NewAddonPath = Join-Path $GMODPath "addons\beatrun"
if (Test-Path $NewAddonPath) {
Remove-Item $NewAddonPath -Force -Recurse # Ensure the temporary directory exists
if (-not (Test-Path -Path $TempPath)) {
New-Item -ItemType Directory -Path $TempPath -Force | Out-Null
} }
Move-Item -Path $AddonPath -Destination $NewAddonPath
Remove-Item $TempPath -Force -Recurse # Download Beatrun zip
Write-Host "Beatrun Installed!" Write-Host "Downloading the Beatrun archive..."
try {
(New-Object Net.WebClient).DownloadFile("https://github.com/JonnyBro/beatrun/archive/refs/heads/main.zip", $ZIPPath)
Write-Host "Download successful!"
} catch {
Write-Host "Error: Unable to download Beatrun. Please check your internet connection or URL."
exit
}
# Unzip the archive
Write-Host "Unpacking the archive..."
try {
Expand-Archive -Path $ZIPPath -DestinationPath $TempPath -Force
Write-Host "Unpacking successful!"
} catch {
Write-Host "Error: Failed to unpack the Beatrun archive."
exit
}
# Rename and prepare files
if (Test-Path $FolderMainPath) {
Rename-Item -Path $FolderMainPath -NewName $FolderPath
} else {
Write-Host "Error: Unpacked folder not found."
exit
}
# Remove the downloaded zip file
Remove-Item -Path $ZIPPath -Force
# Confirm the addon directory exists
if (Test-Path $NewAddonPath) {
Write-Host "Previous Beatrun installation found. Removing old installation..."
Remove-Item -Path $NewAddonPath -Recurse -Force
}
# Move the new Beatrun addon
Write-Host "Installing Beatrun..."
Move-Item -Path $AddonPath -Destination $NewAddonPath -Force
# Clean up temporary files
Remove-Item -Path $TempPath -Recurse -Force
Write-Host "Beatrun has been installed successfully!"
} }