@@ -27,6 +27,7 @@ public static void PatchBios(string path)
2727 Console . WriteLine ( "hex not found, the bios might already been patched, nothing to do." ) ;
2828 return ;
2929 }
30+
3031 Console . WriteLine ( $ "bios patched at { off : X8} ") ;
3132 to . CopyTo ( bios . AsSpan ( off ) ) ;
3233
@@ -40,50 +41,93 @@ public static void PatchKernelCmdline(string serviceConfigPath)
4041 const string to = "androidboot.verifiedbootstate=orange " ;
4142 var xml = new XmlDocument ( ) ;
4243 xml . Load ( serviceConfigPath ) ;
43- var value = xml . SelectNodes ( "/configuration/applicationSettings/Google.Hpe.Service.Properties.EmulatorSettings/setting[@name='EmulatorGuestParameters']/value" ) ! . Item ( 0 ) ! ;
44+ var value = xml . SelectNodes (
45+ "/configuration/applicationSettings/Google.Hpe.Service.Properties.EmulatorSettings/setting[@name='EmulatorGuestParameters']/value" )
46+ ! . Item ( 0 ) ! ;
4447
4548 var oldParam = value . InnerText ;
4649 if ( oldParam . Contains ( to ) )
4750 {
4851 Console . WriteLine ( "Service.exe.config already modified, nothing to do." ) ;
4952 return ;
5053 }
54+
5155 value . InnerText = to + value . InnerText ;
5256 xml . Save ( serviceConfigPath ) ;
5357 Console . WriteLine ( "Service.exe.config modified." ) ;
5458 }
5559
56- public static void PatchServiceExe ( string exePath , string outPath )
60+ public static void PatchServiceExe ( string exePath , string outPath , bool isDev )
5761 {
5862 // remove foreground white-list
5963
6064 using var popCwd = new Pushd ( Path . GetDirectoryName ( exePath ) ! ) ;
6165
62- var assembly = AssemblyDefinition . ReadAssembly ( exePath , new ReaderParameters { AssemblyResolver = new DefaultAssemblyResolver ( ) } ) ;
66+ var assembly = AssemblyDefinition . ReadAssembly ( exePath ) ;
6367 var module = assembly . MainModule ;
6468
65- // System.Void Google.Hpe.Service.AppSession.AppSessionScope::HandleEmulatorSurfaceStateUpdate(Google.Hpe.Service.Emulator.Surface.EmulatorSurfaceState,Google.Hpe.Service.Emulator.Surface.EmulatorSurfaceState)
66- var AppSessionScope = module . GetType ( "Google.Hpe.Service.AppSession.AppSessionScope" ) ;
67- var method = AppSessionScope . Methods . Single ( x => x . Name == "HandleEmulatorSurfaceStateUpdate" ) ;
68- var instructions = method . Body . Instructions ;
69+ Console . WriteLine ( $ "Version: { assembly . Name . FullName } ") ;
6970
70- var begin = instructions . FirstOrDefault ( p => p . Operand is FieldDefinition f && f . Name == "_transientForegroundPackages" ) ;
71+ #region Foreground app limit
7172
72- if ( begin == null )
73+ if ( ! isDev )
7374 {
74- Console . WriteLine ( "nothing to patch. " ) ;
75- return ;
75+ Console . WriteLine ( "* Remove Foreground app limit. * " ) ;
76+ RemoveForegroundLimit ( ) ;
7677 }
77- Console . WriteLine ( $ "Version: { assembly . Name . FullName } ") ;
7878
79- var idx = instructions . IndexOf ( begin ) ;
80- Console . WriteLine ( $ "Patch Instruction at idx { idx } , offset IL_{ begin . Offset : X4} ") ;
79+ void RemoveForegroundLimit ( )
80+ {
81+ // System.Void Google.Hpe.Service.AppSession.AppSessionScope::HandleEmulatorSurfaceStateUpdate(Google.Hpe.Service.Emulator.Surface.EmulatorSurfaceState,Google.Hpe.Service.Emulator.Surface.EmulatorSurfaceState)
82+ var AppSessionScope = module . GetType ( "Google.Hpe.Service.AppSession.AppSessionScope" ) ;
83+ var method = AppSessionScope . Methods . Single ( x => x . Name == "HandleEmulatorSurfaceStateUpdate" ) ;
84+ var instructions = method . Body . Instructions ;
85+ var begin = instructions . FirstOrDefault ( p =>
86+ p . Operand is FieldDefinition { Name : "_transientForegroundPackages" } ) ;
87+
88+ if ( begin == null )
89+ {
90+ Console . WriteLine ( "nothing to patch." ) ;
91+ return ;
92+ }
93+
94+ var idx = instructions . IndexOf ( begin ) ;
95+ Console . WriteLine ( $ "Patch Instruction at idx { idx } , offset IL_{ begin . Offset : X4} ") ;
96+
97+ while ( instructions [ idx ] . OpCode != OpCodes . Leave_S )
98+ {
99+ instructions . RemoveAt ( idx ) ;
100+ }
101+ }
102+
103+ #endregion Foreground app limit
104+
105+ #region Houdini
81106
82- while ( instructions [ idx ] . OpCode != OpCodes . Leave_S )
107+ Console . WriteLine ( "* Enable ARM translation layer (libhoudini). *" ) ;
108+ EnableHoudini ( isDev ) ;
109+
110+ void EnableHoudini ( bool dev )
83111 {
84- instructions . RemoveAt ( idx ) ;
112+ string className = dev
113+ ? "Google.Hpe.Service.KiwiEmulator.EmulatorFeaturePolicyDev"
114+ : "Google.Hpe.Service.KiwiEmulator.EmulatorFeaturePolicyProd" ;
115+
116+ var clazz = module . GetType ( className ) ;
117+ var field = clazz . Fields . Single ( it => it . Name . Contains ( "IsHoudiniEnabled" ) ) ;
118+ foreach ( var ctor in clazz . Methods . Where ( it => it . IsConstructor && ! it . IsStatic ) )
119+ {
120+ var proc = ctor . Body . GetILProcessor ( ) ;
121+ var ret = ctor . Body . Instructions . Last ( ) ;
122+ proc . InsertBefore ( ret , proc . Create ( OpCodes . Ldarg_0 ) ) ;
123+ proc . InsertBefore ( ret , proc . Create ( OpCodes . Ldc_I4_1 ) ) ;
124+ proc . InsertBefore ( ret , proc . Create ( OpCodes . Stfld , field ) ) ;
125+ }
85126 }
86127
128+
129+ #endregion Houdini
130+
87131 assembly . Write ( outPath ) ;
88132 }
89- }
133+ }
0 commit comments