This repository has been archived by the owner on Aug 23, 2018. It is now read-only.
/
Installer.cs
236 lines (201 loc) · 8.89 KB
/
Installer.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
namespace SubnauticaWatcherInstaller
{
#region imports
using System;
using System.IO;
using System.Linq;
using Microsoft.Win32;
using Mono.Cecil;
using Mono.Cecil.Cil;
using NLog;
#endregion
internal delegate void MessageCallback(string message);
internal class Installer
{
private MessageCallback log;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
internal Installer(MessageCallback log)
{
this.log = log;
SubnauticaPath = Path.Combine(SteamPath, @"steamapps\common\Subnautica");
}
private string SteamPath =>
Registry.GetValue(@"HKEY_CURRENT_USER\Software\Valve\Steam", "SteamPath", null) as string;
public string SubnauticaPath { get; set; }
private string SubnauticaManagedPath => Path.Combine(SubnauticaPath, @"Subnautica_Data\Managed");
private string SubnauticaDllPath => Path.Combine(SubnauticaManagedPath, @"Assembly-CSharp.dll");
private string SubnauticaDllBackupPath => SubnauticaDllPath + ".subnautica-watcher-backup";
internal int Install()
{
Logger.Debug("Install Requested");
Logger.Debug($"Changing working dir: {SubnauticaManagedPath}");
var initialDirectory = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(SubnauticaManagedPath);
try
{
var exitCode = ValidatePaths();
if (exitCode != 0) return exitCode;
exitCode = BackupSubnauticaDll();
if (exitCode != 0) return exitCode;
exitCode = UpdateSubnauticaDll();
return exitCode != 0 ? exitCode : 0;
}
finally
{
Logger.Debug($"Restoring working dir: {initialDirectory}");
Directory.SetCurrentDirectory(initialDirectory);
}
}
private int BackupSubnauticaDll()
{
// Always create a backup, safer this way.
var backupFilename = Path.GetFileName(SubnauticaDllBackupPath);
backupFilename += $".{DateTime.Now:yyyyMMddTHHmmss}";
Logger.Debug("Backup of DLL requested.");
try
{
File.Copy(
SubnauticaDllPath,
backupFilename);
log($"Backup of Assembly-CSharp.dll created as '{backupFilename}'.");
}
catch (IOException ex)
{
log("Error: error attempting to create backup of Assembly-CSharp.dll.");
log($"Error: {ex.Message}");
Logger.Error(ex);
return -4;
}
return 0;
}
internal int ValidatePaths()
{
Logger.Debug("Validate Paths requested.");
if (Directory.Exists(SteamPath))
{
log($"Found Steam install directory. [{SteamPath}]");
if (Directory.Exists(SubnauticaPath))
{
log($"Found Subnautica install directory. [{SubnauticaPath}]");
if (Directory.Exists(SubnauticaManagedPath))
{
log($"Found Subnautica Data directory. [{SubnauticaManagedPath}]");
return 0;
}
log("Error: Unable to determine Subnautica Data directory.");
return -3;
}
log("Error: Unable to determine Subnautica install directory.");
return -2;
}
log("Error: Unable to determine Steam install directory.");
return -1;
}
private int UpdateSubnauticaDll()
{
log("Updating Assembly-CSharp.dll to load SubnauticaWatcherMod.");
var targetAssembly = AssemblyDefinition.ReadAssembly(SubnauticaDllPath);
if (!CheckPatched(targetAssembly))
try
{
var modAssemblyPath = Path.Combine(SubnauticaManagedPath, @"SubnauticaWatcherMod.dll");
var modAssembly = AssemblyDefinition.ReadAssembly(modAssemblyPath);
var modClass = modAssembly.MainModule.GetType("SubnauticaWatcherMod.Patching.Patcher");
var modMethod = modClass.Methods.Single(x => x.Name == "Patch");
var targetClass = targetAssembly.MainModule.GetType("GameInput");
var targetMethod = targetClass.Methods.First(x => x.Name == "Awake");
targetMethod.Body.GetILProcessor()
.InsertBefore(
targetMethod.Body.Instructions[0],
Instruction.Create(OpCodes.Call, targetMethod.Module.Import(modMethod)));
log("Saving Assembly.");
targetAssembly.Write(SubnauticaDllPath);
log("Assembly-CSharp.dll successfully updated.");
}
catch (Exception ex)
{
log("Error: Failed to update Assembly-CSharp.dll.");
log($"Error: {ex.Message}");
Logger.Error(ex);
return -5;
}
else
log("Assembly-CSharp.dll appears to have already been modified; no action taken.");
return 0;
}
internal bool IsPatched
{
get
{
try
{
return CheckPatched(AssemblyDefinition.ReadAssembly(SubnauticaDllPath));
}
catch
{
return false;
}
}
}
private static bool CheckPatched(AssemblyDefinition targetAssembly)
{
Logger.Debug("Checking DLL for existin patch.");
var type = targetAssembly.MainModule.GetType("GameInput");
var methodDefinition = type.Methods.First(x => x.Name == "Awake");
// Something else might have updated the DLL, so the patch might no longer
// be the first instruction.
var hasPatchInstruction = methodDefinition.Body.Instructions
.Where(x => x.OpCode == OpCodes.Call)
.Any(
x => (x.Operand as MethodReference)?.DeclaringType.FullName
== "SubnauticaWatcherMod.Patching.Patcher");
return hasPatchInstruction;
}
internal int Uninstall()
{
// Need to be in the correct folder to resolve dependencies correctly
Logger.Debug("Uninstall Requested.");
var initialDirectory = Directory.GetCurrentDirectory();
Logger.Debug($"Changing working dir: {SubnauticaManagedPath}");
Directory.SetCurrentDirectory(SubnauticaManagedPath);
if (!IsPatched)
{
log("Error: Not Patched, uninstall not required.");
return -8;
}
if (BackupSubnauticaDll() != 0) return -9;
var targetAssembly = AssemblyDefinition.ReadAssembly(SubnauticaDllPath);
var type = targetAssembly.MainModule.GetType("GameInput");
var methodDefinition = type.Methods.First(x => x.Name == "Awake");
// Something else might have updated the DLL, so the patch might no longer
// be the first instruction.
try
{
var instruction = methodDefinition.Body.Instructions
.Where(
x => x.OpCode
== OpCodes.Call)
.First(x => (x.Operand as MethodReference)
?.DeclaringType.FullName
== "SubnauticaWatcherMod.Patching.Patcher");
log($"Removing {instruction.Operand}");
methodDefinition.Body.GetILProcessor().Remove(instruction);
log($"Removed {instruction.Operand}");
log("Saving Assembly.");
targetAssembly.Write(SubnauticaDllPath);
}
catch (Exception ex)
{
log($"Error: Exception removing patch: {ex.Message}");
Logger.Error(ex);
return -7;
}
finally
{
Logger.Debug($"Restoring working dir: {initialDirectory}");
Directory.SetCurrentDirectory(initialDirectory);
}
return 0;
}
}
}