-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathRainMeadow.cs
More file actions
354 lines (310 loc) · 12.5 KB
/
Copy pathRainMeadow.cs
File metadata and controls
354 lines (310 loc) · 12.5 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using BepInEx;
using Newtonsoft.Json.Linq;
using RainMeadow.Game;
using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using UnityEngine;
using UnityEngine.Networking;
[assembly: AssemblyVersion(RainMeadow.RainMeadow.MeadowVersionStr)]
#pragma warning disable CS0618
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
namespace RainMeadow
{
[BepInPlugin("henpemaz.rainmeadow", "RainMeadow", MeadowVersionStr)]
public partial class RainMeadow : BaseUnityPlugin
{
public const string MeadowVersionStr = "0.1.15.2";
public const string ReleaseUrl = "https://api.github.com/repos/henpemaz/Rain-Meadow/releases/latest";
public static string NewVersionAvailable = "";
public static RainMeadow instance;
private bool init;
public bool fullyInit;
public bool fullyEnabled;
public static RainMeadowOptions rainMeadowOptions;
private PlopMachine PlopMachine;
public void OnEnable()
{
try
{
instance = this;
rainMeadowOptions = new RainMeadowOptions(this);
On.RainWorld.OnModsInit += RainWorld_OnModsInit;
On.ModManager.RefreshModsLists += ModManagerOnRefreshModsLists;
On.RainWorld.Update += RainWorld_Update;
On.WorldLoader.UpdateThread += WorldLoader_UpdateThread;
On.RoomPreparer.UpdateThread += RoomPreparer_UpdateThread;
On.WorldLoader.FindingCreaturesThread += WorldLoader_FindingCreaturesThread;
On.WorldLoader.CreatingAbstractRoomsThread += WorldLoader_CreatingAbstractRoomsThread;
On.RWCustom.Custom.Log += Custom_Log;
On.RWCustom.Custom.LogImportant += Custom_LogImportant;
On.RWCustom.Custom.LogWarning += Custom_LogWarning;
// Do not add more hooks here unless they are related to logging
// Also note that RainMeadow.log* has the wrong settings until OnModsInit
fullyEnabled = true;
}
catch(Exception e)
{
fullyEnabled = false;
Logger.LogError(e);
}
}
private bool AdvancedProfilingEnabled()
{
foreach(var arg in Environment.GetCommandLineArgs())
{
if (arg == "-meadowprofiler") return true;
}
return false;
}
private void Custom_LogWarning(On.RWCustom.Custom.orig_LogWarning orig, string[] values)
{
Logger.LogWarning(string.Join(" ", values));
orig(values);
}
private void Custom_LogImportant(On.RWCustom.Custom.orig_LogImportant orig, string[] values)
{
Logger.LogInfo(string.Join(" ", values));
orig(values);
}
private void Custom_Log(On.RWCustom.Custom.orig_Log orig, string[] values)
{
Logger.LogInfo(string.Join(" ", values));
orig(values);
}
private void WorldLoader_CreatingAbstractRoomsThread(On.WorldLoader.orig_CreatingAbstractRoomsThread orig, WorldLoader self)
{
try
{
orig(self);
}
catch (Exception e)
{
Logger.LogError(e);
throw;
}
}
private void WorldLoader_FindingCreaturesThread(On.WorldLoader.orig_FindingCreaturesThread orig, WorldLoader self)
{
try
{
orig(self);
}
catch (Exception e)
{
Logger.LogError(e);
throw;
}
}
private void RoomPreparer_UpdateThread(On.RoomPreparer.orig_UpdateThread orig, RoomPreparer self)
{
try
{
orig(self);
}
catch (Exception e)
{
Logger.LogError(e);
throw;
}
}
private void WorldLoader_UpdateThread(On.WorldLoader.orig_UpdateThread orig, WorldLoader self)
{
try
{
orig(self);
}
catch (Exception e)
{
Logger.LogError(e);
throw;
}
}
private void RainWorld_Update(On.RainWorld.orig_Update orig, RainWorld self)
{
try
{
tracing = Input.GetKey("l");
orig(self);
}
catch (Exception e)
{
Logger.LogError(e);
throw;
}
}
private void ModManagerOnRefreshModsLists(On.ModManager.orig_RefreshModsLists orig, RainWorld rainworld)
{
orig(rainworld);
try
{
RainMeadowModInfoManager.RefreshRainMeadowModInfos();
}
catch (Exception e)
{
Logger.LogError($"Error loading Meadow mod info files:\n{e}\n{e.StackTrace}");
}
}
private void RainWorld_OnModsInit(On.RainWorld.orig_OnModsInit orig, RainWorld self)
{
orig(self);
if (init) return;
init = true;
try
{
EssentialMenuHooks(); // sets the error message fallback
InitializeExtEnums();
MachineConnector.SetRegisteredOI("henpemaz_rainmeadow", rainMeadowOptions);
rainMeadowOptions._LoadConfigFile(); // We need the logging settings
if (AdvancedProfilingEnabled())
{
MeadowProfiler.FullPatch();
}
DeathContextualizer.CreateBindings();
StartCoroutine(CheckForUpdates());
UsernameGenerator.Timestamp = DateTime.Now.Ticks;
var sw = Stopwatch.StartNew();
OnlineState.InitializeBuiltinTypes();
sw.Stop();
RainMeadow.Debug($"OnlineState.InitializeBuiltinTypes: {sw.Elapsed}");
sw = Stopwatch.StartNew();
OnlineGameMode.InitializeBuiltinTypes();
sw.Stop();
RainMeadow.Debug($"OnlineGameMode.InitializeBuiltinTypes: {sw.Elapsed}");
sw = Stopwatch.StartNew();
MeadowProgression.InitializeBuiltinTypes();
sw.Stop();
RainMeadow.Debug($"MeadowProgression.InitializeBuiltinTypes: {sw.Elapsed}");
sw = Stopwatch.StartNew();
RPCManager.SetupRPCs();
sw.Stop();
RainMeadow.Debug($"RPCManager.SetupRPCs: {sw.Elapsed}");
AssetBundle bundle = AssetBundle.LoadFromFile(AssetManager.ResolveFilePath("assetbundles/rainmeadow"));
Shader[] newShaders = bundle.LoadAllAssets<Shader>();
foreach (Shader shader in newShaders)
{
RainMeadow.Debug("found shader " + shader.name);
var found = false;
foreach (FShader oldshader in self.Shaders.Values)
{
if (oldshader.shader.name == shader.name)
{
RainMeadow.Debug("replaced existing shader");
oldshader.shader = shader;
found = true;
break;
}
}
if (!found)
{
RainMeadow.Debug("registered as new shader");
self.Shaders[shader.name] = FShader.CreateShader(shader.name, shader);
if (shader.name == "RippleGlowColored"
|| shader.name == "RippleSpawnBodyColored")
{
RainMeadow.Debug("also registering ripple side variant");
self.Shaders[shader.name + "RippleSide"] = FShader.CreateShader(shader.name + "RippleSide", shader, ["ripple_other_side"]);
}
}
}
self.Shaders.Add("FlatWaterLightRippleSide", FShader.CreateShader("FlatWaterLight", Shader.Find("Futile/FlatWaterLight"), ["ripple_other_side"]));
MenuHooks();
GameHooks();
CreatureHooks();
EntityHooks();
ShortcutHooks();
GameplayHooks();
PlayerHooks();
CustomizationHooks();
MeadowHooks();
LoadingHooks();
StoryHooks();
ArenaHooks();
ItemHooks();
ObjectHooks();
JollyHooks();
CosmeticManager.FetchCosmetics();
CosmeticManager.ParseAvailableCosmetics();
MeadowMusic.EnableMusic();
this.PlopMachine = new PlopMachine();
this.PlopMachine.OnEnable();
MeadowProgression.LoadProgression();
self.processManager.sideProcesses.Add(new OnlineManager(self.processManager));
fullyInit = fullyEnabled;
// // Useful for finding where a Translate() method is missing
// On.InGameTranslator.Translate += (origTranslate, translator, s) =>
// {
// return "T";
// };
}
catch (Exception e)
{
Logger.LogError(e);
fullyInit = false;
//throw;
}
}
IEnumerator CheckForUpdates()
{
JObject json = null;
using (UnityWebRequest request = UnityWebRequest.Get(ReleaseUrl))
{
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
json = JObject.Parse(request.downloadHandler.text);
}
else
{
Logger.LogError($"A web request error occured whilst checking for updates: {request.result}");
yield break;
}
}
if (json is null)
{
Logger.LogError($"A web request error occured whilst checking for updates: JSON returned no body.");
yield break;
}
if (json.TryGetValue("tag_name", out var token))
{
string latestVersion = token.ToString();
if (latestVersion.Count(f => f == '.') < 3)
{
latestVersion = "0." + latestVersion;
}
RainMeadow.Debug($"Current Version - {MeadowVersionStr}, Latest Version - {latestVersion}");
if (IsNewerVersion(latestVersion, MeadowVersionStr))
{
RainMeadow.Debug($"NEW RAIN MEADOW VERSION FOUND.");
// One day grace window before users are prompted to update.
if (json.TryGetValue("published_at", out var published)
&& DateTime.TryParse(published.ToString(), out var publishedDate)
&& publishedDate.AddDays(1) > DateTime.Now)
{
RainMeadow.Debug($"Update popup grace period active until: {publishedDate.AddDays(1).ToLongDateString()}");
yield break;
}
NewVersionAvailable = latestVersion;
}
}
}
// This logic could be improved a bit but it seems to work fine for now so I'll leave it be.
public static bool IsNewerVersion(string newVersion, string currentVersion)
{
if (newVersion == currentVersion || string.IsNullOrWhiteSpace(newVersion) || string.IsNullOrWhiteSpace(currentVersion)) return false;
string[] nParts = newVersion.Split('.');
string[] cParts = currentVersion.Split('.');
int length = Math.Max(nParts.Length, cParts.Length);
for (int i = 0; i < length; i++)
{
int nPart = i < nParts.Length ? int.Parse(nParts[i]) : 0;
int cPart = i < cParts.Length ? int.Parse(cParts[i]) : 0;
if (nPart != cPart) return nPart > cPart; // one version newer than the other.
}
return false;
}
}
}