Decompiled source of MinecraftHurtVisuals v1.0.0

Terren-MinecraftHurtVisuals.dll

Decompiled 2 months 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.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Landfall.TABS;
using Landfall.TABS.GameState;
using TFBGames;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("Terren")]
[assembly: AssemblyTitle("MCDamageVisuals")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Terren.MinecraftDamageVisuals
{
	[BepInPlugin("terren.minecrafthurtvisuals", "MinecraftHurtVisuals", "1.0.0")]
	public class Init_MinecraftHurt : BaseUnityPlugin
	{
		public static AssetBundle assetBundle;

		public static ConfigEntry<bool> ConfigDontDropWeapons;

		public static ConfigEntry<bool> ConfigDespawnRagdolls;

		public static ConfigEntry<bool> firstLaunch;

		private void Awake()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			new MinecraftVFXHolder();
			new Harmony("MinecraftHurtVisuals").PatchAll();
			((MonoBehaviour)this).StartCoroutine(WaitForService());
		}

		static Init_MinecraftHurt()
		{
			assetBundle = AssetBundle.LoadFromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("terrenminecrafteffects"));
		}

		private void LoadMod_Delayed()
		{
			SoundBank soundBank = ServiceLocator.GetService<SoundPlayer>().soundBank;
			SoundBank val = assetBundle.LoadAsset<SoundBank>("MinecraftSoundbank");
			SoundBankCategory[] categories = val.Categories;
			for (int i = 0; i < categories.Length; i++)
			{
				categories[i].categoryMixerGroup = soundBank.Categories[0].categoryMixerGroup;
			}
			List<SoundBankCategory> list = soundBank.Categories.ToList();
			list.AddRange(val.Categories);
			soundBank.Categories = list.ToArray();
			ConfigDontDropWeapons = ((BaseUnityPlugin)this).Config.Bind<bool>("Gameplay", "ConfigDontDropWeapons", true, "Disable weapons dropping and skeletons/machines falling apart (Default: true)");
			ConfigDespawnRagdolls = ((BaseUnityPlugin)this).Config.Bind<bool>("Gameplay", "ConfigDespawnRagdolls", true, "Despawn dead units with Poof Effect (Default: true)");
			int num = (ConfigDontDropWeapons.Value ? 1 : 0);
			int num2 = (ConfigDespawnRagdolls.Value ? 1 : 0);
			CreateSettingGameplay(useLocalization: false, (SettingsType)0, "[MCHurtVisuals] Don't Drop Weapons/Fall Apart", num, num, new string[2] { "Disabled", "Enabled" }).OnValueChanged += MinecraftVFXHolder.WeaponDropSetting;
			CreateSettingGameplay(useLocalization: false, (SettingsType)0, "[MCHurtVisuals] Despawn Dead Units ", num2, num2, new string[2] { "Disabled", "Enabled" }).OnValueChanged += MinecraftVFXHolder.RagdollDespawnSetting;
			MinecraftVFXHolder.WeaponDropSetting(num);
			MinecraftVFXHolder.RagdollDespawnSetting(num2);
			Debug.Log((object)"--- MinecraftDamageVisuals v1.0: Up and Running!");
		}

		private IEnumerator WaitForService()
		{
			yield return (object)new WaitUntil((Func<bool>)(() => (Object)(object)Object.FindObjectOfType<ServiceLocator>() != (Object)null));
			yield return (object)new WaitUntil((Func<bool>)(() => (Object)(object)ServiceLocator.GetService<SoundPlayer>() != (Object)null));
			LoadMod_Delayed();
		}

		private void Dummy(int v)
		{
			Debug.LogWarning((object)("[MCHurtVisuals] Dummy is called. " + v));
		}

		public static SettingsInstance CreateSettingGameplay(bool useLocalization, SettingsType type, string key, float defaultValue, float currentValue, string[] options = null, float min = 0f, float max = 1f)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: 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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			SettingsInstance val = new SettingsInstance
			{
				localizeOptions = useLocalization,
				settingsType = type,
				m_settingsKey = key,
				settingName = key,
				toolTip = key,
				options = options,
				defaultValue = (int)defaultValue,
				currentValue = (int)currentValue,
				defaultSliderValue = defaultValue,
				currentSliderValue = currentValue,
				min = min,
				max = max
			};
			GlobalSettingsHandler service = ServiceLocator.GetService<GlobalSettingsHandler>();
			List<SettingsInstance> list = service.GameplaySettings.ToList();
			list.Add(val);
			typeof(GlobalSettingsHandler).GetField("m_gameplaySettings", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(service, list.ToArray());
			return val;
		}
	}
	public class MinecraftUnit : GameStateListener
	{
		private float currentExpLevel;

		private int currentLevel;

		private WeaponHandler myWeapons;

		private Unit unit;

		private HealthHandler health;

		private AnimationCurve colorCurve = new AnimationCurve((Keyframe[])(object)new Keyframe[4]
		{
			new Keyframe(0f, 1f),
			new Keyframe(0.2f, 1f),
			new Keyframe(0.25f, 0f),
			new Keyframe(0.3f, 0f)
		});

		private UnitColorInstance color = new UnitColorInstance
		{
			colorName = "Minecraft_Hurt",
			acceptWhenDead = true,
			color = Color.red
		};

		private UnitColorHandler myColor;

		public AudioPathData hitSound = new AudioPathData("Terren", "MinecraftHurt", 1f, 2f);

		private bool dead;

		private float amount;

		private float immunity;

		public AudioPathData fallSound = new AudioPathData("Terren", "MinecraftFall", 1f, 2f);

		public AudioPathData fallSoundHard = new AudioPathData("Terren", "MinecraftFallBig", 1f, 2f);

		private PlaySoundEffect hurtSoundPlayer;

		private PlaySoundEffect fallSoundPlayer;

		private bool started;

		private float recievedDamage;

		private float currentTime;

		private float currentColorValue;

		private bool effectDone = true;

		private bool removedColor = true;

		private float effectDuration = 1f;

		private void UpdateLevel(float recievedExp)
		{
		}

		private void HurtEffect()
		{
			if (!(immunity > 0f) && !(recievedDamage <= 0f))
			{
				currentTime = 0f;
				effectDone = false;
				removedColor = false;
				immunity = 0.5f;
				hurtSoundPlayer.Go();
			}
		}

		private void Die()
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			if (!unit.data.healthHandler.willBeRewived)
			{
				hurtSoundPlayer.Go();
				if (MinecraftVFXHolder.setting_despawnRagdolls)
				{
					dead = true;
					Material val = new Material(Shader.Find("Standard"));
					val.color = Color.red;
					val.SetFloat("_Glossiness", 0f);
					myColor.SetMaterial(val);
					DespawnAfterSeconds despawnAfterSeconds = ((Component)unit).gameObject.AddComponent<DespawnAfterSeconds>();
					despawnAfterSeconds.seconds = 0.7f;
					despawnAfterSeconds.objectToSpawn = MinecraftVFXHolder.effect_deathPoof;
					Object.Destroy((Object)(object)this);
				}
				else
				{
					HurtEffect();
				}
			}
		}

		public void Init()
		{
			unit = ((Component)this).GetComponent<Unit>();
			myColor = ((Component)((Component)this).transform.root).GetComponentInChildren<UnitColorHandler>();
			health = ((Component)unit).GetComponentInChildren<HealthHandler>();
			if ((Object)(object)myColor == (Object)null || (Object)(object)health == (Object)null)
			{
				Object.Destroy((Object)(object)this);
			}
			health.AssignDamageAction((Action)HurtEffect);
			health.AddDieAction((Action)Die);
			hurtSoundPlayer = ((Component)unit.data.torso).gameObject.AddComponent<PlaySoundEffect>();
			hurtSoundPlayer.PlayOnAwake = false;
			hurtSoundPlayer.soundPathData = hitSound;
			fallSoundPlayer = ((Component)unit.data.torso).gameObject.AddComponent<PlaySoundEffect>();
		}

		private void Update()
		{
			if (dead || !started)
			{
				return;
			}
			if (immunity > 0f)
			{
				immunity -= Time.deltaTime;
			}
			currentTime += Time.deltaTime;
			currentColorValue = Mathf.Lerp(currentColorValue, colorCurve.Evaluate(currentTime), Time.deltaTime * 5f);
			if (Object.op_Implicit((Object)(object)myColor) && currentTime < effectDuration && !effectDone)
			{
				myColor.SetColor(color, colorCurve.Evaluate(currentTime));
			}
			if (currentTime >= effectDuration)
			{
				effectDone = true;
			}
			if (!effectDone || removedColor)
			{
				return;
			}
			for (int i = 0; i < myColor.colors.Count; i++)
			{
				if (color.colorName == myColor.colors[i].colorName)
				{
					myColor.colors.RemoveAt(i);
				}
			}
			removedColor = true;
		}

		public void FallEffect(bool hard)
		{
			fallSoundPlayer.soundPathData = (hard ? fallSoundHard : fallSound);
			fallSoundPlayer.Validate();
			fallSoundPlayer.Go();
			Debug.Log((object)"Fall runs");
		}

		public override void OnEnterBattleState()
		{
			started = true;
		}

		public override void OnEnterPlacementState()
		{
		}

		public void UpdateDamage(float dmg)
		{
			recievedDamage = dmg;
		}

		private void OLD_Update()
		{
			if (!dead && started)
			{
				if (immunity > 0f)
				{
					immunity -= Time.deltaTime;
				}
				amount -= Time.deltaTime;
				if (amount >= 0f && Object.op_Implicit((Object)(object)myColor))
				{
					myColor.SetColor(color, colorCurve.Evaluate(1f - amount));
				}
			}
		}
	}
}
namespace Terren.MinecraftDamageVisuals.Harmony
{
	[HarmonyPatch(typeof(Unit), "InitializeUnit")]
	internal class AddMinecraftHurt
	{
		public static bool Prefix(Unit __instance, Team team)
		{
			//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_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			__instance.Team = team;
			__instance.dead = false;
			__instance.dataHandler.team = team;
			((Component)__instance).gameObject.AddComponent<MinecraftUnit>().Init();
			return false;
		}
	}
	[HarmonyPatch(typeof(DataHandler), "TouchGround")]
	internal class TouchGroundFallSound
	{
		public static bool Prefix(DataHandler __instance, Vector3 collisionPoint, Vector3 normal, Rigidbody rig = null)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.sinceGrounded > 1f && Object.op_Implicit((Object)(object)__instance.mainRig) && __instance.mainRig.velocity.y < -5f)
			{
				MinecraftUnit componentInParent = ((Component)__instance).GetComponentInParent<MinecraftUnit>();
				if ((Object)(object)componentInParent != (Object)null)
				{
					componentInParent.FallEffect(__instance.sinceGrounded > 2f);
				}
			}
			return true;
		}
	}
}
namespace Terren.MinecraftDamageVisuals
{
	public class MinecraftVFXHolder
	{
		public static GameObject effect_deathPoof;

		public static bool setting_dontDropWeapons;

		public static bool setting_despawnRagdolls;

		public MinecraftVFXHolder()
		{
			effect_deathPoof = Init_MinecraftHurt.assetBundle.LoadAsset<GameObject>("E_MinecraftDeathPart");
		}

		public static void WeaponDropSetting(int v)
		{
			Debug.Log((object)("[MCHurtVisuals] setting DontDropWeapons is set to " + ((v != 0) ? "true" : "false")));
			setting_dontDropWeapons = v != 0;
			Init_MinecraftHurt.ConfigDontDropWeapons.Value = v != 0;
		}

		public static void RagdollDespawnSetting(int v)
		{
			Debug.Log((object)("[MCHurtVisuals] setting DontDespawnRagdolls is set to " + ((v != 0) ? "true" : "false")));
			setting_despawnRagdolls = v != 0;
			Init_MinecraftHurt.ConfigDespawnRagdolls.Value = v != 0;
		}
	}
	public class DespawnAfterSeconds : MonoBehaviour
	{
		public float seconds = 30f;

		private float counter;

		public GameObject objectToSpawn;

		public void Update()
		{
			counter += Time.deltaTime;
			if (counter > seconds)
			{
				Spawn();
				Object.Destroy((Object)(object)((Component)((Component)this).transform.root).gameObject);
			}
		}

		public void Spawn()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)objectToSpawn))
			{
				Object.Instantiate<GameObject>(objectToSpawn, ((Component)((Component)this).transform).GetComponentInChildren<DataHandler>().mainRig.position, Quaternion.identity);
			}
		}
	}
}
namespace Terren.MinecraftDamageVisuals.Harmony
{
	[HarmonyPatch(typeof(SkeletonDeathAction), "OnDeathAction")]
	internal class SkeletonDeathIgnore
	{
		public static bool Prefix(SkeletonDeathAction __instance)
		{
			return !MinecraftVFXHolder.setting_dontDropWeapons;
		}
	}
	[HarmonyPatch(typeof(RemoveJointsOnDeath), "Die")]
	internal class JointDeathIgnore
	{
		public static bool Prefix(RemoveJointsOnDeath __instance)
		{
			return !MinecraftVFXHolder.setting_dontDropWeapons;
		}
	}
	[HarmonyPatch(typeof(HoldingHandler), "LetGoOfAll")]
	internal class DissarmIgnore
	{
		public static bool Prefix(HoldingHandler __instance)
		{
			if (__instance.unit.data.Dead)
			{
				return !MinecraftVFXHolder.setting_dontDropWeapons;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(HealthHandler), "TakeDamage")]
	internal class UpdateLastDamage
	{
		public static bool Prefix(HealthHandler __instance, float damage, Vector3 direction, Unit damager = null, DamageType damageType = 0)
		{
			MinecraftUnit componentInParent = ((Component)__instance).GetComponentInParent<MinecraftUnit>();
			if ((Object)(object)componentInParent != (Object)null)
			{
				componentInParent.UpdateDamage(damage);
			}
			return true;
		}
	}
}