Decompiled source of BetterProximityChat v1.0.0

BepInEx/plugins/DarkwaterBetterProximityChat.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Fusion;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Voice.Fusion;
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("DarkwaterBetterProximityChat")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+48c4181de78006afed820bb27a89bac0de742f6d")]
[assembly: AssemblyProduct("Improve proximity based voice chat for Darkwater")]
[assembly: AssemblyTitle("DarkwaterBetterProximityChat")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DarkwaterBetterProximityChat
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "DarkwaterBetterProximityChat";

		public const string PLUGIN_NAME = "Improve proximity based voice chat for Darkwater";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace BetterProximityChat
{
	[BepInPlugin("us.killicow.darkwater.betterproximitychat", "Darkwater Better Proximity Chat", "1.0.0")]
	[BepInProcess("Darkwater.exe")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<float> MinDistance;

		internal static ConfigEntry<float> MaxDistance;

		internal static ConfigEntry<float> MouseMinDistance;

		internal static ConfigEntry<float> MouseMaxDistance;

		internal static ConfigEntry<float> VerticalScale;

		internal static ConfigEntry<float> RolloffFactor;

		internal static readonly AnimationCurve FlatRolloff = AnimationCurve.Constant(0f, 1f, 1f);

		private void Awake()
		{
			MinDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Proximity", "MinDistance", 3f, "Distance (units) at which voice is at full volume.");
			MaxDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Proximity", "MaxDistance", 20f, "Distance (units) at which voice is completely silent.");
			MouseMinDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Proximity", "MouseMinDistance", 1f, "Distance (units) at which voice is at full volume in mouse mode.");
			MouseMaxDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Proximity", "MouseMaxDistance", 8f, "Distance (units) at which voice is completely silent in mouse mode.");
			VerticalScale = ((BaseUnityPlugin)this).Config.Bind<float>("Proximity", "VerticalScale", 3f, "Scale factor for vertical height. 1 = no distortion");
			RolloffFactor = ((BaseUnityPlugin)this).Config.Bind<float>("Proximity", "RolloffFactor", 5f, "Steepness of logarithmic rolloff. 1 = gentle, 10 = steep.");
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)$"Proximity Chat loaded — Min: {MinDistance.Value}u, Max: {MaxDistance.Value}u, VerticalScale: {VerticalScale}x, Rolloff: {RolloffFactor.Value}");
			Harmony.CreateAndPatchAll(typeof(ProximityChatPatch), (string)null);
		}
	}
	[HarmonyPatch]
	internal class ProximityChatPatch
	{
		[HarmonyPatch(typeof(C_Controller_Player), "Update")]
		[HarmonyPostfix]
		public static void ApplyProximityVolume(C_Controller_Player __instance)
		{
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			if (((NetworkBehaviour)__instance).HasInputAuthority || Traverse.Create((object)__instance).Field<bool>("isAI").Value)
			{
				return;
			}
			C_Controller_Player val = CMD_PlayerManager.Instance?.localPlayer;
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			VoiceNetworkObject source_VoiceSpeaker = __instance.source_VoiceSpeaker;
			if ((Object)(object)((source_VoiceSpeaker != null) ? source_VoiceSpeaker.SpeakerInUse : null) == (Object)null)
			{
				return;
			}
			AudioSource component = ((Component)__instance.source_VoiceSpeaker.SpeakerInUse).GetComponent<AudioSource>();
			if (!((Object)(object)component == (Object)null))
			{
				E_SFX component2 = ((Component)__instance.source_VoiceSpeaker.SpeakerInUse).GetComponent<E_SFX>();
				if (!((Object)(object)component2 != (Object)null) || !(component2.echo > 0f))
				{
					float num = (((Object)(object)component2 != (Object)null) ? Traverse.Create((object)component2).Field<float>("target_volume").Value : 1f);
					float num2 = CalculateLogVolume(CalculateWeightedDistance(((Component)val).transform.position, ((Component)__instance).transform.position));
					component.spatialBlend = 1f;
					component.rolloffMode = (AudioRolloffMode)2;
					component.SetCustomCurve((AudioSourceCurveType)0, Plugin.FlatRolloff);
					component.minDistance = (((C_Controller)__instance).MouseModeEnabled() ? Plugin.MouseMinDistance.Value : Plugin.MinDistance.Value);
					component.maxDistance = (((C_Controller)__instance).MouseModeEnabled() ? Plugin.MouseMaxDistance.Value : Plugin.MaxDistance.Value);
					component.volume = num2 * num;
				}
			}
		}

		private static float CalculateLogVolume(float distance)
		{
			float value = Plugin.MinDistance.Value;
			float value2 = Plugin.MaxDistance.Value;
			float num = Mathf.Max(0.1f, Plugin.RolloffFactor.Value);
			if (distance <= value)
			{
				return 1f;
			}
			if (distance >= value2)
			{
				return 0f;
			}
			float num2 = (distance - value) / (value2 - value);
			float num3 = 1f - Mathf.Log(1f + num2 * num) / Mathf.Log(1f + num);
			return Mathf.Clamp01(num3);
		}

		private static float CalculateWeightedDistance(Vector3 from, Vector3 to)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = to - from;
			val.y *= Plugin.VerticalScale.Value;
			return ((Vector3)(ref val)).magnitude;
		}
	}
}