Update Script - Powershell

Supremacy; support/discussion/questions

Moderators: thunderchero, Iceman

Post Reply
User avatar
TempestWales
Ensign
Ensign
Posts: 44
Joined: Sun May 15, 2022 6:16 pm

Update Script - Powershell

Post by TempestWales »

Hi All,

Spent a few hours the other day creating a Powershell script that'll do the leg work for new patches and updating your installation for the Non-CE Client.

You can either Create a Scheduled task to run every X days, or make a Shortcut on your desktop to run when required. Alternatively I set this as the startup Program withing Windows RDWeb so that I can update the game and launch it when connecting to the game via a Browser.

IE
Target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -ExecutionPolicy Bypass -File C:\Scripts\Get-SupremacyUpdate.ps1


You can also run this Passing Variables to the Script itself

IE
.\Get-SupremacyUpdate.ps1 -SupremacyDir "YourInstallFolder" -Force

On first run, it will look for an XML file in the Supremacy Dir that will contain the current patch version, as its not found, it will create that file, pull the latest update and update the XML.
# --------------------------------------------------------
# Script: Get-SupremacyUpdate.ps1
# Author: David Coles
# Contributors: 
# Date: 26/02/2024 13:25:00
# Keywords: Game Updater, Update Scripts, Star Trek Supremacy
# Comments: This updates the Star Trek Supremacy Indevelopment game with patch files from URL Repository
# --------------------------------------------------------
# .\Get-SupremacyUpdate.ps1 -SupremacyDir YourInstallFolder -Force
#Requires -RunAsAdministrator

#region Parameters (SupremacyDir, WebURL, UpdaterPath, Force)
param(
# The folder Supremacy is running from (default is C:\Program Files (x86)\Star Trek - Supremacy)
[Parameter(HelpMessage="The folder you are running Supremacy from")][string]$SupremacyDir = "C:\Program Files (x86)\Star Trek - Supremacy Alpha",
# The URL to pull the Patch from (default is http://download.supremacy.2pixels.net/downloads/)
[Parameter(HelpMessage="The URL to Download Patches")][string]$WebURL = "http://download.supremacy.2pixels.net/downloads/",
# The folder to store downloads in temporarily (default is User TEMP)
[Parameter(HelpMessage="The folder you want this script to download to temporarily")][string]$UpdaterPath = $env:TEMP,
# The folder to Log to (default is Windows User TEMP)
[Parameter(HelpMessage="The folder you want to log this script to")][string]$LogDir = $env:TEMP,
# Is this a forced reinstall?
[Parameter(HelpMessage="Is this a forced reinstall?")][Switch]$Force
)
#endregion

#region Statics
# The filename of the Supremacy download
$filenamefilter = "patch_*.zip"
# Filename
$Filename = "patch_VERSION.zip"
# Filename
$Foldername = "patch_VERSION"
# EXEFilename
$EXEName = "SupremacyClient"
# Logfilename
$Logfile = "$LogDir\Supremacy_update_$(get-date -f yyyy_MM_dd_hh_mm_ss).log"
#endregion

#region Get-CurrentVersion function setup
function Get-CurrentDetails {
 [CmdletBinding()]
 param (
   [string]$XMLFilePath
 )
try {
	$XMLContent = Select-Xml -Path $XMLFilePath -XPath '//Version'
	$Version = $XMLContent.node.InnerXML
}
catch {
	#$XMLFilePath = "c:\temp\Myproject.xml"
	
	$XMLSettings = New-Object System.Xml.XmlWriterSettings
	$XMLSettings.Indent = $true
	
	$XMLWriter = [System.XML.XmlWriter]::Create($XMLFilePath, $XMLSettings)
	$XMLWriter.WriteStartElement("SupremacyUpdater") 
	$XMLWriter.WriteAttributeString("Version", "1.0")
	$XMLWriter.WriteElementString("Version","00000000")
	$XMLWriter.WriteEndElement()
	
	$XMLWriter.Flush()
	$XMLWriter.Close()

	return "00000000"
}
	return $Version
}
#endregion

#region Get-NewVersion function setup
function Get-NewVersion {
	$URLcontent = Invoke-WebRequest $WebURL  -UseBasicParsing
	$URLfilepath = $URLcontent.Links.HREF | Where-Object {$_ -like "*" + $filenamefilter} | Select -First 1
	$URLfilename = Split-Path $URLfilepath -leaf
	$URLversion = $URLfilename -replace "[^0-9]" , ''
	#Write-Host "URLFile: $URLfilename"

return $URLversion
}
#endregion

#region Copy-FileSafer function Setup - Does our File Copying with some checks
function Copy-FileSafer {
 [CmdletBinding()]
 param (
   [string]$path,
   [string]$destinationfolder
 )
 if (-not (Test-Path -Path $path)) {
	throw "File not found: $path" 
 }
 	$sourcefile = Split-Path -Path $path -Leaf
 	$destinationfile = Join-Path -Path $destinationfolder -ChildPath $sourcefile
 try {
	Copy-Item -Path $path -Destination $destinationfolder -Recurse -Force -ErrorAction Stop
 }
 catch {
	throw "File copy failed"
 }
 finally {
	Write-Information -MessageData "File copied successfully" -InformationAction Continue
 }
}
#endregion

#region Set-XMLVersion function setup - Allows us to Set the Version Element
Function Set-XMLVersion {
 [CmdletBinding()]
 param (
	[string]$XMLPath,
	[string]$Node,
	[string]$NodeValue
)
	$XMLFile = New-Object XML
	$XMLFile.Load($XMLPath)
	$XMLElement =  $XMLFile.SelectSingleNode("//$Node")
	$XMLElement.InnerText = $NodeValue
	$XMLFile.Save($XMLPath)

}
#endregion

#region MainScript Generated Variables (UpdateNeeded, SupremacyCurrent.array, SupremacyUpdate.array, TodayDate, Filename, FolderName, FileDownloadURL, ZIPFile)
Start-Transcript -Path $Logfile -NoClobber

[bool]$UpdateNeeded = $false

#Get Current Version from XML File
$SupremacyCurrent = Get-CurrentDetails -XMLFilePath "$SupremacyDir\Updater.xml"
Write-Host "Current Version: $SupremacyCurrent"

#Get Newest Version from Website
$SupremacyUpdate = Get-NewVersion
Write-Host "LatestVersion: $SupremacyUpdate"

#Replce 'VERSION' in File/foldernames with updated version
$Filename = $Filename -replace "VERSION",$SupremacyUpdate
$Foldername = $Foldername -replace "VERSION",$SupremacyUpdate
Write-Host "New Filename: $Filename"

#Construct URL File to Download
$FileDownloadURL = $WebURL+$Filename
Write-Host ("Updated Download URL: $FileDownloadURL")

#Construct Zip File
$ZIPfile = "$UpdaterPath\$Filename"
Write-Host ("Download FilePath: $ZIPfile")

#Compare current version to latest version, define $UpdateNeeded
if ($SupremacyCurrent -ne $SupremacyUpdate) {
$UpdateNeeded = $true
}

$ProcessRunning = Get-Process -Name $EXEName -EA SilentlyContinue
if ($ProcessRunning -eq $null){

	if ($UpdateNeeded -or $Force -and -not $UpdateError){
	#Update is needed, continue
	#Notify of version change and update
	Write-Host ("There is an update pending. Current: " + $SupremacyCurrent + ", Latest: "+$SupremacyUpdate)
	New-Item -Path $UpdaterPath -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
	
	#Download Latest Version to Temp
	Write-Host Dowloading latest release
	Set-Location -Path $UpdaterPath
	$ProgressPreference = 'SilentlyContinue'
	Invoke-WebRequest $FileDownloadURL -Outfile $ZIPfile -UseBasicParsing
	Write-Host Installing updated version
	$ProgressPreference = 'Continue'
	
	#Extract Files and Overwrite
	Write-Host "Extracting Files..."
	Expand-Archive -Path $ZIPfile -DestinationPath $UpdaterPath -Force
	Write-Host "Files Extracted"

	#Unblock EXE
	Unblock-File "$UpdaterPath\$Foldername\$EXEName.exe"
	Write-Host "Unlocked $EXEName"
	
	#Copy Files from Temp to $SupremacyDir
	Write-Host "Copying Files..."
	Copy-FileSafer -Path "$UpdaterPath\$Foldername\*" -Destination $SupremacyDir
	
	#Update Version XML File in Supremacy Directory
	Set-XMLVersion -XMLPath "$SupremacyDir\Updater.xml" -Node "Version" -NodeValue $SupremacyUpdate
	Write-Host "Supremacy has been updated to $SupremacyUpdate"
	
	#Clean Up Temp Files
	Write-Host Cleaning up
	Remove-Item $ZIPfile -Force
	Remove-Item "$UpdaterPath\$Foldername" -Recurse -Force
	
	}
	else{
	#Version is Current - Update not Needed
	Write-Host ("You have the latest version of Supremacy installed - $SupremacyCurrent. Update not required.")
	}
}
else{
	#Update Available but Client is running
	Write-Host ("Update Available for Supremacy but Supremacy Client is running, please close the game and try again")
}
Stop-Transcript
exit $LASTEXITCODE
#endregion
Last edited by TempestWales on Mon Feb 26, 2024 12:12 pm, edited 4 times in total.
Iceman
Admiral
Admiral
Posts: 3318
Joined: Fri Apr 10, 2009 2:00 am

Re: Update Script - Powershell

Post by Iceman »

Thanks TW for your hard work! :up:
I was waiting for Danijel to show up and chime in, he was the one looking into the auto-updater some time ago.
User avatar
Danijel
Lieutenant-Commander
Lieutenant-Commander
Posts: 168
Joined: Tue Mar 06, 2018 9:49 am

Re: Update Script - Powershell

Post by Danijel »

I think Iceman changed paths, so you would need to edit the script, looks nice, didn't test it but if it works for you, there is no reason it shouldn't work for anyone else. I "chimed" that a year and half ago :twisted:
User avatar
TempestWales
Ensign
Ensign
Posts: 44
Joined: Sun May 15, 2022 6:16 pm

Re: Update Script - Powershell

Post by TempestWales »

Well, thats nice changing the paths just after I release this. :-/

Can Easily be amended. Assuming that Patches will be release like previously.
Iceman
Admiral
Admiral
Posts: 3318
Joined: Fri Apr 10, 2009 2:00 am

Re: Update Script - Powershell

Post by Iceman »

I actually changed the struture in the xmas release (23dec). But yes, I deleted the "non-CE" folder (which, like I mentioned, was only kept to allow easier upgrades to 20231223) yesterday, because it was obsolete by now (2 new full releases).
I think the current structure will not change in the future.
User avatar
TempestWales
Ensign
Ensign
Posts: 44
Joined: Sun May 15, 2022 6:16 pm

Re: Update Script - Powershell

Post by TempestWales »

Ah no worries, just means I get to rewrite it and include an install/reinstall feature now.
Iceman
Admiral
Admiral
Posts: 3318
Joined: Fri Apr 10, 2009 2:00 am

Re: Update Script - Powershell

Post by Iceman »

Sounds good. :up:
Iceman
Admiral
Admiral
Posts: 3318
Joined: Fri Apr 10, 2009 2:00 am

Re: Update Script - Powershell

Post by Iceman »

Updated the code in the OT.
User avatar
TempestWales
Ensign
Ensign
Posts: 44
Joined: Sun May 15, 2022 6:16 pm

Re: Update Script - Powershell

Post by TempestWales »

Iceman wrote: Tue Feb 20, 2024 6:11 pm Updated the code in the OT.
Thanks for updating, (Spelling errors too! lol!) I am in the process of adding some more features, but I'm also in the Process of a Datacentre move with work, so have had little time to devote to getting it finished.
Iceman
Admiral
Admiral
Posts: 3318
Joined: Fri Apr 10, 2009 2:00 am

Re: Update Script - Powershell

Post by Iceman »

:up:
Thanks for your work!
User avatar
TempestWales
Ensign
Ensign
Posts: 44
Joined: Sun May 15, 2022 6:16 pm

Re: Update Script - Powershell

Post by TempestWales »

I've updated my code to that prevented downloading the new patch. I'll also be updating it in the near future with a few more features.
Post Reply

Return to “Supremacy”