Patch-GupsGuiResources.ps1 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. param(
  2. [Parameter(Mandatory = $true)][string]$UnityPath
  3. )
  4. $ErrorActionPreference = 'Stop'
  5. $taskPackage = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..'))
  6. $taskCecil = Join-Path (Split-Path -Parent $UnityPath) 'Data/Managed/Unity.Cecil.dll'
  7. if (-not (Test-Path -LiteralPath $taskCecil)) { throw "Unity Cecil library not found: $taskCecil" }
  8. Add-Type -Path $taskCecil
  9. $taskRelativeDll = 'ThirdParty/GUPS/Obfuscator/Editor/Plugins/GUPS.Obfuscator.Editor.dll'
  10. $taskDll = Join-Path $taskPackage $taskRelativeDll
  11. $taskOldPrefix = 'Assets/GUPS/Obfuscator/Editor/Gui/Resources/'
  12. $taskNewPrefix = 'Packages/come.app.runtimes/ThirdParty/GUPS/Obfuscator/Editor/Gui/Resources/'
  13. $taskResources = Join-Path $taskPackage 'ThirdParty/GUPS/Obfuscator/Editor/Gui/Resources'
  14. function Get-InstructionSnapshot($Assembly) {
  15. foreach ($taskType in $Assembly.MainModule.GetTypes()) {
  16. foreach ($taskMethod in $taskType.Methods) {
  17. if (-not $taskMethod.HasBody) { continue }
  18. $taskMethod.FullName
  19. foreach ($taskInstruction in $taskMethod.Body.Instructions) { $taskInstruction.ToString() }
  20. }
  21. }
  22. }
  23. $taskReader = [Mono.Cecil.ReaderParameters]::new()
  24. $taskReader.ReadSymbols = $true
  25. $taskReader.InMemory = $true
  26. $taskAssembly = [Mono.Cecil.AssemblyDefinition]::ReadAssembly($taskDll, $taskReader)
  27. $taskStage = $null
  28. try {
  29. if ($taskAssembly.Name.HasPublicKey) { throw 'The vendor DLL is now signed; review the SDK update before patching.' }
  30. $taskBefore = (Get-InstructionSnapshot $taskAssembly) -join "`n"
  31. $taskChanged = 0
  32. $taskFound = [Collections.Generic.HashSet[string]]::new()
  33. foreach ($taskType in $taskAssembly.MainModule.GetTypes()) {
  34. foreach ($taskMethod in $taskType.Methods) {
  35. if (-not $taskMethod.HasBody) { continue }
  36. foreach ($taskInstruction in $taskMethod.Body.Instructions) {
  37. if ($taskInstruction.OpCode.Name -ne 'ldstr') { continue }
  38. $taskValue = [string]$taskInstruction.Operand
  39. if ($taskValue.StartsWith($taskOldPrefix, [StringComparison]::Ordinal)) {
  40. $taskValue = $taskNewPrefix + $taskValue.Substring($taskOldPrefix.Length)
  41. $taskInstruction.Operand = $taskValue
  42. $taskChanged++
  43. }
  44. if ($taskValue.StartsWith($taskNewPrefix, [StringComparison]::Ordinal)) {
  45. $taskResource = $taskValue.Substring($taskNewPrefix.Length)
  46. if (-not (Test-Path -LiteralPath (Join-Path $taskResources $taskResource) -PathType Leaf)) {
  47. throw "Missing GUPS GUI resource: $taskResource"
  48. }
  49. [void]$taskFound.Add($taskResource)
  50. }
  51. }
  52. }
  53. }
  54. if ($taskFound.Count -ne 14) { throw "Expected 14 GUI resources, found $($taskFound.Count). Review the SDK update." }
  55. if ($taskChanged -eq 0) { Write-Output 'PASS: all 14 GUPS GUI paths already target the package.'; return }
  56. $taskStage = Join-Path ([IO.Path]::GetTempPath()) ('GupsGuiPatch-' + [Guid]::NewGuid().ToString('N'))
  57. [IO.Directory]::CreateDirectory($taskStage) | Out-Null
  58. $taskStagedDll = Join-Path $taskStage 'GUPS.Obfuscator.Editor.dll'
  59. $taskWriter = [Mono.Cecil.WriterParameters]::new()
  60. $taskWriter.WriteSymbols = $true
  61. $taskAssembly.Write($taskStagedDll, $taskWriter)
  62. $taskCheck = [Mono.Cecil.AssemblyDefinition]::ReadAssembly($taskStagedDll, $taskReader)
  63. try {
  64. $taskAfter = (Get-InstructionSnapshot $taskCheck) -join "`n"
  65. if ($taskAfter -cne $taskBefore.Replace($taskOldPrefix, $taskNewPrefix)) { throw 'Unexpected IL changes in patched DLL.' }
  66. if (-not $taskCheck.MainModule.HasSymbols -or $taskCheck.MainModule.Mvid -ne $taskAssembly.MainModule.Mvid) {
  67. throw 'Debug symbols or module identity changed unexpectedly.'
  68. }
  69. } finally { $taskCheck.Dispose() }
  70. $taskAssembly.Dispose()
  71. [IO.File]::Copy($taskStagedDll, $taskDll, $true)
  72. [IO.File]::Copy([IO.Path]::ChangeExtension($taskStagedDll, '.pdb'), [IO.Path]::ChangeExtension($taskDll, '.pdb'), $true)
  73. Write-Output "PASS: patched $taskChanged GUI resource paths; other IL, module identity and symbols verified."
  74. } finally {
  75. $taskAssembly.Dispose()
  76. if ($taskStage) {
  77. foreach ($taskName in @('GUPS.Obfuscator.Editor.dll', 'GUPS.Obfuscator.Editor.pdb')) {
  78. $taskFile = Join-Path $taskStage $taskName
  79. if (Test-Path -LiteralPath $taskFile) { Remove-Item -LiteralPath $taskFile }
  80. }
  81. [IO.Directory]::Delete($taskStage, $false)
  82. }
  83. }