-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.cs
94 lines (76 loc) · 2.69 KB
/
Plugin.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
using System.Collections.Generic;
using System.Reflection;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Bloodstone.API;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using UnityEngine;
using v_rising_discord_bot_companion.query;
namespace v_rising_discord_bot_companion;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInDependency("gg.deca.Bloodstone")]
[Reloadable]
public class Plugin : BasePlugin {
public static ManualLogSource Logger { get; private set; } = null!;
public static Plugin Instance { get; private set; } = null!;
private Harmony? _harmony;
private Component? _queryDispatcher;
private PluginConfig? _pluginConfig;
private ConfigEntry<string> _basicAuthUsers;
public Plugin() {
Instance = this;
Logger = Log;
_basicAuthUsers = Config.Bind(
"Authentication",
"BasicAuthUsers",
"",
"A list of comma separated username:password entries that are allowed to query the HTTP API."
);
}
public override void Load() {
if (!VWorld.IsServer) {
Log.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} must be installed on the server side.");
return;
}
// Plugin startup logic
Log.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} version {MyPluginInfo.PLUGIN_VERSION} is loaded!");
// inject components
ClassInjector.RegisterTypeInIl2Cpp<QueryDispatcher>();
_queryDispatcher = AddComponent<QueryDispatcher>();
// Harmony patching
_harmony = new Harmony(MyPluginInfo.PLUGIN_GUID);
_harmony.PatchAll(Assembly.GetExecutingAssembly());
}
public override bool Unload() {
_harmony?.UnpatchSelf();
if (_queryDispatcher != null) {
Object.Destroy(_queryDispatcher);
}
_pluginConfig = null;
return true;
}
public PluginConfig GetPluginConfig() {
_pluginConfig ??= ParsePluginConfig();
return (PluginConfig) _pluginConfig;
}
private PluginConfig ParsePluginConfig() {
var basicAuthUsers = new List<BasicAuthUser>();
foreach (var basicAuthUser in _basicAuthUsers.Value.Split(",")) {
var parts = basicAuthUser.Split(":", 2);
if (parts.Length == 2) {
basicAuthUsers.Add(
new BasicAuthUser(
Username: parts[0].Trim(),
Password: parts[1].Trim()
)
);
}
}
return new PluginConfig(
BasicAuthUsers: basicAuthUsers
);
}
}