-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpecialK_PSLibrary.psm1
More file actions
820 lines (732 loc) · 29.8 KB
/
Copy pathSpecialK_PSLibrary.psm1
File metadata and controls
820 lines (732 loc) · 29.8 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
<#
.NOTES
Special K Powershell Command Library v22.09.09
Copyright (C) 2022-2023 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/>.
#>
function Get-SkPath {
<#
.SYNOPSIS
Returns the a path of a Special K installation (if it includes SpecialK32.dll or SpecialK64.dll in any form). Throws an error otherwise.
Checks the following places in this order: Working Directory, Script Root, default home dir of SK (Documents\My Mods\SpecialK\)
.PARAMETER Path
Returns the given path if it includes SpecialK32.dll or SpecialK64.dll in any form. Gives an error otherwise.
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)][AllowEmptyString()][Alias('PSPath', 'LP', 'LiteralPath')] [string[]] $Path,
[Parameter()] [switch] $old
)
if ($old) {
$filter = '*.old'
}
else {
$filter = '*.dll'
}
if ($path) {
if (Test-Path -LiteralPath $Path -PathType 'Container') {
#$Path -replace '(\?|\*|\[)', '`$1'
if (Get-ChildItem -LiteralPath $Path -Filter $filter -Depth 0 `
| Select-Object -ExpandProperty 'VersionInfo' `
| Where-Object -Property 'ProductName' -EQ 'Special K') {
Write-Output (Get-Item $path)
return
}
else {
Write-Error -Category 'ObjectNotFound' "The Path `"$Path`" is not a Special K installation. No valid DLL was found."
return
}
}
else {
Write-Error -Category 'ObjectNotFound' "The Path `"$Path`" does not exist."
return
}
}
if (Get-ChildItem -LiteralPath '.\' -Filter $filter -Depth 0 `
| Select-Object -ExpandProperty 'VersionInfo' `
| Where-Object -Property 'ProductName' -EQ 'Special K') {
Write-Output (Get-Item '.')
return
}
if ($PSScriptRoot) {
if (Get-ChildItem -LiteralPath '.\' -Filter $filter -Depth 0 `
| Select-Object -ExpandProperty 'VersionInfo' `
| Where-Object -Property 'ProductName' -EQ 'Special K') {
Write-Output (Get-Item $PSScriptRoot)
return
}
}
$SKReg = 'Registry::HKEY_CURRENT_USER\SOFTWARE\Kaldaien\Special K'
if (Test-Path $SKReg) {
try {
$RegPath = (Get-ItemProperty -Path $SKReg -ErrorAction 'Stop').Path
}
catch {}
if ($RegPath) {
if (Test-Path -LiteralPath $RegPath -PathType 'Container') {
if (Get-ChildItem -LiteralPath $RegPath -Filter $filter -Depth 0 `
| Select-Object -ExpandProperty 'VersionInfo' `
| Where-Object -Property 'ProductName' -EQ 'Special K') {
Write-Output (Get-Item $RegPath)
return
}
}
}
}
if (Get-ChildItem -LiteralPath (Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath '\My Mods\SpecialK\') -Filter $filter -Depth 0 `
| Select-Object -ExpandProperty 'VersionInfo' `
| Where-Object -Property 'ProductName' -EQ 'Special K') {
Write-Output (Get-Item (Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath '\My Mods\SpecialK\'))
return
}
Write-Error -Category 'ObjectNotFound' 'No valid Special K found. No valid DLL was found.'
return
}
function Get-SkDll {
param (
[Parameter(ValueFromPipelineByPropertyName)][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
begin {
try {
$InstallPath = Get-SkPath $SkInstallPath -ErrorAction 'Stop'
}
catch {
Write-Error -Category 'ObjectNotFound' "The Path `"$SkInstallPath`" is no valid Special K installation. No valid DLL was found."
break
}
}
process {
#[System.IO.Directory]::EnumerateFiles($_, 'SpecialK*.dll', 'TopDirectoryOnly')
Get-ChildItem -LiteralPath $InstallPath -Filter "*.dll" -Depth 0 `
| Where-Object { $_.VersionInfo.ProductName -EQ 'Special K' } `
| Write-Output
}
}
function Test-SkIsGlobal {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Mandatory)][Alias('PSPath', 'LP', 'LiteralPath')] [string] $Path
)
$SK32 = (Join-Path -Path $Path -ChildPath '\SpecialK32.dll')
$SK64 = (Join-Path -Path $Path -ChildPath '\SpecialK64.dll')
if ([Environment]::Is64BitOperatingSystem) {
if ((Test-Path -LiteralPath $SK64) -or (Test-Path -LiteralPath $SK32)) {
Write-Output $true
}
else {
Write-Information "The Path `"$Path`" is not valid for a global Special K installation. No SpecialK32.dll or SpecialK64.dll was found."
Write-Output $false
}
}
else {
if ((Test-Path -LiteralPath $SK32)) {
Write-Output $true
}
else {
Write-Information "The Path `"$Path`" is not valid for a global Special K installation. No SpecialK32.dll was found."
Write-Output $false
}
}
}
function Get-SkTeardownStatus {
<#
.SYNOPSIS
Returns an array of which teardown handles are aviailable (32, 64). Might also get handles when the injection service is not running, but SK is still injected in a process.
#>
[System.Threading.EventWaitHandle]$SK_Event = $null
if ( [System.Threading.EventWaitHandle]::TryOpenExisting("Local\SK_GlobalHookTeardown32", [ref]$SK_Event) ) {
$SK_Event.close()
Write-Output '32'
}
if ( [System.Threading.EventWaitHandle]::TryOpenExisting("Local\SK_GlobalHookTeardown64", [ref]$SK_Event) ) {
$SK_Event.close()
Write-Output '64'
}
}
function Get-SkTeardown {
<#
.SYNOPSIS
Gets the Teardown event handle of either 32 or 64Bit SK. Does NOT close the handle on its own! Use "-Timeout <Millisecons>" if you want to wait until the handle appears.
.PARAMETER Timeout
Timeout in milliseconds
Waits until a handle is obtained or until the timeout has passed. Always rounds down to the nearest 250ms interval.
.NOTES
Does NOT close the handle on its own! Make sure you assign this to an object and use the .close() method, when you're done with the event (like "$obj.close()"). SK might not shut down correctly if this event isn't closed.
If the .set() method is used on the handle SKs will try to shutdown and remove itself from any process that it is not fully active.
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline, Mandatory)][ValidateSet('32', '64')] [string] $Bitness,
[Parameter()] [int] $Timeout
)
begin {
[System.Threading.EventWaitHandle]$SK_Event = $null
$times = [math]::Truncate($Timeout / 250)
}
process {
if ($Timeout) {
$i = 0
while ($i -le $times) {
if ( [System.Threading.EventWaitHandle]::TryOpenExisting("Local\SK_GlobalHookTeardown$Bitness", [ref]$SK_Event) ) {
Write-Output $SK_Event
break
}
else {
$i++
Start-Sleep -Milliseconds "250"
}
}
}
else {
if ( [System.Threading.EventWaitHandle]::TryOpenExisting("Local\SK_GlobalHookTeardown$Bitness", [ref]$SK_Event) ) {
Write-Output $SK_Event
}
}
}
}
function Get-SkInjectedProcess {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline, Position = 0, Mandatory)][ValidateSet('32', '64')] [string] $Bitness,
[Parameter()] [switch] $InjectorOnly
)
process {
if ($InjectorOnly) {
if ($Bitness -eq 32) {
if ([Environment]::Is64BitOperatingSystem) {
if ([Environment]::Is64BitProcess) {
#64-Bit OS and 64-bit Powershell
. (Join-Path ([Environment]::GetFolderPath('SystemX86')) 'WindowsPowerShell\v1.0\powershell.exe') -NoProfile {
get-process 'SKIFsvc32', 'rundll32' -ErrorAction 'SilentlyContinue' | where-object { $_.Modules.Product -eq 'Special K' } | Sort-Object -Unique
exit
} | ForEach-Object {
if ($_) {
Add-Member -PassThru -InputObject $_ -MemberType 'ScriptProperty' -Name 'SkModule' -Value { ($this.Modules | where-object Product -eq 'Special K').FileName }
}
}
return
}
}
#32-Bit OS and 32-bit Powershell or 64-Bit OS but 32-bit Powershell
get-process 'SKIFsvc32', 'rundll32' -ErrorAction 'SilentlyContinue' | where-object { $_.Modules.Product -eq 'Special K' } | Sort-Object -Unique | ForEach-Object {
if ($_) {
Add-Member -PassThru -InputObject $_ -MemberType 'ScriptProperty' -Name 'SkModule' -Value { ($this.Modules | where-object Product -eq 'Special K').FileName }
}
}
}
if ($Bitness -eq 64) {
if (-not [Environment]::Is64BitOperatingSystem) {
Write-Error "This Operating System isn't capable of 64-Bit"
return
}
if ([Environment]::Is64BitProcess) {
get-process 'SKIFsvc64', 'rundll32' -ErrorAction 'SilentlyContinue' | where-object { $_.Modules.Product -eq 'Special K' } | Sort-Object -Unique | ForEach-Object {
if ($_) {
Add-Member -PassThru -InputObject $_ -MemberType 'ScriptProperty' -Name 'SkModule' -Value { ($this.Modules | where-object Product -eq 'Special K').FileName }
}
}
}
else {
. "$env:windir\SysNative\WindowsPowerShell\v1.0\powershell.exe" -NoProfile {
get-process 'SKIFsvc64', 'rundll32' -ErrorAction 'SilentlyContinue' | where-object { $_.Modules.Product -eq 'Special K' } | Sort-Object -Unique
exit
} | ForEach-Object {
if ($_) {
Add-Member -PassThru -InputObject $_ -MemberType 'ScriptProperty' -Name 'SkModule' -Value { ($this.Modules | where-object Product -eq 'Special K').FileName }
}
}
}
}
}
else {
if ($Bitness -eq 32) {
if ([Environment]::Is64BitOperatingSystem) {
if ([Environment]::Is64BitProcess) {
#64-Bit OS and 64-bit Powershell
. (Join-Path ([Environment]::GetFolderPath('SystemX86')) 'WindowsPowerShell\v1.0\powershell.exe') -NoProfile {
get-process -ErrorAction 'SilentlyContinue' | where-object { $_.Modules.Product -eq 'Special K' } | Sort-Object -Unique
exit
} | ForEach-Object {
Add-Member -PassThru -InputObject $_ -MemberType 'ScriptProperty' -Name 'SkModule' -Value { ($this.Modules | where-object Product -eq 'Special K').FileName }
}
return
}
}
#32-Bit OS and 32-bit Powershell or 64-Bit OS but 32-bit Powershell
get-process -ErrorAction 'SilentlyContinue' | where-object { $_.Modules.Product -eq 'Special K' } | Sort-Object -Unique | ForEach-Object {
Add-Member -PassThru -InputObject $_ -MemberType 'ScriptProperty' -Name 'SkModule' -Value { ($this.Modules | where-object Product -eq 'Special K').FileName }
}
}
if ($Bitness -eq 64) {
if (-not [Environment]::Is64BitOperatingSystem) {
Write-Error "This Operating System isn't capable of 64-Bit"
return
}
if ([Environment]::Is64BitProcess) {
get-process -ErrorAction 'SilentlyContinue' | where-object { $_.Modules.Product -eq 'Special K' } | Sort-Object -Unique | ForEach-Object {
Add-Member -PassThru -InputObject $_ -MemberType 'ScriptProperty' -Name 'SkModule' -Value { ($this.Modules | where-object Product -eq 'Special K').FileName }
}
}
else {
. "$env:windir\SysNative\WindowsPowerShell\v1.0\powershell.exe" -NoProfile {
get-process -ErrorAction 'SilentlyContinue' | where-object { $_.Modules.Product -eq 'Special K' } | Sort-Object -Unique
exit
} | ForEach-Object {
Add-Member -PassThru -InputObject $_ -MemberType 'ScriptProperty' -Name 'SkModule' -Value { ($this.Modules | where-object Product -eq 'Special K').FileName }
}
}
}
}
}
}
function Get-SkServiceProcess {
<#
.SYNOPSIS
Returns a process object of an active service. Only works if the service was started from the current SkPath (see Get-SkPath). Use "-Timeout <Millisecons>" if you want to wait until the process appears.
.PARAMETER Bitness
Defines the Bitness (32, 64) of the service to get.
.PARAMETER Timeout
Timeout in milliseconds
Waits until a process is obtained or until the timeout has passed. Always rounds down to the nearest 250ms interval.
.PARAMETER SkInstallPath
Defines an alternative path to an SkPath that was started.
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 0, Mandatory)][ValidateSet('32', '64')] [string] $Bitness,
[Parameter()] [int] $Timeout,
[Parameter(ValueFromPipelineByPropertyName)][AllowEmptyString()][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
begin {
$InstallPath = Get-SkPath $SkInstallPath
if (!(Test-SkIsGlobal -Path $InstallPath)) {
Write-Error -Category 'ObjectNotFound' "The Path `"$SkInstallPath`" is no valid global Special K installation"
break
}
}
process {
$Path = (Join-Path -Path $InstallPath -ChildPath "\Servlet\SpecialK$($Bitness).pid")
if ($Timeout) {
$times = [math]::Truncate($Timeout / 250)
$i = 0
while ($i -le $times) {
Try {
$ID = Get-Content -LiteralPath $Path -ErrorAction 'Stop'
$Process = Get-Process -ID $ID -ErrorAction 'Stop'
if ($Process) { Write-Output $Process }
break
}
Catch {
$i++
Start-Sleep -Milliseconds "250"
}
}
}
else {
if (Test-Path -LiteralPath $Path) {
try {
$ID = Get-Content -LiteralPath $Path
$Process = Get-Process -ID $ID -ErrorAction 'Stop'
}
catch [Microsoft.PowerShell.Commands.ProcessCommandException] {}
if ($Process) { Write-Output $Process }
}
}
}
}
function Wait-SkAck {
<#
.SYNOPSIS
Waits until Special K sends it's ack event. Without a timeout this command will wait indefinitely.
.PARAMETER When
Defines what type of event to wait for.
- Injected
This event is send as soon SK is active and rendered it's first frame.
- Exit
This event is send when an active SK session is shut down (game closed). This has to be listened to for before the shut down, or it is never fired by SK!
.PARAMETER Timeout
Timeout in milliseconds
When a timeout is set this command will only wait the specified duration and return $true if the event was fired and nothing if it wasn't. Without a timeout this command will wait indefinitely.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)][AllowEmptyString()][ValidateSet('Injected', 'Exit', '')] [string] $When,
[Parameter()] [int] $Timeout
)
if ($When -ne 'Exit') {
$handle = New-Object -TypeName System.Threading.EventWaitHandle -ArgumentList $false, AutoReset, 'Local\SKIF_InjectAck'
if ($Timeout) {
$output = $handle.WaitOne($Timeout)
}
else {
[void]$handle.WaitOne()
}
$handle.Close()
if ($output) { Write-Output $output }
return
}
else {
$handle = New-Object -TypeName System.Threading.EventWaitHandle -ArgumentList $false, AutoReset, 'Local\SKIF_InjectExitAck'
if ($Timeout) {
$output = $handle.WaitOne($Timeout)
}
else {
[void]$handle.WaitOne()
}
$handle.Close()
if ($output) { Write-Output $output }
return
}
}
Function Start-SkService {
<#
.SYNOPSIS
Starts the SK injection service in the current SkPath (see Get-SkPath).
.PARAMETER SkInstallPath
Defines an alternative path to an SkPath.
#>
param (
[Parameter(ValueFromPipelineByPropertyName)][AllowEmptyString()][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
$InstallPath = Get-SkPath $SkInstallPath
if (!(Test-SkIsGlobal -Path $InstallPath)) {
Write-Error -Category 'ObjectNotFound' "The Path `"$SkInstallPath`" is no valid global Special K installation"
return
}
$ServletPath = (Join-Path -Path $InstallPath -ChildPath 'Servlet')
$SK32 = (Join-Path -Path $InstallPath -ChildPath '\SpecialK32.dll')
$SK64 = (Join-Path -Path $InstallPath -ChildPath '\SpecialK64.dll')
if (Test-Path -LiteralPath $SK32) {
If (Test-Path -LiteralPath "$ServletPath\SKIFsvc32.exe" -PathType leaf) {
Write-Information 'Starting 32Bit service (SKIF Standalone)...'
Start-Process -FilePath "$ServletPath\SKIFsvc32.exe" -WorkingDirectory $ServletPath -ArgumentList "Start"
}
else {
if (!(Test-Path -LiteralPath $ServletPath -PathType 'Container')) {
[void](New-Item $ServletPath -ItemType 'Directory')
}
$rundll = Join-Path -Path ([Environment]::GetFolderPath('SystemX86')) 'rundll32.exe'
If (Test-Path -LiteralPath "$rundll" -PathType leaf) {
Write-Information 'Starting 32Bit service (Rundll32)...'
Start-Process -FilePath $rundll -WorkingDirectory $ServletPath -ArgumentList "`"$SK32`", RunDLL_InjectionManager Install"
}
else {
Write-Error -Category 'ObjectNotFound' 'No SKIFsvc32.exe or 32-Bit rundll32.exe found. Your system seems broken...'
return
}
}
$SK_Event32 = Get-SkServiceProcess -Path $SkInstallPath -Bitness '32' -Timeout '10000'
if (! $SK_Event32) {
if (Get-SkTeardownStatus -contains '32' ) {
Write-Error 'Failed to start 32-bit Service (Timeout), but another instance might be running or SK is still stuck somewhere.'
}
else {
Write-Error 'Failed to start 32-bit Service (Timeout)'
}
}
else {
Write-Information 'Success'
}
}
if ([Environment]::Is64BitOperatingSystem) {
if (Test-Path -LiteralPath $SK64) {
If (Test-Path -LiteralPath "$ServletPath\SKIFsvc64.exe" -PathType 'Leaf') {
Write-Information 'Starting 64Bit service (SKIF Standalone)...'
Start-Process -FilePath "$ServletPath\SKIFsvc64.exe" -WorkingDirectory $ServletPath -ArgumentList "Start"
}
else {
if (!(Test-Path -LiteralPath $ServletPath -PathType 'Container')) {
[void](New-Item $ServletPath -ItemType 'Directory')
}
$rundll = Join-Path -Path ([Environment]::GetFolderPath('System')) 'rundll32.exe'
If (Test-Path -LiteralPath "$rundll" -PathType 'Leaf') {
Write-Information 'Starting 64Bit service (Rundll32)...'
Start-Process -FilePath $rundll -WorkingDirectory $ServletPath -ArgumentList "`"$SK64`", RunDLL_InjectionManager Install"
}
else {
Write-Error -Category 'ObjectNotFound' 'No SKIFsvc64.exe or 64Bit rundll32.exe found. Your system seems broken...'
return
}
}
$SK_Event64 = Get-SkServiceProcess -Path $SkInstallPath -Bitness '64' -Timeout '10000'
if (! $SK_Event64) {
if (Get-SkTeardownStatus -contains '64' ) {
Write-Error 'Failed to start 64bit Service (Timeout), but another instance might be running or SK is still stuck somewhere.'
}
else {
Write-Error 'Failed to start 64bit Service (Timeout)'
}
}
else {
Write-Information 'Successfully started 64-bit Service'
}
}
}
Start-Sleep -Milliseconds '150' #This is to prevent race conditions
}
Function Stop-SkService {
<#
.SYNOPSIS
Stops any active SK injection service.
#>
param (
[Parameter(ValueFromPipelineByPropertyName)][AllowEmptyString()][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
$InstallPath = Get-SkPath $SkInstallPath
if (!(Test-SkIsGlobal -Path $InstallPath)) {
Write-Error -Category 'ObjectNotFound' "The Path `"$SkInstallPath`" is no valid global Special K installation"
return
}
$ServletPath = (Join-Path -Path $InstallPath -ChildPath 'Servlet')
$SK32 = (Join-Path -Path $InstallPath -ChildPath '\SpecialK32.dll')
$SK64 = (Join-Path -Path $InstallPath -ChildPath '\SpecialK64.dll')
if (Test-Path -LiteralPath $SK32) {
If (Test-Path -LiteralPath "$ServletPath\SKIFsvc32.exe" -PathType leaf) {
Write-Information 'Stopping 32Bit service (SKIF Standalone)...'
Start-Process -FilePath "$ServletPath\SKIFsvc32.exe" -WorkingDirectory $ServletPath -ArgumentList "Stop"
}
else {
if (!(Test-Path -LiteralPath $ServletPath -PathType 'Container')) {
[void](New-Item $ServletPath -ItemType 'Directory')
}
$rundll = Join-Path -Path ([Environment]::GetFolderPath('SystemX86')) 'rundll32.exe'
If (Test-Path -LiteralPath $rundll -PathType leaf) {
Write-Information 'Stopping 32Bit service (Rundll32)...'
Start-Process -FilePath $rundll -WorkingDirectory $ServletPath -ArgumentList "`"$SK32`", RunDLL_InjectionManager Remove"
}
else {
Write-Error -Category 'ObjectNotFound' 'No SKIFsvc32.exe or 32-Bit rundll32.exe found. Your system seems broken...'
return
}
}
}
if ([Environment]::Is64BitOperatingSystem) {
if (Test-Path -LiteralPath $SK64) {
If (Test-Path -LiteralPath "$ServletPath\SKIFsvc64.exe" -PathType 'Leaf') {
Write-Information 'Stopping 64Bit service (SKIF Standalone)...'
Start-Process -FilePath "$ServletPath\SKIFsvc64.exe" -WorkingDirectory $ServletPath -ArgumentList "Stop"
}
else {
if (!(Test-Path -LiteralPath $ServletPath -PathType 'Container')) {
[void](New-Item $ServletPath -ItemType 'Directory')
}
$rundll = Join-Path -Path ([Environment]::GetFolderPath('System')) 'rundll32.exe'
If (Test-Path -LiteralPath $rundll -PathType 'Leaf') {
Write-Information 'Stopping 64Bit service (Rundll32)...'
Start-Process -FilePath $rundll -WorkingDirectory $ServletPath -ArgumentList "`"$SK64`", RunDLL_InjectionManager Remove"
}
else {
Write-Error -Category 'ObjectNotFound' 'No SKIFsvc64.exe or 64-Bit rundll32.exe found. Your system seems broken...'
return
}
}
}
}
Start-Sleep -Milliseconds '150' #This is to prevent race conditions
}
function Get-SkList {
[CmdletBinding(PositionalBinding = $false)]
param (
[Parameter(Mandatory)][ValidateSet('white', 'black', 'allow', 'deny')] [string] $Type,
[Parameter(ValueFromPipelineByPropertyName)][AllowEmptyString()][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
$InstallPath = Get-SkPath $SkInstallPath
if (!(Test-SkIsGlobal -Path $InstallPath)) {
Write-Error -Category 'ObjectNotFound' "The Path `"$SkInstallPath`" is no valid global Special K installation"
return
}
if (($Type -eq 'white') -or (($Type -eq 'allow'))) {
$Path = (Join-Path -Path $InstallPath -ChildPath '\Global\whitelist.ini')
}
else {
$Path = (Join-Path -Path $InstallPath -ChildPath '\Global\blacklist.ini')
}
if (Test-Path -LiteralPath $Path) {
$output = Get-Content $Path
Write-Output $output
}
else {
#Write-Error -Category 'ObjectNotFound' "`"$Path`" not found."
}
}
function Add-SkList {
<#
.SYNOPSIS
Writes an entry in the white/blacklist of the current SkPath (see Get-SkPath). Paths that are whitelisted by default in SK will be ignored.
.PARAMETER Value
Defines the text to write in the list. Special characters are automatically escaped unless the "-Raw" switch is used.
.PARAMETER Type
Defines which list the value is added to. Allowed types are 'allow' or 'white' for whitelisting and 'deny' or 'black' for blacklisting.
.PARAMETER Raw
Special regex characters in the string won't be escaped. Use this if you actually want to write an regex expression and not an exact match.
.PARAMETER SkInstallPath
Defines an alternative path to an SkPath.
#>
[CmdletBinding(PositionalBinding = $false)]
param (
[Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $Value,
[Parameter(Mandatory)][ValidateSet('white', 'black', 'allow', 'deny')] [string] $Type,
[Parameter()] [switch] $Raw,
[Parameter(ValueFromPipelineByPropertyName)][AllowEmptyString()][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
begin {
$InstallPath = Get-SkPath $SkInstallPath
if (!(Test-SkIsGlobal -Path $InstallPath)) {
Write-Error -Category 'ObjectNotFound' "The Path `"$SkInstallPath`" is no valid global Special K installation."
break
}
}
Process {
if (($Type -eq 'white') -or (($Type -eq 'allow'))) {
$Path = (Join-Path -Path $InstallPath -ChildPath '\Global\whitelist.ini')
if (($Value -like '*SteamApps*') -or ($Value -like '*Epic Games\*') -or ($Value -like '*GOG Galaxy\Games*') -or ($Value -like '*Origin Games\*')) {
Write-Warning 'Ignoring entry, as this path is already whitelisted by default in Special K.'
return
}
}
else {
$Path = (Join-Path -Path $InstallPath -ChildPath '\Global\blacklist.ini')
}
if (! (Test-Path -LiteralPath $Path -PathType 'Leaf')) {
Write-Warning "`"$Path`" does not exist and will be created."
$parent = (Split-Path $Path -Parent)
if (! (Test-Path $parent -PathType 'Container')) {
[void](New-Item $parent -ItemType 'Directory')
}
[void](New-Item $Path -ItemType 'File')
}
if (! $Raw) {
$Value = ($Value -replace '(\\|\^|\$|\.|\||\?|\*|\+|\(|\)|\[\{)', '\$1') #escaping all special chars in regex \blah.exe -> \\blah\.exe (matches "\blah.exe")
}
$CompareReg = ($Value -replace '(\\|\^|\$|\.|\||\?|\*|\+|\(|\)|\[\{)', '\$1') # \\blah\.exe -> \\\\blah\\.exe (matches "\\blah\.exe")
$CompareResult = Get-Content -Encoding 'utf8' -LiteralPath $Path | Where-Object { $_ -match $CompareReg }
if (! $CompareResult) {
Write-Information "Adding $Value"
if ($null -eq @(Get-Content -Encoding 'utf8' -LiteralPath $Path)[-1]) {
[System.IO.File]::AppendAllText($Path, $Value)
}
else {
[System.IO.File]::AppendAllText($Path, "`r`n$Value")
}
$Success = $true
}
}
end { Write-Output $Success }
}
function Remove-SkList {
<#
.SYNOPSIS
Removes an entry in the white/blacklist of the current SkPath (see Get-SkPath).
.PARAMETER Value
Defines the value to remove in the list. Special characters are automatically escaped unless the "-Regex" switch is used.
.PARAMETER Type
Defines which list the value is removed from. Allowed types are 'allow' or 'white' for whitelisting and 'deny' or 'black' for blacklisting.
.PARAMETER Raw
Special regex characters in the string won't be escaped. Use this if you want to remove an regex entry that was previously added with the "-Raw" parameter.
.PARAMETER SkInstallPath
Defines an alternative path to an SkPath.
#>
[CmdletBinding(PositionalBinding = $false)]
param (
[Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $Value,
[Parameter(Mandatory)][ValidateSet('white', 'black', 'allow', 'deny')] [string] $Type,
[Parameter()] [switch] $Raw,
[Parameter(ValueFromPipelineByPropertyName)][AllowEmptyString()][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
begin {
$InstallPath = Get-SkPath $SkInstallPath
if (!(Test-SkIsGlobal -Path $InstallPath)) {
Write-Error -Category 'ObjectNotFound' "The Path `"$SkInstallPath`" is no valid global Special K installation"
break
}
}
Process {
if (($Type -eq 'white') -or (($Type -eq 'allow'))) {
$Path = (Join-Path -Path $InstallPath -ChildPath '\Global\whitelist.ini')
}
else {
$Path = (Join-Path -Path $InstallPath -ChildPath '\Global\blacklist.ini')
}
if (! $Raw) {
$Value = ($Value -replace '(\\|\^|\$|\.|\||\?|\*|\+|\(|\)|\[\{)', '\$1') # \\blah\.exe -> \\\\blah\\.exe (matches "\\blah\.exe")
}
$Content = Get-Content -Encoding 'utf8' -LiteralPath $Path | Where-Object { ($_ -ne $Value) -and ($_ -ne '') }
if ($null -ne $Content) {
$Content = $Content -join "`r`n" #add nweline after each object, but get rid of the newline at the end of file
}
else {
$Content = @()
}
Write-Information "Removing $Value"
#Set-Content -Encoding 'utf8' -LiteralPath $Path -Value $Content -NoNewline
[System.IO.File]::WriteAllLines($Path, $Content) #Workaround for powsh 5.1, so no BOM is written
}
}
function Get-SkVersion {
param (
[Parameter(ValueFromPipelineByPropertyName)][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
Write-Warning 'This function is deprecated, please use "Get-SkDll" instead and refer to "$_.VersionInfo".'
try {
$InstallPath = Get-SkPath $SkInstallPath -ErrorAction 'Stop'
}
catch {
Write-Error -Category 'ObjectNotFound' "The Path `"$Path`" is no valid Special K installation. No valid DLL was found."
return
}
#[System.IO.Directory]::EnumerateFiles($_, 'SpecialK*.dll', 'TopDirectoryOnly')
Get-ChildItem -LiteralPath $InstallPath -Filter "SpecialK*.dll" -Depth 0 `
| Select-Object -Property 'Name', 'BaseName' -ExpandProperty 'VersionInfo' `
| Where-Object -Property 'ProductName' -EQ 'Special K' `
| Select-Object -Property 'Name', 'BaseName', 'ProductVersion', 'ProductVersionRaw', 'InternalName' `
| Write-Output
}
function Rename-SkToOld {
param (
[Parameter(ValueFromPipelineByPropertyName)][AllowEmptyString()][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
$InstallPath = Get-SkPath $SkInstallPath
if (!(Test-SkIsGlobal -Path $InstallPath)) {
Write-Error -Category 'ObjectNotFound' "The Path `"$SkInstallPath`" is no valid global Special K installation"
return
}
$SK32 = Join-Path $InstallPath '\SpecialK32.dll'
$SK64 = Join-Path $InstallPath '\SpecialK64.dll'
$SK32o = Join-Path $InstallPath '\SpecialK32.old'
$SK64o = Join-Path $InstallPath '\SpecialK64.old'
If ((Test-Path -LiteralPath $SK32 -PathType 'Leaf')) {
Rename-Item -LiteralPath $SK32 (Split-Path $SK32o -Leaf)
}
If ((Test-Path -LiteralPath $SK64 -PathType 'Leaf')) {
Rename-Item -LiteralPath $SK64 (Split-Path $SK64o -Leaf)
}
}
function Remove-SkOld {
param (
[Parameter(ValueFromPipelineByPropertyName)][AllowEmptyString()][Alias('PSPath', 'Path')] [string] $SkInstallPath
)
try {
$InstallPath = Get-SkPath $SkInstallPath -Old -ErrorAction 'Stop'
}
catch {
Write-Error -Category 'ObjectNotFound' "The Path `"$SkInstallPath`" is no valid Special K installation. No valid DLL was found."
return
}
$SK32o = Join-Path $InstallPath '\SpecialK32.old'
$SK64o = Join-Path $InstallPath '\SpecialK64.old'
If ((Test-Path -LiteralPath $SK32o -PathType 'Leaf')) {
Remove-Item -LiteralPath $SK32o
}
If ((Test-Path -LiteralPath $SK64o -PathType 'Leaf')) {
Remove-Item -LiteralPath $SK64o
}
}