Decompiled source of CustomGameSpeed v1.0.1

CustomGameSpeed.dll

Decompiled 3 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Eremite.Services;
using Eremite.View.HUD;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("CustomGameSpeed")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CustomGameSpeed")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3d011369-a82e-4ed5-9f25-7ddf74b4e0db")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CustomGameSpeed
{
	[BepInPlugin("com.madmer.CustomGameSpeed", "CustomGameSpeed", "1.0.0")]
	public class CustomGameSpeedPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.madmer.CustomGameSpeed";

		private const string PluginName = "CustomGameSpeed";

		private const string VersionString = "1.0.0";

		private static readonly Harmony Harmony = new Harmony("com.madmer.CustomGameSpeed");

		public static ConfigEntry<float> Speed3;

		public static ConfigEntry<float> Speed4;

		public static ConfigEntry<float> Speed5;

		public static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Speed3 = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Speed 3", 3f, "Custom time scale for slot 3");
			Speed4 = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Speed 4", 5f, "Custom time scale for slot 4");
			Speed5 = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Speed 5", 10f, "Custom time scale for slot 5");
			Harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[CustomGameSpeed] CustomGameSpeed 1.0.0 loaded.");
		}
	}
	[HarmonyPatch(typeof(TimeScalePanel), "Start")]
	public static class TimeScalePanelPatch
	{
		[HarmonyPatch(typeof(TimeScaleService), "Change")]
		public static class PatchTimeScaleChange
		{
			private static void Postfix(float scale)
			{
				float num = scale;
				if (Mathf.Approximately(scale, 1.5f))
				{
					num = CustomGameSpeedPlugin.Speed3.Value;
				}
				else if (Mathf.Approximately(scale, 2f))
				{
					num = CustomGameSpeedPlugin.Speed4.Value;
				}
				else if (Mathf.Approximately(scale, 3f))
				{
					num = CustomGameSpeedPlugin.Speed5.Value;
				}
				if (!Mathf.Approximately(Time.timeScale, num))
				{
					Time.timeScale = num;
					CustomGameSpeedPlugin.Log.LogInfo((object)$"[CustomGameSpeed] Forced time scale override: {num} (was {scale})");
				}
			}
		}

		[CompilerGenerated]
		private sealed class <WaitAndPatchSlots>d__1 : IEnumerator<object>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private object <>2__current;

			public TimeScalePanel panel;

			private TimeScaleSlot[] <slots>5__2;

			private float <timeout>5__3;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <WaitAndPatchSlots>d__1(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<slots>5__2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<slots>5__2 = null;
					<timeout>5__3 = 5f;
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				if (<timeout>5__3 > 0f)
				{
					<slots>5__2 = ((Component)panel).GetComponentsInChildren<TimeScaleSlot>(true);
					if (<slots>5__2 == null || <slots>5__2.Length < 5)
					{
						<timeout>5__3 -= Time.deltaTime;
						<>2__current = null;
						<>1__state = 1;
						return true;
					}
				}
				if (<slots>5__2 == null || <slots>5__2.Length < 5)
				{
					CustomGameSpeedPlugin.Log.LogWarning((object)"[CustomGameSpeed] TimeScaleSlots not found after waiting.");
					return false;
				}
				AddSpeedOverride(<slots>5__2[2], CustomGameSpeedPlugin.Speed3.Value);
				UpdateSlotText(<slots>5__2[2], $"x{CustomGameSpeedPlugin.Speed3.Value}");
				AddSpeedOverride(<slots>5__2[3], CustomGameSpeedPlugin.Speed4.Value);
				UpdateSlotText(<slots>5__2[3], $"x{CustomGameSpeedPlugin.Speed4.Value}");
				AddSpeedOverride(<slots>5__2[4], CustomGameSpeedPlugin.Speed5.Value);
				UpdateSlotText(<slots>5__2[4], $"x{CustomGameSpeedPlugin.Speed5.Value}");
				CustomGameSpeedPlugin.Log.LogInfo((object)"[CustomGameSpeed] Custom speed overrides applied to slots 3–5 (delayed)");
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private static void Postfix(TimeScalePanel __instance)
		{
			((MonoBehaviour)__instance).StartCoroutine(WaitAndPatchSlots(__instance));
		}

		[IteratorStateMachine(typeof(<WaitAndPatchSlots>d__1))]
		private static IEnumerator WaitAndPatchSlots(TimeScalePanel panel)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <WaitAndPatchSlots>d__1(0)
			{
				panel = panel
			};
		}

		private static void AddSpeedOverride(TimeScaleSlot slot, float newSpeed)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			Button component = ((Component)slot).GetComponent<Button>();
			if ((Object)(object)component == (Object)null)
			{
				CustomGameSpeedPlugin.Log.LogWarning((object)"[CustomGameSpeed] No Button found on TimeScaleSlot.");
				return;
			}
			CustomGameSpeedPlugin.Log.LogInfo((object)$"[CustomGameSpeed] Hooked onClick for {((Object)slot).name}, target speed: {newSpeed}");
			((UnityEvent)component.onClick).AddListener((UnityAction)delegate
			{
				Time.timeScale = newSpeed;
				CustomGameSpeedPlugin.Log.LogInfo((object)$"[CustomGameSpeed] Applied custom time scale: {newSpeed}");
			});
		}

		private static void UpdateSlotText(TimeScaleSlot slot, string text)
		{
			Transform val = ((Component)slot).transform.Find("Desc");
			if ((Object)(object)val == (Object)null)
			{
				CustomGameSpeedPlugin.Log.LogWarning((object)"[CustomGameSpeed] Desc child not found in slot.");
				return;
			}
			TextMeshProUGUI val2 = ((Component)val).GetComponent<TextMeshProUGUI>();
			if ((Object)(object)val2 == (Object)null)
			{
				val2 = ((Component)val).GetComponentInChildren<TextMeshProUGUI>();
			}
			if ((Object)(object)val2 != (Object)null)
			{
				((TMP_Text)val2).text = text;
				CustomGameSpeedPlugin.Log.LogInfo((object)("[CustomGameSpeed] Updated slot text to '" + text + "'"));
			}
			else
			{
				CustomGameSpeedPlugin.Log.LogWarning((object)"[CustomGameSpeed] TextMeshProUGUI not found in Desc.");
			}
		}
	}
}
namespace CustomGameSpeed.MonoBehaviours
{
	internal class CustomGameSpeedComponent : MonoBehaviour
	{
		public void Awake()
		{
		}

		public void Start()
		{
		}

		public void Update()
		{
		}

		public void LateUpdate()
		{
		}
	}
}