-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstaller.ps1
194 lines (149 loc) · 6.91 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
param(
$install
)
$filePath = $($MyInvocation.MyCommand.Path)
$scriptRoot = Split-Path $filePath -Parent
$scriptPath = "$scriptRoot\DisplaySwitch.ps1"
# This script modifies the global_prep_cmd setting in the Sunshine configuration file
# to add a command that runs DisplaySwitch.ps1
# Check if the current user has administrator privileges
$isAdmin = [bool]([System.Security.Principal.WindowsIdentity]::GetCurrent().groups -match 'S-1-5-32-544')
# If the current user is not an administrator, re-launch the script with elevated privileges
if (-not $isAdmin) {
Start-Process powershell.exe -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -NoExit -File `"$filePath`" $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 DisplaySwitch 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 DisplaySwitch Commands
for ($i = 0; $i -lt $globalPrepCmdArray.Count; $i++) {
if (-not ($globalPrepCmdArray[$i].do -like "*DisplaySwitch*")) {
$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
}
# Add a new command to run DisplaySwitch.ps1 to the global_prep_cmd value
function Add-Command {
# Remove any existing commands that contain DisplaySwitch from the global_prep_cmd value
$globalPrepCmdArray = Remove-Command -ConfigPath $confPath
# Read the values from config.txt
$configFilePath = "$scriptRoot\config.txt"
$configContent = Get-Content -Path $configFilePath | ForEach-Object {
$key, $value = $_ -split '='
[PSCustomObject]@{ Key = $key.Trim(); Value = $value.Trim() }
}
$do = $configContent | Where-Object { $_.Key -eq 'do' } | Select-Object -ExpandProperty Value
$undo = $configContent | Where-Object { $_.Key -eq 'undo' } | Select-Object -ExpandProperty Value
# Create a new object with the command to run DisplaySwitch.ps1
$DisplaySwitchCommand = [PSCustomObject]@{
do = "powershell.exe -executionpolicy bypass -WindowStyle Hidden -file `"$($scriptPath)`" $do"
elevated = "false"
undo = "powershell.exe -executionpolicy bypass -WindowStyle Hidden -file `"$($scriptPath)`" $undo"
}
# Add the new object to the global_prep_cmd array
[object[]]$globalPrepCmdArray += $DisplaySwitchCommand
return [object[]]$globalPrepCmdArray
}
$commands = @()
if ($install -eq "True") {
$commands = Add-Command
}
else {
$commands = Remove-Command
}
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."