-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cs
More file actions
445 lines (385 loc) · 18.8 KB
/
Core.cs
File metadata and controls
445 lines (385 loc) · 18.8 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using Il2Cpp;
using MelonLoader;
using MelonLoader.Utils;
using UnityEngine;
[assembly: MelonInfo(typeof(BluePrinceMapOverlay.Core), "BluePrinceMapOverlay", "1.1.0", "ComplexSimple", null)]
[assembly: MelonGame("Dogubomb", "BLUE PRINCE")]
namespace BluePrinceMapOverlay
{
public class Core : MelonMod
{
private const string PreferencesCategoryName = "BluePrinceMapOverlay";
private MelonPreferences_Category _category;
// I had to make these doubles since the config wouldn't allow for the right precision with floats.
private MelonPreferences_Entry<double> _overlayX;
private MelonPreferences_Entry<double> _overlayY;
private MelonPreferences_Entry<float> _scale;
private MelonPreferences_Entry<float> _transparency;
private MelonPreferences_Entry<KeyCode> _toggleKey;
private MelonPreferences_Entry<bool> _visibleAtStart;
private const string HudGameObjectPath = "__SYSTEM/HUD";
private const string CullingReferencePath = "__SYSTEM/HUD/Steps/Steps Icon";
private const string GridReferencePath = "__SYSTEM/THE GRID";
private const string SystemGameObjectPath = "__SYSTEM";
private const string PlayerIconRelativePath = "FPS Home/FPSController - Prince/Player Core/Player Icon";
private const string GridManagerPath = "ROOMS/Grid Manager";
private const string MapDraftingCameraPath = "UI OVERLAY CAM/Map HOlder/Map Camera Drafting";
private const string DraftReferencePath = "__SYSTEM/THE DRAFT/anchor/DRAFT UI";
private const string BundlePathSuffix = "BluePrinceMapOverlay/assets/map_overlay.bundle";
private const string PrefabName = "Map Overlay";
private const float OverlayZPosition = 27.46f;
private const float ResolutionScale = 2f; // Adjust this value to change the resolution of the overlay
private readonly Vector3 BaseScale = new(34f, 20f, 1f);
private GameObject _mapOverlay;
private Renderer _mapOverlayRenderer;
private GameObject _cullingReference;
private Camera _mapOverlayCamera;
private Dictionary<Renderer, bool> _mapRendererVisibilities;
private GameObject _playerIcon;
private GridManager _gridManager;
private GameObject _mapDraftingCamera;
private PlayMakerFSM _theGridFsm;
private PlayMakerFSM _draftUIFSM;
private bool _openingCutsceneFinished;
private bool _isToggledOn = false;
public override void OnInitializeMelon()
{
InitPreferences();
LoggerInstance.Msg("Initialized Blue Prince Map Overlay Mod.");
}
// Initializes the preferences for the compass mod, including position, scale, and
// rotation inversion.
private void InitPreferences()
{
_category = MelonPreferences.CreateCategory(PreferencesCategoryName);
_overlayX = MelonPreferences.CreateEntry<double>(PreferencesCategoryName, "OverlayX", -11.3f, null, "The x position of the overlay relative to the HUD GameObject.");
_overlayY = MelonPreferences.CreateEntry<double>(PreferencesCategoryName, "OverlayY", -1004.6f, null, "The y position of the overlay relative to the HUD GameObject.");
_scale = MelonPreferences.CreateEntry<float>(PreferencesCategoryName, "Scale", 1.0f, null, "The size of the overlay relative to its default size.");
_transparency = MelonPreferences.CreateEntry<float>(PreferencesCategoryName, "Transparency", 0.5f, null, "The transparency of the overlay, from 0 (opaque) to 1 (fully transparent).");
_toggleKey = MelonPreferences.CreateEntry<KeyCode>(PreferencesCategoryName, "ToggleKey", KeyCode.LeftControl, null,
"The key to toggle the overlay visibility. Left Control by default. See Unity KeyCodes (https://docs.unity3d.com/6000.1/Documentation/ScriptReference/KeyCode.html)");
_visibleAtStart = MelonPreferences.CreateEntry<bool>(PreferencesCategoryName, "VisibleAtStart", true, null, "The default visibility of the overlay when the day starts.");
LoggerInstance.Msg($"Compass Preferences: {Path.Combine(MelonEnvironment.UserDataDirectory, $"{PreferencesCategoryName}.cfg")}");
_category.SetFilePath(Path.Combine(MelonEnvironment.UserDataDirectory, $"{PreferencesCategoryName}.cfg"));
}
public override void OnSceneWasInitialized(int buildIndex, string sceneName)
{
GameObject hud = GameObject.Find(HudGameObjectPath);
if (hud == null)
{
return;
}
_openingCutsceneFinished = false;
_cullingReference = GetCullingReference();
_mapOverlay = LoadMapOverlay(hud, _cullingReference);
_mapOverlayRenderer = _mapOverlay.GetComponent<Renderer>();
_mapDraftingCamera = GameObject.Find(MapDraftingCameraPath);
if (_mapDraftingCamera == null)
{
LoggerInstance.Error($"Map Drafting Camera GameObject \"{MapDraftingCameraPath}\" not found in the scene, skipping camera creation.");
return;
}
_mapOverlayCamera = CreateMapOverlayCamera(_mapDraftingCamera);
Material material = SetupMaterial(_mapOverlay, _mapOverlayCamera);
if (material == null)
{
LoggerInstance.Error("Failed to set up Map Overlay material.");
return;
}
GameObject grid = GameObject.Find(GridReferencePath);
_theGridFsm = grid?.GetComponent<PlayMakerFSM>();
_mapRendererVisibilities = GetRendererVisibilities(grid);
_playerIcon = ClonePlayerIcon();
_gridManager = GetGridManager();
_draftUIFSM = GameObject.Find(DraftReferencePath)?.GetComponent<PlayMakerFSM>();
_isToggledOn = false;
}
// Retrieves the culling reference GameObject from the HUD GameObject using the
// specified relative path.
private GameObject GetCullingReference()
{
GameObject cullingReference = GameObject.Find(CullingReferencePath);
if (cullingReference == null)
{
LoggerInstance.Error($"Could not find culling reference Transform \"{CullingReferencePath}\"");
return null;
}
return cullingReference;
}
// Instantiates the map overlay prefab from the asset bundle and sets it up with the
private GridManager GetGridManager()
{
GameObject gridManager = GameObject.Find(GridManagerPath);
if (gridManager == null)
{
LoggerInstance.Error($"Could not find Grid Manager GameObject \"{GridManagerPath}\"");
return null;
}
return gridManager.GetComponent<GridManager>();
}
private GameObject FindRelativeToSystem(string relativePath)
{
GameObject system = GameObject.Find(SystemGameObjectPath);
Transform relativeTransform = system.transform.Find(relativePath);
if (relativeTransform == null)
{
LoggerInstance.Error($"Could not find relative Transform \"{relativePath}\" in system GameObject.");
return null;
}
return relativeTransform.gameObject;
}
// Loads the map overlay prefab from the asset bundle and instantiates it at the specified
// position and scale.
private GameObject LoadMapOverlay(GameObject hud, GameObject cullingReference)
{
string bundlePath = Path.Combine(MelonEnvironment.ModsDirectory, BundlePathSuffix);
LoggerInstance.Msg($"Bundle Path: {bundlePath}");
AssetBundle bundle = AssetBundle.LoadFromFile(bundlePath);
if (bundle == null)
{
LoggerInstance.Error($"Failed to load asset bundle from path: {bundlePath}");
return null;
}
GameObject prefab = bundle.LoadAsset<GameObject>(PrefabName);
bundle.Unload(false);
if (prefab == null)
{
LoggerInstance.Error($"Failed to load prefab \"{PrefabName}\" from asset bundle.");
return null;
}
Vector3 overlayPosition = new((float)_overlayX.Value, (float)_overlayY.Value, OverlayZPosition);
GameObject mapOverlay = GameObject.Instantiate(prefab, overlayPosition, Quaternion.identity, hud.transform);
mapOverlay.SetActive(false); // Start with the overlay inactive, activate it after the cutscene.
Vector3 scale = _scale.Value * BaseScale;
mapOverlay.transform.localScale = scale;
InitCulling(mapOverlay, hud, cullingReference);
return mapOverlay;
}
// Sets up the material for the map overlay, including the RenderTexture and transparency.
private Material SetupMaterial(GameObject mapOverlay, Camera camera)
{
Renderer renderer = mapOverlay.GetComponent<Renderer>();
if (renderer == null)
{
LoggerInstance.Error("Map Overlay GameObject does not have a Renderer component.");
return null;
}
Material material = renderer.material;
if (material == null)
{
LoggerInstance.Error("Map Overlay GameObject does not have a Material component.");
return null;
}
int textureWidth = (int) (camera.pixelWidth * ResolutionScale);
int textureHeight = (int) (camera.pixelHeight * ResolutionScale);
RenderTexture renderTexture = new(textureWidth, textureHeight, 24);
material.mainTexture = renderTexture; // Set the main texture of the material to the RenderTexture
camera.targetTexture = renderTexture;
material.SetFloat("_Alpha", 1 - _transparency.Value);
return material;
}
// Sets up map overlay culling so it will only be rendered when steps, gems and keys are
// rendered.
private void InitCulling(GameObject obj, GameObject hud, GameObject cullingReference)
{
Culler hudCuller = hud.GetComponent<Culler>();
if (hudCuller == null)
{
LoggerInstance.Error($"HUD GameObject with name \"{HudGameObjectPath}\" does not have a Culler component. The map overlay will not be added to the culler.");
return;
}
// This (like this whole codebase) is incredibly overengineered, but I have an
// irrational fear of breaking forward compatibility if I use hardcoded values.
bool referenceInEnabledList = true;
if (cullingReference == null)
{
LoggerInstance.Error("The culling reference is null, so the rendering layer couldn't be determined.");
}
else
{
obj.layer = cullingReference.layer;
Renderer referenceRenderer = _cullingReference.GetComponent<Renderer>();
referenceInEnabledList = (referenceRenderer != null &&
hudCuller._childRenderersEnabled.Contains(referenceRenderer));
}
Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
// I wasn't sure how to convert from the array to an Il2CPP enumerable, so I just
// add the MeshRenderers one by one.
foreach (Renderer renderer in renderers)
{
// I don't like using these private fields, but I don't know how to add the
// map overlay to the culler otherwise.
hudCuller._childRenderers.Add(renderer);
if (referenceInEnabledList)
{
hudCuller._childRenderersEnabled.Add(renderer);
}
else
{
hudCuller._childRenderersDisabled.Add(renderer);
}
}
}
// Creates a new camera for the map overlay, which will render the map overlay
private Camera CreateMapOverlayCamera(GameObject mapDraftingCamera)
{
GameObject cameraObject = GameObject.Instantiate(
mapDraftingCamera,
mapDraftingCamera.transform.position,
mapDraftingCamera.transform.rotation,
mapDraftingCamera.transform.parent
);
cameraObject.SetActive(true);
cameraObject.name = "Map Overlay Camera";
Camera camera = cameraObject.GetComponent<Camera>();
camera.clearFlags = CameraClearFlags.Nothing;
// Blue Prince constantly enables and disables the map tile renderers, and I'm not sure where that's happening.
// I added these callbacks as a workaround to ensure the map overlay is always visible when the camera is active.
Camera.onPreCull += (Camera.CameraCallback) PreCullCallback;
Camera.onPostRender += (Camera.CameraCallback) PostRenderCallback;
return camera;
}
private void PreCullCallback(Camera cam)
{
if (cam == _mapOverlayCamera)
{
ClearRenderTexture(cam);
SetRenderers(_mapRendererVisibilities, true);
}
}
private void PostRenderCallback(Camera cam)
{
if (cam == _mapOverlayCamera)
{
ResetRenderers(_mapRendererVisibilities);
}
}
// The bottom of the RenderTexture isn't cleared by default, so we need to clear it manually.
private void ClearRenderTexture(Camera cam)
{
if (cam != _mapOverlayCamera)
{
return;
}
RenderTexture renderTexture = cam.targetTexture;
if (renderTexture != null)
{
RenderTexture.active = renderTexture;
GL.Clear(true, true, Color.clear);
RenderTexture.active = null;
}
}
// Retrieves the visibilities of all renderers in the GameObject and stores them in a dictionary.
private static Dictionary<Renderer, bool> GetRendererVisibilities(GameObject root)
{
Dictionary<Renderer, bool> rendererVisibilities = [];
Renderer[] renderers = root.GetComponentsInChildren<Renderer>(true);
foreach (Renderer renderer in renderers)
{
rendererVisibilities[renderer] = renderer.enabled;
}
return rendererVisibilities;
}
// Sets the renderers to the given values and stores their previous values in the map.
private static void SetRenderers(Dictionary<Renderer, bool> rendererVisibilities, bool enabled)
{
foreach (Renderer renderer in rendererVisibilities.Keys)
{
rendererVisibilities[renderer] = renderer.enabled;
renderer.enabled = enabled;
}
}
// Resets the visibility of all map renderers to their original state after rendering.
private static void ResetRenderers(Dictionary<Renderer, bool> rendererVisibilities)
{
foreach (KeyValuePair<Renderer, bool> entries in rendererVisibilities)
{
Renderer renderer = entries.Key;
bool enabled = entries.Value;
renderer.enabled = enabled;
}
}
// Clones the existing player icon GameObject
private GameObject ClonePlayerIcon()
{
// The player icon starts disabled, so we can't use GameObject.Find directly to get it.
GameObject oldPlayerIcon = FindRelativeToSystem(PlayerIconRelativePath);
GameObject newPlayerIcon = GameObject.Instantiate(oldPlayerIcon,
// The icon is too high at the top of the clocktower, so we move it down a bit.
oldPlayerIcon.transform.position - new Vector3(0, 2, 0),
oldPlayerIcon.transform.rotation,
oldPlayerIcon.transform.parent
);
newPlayerIcon.name = "Player Icon Map Overlay";
return newPlayerIcon;
}
public override void OnUpdate()
{
if (_mapOverlay == null)
{
return;
}
HandleCutsceneFinished();
HandleToggleKeyPress();
HandleDraftingUI();
SetPlayerIconActive();
SetOverlayActive();
}
// Enables the map overlay after the opening cutscene is finished.
private void HandleCutsceneFinished()
{
if (_openingCutsceneFinished)
{
return;
}
if (_cullingReference.activeInHierarchy)
{
_openingCutsceneFinished = true;
_isToggledOn = _visibleAtStart.Value;
}
}
// Toggles the visibility of the map overlay when the toggle key is pressed.
private void HandleToggleKeyPress()
{
if (Input.GetKeyDown(_toggleKey.Value))
{
_isToggledOn = !_isToggledOn;
}
}
// Sets the player icon active or inactive based on the player's position in the grid.
private void SetPlayerIconActive()
{
if (_playerIcon == null)
{
return;
}
bool active = _mapOverlayRenderer.isVisible &&
(_gridManager == null || !_gridManager.IsPlayerActuallyOutside);
_playerIcon.SetActive(active);
}
// Sets the map overlay active or inactive based on the toggle state and whether the drafting map is visible.
private void SetOverlayActive()
{
bool isActive = _isToggledOn;
if (_mapDraftingCamera.activeInHierarchy)
{
isActive = false; // Hide the overlay while the drafting map is visible.
}
_mapOverlay.SetActive(isActive);
}
private void HandleDraftingUI()
{
bool isDrafting = _draftUIFSM.FsmVariables.GetFsmBool("are drafting").Value;
if (!isDrafting)
{
return;
}
// In the original game, the tile is only enabled when the drafting map is enabled.
// With this, it can show up on the minimap right away with the correct rotation.
GameObject targetTileChild = _theGridFsm.FsmVariables.GetFsmGameObject("TargetTileChild")?.Value;
targetTileChild.SetActive(true);
float direction = _theGridFsm.FsmVariables.GetFsmFloat("Cardinal Direction").Value;
targetTileChild.transform.rotation = Quaternion.Euler(0, direction, 0);
}
}
}