Decompiled source of Stronger Hit Feedback v1.1.1

StrongerHitFeedback.dll

Decompiled 3 months 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.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using StrongerHitFeedback;
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("StrongerHitFeedback")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StrongerHitFeedback")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("17665fc7-b7f3-448d-b85b-22506f488236")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
public class AudioResourceLoader
{
	public static void loadFromFolder(string modName)
	{
		ResourceLoaderSoundbanks resourceLoaderSoundbanks = new ResourceLoaderSoundbanks();
		resourceLoaderSoundbanks.AutoloadFromPath(ETGMod.FolderPath((BaseUnityPlugin)(object)HitFeedbackModule.instance), modName);
	}
}
public class ResourceLoaderSoundbanks
{
	public void AutoloadFromPath(string path, string prefix)
	{
		if (string.IsNullOrEmpty(path))
		{
			throw new ArgumentNullException("path", "Path cannot be null.");
		}
		if (string.IsNullOrEmpty(prefix))
		{
			throw new ArgumentNullException("prefix", "Prefix name cannot be null.");
		}
		prefix = prefix.Trim();
		if (string.IsNullOrEmpty(prefix))
		{
			throw new ArgumentException("Prefix name cannot be an empty (or whitespace only) string.", "prefix");
		}
		path = path.Replace('/', Path.DirectorySeparatorChar);
		path = path.Replace('\\', Path.DirectorySeparatorChar);
		if (!Directory.Exists(path))
		{
			Debug.LogError((object)$"{typeof(ResourceLoaderSoundbanks)}: No autoload directory in path, not autoloading anything. Path='{path}'.");
			return;
		}
		List<string> list = new List<string>(Directory.GetFiles(path, "*.bnk", SearchOption.AllDirectories));
		for (int i = 0; i < list.Count; i++)
		{
			string text = list[i];
			string text2 = text;
			text2 = text2.Replace('/', Path.DirectorySeparatorChar);
			text2 = text2.Replace('\\', Path.DirectorySeparatorChar);
			text2 = text2.Substring(text2.IndexOf(path) + path.Length);
			text2 = text2.Substring(0, text2.Length - ".bnk".Length);
			if (text2.IndexOf(Path.DirectorySeparatorChar) == 0)
			{
				text2 = text2.Substring(1);
			}
			text2 = prefix + ":" + text2;
			Debug.Log((object)$"{typeof(ResourceLoaderSoundbanks)}: Soundbank found, attempting to autoload: name='{text2}' file='{text}'");
			using FileStream stream = File.OpenRead(text);
			LoadSoundbankFromStream(stream, text2);
		}
	}

	private void LoadSoundbankFromStream(Stream stream, string name)
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		byte[] array = StreamToByteArray(stream);
		IntPtr intPtr = Marshal.AllocHGlobal(array.Length);
		try
		{
			Marshal.Copy(array, 0, intPtr, array.Length);
			uint num = default(uint);
			AKRESULT val = AkSoundEngine.LoadAndDecodeBankFromMemory(intPtr, (uint)array.Length, false, name, false, ref num);
			Debug.Log((object)$"Result of soundbank load: {val}.");
		}
		finally
		{
			Marshal.FreeHGlobal(intPtr);
		}
	}

	public static byte[] StreamToByteArray(Stream input)
	{
		byte[] array = new byte[16384];
		using MemoryStream memoryStream = new MemoryStream();
		int count;
		while ((count = input.Read(array, 0, array.Length)) > 0)
		{
			memoryStream.Write(array, 0, count);
		}
		return memoryStream.ToArray();
	}
}
namespace StrongerHitFeedback;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("kleirof.etg.strongerhitfeedback", "Stronger Hit Feedback", "1.1.1")]
public class HitFeedbackModule : BaseUnityPlugin
{
	public class HitFeedbackPatches
	{
		[HarmonyILManipulator]
		[HarmonyPatch(typeof(Projectile), "HandleKnockback")]
		public static void HandleKnockbackPatch(ILContext ctx)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			ILCursor val = new ILCursor(ctx);
			if (val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction x) => ILPatternMatchingExt.MatchMul(x)
			}))
			{
				val.EmitDelegate<Func<float, float>>((Func<float, float>)((float orig) => orig * 2f));
			}
		}

		[HarmonyILManipulator]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		public static void FlashOnHitPatch(ILContext ctx)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(ctx);
			if (val.TryGotoNext((MoveType)0, new Func<Instruction, bool>[1]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdstr(x, "Play_ENM_hurt")
			}))
			{
				FieldInfo fieldInfo = AccessTools.Field(Type.GetType("HealthHaver+<FlashOnHit>c__Iterator0, Assembly-CSharp"), "$this");
				val.Emit(OpCodes.Ldarg_0);
				val.Emit(OpCodes.Ldfld, fieldInfo);
				val.EmitDelegate<Action<HealthHaver>>((Action<HealthHaver>)delegate(HealthHaver self)
				{
					Random random = new Random();
					int num = random.Next(1, 7);
					AkSoundEngine.PostEvent($"Play_hit_{num}", ((Component)self).gameObject);
				});
			}
			val.Index = 0;
			while (val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdcR4(x, 0.04f)
			}))
			{
				val.EmitDelegate<Func<float, float>>((Func<float, float>)((float orig) => orig * 9f));
			}
			val.Index = 0;
			if (val.TryGotoNext((MoveType)2, new Func<Instruction, bool>[1]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdcR4(x, 0.2f)
			}))
			{
				val.EmitDelegate<Func<float, float>>((Func<float, float>)((float orig) => 0f));
			}
		}

		[HarmonyILManipulator]
		[HarmonyPatch(typeof(HealthHaver), "ApplyDamageDirectional")]
		public static void ApplyDamageDirectionalPatch(ILContext ctx)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			ILCursor val = new ILCursor(ctx);
			if (val.TryGotoNext((MoveType)0, new Func<Instruction, bool>[1]
			{
				(Instruction x) => ILPatternMatchingExt.MatchLdstr(x, "Play_ENM_death")
			}))
			{
				val.Emit(OpCodes.Ldarg_0);
				val.EmitDelegate<Action<HealthHaver>>((Action<HealthHaver>)delegate(HealthHaver self)
				{
					Random random = new Random();
					int num = random.Next(1, 5);
					AkSoundEngine.PostEvent($"Play_killed_{num}", ((Component)self).gameObject);
				});
			}
		}
	}

	public const string GUID = "kleirof.etg.strongerhitfeedback";

	public const string NAME = "Stronger Hit Feedback";

	public const string VERSION = "1.1.1";

	public const string TEXT_COLOR = "#92A1E6";

	public static HitFeedbackModule instance;

	public void Start()
	{
		instance = this;
		ETGModMainBehaviour.WaitForGameManagerStart((Action<GameManager>)GMStart);
	}

	public void GMStart(GameManager g)
	{
		Log("Stronger Hit Feedback v1.1.1 started successfully.", "#92A1E6");
		AudioResourceLoader.loadFromFolder("Stronger Hit Feedback");
		Harmony.CreateAndPatchAll(typeof(HitFeedbackPatches), (string)null);
	}

	public static void Log(string text, string color = "FFFFFF")
	{
		ETGModConsole.Log((object)("<color=" + color + ">" + text + "</color>"), false);
	}
}