generated from Nonary/SunshineScriptInstaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstaller.ps1
281 lines (230 loc) · 10 KB
/
Installer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
param(
[Parameter(Position = 0, Mandatory = $true)]
[Alias("n")]
[string]$scriptName,
[Parameter(Position = 1, Mandatory = $true)]
[Alias("i")]
[string]$install
)
Set-Location (Split-Path $MyInvocation.MyCommand.Path -Parent)
$filePath = $($MyInvocation.MyCommand.Path)
$scriptRoot = Split-Path $filePath -Parent
$scriptPath = "$scriptRoot\StreamMonitor.ps1"
. .\Helpers.ps1 -n $scriptName
$settings = Get-Settings
# This script modifies the global_prep_cmd setting in the Sunshine/Apollo configuration files
function Test-UACEnabled {
$key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System'
$uacEnabled = Get-ItemProperty -Path $key -Name 'EnableLUA'
return [bool]$uacEnabled.EnableLUA
}
$isAdmin = [bool]([System.Security.Principal.WindowsIdentity]::GetCurrent().Groups -match 'S-1-5-32-544')
# If the user is not an administrator and UAC is enabled, re-launch the script with elevated privileges
if (-not $isAdmin -and (Test-UACEnabled)) {
Start-Process powershell.exe -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -NoExit -File `"$filePath`" -n `"$scriptName`" -i `"$install`""
exit
}
function Find-ConfigurationFiles {
$sunshineDefaultPath = "C:\Program Files\Sunshine\config\sunshine.conf"
$apolloDefaultPath = "C:\Program Files\Apollo\config\sunshine.conf"
$sunshineFound = Test-Path $sunshineDefaultPath
$apolloFound = Test-Path $apolloDefaultPath
$configPaths = @{}
# If either one is found, use their default paths
if ($sunshineFound) {
$configPaths["Sunshine"] = $sunshineDefaultPath
Write-Host "Sunshine config found at: $sunshineDefaultPath"
}
if ($apolloFound) {
$configPaths["Apollo"] = $apolloDefaultPath
Write-Host "Apollo config found at: $apolloDefaultPath"
}
# Only prompt if neither is found
if (-not $sunshineFound -and -not $apolloFound) {
# Show error message dialog
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.MessageBox]::Show("Neither Sunshine nor Apollo configuration could be found. Please locate a configuration file.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null
# Open file dialog
$fileDialog = New-Object System.Windows.Forms.OpenFileDialog
$fileDialog.Title = "Open sunshine.conf"
$fileDialog.Filter = "Configuration files (*.conf)|*.conf"
if ($fileDialog.ShowDialog() -eq "OK") {
$selectedPath = $fileDialog.FileName
# Check if the selected path is valid
if (Test-Path $selectedPath) {
Write-Host "File selected: $selectedPath"
if ($selectedPath -like "*Apollo*") {
$configPaths["Apollo"] = $selectedPath
} else {
$configPaths["Sunshine"] = $selectedPath
}
}
else {
Write-Error "Invalid file path selected."
exit 1
}
}
else {
Write-Error "Configuration file dialog was canceled or no valid file was selected."
exit 1
}
}
return $configPaths
}
# Find configuration files
$configPaths = Find-ConfigurationFiles
# Save paths to settings
if ($configPaths.ContainsKey("Sunshine")) {
Update-JsonProperty -FilePath "./settings.json" -Property "sunshineConfigPath" -NewValue $configPaths["Sunshine"]
}
if ($configPaths.ContainsKey("Apollo")) {
Update-JsonProperty -FilePath "./settings.json" -Property "apolloConfigPath" -NewValue $configPaths["Apollo"]
}
# Get the current value of global_prep_cmd from the configuration file
function Get-GlobalPrepCommand {
param (
[string]$ConfigPath
)
# Read the contents of the configuration file into an array of strings
$config = Get-Content -Path $ConfigPath
# Find the line that contains the global_prep_cmd setting
$globalPrepCmdLine = $config | Where-Object { $_ -match '^global_prep_cmd\s*=' }
# Extract the current value of global_prep_cmd
if ($globalPrepCmdLine -match '=\s*(.+)$') {
return $matches[1]
}
else {
Write-Information "Unable to extract current value of global_prep_cmd, this probably means user has not setup prep commands yet."
return [object[]]@()
}
}
# Remove any existing commands that contain the scripts name from the global_prep_cmd value
function Remove-Command {
param (
[string]$ConfigPath
)
# Get the current value of global_prep_cmd as a JSON string
$globalPrepCmdJson = Get-GlobalPrepCommand -ConfigPath $ConfigPath
# Convert the JSON string to an array of objects
$globalPrepCmdArray = $globalPrepCmdJson | ConvertFrom-Json
$filteredCommands = @()
# Remove any existing matching Commands
for ($i = 0; $i -lt $globalPrepCmdArray.Count; $i++) {
if (-not ($globalPrepCmdArray[$i].do -like "*$scriptRoot*")) {
$filteredCommands += $globalPrepCmdArray[$i]
}
}
return [object[]]$filteredCommands
}
# Set a new value for global_prep_cmd in the configuration file
function Set-GlobalPrepCommand {
param (
[string]$ConfigPath,
# The new value for global_prep_cmd as an array of objects
[object[]]$Value
)
if ($null -eq $Value) {
$Value = [object[]]@()
}
# Read the contents of the configuration file into an array of strings
$config = Get-Content -Path $ConfigPath
# Get the current value of global_prep_cmd as a JSON string
$currentValueJson = Get-GlobalPrepCommand -ConfigPath $ConfigPath
# Convert the new value to a JSON string - ensure proper JSON types
$newValueJson = ConvertTo-Json -InputObject $Value -Compress -Depth 10
# Fix boolean values to be JSON compliant
$newValueJson = $newValueJson -replace '"elevated"\s*:\s*"true"', '"elevated": true'
$newValueJson = $newValueJson -replace '"elevated"\s*:\s*"false"', '"elevated": false'
# Replace the current value with the new value in the config array
try {
$config = $config -replace [regex]::Escape($currentValueJson), $newValueJson
}
catch {
# If it failed, it probably does not exist yet.
# In the event the config only has one line, we will cast this to an object array so it appends a new line automatically.
if ($Value.Length -eq 0) {
[object[]]$config += "global_prep_cmd = []"
}
else {
[object[]]$config += "global_prep_cmd = $($newValueJson)"
}
}
# Write the modified config array back to the file
$config | Set-Content -Path $ConfigPath -Force
}
function OrderCommands($commands, $scriptNames) {
$orderedCommands = New-Object System.Collections.ArrayList
if($commands -isnot [System.Collections.IEnumerable]) {
# PowerShell likes to magically change types on you, so we have to check for this
$commands = @(, $commands)
}
$orderedCommands.AddRange($commands)
for ($i = 1; $i -lt $scriptNames.Count; $i++) {
if ($i - 1 -lt 0) {
continue
}
$before = $scriptNames[$i - 1]
$after = $scriptNames[$i]
$afterCommand = $orderedCommands | Where-Object { $_.do -like "*$after*" -or $_.undo -like "*$after*" } | Select-Object -First 1
$beforeIndex = $null
for ($j = 0; $j -lt $orderedCommands.Count; $j++) {
if ($orderedCommands[$j].do -like "*$before*" -or $orderedCommands[$j].undo -like "*$before*") {
$beforeIndex = $j
break
}
}
$afterIndex = $null
for ($j = 0; $j -lt $orderedCommands.Count; $j++) {
if ($orderedCommands[$j].do -like "*$after*" -or $orderedCommands[$j].undo -like "*$after*") {
$afterIndex = $j
break
}
}
if ($null -ne $afterIndex -and ($afterIndex -lt $beforeIndex)) {
$orderedCommands.RemoveAt($afterIndex)
$orderedCommands.Insert($beforeIndex, $afterCommand)
}
}
$orderedCommands
}
function Add-Command {
param (
[string]$ConfigPath
)
# Remove any existing commands that contain the scripts name from the global_prep_cmd value
$globalPrepCmdArray = Remove-Command -ConfigPath $ConfigPath
$command = [PSCustomObject]@{
do = "powershell.exe -executionpolicy bypass -file `"$($scriptPath)`" -n $scriptName"
elevated = $false
undo = "powershell.exe -executionpolicy bypass -file `"$($scriptRoot)\UndoScript.ps1`" -n $scriptName"
}
# Add the new object to the global_prep_cmd array
[object[]]$globalPrepCmdArray += $command
return [object[]]$globalPrepCmdArray
}
# Process each found configuration file
foreach ($key in $configPaths.Keys) {
$configPath = $configPaths[$key]
$commands = @()
if ($install -eq 1) {
$commands = Add-Command -ConfigPath $configPath
}
else {
$commands = Remove-Command -ConfigPath $configPath
}
if ($settings.installationOrderPreferences.enabled) {
$commands = OrderCommands $commands $settings.installationOrderPreferences.scriptNames
}
Set-GlobalPrepCommand -ConfigPath $configPath -Value $commands
if ($key -eq "Sunshine") {
$service = Get-Service -ErrorAction Ignore | Where-Object { $_.Name -eq 'sunshinesvc' -or $_.Name -eq 'SunshineService' }
$service | Restart-Service -WarningAction SilentlyContinue
Write-Host "Sunshine configuration updated successfully!"
} elseif ($key -eq "Apollo") {
$service = Get-Service -ErrorAction Ignore | Where-Object { $_.Name -eq 'Apollo Service' }
# Uncomment the line below if you want to automatically restart the service
$service | Restart-Service -WarningAction SilentlyContinue
Write-Host "Apollo configuration updated successfully!"
}
}
Write-Host "If you didn't see any errors, that means the script installed without issues! You can close this window."