forked from aedenthorn/ValheimMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatches.cs
71 lines (61 loc) · 2.96 KB
/
Patches.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
// Based on code made by MarC0 / ManlyMarco
// Copyright 2018 GNU General Public License v3.0
using HarmonyLib;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace ConfigurationManager
{
static class Patches
{
const string OpenLogString = "Show Player.log";
private static GameObject OpenMenuButton { get; set; }
internal static void ApplyPatches()
{
Harmony harmony = new Harmony(BepInExPlugin.GUID);
harmony.Patch(
original: AccessTools.Method(typeof(FejdStartup), "Start"),
postfix: new HarmonyMethod(AccessTools.Method(typeof(Patches), nameof(Patches.Start)))
);
harmony.Patch(
original: AccessTools.Method(typeof(Console), "InputText"),
prefix: new HarmonyMethod(AccessTools.Method(typeof(Patches), nameof(Patches.InputText)))
);
BepInExPlugin._openMenuText.SettingChanged += (s,e) => SetupMenuButton();
BepInExPlugin._showMenuButton.SettingChanged += (s,e) => SetupMenuButton();
}
internal static void SetupMenuButton()
{
if (OpenMenuButton == null && FejdStartup.instance?.m_mainMenu.GetComponentsInChildren<Text>().FirstOrDefault(t => t.text == OpenLogString || t.text == Localization.instance.Localize(OpenLogString)) is Text openLogButton)
{
OpenMenuButton = Object.Instantiate(openLogButton.transform.parent.gameObject, openLogButton.transform.parent.parent);
OpenMenuButton.name = "OpenConfigMenu";
OpenMenuButton.transform.localPosition += new Vector3(0, 25, 0);
OpenMenuButton.GetComponentInChildren<Text>().text = BepInExPlugin._openMenuText.Value;
var button = OpenMenuButton.GetComponent<Button>();
button.onClick = new Button.ButtonClickedEvent();
button.onClick.AddListener(() => BepInExPlugin.context.DisplayingWindow = true);
}
OpenMenuButton.GetComponentInChildren<Text>().text = BepInExPlugin._openMenuText.Value;
OpenMenuButton.SetActive(BepInExPlugin._showMenuButton.Value);
}
private static bool InputText(Console __instance)
{
string text = __instance.m_input.text;
if (text.ToLower().Equals($"{typeof(BepInExPlugin).Namespace.ToLower()} reset"))
{
BepInExPlugin.context.Config.Reload();
BepInExPlugin.context.Config.Save();
Traverse.Create(__instance).Method("AddString", new object[] { text }).GetValue();
Traverse.Create(__instance).Method("AddString", new object[] { $"{BepInExPlugin.context.Info.Metadata.Name} config reloaded" }).GetValue();
return false;
}
return true;
}
private static void Start()
{
SetupMenuButton();
}
}
}