forked from baerchen201/LethalScanCommand
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLethalScanCommand.cs
More file actions
240 lines (214 loc) · 8.21 KB
/
Copy pathLethalScanCommand.cs
File metadata and controls
240 lines (214 loc) · 8.21 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
using System.Collections.Generic;
using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.VisualBasic;
using UnityEngine;
namespace AutoScan;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class AutoScan : BaseUnityPlugin
{
public static AutoScan Instance { get; private set; } = null!;
internal static new ManualLogSource Logger { get; private set; } = null!;
internal static Harmony? Harmony { get; set; }
private ConfigEntry<bool>? autoAnnounce;
public static bool AutoAnnounce => Instance?.autoAnnounce?.Value == true;
private ConfigEntry<bool>? announceOutsideLoot;
public static bool AnnounceOutsideLoot => Instance?.announceOutsideLoot?.Value == true;
private ConfigEntry<bool>? announceValue;
public static bool AnnounceValue => Instance?.announceValue?.Value == true;
private ConfigEntry<bool>? hostOnly;
public static bool HostOnly => Instance?.hostOnly?.Value == true;
private ConfigEntry<float>? scanDelay;
public static float ScanDelay => Instance?.scanDelay?.Value ?? 2.5f;
private ConfigEntry<bool>? announceExactValue;
public static bool AnnounceExactValue => Instance?.announceExactValue?.Value == true;
private ConfigEntry<string>? announcePrefix;
public static string AnnouncePrefix => Instance?.announcePrefix?.Value ?? "Scan: ";
private ConfigEntry<string>? announceColor;
public static string AnnounceColor => Instance?.announceColor?.Value ?? "#00ff00";
private void Awake()
{
Logger = base.Logger;
Instance = this;
autoAnnounce = Config.Bind(
"General",
"AutoAnnounce",
true,
"Automatically announce the number of items outside the ship at the start of the round"
);
announceOutsideLoot = Config.Bind(
"General",
"AnnounceOutsideLoot",
true,
"Announce the amount of loot outside the facility including Beehives and Sapsucker eggs"
);
announceValue = Config.Bind(
"General",
"AnnounceValue",
true,
"Announce the total loot value when scanning"
);
announceExactValue = Config.Bind(
"General",
"AnnounceExactValue",
false,
"Announce the exact value instead of the terminal's randomized value (Only works for host)"
);
scanDelay = Config.Bind(
"General",
"ScanDelay",
2.5f,
"Delay in seconds before scanning starts"
);
hostOnly = Config.Bind("General", "HostOnly", true, "Only announce if you are the host");
announcePrefix = Config.Bind(
"General",
"AnnouncePrefix",
"Scan: ",
"Prefix to use in announce messages"
);
announceColor = Config.Bind(
"General",
"AnnounceColor",
"#00ff00",
"Color to use in announce messages (in hex format, e.g. #RRGGBB)"
);
Harmony ??= new Harmony(MyPluginInfo.PLUGIN_GUID);
Logger.LogDebug("Patching...");
Harmony.PatchAll();
Logger.LogDebug("Finished patching!");
Logger.LogInfo($"{MyPluginInfo.PLUGIN_GUID} v{MyPluginInfo.PLUGIN_VERSION} has loaded!");
}
public static void ReloadConfig()
{
if (BepInEx.Bootstrap.Chainloader.ManagerObject == null)
return;
var pluginInstance = BepInEx.Bootstrap.Chainloader.ManagerObject.GetComponent<AutoScan>();
if (pluginInstance == null)
return;
pluginInstance.Config.Reload();
}
[HarmonyPatch(typeof(StartOfRound), nameof(StartOfRound.openingDoorsSequence))]
internal class AutoAnnouncePatch
{
public static bool Countitems(
bool exactValue,
out string? error,
out int outsideShip,
out int outsideShipValue
)
{
outsideShip = 0;
outsideShipValue = 0;
error = "items is null";
var items = Object.FindObjectsOfType<GrabbableObject>();
if (items == null)
return false;
error = "ship is null";
var ship = GameObject.Find("Environment/HangarShip");
if (ship == null)
return false;
error = "vehicles is null";
var vehicles = Object.FindObjectsOfType<VehicleController>();
if (vehicles == null)
return false;
var random = new System.Random(StartOfRound.Instance.randomMapSeed + 91);
for (int i = 0; i < items.Length; i++)
{
var item = items[i];
if (
!item.itemProperties.isScrap
|| item.isInShipRoom
|| item.transform.IsChildOf(ship.transform)
|| vehicles.Any(_car => item.transform.IsChildOf(_car.transform))
)
continue;
else
{
outsideShip++;
outsideShipValue += a(!exactValue, item, i);
}
}
return true;
int a(bool randomize, GrabbableObject item, int i) =>
randomize
? Mathf.Clamp(
random.Next(item.itemProperties.minValue, item.itemProperties.maxValue),
item.scrapValue - 6 * i,
item.scrapValue + 9 * i
)
: item.scrapValue;
}
// ReSharper disable once UnusedMember.Local
private static void Postfix(ref StartOfRound __instance)
{
__instance.StartCoroutine(DelayedAnnounce(__instance));
}
private static System.Collections.IEnumerator DelayedAnnounce(StartOfRound __instance)
{
ReloadConfig();
bool exactValue = AnnounceExactValue && __instance.IsServer;
if (!AutoAnnounce || (!__instance.IsServer && HostOnly))
yield break;
yield return new WaitForSeconds(0.2f);
if (
Countitems(
exactValue,
out string? beforeerror,
out int beforeitems,
out int beforevalue
)
)
{
Logger.LogInfo($"{beforeitems} {beforevalue}");
}
else
{
Logger.LogError($"Error while counting items: {beforeerror}");
}
yield return new WaitForSeconds(ScanDelay);
Logger.LogDebug(
$">> DelayedAutoAnnounce({__instance}) IsServer:{__instance.IsServer} AutoAnnounce:{AutoAnnounce}"
);
if (
Countitems(
exactValue,
out string? aftererror,
out int afteritems,
out int aftervalue
)
)
{
Logger.LogInfo($"{afteritems} {aftervalue}");
if (afteritems > 0)
{
string msg = $"<color={AnnounceColor}>{AnnouncePrefix}{beforeitems}";
if (AnnounceOutsideLoot)
msg += $"/{afteritems - beforeitems}";
if (AnnounceValue)
{
if (exactValue)
msg += $" {beforevalue}";
else
msg += $" ≈{beforevalue}";
}
if (AnnounceOutsideLoot && AnnounceValue)
msg += $"/{aftervalue - beforevalue}";
msg += "</color>";
HUDManager.Instance.AddTextMessageServerRpc(msg);
}
}
else
{
Logger.LogError($"Error while counting items: {aftererror}");
HUDManager.Instance.AddTextMessageServerRpc(
$"<color=#ff0000>Error while counting items: {aftererror}</color>"
);
}
}
}
}