Decompiled source of CleanMainMenu v1.2.0

CleanMainMenu/CleanMainMenu.dll

Decompiled a month ago
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("crevitka.cleanmenu", "Clean Main Menu", "1.2.0")]
public class CleanMainMenuPlugin : BaseUnityPlugin
{
	private ConfigEntry<bool> _hideModdedText;

	private ConfigEntry<bool> _hideShowLogButton;

	private ConfigEntry<bool> _hideTopRightMerch;

	private ConfigEntry<bool> _hideChangelog;

	private ConfigEntry<bool> _hideChangelogButton;

	private ConfigEntry<bool> _hideEulaButton;

	private ConfigEntry<bool> _hideVersionNumber;

	private ConfigEntry<float> _initialDelaySeconds;

	private ConfigEntry<int> _attempts;

	private ConfigEntry<float> _intervalSeconds;

	private ConfigEntry<bool> _reapplyOnMenuScenes;

	private ConfigEntry<bool> _watchMenuContinuously;

	private ConfigEntry<float> _watchIntervalSeconds;

	private Coroutine _hideRoutine;

	private Coroutine _watchRoutine;

	private bool _changelogClosedOnce;

	private static readonly BindingFlags FieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

	private static readonly FieldInfo ChangeLogField = typeof(FejdStartup).GetField("m_changeLog", FieldFlags);

	private static readonly FieldInfo ChangeLogNoticeField = typeof(FejdStartup).GetField("m_changeLogNotice", FieldFlags);

	private static readonly FieldInfo ChangeLogButtonField = typeof(FejdStartup).GetField("m_showChangelogButton", FieldFlags);

	private static readonly FieldInfo EulaButtonField = typeof(FejdStartup).GetField("m_showEulaButton", FieldFlags);

	private void Awake()
	{
		_hideModdedText = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Hide modded_text", true, "Hide the 'modded' warning text in main menu (object name: modded_text).");
		_hideShowLogButton = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Hide showlog button", true, "Hide 'showlog' button in main menu (object name: showlog).");
		_hideTopRightMerch = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Hide TopRight (Merch)", true, "Hide top-right panel (usually merch store / icons) (object name: TopRight).");
		_hideChangelog = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Hide Changelog", true, "Hide the changelog panel and its 'new changes' notice (Valheim 1.0+: FejdStartup.m_changeLog / m_changeLogNotice; older: Canvas Changelog).");
		_hideChangelogButton = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Hide Changelog button", true, "Hide the 'Show changelog' button added in Valheim 1.0 (FejdStartup.m_showChangelogButton). If you keep the button, the changelog is only hidden once on menu load so you can still open it manually.");
		_hideEulaButton = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Hide EULA button", true, "Hide the 'EULA' (user agreement) button in the main menu (FejdStartup.m_showEulaButton). The game still shows the agreement window automatically when it is required.");
		_hideVersionNumber = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Hide Version Number", true, "Hide Version Number (object name: version_text).");
		_initialDelaySeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Timing", "Initial delay (seconds)", 1.5f, "Wait before first attempt (menu UI may spawn a bit later).");
		_attempts = ((BaseUnityPlugin)this).Config.Bind<int>("Timing", "Attempts", 10, "How many times to try hiding (useful when UI spawns late or gets recreated).");
		_intervalSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Timing", "Interval between attempts (seconds)", 1f, "Delay between attempts.");
		_reapplyOnMenuScenes = ((BaseUnityPlugin)this).Config.Bind<bool>("Advanced", "Reapply on menu scene load", true, "Re-apply cleanup whenever a menu scene loads (fixes UI coming back after exiting a world).");
		_watchMenuContinuously = ((BaseUnityPlugin)this).Config.Bind<bool>("Advanced", "Watch menu continuously", true, "While in the menu, keep re-applying cleanup periodically (handles UI being recreated later).");
		_watchIntervalSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Advanced", "Watch interval (seconds)", 1f, "How often to re-apply cleanup while watching the menu.");
		if (_reapplyOnMenuScenes.Value)
		{
			SceneManager.sceneLoaded += OnSceneLoaded;
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Clean Main Menu loaded (config initialized).");
	}

	private void OnDestroy()
	{
		SceneManager.sceneLoaded -= OnSceneLoaded;
	}

	private void Start()
	{
		StartApplyForCurrentScene();
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		StartApplyForScene(scene);
	}

	private void StartApplyForCurrentScene()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		StartApplyForScene(SceneManager.GetActiveScene());
	}

	private void StartApplyForScene(Scene scene)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		if (!IsMenuScene(scene))
		{
			StopWatchdog();
			return;
		}
		RestartHideRoutine();
		if (_watchMenuContinuously.Value)
		{
			StartWatchdog();
		}
		else
		{
			StopWatchdog();
		}
	}

	private bool IsMenuScene(Scene scene)
	{
		if (!string.IsNullOrEmpty(((Scene)(ref scene)).name))
		{
			string text = ((Scene)(ref scene)).name.ToLower();
			if (text == "start" || text.Contains("start"))
			{
				return true;
			}
		}
		return (Object)(object)GameObject.Find("StartGui") != (Object)null || (Object)(object)GameObject.Find("GUI") != (Object)null;
	}

	private void RestartHideRoutine()
	{
		if (_hideRoutine != null)
		{
			((MonoBehaviour)this).StopCoroutine(_hideRoutine);
			_hideRoutine = null;
		}
		_changelogClosedOnce = false;
		_hideRoutine = ((MonoBehaviour)this).StartCoroutine(HideRoutine());
	}

	private void StartWatchdog()
	{
		if (_watchRoutine == null)
		{
			_watchRoutine = ((MonoBehaviour)this).StartCoroutine(MenuWatchdog());
		}
	}

	private void StopWatchdog()
	{
		if (_watchRoutine != null)
		{
			((MonoBehaviour)this).StopCoroutine(_watchRoutine);
			_watchRoutine = null;
		}
	}

	private IEnumerator HideRoutine()
	{
		float initialDelay = Mathf.Max(0f, _initialDelaySeconds.Value);
		if (initialDelay > 0f)
		{
			yield return (object)new WaitForSeconds(initialDelay);
		}
		int tries = Mathf.Clamp(_attempts.Value, 1, 60);
		float wait = Mathf.Clamp(_intervalSeconds.Value, 0.1f, 10f);
		for (int i = 0; i < tries; i++)
		{
			ApplyOnce(initialPass: true);
			yield return (object)new WaitForSeconds(wait);
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Clean Main Menu applied.");
	}

	private IEnumerator MenuWatchdog()
	{
		float wait = Mathf.Clamp(_watchIntervalSeconds.Value, 0.25f, 10f);
		while (IsMenuScene(SceneManager.GetActiveScene()))
		{
			ApplyOnce(initialPass: false);
			yield return (object)new WaitForSeconds(wait);
		}
		StopWatchdog();
	}

	private void ApplyOnce(bool initialPass)
	{
		if (_hideModdedText.Value)
		{
			HideByName("modded_text");
		}
		if (_hideShowLogButton.Value)
		{
			HideByName("showlog");
		}
		if (_hideTopRightMerch.Value)
		{
			HideByName("TopRight");
		}
		if (_hideChangelog.Value)
		{
			HideByName("Canvas Changelog");
		}
		HideChangelog(initialPass);
		if (_hideEulaButton.Value)
		{
			HideStartupField(EulaButtonField, "EULA button");
		}
		if (_hideVersionNumber.Value)
		{
			HideByName("version_text");
		}
	}

	private void HideStartupField(FieldInfo field, string label)
	{
		FejdStartup instance = FejdStartup.instance;
		if ((Object)(object)instance != (Object)null)
		{
			HideField(instance, field, label);
		}
	}

	private void HideChangelog(bool initialPass)
	{
		FejdStartup instance = FejdStartup.instance;
		if ((Object)(object)instance == (Object)null)
		{
			return;
		}
		bool value = _hideChangelogButton.Value;
		if (value)
		{
			HideField(instance, ChangeLogButtonField, "Changelog button");
		}
		if (!_hideChangelog.Value)
		{
			return;
		}
		HideField(instance, ChangeLogNoticeField, "Changelog notice");
		if (value || (initialPass && !_changelogClosedOnce))
		{
			if ((((object)ChangeLogField != null) ? ChangeLogField.GetValue(instance) : null) is GameObject)
			{
				_changelogClosedOnce = true;
			}
			HideField(instance, ChangeLogField, "Changelog panel");
		}
	}

	private void HideField(FejdStartup startup, FieldInfo field, string label)
	{
		if (!(field == null))
		{
			object? value = field.GetValue(startup);
			GameObject val = (GameObject)((value is GameObject) ? value : null);
			if (!((Object)(object)val == (Object)null) && val.activeSelf)
			{
				val.SetActive(false);
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Hidden: {label} ({((Object)val).name})");
			}
		}
	}

	private void HideByName(string name)
	{
		GameObject val = GameObject.Find(name);
		if (!((Object)(object)val == (Object)null) && val.activeSelf)
		{
			val.SetActive(false);
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Hidden: {name}");
		}
	}
}