This repository has been archived by the owner on Feb 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOri_Controller_Remap.ps1
817 lines (694 loc) · 21.8 KB
/
Ori_Controller_Remap.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
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
# Dummy param block to allow Verbose switch
[CmdletBinding()]
Param()
#region Helper functions
<#
.Synopsis
Gets the content of an INI file
.Description
Gets the content of an INI file and returns it as a hashtable
.Notes
Author : Oliver Lipkau <oliver@lipkau.net>
Blog : http://oliver.lipkau.net/blog/
Source : https://github.com/lipkau/PsIni
http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91
Version : 1.0 - 2010/03/12 - Initial release
1.1 - 2014/12/11 - Typo (Thx SLDR)
Typo (Thx Dave Stiff)
#Requires -Version 2.0
.Inputs
System.String
.Outputs
System.Collections.Hashtable
.Parameter FilePath
Specifies the path to the input file.
.Example
$FileContent = Get-IniContent "C:\myinifile.ini"
-----------
Description
Saves the content of the c:\myinifile.ini in a hashtable called $FileContent
.Example
$inifilepath | $FileContent = Get-IniContent
-----------
Description
Gets the content of the ini file passed through the pipe into a hashtable called $FileContent
.Example
C:\PS>$FileContent = Get-IniContent "c:\settings.ini"
C:\PS>$FileContent["Section"]["Key"]
-----------
Description
Returns the key "Key" of the section "Section" from the C:\settings.ini file
.Link
Out-IniFile
#>
Function Get-IniContent
{
[CmdletBinding()]
Param
(
[ValidateNotNullOrEmpty()]
[ValidateScript({
(Test-Path $_) -and ((Get-Item $_).Extension -eq '.ini')
})]
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
[string]$FilePath
)
Process
{
Write-Verbose "Reading ini file: $Filepath"
$ini = @{}
switch -regex -file $FilePath
{
'^\[(.+)\]$' # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
'^(;.*)$' # Comment
{
if (!($section))
{
$section = 'No-Section'
$ini[$section] = @{}
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = 'Comment' + $CommentCount
$ini[$section][$name] = $value
}
'(.+?)\s*=\s*(.*)' # Key
{
if (!($section))
{
$section = 'No-Section'
$ini[$section] = @{}
}
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
$ini
}
}
<#
.Synopsis
PowerShell pause implementation
.Link
https://adamstech.wordpress.com/2011/05/12/how-to-properly-pause-a-powershell-script/
#>
function Pause {
Param
(
[string]$Message = 'Press any key to continue...',
[Parameter(ValueFromRemainingArguments = $true)]
$PassMe
)
If ($psISE) {
# The "ReadKey" functionality is not supported in Windows PowerShell ISE.
#$Shell = New-Object -ComObject 'WScript.Shell'
#$Button = $Shell.Popup('Click OK to continue.', 0, 'Script Paused', 0)
return
}
Show-Text -Text $Message -NoNewline $PassMe
$Ignore =
16, # Shift (left or right)
17, # Ctrl (left or right)
18, # Alt (left or right)
20, # Caps lock
91, # Windows key (left)
92, # Windows key (right)
93, # Menu key
144, # Num lock
145, # Scroll lock
166, # Back
167, # Forward
168, # Refresh
169, # Stop
170, # Search
171, # Favorites
172, # Start/Home
173, # Mute
174, # Volume Down
175, # Volume Up
176, # Next Track
177, # Previous Track
178, # Stop Media
179, # Play
180, # Mail
181, # Select Media
182, # Application 1
183 # Application 2
While ($KeyInfo.VirtualKeyCode -Eq $Null -Or $Ignore -Contains $KeyInfo.VirtualKeyCode) {
$KeyInfo = $Host.UI.RawUI.ReadKey('NoEcho, IncludeKeyDown')
}
Write-Host
}
<#
.Synopsis
Get path to game's assembly directory. Searches registry + paths specified in Path parameter.
#>
function Get-GameAssemblyDirectory
{
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string[]]$Path,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string]$GameAssembly
)
Begin
{
$AssemblySubdir = 'ori_Data\Managed'
$RegistryKeys = @{
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 261570' = 'InstallLocation'
'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 261570' = 'InstallLocation'
}
}
Process
{
# Get Ori installation directory from registry
[string[]]$PathsFromRegistry = $RegistryKeys.GetEnumerator() |
ForEach-Object {
$InstallDir = (Get-ItemProperty -Path $_.Key -ErrorAction SilentlyContinue).($_.Value)
if($InstallDir)
{
Join-Path -Path $InstallDir -ChildPath $AssemblySubDir
}
}
# Check all paths for game assembly file (Assembly-CSharp.dll)
# and return first valid one
$Path + $PathsFromRegistry | ForEach-Object {
$PathToGameAssembly = Join-Path -Path $_ -ChildPath $GameAssembly
if(Test-Path -Path $PathToGameAssembly -PathType Leaf)
{
$_
}
} | Select-Object -First 1
}
}
<#
.Synopsis
Show text on screen, optionally center it and specify color.
Centering works only in PS console, not ISE.
#>
function Show-Text
{
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline = $true)]
[string]$Text,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateScript({
[Enum]::GetValues([ConsoleColor]) -contains $_
})]
[string]$ForegroundColor,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateScript({
[Enum]::GetValues([ConsoleColor]) -contains $_
})]
[string]$BackgroundColor,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]$Center,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]$NoNewline,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]$AsSeparator
)
Begin
{
# Get console width (PS console only)
$ConsoleWidth = $host.UI.RawUI.WindowSize.Width
}
Process
{
# This will allow Write-Host to use default values for ForegroundColor\BackgroundColor
$WriteHost = {
'$_ | Write-Host'
if($ForegroundColor){'-ForegroundColor $ForegroundColor'}
if($BackgroundColor){'-BackgroundColor $BackgroundColor'}
if($NoNewline){'-NoNewline'}
}
# Treat Text as Separator?
if($AsSeparator)
{
# Create separator
$ret = $Text * ($ConsoleWidth / $Text.Length)
}
else
{
# If Center switch specified and we're in PS console
if($Center -and $ConsoleWidth)
{
# Pad text to center it
$ret = $Text | ForEach-Object {
$_ -split [System.Environment]::NewLine | ForEach-Object {
$PadSize = [System.Math]::Floor($ConsoleWidth/2 - $_.Length/2)
$_.PadLeft($_.Length + $PadSize)
}
}
}
else
{
# Do nothing to text
$ret = $Text
}
}
# Display text
$ret | ForEach-Object -Process ([scriptblock]::Create($WriteHost.InvokeReturnAsIs() -join ' '))
}
}
<#
.Synopsis
Menu, where user selects custom controller mapping
#>
function Show-ControllerMappingMenu
{
Param
(
[Parameter(Mandatory = $true)]
[hashtable]$Custom
)
# Create menu from hashtable with button mappings
$Keys = $Custom.GetEnumerator() | ForEach-Object {$_.Key} | Sort-Object
$PropertyOrder = @(
'Number', 'Name',
'A', 'B', 'X','Y',
'LTrigger', 'RTrigger',
'LShoulder', 'RShoulder',
'LStick', 'RStick',
'Select', 'Start'
)
$i = 0
$Menu = $Keys | ForEach-Object -Begin {} {
New-Object -TypeName psobject -Property (@{Name = $_ ; Number = $i} + $Custom[$_]) |
Select-Object -Property $PropertyOrder
$i++
}
$Choice = -1
Write-Host 'Available controller mappings:' -ForegroundColor Yellow
$Menu | Format-Table -AutoSize -Property * | Out-String | Write-Host -ForegroundColor Green
while($Choice -lt 0 -or $Choice -gt $i)
{
Write-Host 'Select configuration number: ' -ForegroundColor Yellow -NoNewline
$Choice = [int](Read-Host)
}
Write-Host 'Active configuration:' -ForegroundColor Yellow
$Menu[$Choice] | Format-Table -AutoSize -Property * | Out-String | Write-Host -ForegroundColor Green
$Custom[$Menu[$Choice].Name]
}
<#
.Synopsis
Generate assembly from C# source code
.Parameter String
String containig C# source code
.Parameter File
File containig C# source code
.Parameter OutputAssembly
Path to the new assembly file
.Parameter ReferencedAssemblies
Array of paths to the assemblies, referenced by source code
.Parameter CompilerVersion
C# compiler version to use. Valid values: 'v2.0', 'v3.0', 'v3.5', 'v4.0'
.Parameter CompilerOptions
Array of the the C# compiler options: https://msdn.microsoft.com/en-us/library/6ds95cz0.aspx
.Parameter IncludeDebugInformation
True if debug information should be generated; otherwise, false.
https://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters.includedebuginformation.aspx
#>
function New-AssemblyFromSource
{
[CmdletBinding(DefaultParameterSetName = 'String')]
Param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'String')]
[ValidateNotNullOrEmpty()]
[string]$String,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'File')]
[ValidateScript({
if(!(Test-Path -Path $_ -PathType Leaf))
{
throw [System.IO.FileNotFoundException] "File not found: $_"
}
$true
})]
[ValidateNotNullOrEmpty()]
[string]$File,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({
if(!(Test-Path -Path (Split-Path -Path $_) -PathType Container))
{
throw [System.IO.FileNotFoundException] "Folder not found: $_"
}
$true
})]
[string]$OutputAssembly,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ReferencedAssemblies,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateSet('v2.0', 'v3.0', 'v3.5', 'v4.0')]
[string]$CompilerVersion,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string[]]$CompilerOptions,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]$IncludeDebugInformation
)
Process
{
# Set compiler version, if specified
if($CompilerVersion)
{
$CompilerVersionDict = New-Object -TypeName 'System.Collections.Generic.Dictionary[String,String]'
$CompilerVersionDict.Add('CompilerVersion', $CompilerVersion)
$Provider = New-Object -TypeName Microsoft.CSharp.CSharpCodeProvider -ArgumentList $CompilerVersionDict
}
else
{
$Provider = New-Object -TypeName Microsoft.CSharp.CSharpCodeProvider
}
# Create new compiler parameters' object
$CompilerParameters = New-Object -TypeName System.CodeDom.Compiler.CompilerParameters
# Set compiler parameters
$CompilerParameters.GenerateInMemory = $false
$CompilerParameters.IncludeDebugInformation = $IncludeDebugInformation
if(!$OutputAssembly)
{
$OutputAssembly = [System.IO.Path]::GetTempFileName()
}
$CompilerParameters.OutputAssembly = $OutputAssembly
# Set compiler options
if($CompilerOptions)
{
$CompilerParameters.CompilerOptions = $CompilerOptions -join ' '
}
# Add referenced assemblies
if($ReferencedAssemblies)
{
$CompilerParameters.ReferencedAssemblies.AddRange($ReferencedAssemblies)
}
if($String)
{
# Generate assembly from string
$CompilerResults = $Provider.CompileAssemblyFromSource($CompilerParameters, $String)
}
else
{
# Generate assembly from file
$CompilerResults = $Provider.CompileAssemblyFromSource($CompilerParameters, ([System.IO.File]::ReadAllText($File)))
}
if($CompilerResults.Errors.Count -gt 0)
{
# Return compiler errors if any
throw $CompilerResults.Errors
}
else
{
# Otherwise, return path to generated assembly
$CompilerResults.PathToAssembly
}
}
}
<#
.Synopsis
Get member of a class
.Parameter AssemblyDefinition
[Mono.Cecil.Reflexil.AssemblyDefinition] to get class and member from
.Parameter Class
String. Class name.
.Parameter Member
String. Member name
.Parameter MemberType
String. This is the name of the method, used to get a member of the class.
For a method: Methods
For a field: NestedTypes
#>
function Get-ClassMember
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Mono.Cecil.Reflexil.AssemblyDefinition]$AssemblyDefinition,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string]$Class,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string]$Member,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateSet('Methods', 'NestedTypes')]
[string]$MemberType
)
Process
{
($AssemblyDefinition.MainModule.Types |Where-Object {$_.Name -eq $Class})."$MemberType" |
Where-Object {$_.Name -eq $Member}
}
}
<#
.Synopsis
Get source code for the GetButton method with remapped buttons.
.Parameter Default
Hashtable. Default button mapping.
.Parameter Custom
Hashtable. Custom button mapping.
.Parameter Base
Int. This number will be subtracted from custom button's number.
Allows users to store button numbers in ini file that start from 1 (internal mappings start from 0).
#>
function New-GetButtonMethodSource
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[hashtable]$Default,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[hashtable]$Custom,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[int]$Base = 1
)
Begin
{
# Source code for the patched GetButton method
$GetButtonMethodSource = {@"
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
public class MoonInput
{
public static bool GetButton(string buttonName)
{
// string[] strArray = new string[] { "1", "2", "0", "3", "4", "5", "8", "9", "10", "11", "6", "7" };
string[] strArray = new string[] {$($NewButtonMappingArray -join ',')};
Match match = Regex.Match(buttonName, @"^(Joystick\dButton)([0-9]|1[0-1])$", RegexOptions.Singleline);
if (match.Success)
{
return Input.GetButton(match.Groups[1].Value + strArray[int.Parse(match.Groups[2].Value)]);
}
return Input.GetButton(buttonName);
}
}
"@}
}
Process
{
# Create array of button mappings
$NewButtonMappingArray = New-Object -TypeName string[] -ArgumentList $Default.Count
for($i = 0; $i -lt $Default.Count; $i++)
{
$NewButtonMappingArray[$i] = '"' + ($Custom[$Default[$i]] - $Base) + '"'
}
# Return new GetButton method wwith custom button mapping array
$GetButtonMethodSource.InvokeReturnAsIs()
}
}
#endregion
#region Configuration
# ASCII art for startup banner
$Banner = @{
Ori = @'
___ ______ ________)
/(, ) , /) /) (, / ) /) , /) (, /
/ / __ _ __ _(/ _/(/ _ /---( // __ _(/ /___, ____ _ _ _/_
/ / / (_(_ (_(/ ((_(_ (_/ )_(/_ ) / ____)(/__(_/ ((_(_ ) / (_) (_(//_)_(__
(___ / (_/ ( (_/
'@
Tool = @'
__
/ _ _ |_ _ _ | | _ _ _ _ _ _ _ |_ _ _ |
\__ (_) | ) |_ | (_) | | (- | | (- ||| (_| |_) |_ (_) (_) |
|
'@
}
# Script's configuration and internal variables
$Cfg = @{
Game = @{
Assembly = 'Assembly-CSharp.dll'
InstallDir = $null
}
Patch = @{
Class = 'MoonInput'
Member = 'GetButton'
MemberType = 'Methods'
}
Compile = @{
ReferencedAssemblies = 'mscorlib.dll', 'System.dll', 'UnityEngine.dll'
CompilerVersion = 'v2.0'
CompilerOptions = '/nostdlib'
}
Script = @{
Dir = Split-Path $script:MyInvocation.MyCommand.Path
Name = Split-Path $script:MyInvocation.MyCommand.Path -Leaf
}
Mapping = @{
Default = @{
0 = 'A'
1 = 'B'
2 = 'X'
3 = 'Y'
4 = 'LShoulder'
5 = 'RShoulder'
6 = 'Select'
7 = 'Start'
8 = 'LStick'
9 = 'RStick'
10 = 'LTrigger'
11 = 'RTrigger'
}
Custom = $null
}
Pause = @{
Msg = 'Press any key to continue...'
ExitMsg = 'Press any key to exit...'
FColor = 'Yellow'
}
ReflexilAssembly = 'Mono.Cecil.Reflexil.dll'
}
#endregion
#region Startup
# Show banner text
$Banner.Ori | Show-Text -ForegroundColor Green -Center
$Banner.Tool | Show-Text -ForegroundColor Yellow -Center
Show-Text -Text '_' -ForegroundColor Cyan -AsSeparator
Write-Verbose "Script directory: $($Cfg.Script.Dir)"
# Load Mono.Cecil.Reflexil
Join-Path -Path $Cfg.Script.Dir -ChildPath $Cfg.ReflexilAssembly |
ForEach-Object {
Write-Verbose "Loading Mono.Cecil from: $_"
try
{
Add-Type -Path $_ -ErrorAction Stop
}
catch
{
Write-Warning "Can't load $($Cfg.ReflexilAssembly), press any key to exit..."
Pause -Message $Cfg.Pause.ExitMsg $Cfg.Pause.FColor
Exit
}
}
# Import custom controller mappings
$Cfg.Mapping.Custom = Get-IniContent -FilePath (
Join-Path -Path $Cfg.Script.Dir -ChildPath ([System.IO.Path]::GetFileNameWithoutExtension($Cfg.Script.Name) + '.ini')
)
if(!$Cfg.Mapping.Custom)
{
Write-Warning -Message 'No custom controller mappings found, exiting!'
Pause -Message $Cfg.Pause.ExitMsg $Cfg.Pause.FColor
Exit
}
# Select custom mapping
$Cfg.Mapping.Custom = Show-ControllerMappingMenu -Custom $Cfg.Mapping.Custom
Pause -Message $Cfg.Pause.Msg $Cfg.Pause.FColor
# Get Ori's install directory (or script directory, of Ori's files are there)
Write-Host "Searching for '$($Cfg.Game.Assembly)'" -ForegroundColor Cyan
$Cfg.Game.InstallDir = Get-GameAssemblyDirectory -Path $Cfg.Script.Dir -GameAssembly $Cfg.Game.Assembly
if(!$Cfg.Game.InstallDir)
{
Write-Warning "Can't find $($Cfg.Game.Assembly) in $($Cfg.Game.InstallDir)"
Pause -Message $Cfg.Pause.ExitMsg $Cfg.Pause.FColor
Exit
}
# Check, if referenced assemblies are in the
# game directory, otherwise patch will fail.
Write-Host "Searching for referenced assemblies: $($Cfg.Compile.ReferencedAssemblies -join ', ')" -ForegroundColor Cyan
[array]$RefNotFound = $Cfg.Compile.ReferencedAssemblies | ForEach-Object {
$FullPath = Join-Path -Path $Cfg.Game.InstallDir -ChildPath $_
@{
Path = $FullPath
Exists = (Test-Path -Path $FullPath -PathType Leaf)
}
} | Where-Object {!$_.Exists}
if($RefNotFound)
{
Write-Warning 'Can''t find referenced assemblies:'
($RefNotFound).Path | Write-Warning
Pause -Message $Cfg.Pause.ExitMsg $Cfg.Pause.FColor
Exit
}
# Update paths to reference assemblies to fully qualified
$Cfg.Compile.ReferencedAssemblies = $Cfg.Compile.ReferencedAssemblies |
ForEach-Object {
Join-Path -Path $Cfg.Game.InstallDir -ChildPath $_
}
# Create backup
$GameAssemblyPath = Join-Path -Path $Cfg.Game.InstallDir -ChildPath $Cfg.Game.Assembly
$GameAssemblyBackupPath = $GameAssemblyPath + '.bak'
if(!(Test-Path -Path $GameAssemblyBackupPath -PathType Leaf))
{
Write-Host "Creating backup: $GameAssemblyBackupPath" -ForegroundColor Cyan
Copy-Item -Path $GameAssemblyPath -Destination $GameAssemblyBackupPath -Force
}
else
{
Write-Host "Backup already exists: $GameAssemblyBackupPath" -ForegroundColor Cyan
}
Write-Host 'Applying patch...' -ForegroundColor Cyan
# Import original assembly with Mono.Cecil
$GameAssemblyDefinition = [Mono.Cecil.Reflexil.AssemblyDefinition]::ReadAssembly($GameAssemblyPath)
# Build assembly with patched GetButton method
$SplatGBMS = $Cfg.Mapping
$SplatNAFS = $Cfg.Compile
$PatchAssemblyPath = New-GetButtonMethodSource @SplatGBMS | New-AssemblyFromSource @SplatNAFS
# Import patch assembly with Mono.Cecil
$PatchAssemblyDefinition = [Mono.Cecil.Reflexil.AssemblyDefinition]::ReadAssembly($PatchAssemblyPath)
if($PatchAssemblyDefinition -and $GameAssemblyDefinition)
{
# Get source\destination method for patch
$SplatGCM = $Cfg.Patch
$GameMethod = Get-ClassMember @SplatGCM -AssemblyDefinition $GameAssemblyDefinition
$PatchMethod = Get-ClassMember @SplatGCM -AssemblyDefinition $PatchAssemblyDefinition
try
{
# Replace GetButton method in original assembly with patched one, optimze and fix IL
[Reflexil.Utils.CecilHelper]::CloneMethodBody($PatchMethod, $GameMethod, $true)
# Save original assembly with patched GetButton method to disk
$GameAssemblyDefinition.Write($GameAssemblyPath)
# All done
Write-Host 'Success!' -ForegroundColor Cyan
}
catch
{
# Something went wrong...
Write-Warning 'Patch failed, see error messages above!'
}
}
else
{
# Something went wrong...
Write-Warning 'Patch failed, see error messages above!'
# Restore backup
Remove-Item $GameAssemblyPath -Force
Copy-Item -Path $GameAssemblyBackupPath -Destination $GameAssemblyPath -Force
}
# Delete patch assembly
Remove-Item -Path $PatchAssemblyPath -Force
# Exit
Pause -Message $Cfg.Pause.ExitMsg $Cfg.Pause.FColor
#endregion