Skip to content

Commit

Permalink
Update Service.exe Patch
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Sep 11, 2024
1 parent 6197db4 commit a64e9e3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
14 changes: 14 additions & 0 deletions Exts.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
50 changes: 33 additions & 17 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Security.Principal;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a64e9e3

Please sign in to comment.