Patch-GupsGuiResources.ps1 4.8 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. # Stage and verify the DLL/PDB before replacing either shipped file. No runtime
  57. # instructions may change except these resource strings; preserve debug symbols.
  58. $taskStage = Join-Path ([IO.Path]::GetTempPath()) ('GupsGuiPatch-' + [Guid]::NewGuid().ToString('N'))
  59. [IO.Directory]::CreateDirectory($taskStage) | Out-Null
  60. $taskStagedDll = Join-Path $taskStage 'GUPS.Obfuscator.Editor.dll'
  61. $taskWriter = [Mono.Cecil.WriterParameters]::new()
  62. $taskWriter.WriteSymbols = $true
  63. $taskAssembly.Write($taskStagedDll, $taskWriter)
  64. $taskCheck = [Mono.Cecil.AssemblyDefinition]::ReadAssembly($taskStagedDll, $taskReader)
  65. try {
  66. $taskAfter = (Get-InstructionSnapshot $taskCheck) -join "`n"
  67. if ($taskAfter -cne $taskBefore.Replace($taskOldPrefix, $taskNewPrefix)) { throw 'Unexpected IL changes in patched DLL.' }
  68. if (-not $taskCheck.MainModule.HasSymbols -or $taskCheck.MainModule.Mvid -ne $taskAssembly.MainModule.Mvid) {
  69. throw 'Debug symbols or module identity changed unexpectedly.'
  70. }
  71. } finally { $taskCheck.Dispose() }
  72. $taskAssembly.Dispose()
  73. [IO.File]::Copy($taskStagedDll, $taskDll, $true)
  74. [IO.File]::Copy([IO.Path]::ChangeExtension($taskStagedDll, '.pdb'), [IO.Path]::ChangeExtension($taskDll, '.pdb'), $true)
  75. Write-Output "PASS: patched $taskChanged GUI resource paths; other IL, module identity and symbols verified."
  76. } finally {
  77. $taskAssembly.Dispose()
  78. if ($taskStage) {
  79. # Only remove the two exact staging files, never recurse over a computed path.
  80. foreach ($taskName in @('GUPS.Obfuscator.Editor.dll', 'GUPS.Obfuscator.Editor.pdb')) {
  81. $taskFile = Join-Path $taskStage $taskName
  82. if (Test-Path -LiteralPath $taskFile) { Remove-Item -LiteralPath $taskFile }
  83. }
  84. [IO.Directory]::Delete($taskStage, $false)
  85. }
  86. }