2using System.Collections.Generic;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
10using Microsoft.Xna.Framework;
11using Microsoft.Xna.Framework.Input;
12using Microsoft.Xna.Framework.Graphics;
18 public partial class CUI
20 public static void CheckOtherPatches(
string msg =
"")
23 CUI.Log($
"Harmony.GetAllPatchedMethods:", Color.Lime);
24 foreach (MethodBase mb
in Harmony.GetAllPatchedMethods())
26 Patches patches = Harmony.GetPatchInfo(mb);
28 if (patches.Prefixes.Count() > 0 || patches.Postfixes.Count() > 0)
30 CUI.Log($
"{mb.DeclaringType}.{mb.Name}:");
31 if (patches.Prefixes.Count() > 0)
33 CUI.Log($
" Prefixes:");
34 foreach (Patch patch
in patches.Prefixes) { CUI.Log($
" {patch.owner}"); }
37 if (patches.Postfixes.Count() > 0)
39 CUI.Log($
" Postfixes:");
40 foreach (Patch patch
in patches.Postfixes) { CUI.Log($
" {patch.owner}"); }
46 public static void CheckPatches(
string typeName,
string methodName)
48 CUI.Log($
"Harmony.GetAllPatchedMethods:", Color.Lime);
49 foreach (MethodBase mb
in Harmony.GetAllPatchedMethods())
52 !
string.Equals(typeName, mb.DeclaringType.Name, StringComparison.OrdinalIgnoreCase) ||
53 !
string.Equals(methodName, mb.Name, StringComparison.OrdinalIgnoreCase)
56 Patches patches = Harmony.GetPatchInfo(mb);
58 if (patches.Prefixes.Count() > 0 || patches.Postfixes.Count() > 0)
60 CUI.Log($
"{mb.DeclaringType}.{mb.Name}:");
61 if (patches.Prefixes.Count() > 0)
63 CUI.Log($
" Prefixes:");
64 foreach (Patch patch
in patches.Prefixes) { CUI.Log($
" {patch.owner}"); }
67 if (patches.Postfixes.Count() > 0)
69 CUI.Log($
" Postfixes:");
70 foreach (Patch patch
in patches.Postfixes) { CUI.Log($
" {patch.owner}"); }
76 private static void AddHooks()
78 GameMain.LuaCs.Hook.Add(
"GUI_Draw_Prefix", CUIHookID, (
object[] args) =>
80 GUI_Draw_Prefix((SpriteBatch)args.ElementAtOrDefault(0));
84 GameMain.LuaCs.Hook.Add(
"GUI_DrawCursor_Prefix", CUIHookID, (
object[] args) =>
86 GUI_DrawCursor_Prefix((SpriteBatch)args.ElementAtOrDefault(0));
97 GameMain.LuaCs.Hook.Add(
"KeyboardDispatcher_set_Subscriber_Prefix", CUIHookID, (
object[] args) =>
99 KeyboardDispatcher_set_Subscriber_Prefix(
100 (KeyboardDispatcher)args.ElementAtOrDefault(0),
101 (IKeyboardSubscriber)args.ElementAtOrDefault(1)
106 GameMain.LuaCs.Hook.Add(
"GUI_InputBlockingMenuOpen_Postfix", CUIHookID, (
object[] args) => CUI.InputBlockingMenuOpen);
108 GameMain.LuaCs.Hook.Add(
"GUI_TogglePauseMenu_Postfix", CUIHookID, (
object[] args) =>
110 GUI_TogglePauseMenu_Postfix();
115 private static void AddHarmonyPatches()
118 original: typeof(GUI).GetMethod(
"Draw", AccessTools.all),
119 prefix:
new HarmonyMethod(typeof(CUI).GetMethod(
"GUI_Draw_Prefix", AccessTools.all))
123 original: typeof(GUI).GetMethod(
"DrawCursor", AccessTools.all),
124 prefix:
new HarmonyMethod(typeof(CUI).GetMethod(
"GUI_DrawCursor_Prefix", AccessTools.all))
138 original: typeof(Camera).GetMethod(
"MoveCamera", AccessTools.all),
139 prefix:
new HarmonyMethod(typeof(CUI).GetMethod(
"Camera_MoveCamera_Prefix", AccessTools.all))
143 original: typeof(KeyboardDispatcher).GetMethod(
"set_Subscriber", AccessTools.all),
144 prefix:
new HarmonyMethod(typeof(CUI).GetMethod(
"KeyboardDispatcher_set_Subscriber_Prefix", AccessTools.all))
148 original: typeof(GUI).GetMethod(
"TogglePauseMenu", AccessTools.all,
new Type[] { }),
149 postfix:
new HarmonyMethod(typeof(CUI).GetMethod(
"GUI_TogglePauseMenu_Postfix", AccessTools.all))
153 original: typeof(GUI).GetMethod(
"get_InputBlockingMenuOpen", AccessTools.all),
154 postfix:
new HarmonyMethod(typeof(CUI).GetMethod(
"GUI_InputBlockingMenuOpen_Postfix", AccessTools.all))
158 private static void AddAnyway()
160 GameMain.LuaCs.Hook.Add(
"think", CUIHookID, (
object[] args) =>
163 CUIUpdate(Timing.TotalTime);
167 GameMain.LuaCs.Hook.Add(
"item.created", CUIHookID, (
object[] args) =>
169 Item item = args.ElementAtOrDefault(0) as Item;
170 AttachedItems.ConnectIfMapped(item);
174 GameMain.LuaCs.Hook.Add(
"roundStart", CUIHookID, (
object[] args) =>
176 AttachedItems.ShakeTheRefs();
182 private static void PatchAll()
191 private static void GameMain_Update_Postfix(GameTime gameTime)
193 if (GhostDetector.Check())
return;
194 CUIUpdate(gameTime.TotalGameTime.TotalSeconds);
196 private static void CUIUpdate(
double time)
198 if (
Main ==
null) CUI.Error($
"CUIUpdate: CUI.Main in {HookIdentifier} was null, tell the dev", 20, 5);
201 AttachedItems.UpdateAll();
202 CUIAnimation.UpdateAllAnimations(time);
203 CUI.Input?.Scan(time);
209 CUI.Warning($
"CUI: {e}");
213 private static void GUI_Draw_Prefix(SpriteBatch spriteBatch)
215 if (GhostDetector.Check())
return;
217 catch (Exception e) { CUI.Warning($
"CUI: {e}"); }
220 private static void GUI_DrawCursor_Prefix(SpriteBatch spriteBatch)
222 if (GhostDetector.Check())
return;
224 catch (Exception e) { CUI.Warning($
"CUI: {e}"); }
227 private static void GUI_UpdateMouseOn_Postfix(ref GUIComponent __result)
229 if (GhostDetector.Check())
return;
233 private static void CUIUpdateMouseOn()
235 if (
Main ==
null) CUI.Error($
"CUIUpdateMouseOn: CUI.Main in {HookIdentifier} was null, tell the dev", 20);
236 if (GUI.MouseOn ==
null &&
Main !=
null &&
Main.MouseOn !=
null &&
Main.MouseOn !=
Main) GUI.MouseOn = CUIComponent.dummyComponent;
240 private static Dictionary<string, bool> CUIBlockScroll()
242 if (GUI.MouseOn != CUIComponent.dummyComponent)
return null;
244 return new Dictionary<string, bool>()
246 [
"allowZoom"] =
false,
250 private static void Camera_MoveCamera_Prefix(
float deltaTime, ref
bool allowMove, ref
bool allowZoom,
bool allowInput,
bool? followSub)
252 if (GhostDetector.Check())
return;
253 if (GUI.MouseOn == CUIComponent.dummyComponent) allowZoom =
false;
256 private static void KeyboardDispatcher_set_Subscriber_Prefix(KeyboardDispatcher __instance, IKeyboardSubscriber value)
258 if (GhostDetector.Check())
return;
262 public static void GUI_InputBlockingMenuOpen_Postfix(ref
bool __result)
264 if (GhostDetector.Check())
return;
265 __result = __result || CUI.InputBlockingMenuOpen;
268 public static void GUI_TogglePauseMenu_Postfix()
270 if (GhostDetector.Check())
return;
273 if (GUI.PauseMenu !=
null)
275 GUIFrame frame = GUI.PauseMenu;
276 GUIComponent pauseMenuInner = frame.GetChild(1);
277 GUIComponent list = frame.GetChild(1).GetChild(0);
278 GUIButton resumeButton = (GUIButton)list.GetChild(0);
280 GUIButton.OnClickedHandler oldHandler = resumeButton.OnClicked;
282 resumeButton.OnClicked = (GUIButton button,
object obj) =>
284 bool guh = oldHandler(button, obj);
285 CUI.InvokeOnPauseMenuToggled();
290 catch (Exception e) { CUI.Warning(e); }
292 CUI.InvokeOnPauseMenuToggled();
static CUIMainComponent TopMain
Orchestrates Drawing and updates, there could be only one CUI.TopMain is located above vanilla GUI.
static CUIMainComponent Main
Orchestrates Drawing and updates, there could be only one CUI.Main is located under vanilla GUI.
static bool UseCursedPatches
If true other compiled mods that use CUI will break But if false then you'll have to setup hooks with...
static CUIFocusResolver FocusResolver
Adapter to vanilla focus system, don't use.
new void Draw(SpriteBatch spriteBatch)
Here component should be drawn.