-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.cs
225 lines (201 loc) · 8.14 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
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
/*
<\ _
\\ _/{
onyx _ \\ _- -_
ChapterSelect /{ / `\ _- - - -_
v^v^ _~ = ( @ \ - - - -_
ready? _- - ~-_ \( \ \ - - -_
_~ - - ~_ | 1 \ \ _-~-_ - -_
_- - - ~ |V: \ \ _-~ ~-_- -_
_-~ - - / | : \ \ ~-_- -_
_-~ - _.._ { | : _-`` ~- _-_
_-~ -__..--~ ~-_ { : \:}
=~__.--~~ ~-_\ : /
\ : /__
//`Y'--\\ =
<+ \\
\\ WWW
MMM
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using BepInEx;
using HarmonyLib;
using UnityEngine;
using ChapterSelect.Code;
using Koop;
using UnityEngine.Localization.Settings;
namespace ChapterSelect
{
public static class ReflectionExtensions
{
public static T GetFieldValue<T>(this object obj, string name)
{
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var field = obj.GetType().GetField(name, bindingFlags);
return (T)field?.GetValue(obj);
}
public static object CallPrivate(this object o, string methodName, params object[] args)
{
var mi = o.GetType ().GetMethod (methodName, BindingFlags.NonPublic | BindingFlags.Instance );
return mi != null ? mi.Invoke (o, args) : null;
}
}
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
public class Plugin : BaseUnityPlugin
{
public static BepInEx.Logging.ManualLogSource LOG;
private static Harmony _harmony;
public static AssetBundle Assets;
public static readonly Dictionary<string, Texture2D> Textures = new();
/**
* Plugin entry. This has 3 responsibilities:
* - Set up the managers
* - Insert localization info
* - Apply patches
*
* Do nothing else here. Put it in a manager if it's important.
*/
private void Awake()
{
// Plugin startup logic
Logger.LogInfo($"Plugin {PluginInfo.PLUGIN_GUID} is loaded!");
LOG = BepInEx.Logging.Logger.CreateLogSource("ChapterSelect");
BepInEx.Logging.Logger.Sources.Add(LOG);
// inject managers; kindly place them alongside the game's managers
var managerMaster = GameObject.Find("MANAGER_MASTER");
var chaptersMaster = new GameObject("MOD_ChapterSelect");
chaptersMaster.transform.SetParent(managerMaster.transform);
var gameAssetLoader = new GameObject("CS_GameAssetLoader");
DontDestroyOnLoad(gameAssetLoader);
gameAssetLoader.transform.SetParent(chaptersMaster.transform);
gameAssetLoader.AddComponent<Mgr_CS_GameAssetLoader>().enabled = true;
var chapterSelect = new GameObject("ChapterSelectManager");
DontDestroyOnLoad(chapterSelect);
chapterSelect.transform.SetParent(chaptersMaster.transform);
chapterSelect.AddComponent<Mgr_ChapterSelect>().enabled = true;
var uiInjection = new GameObject("CS_UIInjection");
DontDestroyOnLoad(uiInjection);
uiInjection.transform.SetParent(chaptersMaster.transform);
uiInjection.AddComponent<Mgr_CS_UIInjection>().enabled = true;
var saveManagement = new GameObject("CS_SaveManagement");
DontDestroyOnLoad(saveManagement);
saveManagement.transform.SetParent(chaptersMaster.transform);
saveManagement.AddComponent<Mgr_CS_SaveManagement>().enabled = true;
var table = LocalizationSettings.StringDatabase.GetTable("MISC", LocalizationSettings.AvailableLocales.GetLocale("en"));
foreach (var element in Localization.GetEnglishLocalizations())
{
table.AddEntry(element.Key, element.Value);
}
_harmony = new Harmony(PluginInfo.PLUGIN_GUID);
_harmony.PatchAll();
Assets = OnyxUtils.LoadEmbeddedAssetBundle("ChapterSelect.Assets.chaptermod.assets");
foreach (var scene in SelectableScene.AllScenes)
{
Textures[scene.SceneName] = Assets.LoadAsset<Texture2D>("CH_I_" + scene.SceneName);
Textures["locked"] = Assets.LoadAsset<Texture2D>("locked");
}
}
}
/**
* Plugin Required Hooks. They are all here.
*
* Some guidelines:
* - Add as few hooks as possible. They damage readability as they lack surrounding file context.
* - Forward to managers if a function is getting too long. Keep it brief.
*/
[HarmonyPatch(typeof(Mgr_LevelFlow))]
[HarmonyPatch("ReadyToRevealNewScene")]
public class RevealNewScenePatch
{
static void Postfix()
{
Mgr_ChapterSelect.Instance.CallPrivate("_SceneLoadedInternal");
}
}
[HarmonyPatch(typeof(Mgr_LevelFlow))]
[HarmonyPatch("OnSceneLoaded")]
public class OnSceneLoadedPatch
{
static void Postfix(Mgr_LevelFlow __instance, UnityEngine.SceneManagement.Scene scene)
{
Mgr_ChapterSelect.Instance.CurrentScene = scene.name;
}
}
// Targeting specifically the instance method.
[HarmonyPatch(typeof(Mgr_Achievements))]
[HarmonyPatch("AwardAchievement")]
[HarmonyPatch(new Type[] { typeof(SO_Achievement) })]
public class AwardAchievementPatch
{
static bool Prefix(SO_Achievement ach)
{
return !Mgr_ChapterSelect.Instance.IsInThrowback;
}
}
[HarmonyPatch(typeof(TitleScreen))]
[HarmonyPatch("Awake")]
public class TitleScreenAwakePatch
{
static void Prefix(TitleScreen __instance)
{
if (Mgr_ChapterSelect.Instance.IsInThrowback)
Mgr_ChapterSelect.Instance.EndThrowback(); // restores save state
}
}
/**
* Whenever we're in a throwback, we want to jump back to the title scene when the scene finishes
* Implementing it like this is the preferred way as it won't break in-scene minigames :>
*/
[HarmonyPatch(typeof(Helper_Ink))]
[HarmonyPatch("ContainsSpecialCall")]
public class ContainsSpecialCallPatch
{
static bool Prefix(string speak, bool doAction, ref bool __result)
{
if (!Mgr_ChapterSelect.Instance.IsInThrowback)
return true;
if (speak.Contains("NEXT:"))
{
if (doAction)
{
Mgr_LevelFlow.Instance.nextSceneFromInk = "Title";
InkMaster.ActiveInstance.Continue();
}
__result = true;
return false;
}
return true;
}
}
/**
* Patch to disable saves whenever we're in a throwback.
*/
[HarmonyPatch(typeof(Mgr_Saves))]
[HarmonyPatch("SaveGameplay")]
public class SaveGameplayPatch
{
private static IEnumerator PostSavedNotif()
{
// Our prefix might prevent this callback from being sent
// If anything (?) uses this, it may block, so it's probably a good idea to send it as expected.
Notify gameDataSavedToDisk = Mgr_Saves.GameDataSavedToDisk;
if (gameDataSavedToDisk != null)
{
gameDataSavedToDisk();
}
yield break;
}
static bool Prefix()
{
if (Mgr_CS_SaveManagement.Instance == null)
return true;
if (Mgr_CS_SaveManagement.Instance.SavesEnabled)
return true;
Mgr_ChapterSelect.Instance.StartCoroutine(PostSavedNotif());
return false;
}
}
}