-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropsPlugin.cs
122 lines (102 loc) · 3.94 KB
/
DropsPlugin.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
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
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using SideLoader;
using SideLoader.UI.Modules;
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Nm1fiOutward.Drops
{
[BepInPlugin(GUID, NAME, VERSION)]
[BepInDependency(SL.GUID, "3.3.3")]
public class DropsPlugin : BaseUnityPlugin
{
internal static DropsPlugin Instance { get; private set; }
public const string GUID = "github.raphendyr.droptablealterations";
public const string NAME = "DropTableAlterations";
public const string FEATURE_VERSION = "0.2";
public const string VERSION = FEATURE_VERSION + ".0";
public static bool IsEnabled => Instance != null && Instance.isEnabled;
internal static new DropsConfig Config;
// Logging helpers
private static string LogLevelColor(LogLevel level)
{
switch (level)
{
case LogLevel.Warning: return ColorUtility.ToHtmlStringRGB(Color.yellow);
case LogLevel.Error: return ColorUtility.ToHtmlStringRGB(Color.red);
default: return ColorUtility.ToHtmlStringRGB(Color.white);
}
}
internal static void Log(string message, LogLevel level = LogLevel.Message)
{
Instance.Logger.Log(level, message);
DebugConsole.Log(NAME + ": " + message, LogLevelColor(level));
}
internal static void LogInfo(string message) => Log(message, LogLevel.Info);
internal static void LogWarning(string message) => Log(message, LogLevel.Warning);
internal static void LogError(string message) => Log(message, LogLevel.Error);
// Instance
internal void Awake()
{
if (Instance != null)
return;
Instance = this;
Log($"Version {VERSION} loading...");
Config = new DropsConfig(base.Config);
if (Config.Enabled)
SetEnabled();
else
Logger.Log(LogLevel.Message, "Mod is not enabled, not injecting game hooks.");
}
private bool isEnabled = false;
internal void SetEnabled(bool enabled = true)
{
if (enabled == isEnabled)
return;
if (enabled)
{
try
{
new Harmony(GUID).PatchAll();
}
catch (Exception e)
{
Logger.Log(LogLevel.Warning, "Exception applying Harmony patches!");
SL.LogInnerException(e);
Logger.Log(LogLevel.Warning, "Mod is not enabled!");
return;
}
SL.OnSceneLoaded += OnSceneLoaded;
SceneManager.sceneUnloaded += OnSceneUnloaded;
Logger.Log(LogLevel.Message, "Enabled game hooks");
}
else
{
SL.OnSceneLoaded -= OnSceneLoaded;
SceneManager.sceneUnloaded -= OnSceneUnloaded;
StopAllCoroutines();
new Harmony(GUID).UnpatchAll(GUID);
Logger.Log(LogLevel.Message, "Disabled game hooks"); ;
}
isEnabled = enabled;
}
private void OnSceneUnloaded(Scene current)
=> StopAllCoroutines();
private void OnSceneLoaded()
{
if (!Config.Enabled || PhotonNetwork.isNonMasterClientInRoom)
return;
DropsPatcher.UpdatedMerchantInventories.Clear();
if (Config.Simulate)
StartCoroutine(DropTableLogger.LogAllCoro());
var treasureChests = Resources.FindObjectsOfTypeAll<TreasureChest>()
.Where(chest => chest.gameObject?.scene != null)
.ToList();
if (treasureChests.Any())
StartCoroutine(DropsPatcher.PatchTreasureChestsCoro(treasureChests));
}
}
}