-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSKSS.ps1
More file actions
449 lines (374 loc) · 18.1 KB
/
Copy pathSKSS.ps1
File metadata and controls
449 lines (374 loc) · 18.1 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<#
.SYNOPSIS
Starts an application and automatically starts and stops Special K's global injection with it.
.DESCRIPTION
This script will automatically start Special K's global injection and your defined app. After the app is closed it also automatically ends Special K's global injection. It also comes with a little wrapper batch, that will run this scrip more conveniently and is required to run this from Steam.
.NOTES
If you want to use this within Steam follow these steps:
1) Put the wrapper and script in Steams directory (the one with steam.exe)
2) Set your app's launch options in Steam to SKSS.bat %command% <startup parameters> e.g. SKSS.bat %command% -ipv4. Steam replaces %command% with the full path to the started app and any launch option it itself uses (like connecting to a friends lobby).
You can also drag & Drop an executable at the wrapper batch.
Special K Starter Script
Copyright (C) 2022 Spodi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
.EXAMPLE
SKSS.bat "D:\SteamLibrary\steamapps\common\Monster Hunter World\MonsterHunterWorld.exe" -ipv4 anotheroption
Starts MonsterHunterWorld.exe with launch options set to: -ipv4 anotheroption
Equivalent for launch options for Monster Hunter World in Steam:
SKSS.bat %command% -ipv4 anotheroption
.EXAMPLE
SKSS.bat "D:\SteamLibrary\steamapps\common\Monster Hunter World\MonsterHunterWorld.exe" -Test """Test""" -ipv4"
Uses automatic whitelisting and starts MonsterHunterWorld.exe with launch options set to: -Test "Test" -ipv4
Equivalent for launch options for Monster Hunter World in Steam:
SKSS.bat %command% -Test """Test""" -ipv4
.EXAMPLE
SKSS.bat "D:\SteamLibrary\steamapps\common\Monster Hunter World\MonsterHunterWorld.exe" -Whitelist -SK_AppParams "-SK_AutoStop """Test""" -ipv4"
Starts MonsterHunterWorld.exe with launch options set to: -ipv4 -SK_AutoStop "Test"
No equivalent in Steam!
.COMPONENT
Requires Module SpecialK_PSLibrary
.PARAMETER SK_StartApp
[MANDATORY]
Specifies the App to Start
The parameter name (-SK_StartApp) can be omitted when used as the first parameter.
.PARAMETER SK_AppParams
Specifies startup parameters for the started app.
The parameter name (-SK_AppParams) can be omitted when used as the second parameter. When the name is omitted, any parameter not used by the script will be passed to the started app. If any parameter normally parsed by this script should be sent to the started app instead, the name can not be omitted.
Use tripple quotes (""") instead of normal ones ("), if your app needs them in its own parameters!
DON'T use the named version (-SK_AppParams) in Steams launch options! That will break, if Steam also adds parameters by itself (like connecting to a friends lobby)!
.PARAMETER SK_InstallPath
Specifies the path to your Special K installation.
This is the folder with SpecialK32.dll and SpecialK64.dll. System variables are working here. ${env:USERPROFILE} will be replaced with your user folder e.g. C:\Users\DonaldDuck.
When not specified the script checks the following places in this order: Working Directory, Script Root, default home dir of SK (Documents\My Mods\SpecialK\)
Edit $SK_InstallPath inside the Script file to override the default value (so you don't have to type this parameter every time).
.PARAMETER SK_InjectOther
Specifies the full path to a different directory (with trailing backslash "\") that should be injected . Whitelists this directory instead of the directory of the started app. Blacklists the directly started app.
Use this if the service stops prematurely because Special K activated in a launcher or if automatic whitelisting fails to pick the right folder.
.PARAMETER SK_AutoStop
- injected (Default)
Waits until the app and all child processes are closed instead waiting until Special K is active and rendered its first frame before closing this script. Useful if you're using this for a non-Steam game added to Steam and want to keep the game in your status. Steam ususally will only display you ingame, as long this window is open.
- Exit
Same as above, but also doesn't stop the injection service until the script ends. Keeps the service running even when the app is restarted trough Special K or the app itself. Useful when Special K activates in a launcher and the service stops prematurly or you have to regularly auto-restart the app.
The console needs to be kept open until the started app exited, so it can actually stop the service.
.PARAMETER SK_AsAdmin
Starts the service with elevated rights (Admin).
In this mode the console needs to be kept open when the service was already running until the started app exited to restart the service with normal privileges.
.PARAMETER SK_AdminMode
Internal parameter that is used with "-SK_AsAdmin".
Do NOT use this to start the script!
.PARAMETER SK_WorkingDirectory
Internal parameter that normally is used with "-SK_AsAdmin".
You can use this to set the working directory of the script.
.PARAMETER SK_Help
Shows this help in a new window. Essentally the same as Get-Help <ScriptName> -ShowWindow.
Only works if called from a terminal.
#>
param([CmdletBinding(PositionalBinding = $false, DefaultParameterSetName = 'AutoMode')]
[Parameter(ParameterSetName = 'AutoMode', Position = 0, Mandatory)] [string] $SK_StartApp,
[Parameter(ParameterSetName = 'AutoMode', Position = 1, ValueFromRemainingArguments)] [string[]] $SK_AppParams,
[Parameter(ParameterSetName = 'AutoMode')] [string] $SK_WorkingDirectory,
[Parameter(ParameterSetName = 'AutoMode')] [string] $SK_InjectOther,
[Parameter(ParameterSetName = 'AutoMode')] [ValidateSet('Injected', 'Exit')] [string] $SK_AutoStop,
[Parameter(ParameterSetName = 'AutoMode')] [int] $SK_InjectDelay,
[Parameter(ParameterSetName = 'AdminMode', Mandatory)] [string] $SK_AdminMode,
[Parameter(ParameterSetName = 'ServiceControl')] [Parameter(ParameterSetName = 'AutoMode')] [switch] $SK_AsAdmin,
[Parameter(ParameterSetName = 'ServiceControl')] [Parameter(ParameterSetName = 'AutoMode')] [string]
# ------------------------------ <SETTINGS> ------------------------------
# Edit this to set a default value for the path to your Special K installation.
# DEFAULT: $SK_InstallPath = [Environment]::GetFolderPath('MyDocuments') + '\My Mods\SpecialK'
$SK_InstallPath = ''
# Do NOT change anything below!
# ------------------------------ </SETTINGS> -----------------------------
,
[Parameter(ParameterSetName = 'Help')] [Alias('?', 'h')] [switch] $Help
)
#region -------------------------- <FUNCTIONS> -----------------------------
function IsAdministrator {
$Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = New-Object System.Security.Principal.WindowsPrincipal($Identity)
$Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
function IsUacEnabled {
(Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System).EnableLua -ne 0
}
#endregion ----------------------- </FUNCTIONS> ----------------------------
#Importing file for AdminMode parameters
if ($SK_AdminMode) {
Set-Location $SK_AdminMode
if (Test-Path 'temp_token' -PathType 'Leaf') {
$tokens = Get-Content 'temp_token' | ConvertFrom-Json
$tokens | Get-Member -MemberType NoteProperty, Property | Select-Object -ExpandProperty Name | ForEach-Object {
if ($tokens.$_.IsPresent) {
Write-Host "$_ = `@{IsPresent=$true}"
Set-Variable -Name $_ -Value $true
}
else {
Write-Host "$_ = $($tokens.$_)"
Set-Variable -Name $_ -Value $tokens.$_
}
}
}
else { throw }
}
if ((Test-Path 'temp_token' -PathType 'Leaf') -and (! $Sk_AdminMode)) {
Remove-Item 'temp_token'
}
if ($SK_WorkingDirectory) {
Set-Location $SK_WorkingDirectory
}
if (!$PSScriptRoot) {
$ScriptRoot = (Get-Location).Path
} else {
$ScriptRoot = $PSScriptRoot
}
Import-Module (Join-Path $ScriptRoot 'SpecialK_PSLibrary.psm1')
Add-Type -Name ConsoleUtils -Namespace WPIA -MemberDefinition @'
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'@
$ConsoleMode = @{
Hide = 0
Normal = 1
Minimize = 2
Maximize = 3
NormalNoActivate = 4
Show = 5
MinimizeShowNext = 6
MinimizeNoActivate = 7
ShowNoActivate = 8
Restore = 9
Default = 10
ForceMinimize = 11
}
$hWnd = [WPIA.ConsoleUtils]::GetConsoleWindow()
#Splash
Write-Host -NoNewline -ForegroundColor 'White' 'S'
Write-Host -NoNewline -ForegroundColor 'Gray' 'pecial '
Write-Host -NoNewline -ForegroundColor 'White' 'K '
Write-Host -NoNewline -ForegroundColor 'White' 'S'
Write-Host -NoNewline -ForegroundColor 'Gray' 'tarter '
Write-Host -NoNewline -ForegroundColor 'White' 'S'
Write-Host -ForegroundColor 'Gray' 'cript'
Write-Host 'v25.08.11
'
Write-Host -NoNewline -ForegroundColor 'White' 'S'
Write-Host -NoNewline -ForegroundColor 'Gray' 'pecial '
Write-Host -ForegroundColor 'White' 'K'
$Versions = Get-SkDll -SkInstallPath $SK_InstallPath
Write-Host "32Bit v$(($Versions | Where-Object 'Name' -EQ 'SpecialK32.dll').VersionInfo.ProductVersion) | 64Bit v$(($Versions | Where-Object 'Name' -EQ 'SpecialK64.dll').VersionInfo.ProductVersion)
"
Remove-Variable 'Versions'
$IsRunning = $null
$ServicePath = $null
$ServiceProcess = $null
if (! $injectedDlls) {
$injectedDlls = (Get-SkTeardownStatus | Get-SkInjectedProcess -InjectorOnly).SkModule | Sort-Object -Unique
}
if ($injectedDlls) {
$Path = Split-Path $injectedDlls | Sort-Object -Unique
$ServiceProcess = Invoke-Command {
if ($injectedDlls -like '*32.dll') { $Path | ForEach-Object { Get-SkServiceProcess -Bitness 32 -Path $_ } }
if ($injectedDlls -like '*64.dll') { $Path | ForEach-Object { Get-SkServiceProcess -Bitness 64 -Path $_ } }
} | ForEach-Object { Split-Path $_.Path } | Sort-Object -Unique
}
if ($ServiceProcess) {
$ServicePath = Split-Path $ServiceProcess | Get-Item
$IsRunning = $true
if ((@($ServiceProcess).Count) -gt 1) {
Write-Warning "Multiple different SK installations are running, only the first one will be restarted:$ServicePath | ForEach-Object {"`r`n$_"})"
}
}
else { $ServicePath = Get-SkPath $SK_InstallPath }
if ($SK_AsAdmin) {
if (!(IsAdministrator)) {
if (IsUacEnabled) {
$argList = @('-NoLogo', '-ExecutionPolicy Bypass', "-File `"$PSCommandPath`"")
$argList += "-SK_AdminMode `"$(Get-Location)`""
#Screw it, I will use a file for parameters, or else qoting is breaking things left and right
$tokenFile = $MyInvocation.BoundParameters
if ($SK_AppParams) { $tokenFile['SK_AppParams'] = $SK_AppParams }
if ($injectedDlls) { $tokenFile['injectedDlls'] = $injectedDlls }
[void]$tokenFile.Remove('SK_AsAdmin')
$tokenFile | ConvertTo-Json | Out-File 'temp_token'
Remove-Variable 'tokenFile'
Write-Host 'Elevating Script...'
[void][WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.Hide)
[void][WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.MinimizeNoActivate)
[void][WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.Hide)
try {
Start-Process PowerShell.exe -PassThru -Verb Runas -WorkingDirectory (Get-Location).Path -ArgumentList $argList -ErrorAction 'Stop' | Wait-Process
Remove-Variable 'argList'
if (Test-Path 'temp_token' -PathType 'Leaf') {
Remove-Item 'temp_token'
}
}
finally {}
[void][WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.ShowNoActivate)
[void][WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.NormalNoActivate)
Write-Host 'Returned to normal privileges.'
}
else {
throw 'You must be administrator to run this script with -SK_AsAdmin'
}
}
}
# Help pop-up, if there is no app to start
if ($SK_Help) {
Get-Help -ShowWindow $PSCommandPath
Write-Host 'Closing this will also close the help window.'
Write-Host 'You can close this when you finished reading.'
Pause
exit 0
}
elseif ((! $SK_StartApp)) {
Get-Help $PSCommandPath
Pause
exit 0
}
$SK_StartAppPath = (Split-Path $SK_StartApp -Parent)
if (! $SK_StartAppPath) {
$SK_StartAppPath = (Get-Location).Path
}
if ($SK_StartApp -match ':\/\/') {
$SK_StartAppPath = $null
}
if (! $SK_AsAdmin) {
#only run if you don't spawn the second instance
if ($SK_AdminMode -and $IsRunning) {
Write-Host 'Stopping unelevated global injection service...'
Stop-SkService -SkInstallPath $SK_InstallPath
}
elseif ($IsRunning) {
if (($ServicePath -ne (Get-SkPath $SK_InstallPath)) -or ((@($ServiceProcess).Count) -gt 1)) {
Write-Host 'Stopping other global injection services...'
Stop-SkService -SkInstallPath $SK_InstallPath
}
}
#Whitelist handling
$WhitelistItem = $SK_StartAppPath
if ($SK_InjectOther) {
$WhitelistItem = $SK_InjectOther
$BlacklistItem = $SK_StartAppPath
}
$WhitelistWritten = $null
$BlacklistWritten = $null
if ($WhitelistItem) {
Write-Host 'Whitelisting' $WhitelistItem
$WhitelistWritten = Add-SkList -Type 'allow' $WhitelistItem -SkInstallPath $SK_InstallPath
}
if ($BlacklistItem) {
Write-Host 'Blacklisting' $BlacklistItem
$BlacklistWritten = Add-SkList -Type 'deny' $BlacklistItem -SkInstallPath $SK_InstallPath
}
if (!$SK_InjectDelay) {
Write-Host 'Starting global injection service...'
Start-SkService -SkInstallPath $SK_InstallPath
}
Write-Host "Starting `"$SK_StartApp`" $SK_AppParams"
$jobInput = New-Object PSObject
Add-Member -InputObject $jobInput -MemberType 'NoteProperty' -Name 'StartApp' -Value $SK_StartApp
Add-Member -InputObject $jobInput -MemberType 'NoteProperty' -Name 'StartAppPath' -Value $SK_StartAppPath
Add-Member -InputObject $jobInput -MemberType 'NoteProperty' -Name 'AppParams' -Value $SK_AppParams
Add-Member -InputObject $jobInput -MemberType 'NoteProperty' -Name 'WorkingDir' -Value (Get-Location).Path
$StarterRunspace = [runspacefactory]::CreateRunspace()
$StarterRunspace.Open()
$StarterRunspace.SessionStateProxy.SetVariable('jobInput', $jobInput)
$StarterPowershell = [powershell]::Create()
$StarterPowershell.Runspace = $StarterRunspace
[void]$StarterPowershell.AddScript({
function Start-ProcessSK {
<#
.SYNOPSIS
Just for simpler use later, because PS doesn't like empty arguments by deafult.
#>
param(
[string][Parameter(Mandatory, Position = 0)]$FilePath,
[string[]][Parameter(Position = 1)][AllowEmptyString()]$ArgumentList,
[string][AllowEmptyString()]$WorkingDirectory
)
if ($ArgumentList -and $WorkingDirectory) {
Start-Process -Wait -FilePath $FilePath -WorkingDirectory $WorkingDirectory -ArgumentList $ArgumentList
}
elseif ($ArgumentList) {
Start-Process -Wait -FilePath $FilePath -ArgumentList $ArgumentList
}
elseif ($WorkingDirectory) {
Start-Process -Wait -FilePath $FilePath -WorkingDirectory $WorkingDirectory
}
else {
Start-Process -Wait -FilePath $FilePath
}
}
Set-Location $jobInput.WorkingDir
Start-ProcessSK -FilePath $jobInput.StartApp -WorkingDirectory $jobInput.StartAppPath -ArgumentList $jobInput.AppParams # -ErrorAction 'Stop'
})
$WaiterRunspace = [runspacefactory]::CreateRunspace()
$WaiterRunspace.Open()
$WaiterRunspace.SessionStateProxy.SetVariable('Path', $ScriptRoot)
$WaiterRunspace.SessionStateProxy.SetVariable('SK_AutoStop', $SK_AutoStop)
$WaiterPowershell = [powershell]::Create()
$WaiterPowershell.Runspace = $WaiterRunspace
[void]$WaiterPowershell.AddScript({
Import-Module (Join-Path $path 'SpecialK_PSLibrary.psm1')
Wait-SkAck -When $SK_AutoStop
})
[void][WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.MinimizeNoActivate)
$StarterHandle = $StarterPowershell.BeginInvoke()
$WaiterHandle = $WaiterPowershell.BeginInvoke()
Remove-Variable 'jobInput'
if ($SK_InjectDelay) {
Start-Sleep -Seconds $SK_InjectDelay
Start-SkService -SkInstallPath $SK_InstallPath
}
while (!($StarterHandle.IsCompleted) -or !($WaiterHandle.IsCompleted)) {
Start-Sleep -Milliseconds 250
}
if ((! $IsRunning) -or ($SK_AdminMode) -or ($ServicePath -ne (Get-SkPath $SK_InstallPath)) -or ((@($ServiceProcess).Count) -gt 1)) {
Write-Host 'Stopping service'
Stop-SkService -SkInstallPath $SK_InstallPath
}
if (($SK_AdminMode) -and ($IsRunning)) {
while (! $StarterHandle.IsCompleted) {
Start-Sleep -Milliseconds 250
}
}
[void][WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.NormalNoActivate)
#$StarterPowershell.EndInvoke($StarterHandle)
$StarterPowershell.Dispose()
$StarterRunspace.CloseAsync()
#$WaiterPowershell.EndInvoke($WaiterHandle)
#$WaiterPowershell.Dispose()
$WaiterRunspace.CloseAsync()
if ($WhitelistWritten) {
Write-Host 'Removing' $WhitelistItem 'from whitelist'
Remove-SkList -Type 'allow' $WhitelistItem
}
if ($BlacklistWritten) {
Write-Host 'Removing' $BlacklistItem 'from blacklist'
Remove-SkList -Type 'deny' $BlacklistItem
}
}
if (! $SK_AdminMode) {
if ($IsRunning -and (($ServicePath -ne (Get-SkPath $SK_InstallPath)) -or ((@($ServiceProcess).Count) -gt 1))) {
Write-Host "Restarting previous Service... $ServicePath"
Start-SkService -path @($ServicePath)[0]
}
elseif (($IsRunning) -and ($SK_AsAdmin)) {
Write-Host 'Restarting Service...'
Start-SkService -path $SK_InstallPath
}
}
Write-Host '
Done!'
exit