Skip to content

Commit

Permalink
Merge pull request #5 from ds5678/EpisodeFourFix
Browse files Browse the repository at this point in the history
Update for TLD 1.95
  • Loading branch information
ds5678 committed Oct 7, 2021
2 parents 333a72b + b9ee7e7 commit dbe4638
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Attributes/AttributeFieldTypes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using Harmony;
using HarmonyLib;

namespace ModSettings {
internal static class AttributeFieldTypes {
Expand Down
2 changes: 1 addition & 1 deletion CustomModeMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal static class CustomModeMenu {

internal static void BuildGUI() {
guiBuilt = true;
CustomModeGUIBuilder guiBuilder = new CustomModeGUIBuilder(InterfaceManager.m_Panel_CustomXPSetup);
CustomModeGUIBuilder guiBuilder = new CustomModeGUIBuilder(InterfaceManager.LoadPanel<Panel_CustomXPSetup>());

for (int position = 0; position < numPositions; ++position) {
List<ModSettingsBase> settingsAtCurrentPos = settingsAtPosition[position];
Expand Down
4 changes: 2 additions & 2 deletions GUI/CustomModeGUIBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal class CustomModeGUIBuilder : GUIBuilder {
internal void NextSection() {
if (sections.Count == 0) {
if (afterLast) {
Debug.LogWarning("[ModSettings] Exhausted all GUI sections, skipping NextSection!");
MelonLoader.MelonLogger.Warning("Exhausted all GUI sections, skipping NextSection!");
}
afterLast = true;
return;
Expand Down Expand Up @@ -88,7 +88,7 @@ internal class CustomModeGUIBuilder : GUIBuilder {
internal void Finish() {
// Make sure that all sections have been added to the table
if (sections.Count > 0) {
Debug.LogWarning("[ModSettings] More GUI elements in queue!");
MelonLoader.MelonLogger.Warning("More GUI elements in queue!");
while (sections.Count > 0) {
NextSection();
}
Expand Down
2 changes: 1 addition & 1 deletion GUI/CustomModePatches.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using Harmony;
using HarmonyLib;
using UnityEngine;

namespace ModSettings {
Expand Down
62 changes: 4 additions & 58 deletions GUI/GUIBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,6 @@ internal abstract class GUIBuilder {

internal const int gridCellHeight = 33;

private static readonly GameObject headerLabelPrefab;
private static readonly GameObject sliderPrefab;
private static readonly GameObject comboBoxPrefab;
private static readonly GameObject keyEntryPrefab;

static GUIBuilder() {
Transform firstSection = InterfaceManager.m_Panel_CustomXPSetup.m_ScrollPanelOffsetTransform.GetChild(0);

headerLabelPrefab = UnityEngine.Object.Instantiate(firstSection.Find("Header").gameObject);
headerLabelPrefab.SetActive(false);

comboBoxPrefab = UnityEngine.Object.Instantiate(InterfaceManager.m_Panel_CustomXPSetup.m_AllowInteriorSpawnPopupList.gameObject);
comboBoxPrefab.SetActive(false);

keyEntryPrefab = MakeKeyEntryPrefab();
keyEntryPrefab.SetActive(false);

UnityEngine.Object.DestroyImmediate(InterfaceManager.m_Panel_OptionsMenu.m_FieldOfViewSlider.m_SliderObject.GetComponent<GenericSliderSpawner>());
sliderPrefab = UnityEngine.Object.Instantiate(InterfaceManager.m_Panel_OptionsMenu.m_FieldOfViewSlider.gameObject);
sliderPrefab.SetActive(false);
sliderPrefab.transform.Find("Label_FOV").localPosition = new Vector3(-10, 0, -1);

// Fix slider hitbox
BoxCollider collider = sliderPrefab.GetComponentInChildren<BoxCollider>();
collider.center = new Vector3(150, 0);
collider.size = new Vector3(900, 30);
}

protected readonly UIGrid uiGrid;
protected readonly Il2Cpp.List<GameObject> menuItems;

Expand All @@ -48,32 +20,6 @@ internal abstract class GUIBuilder {
this.menuItems = menuItems;
}

private static GameObject MakeKeyEntryPrefab() {
GameObject result = GameObject.Instantiate(comboBoxPrefab);

Transform rebindingTab = InterfaceManager.m_Panel_OptionsMenu.m_RebindingTab.transform;
GameObject originalButton = rebindingTab?.FindChild("GameObject")?.FindChild("LeftSide")?.FindChild("Button_Rebinding")?.gameObject;
GameObject keybindingButton = GameObject.Instantiate(originalButton);

keybindingButton.transform.position = result.transform.FindChild("Label_Value").position;
keybindingButton.transform.parent = result.transform;
keybindingButton.name = "Keybinding_Button";

GameObject.DestroyImmediate(result.GetComponent<ConsoleComboBox>());
DestroyChild(result, "Button_Decrease");
DestroyChild(result, "Button_Increase");
DestroyChild(result, "Label_Value");

DestroyChild(keybindingButton, "Label_Name");

return result;
}

private static void DestroyChild(GameObject parent, string childName) {
GameObject child = parent?.transform?.FindChild(childName)?.gameObject;
if(child) GameObject.DestroyImmediate(child);
}

internal virtual void AddSettings(ModSettingsBase modSettings) {
foreach (FieldInfo field in modSettings.GetFields()) {
Attributes.GetAttributes(field, out SectionAttribute section, out NameAttribute name,
Expand Down Expand Up @@ -117,7 +63,7 @@ internal abstract class GUIBuilder {
private void AddHeader(SectionAttribute section) {
GameObject padding = NGUITools.AddChild(uiGrid.gameObject);
GameObject header = NGUITools.AddChild(uiGrid.gameObject);
GameObject label = NGUITools.AddChild(header, headerLabelPrefab);
GameObject label = NGUITools.AddChild(header, ObjectPrefabs.HeaderLabelPrefab);

label.SetActive(true);
label.transform.localPosition = new Vector2(-70, 0);
Expand All @@ -134,7 +80,7 @@ internal abstract class GUIBuilder {

private void AddKeySetting(ModSettingsBase modSettings, FieldInfo field, NameAttribute name, DescriptionAttribute description) {
// Create menu item
GameObject setting = CreateSetting(name, description, keyEntryPrefab, "Label");
GameObject setting = CreateSetting(name, description, ObjectPrefabs.KeyEntryPrefab, "Label");
GameObject keyButtonObject = setting.transform.FindChild("Keybinding_Button").gameObject;

CustomKeybinding customKeybinding = setting.AddComponent<CustomKeybinding>();
Expand Down Expand Up @@ -163,7 +109,7 @@ internal abstract class GUIBuilder {

private void AddChoiceSetting(ModSettingsBase modSettings, FieldInfo field, NameAttribute name, DescriptionAttribute description, ChoiceAttribute choice) {
// Create menu item
GameObject setting = CreateSetting(name, description, comboBoxPrefab, "Label");
GameObject setting = CreateSetting(name, description, ObjectPrefabs.ComboBoxPrefab, "Label");
ConsoleComboBox comboBox = setting.GetComponent<ConsoleComboBox>();

// Add selectable values
Expand Down Expand Up @@ -197,7 +143,7 @@ internal abstract class GUIBuilder {

private void AddSliderSetting(ModSettingsBase modSettings, FieldInfo field, NameAttribute name, DescriptionAttribute description, SliderAttribute range) {
// Create menu
GameObject setting = CreateSetting(name, description, sliderPrefab, "Label_FOV");
GameObject setting = CreateSetting(name, description, ObjectPrefabs.SliderPrefab, "Label_FOV");
ConsoleSlider slider = setting.GetComponent<ConsoleSlider>();
UILabel uiLabel = slider.m_SliderObject.GetComponentInChildren<UILabel>();
UISlider uiSlider = slider.m_SliderObject.GetComponentInChildren<UISlider>();
Expand Down
17 changes: 8 additions & 9 deletions GUI/ModSettingsGUI.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System.Collections.Generic;
using MelonLoader;
using System.Collections.Generic;
using UnhollowerBaseLib.Attributes;
using UnityEngine;
using Il2Cpp = Il2CppSystem.Collections.Generic;

namespace ModSettings {
[RegisterTypeInIl2Cpp]
internal class ModSettingsGUI : MonoBehaviour {

static ModSettingsGUI() {
UnhollowerRuntimeLib.ClassInjector.RegisterTypeInIl2Cpp<ModSettingsGUI>();
}

public ModSettingsGUI(System.IntPtr ptr) : base(ptr) { }

private readonly Dictionary<string, ModTab> modTabs = new Dictionary<string, ModTab>();
Expand All @@ -23,6 +21,8 @@ internal class ModSettingsGUI : MonoBehaviour {
private GameObject scrollBar;
private UISlider scrollBarSlider;

private void Awake() => Build();

[HideFromIl2Cpp]
internal void Build() {
Transform contentArea = transform.Find("GameObject");
Expand Down Expand Up @@ -87,7 +87,7 @@ internal class ModSettingsGUI : MonoBehaviour {
[HideFromIl2Cpp]
private GameObject CreateScrollBar(UIPanel scrollPanel) {
// Terrible code in this method. I've given up trying to fix their scroll bar layout
Panel_CustomXPSetup xpModePanel = InterfaceManager.m_Panel_CustomXPSetup;
Panel_CustomXPSetup xpModePanel = InterfaceManager.LoadPanel<Panel_CustomXPSetup>();
GameObject scrollbarParent = xpModePanel.m_Scrollbar;
GameObject scrollbarPrefab = scrollbarParent.transform.GetChild(0).gameObject;
GameObject scrollbar = NGUITools.AddChild(gameObject, scrollbarPrefab);
Expand Down Expand Up @@ -196,8 +196,7 @@ internal class ModSettingsGUI : MonoBehaviour {
}

private void OnEnable() {
ModSettingsMenu.SetSettingsVisible(isMainMenu: InterfaceManager.IsMainMenuActive(), visible: true);

ModSettingsMenu.SetSettingsVisible(isMainMenu: InterfaceManager.IsMainMenuEnabled(), visible: true);
if (modSelector.items.Count > 0) {
modSelector.items.Sort();

Expand All @@ -208,7 +207,7 @@ internal class ModSettingsGUI : MonoBehaviour {
}

private void OnDisable() {
ModSettingsMenu.SetSettingsVisible(isMainMenu: InterfaceManager.IsMainMenuActive(), visible: false);
ModSettingsMenu.SetSettingsVisible(isMainMenu: InterfaceManager.IsMainMenuEnabled(), visible: false);

previousMod = modSelector?.value;
foreach (ModTab tab in modTabs.Values) {
Expand Down
20 changes: 12 additions & 8 deletions GUI/ModSettingsPatches.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Harmony;
using HarmonyLib;
using MelonLoader;
using UnityEngine;

namespace ModSettings {
Expand All @@ -10,33 +11,36 @@ internal static class ModSettingsPatches {

[HarmonyPatch(typeof(Panel_OptionsMenu), "InitializeAutosaveMenuItems", new Type[0])]
private static class BuildModSettingsGUIPatch {
private static void Postfix() {
private static void Postfix(Panel_OptionsMenu __instance) {
InterfaceManager.m_Panel_OptionsMenu = __instance;
ObjectPrefabs.Initialize(__instance);

DateTime tStart = DateTime.UtcNow;

try {
Debug.Log("[ModSettings] Building Mod Settings GUI");
MelonLogger.Msg("Building Mod Settings GUI");
ModSettingsMenu.BuildGUI();
} catch (Exception e) {
Debug.LogError("[ModSettings] Exception while building Mod Settings GUI\n" + e.ToString());
MelonLogger.Error("Exception while building Mod Settings GUI\n" + e.ToString());
return;
}
try {
Debug.Log("[ModSettings] Building Custom Mode GUI");
MelonLogger.Msg("Building Custom Mode GUI");
CustomModeMenu.BuildGUI();
} catch (Exception e) {
Debug.LogError("[ModSettings] Exception while building Custom Mode GUI\n" + e.ToString());
MelonLogger.Error("Exception while building Custom Mode GUI\n" + e.ToString());
return;
}

long timeMillis = (long) (DateTime.UtcNow - tStart).TotalMilliseconds;
Debug.Log("[ModSettings] Done! Took " + timeMillis + " ms. Have a nice day!");
MelonLogger.Msg("Done! Took " + timeMillis + " ms. Have a nice day!");
}
}

[HarmonyPatch(typeof(Panel_OptionsMenu), "ConfigureMenu", new Type[0])]
private static class AddModSettingsButton {
private static void Postfix(Panel_OptionsMenu __instance) {
if (!ModSettingsMenu.HasVisibleModSettings(isMainMenu: InterfaceManager.IsMainMenuActive()))
if (!ModSettingsMenu.HasVisibleModSettings(isMainMenu: InterfaceManager.IsMainMenuEnabled()))
return;

BasicMenu basicMenu = __instance.m_BasicMenu;
Expand Down
138 changes: 138 additions & 0 deletions GUI/ObjectPrefabs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using UnityEngine;

namespace ModSettings {
internal static class ObjectPrefabs {
internal static GameObject ComboBoxPrefab { get; private set; }
internal static GameObject CustomComboBoxPrefab { get; private set; }
internal static GameObject DisplayPrefab { get; private set; }
internal static GameObject EmptyPrefab { get; private set; }
internal static GameObject HeaderLabelPrefab { get; private set; }
internal static GameObject KeyEntryPrefab { get; private set; }
internal static GameObject SliderPrefab { get; private set; }
internal static GameObject TextEntryPrefab { get; private set; }
private static bool isInitialized;

internal static void Initialize(Panel_OptionsMenu optionsPanel) {
if (isInitialized)
return;

Transform firstSection = InterfaceManager.LoadPanel<Panel_CustomXPSetup>().m_ScrollPanelOffsetTransform.GetChild(0);

HeaderLabelPrefab = UnityEngine.Object.Instantiate(firstSection.Find("Header").gameObject);
HeaderLabelPrefab.SetActive(false);

ComboBoxPrefab = UnityEngine.Object.Instantiate(InterfaceManager.LoadPanel<Panel_CustomXPSetup>().m_AllowInteriorSpawnPopupList.gameObject);
ComboBoxPrefab.SetActive(false);

CustomComboBoxPrefab = MakeCustomComboBoxPrefab();
CustomComboBoxPrefab.SetActive(false);

DisplayPrefab = MakeDisplayPrefab();
DisplayPrefab.SetActive(false);

EmptyPrefab = MakeEmptyPrefab();
EmptyPrefab.SetActive(false);

KeyEntryPrefab = MakeKeyEntryPrefab(optionsPanel);
KeyEntryPrefab.SetActive(false);

TextEntryPrefab = MakeTextEntryPrefab();
TextEntryPrefab.SetActive(false);

UnityEngine.Object.DestroyImmediate(optionsPanel.m_FieldOfViewSlider.m_SliderObject.GetComponent<GenericSliderSpawner>());
SliderPrefab = UnityEngine.Object.Instantiate(optionsPanel.m_FieldOfViewSlider.gameObject);
SliderPrefab.SetActive(false);
SliderPrefab.transform.Find("Label_FOV").localPosition = new Vector3(-10, 0, -1);

// Fix slider hitbox
BoxCollider collider = SliderPrefab.GetComponentInChildren<BoxCollider>();
collider.center = new Vector3(150, 0);
collider.size = new Vector3(900, 30);

isInitialized = true;
}

private static GameObject MakeCustomComboBoxPrefab() {
GameObject result = GameObject.Instantiate(ComboBoxPrefab);
GameObject.DestroyImmediate(result.GetComponent<ConsoleComboBox>());
return result;
}

private static GameObject MakeDisplayPrefab() {
GameObject result = GameObject.Instantiate(ComboBoxPrefab);

GameObject.DestroyImmediate(result.GetComponent<ConsoleComboBox>());
result.DestroyChild("Button_Decrease");
result.DestroyChild("Button_Increase");

return result;
}

private static GameObject MakeEmptyPrefab() {
GameObject result = GameObject.Instantiate(ComboBoxPrefab);

GameObject.DestroyImmediate(result.GetComponent<ConsoleComboBox>());
result.DestroyChild("Button_Decrease");
result.DestroyChild("Button_Increase");
result.DestroyChild("Label_Value");

return result;
}

private static GameObject MakeKeyEntryPrefab(Panel_OptionsMenu optionsPanel) {
GameObject result = GameObject.Instantiate(ComboBoxPrefab);

Transform rebindingTab = optionsPanel.m_RebindingTab.transform;
GameObject originalButton = rebindingTab?.FindChild("GameObject")?.FindChild("LeftSide")?.FindChild("Button_Rebinding")?.gameObject;
GameObject keybindingButton = GameObject.Instantiate(originalButton);

keybindingButton.transform.position = result.transform.FindChild("Label_Value").position;
keybindingButton.transform.parent = result.transform;
keybindingButton.name = "Keybinding_Button";

GameObject.DestroyImmediate(result.GetComponent<ConsoleComboBox>());
result.DestroyChild("Button_Decrease");
result.DestroyChild("Button_Increase");
result.DestroyChild("Label_Value");

keybindingButton.DestroyChild("Label_Name");

return result;
}

private static GameObject MakeTextEntryPrefab() {
GameObject result = GameObject.Instantiate(ComboBoxPrefab);

GameObject originalTextBox = InterfaceManager.LoadPanel<Panel_Confirmation>().m_GenericMessageGroup?.m_InputField?.gameObject;
GameObject newTextBox = GameObject.Instantiate(originalTextBox);

newTextBox.transform.position = result.transform.FindChild("Label_Value").position;
newTextBox.transform.parent = result.transform;
newTextBox.name = "Text_Box";

TextInputField textInputField = newTextBox.GetComponent<TextInputField>();
textInputField.m_MaxLength = 25;

GameObject.DestroyImmediate(result.GetComponent<ConsoleComboBox>());
result.DestroyChild("Button_Decrease");
result.DestroyChild("Button_Increase");
result.DestroyChild("Label_Value");

newTextBox.DestroyChild("bg");
newTextBox.DestroyChild("glow");

result.AddComponent<UIButton>();

return result;
}

private static GameObject GetChild(this GameObject parent, string childName) {
return parent?.transform?.FindChild(childName)?.gameObject;
}

private static void DestroyChild(this GameObject parent, string childName) {
GameObject child = parent?.transform?.FindChild(childName)?.gameObject;
if (child) GameObject.DestroyImmediate(child);
}
}
}
Loading

0 comments on commit dbe4638

Please sign in to comment.