-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaunchControlPlugin.cs
More file actions
128 lines (105 loc) · 4.56 KB
/
Copy pathLaunchControlPlugin.cs
File metadata and controls
128 lines (105 loc) · 4.56 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
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Ostranauts.Bit.Items.Categories;
using UnityEngine;
namespace Ostranauts.Bit
{
/// <summary>
/// LaunchControl - Core library providing command registration and utility functions for Ostranauts mods
/// </summary>
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
public class LaunchControlPlugin : BaseUnityPlugin
{
// Delegate declaration compatible with Unity's older Mono runtime
private delegate void InitDelegate();
internal static new ManualLogSource Logger;
private Harmony _harmony;
private GameObject _launchControlObject;
private void Awake()
{
Logger = base.Logger;
Logger.LogInfo($"Plugin {PluginInfo.PLUGIN_GUID} v{PluginInfo.PLUGIN_VERSION} is loading...");
// Initialize LaunchControl singleton
_launchControlObject = new GameObject("LaunchControl");
DontDestroyOnLoad(_launchControlObject);
_launchControlObject.AddComponent<LaunchControl>();
Logger.LogInfo("LaunchControl singleton initialized");
// Apply Harmony patches
_harmony = new Harmony(PluginInfo.PLUGIN_GUID);
_harmony.PatchAll(typeof(LaunchControlPlugin).Assembly);
// Log all patches applied
var patches = _harmony.GetPatchedMethods();
int patchCount = 0;
foreach (var method in patches)
{
patchCount++;
Logger.LogInfo($"Patched method: {method.DeclaringType?.Name}.{method.Name}");
}
Logger.LogInfo($"LaunchControl patches applied successfully! ({patchCount} methods patched)");
// Register built-in commands
RegisterBuiltInCommands();
Logger.LogInfo("LaunchControl loaded successfully!");
Logger.LogInfo(" Command registration system available");
Logger.LogInfo(" Call LaunchControl.RegisterCommand(name, callback) from your mods");
}
private void Start()
{
// Use a coroutine to wait for DataHandler.bLoaded since delegate subscription is causing issues
StartCoroutine(WaitForDataHandlerLoad());
}
private System.Collections.IEnumerator WaitForDataHandlerLoad()
{
// Wait until DataHandler finishes loading
while (!DataHandler.bLoaded)
{
yield return null;
}
Logger.LogInfo("DataHandler loading complete, initializing item categories...");
InitializeItemCategories();
}
private void InitializeItemCategories()
{
if (LaunchControl.Instance == null)
{
Logger.LogError("Cannot initialize item categories: LaunchControl instance or ItemCategoryManager is null");
return;
}
if (DataHandler.dictCOs == null || DataHandler.dictCOs.Count == 0)
{
Logger.LogWarning("DataHandler.dictCOs is not yet available. Skipping item category initialization.");
return;
}
try
{
Logger.LogInfo("Initializing item categories after DataHandler load...");
DefaultCategories.Initialize(LaunchControl.Instance.Items.Categories);
// Populate categories from predicates now that all items are loaded
LaunchControl.Instance.Items.Categories.PopulateFromPredicates();
Logger.LogInfo("Item category system initialized successfully");
}
catch (System.Exception ex)
{
Logger.LogError($"Failed to initialize item categories: {ex.Message}");
Logger.LogError(ex.StackTrace);
}
}
private void RegisterBuiltInCommands()
{
// ListSprites command for discovering available sprites
_launchControlObject.AddComponent<Commands.ListSpritesCommand>();
Logger.LogInfo("Built-in commands registered");
}
private void OnDestroy()
{
_harmony?.UnpatchSelf();
Logger.LogInfo("LaunchControl unloaded");
}
}
public static class PluginInfo
{
public const string PLUGIN_GUID = "com.ostranauts.LaunchControl";
public const string PLUGIN_NAME = "LaunchControl";
public const string PLUGIN_VERSION = "1.0.0";
}
}