-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomBeatmaps.cs
161 lines (141 loc) · 6.08 KB
/
CustomBeatmaps.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
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
using System;
using System.IO;
using System.Threading.Tasks;
using System.Timers;
using BepInEx;
using CustomBeatmaps.CustomPackages;
using CustomBeatmaps.Patches;
using CustomBeatmaps.UI;
using CustomBeatmaps.Util;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;
using Debug = UnityEngine.Debug;
namespace CustomBeatmaps
{
[BepInPlugin("tacodog.unbeatable.custombeatmaps", "Custom Beatmaps V3", "3.3.7")]
public class CustomBeatmaps : BaseUnityPlugin
{
public static ModConfig ModConfig { get; private set; }
public static BackendConfig BackendConfig { get; private set; }
public static UserSession UserSession { get; private set; }
public static LocalPackageManager LocalUserPackages { get; private set; }
public static LocalPackageManager LocalServerPackages { get; private set; }
public static SubmissionPackageManager SubmissionPackageManager { get; private set; }
public static OSUBeatmapManager OSUBeatmapManager { get; private set; }
public static PlayedPackageManager PlayedPackageManager { get; private set; }
public static ServerHighScoreManager ServerHighScoreManager { get; private set; }
public static BeatmapDownloader Downloader { get; private set; }
public static GameMemory Memory { get; private set; }
private static readonly string MEMORY_LOCATION = "CustomBeatmapsV3-Data/.memory";
// Check for config reload every 2 seconds
private readonly Timer _checkConfigReload = new Timer(2000);
static CustomBeatmaps()
{
// Log inner exceptions by default
EventBus.ExceptionThrown += ex => ScheduleHelper.SafeInvoke(() => Debug.LogException(ex));
// Anything with Static access should be ALWAYS present.
LocalUserPackages = new LocalPackageManager(OnError);
LocalServerPackages = new LocalPackageManager(OnError);
SubmissionPackageManager = new SubmissionPackageManager(OnError);
OSUBeatmapManager = new OSUBeatmapManager();
ServerHighScoreManager = new ServerHighScoreManager();
if (!Directory.Exists("CustomBeatmapsV3-Data"))
Directory.CreateDirectory("CustomBeatmapsV3-Data");
// Load game memory from disk
Memory = GameMemory.Load(MEMORY_LOCATION);
ConfigHelper.LoadConfig("custombeatmaps_config.json",() => new ModConfig(), config =>
{
ModConfig = config;
// Local package folders
LocalUserPackages.SetFolder(config.UserPackagesDir);
LocalServerPackages.SetFolder(config.ServerPackagesDir);
OSUBeatmapManager.SetOverride(config.OsuSongsOverrideDirectory);
PlayedPackageManager = new PlayedPackageManager(config.PlayedBeatmapList);
});
ConfigHelper.LoadConfig("CustomBeatmapsV3-Data/custombeatmaps_backend.json", () => new BackendConfig(), config => BackendConfig = config);
UserSession = new UserSession();
Downloader = new BeatmapDownloader();
}
private static void OnError(Exception ex)
{
ScheduleHelper.SafeInvoke(() => Debug.LogException(ex));
try
{
EventBus.ExceptionThrown?.Invoke(ex);
} catch (Exception e)
{
// ???
ScheduleHelper.SafeInvoke(() => Debug.LogException(e));
}
}
private void Awake()
{
Logger.LogInfo("CustomBeatmapsV3: Awake?");
// At a regular interval, reload changed configs.
_checkConfigReload.Elapsed += (obj, evt) => ScheduleHelper.SafeInvoke(ConfigHelper.ReloadChangedConfigs);
_checkConfigReload.Start();
// User session
Task.Run(UserSession.AttemptLogin);
// Harmony Patching
Type[] classesToPatch = {
typeof(DebugLogPatch),
typeof(WhiteLabelMainMenuPatch),
typeof(CustomBeatmapLoadingOverridePatch),
typeof(OsuEditorPatch),
typeof(HighScoreScreenPatch),
typeof(PauseMenuPatch),
typeof(DisablePracticeRoomOpenerPatch),
typeof(CursorUnhidePatch),
typeof(OneLifeModePatch),
typeof(FlipModePatch),
typeof(SimpleJankHighScoreSongReplacementPatch),
typeof(DisableRewiredMouseInputPatch)
};
foreach (var toPatch in classesToPatch)
{
try
{
Logger.LogDebug($"Patching {toPatch}");
Harmony.CreateAndPatchAll(toPatch);
}
catch (Exception e)
{
Logger.LogError("EXCEPTION CAUGHT while PATCHING:");
Logger.LogError(e.ToString());
}
}
// Disclaimer screen
Logger.LogDebug($"Opening Disclaimer Disabled: {Memory.OpeningDisclaimerDisabled}");
if (!Memory.OpeningDisclaimerDisabled)
{
// Make the game freeze
Time.timeScale = 0;
var disclaimer = new GameObject().AddComponent<OpeningDisclaimerUIBehaviour>();
disclaimer.OnSelect += () =>
{
// Reload
Time.timeScale = 1;
Memory.OpeningDisclaimerDisabled = true;
GameMemory.Save(MEMORY_LOCATION, Memory);
SceneManager.LoadScene(0);
};
}
}
private static bool _quitted;
private void OnDestroy()
{
// Save our memory
if (!_quitted)
GameMemory.Save(MEMORY_LOCATION, Memory);
_quitted = true;
}
private void OnApplicationQuit()
{
// Save our memory
if (!_quitted)
GameMemory.Save(MEMORY_LOCATION, Memory);
_quitted = true;
}
}
}