-
Notifications
You must be signed in to change notification settings - Fork 1
/
Installer.ps1
242 lines (188 loc) · 8.09 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
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 configuration file
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 Test-AndRequest-SunshineConfig {
param(
[string]$InitialPath
)
# Check if the initial path exists
if (Test-Path $InitialPath) {
Write-Host "File found at: $InitialPath"
return $InitialPath
}
else {
# Show error message dialog
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.MessageBox]::Show("Sunshine configuration could not be found. Please locate it.", "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"
$fileDialog.InitialDirectory = [System.IO.Path]::GetDirectoryName($InitialPath)
if ($fileDialog.ShowDialog() -eq "OK") {
$selectedPath = $fileDialog.FileName
# Check if the selected path is valid
if (Test-Path $selectedPath) {
Write-Host "File selected: $selectedPath"
return $selectedPath
}
else {
Write-Error "Invalid file path selected."
}
}
else {
Write-Error "Sunshine Configuiration file dialog was canceled or no valid file was selected."
exit 1
}
}
}
# Define the path to the Sunshine configuration file
$confPath = Test-AndRequest-SunshineConfig -InitialPath "C:\Program Files\Sunshine\config\sunshine.conf"
$scriptRoot = Split-Path $scriptPath -Parent
# Get the current value of global_prep_cmd from the configuration file
function Get-GlobalPrepCommand {
# Read the contents of the configuration file into an array of strings
$config = Get-Content -Path $confPath
# 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 {
# Get the current value of global_prep_cmd as a JSON string
$globalPrepCmdJson = Get-GlobalPrepCommand -ConfigPath $confPath
# 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 "*$scriptName*")) {
$filteredCommands += $globalPrepCmdArray[$i]
}
}
return [object[]]$filteredCommands
}
# Set a new value for global_prep_cmd in the configuration file
function Set-GlobalPrepCommand {
param (
# 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 $confPath
# Get the current value of global_prep_cmd as a JSON string
$currentValueJson = Get-GlobalPrepCommand -ConfigPath $confPath
# Convert the new value to a JSON string
$newValueJson = ConvertTo-Json -InputObject $Value -Compress
# 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 $confPath -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 {
# Remove any existing commands that contain the scripts name from the global_prep_cmd value
$globalPrepCmdArray = Remove-Command -ConfigPath $confPath
$command = [PSCustomObject]@{
do = "powershell.exe -executionpolicy bypass -file `"$($scriptPath)`" -n $scriptName"
elevated = "false"
undo = "powershell.exe -executionpolicy bypass -file `"$($scriptRoot)\Helpers.ps1`" -n $scriptName -t 1"
}
# Add the new object to the global_prep_cmd array
[object[]]$globalPrepCmdArray += $command
return [object[]]$globalPrepCmdArray
}
$commands = @()
if ($install -eq 1) {
$commands = Add-Command
}
else {
$commands = Remove-Command
}
if ($settings.installationOrderPreferences.enabled) {
$commands = OrderCommands $commands $settings.installationOrderPreferences.scriptNames
}
Set-GlobalPrepCommand $commands
$sunshineService = Get-Service -ErrorAction Ignore | Where-Object { $_.Name -eq 'sunshinesvc' -or $_.Name -eq 'SunshineService' }
# In order for the commands to apply we have to restart the service
$sunshineService | Restart-Service -WarningAction SilentlyContinue
Write-Host "If you didn't see any errors, that means the script installed without issues! You can close this window."