-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHPEInstallation.cs
165 lines (134 loc) · 4.53 KB
/
HPEInstallation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Diagnostics;
using System.Security.Principal;
namespace hpesuperpower;
sealed class HPEInstallation
{
public const string StablePath = @"C:\Program Files\Google\Play Games\current";
public const string DevPath = @"C:\Program Files\Google\Play Games Developer Emulator\current";
public readonly bool Dev;
public readonly string installPath;
public readonly string bios;
public readonly string serviceExe;
public readonly string serviceConfig;
public readonly string aggregateImg;
public readonly string stockBios;
public readonly string stockServiceExe;
public readonly string stockServiceConfig;
public readonly string stockBootImg;
public HPEInstallation(bool dev)
{
Dev = dev;
installPath = dev ? DevPath : StablePath;
bios = Path.Combine(installPath, @"emulator\avd\bios.rom");
serviceExe = Path.Combine(installPath, @"service\Service.exe");
serviceConfig = Path.Combine(installPath, @"service\Service.exe.config");
aggregateImg = Path.Combine(installPath, @"emulator\avd\aggregate.img");
stockBios = Path.Combine(installPath, @"emulator\avd\bios.rom.bak");
stockServiceExe = Path.Combine(installPath, @"service\Service.exe.bak");
stockServiceConfig = Path.Combine(installPath, @"service\Service.exe.config.bak");
stockBootImg = Path.Combine(installPath, @"emulator\avd\boot_a.img");
}
static bool CanWrite()
{
bool isElevated;
using (var identity = WindowsIdentity.GetCurrent())
{
var principal = new WindowsPrincipal(identity);
isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
return isElevated;
}
public bool Check(bool write)
{
if (!Directory.Exists(installPath))
{
Console.WriteLine("Google Play Games have not installed. Get it at https://play.google.com/googleplaygames or https://developer.android.com/games/playgames/emulator");
return false;
}
if (write && Process.GetProcesses().Any(p => p.GetFileNameOrDefault() == serviceExe))
{
Console.WriteLine($"Please quit Google Play Games.");
return false;
}
if (write && !CanWrite())
{
Console.WriteLine("Please run this program from Elevated Command Prompt");
return false;
}
return true;
}
public void Unlock()
{
if (!File.Exists(stockBios))
{
Console.WriteLine("\n\n############# Backup bios.rom");
File.Copy(bios, stockBios);
}
Console.WriteLine("\n\n############# Patch bios.rom");
UnlockCommand.PatchBios(bios);
if (!File.Exists(stockServiceConfig))
{
Console.WriteLine("\n\n############# Backup Service.exe.config");
File.Copy(serviceConfig, stockServiceConfig);
}
Console.WriteLine("\n\n############# Patch Service.exe.config");
UnlockCommand.PatchKernelCmdline(serviceConfig);
if (!File.Exists(stockServiceExe))
{
Console.WriteLine("\n\n############# Backup Service.exe");
File.Copy(serviceExe, stockServiceExe);
}
if (!Dev)
{
Console.WriteLine("\n\n############# Patch Service.exe");
UnlockCommand.PatchServiceExe(stockServiceExe, serviceExe);
}
}
public void Patch(FileInfo magiskApk)
{
var stockBoot = new FileInfo(stockBootImg);
if (!stockBoot.Exists)
{
Console.WriteLine("\n\n############# Extract boot.img");
PartitionCommand.Extract(aggregateImg, "boot_a", stockBoot);
}
Console.WriteLine("\n\n############# Patch boot.img");
var newBoot = BootPatchCommand.BootPatch(magiskApk, stockBoot);
Console.WriteLine("\n\n############# Flash patched boot img");
PartitionCommand.Flash(aggregateImg, "boot_a", newBoot);
Console.WriteLine("\n\n############# Cleanup");
BootPatchCommand.Cleanup();
}
public void FlashCommand(string partname, FileInfo infile, bool superpower)
{
bool patch = partname == "boot_a" && superpower;
if (patch)
infile = BootPatchCommand.SuperpowerPatch(infile);
PartitionCommand.Flash(aggregateImg, partname, infile);
if (patch)
BootPatchCommand.Cleanup();
}
public void Restore()
{
if (!File.Exists(stockBios))
{
Console.WriteLine("No backup founded.");
return;
}
Console.WriteLine("\n\n############# Restore bios.rom");
File.Copy(stockBios, bios, true);
Console.WriteLine("\n\n############# Restore Service.exe");
File.Copy(stockServiceExe, serviceExe, true);
Console.WriteLine("\n\n############# Restore Service.exe.config");
File.Copy(stockServiceConfig, serviceConfig, true);
if (File.Exists(stockBootImg))
{
Console.WriteLine("\n\n############# Restore stock boot");
PartitionCommand.Flash(aggregateImg, "boot_a", new FileInfo(stockBootImg));
}
else
{
Console.WriteLine("Warning: stock boot image not found, skipped.");
}
}
}