diff --git a/Exts.cs b/Exts.cs index e2d9e53..cc7ee61 100644 --- a/Exts.cs +++ b/Exts.cs @@ -1,6 +1,20 @@ +using System.Diagnostics; + static class Exts { public static string Unix(this string it) { return it.ReplaceLineEndings("\n"); } public static string NoEOL(this string it) { return it.ReplaceLineEndings(" "); } public static void Z(this int it) { if (it != 0) throw new Exception("result not zero"); } + + public static string? GetFileNameOrDefault(this Process p) + { + try + { + return p.MainModule?.FileName; + } + catch + { + return null; + } + } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 97f33e6..13f93e5 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using System.Diagnostics; using System.IO.Compression; using System.Security.Cryptography; using System.Security.Principal; @@ -144,6 +145,12 @@ static void Main(string[] args) return; } + if (Process.GetProcesses().Any(p => p.GetFileNameOrDefault() == serviceExe)) + { + Console.WriteLine($"Please quit Google Play Games."); + return; + } + if (!File.Exists(stockBios)) { Console.WriteLine("\n\n############# Backup bios.rom"); @@ -186,30 +193,39 @@ static void Main(string[] args) static void PatchServiceExe(string exePath, string outPath) { Environment.CurrentDirectory = Path.GetDirectoryName(exePath)!; - var assembly = AssemblyDefinition.ReadAssembly(exePath, new ReaderParameters { AssemblyResolver = new DefaultAssemblyResolver() }); - var module = assembly.MainModule; + try + { - // System.Void Google.Hpe.Service.AppSession.AppSessionScope::HandleEmulatorSurfaceStateUpdate(Google.Hpe.Service.Emulator.Surface.EmulatorSurfaceState,Google.Hpe.Service.Emulator.Surface.EmulatorSurfaceState) - var AppSessionScope = module.GetType("Google.Hpe.Service.AppSession.AppSessionScope"); - var method = AppSessionScope.Methods.Single(x => x.Name == "HandleEmulatorSurfaceStateUpdate"); + var assembly = AssemblyDefinition.ReadAssembly(exePath, new ReaderParameters { AssemblyResolver = new DefaultAssemblyResolver() }); + var module = assembly.MainModule; - Instruction? instruct = method.Body.Instructions.FirstOrDefault(p => p.Operand is FieldDefinition f && f.Name == "_transientForegroundPackages"); + // System.Void Google.Hpe.Service.AppSession.AppSessionScope::HandleEmulatorSurfaceStateUpdate(Google.Hpe.Service.Emulator.Surface.EmulatorSurfaceState,Google.Hpe.Service.Emulator.Surface.EmulatorSurfaceState) + var AppSessionScope = module.GetType("Google.Hpe.Service.AppSession.AppSessionScope"); + var method = AppSessionScope.Methods.Single(x => x.Name == "HandleEmulatorSurfaceStateUpdate"); + var instructions = method.Body.Instructions; - if (instruct == null) - { - Console.WriteLine("nothing to patch."); - return; - } + var begin = instructions.FirstOrDefault(p => p.Operand is FieldDefinition f && f.Name == "_transientForegroundPackages"); - Console.WriteLine($"Patch Instruction at {method.Body.Instructions.IndexOf(instruct)}"); + if (begin == null) + { + Console.WriteLine("nothing to patch."); + return; + } - var processor = method.Body.GetILProcessor(); - var newInstruction = processor.Create(OpCodes.Ret); + var idx = instructions.IndexOf(begin); + Console.WriteLine($"Patch Instruction at idx {idx}, offset IL_{begin.Offset:X4}"); - processor.Replace(instruct, newInstruction); + while (instructions[idx].OpCode != OpCodes.Leave_S) + { + instructions.RemoveAt(idx); + } - assembly.Write(outPath); - Environment.CurrentDirectory = StartDirectory; + assembly.Write(outPath); + } + finally + { + Environment.CurrentDirectory = StartDirectory; + } } static byte[] ExtractBoot(string diskPath)