forked from LavaGang/MelonLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMelonLoaderBase.cs
187 lines (174 loc) · 7.44 KB
/
MelonLoaderBase.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
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using Harmony;
namespace MelonLoader
{
public static class MelonLoaderBase
{
internal static MelonGameAttribute CurrentGameAttribute = null;
internal static bool _IsVRChat = false;
public static bool IsVRChat { get => _IsVRChat; }
internal static bool _IsBoneworks = false;
public static bool IsBoneworks { get => _IsBoneworks; }
private static void Initialize()
{
Setup();
MelonConsole.Check();
AssemblyGenerator.Check();
if (!AssemblyGenerator.HasGeneratedAssembly)
return;
MelonHandler.LoadAll(true);
if (!MelonHandler.HasMelons)
return;
MelonPrefs.Setup();
MelonHandler.OnPreInitialization();
}
private static void Startup()
{
if (!AssemblyGenerator.HasGeneratedAssembly)
return;
SetupSupport();
WelcomeLog();
MelonHandler.LoadAll();
MelonHandler.LogAndPrune();
if (!MelonHandler.HasMelons)
return;
MelonPrefs.Setup();
AddUnityDebugLog();
MelonHandler.OnApplicationStart();
if (!MelonHandler.HasMelons)
SupportModule.Destroy();
}
internal static void Quit()
{
if (MelonHandler.HasMelons)
MelonPrefs.SaveConfig();
HarmonyInstance.UnpatchAllInstances();
UNLOAD();
if (IsQuitFix()) Process.GetCurrentProcess().Kill();
}
private static void Setup()
{
FixCurrentBaseDirectory();
AppDomain.CurrentDomain.UnhandledException += ExceptionHandler;
CurrentGameAttribute = new MelonGameAttribute(Imports.GetCompanyName(), Imports.GetProductName());
if (Imports.IsIl2CppGame())
{
_IsVRChat = CurrentGameAttribute.IsGame("VRChat", "VRChat");
_IsBoneworks = CurrentGameAttribute.IsGame("Stress Level Zero", "BONEWORKS");
}
}
private static void SetupSupport()
{
if (Imports.IsIl2CppGame())
{
if (_IsVRChat)
MelonHandler.Assembly_CSharp = Assembly.Load("Assembly-CSharp");
UnhollowerSupport.Initialize();
}
SupportModule.Initialize();
}
private static void WelcomeLog()
{
MelonLogger.Log("------------------------------");
MelonLogger.Log("Unity " + _UnityVersion);
MelonLogger.Log("OS: " + Environment.OSVersion.ToString());
MelonLogger.Log("------------------------------");
MelonLogger.Log("Name: " + CurrentGameAttribute.GameName);
MelonLogger.Log("Developer: " + CurrentGameAttribute.Developer);
MelonLogger.Log("Type: " + (Imports.IsIl2CppGame() ? "Il2Cpp" : (IsOldMono() ? "Mono" : "MonoBleedingEdge")));
MelonLogger.Log("------------------------------");
MelonLogger.Log("Using v" + BuildInfo.Version + " Open-Beta");
MelonLogger.Log("------------------------------");
}
private static void AddUnityDebugLog()
{
SupportModule.UnityDebugLog("--------------------------------------------------------------------------------------------------");
SupportModule.UnityDebugLog("~ This Game has been MODIFIED using MelonLoader. DO NOT report any issues to the Developers! ~");
SupportModule.UnityDebugLog("--------------------------------------------------------------------------------------------------");
}
public static void FixCurrentBaseDirectory()
{
((AppDomainSetup)typeof(AppDomain).GetProperty("SetupInformationNoCopy", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(AppDomain.CurrentDomain, new object[0])).ApplicationBase = Imports.GetGameDirectory();
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
}
private static void ExceptionHandler(object sender, UnhandledExceptionEventArgs e) => MelonLogger.LogError((e.ExceptionObject as Exception).ToString());
private static string _UserDataPath = null;
public static string UserDataPath
{
get
{
if (_UserDataPath == null)
{
_UserDataPath = Path.Combine(Imports.GetGameDirectory(), "UserData");
if (!Directory.Exists(_UserDataPath))
Directory.CreateDirectory(_UserDataPath);
}
return _UserDataPath;
}
}
private static string _UnityVersion = null;
public static string UnityVersion
{
get
{
if (_UnityVersion != null)
return _UnityVersion;
string exepath = Imports.GetExePath();
string ggm_path = Path.Combine(Imports.GetGameDataDirectory(), "globalgamemanagers");
if (!File.Exists(ggm_path))
{
FileVersionInfo versioninfo = FileVersionInfo.GetVersionInfo(exepath);
if ((versioninfo == null) || string.IsNullOrEmpty(versioninfo.FileVersion))
return "UNKNOWN";
return versioninfo.FileVersion.Substring(0, versioninfo.FileVersion.LastIndexOf('.'));
}
byte[] ggm_bytes = File.ReadAllBytes(ggm_path);
if ((ggm_bytes == null) || (ggm_bytes.Length <= 0))
return "UNKNOWN";
int start_position = 0;
for (int i = 10; i < ggm_bytes.Length; i++)
{
byte pos_byte = ggm_bytes[i];
if ((pos_byte <= 0x39) && (pos_byte >= 0x30))
{
start_position = i;
break;
}
}
if (start_position == 0)
return "UNKNOWN";
int end_position = 0;
for (int i = start_position; i < ggm_bytes.Length; i++)
{
byte pos_byte = ggm_bytes[i];
if ((pos_byte != 0x2E) && ((pos_byte > 0x39) || (pos_byte < 0x30)))
{
end_position = (i - 1);
break;
}
}
if (end_position == 0)
return "UNKNOWN";
int verstr_byte_pos = 0;
byte[] verstr_byte = new byte[((end_position - start_position) + 1)];
for (int i = start_position; i <= end_position; i++)
{
verstr_byte[verstr_byte_pos] = ggm_bytes[i];
verstr_byte_pos++;
}
return _UnityVersion = Encoding.UTF8.GetString(verstr_byte, 0, verstr_byte.Length);
}
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal extern static bool IsOldMono();
[MethodImpl(MethodImplOptions.InternalCall)]
private extern static bool IsQuitFix();
[MethodImpl(MethodImplOptions.InternalCall)]
internal extern static void UNLOAD(bool doquitfix = true);
}
}