Decompiled source of Configurable Hero Chant Volume v1.0.0

ConfigurableHeroChantVolume.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.IO;
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.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using P2.Game;
using P2.Game.Unit;
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(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("ConfigurableHeroChantVolume")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Configurable Hero Chant Volume")]
[assembly: AssemblyTitle("ConfigurableHeroChantVolume")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
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 ConfigurableHeroChantVolume
{
	[BepInPlugin("com.nextrg.configurableherochantvolume", "Configurable Hero Chant Volume", "1.0.2")]
	public class Plugin : BasePlugin
	{
		public static ConfigEntry<float> configDistVol;

		public static ConfigEntry<float> configChantVol;

		internal static ManualLogSource Log;

		public static ConfigFile config;

		private FileSystemWatcher _watcher;

		private const string guid = "com.nextrg.configurableherochantvolume";

		public override void Load()
		{
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			Log = ((BasePlugin)this).Log;
			config = ((BasePlugin)this).Config;
			Log.LogInfo((object)"Loaded");
			configChantVol = ((BasePlugin)this).Config.Bind<float>("General", "ChantVolume", 0f, "Global volume (0, 0.1, 0.2 .. 1), value 0 will mute the sound");
			configDistVol = ((BasePlugin)this).Config.Bind<float>("General", "DistanceVolume", 0f, "Distance-based volume (0, 0.1, 0.2 .. 1), value 0 will nullify the effect");
			UpdateVariables();
			CheckConfigChanges();
			Harmony val = new Harmony("com.nextrg.configurableherochantvolume");
			val.PatchAll();
		}

		public void CheckConfigChanges()
		{
			_watcher = new FileSystemWatcher(Path.GetDirectoryName(config.ConfigFilePath))
			{
				Filter = Path.GetFileName(config.ConfigFilePath),
				NotifyFilter = (NotifyFilters.Size | NotifyFilters.LastWrite),
				IncludeSubdirectories = false
			};
			_watcher.Changed += delegate
			{
				RefreshConfig();
			};
			_watcher.EnableRaisingEvents = true;
		}

		public static void UpdateVariables()
		{
			Variables.distGlobalVol = Math.Clamp(configDistVol.Value, 0f, 1f);
			Variables.chantGlobalVol = Math.Clamp(configChantVol.Value, 0f, 3f);
		}

		public static void RefreshConfig()
		{
			try
			{
				config.Reload();
			}
			catch (Exception)
			{
			}
			UpdateVariables();
		}
	}
	public static class Reflection
	{
		public static object GetValue(object instance, string propertyName)
		{
			PropertyInfo property = instance.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property == null)
			{
				return null;
			}
			return property.GetValue(instance);
		}

		public static void SetValue(object instance, string propertyName, object value)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			PropertyInfo property = instance.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property == null)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(26, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Property '");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(propertyName);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' not found in ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(instance.GetType().Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("!");
				}
				log.LogError(val);
			}
			else
			{
				property.SetValue(instance, value);
			}
		}

		public static object GetValueFirst(object instance, string propertyName)
		{
			Type type = instance.GetType();
			PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			PropertyInfo[] array = properties;
			foreach (PropertyInfo propertyInfo in array)
			{
				if (propertyInfo.Name == propertyName)
				{
					return propertyInfo.GetValue(instance);
				}
			}
			return null;
		}
	}
	public static class Variables
	{
		public static float distGlobalVol;

		public static float chantGlobalVol;

		public static float heroX;
	}
	[HarmonyPatch(typeof(TrackingCamera), "update")]
	public static class CameraPosition
	{
		public static float GetCameraX(TrackingCamera __instance)
		{
			if (Variables.distGlobalVol == 0f || Variables.chantGlobalVol == 0f)
			{
				return 0f;
			}
			float num = (float)Reflection.GetValue(__instance, "realX_");
			float num2 = (float)Reflection.GetValue(__instance, "manualOffsetX_");
			return num + num2;
		}

		public static float GetDistance(TrackingCamera __instance)
		{
			if (Variables.distGlobalVol == 0f || Variables.chantGlobalVol == 0f)
			{
				return 0f;
			}
			float cameraX = GetCameraX(__instance);
			return Math.Abs(cameraX - Variables.heroX - 25f);
		}

		public static float GetDistanceVolume(float distance)
		{
			float num = Math.Clamp(1f - distance / 450f, 0f, 1f);
			return Mathf.Lerp(1f - Variables.distGlobalVol, 1f, num);
		}

		public static void Prefix(TrackingCamera __instance)
		{
			float distanceVolume = GetDistanceVolume(GetDistance(__instance));
			float num = Variables.chantGlobalVol * distanceVolume;
			SingletonMonobehaviour<CriSoundManager>.Instance.setCategorySetVolume((MixingCategory)10, num, 0f);
		}
	}
	[HarmonyPatch(typeof(UnitSquad), "checkHeroMode")]
	public static class HeroPosition
	{
		public static void Postfix(UnitSquad __instance, bool __result)
		{
			if (Variables.distGlobalVol != 0f && Variables.chantGlobalVol != 0f)
			{
				object instance = AccessTools.Method(((object)__instance).GetType(), "getUnit_LineIndex", (Type[])null, (Type[])null)?.Invoke(__instance, new object[1] { 0 });
				if ((int)Reflection.GetValue(Reflection.GetValueFirst(instance, "info_"), "uniqueId") == 0)
				{
					object value = Reflection.GetValue(instance, "unitModel_");
					Variables.heroX = ((Vector4)Reflection.GetValue(value, "pos_")).x;
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "ConfigurableHeroChantVolume";

		public const string PLUGIN_NAME = "Configurable Hero Chant Volume";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}