-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPlugin.cs
More file actions
405 lines (348 loc) · 18.8 KB
/
Plugin.cs
File metadata and controls
405 lines (348 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
using BepInEx;
using SPT.Reflection.Patching;
using SPT.Reflection.Utils;
using System.Reflection;
using UnityEngine;
using EFT.Communications;
using EFT;
using System.Collections.Generic;
using BepInEx.Configuration;
using Comfort.Common;
using BepInEx.Logging;
using System.Text;
using BepInEx.Bootstrap;
using System;
using System.Linq;
using HarmonyLib;
#pragma warning disable IDE0051 // Remove unused private members
namespace BossNotifier {
[BepInPlugin("Mattdokn.BossNotifier", "BossNotifier", "1.5.4")]
[BepInDependency("com.fika.core", BepInDependency.DependencyFlags.SoftDependency)]
public class BossNotifierPlugin : BaseUnityPlugin {
public static FieldInfo FikaIsPlayerHost;
// Configuration entries
public static ConfigEntry<KeyboardShortcut> showBossesKeyCode;
public static ConfigEntry<bool> showNotificationsOnRaidStart;
public static ConfigEntry<int> intelCenterUnlockLevel;
// public static ConfigEntry<bool> showBossLocation;
public static ConfigEntry<int> intelCenterLocationUnlockLevel;
// public static ConfigEntry<bool> showBossDetected;
public static ConfigEntry<int> intelCenterDetectedUnlockLevel;
private static ManualLogSource logger;
// Logging methods
public static void Log(LogLevel level, string msg) {
logger.Log(level, msg);
}
// Dictionary mapping boss types to names
public static readonly Dictionary<WildSpawnType, string> bossNames = new Dictionary<WildSpawnType, string>() {
{ WildSpawnType.bossBully, "Reshala" },
{ WildSpawnType.bossKnight, "Goons" },
{ WildSpawnType.sectantPriest, "Cultists" },
{ WildSpawnType.bossTagilla, "Tagilla" },
{ WildSpawnType.bossKilla, "Killa" },
{ WildSpawnType.bossZryachiy, "Zryachiy" },
{ WildSpawnType.bossGluhar, "Glukhar" },
{ WildSpawnType.bossSanitar, "Sanitar" },
{ WildSpawnType.bossKojaniy, "Shturman" },
{ WildSpawnType.bossBoar, "Kaban" },
{ WildSpawnType.gifter, "Santa Claus" },
{ WildSpawnType.arenaFighterEvent, "Blood Hounds" },
{ WildSpawnType.crazyAssaultEvent, "Crazy Scavs" },
{ WildSpawnType.exUsec, "Rogues" },
{ WildSpawnType.bossKolontay, "Kollontay" },
{ WildSpawnType.bossPartisan, "Partisan" },
{ (WildSpawnType)4206927, "Punisher" },
{ (WildSpawnType)199, "Legion" },
};
// Set of plural boss names
public static readonly HashSet<string> pluralBosses = new HashSet<string>() {
"Goons",
"Cultists",
"Blood Hounds",
"Crazy Scavs",
"Rogues",
};
// Dictionary mapping zone IDs to names
public static readonly Dictionary<string, string> zoneNames = new Dictionary<string, string>() {
{"ZoneScavBase", "Scav Base" },
{"ZoneDormitory", "Dormitory" },
{"ZoneGasStation", "Gas Station" },
{"ZoneTankSquare", "Old Construction" },
{"ZoneWade", "RUAF Roadblock" },
{"BotZone", "" },
{"ZoneCenterBot", "Center Floor 2" },
{"ZoneCenter", "Center Floor 1" },
{"ZoneOLI", "OLI" },
{"ZoneIDEA", "IDEA" },
{"ZoneGoshan", "Goshan" },
{"ZoneIDEAPark", "IDEA Parking" },
{"ZoneOLIPark", "OLI Parking" },
{"BotZoneFloor1", "Floor 1" },
{"BotZoneFloor2", "Floor 2" },
{"BotZoneBasement", "Basement" },
{"BotZoneGate1", "Gate 1" },
{"BotZoneGate2", "Gate 2" },
{"ZoneRailStrorage", "Rail Storage" },
{"ZonePTOR1", "Black Pawn" },
{"ZonePTOR2", "White Knight" },
{"ZoneBarrack", "Barracks" },
{"ZoneSubStorage", "Sub Storage Д" },
{"ZoneSubCommand", "Sub Command Д" },
{"ZoneForestGasStation", "Forest Gas Station" },
{"ZoneForestSpawn", "Forest" },
{"ZonePort", "Pier" },
{"ZoneSanatorium1", "Sanatorium West" },
{"ZoneSanatorium2", "Sanatorium East" },
{"ZoneMiniHouse", "Mini House" },
{"ZoneBrokenVill", "Broken Village" },
{"ZoneWoodCutter", "Wood Cutter" },
{"ZoneCard1", "Card 1" },
};
private void Awake() {
logger = Logger;
Type FikaUtilExternalType = Type.GetType("Fika.Core.Coop.Utils.FikaBackendUtils, Fika.Core", false);
if (FikaUtilExternalType != null) {
FikaIsPlayerHost = AccessTools.Field(FikaUtilExternalType, "MatchingType");
}
// Initialize configuration entries
showBossesKeyCode = Config.Bind("General", "Keyboard Shortcut", new KeyboardShortcut(KeyCode.O), "Key to show boss notifications.");
showNotificationsOnRaidStart = Config.Bind("General", "Show Bosses on Raid Start", true, "Show boss notifications on raid start.");
// showBossLocation = Config.Bind("Balance", "Show Boss Spawn Location", true, "Show boss locations in notification.");
// showBossDetected = Config.Bind("In-Raid Updates", "Show Boss Detected Notification", true, "Show detected notification when bosses spawn during the raid.");
// intelCenterUnlockLevel = Config.Bind("Balance", "Intel Center Level Requirement", 0, "Level to unlock at.");
intelCenterUnlockLevel = Config.Bind("Intel Center Unlocks (4 means Disabled)", "1. Intel Center Level Requirement", 0,
new ConfigDescription("Level to unlock plain notifications at.",
new AcceptableValueRange<int>(0, 4)));
// intelCenterLocationUnlockLevel = Config.Bind("Balance", "Intel Center Location Level Requirement", 0, "Unlocks showing boss spawn location.");
intelCenterLocationUnlockLevel = Config.Bind("Intel Center Unlocks (4 means Disabled)", "2. Intel Center Location Level Requirement", 0,
new ConfigDescription("Unlocks showing boss spawn location in notification.",
new AcceptableValueRange<int>(0, 4)));
// intelCenterDetectedUnlockLevel = Config.Bind("Intel Center Unlocks", "Intel Center Detection Requirement", 0, "Unlocks showing boss detected notification.");
intelCenterDetectedUnlockLevel = Config.Bind("Intel Center Unlocks (4 means Disabled)", "3. Intel Center Detection Requirement", 0,
new ConfigDescription("Unlocks showing boss detected notification. (When you get near a boss)",
new AcceptableValueRange<int>(0, 4)));
// Enable patches
new BossLocationSpawnPatch().Enable();
new NewGamePatch().Enable();
new BotBossPatch().Enable();
// Subscribe to config changes
Config.SettingChanged += Config_SettingChanged;
Logger.LogInfo($"Plugin BossNotifier is loaded!");
}
// Event handler for configuration changes
private void Config_SettingChanged(object sender, SettingChangedEventArgs e) {
ConfigEntryBase changedSetting = e.ChangedSetting;
// If player is in a raid, reset their notifications to reflect changes
if (BossNotifierMono.Instance) BossNotifierMono.Instance.GenerateBossNotifications();
}
// Get boss name by type
public static string GetBossName(WildSpawnType type) {
// Return boss name if found, otherwise null
return bossNames.ContainsKey(type) ? bossNames[type] : null;
}
// Get zone name by ID
public static string GetZoneName(string zoneId) {
// Return zone name if found, otherwise clean up the zoneId
if (zoneNames.ContainsKey(zoneId)) return zoneNames[zoneId];
string location = zoneId.Replace("Bot", "").Replace("Zone", "");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < location.Length; i++) {
char c = location[i];
if (char.IsUpper(c) && i != 0 && i < location.Length - 1 && !char.IsUpper(location[i + 1]) && !char.IsDigit(location[i + 1])) {
sb.Append(" ");
}
sb.Append(c);
}
return sb.ToString().Replace("_", " ").Trim();
}
}
// Patch for tracking boss location spawns
internal class BossLocationSpawnPatch : ModulePatch {
protected override MethodBase GetTargetMethod() => typeof(BossLocationSpawn).GetMethod("Init");
// Bosses in raid along with their locations ex Key: Reshala Value: Dorms, Gas Station
public static Dictionary<string, string> bossesInRaid = new Dictionary<string, string>();
// Add boss spawn if not already present
private static void TryAddBoss(string boss, string location) {
if (location == null) {
Logger.LogError("Tried to add boss with null location.");
return;
}
// If boss is already added
if (bossesInRaid.ContainsKey(boss)) {
// If location isn't already present, and location isnt empty, add it.
if (!bossesInRaid[boss].Contains(location) && !location.Equals("")) {
// If the boss has an empty location, set new location
if (bossesInRaid[boss].Equals("")) {
bossesInRaid[boss] = location;
} else {
// Otherwise if boss has a location, append our new location
bossesInRaid[boss] += ", " + location;
}
}
} else {
// Add the boss entry
bossesInRaid.Add(boss, location);
}
}
// Handle boss location spawns
[PatchPostfix]
private static void PatchPostfix(BossLocationSpawn __instance) {
// If the boss will spawn
if (__instance.ShallSpawn) {
// Get it's name, if no name found then return.
string name = BossNotifierPlugin.GetBossName(__instance.BossType);
if (name == null) return;
// Get the spawn location
string location = BossNotifierPlugin.GetZoneName(__instance.BornZone);
BossNotifierPlugin.Log(LogLevel.Info, $"Boss {name} @ zone {__instance.BornZone} translated to {(location == null ? __instance.BornZone.Replace("Bot", "").Replace("Zone", ""): location)}");
if (location == null) {
// If it's null then use cleaned up BornZone
TryAddBoss(name, __instance.BornZone.Replace("Bot", "").Replace("Zone", ""));
} else if (location.Equals("")) {
// If it's empty location (Factory Spawn)
TryAddBoss(name, "");
} else {
// Location is valid
TryAddBoss(name, location);
}
}
}
}
// Patch for tracking live boss spawns
internal class BotBossPatch : ModulePatch {
protected override MethodBase GetTargetMethod() => typeof(BotBoss).GetConstructors()[0];
// Bosses spawned in raid
public static HashSet<string> spawnedBosses = new HashSet<string>();
// Notification queue
public static Queue<string> vicinityNotifications = new Queue<string>();
[PatchPostfix]
private static void PatchPostfix(BotBoss __instance) {
WildSpawnType role = __instance.Owner.Profile.Info.Settings.Role;
// Get it's name, if no name found then return.
string name = BossNotifierPlugin.GetBossName(role);
if (name == null) return;
// Get the spawn location
Vector3 positionVector = __instance.Player().Position;
string position = $"{(int)positionVector.x}, {(int)positionVector.y}, {(int)positionVector.z}";
// {name} has spawned at (x, y, z) on {map}
BossNotifierPlugin.Log(LogLevel.Info, $"{name} has spawned at {position} on {Singleton<GameWorld>.Instance.LocationId}");
// Add boss to spawnedBosses
spawnedBosses.Add(name);
vicinityNotifications.Enqueue($"{name} {(BossNotifierPlugin.pluralBosses.Contains(name) ? "have" : "has")} been detected in your vicinity.");
//if (BossNotifierMono.Instance.intelCenterLevel >= BossNotifierPlugin.intelCenterDetectedUnlockLevel.Value) {
// NotificationManagerClass.DisplayMessageNotification($"{name} {(BossNotifierPlugin.pluralBosses.Contains(name) ? "have" : "has")} been detected in your vicinity.", ENotificationDurationType.Long);
// BossNotifierMono.Instance.GenerateBossNotifications();
//}
}
}
// Patch for hooking when a raid is started
internal class NewGamePatch : ModulePatch {
protected override MethodBase GetTargetMethod() => typeof(GameWorld).GetMethod("OnGameStarted");
[PatchPrefix]
public static void PatchPrefix() {
// Start BossNotifierMono
BossNotifierMono.Init();
}
}
// Monobehavior for boss notifier
class BossNotifierMono : MonoBehaviour {
// Required to invalidate notification cache on settings changed event.
public static BossNotifierMono Instance;
// Caching the notification messages
private List<string> bossNotificationMessages;
// Intel Center level, only updated when raid is entered.
public int intelCenterLevel;
private void SendBossNotifications() {
if (!ShouldFunction()) return;
if (intelCenterLevel < BossNotifierPlugin.intelCenterUnlockLevel.Value) return;
// If we have no notifications to display, send one saying there's no bosses located.
if (bossNotificationMessages.Count == 0) {
NotificationManagerClass.DisplayMessageNotification("No Bosses Located", ENotificationDurationType.Long);
return;
}
foreach (var bossMessage in bossNotificationMessages) {
NotificationManagerClass.DisplayMessageNotification(bossMessage, ENotificationDurationType.Long);
}
}
// Initializes boss notifier mono and attaches it to the game world object
public static void Init() {
if (Singleton<GameWorld>.Instantiated) {
Instance = Singleton<GameWorld>.Instance.GetOrAddComponent<BossNotifierMono>();
BossNotifierPlugin.Log(LogLevel.Info, $"Game started on map {Singleton<GameWorld>.Instance.LocationId}");
if (ClientAppUtils.GetMainApp().GetClientBackEndSession() == null) {
Instance.intelCenterLevel = 0;
} else {
Instance.intelCenterLevel = ClientAppUtils.GetMainApp().GetClientBackEndSession().Profile.Hideout.Areas[11].Level;
}
}
}
public void Start() {
GenerateBossNotifications();
if (!BossNotifierPlugin.showNotificationsOnRaidStart.Value) return;
Invoke("SendBossNotifications", 2f);
}
public void Update() {
if (BotBossPatch.vicinityNotifications.Count > 0) {
string notif = BotBossPatch.vicinityNotifications.Dequeue();
if (Instance.intelCenterLevel >= BossNotifierPlugin.intelCenterDetectedUnlockLevel.Value) {
NotificationManagerClass.DisplayMessageNotification(notif, ENotificationDurationType.Long);
Instance.GenerateBossNotifications();
}
}
if (IsKeyPressed(BossNotifierPlugin.showBossesKeyCode.Value)) {
SendBossNotifications();
}
}
public void OnDestroy() {
// Clear out boss locations for this raid
BossLocationSpawnPatch.bossesInRaid.Clear();
// Clear out spawned bosses for this raid
BotBossPatch.spawnedBosses.Clear();
}
public bool ShouldFunction() {
if (BossNotifierPlugin.FikaIsPlayerHost == null) return true;
return (int)BossNotifierPlugin.FikaIsPlayerHost.GetValue(null) == 2;
}
public void GenerateBossNotifications() {
// Clear out boss notification cache
bossNotificationMessages = new List<string>();
// Check if it's daytime to prevent showing Cultist notif.
// This is the same method that DayTimeCultists patches so if that mod is installed then this always returns false
bool isDayTime = Singleton<IBotGame>.Instance.BotsController.ZonesLeaveController.IsDay();
// Get whether location is unlocked or not.
bool isLocationUnlocked = intelCenterLevel >= BossNotifierPlugin.intelCenterLocationUnlockLevel.Value;
// Get whether detection is unlocked or not.
bool isDetectionUnlocked = intelCenterLevel >= BossNotifierPlugin.intelCenterDetectedUnlockLevel.Value;
foreach (var bossSpawn in BossLocationSpawnPatch.bossesInRaid) {
// If it's daytime then cultists don't spawn
if (isDayTime && bossSpawn.Key.Equals("Cultists")) continue;
// If boss has been spawned/detected
bool isDetected = BotBossPatch.spawnedBosses.Contains(bossSpawn.Key);
string notificationMessage;
// If we don't have locations or value is null/whitespace
if (!isLocationUnlocked || bossSpawn.Value == null || bossSpawn.Value.Equals("")) {
// Then just show that they spawned and nothing else
notificationMessage = $"{bossSpawn.Key} {(BossNotifierPlugin.pluralBosses.Contains(bossSpawn.Key) ? "have" : "has")} been located.{(isDetectionUnlocked && isDetected ? $" ✓" : "")}";
} else {
// Location is unlocked and location isnt null
notificationMessage = $"{bossSpawn.Key} {(BossNotifierPlugin.pluralBosses.Contains(bossSpawn.Key) ? "have" : "has")} been located near {bossSpawn.Value}{(isDetectionUnlocked && isDetected ? $" ✓" : "")}";
}
BossNotifierPlugin.Log(LogLevel.Info, notificationMessage);
// Add notification to cache list
bossNotificationMessages.Add(notificationMessage);
}
}
// Credit to DrakiaXYZ, thank you!
bool IsKeyPressed(KeyboardShortcut key) {
if (!UnityInput.Current.GetKeyDown(key.MainKey)) {
return false;
}
foreach (var modifier in key.Modifiers) {
if (!UnityInput.Current.GetKey(modifier)) {
return false;
}
}
return true;
}
}
}