Decompiled source of SpeedrunTypingSound v1.1.0

SpeedrunTypingSound.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ModOptions;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("SpeedrunTypingSound")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+d0d8d5557422b18ea63dce23a17df65d176f1f8d")]
[assembly: AssemblyProduct("SpeedrunTypingSound")]
[assembly: AssemblyTitle("SpeedrunTypingSound")]
[assembly: AssemblyVersion("1.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 SpeedrunTypingSound
{
	internal static class Events
	{
		public const uint TERMINAL_INPUT_1ST_PERSON = 971951075u;

		public const uint TERMINAL_INPUT_3RD_PERSON = 637303252u;

		public const int VolumeStepCount = 30;

		public const float VolumeStepDb = 2f;

		public const int VolumeTenthsPerStep = 20;

		public const int DistanceStepM = 5;

		public const int MinConfigDistanceM = 0;

		public const int MinBankDistanceM = 5;

		public const int MaxDistanceM = 50;

		public const uint SILENT = 0u;

		public static uint Hash(string name)
		{
			uint num = 2166136261u;
			string text = name.ToLowerInvariant();
			foreach (char c in text)
			{
				num *= 16777619;
				num ^= (byte)c;
			}
			return num;
		}

		public static uint TypingEvent(int stepIndex, bool positional, int distanceM = 0)
		{
			return Hash(positional ? $"SPEEDRUN_TYPING3D_D{distanceM}_DB{stepIndex * 20}" : $"SPEEDRUN_TYPING_DB{stepIndex * 20}");
		}

		public static int SnapDistance(int metres)
		{
			return Math.Clamp((int)Math.Round((double)metres / 5.0) * 5, 5, 50);
		}

		public static int VolumeStep(float volume)
		{
			if (!(volume > 0f))
			{
				return -1;
			}
			double num = Math.Min(volume, 1f);
			return Math.Clamp((int)Math.Round((1.0 - num) * 30.0), 0, 30);
		}

		public static int StepDb(int stepIndex)
		{
			return -stepIndex * 20 / 10;
		}

		public static int VolumePercent(int stepIndex)
		{
			return (int)Math.Round((double)(30 - stepIndex) * 100.0 / 30.0);
		}
	}
	internal static class Redirect
	{
		private static uint _target2D;

		private static uint _target3D;

		public static void Recalculate()
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Expected O, but got Unknown
			int num = Events.VolumeStep(Plugin.Volume.Value);
			int num2 = Events.VolumeStep(Plugin.TeammateVolume.Value);
			int value = Plugin.HearingDistance.Value;
			int num3 = ((value > 0) ? Events.SnapDistance(value) : 0);
			_target2D = ((num >= 0) ? Events.TypingEvent(num, positional: false) : 0u);
			_target3D = ((num2 >= 0 && value > 0) ? Events.TypingEvent(num2, positional: true, num3) : 0u);
			ManualLogSource logger = Plugin.Logger;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(43, 5, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Volume: own ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(Describe(num));
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", teammates ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(Describe(num2));
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>((value <= 0) ? "with range 0 (muted)" : $"within {num3} m");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" ");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("-> events 2D=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<uint>(_target2D);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" 3D=");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<uint>(_target3D);
			}
			logger.LogInfo(val);
		}

		private static string Describe(int step)
		{
			if (step >= 0)
			{
				return $"{Events.VolumePercent(step)}% ({Events.StepDb(step)} dB)";
			}
			return "muted";
		}

		public static void Apply(ref uint eventID)
		{
			if (eventID == 971951075 && Plugin.Enabled.Value)
			{
				eventID = _target2D;
			}
			else if (eventID == 637303252 && Plugin.ThirdPerson.Value)
			{
				eventID = _target3D;
			}
		}
	}
	[HarmonyPatch(typeof(CellSoundPlayer), "Post", new Type[]
	{
		typeof(uint),
		typeof(bool)
	})]
	internal static class Patch_Post_Global
	{
		[HarmonyPrefix]
		private static void Prefix(ref uint eventID)
		{
			Redirect.Apply(ref eventID);
		}
	}
	[HarmonyPatch(typeof(CellSoundPlayer), "Post", new Type[]
	{
		typeof(uint),
		typeof(Vector3)
	})]
	internal static class Patch_Post_Positional
	{
		[HarmonyPrefix]
		private static void Prefix(ref uint eventID)
		{
			Redirect.Apply(ref eventID);
		}
	}
	[BepInPlugin("Oxylight.SpeedrunTypingSound", "SpeedrunTypingSound", "1.1.0")]
	[BepInDependency("Oxylight.ModOptions", ">=1.0.0")]
	[BepInProcess("GTFO.exe")]
	internal sealed class Plugin : BasePlugin
	{
		public const string GUID = "Oxylight.SpeedrunTypingSound";

		public const string NAME = "SpeedrunTypingSound";

		public const string VERSION = "1.1.0";

		internal static ManualLogSource Logger;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> ThirdPerson;

		internal static ConfigEntry<float> Volume;

		internal static ConfigEntry<float> TeammateVolume;

		internal static ConfigEntry<int> HearingDistance;

		private Harmony? _harmony;

		public override void Load()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Expected O, but got Unknown
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Expected O, but got Unknown
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: Expected O, but got Unknown
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cc: Expected O, but got Unknown
			Logger = ((BasePlugin)this).Log;
			Enabled = ((BasePlugin)this).Config.Bind<bool>("General", "Enabled", true, "Replace the terminal typing sound when you type.");
			Volume = ((BasePlugin)this).Config.Bind<float>("General", "Volume", 0.5f, new ConfigDescription("How loud your own typing is. 0 mutes it.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			ThirdPerson = ((BasePlugin)this).Config.Bind<bool>("General", "ReplaceThirdPerson", true, "Replace the terminal typing sound when your teammates type.");
			TeammateVolume = ((BasePlugin)this).Config.Bind<float>("General", "TeammateVolume", Volume.Value, new ConfigDescription("How loud your teammates' typing is. 0 mutes it.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			HearingDistance = ((BasePlugin)this).Config.Bind<int>("General", "HearingDistanceMeters", 15, new ConfigDescription("How far away your teammates' typing can be heard, in metres. 0 mutes it.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 50), Array.Empty<object>()));
			Redirect.Recalculate();
			Volume.SettingChanged += delegate
			{
				Redirect.Recalculate();
			};
			TeammateVolume.SettingChanged += delegate
			{
				Redirect.Recalculate();
			};
			HearingDistance.SettingChanged += delegate
			{
				Redirect.Recalculate();
			};
			SettingsMenu.Register();
			_harmony = new Harmony("Oxylight.SpeedrunTypingSound");
			_harmony.PatchAll(typeof(Patch_Post_Global));
			_harmony.PatchAll(typeof(Patch_Post_Positional));
			ManualLogSource logger = Logger;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(10, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("SpeedrunTypingSound");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" v");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.1.0");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" loaded.");
			}
			logger.LogInfo(val);
		}

		public override bool Unload()
		{
			Harmony? harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			return ((BasePlugin)this).Unload();
		}
	}
	internal static class SettingsMenu
	{
		public static void Register()
		{
			ModPage obj = ModOptionsApi.Register("Oxylight.SpeedrunTypingSound", "SpeedrunTypingSound", "Replaces the terminal typing sound with frantic keyboard mashing.");
			obj.HotReloadSupported(true);
			obj.ConfigSectionsInMenuOrder();
			obj.Author("Oxylight");
			((SettingsPage)obj).AddToggle(Plugin.Enabled, "Replace typing sound", (string)null);
			AddVolume((SettingsPage)(object)obj, Plugin.Volume, "My typing volume");
			((SettingsPage)obj).AddToggle(Plugin.ThirdPerson, "Replace teammates' typing", (string)null);
			AddVolume((SettingsPage)(object)obj, Plugin.TeammateVolume, "Teammates' typing volume");
			((SettingsPage)obj).AddSlider(Plugin.HearingDistance, "Teammates' typing range", (SliderDisplay)1, (string)null).Step(5f).ValueText((Func<float, string>)((float v) => (!(v <= 0f)) ? $"{Events.SnapDistance((int)v)} m" : "Muted"));
		}

		private static void AddVolume(SettingsPage page, ConfigEntry<float> entry, string label)
		{
			page.AddSlider(entry, label, (SliderDisplay)0, (string)null).Step(1f / 30f).ValueText((Func<float, string>)delegate(float v)
			{
				int num = Events.VolumeStep(v);
				return (num >= 0) ? $"{Events.VolumePercent(num)}%" : "Muted";
			});
		}
	}
	internal static class BuildInfo
	{
		public const string Version = "1.1.0";
	}
}