Decompiled source of AnimateOnTwos v0.0.2

AnimateOnTwos.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using AnimateOnTwos.Patches;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Reptile;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("BRC_HarmonyTemplate")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BRC_HarmonyTemplate")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("662709d2-b924-44ea-8697-a8d56c754d2b")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace AnimateOnTwos
{
	internal class AnimatorPatchState
	{
		internal Animator animator;

		internal float time;

		internal float someoneElseSetThisSpeed = 1f;

		internal float iSetThisSpeed = 1f;
	}
	[BepInPlugin("com.cspotcode.AnimateOnTwos", "AnimateOnTwos", "1.0.0")]
	public class AnimateOnTwosPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.cspotcode.AnimateOnTwos";

		private const string PluginName = "AnimateOnTwos";

		private const string VersionString = "1.0.0";

		internal static int PlayerTargetFps = 24;

		internal static int PedestrianTargetFps = 8;

		public static string PlayerTargetFpsConfigKey = "Player animation fps";

		public static string PedestrianTargetFpsConfigKey = "Pedestrian animation fps";

		public static ConfigEntry<int> PlayerTargetFpsConfig;

		public static ConfigEntry<int> PedestrianTargetFpsConfig;

		private Harmony harmony;

		public static Player player;

		internal static ConditionalWeakTable<Player, AnimatorPatchState> PlayerPatchStates = new ConditionalWeakTable<Player, AnimatorPatchState>();

		internal static ConditionalWeakTable<Pedestrian, AnimatorPatchState> PedestrianPatchStates = new ConditionalWeakTable<Pedestrian, AnimatorPatchState>();

		internal static HashSet<Pedestrian> Pedestrians = new HashSet<Pedestrian>();

		private FileSystemWatcher ConfigFileWatcher;

		private object ConfigFileChangedLock = new object();

		private bool ConfigDirty = false;

		private void Awake()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Expected O, but got Unknown
			harmony = new Harmony("com.cspotcode.AnimateOnTwos");
			PlayerTargetFpsConfig = ((BaseUnityPlugin)this).Config.Bind<int>("General", PlayerTargetFpsConfigKey, PlayerTargetFps, new ConfigDescription("Set to zero to disable effect", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 60), Array.Empty<object>()));
			PedestrianTargetFpsConfig = ((BaseUnityPlugin)this).Config.Bind<int>("General", PedestrianTargetFpsConfigKey, PedestrianTargetFps, new ConfigDescription("Set to zero to disable effect", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 60), Array.Empty<object>()));
			PlayerTargetFpsConfig.SettingChanged += ConfigSettingChanged;
			PedestrianTargetFpsConfig.SettingChanged += ConfigSettingChanged;
			ConfigFileWatcher = new FileSystemWatcher
			{
				Path = Path.GetDirectoryName(((BaseUnityPlugin)this).Config.ConfigFilePath),
				Filter = Path.GetFileName(((BaseUnityPlugin)this).Config.ConfigFilePath)
			};
			ConfigFileWatcher.Changed += ConfigSettingChanged;
			ConfigFileWatcher.EnableRaisingEvents = true;
			ReloadConfig();
			harmony.PatchAll(typeof(PlayerPatch));
			harmony.PatchAll(typeof(PedestrianPatch));
		}

		private void ConfigSettingChanged(object sender, EventArgs e)
		{
			lock (ConfigFileChangedLock)
			{
				ConfigDirty = true;
			}
		}

		private void ReloadConfig()
		{
			lock (ConfigFileChangedLock)
			{
				((BaseUnityPlugin)this).Config.Reload();
				PlayerTargetFps = (int)((ConfigEntryBase)PlayerTargetFpsConfig).BoxedValue;
				PedestrianTargetFps = (int)((ConfigEntryBase)PedestrianTargetFpsConfig).BoxedValue;
				ConfigDirty = false;
			}
		}

		private void Update()
		{
			if (ConfigDirty)
			{
				ReloadConfig();
			}
			PedestrianPatch.Update();
		}
	}
}
namespace AnimateOnTwos.Patches
{
	internal static class PlayerPatch
	{
		[HarmonyPatch(typeof(Player), "Init")]
		[HarmonyPostfix]
		private static void Player_Init_Postfix(Player __instance)
		{
			if (!__instance.isAI)
			{
				AnimateOnTwosPlugin.player = __instance;
			}
			AnimatorPatchState value = new AnimatorPatchState();
			AnimateOnTwosPlugin.PlayerPatchStates.Add(__instance, value);
		}

		[HarmonyPatch(typeof(Player), "UpdatePlayer")]
		[HarmonyPostfix]
		private static void Player_UpdatePlayer_Postfix(Player __instance)
		{
			if (AnimateOnTwosPlugin.PlayerTargetFps != 0)
			{
				AnimateOnTwosPlugin.PlayerPatchStates.TryGetValue(__instance, out var value);
				float num = 1f / (float)AnimateOnTwosPlugin.PlayerTargetFps;
				value.animator = (((Object)(object)value.animator != (Object)null) ? value.animator : ((Component)__instance).GetComponentInChildren<Animator>());
				value.time += Time.deltaTime;
				if (value.animator.speed != 0f && value.animator.speed != value.iSetThisSpeed)
				{
					value.someoneElseSetThisSpeed = value.animator.speed;
				}
				value.animator.speed = (value.iSetThisSpeed = 0f);
				if (value.time > num)
				{
					value.time -= num;
					value.animator.speed = (value.iSetThisSpeed = value.someoneElseSetThisSpeed * num / Time.deltaTime);
				}
			}
		}
	}
	internal static class PedestrianPatch
	{
		[HarmonyPatch(typeof(StreetLife), "Init")]
		[HarmonyPostfix]
		private static void Init_Postfix(StreetLife __instance)
		{
			Pedestrian val = (Pedestrian)(object)((__instance is Pedestrian) ? __instance : null);
			if (val != null)
			{
				AnimatorPatchState value = new AnimatorPatchState();
				try
				{
					AnimateOnTwosPlugin.PedestrianPatchStates.Add(val, value);
				}
				catch (ArgumentException)
				{
				}
				AnimateOnTwosPlugin.Pedestrians.Add(val);
			}
		}

		[HarmonyPatch(typeof(StreetLife), "OnDestroy")]
		[HarmonyPrefix]
		private static void OnDestroy_Prefix(StreetLife __instance)
		{
			Pedestrian val = (Pedestrian)(object)((__instance is Pedestrian) ? __instance : null);
			if (val != null)
			{
				AnimateOnTwosPlugin.Pedestrians.Remove(val);
			}
		}

		internal static void Update()
		{
			if (AnimateOnTwosPlugin.PedestrianTargetFps == 0)
			{
				return;
			}
			float num = 1f / (float)AnimateOnTwosPlugin.PedestrianTargetFps;
			foreach (Pedestrian pedestrian in AnimateOnTwosPlugin.Pedestrians)
			{
				AnimateOnTwosPlugin.PedestrianPatchStates.TryGetValue(pedestrian, out var value);
				value.animator = (((Object)(object)value.animator != (Object)null) ? value.animator : ((Component)pedestrian).GetComponentInChildren<Animator>());
				value.time += Time.deltaTime;
				if (value.animator.speed != 0f && value.animator.speed != value.iSetThisSpeed)
				{
					value.someoneElseSetThisSpeed = value.animator.speed;
				}
				value.animator.speed = (value.iSetThisSpeed = 0f);
				if (value.time > num)
				{
					value.time -= num;
					value.animator.speed = (value.iSetThisSpeed = value.someoneElseSetThisSpeed * num / Time.deltaTime);
				}
			}
		}
	}
}