-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
197 lines (184 loc) · 7.03 KB
/
Plugin.cs
File metadata and controls
197 lines (184 loc) · 7.03 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
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using static System.Net.Mime.MediaTypeNames;
using static UnityEngine.UIElements.StylePropertyAnimationSystem;
namespace TestLevelLoader;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class Plugin : BaseUnityPlugin
{
public static new ManualLogSource Logger;
public static HashSet<AssetBundle> loadedBundles = [];
public static Dictionary<GameObject, string> levelPrefabsList = [];
public static string bundleFolderPath;
public static ConfigEntry<KeyCode> toggleKey;
public static ConfigEntry<string> bundleFolder;
public static ConfigEntry<bool> bundleFolderRelative;
public static bool UIEnabled = false;
public void Awake() // As of 7.8.2025 White Knuckle still seems to require HideManagerGameObject enabled in BepInEx config.
{
// Plugin startup logic
Logger = base.Logger;
bundleFolder = Config.Bind("Misc", "PathToFolder", "CustomLevels", "Where we look for bundles.");
bundleFolderRelative = Config.Bind("Misc", "PathToFolderIsRelative", true, "Is PathToFolder relative to the bepinex plugin folder?");
bundleFolderPath = bundleFolderRelative.Value ? Path.Combine(Paths.PluginPath, bundleFolder.Value) : bundleFolder.Value;
UIEnabled = Config.Bind<bool>("Controls", "UIStartsEnabled", false).Value;
if (!Directory.Exists(bundleFolderPath))
{
Directory.CreateDirectory(bundleFolderPath);
}
toggleKey = Config.Bind("Controls", "UIKey", KeyCode.F7);
Harmony.CreateAndPatchAll(typeof(Patches)); // Does all the patches in Patches
}
public static Rect windowRect = new Rect(20, 20, 400, 300);
public static Vector2 scrollPos = Vector2.zero;
void OnGUI()
{
if (!UIEnabled) return;
windowRect = GUI.Window(69, windowRect, DoMyWindow, "Level Loader ("+toggleKey.Value+" to hide)");
}
void Update()
{
if (Input.GetKeyDown(toggleKey.Value))
{
UIEnabled = !UIEnabled;
}
}
static void DoMyWindow(int windowID)
{
var centeredStyle = GUI.skin.GetStyle("Label");
centeredStyle.alignment = TextAnchor.UpperCenter;
centeredStyle.fontSize = 18;
centeredStyle.richText = true;
GUI.DragWindow(new Rect(0, 0, 10000, 20));
GUILayout.Label("Loaded bundles: "+loadedBundles.Count);
if(loadedBundles.Count <= 0)
{
GUILayout.Label("No bundles loaded. Databases either havent loaded or something messed up");
}
scrollPos = GUILayout.BeginScrollView(scrollPos, alwaysShowHorizontal: false, alwaysShowVertical: true);
{
foreach (AssetBundle bundle in loadedBundles)
{
if (bundle.name.IsNullOrWhiteSpace()) continue;
GUILayout.BeginHorizontal();
GUILayout.Label("Bundle: " + bundle.name, centeredStyle);
GUILayout.EndHorizontal();
IEnumerable<KeyValuePair<GameObject, string>> levels = levelPrefabsList.Where((levelPair) => levelPair.Value == bundle.name);
foreach (var levelPair in levels)
{
var level = levelPair.Key;
if (GUILayout.Button(level.name))
CL_GameManager.gMan.LoadLevels([level.name]);
}
}
}
GUILayout.EndScrollView();
if (GUILayout.Button("Reload All Bundles (buggy, might break mats)"))
{
string curname = CL_EventManager.currentLevel != null ? CL_EventManager.currentLevel.levelName : "";
UnloadBundles();
LoadAllBundles();
FXManager.handholdMaterialDict = null;
if (SceneManager.GetActiveScene().name != "Game-Main") return;
CL_GameManager.gMan.LoadLevels([curname]);
}
}
public static void LoadAllBundles()
{
string[] files = Directory.GetFiles(bundleFolderPath, "*.*", SearchOption.AllDirectories);
AssetBundle[] loadedAlready = AssetBundle.GetAllLoadedAssetBundles().ToArray();
foreach (string text in files)
{
try
{
string filename = Path.GetFileName(text);
string extension = Path.GetExtension(filename);
if (!extension.IsNullOrWhiteSpace() || loadedAlready.Any((b) => b.name == filename)) continue;
AssetBundle val = AssetBundle.LoadFromFile(text);
if (val == null) continue;
if(val.name.IsNullOrWhiteSpace())
{
Plugin.Logger.LogWarning("Ignoring file " + filename + " because it is missing a bundle name");
val.Unload(false);
continue;
}
loadedBundles.Add(val);
}
catch (Exception e)
{
Plugin.Logger.LogError(e);
}
}
RegisterAllLevels();
}
public static void RegisterAllLevels()
{
if (levelPrefabsList == null) return;
foreach (AssetBundle bundle in loadedBundles)
{
RegisterLevelsFromBundle(bundle);
}
}
public static void RegisterLevelsFromBundle(AssetBundle bundle)
{
if (bundle == null) return;
GameObject[] array = bundle.LoadAllAssets<GameObject>();
foreach (GameObject gameObject in array)
{
if (levelPrefabsList.Any((level) => level.Key.name == gameObject.name)) continue;
levelPrefabsList.Add(gameObject, bundle.name);
CL_AssetManager.GetBaseAssetDatabase().levelPrefabs.Add(gameObject);
}
}
public static void UnloadBundle(AssetBundle bundle)
{
loadedBundles.Remove(bundle);
List<GameObject> toRemove = new();
foreach (var pair in levelPrefabsList)
{
if(pair.Value == bundle.name)
toRemove.Add(pair.Key);
}
foreach(var thing in toRemove)
{
CL_AssetManager.GetBaseAssetDatabase().levelPrefabs.Remove(thing);
levelPrefabsList.Remove(thing);
}
bundle.Unload(false);
}
public static void UnloadBundles()
{
AssetBundle[] bundles = loadedBundles.ToArray();
foreach (AssetBundle bundle in bundles)
{
UnloadBundle(bundle);
}
loadedBundles.Clear();
}
}
class Patches
{
[HarmonyPatch(typeof(CL_AssetManager), nameof(CL_AssetManager.InitializeAssetManager))]
[HarmonyPostfix]
[HarmonyWrapSafe]
static void Patch()
{
if (Plugin.loadedBundles.Count > 0) return;
Plugin.LoadAllBundles();
}
}