Decompiled source of TimeController v1.0.2

TimeController.dll

Decompiled 3 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Shapez2Mods.TimeController.UI;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TimeController")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+1c2dfab3ee149e55c238a05102930f1109b4b015")]
[assembly: AssemblyProduct("TimeController")]
[assembly: AssemblyTitle("TimeController")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace Shapez2Mods.TimeController
{
	[BepInPlugin("urakka.shapez2mods.timecontroller", "Time Controller", "1.0.0")]
	public class TimeController : BaseUnityPlugin
	{
		internal static TimeController Instance;

		internal static GameCore CurrentGameCore;

		internal static SimulationSpeedManager SimManager;

		internal static TimeControlUI UI;

		private void Awake()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			Instance = this;
			Harmony val = new Harmony("urakka.shapez2mods.timecontroller");
			val.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Time Controller loaded.");
		}

		public static void TryInitialize(GameCore GC)
		{
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)"[TimeController] TryInitialize");
			CurrentGameCore = GC;
			if ((Object)(object)CurrentGameCore == (Object)null)
			{
				Debug.LogWarning((object)"[TimeController] GameCore not found yet.");
				return;
			}
			SimManager = ((StaticGameCoreAccessor)CurrentGameCore).SimulationSpeed;
			if (SimManager == null)
			{
				Debug.LogWarning((object)"[TimeController] SimulationSpeedManager not found.");
				return;
			}
			Debug.Log((object)"[TimeController] SimulationSpeedManager found.");
			CreateUI();
			if ((Object)(object)UI == (Object)null)
			{
				ApplyInitialSpeed();
			}
		}

		private static void ApplyInitialSpeed()
		{
			if (SimManager != null)
			{
				SetSpeed(1f);
			}
		}

		public static void SetSpeed(float speed)
		{
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)"Speed trying to be set");
			if (SimManager == null)
			{
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)"Simulation manager not found!");
				return;
			}
			if (speed == 0f)
			{
				SimManager.IsPaused = true;
			}
			SimManager.Speed = speed;
			if (SimManager.Speed != speed)
			{
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)$" Failed to set speed to {speed}x");
			}
			else
			{
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)$"[TimeController] Speed set to {speed}x");
			}
		}

		private static void CreateUI()
		{
			if ((Object)(object)GameObject.Find("BepInEx_Manager") != (Object)null)
			{
				TimeControlUI timeControlUI = GameObject.Find("BepInEx_Manager").AddComponent<TimeControlUI>();
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)"[TimeController] TimeControlUI created.");
			}
		}
	}
}
namespace Shapez2Mods.TimeController.UI
{
	public class TimeControlUI : MonoBehaviour
	{
		private readonly float[] speeds = new float[5] { 0f, 1f, 5f, 10f, 25f };

		private readonly string[] labels = new string[5] { "II", ">", ">>", ">>>", ">|" };

		private int selectedIndex = 1;

		private ManualLogSource logger;

		public void Awake()
		{
			logger = Logger.CreateLogSource("TimeControlUI");
		}

		private void OnGUI()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			if (TimeController.SimManager == null)
			{
				return;
			}
			GUI.BeginGroup(new Rect((float)(Screen.width - 190), 60f, 180f, 40f));
			for (int i = 0; i < speeds.Length; i++)
			{
				if (GUI.Button(new Rect((float)(i * 35), 0f, 35f, 30f), labels[i]))
				{
					selectedIndex = i;
					TimeController.SetSpeed(speeds[i]);
				}
			}
			GUI.EndGroup();
			if ((Object)(object)TimeController.CurrentGameCore == (Object)null)
			{
				logger.Dispose();
				Destroy();
			}
		}

		private void ApplySpeed(float speed)
		{
		}

		private void Destroy()
		{
			Object.Destroy((Object)(object)this);
		}
	}
}
namespace Shapez2Mods.TimeController.Patches
{
	[HarmonyPatch]
	public static class GameStartPatches
	{
		private static bool menuWasActive = true;

		[HarmonyPostfix]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static void ContinueExisting_Postfix(GameStartOptionsContinueExisting __instance)
		{
			CheckMenuMode(__instance.MenuMode);
		}

		[HarmonyPostfix]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static void StartNew_Postfix(GameStartOptionsStartNew __instance)
		{
			CheckMenuMode(__instance.MenuMode);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameCore), "Init")]
		private static void GameCore_Init_Postfix(GameCore __instance)
		{
			if (menuWasActive)
			{
				Debug.Log((object)"[TimeController] Skipping GameCore.Init (still in menu).");
				return;
			}
			Debug.Log((object)"[TimeController] GameCore.Init() detected — setting up Time Controller...");
			SimulationSpeedManager simulationSpeed = ((StaticGameCoreAccessor)__instance).SimulationSpeed;
			if (simulationSpeed == null)
			{
				Debug.LogWarning((object)"[TimeController] SimulationSpeedManager not found in GameCore!");
			}
			else
			{
				TimeController.TryInitialize(__instance);
			}
		}

		private static void CheckMenuMode(bool menuMode)
		{
			if (menuMode && !menuWasActive)
			{
				menuWasActive = true;
			}
			else if (!menuMode && menuWasActive)
			{
				menuWasActive = false;
				Debug.Log((object)"[TimeController] Menu closed, reinitializing GameCore...");
			}
		}
	}
}