Decompiled source of Cinematic Hit v1.0.0

CinematicHit.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Landfall.TABS;
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("CinematicHit")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("GeeztJeez")]
[assembly: AssemblyProduct("CinematicHit")]
[assembly: AssemblyCopyright("Copyright © Geezt Lifetime")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7acfaef0-7669-4401-8bff-5a9a02e18c75")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace CinematicHit;

[BepInPlugin("GeeztJeez.CinematicHit", "CinematicHit", "1.0.0")]
internal class Loader : BaseUnityPlugin
{
	public static ConfigEntry<bool> doHitSlowmo;

	public static ConfigEntry<bool> doDeathSlowmo;

	public static ConfigEntry<float> minSlowmo;

	public static ConfigEntry<float> maxSlowmo;

	public static ConfigEntry<float> slowmoRatio;

	public static ConfigEntry<float> minDamage;

	public static bool GetBoolDoHitSlowmo => doHitSlowmo.Value;

	public static bool GetBoolDoDeathSlowmo => doDeathSlowmo.Value;

	public static float GetMinSlowmo => minSlowmo.Value;

	public static float GetMaxSlowmo => maxSlowmo.Value;

	public static float GetMinDamage => minDamage.Value;

	public static float GetSlowmoRatio => slowmoRatio.Value;

	public static event Action<bool> OnDoHitSlowmoChanged;

	public static event Action<bool> OnDoDeathSlowmoChanged;

	public static event Action<float> OnMinSlowmoChanged;

	public static event Action<float> OnMaxSlowmoChanged;

	public static event Action<float> OnMinDamageChanged;

	public static event Action<float> OnSlowmoRatioChanged;

	private void CreateCinematicHitSettings()
	{
		doHitSlowmo = ((BaseUnityPlugin)this).Config.Bind<bool>("CinematicHit", "Hit Slowmo Enabled", true, "Enable slow-motion for hit events.");
		AttachBoolSettingChangedHandler(doHitSlowmo, Loader.OnDoHitSlowmoChanged);
		doDeathSlowmo = ((BaseUnityPlugin)this).Config.Bind<bool>("CinematicHit", "Death Slowmo Enabled", true, "Enable slow-motion for kill events.");
		AttachBoolSettingChangedHandler(doDeathSlowmo, Loader.OnDoDeathSlowmoChanged);
		minSlowmo = ((BaseUnityPlugin)this).Config.Bind<float>("CinematicHit", "Min Slowmo", 0.01f, "Minimum slow-motion scale.");
		AttachFloatSettingChangedHandler(minSlowmo, Loader.OnMinSlowmoChanged);
		maxSlowmo = ((BaseUnityPlugin)this).Config.Bind<float>("CinematicHit", "Max Slowmo", 5f, "Maximum slow-motion scale.");
		AttachFloatSettingChangedHandler(maxSlowmo, Loader.OnMaxSlowmoChanged);
		slowmoRatio = ((BaseUnityPlugin)this).Config.Bind<float>("CinematicHit", "Slowmo Ratio", 1000f, "Damage → slowmo conversion multiplier.");
		AttachFloatSettingChangedHandler(slowmoRatio, Loader.OnSlowmoRatioChanged);
		minDamage = ((BaseUnityPlugin)this).Config.Bind<float>("CinematicHit", "Min Damage", 100f, "Minimum damage required to trigger slow-motion.");
		AttachFloatSettingChangedHandler(minDamage, Loader.OnMinDamageChanged);
	}

	private void Awake()
	{
		//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_0016: Expected O, but got Unknown
		GameObject val = new GameObject("CinematicHit_TimeController");
		val.AddComponent<TimeController>();
		Object.DontDestroyOnLoad((Object)val);
		((MonoBehaviour)this).StartCoroutine(Call());
	}

	private void AttachBoolSettingChangedHandler(ConfigEntry<bool> entry, Action<bool> targetEvent)
	{
		entry.SettingChanged += delegate
		{
			targetEvent?.Invoke(entry.Value);
		};
	}

	private void AttachFloatSettingChangedHandler(ConfigEntry<float> entry, Action<float> targetEvent)
	{
		entry.SettingChanged += delegate
		{
			targetEvent?.Invoke(entry.Value);
		};
	}

	private SettingsInstance CreateOptionsSettingInstance(ConfigEntry<bool> configEntry, string key, string toolTip)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Expected O, but got Unknown
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		SettingsInstance val2 = new SettingsInstance();
		val2.settingsType = (SettingsType)0;
		val2.options = new string[2] { "On", "Off" };
		val2.currentValue = ((!configEntry.Value) ? 1 : 0);
		val2.m_settingsKey = key;
		val2.toolTip = toolTip;
		val2.OnValueChanged += delegate(int val)
		{
			configEntry.Value = val == 0;
		};
		return val2;
	}

	private IEnumerator Call()
	{
		yield return (object)new WaitUntil((Func<bool>)(() => (Object)(object)Object.FindObjectOfType<ServiceLocator>() != (Object)null));
		CreateCinematicHitSettings();
		try
		{
			new Harmony("GeeztJeez.CinematicHit").PatchAll(Assembly.GetExecutingAssembly());
			Debug.Log((object)"[CinematicHit] Harmony patched successfully.");
		}
		catch (Exception ex)
		{
			Debug.LogWarning((object)("[CinematicHit] Harmony patch failed: " + ex));
		}
		yield return ((MonoBehaviour)this).StartCoroutine(GenerateSettings());
		Debug.Log((object)"[CinematicHit] Loaded Successfully!");
	}

	private IEnumerator GenerateSettings()
	{
		GlobalSettingsHandler service = null;
		yield return (object)new WaitUntil((Func<bool>)(() => Object.op_Implicit((Object)(object)(service = ServiceLocator.GetService<GlobalSettingsHandler>()))));
		List<SettingsInstance> list = service.GameplaySettings.ToList();
		list.Add(CreateOptionsSettingInstance(doHitSlowmo, "Hit Slowmo Enabled", "Enable slow-motion for hit events (CinematicHit mod)."));
		list.Add(CreateOptionsSettingInstance(doDeathSlowmo, "Death Slowmo Enabled", "Enable slow-motion for kill events (CinematicHit mod)."));
		list.Add(CreateSliderSettingInstance(minSlowmo, "Min Slowmo", "Minimum slow-motion scale (CinematicHit mod).", 0.01f, 1f, (float)((ConfigEntryBase)minSlowmo).DefaultValue));
		list.Add(CreateSliderSettingInstance(maxSlowmo, "Max Slowmo", "Maximum slow-motion scale (CinematicHit mod).", 1f, 10f, (float)((ConfigEntryBase)maxSlowmo).DefaultValue));
		list.Add(CreateSliderSettingInstance(slowmoRatio, "Slowmo Ratio", "Damage → slowmo conversion multiplier (CinematicHit mod).", 100f, 5000f, (float)((ConfigEntryBase)slowmoRatio).DefaultValue));
		list.Add(CreateSliderSettingInstance(minDamage, "Min Damage", "Minimum damage required to trigger slow-motion (CinematicHit mod).", 10f, 500f, (float)((ConfigEntryBase)minDamage).DefaultValue));
		((object)service).GetType().GetField("m_gameplaySettings", (BindingFlags)(-1)).SetValue(service, list.ToArray());
	}

	private SettingsInstance CreateSliderSettingInstance(ConfigEntry<float> configEntry, string key, string toolTip, float min, float max, float defaultValue)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: 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)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Expected O, but got Unknown
		SettingsInstance val2 = new SettingsInstance
		{
			settingsType = (SettingsType)1,
			currentSliderValue = configEntry.Value,
			defaultSliderValue = defaultValue,
			min = min,
			max = max,
			m_settingsKey = key,
			toolTip = toolTip
		};
		val2.OnSliderValueChanged += delegate(float val)
		{
			configEntry.Value = val;
		};
		return val2;
	}
}
public class TimeController : MonoBehaviour
{
	private float slowTimeRemaining;

	private float targetSlowScale = 1f;

	private static Coroutine slowMoCoroutine;

	public static TimeController Instance { get; private set; }

	private void Awake()
	{
		if ((Object)(object)Instance != (Object)null && (Object)(object)Instance != (Object)(object)this)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
			return;
		}
		Instance = this;
		Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
	}

	public void RequestSlowMo(float scale, float duration)
	{
		if (scale < targetSlowScale)
		{
			targetSlowScale = scale;
		}
		slowTimeRemaining += duration;
		if (slowMoCoroutine == null)
		{
			slowMoCoroutine = ((MonoBehaviour)this).StartCoroutine(SlowMoRoutine());
		}
	}

	private IEnumerator SlowMoRoutine()
	{
		Time.timeScale = targetSlowScale;
		while (slowTimeRemaining > 0f)
		{
			slowTimeRemaining -= Time.unscaledDeltaTime;
			yield return null;
		}
		Time.timeScale = 1f;
		targetSlowScale = 1f;
		slowMoCoroutine = null;
	}
}
[HarmonyPatch(typeof(HealthHandler), "TakeDamage")]
public class CHTakeDamagePatch
{
	private static void Prefix(HealthHandler __instance, float damage)
	{
		if (float.IsNaN(damage) || float.IsInfinity(damage) || damage <= 0f)
		{
			return;
		}
		FieldInfo field = typeof(HealthHandler).GetField("data", BindingFlags.Instance | BindingFlags.NonPublic);
		object? obj = ((field != null) ? field.GetValue(__instance) : null);
		DataHandler val = (DataHandler)((obj is DataHandler) ? obj : null);
		if (!((Object)(object)val == (Object)null) && !val.Dead && Loader.GetBoolDoHitSlowmo && !(damage <= Loader.GetMinDamage))
		{
			float duration = Mathf.Clamp(damage / Loader.GetSlowmoRatio, Loader.GetMinSlowmo, Loader.GetMaxSlowmo);
			TimeController instance = TimeController.Instance;
			if (!((Object)(object)instance == (Object)null))
			{
				instance.RequestSlowMo(Mathf.Lerp(0.05f, 0.15f, Mathf.Clamp01(1f - damage / Loader.GetSlowmoRatio)), duration);
			}
		}
	}
}
[HarmonyPatch(typeof(HealthHandler), "Die")]
public class CHDiePatch
{
	private static bool Prefix(HealthHandler __instance, Unit damager)
	{
		if (!Loader.GetBoolDoDeathSlowmo)
		{
			return true;
		}
		float maxHealth = ((Component)((Component)__instance).transform.root).GetComponentInChildren<Unit>().data.maxHealth;
		float duration = Mathf.Clamp(maxHealth / Loader.GetSlowmoRatio, Loader.GetMinSlowmo, Loader.GetMaxSlowmo);
		TimeController.Instance?.RequestSlowMo(Mathf.Lerp(0.05f, 0.12f, Mathf.Clamp01(1f - maxHealth / Loader.GetSlowmoRatio)), duration);
		return true;
	}
}