Decompiled source of ImpactFrameMod v1.0.1

ImpactFrame.dll

Decompiled 11 hours ago
using System;
using System.Collections;
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 HarmonyLib;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ImpactFrame")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ImpactFrame")]
[assembly: AssemblyTitle("ImpactFrame")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace ImpactFrameMod;

public static class ImpactFrameConfig
{
	internal static ConfigEntry<bool> Enabled { get; private set; }

	internal static ConfigEntry<int> DurationFrames { get; private set; }

	internal static ConfigEntry<float> LensDistortionIntensity { get; private set; }

	internal static ConfigEntry<float> ChromaticAberrationIntensity { get; private set; }

	internal static ConfigEntry<bool> DetectBySniperName { get; private set; }

	internal static ConfigEntry<bool> DetectByDamage { get; private set; }

	internal static ConfigEntry<int> DamageThreshold { get; private set; }

	public static void Initialize(ConfigFile config)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Expected O, but got Unknown
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Expected O, but got Unknown
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Expected O, but got Unknown
		//IL_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Expected O, but got Unknown
		Enabled = config.Bind<bool>("Impact Frame", "Enabled", true, "Enables or disables the complete Impact Frame effect.");
		DurationFrames = config.Bind<int>("Impact Frame", "Duration Frames (60 FPS)", 50, new ConfigDescription("Effect duration in frames at 60 FPS. 30 frames equals 0.5 seconds.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 90), Array.Empty<object>()));
		LensDistortionIntensity = config.Bind<float>("Impact Frame", "Lens Distortion Intensity", -0.6f, new ConfigDescription("Lens distortion at the start. Negative values distort outward; positive values distort inward.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(-1f, 1f), Array.Empty<object>()));
		ChromaticAberrationIntensity = config.Bind<float>("Impact Frame", "Chromatic Aberration Intensity", 1f, new ConfigDescription("Chromatic aberration at the start of the effect.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		DetectBySniperName = config.Bind<bool>("Impact Frame Detection", "Detect By Name", true, "Detects a sniper when its GameObject name contains 'Sniper'.");
		DetectByDamage = config.Bind<bool>("Impact Frame Detection", "Detect By Damage", true, "Detects a sniper when weapon damage is at or above the threshold.");
		DamageThreshold = config.Bind<int>("Impact Frame Detection", "Damage Threshold", 150, new ConfigDescription("Minimum weapon damage required when detection by damage is enabled.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(100, 150), Array.Empty<object>()));
	}
}
[HarmonyPatch(typeof(Weapon), "Shoot")]
public class ImpactFramePatch
{
	private const float ReferenceFramesPerSecond = 60f;

	private static Volume _impactFrameVolume;

	private static LensDistortion _lensDistortion;

	private static ChromaticAberration _chromaticAberration;

	private static Coroutine _recoveryCoroutine;

	[HarmonyPrefix]
	private static void Prefix(Weapon __instance, ref int __state)
	{
		__state = (((Object)(object)__instance != (Object)null) ? __instance.Ammo : (-1));
	}

	[HarmonyPostfix]
	private static void Postfix(Weapon __instance, int __state)
	{
		if (ImpactFrameConfig.Enabled.Value && !((Object)(object)__instance == (Object)null) && __instance.Ammo < __state && IsSniperWeapon(__instance))
		{
			if ((Object)(object)_impactFrameVolume == (Object)null)
			{
				CreateImpactFrameVolume();
			}
			if (_recoveryCoroutine != null)
			{
				((MonoBehaviour)Plugin.Instance).StopCoroutine(_recoveryCoroutine);
			}
			ApplyInitialValues();
			_recoveryCoroutine = ((MonoBehaviour)Plugin.Instance).StartCoroutine(RecoverEffect());
		}
	}

	private static bool IsSniperWeapon(Weapon weapon)
	{
		bool flag = false;
		bool flag2 = false;
		if (ImpactFrameConfig.DetectBySniperName.Value)
		{
			string text = (((Object)(object)((Component)weapon).gameObject != (Object)null) ? ((Object)((Component)weapon).gameObject).name : null);
			flag = !string.IsNullOrEmpty(text) && text.IndexOf("sniper", StringComparison.OrdinalIgnoreCase) >= 0;
		}
		if (ImpactFrameConfig.DetectByDamage.Value)
		{
			flag2 = weapon.Damage >= ImpactFrameConfig.DamageThreshold.Value;
		}
		return flag || flag2;
	}

	private static void CreateImpactFrameVolume()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		GameObject val = new GameObject("ImpactFrameVolume");
		Object.DontDestroyOnLoad((Object)val);
		_impactFrameVolume = val.AddComponent<Volume>();
		_impactFrameVolume.isGlobal = true;
		_impactFrameVolume.priority = 100f;
		_impactFrameVolume.profile = ScriptableObject.CreateInstance<VolumeProfile>();
		_lensDistortion = _impactFrameVolume.profile.Add<LensDistortion>(true);
		_chromaticAberration = _impactFrameVolume.profile.Add<ChromaticAberration>(true);
		ClearValues();
	}

	private static void ApplyInitialValues()
	{
		((VolumeParameter<float>)(object)_lensDistortion.intensity).Override(ImpactFrameConfig.LensDistortionIntensity.Value);
		((VolumeParameter<float>)(object)_chromaticAberration.intensity).Override(ImpactFrameConfig.ChromaticAberrationIntensity.Value);
	}

	private static IEnumerator RecoverEffect()
	{
		float duration = (float)ImpactFrameConfig.DurationFrames.Value / 60f;
		float initialLensDistortion = ImpactFrameConfig.LensDistortionIntensity.Value;
		float initialChromaticAberration = ImpactFrameConfig.ChromaticAberrationIntensity.Value;
		float elapsed = 0f;
		while (elapsed < duration)
		{
			elapsed += Time.unscaledDeltaTime;
			float num = Mathf.Clamp01(elapsed / duration);
			float num2 = Mathf.SmoothStep(0f, 1f, num);
			((VolumeParameter<float>)(object)_lensDistortion.intensity).Override(Mathf.Lerp(initialLensDistortion, 0f, num2));
			((VolumeParameter<float>)(object)_chromaticAberration.intensity).Override(Mathf.Lerp(initialChromaticAberration, 0f, num2));
			yield return null;
		}
		ClearValues();
		_recoveryCoroutine = null;
	}

	private static void ClearValues()
	{
		((VolumeParameter<float>)(object)_lensDistortion.intensity).Override(0f);
		((VolumeParameter<float>)(object)_chromaticAberration.intensity).Override(0f);
	}
}
[BepInPlugin("com.impactframe.mod", "Impact Frame", "0.1.0")]
public sealed class Plugin : BaseUnityPlugin
{
	public const string PluginGuid = "com.impactframe.mod";

	public const string PluginName = "Impact Frame";

	public const string PluginVersion = "0.1.0";

	private Harmony _harmony;

	internal static Plugin Instance { get; private set; }

	internal static ManualLogSource Log { get; private set; }

	private void Awake()
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		ImpactFrameConfig.Initialize(((BaseUnityPlugin)this).Config);
		_harmony = new Harmony("com.impactframe.mod");
		_harmony.PatchAll();
		Log.LogInfo((object)"Impact Frame 0.1.0 loaded.");
	}

	private void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
		Instance = null;
	}
}