Decompiled source of ChronoFlow v1.0.0

plugins/com.github.Kirshoo.ChronoFlow.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.Kirshoo.ChronoFlow")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("com.github.Kirshoo.ChronoFlow")]
[assembly: AssemblyTitle("ChronoFlow")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace ChronoFlow
{
	internal class DayNightManagerPatch
	{
		[HarmonyPatch(typeof(DayNightManager), "Update")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> TimeControlInjection(IEnumerable<CodeInstruction> instructions, ILGenerator il)
		{
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			for (int i = 14; i < list.Count; i++)
			{
				if (list[i].opcode == OpCodes.Stfld && (FieldInfo)list[i].operand == AccessTools.Field(typeof(DayNightManager), "timeOfDay") && list[i - 1].opcode == OpCodes.Add && list[i - 2].opcode == OpCodes.Mul)
				{
					Label label = il.DefineLabel();
					list[i + 1].labels.Add(label);
					list.InsertRange(i - 14, CreateIfStopCheck(label));
					list.InsertRange(i - 1, CreateTimeSpeedMultiplier());
					break;
				}
			}
			for (int j = 0; j < list.Count; j++)
			{
				Plugin.Log.LogDebug((object)$"{j}: {list[j]}");
			}
			return list;
		}

		private static IEnumerable<CodeInstruction> CreateIfStopCheck(Label jumpTo)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Expected O, but got Unknown
			return (IEnumerable<CodeInstruction>)(object)new CodeInstruction[2]
			{
				new CodeInstruction(OpCodes.Ldsfld, (object)typeof(Plugin).GetField("IsTimeStopped", BindingFlags.Static | BindingFlags.Public)),
				new CodeInstruction(OpCodes.Brtrue, (object)jumpTo)
			};
		}

		private static IEnumerable<CodeInstruction> CreateTimeSpeedMultiplier()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			return (IEnumerable<CodeInstruction>)(object)new CodeInstruction[2]
			{
				new CodeInstruction(OpCodes.Ldsfld, (object)typeof(Plugin).GetField("TimeSpeed", BindingFlags.Static | BindingFlags.Public)),
				new CodeInstruction(OpCodes.Mul, (object)null)
			};
		}
	}
	[BepInPlugin("com.github.Kirshoo.ChronoFlow", "ChronoFlow", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static bool IsTimeStopped = false;

		public static float TimeSpeed = 1f;

		private float min_increment = 0.05f;

		private float max_increment = 5f;

		private float speedIncrement = 0.5f;

		private float changeRate = 0.2f;

		private int increment_times;

		private int decrement_times;

		private ConfigEntry<KeyCode> StopToggleKey;

		private ConfigEntry<KeyCode> IncreaseSpeedKey;

		private ConfigEntry<KeyCode> DecreaseSpeedKey;

		private ConfigEntry<KeyCode> ResetKey;

		public const string Id = "com.github.Kirshoo.ChronoFlow";

		internal static ManualLogSource Log { get; private set; } = null;


		public static string Name => "ChronoFlow";

		public static string Version => "1.0.0";

		private void Awake()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			BindConfig();
			new Harmony("com.github.Kirshoo.ChronoFlow").PatchAll(typeof(DayNightManagerPatch));
			Log.LogInfo((object)("Plugin " + Name + " is loaded!"));
		}

		private void BindConfig()
		{
			StopToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "TimeToggle", (KeyCode)112, "When pressed, will toggle the day/night cycle.\r\n(At the start of the run, time is always flowing)");
			ResetKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "ResetTime", (KeyCode)48, "Will reset any temporal changes done to the day/night cycle.");
			IncreaseSpeedKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "SpeedupTime", (KeyCode)61, "When pressed, will increase the speed at which day/night cycle changes.");
			DecreaseSpeedKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "SlowdownTime", (KeyCode)45, "When pressed, will decrease the speed at which day/night cycle changes.");
		}

		private void Update()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			if (Input.GetKeyDown(StopToggleKey.Value))
			{
				IsTimeStopped = !IsTimeStopped;
			}
			if (Input.GetKeyDown(DecreaseSpeedKey.Value))
			{
				increment_times = 0;
				decrement_times++;
				TimeSpeed -= max_increment - speedIncrement;
				speedIncrement += smoothChangeTowards(min_increment, speedIncrement, changeRate, 0.2f * (float)decrement_times);
			}
			if (Input.GetKeyDown(IncreaseSpeedKey.Value))
			{
				decrement_times = 0;
				increment_times++;
				TimeSpeed += speedIncrement;
				speedIncrement += smoothChangeTowards(max_increment, speedIncrement, changeRate, 0.2f * (float)increment_times);
			}
			if (Input.GetKeyDown(ResetKey.Value))
			{
				TimeSpeed = 1f;
				speedIncrement = max_increment / 10f;
				decrement_times = 0;
				increment_times = 0;
			}
		}

		private static float smoothChangeTowards(float target, float current, float rate, float step)
		{
			return (target - current) * (1f - Mathf.Exp((0f - rate) * step));
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}