-
Notifications
You must be signed in to change notification settings - Fork 51
/
Program.cs
305 lines (271 loc) · 12.3 KB
/
Program.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
namespace QModManager
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
internal enum Action
{
Install,
Uninstall,
RunByUser,
CleanUp
}
[Flags]
internal enum OS
{
None = 0b00,
Windows = 0b01,
Mac = 0b10,
Both = Windows | Mac,
}
internal static class Executable
{
internal static Action action = Action.RunByUser;
internal static OS os;
internal static void Main(string[] args)
{
try
{
InitAssemblyResolver();
var parsedArgs = new Dictionary<string, string>();
foreach (string arg in args)
{
if (arg.Contains("="))
{
parsedArgs = args.Select(s => s.Split(new[] { '=' }, 1)).ToDictionary(s => s[0], s => s[1]);
}
else if (arg == "-i")
action = Action.Install;
else if (arg == "-u")
action = Action.Uninstall;
else if (arg == "-c")
action = Action.CleanUp;
}
string gameRootDirectory = Path.Combine(Environment.CurrentDirectory, "../../..");
string snManagedDirectory = Path.Combine(gameRootDirectory, "Subnautica_Data", "Managed");
string bzManagedDirectory = Path.Combine(gameRootDirectory, "SubnauticaZero_Data", "Managed");
string managedDirectory;
if (Directory.Exists(snManagedDirectory))
{
managedDirectory = snManagedDirectory;
}
else if (Directory.Exists(bzManagedDirectory))
{
managedDirectory = bzManagedDirectory;
}
else
{
Console.WriteLine("Could not find Managed directory.");
Console.WriteLine("Please make sure you have installed QModManager in the right folder.");
Console.WriteLine("If the problem persists, open a bug report on NexusMods or an issue on GitHub");
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Environment.Exit(1);
return;
}
string globalgamemanagers = Path.Combine(managedDirectory, "../globalgamemanagers");
os = GetOS();
if (os == OS.Both)
{
// This runs if both windows and mac files were detected, but it should NEVER happen.
Console.WriteLine("An unexpected error has occurred.");
Console.WriteLine("Both Windows and Mac files detected!");
Console.WriteLine("Is this a Windows or a Mac environment?");
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Environment.Exit(1);
return;
}
else if (os == OS.None)
{
Console.WriteLine("Could not find any game to patch!");
Console.WriteLine("An assembly file was found, but no executable was detected.");
Console.WriteLine("Please make sure you have installed QModManager in the right folder.");
Console.WriteLine("If the problem persists, open a bug report on NexusMods or an issue on GitHub");
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Environment.Exit(1);
return;
}
if (!File.Exists(Path.Combine(managedDirectory, "Assembly-CSharp.dll")))
{
Console.WriteLine("Could not find the assembly file.");
Console.WriteLine("Please make sure you have installed QModManager in the right folder.");
Console.WriteLine("If the problem persists, open a bug report on NexusMods or an issue on GitHub");
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Environment.Exit(1);
}
if (action == Action.Install)
{
Console.WriteLine("Attempting to enable Unity audio...");
UnityAudioFixer.EnableUnityAudio();
Console.WriteLine("Unity audio enabled.");
Environment.Exit(0);
}
else if (action == Action.Uninstall)
{
Console.WriteLine("Attempting to disable Unity audio...");
UnityAudioFixer.DisableUnityAudio();
Console.WriteLine("Unity audio disabled.");
Environment.Exit(0);
}
else if (action == Action.CleanUp)
{
Console.WriteLine("Attempting to clean up Nitrox and previous QMM installs...");
CleanUp.Initialize(gameRootDirectory, managedDirectory);
Console.WriteLine("Clean-up complete.");
Environment.Exit(0);
}
else
{
Console.Write("Clean up install? [Y/N] > ");
ConsoleKey key = Console.ReadKey().Key;
Console.WriteLine();
if (key == ConsoleKey.Y)
{
Console.WriteLine("Attempting to clean up Nitrox and previous QMM installs...");
CleanUp.Initialize(gameRootDirectory, managedDirectory);
Console.WriteLine("Clean-up complete.");
Console.WriteLine();
}
Console.Write("Enable Unity sound? [Y/N] > ");
key = Console.ReadKey().Key;
Console.WriteLine();
if (key == ConsoleKey.Y)
{
Console.WriteLine("Attempting to enable Unity audio...");
UnityAudioFixer.EnableUnityAudio();
Console.WriteLine("Unity audio enabled.");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Environment.Exit(0);
}
else if (key == ConsoleKey.N)
{
Console.WriteLine("Attempting to disable Unity audio...");
UnityAudioFixer.DisableUnityAudio();
Console.WriteLine("Unity audio disabled.");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Environment.Exit(0);
}
}
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION CAUGHT!");
Console.WriteLine(e.ToString());
Console.WriteLine();
Console.ReadKey();
Environment.Exit(2);
}
}
private static OS GetOS()
{
string windowsDirectory = Path.Combine(Environment.CurrentDirectory, "../../..");
string macDirectory = Path.Combine(Environment.CurrentDirectory, "../../../../../..");
bool subnautica = false, belowzero = false;
// Check if the device is running Windows OS
bool onWindows = false, onWindowsSN, onWindowsBZ;
if (Directory.Exists(windowsDirectory))
{
try
{
// Try to get the Subnautica executable
// This method throws a lot of exceptions
onWindowsSN = Directory.GetFiles(windowsDirectory, "Subnautica.exe", SearchOption.TopDirectoryOnly).Length > 0;
onWindowsBZ = Directory.GetFiles(windowsDirectory, "SubnauticaZero.exe", SearchOption.TopDirectoryOnly).Length > 0;
onWindows = onWindowsSN || onWindowsBZ;
subnautica = subnautica || onWindowsSN;
belowzero = belowzero || onWindowsBZ;
}
catch (Exception)
{
// If an exception was thrown, the file probably isn't there
onWindows = false;
}
}
// Check if the device is running Mac OS
bool onMac = false, onMacSN, onMacBZ;
if (Directory.Exists(macDirectory))
{
try
{
// Try to get the Subnautica executable
// This method throws a lot of exceptions
// On mac, .app files act as files and folders at the same time, but they are detected as folders.
onMacSN = Directory.GetDirectories(macDirectory, "Subnautica.app", SearchOption.TopDirectoryOnly).Length > 0;
onMacBZ = Directory.GetDirectories(macDirectory, "SubnauticaZero.app", SearchOption.TopDirectoryOnly).Length > 0;
onMac = onMacSN || onMacBZ;
subnautica = subnautica || onMacSN;
belowzero = belowzero || onMacBZ;
}
catch (Exception)
{
// If an exception was thrown, the file probably isn't there
onMac = false;
}
}
var os = OS.None;
if (onWindows)
{
os |= OS.Windows;
}
if (onMac)
{
os |= OS.Mac;
}
return os;
}
private static void InitAssemblyResolver()
{
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblies;
}
internal static string GameRootDirectory => Path.Combine(Environment.CurrentDirectory, "../../..");
internal static string BepInExRootDirectory => Path.Combine(GameRootDirectory, "BepInEx");
internal static string BepInExAssemblyDirectory => Path.Combine(BepInExRootDirectory, "core");
internal static string QModManagerPluginDirectory => Path.Combine(BepInExRootDirectory, "plugins", "QModManager");
internal static string QModManagerPatcherDirectory => Path.Combine(BepInExRootDirectory, "patchers", "QModManager");
private static Assembly ResolveAssemblies(object sender, ResolveEventArgs e)
{
var name = new AssemblyName(e.Name);
if (Utility.TryResolveDllAssembly(name, BepInExRootDirectory, out var assembly) || // First try BepInEx assemblies
Utility.TryResolveDllAssembly(name, QModManagerPluginDirectory, out assembly) || // Then QModManager plugins
Utility.TryResolveDllAssembly(name, QModManagerPatcherDirectory, out assembly)) // Then QModManager patchers
{
return assembly;
}
else
{
return null;
}
}
#region Disable exit
internal static void DisableExit()
{
DisableExitButton();
Console.CancelKeyPress += CancelKeyPress;
Console.TreatControlCAsInput = true;
}
private static void DisableExitButton()
{
EnableMenuItem(GetSystemMenu(GetConsoleWindow(), false), 0xF060, 0x1);
}
private static void CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
}
[DllImport("user32.dll")] private static extern int EnableMenuItem(IntPtr tMenu, int targetItem, int targetStatus);
[DllImport("user32.dll")] private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("kernel32.dll", ExactSpelling = true)] private static extern IntPtr GetConsoleWindow();
#endregion
}
}