Skip to content

Commit

Permalink
scripts - update installer
Browse files Browse the repository at this point in the history
  • Loading branch information
shdwmtr committed Jul 14, 2024
1 parent 2e74ce8 commit 3a2f3b3
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 56 deletions.
208 changes: 152 additions & 56 deletions scripts/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
# https://github.com/SteamClientHomebrew/Millennium/blob/main/scripts/install.ps1
# Copyright (c) 2024 Millennium

# Check if the script is being run as an updater
$IsUpdater = $args -contains "--update"

# write installer state
Write-Output "${BoldPurple}[+]${ResetColor} Running as $IsUpdater"
# Millennium artifacts repository
$apiUrl = "https://api.github.com/repos/SteamClientHomebrew/Millennium/releases"

# Define ANSI escape sequence for bold purple color
$BoldPurple = [char]27 + '[38;5;219m'
Expand All @@ -16,6 +13,34 @@ $BoldLightBlue = [char]27 + '[38;5;75m'

$ResetColor = [char]27 + '[0m'

function Ask-Boolean-Question {
param([bool]$newLine = $true, [string]$question, [bool]$default = $false)

$choices = if ($default) { "[Y/n]" } else { "[y/N]" }
$parsedQuestion = "${BoldPurple}::${ResetColor} $question $choices"
$parsedQuestion = if ($newLine) { "`n$parsedQuestion" } else { $parsedQuestion }

$choice = Read-Host "$parsedQuestion"

if ($choice -eq "") {
$choice = if ($default) { "y" } else { "n" }
}

if ($choice -eq "y" -or $choice -eq "yes") {
$choice = "Yes"
}
else {
$choice = "No"
}

[Console]::CursorTop -= if ($newLine) { 2 } else { 1 }
[Console]::SetCursorPosition(0, [Console]::CursorTop)
[Console]::Write(' ' * [Console]::WindowWidth)
Write-Host "`r${parsedQuestion}: ${BoldLightBlue}$choice${ResetColor}"

return $(if ($choice -eq "yes") { $true } else { $false })
}

function Close-SteamProcess {
$steamProcess = Get-Process -Name "steam" -ErrorAction SilentlyContinue

Expand Down Expand Up @@ -43,7 +68,7 @@ Close-SteamProcess
function ConvertTo-ReadableSize {
param([int64]$size)

$units = @("B", "KiB", "MiB", "GiB")
$units = @("Bytes", "KiB", "MiB", "GiB")
$index = [Math]::Floor([Math]::Log($size, 1024))
$sizeFormatted = [Math]::Round($size / [Math]::Pow(1024, $index), 2, [MidpointRounding]::AwayFromZero)

Expand Down Expand Up @@ -87,7 +112,7 @@ function Show-Progress {
$spaces = " " * $addLength
$backspace = "`b" * $currentDownload.Length

Write-Host -NoNewline "`r$ProgressMessage$spaces${BoldGreen}${backspace}${currentDownload} [$ProgressBar]${ResetColor} $PercentComplete%"
Write-Host -NoNewline "`r$ProgressMessage$spaces${backspace}${currentDownload} ${BoldLightBlue}[$ProgressBar]${ResetColor} $PercentComplete%"
}

function DownloadFileWithProgress {
Expand Down Expand Up @@ -125,65 +150,85 @@ function DownloadFileWithProgress {
}
}

$apiUrl = "https://api.github.com/repos/SteamClientHomebrew/Millennium/releases"
$response = Invoke-RestMethod -Uri $apiUrl -Headers @{ "User-Agent" = "Millennium.Installer/1.0" }
$latestRelease = $response | Where-Object { -not $_.prerelease } | Sort-Object -Property created_at -Descending | Select-Object -First 1

$registrySteamPath = (Get-ItemProperty -Path "HKCU:\Software\Valve\Steam").SteamPath
$customSteamPath = Read-Host "${BoldPurple}[?]${ResetColor} Steam Path (leave blank for default)"

if (-not $registrySteamPath) {
Write-Host "Steam path not found in registry."
exit
}
if (-not $customSteamPath) {
$steamPath = (Get-ItemProperty -Path "HKCU:\Software\Valve\Steam").SteamPath

if ($IsUpdater) {
Write-Output "${BoldPurple}[+]${ResetColor} Updating Millennium..."
$steamPath = $registrySteamPath
}
else {
$customSteamPath = Read-Host "${BoldPurple}[?]${ResetColor} Steam Path (leave blank for default)"

if (-not $customSteamPath) {
$steamPath = $registrySteamPath

[Console]::CursorTop -= 1
[Console]::SetCursorPosition(0, [Console]::CursorTop)
[Console]::Write(' ' * [Console]::WindowWidth)

Write-Output "`r${BoldPurple}[?]${ResetColor} Steam Path (leave blank for default): ${BoldLightBlue}$steamPath${ResetColor}"
if (-not $steamPath) {
Write-Host "Steam path not found in registry."
exit
}
else {
$steamPath = $customSteamPath
}
[Console]::CursorTop -= 1
[Console]::SetCursorPosition(0, [Console]::CursorTop)
[Console]::Write(' ' * [Console]::WindowWidth)

Write-Output "`r${BoldPurple}[?]${ResetColor} Steam Path (leave blank for default): ${BoldLightBlue}$steamPath${ResetColor}"
}
else {
$steamPath = $customSteamPath
}

# Ensure the destination directory exists
if (-not (Test-Path -Path $steamPath)) {
New-Item -ItemType Directory -Path $steamPath
}

$totalBytesFromRelease = ($latestRelease.assets | Measure-Object -Property size -Sum).Sum

function Calculate-Installed-Size {

$installedSize = 0
for ($i = 0; $i -lt $packageCount; $i++) {
$asset = $latestRelease.assets[$i]
$FilePath = Join-Path -Path $steamPath -ChildPath $asset.name

if (Test-Path -Path $FilePath) {
$installedSize += (Get-Item $FilePath).Length
}
}

if ($installedSize -eq 0) {
return -1
}

if ($totalBytesFromRelease - $installedSize -eq 0) {
return "0 Bytes"
}

return ConvertTo-ReadableSize -size ($totalBytesFromRelease - $installedSize)
}

$releaseTag = $latestRelease.tag_name
$packageCount = $latestRelease.assets.Count
Write-Output "`n${BoldPurple}[+]${ResetColor} Packages ($packageCount) ${BoldPurple}Millennium@$releaseTag${ResetColor}`n"

$totalSizeReadable = ConvertTo-ReadableSize -size ($latestRelease.assets | Measure-Object -Property size -Sum).Sum
Write-Output "${BoldPurple}[+]${ResetColor} Total Download Size: $totalSizeReadable"
Write-Output "${BoldPurple}[+]${ResetColor} Total Installed Size: $totalSizeReadable"
$totalSizeReadable = ConvertTo-ReadableSize -size $totalBytesFromRelease
$totalInstalledSize = Calculate-Installed-Size

Write-Output "${BoldPurple}[+]${ResetColor} Total Download Size: $totalSizeReadable"
Write-Output "${BoldPurple}[+]${ResetColor} Total Installed Size: $totalSizeReadable"

if ($totalInstalledSize -ne -1) {
Write-Output "${BoldPurple}[+]${ResetColor} Net Upgrade Size: $totalInstalledSize"
}

# offer to proceed with installation
if (-not $IsUpdater)
{
$choice = Read-Host "`n${BoldPurple}::${ResetColor} Proceed with installation? [Y/n]"
$result = Ask-Boolean-Question -question "Proceed with installation?" -default $true

if ($choice -eq "n" -or $choice -eq "no") {
Write-Output "${BoldPurple}[+]${ResetColor} Installation aborted."
exit
}
if (-not $result) {
Write-Output "${BoldPurple}[+]${ResetColor} Installation aborted."
exit
}

Write-Host "${BoldPurple}::${ResetColor} Retreiving packages..."

$FileCount = $latestRelease.assets.Count

# Iterate through each asset and download it
for ($i = 0; $i -lt $FileCount; $i++) {

$asset = $latestRelease.assets[$i]
Expand All @@ -192,23 +237,74 @@ for ($i = 0; $i -lt $FileCount; $i++) {
DownloadFileWithProgress -Url $downloadUrl -DestinationPath $outputFile -FileNumber ($i + 1) -TotalFiles $FileCount -FileName $asset.name
}

Write-Host "`n`n${BoldPurple}[+]${ResetColor} Installation complete."

Start-Steam -steamPath $steamPath
# This portion of the script is used to configure the millennium.ini file
# The script will prompt the user to enable these features.
Function Get-IniFile ($file) # Based on "https://stackoverflow.com/a/422529"
{
$ini = [ordered]@{}
$section = "NO_SECTION"
$ini[$section] = [ordered]@{}

# future options net yet available
switch -regex -file $file {
"^\[(.+)\]$" {
$section = $matches[1].Trim()
$ini[$section] = [ordered]@{}
}
"^\s*(.+?)\s*=\s*(.*)" {
$name,$value = $matches[1..2]
$ini[$section][$name] = $value.Trim()
}
default { $ini[$section]["<$("{0:d4}" -f $CommentCount++)>"] = $_ }
}
$ini
}

Function Set-IniFile ($iniObject, $Path, $PrintNoSection=$false, $PreserveNonData=$true)
{ # Based on "http://www.out-web.net/?p=109"
$Content = @()
ForEach ($Category in $iniObject.Keys) {
if ( ($Category -notlike 'NO_SECTION') -or $PrintNoSection ) {
$seperator = if ($Content[$Content.Count - 1] -eq "") {} else { "`n" }
$Content += $seperator + "[$Category]";
}

ForEach ($Key in $iniObject.$Category.Keys) {
if ($Key.StartsWith('<')) {
if ($PreserveNonData) {
$Content += $iniObject.$Category.$Key
}
}
else {
$Content += "$Key = " + $iniObject.$Category.$Key
}
}
}
$Content | Set-Content $Path -Force
}

$configPath = Join-Path -Path $steamPath -ChildPath "/ext/millennium.ini"

# check if the config file exists, if not, create it
if (-not (Test-Path -Path $configPath)) {
New-Item -Path $configPath -ItemType File -Force > $null
}

Write-Host "`n`r"
$iniObj = Get-IniFile $configPath
$result = Ask-Boolean-Question -question "Do you want to install developer packages?" -default $false

if (-not $iniObj.Contains("PackageManager")) { $iniObj["PackageManager"] = [ordered]@{} }
$iniObj["PackageManager"]["devtools"] = if ($result) { "yes" } else { "no" }

# $choice = Read-Host "`n${BoldPurple}[?]${ResetColor} Do you want to install developer packages? [y/N]"
# if ($choice -eq "y" -or $choice -eq "yes") {
# Write-Host "yes"
# }

# $choice = Read-Host "${BoldPurple}[?]${ResetColor} Do you want Millennium to auto update? (Recommeded) [Y/n]"
# if ($choice -ne "n" -or $choice -ne "no") {
# Update-Config -Key "autoUpdate" -Value $true -JsonFilePath (Join-Path -Path $steamPath -ChildPath "/ext1/plugins.json")
# }
$result = Ask-Boolean-Question -question "Do you want Millennium to auto update? [Excluding Beta] (Recommeded)" -default $true -newLine $false

# $choice = Read-Host "${BoldPurple}[?]${ResetColor} Do you want to use Millennium's GUI? (no = configure millennium with files only) [Y/n]"
# if ($choice -ne "n" -or $choice -ne "no") {
# Update-Config -Key "gui" -Value $true -JsonFilePath (Join-Path -Path $steamPath -ChildPath "/ext1/plugins.json")
# }
if (-not $iniObj.Contains("Settings")) { $iniObj["Settings"] = [ordered]@{} }
$iniObj["Settings"]["check_for_updates"] = if ($result) { "yes" } else { "no" }


Set-IniFile $iniObj $configPath -PreserveNonData $false

Start-Steam -steamPath $steamPath
Write-Host "`n${BoldPurple}[+]${ResetColor} Installation complete."
73 changes: 73 additions & 0 deletions scripts/update.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This file is part of the Millennium project.
# This script is used to update Millennium on a Windows machine.
# https://github.com/SteamClientHomebrew/Millennium/blob/main/scripts/update.ps1
# Copyright (c) 2024 Millennium

# Millennium artifacts repository
$apiUrl = "https://api.github.com/repos/SteamClientHomebrew/Millennium/releases"

function Close-SteamProcess {
$steamProcess = Get-Process -Name "steam" -ErrorAction SilentlyContinue

if ($steamProcess) {
Stop-Process -Name "steam" -Force
}
}

function Start-Steam {
param([string]$steamPath)

$steamExe = Join-Path -Path $steamPath -ChildPath "Steam.exe"
if (-not (Test-Path -Path $steamExe)) {
exit
}

Start-Process -FilePath $steamExe
}

# Kill steam process before installing
Close-SteamProcess

function DownloadFileWithProgress {
param ([string]$Url, [string]$DestinationPath, [int]$FileNumber, [int]$TotalFiles, [string]$FileName)

try {
$webRequest = [System.Net.HttpWebRequest]::Create($Url)
$webRequest.Method = "GET"
$webRequest.UserAgent = "Millennium.Installer/1.0"

$response = $webRequest.GetResponse()
$responseStream = $response.GetResponseStream()

$fileStream = New-Object System.IO.FileStream($DestinationPath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write, [System.IO.FileShare]::None)
$buffer = New-Object byte[] 8192
$bytesRead = 0

while (($bytesRead = $responseStream.Read($buffer, 0, $buffer.Length)) -gt 0) {
$fileStream.Write($buffer, 0, $bytesRead)
}

$fileStream.Close()
$responseStream.Close()
}
catch {
Write-Host "Error downloading file: $_"
if ($fileStream) { $fileStream.Close() }
if ($responseStream) { $responseStream.Close() }
}
}

$response = Invoke-RestMethod -Uri $apiUrl -Headers @{ "User-Agent" = "Millennium.Installer/1.0" }
$latestRelease = $response | Where-Object { -not $_.prerelease } | Sort-Object -Property created_at -Descending | Select-Object -First 1

$steamPath = (Get-ItemProperty -Path "HKCU:\Software\Valve\Steam").SteamPath

$FileCount = $latestRelease.assets.Count
for ($i = 0; $i -lt $FileCount; $i++) {

$asset = $latestRelease.assets[$i]
$downloadUrl = $asset.browser_download_url
$outputFile = Join-Path -Path $steamPath -ChildPath $asset.name
DownloadFileWithProgress -Url $downloadUrl -DestinationPath $outputFile -FileNumber ($i + 1) -TotalFiles $FileCount -FileName $asset.name
}
Start-Steam -steamPath $steamPath

0 comments on commit 3a2f3b3

Please sign in to comment.