Decompiled source of RestInPieces v1.0.4

RestInPieces.dll

Decompiled 10 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.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using DM;
using Functions;
using HarmonyLib;
using Landfall.TABS;
using Landfall.TABS.AI;
using RestInPieces;
using TFBGames;
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("RestInPieces")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("GeeztJeez")]
[assembly: AssemblyProduct("RestInPieces")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[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.4.0")]
[module: UnverifiableCode]
namespace RestInPieces
{
	[BepInPlugin("GeeztJeez.RestInPieces", "RestInPieces", "1.0.4")]
	internal class Loader : BaseUnityPlugin
	{
		private static ConfigEntry<float> configPiecesDuration;

		private static ConfigEntry<bool> configEnableMod;

		private static ConfigEntry<float> configPiecesChance;

		public static bool Enabled => configEnableMod.Value;

		public static float Duration => configPiecesDuration.Value;

		public static float Chances => configPiecesChance.Value;

		private void Awake()
		{
			((MonoBehaviour)this).StartCoroutine("Call");
		}

		private IEnumerator Call()
		{
			yield return (object)new WaitUntil((Func<bool>)(() => (Object)(object)Object.FindObjectOfType<ServiceLocator>() != (Object)null));
			yield return (object)new WaitUntil((Func<bool>)(() => ServiceLocator.GetService<ISaveLoaderService>() != null));
			Debug.Log((object)"Loading RestInPieces...");
			SLMALoader.GetInstance();
			_ = ContentDatabase.Instance().LandfallContentDatabase;
			new Harmony("RestInPieces").PatchAll();
			GameObject[] array = Resources.FindObjectsOfTypeAll<GameObject>();
			for (int i = 0; i < array.Length; i++)
			{
				if (Object.op_Implicit((Object)(object)array[i].GetComponentInChildren<Unit>()))
				{
					array[i].gameObject.AddComponent<GoreMechanic>();
				}
			}
			DoConfig();
			((MonoBehaviour)this).StartCoroutine(GenerateSettings());
			Debug.Log((object)"Loaded RestInPieces 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();
			SettingsInstance val2 = new SettingsInstance();
			val2.settingsType = (SettingsType)0;
			val2.options = new string[2] { "On", "Off" };
			val2.currentValue = ((!configEnableMod.Value) ? 1 : 0);
			val2.m_settingsKey = "Use Rest In Pieces";
			val2.toolTip = "Enables Rest In Pieces mod.";
			SettingsInstance val3 = val2;
			val3.OnValueChanged += delegate(int val)
			{
				configEnableMod.Value = val == 0;
			};
			list.Add(val3);
			SettingsInstance val4 = new SettingsInstance
			{
				currentSliderValue = configPiecesDuration.Value,
				defaultSliderValue = 1f,
				max = 5f,
				min = 0.1f,
				m_settingsKey = "Pieces Duration",
				settingsType = (SettingsType)1,
				toolTip = "Determines the duration of body parts pieces in battlefield. (default: 1)"
			};
			val4.OnSliderValueChanged += delegate(float val)
			{
				configPiecesDuration.Value = val;
			};
			list.Add(val4);
			SettingsInstance val5 = new SettingsInstance
			{
				currentSliderValue = configPiecesChance.Value,
				defaultSliderValue = 25f,
				max = 100f,
				min = 0f,
				m_settingsKey = "Body Explosion Chances",
				settingsType = (SettingsType)1,
				toolTip = "Determines the chance of body parts pieces explodes in battlefield. (default: 25)"
			};
			val5.OnSliderValueChanged += delegate(float val)
			{
				configPiecesChance.Value = val;
			};
			list.Add(val5);
			((object)service).GetType().GetField("m_gameplaySettings", (BindingFlags)(-1)).SetValue(service, list.ToArray());
		}

		private void DoConfig()
		{
			configPiecesChance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Chance", 25f, "Determines the change of body parts explosion");
			configPiecesDuration = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Duration", 1f, "Determines the duration of pieces");
			configEnableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable Rest In Pieces Mod");
		}
	}
}
namespace Functions
{
	public class GoreMechanic : MonoBehaviour
	{
		public Unit unit;

		public bool isIceGiant;

		public void Start()
		{
			if (Loader.Enabled)
			{
				unit = ((Component)((Component)this).gameObject.transform.root).GetComponentInChildren<Unit>();
				if (Object.op_Implicit((Object)(object)((Component)unit).GetComponentInChildren<IceGiant>()))
				{
					isIceGiant = true;
				}
			}
		}

		public void Update()
		{
			if (Loader.Enabled && unit.data.Dead)
			{
				Weapon[] componentsInChildren = ((Component)unit).GetComponentsInChildren<Weapon>();
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					((Component)componentsInChildren[i]).gameObject.transform.SetParent((Transform)null);
				}
				if (Random.Range(0f, 100f) <= Loader.Chances)
				{
					BodyExplodes(unit.unitBlueprint);
				}
				Object.Destroy((Object)(object)this);
			}
		}

		private void SpawnBodyPart(string exclusion)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)unit))
			{
				return;
			}
			GameObject[] array = GetCorpseBlueprint(unit.unitBlueprint).Spawn(unit.data.mainRig.position, unit.data.mainRig.rotation, unit.Team, 1f, (UnitPoolInfo?)null);
			ConfigurableJoint val3 = default(ConfigurableJoint);
			foreach (GameObject val in array)
			{
				CleanUp(val);
				SetParent[] componentsInChildren = val.GetComponentsInChildren<SetParent>();
				foreach (SetParent val2 in componentsInChildren)
				{
					if (!((Object)((Component)val2).gameObject).name.Contains(exclusion))
					{
						Transform transform = ((Component)val2).gameObject.transform;
						transform.localScale *= 0f;
						if (((Component)((Component)val2).transform.parent).gameObject.TryGetComponent<ConfigurableJoint>(ref val3))
						{
							Transform transform2 = ((Component)((Component)val2).transform.parent).gameObject.transform;
							transform2.localScale *= 0f;
						}
					}
				}
			}
		}

		private void SpawnBodyPartMammoth(string exclusion)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)unit))
			{
				return;
			}
			GameObject[] array = GetCorpseBlueprint(unit.unitBlueprint).Spawn(unit.data.mainRig.position, unit.data.mainRig.rotation, unit.Team, 1f, (UnitPoolInfo?)null);
			ConfigurableJoint val3 = default(ConfigurableJoint);
			foreach (GameObject val in array)
			{
				CleanUp(val);
				SetParent[] componentsInChildren = val.GetComponentsInChildren<SetParent>();
				foreach (SetParent val2 in componentsInChildren)
				{
					if (((Object)((Component)val2).gameObject).name.Contains("M_") && !((Object)((Component)val2).gameObject).name.Contains(exclusion))
					{
						Transform transform = ((Component)val2).gameObject.transform;
						transform.localScale *= 0f;
						if (((Component)((Component)val2).transform.parent).gameObject.TryGetComponent<ConfigurableJoint>(ref val3))
						{
							Transform transform2 = ((Component)((Component)val2).transform.parent).gameObject.transform;
							transform2.localScale *= 0f;
						}
					}
				}
				if (!(exclusion != "M_Head"))
				{
					continue;
				}
				Head[] componentsInChildren2 = val.GetComponentsInChildren<Head>();
				foreach (Head val4 in componentsInChildren2)
				{
					if (((Object)((Component)val4).gameObject).name.Contains("Bete"))
					{
						Transform transform3 = ((Component)val4).gameObject.transform;
						transform3.localScale *= 0f;
					}
				}
			}
		}

		public void BodyExplodes(UnitBlueprint unitBlueprint)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Invalid comparison between Unknown and I4
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Invalid comparison between Unknown and I4
			if ((int)unit.RigType == 3)
			{
				SetParent[] componentsInChildren = ((Component)unit).GetComponentsInChildren<SetParent>();
				foreach (SetParent val in componentsInChildren)
				{
					if (((Object)((Component)val).gameObject).name.Contains("M_"))
					{
						SpawnBodyPartMammoth(((Object)((Component)val).gameObject).name);
					}
				}
				Object.Destroy((Object)(object)((Component)unit).gameObject);
			}
			else
			{
				if (isIceGiant || unitBlueprint.Name.Contains("RAPTOR") || (int)unit.unitType == 1)
				{
					return;
				}
				SetParent[] componentsInChildren = ((Component)unit).GetComponentsInChildren<SetParent>();
				foreach (SetParent val2 in componentsInChildren)
				{
					if (((Object)((Component)val2).gameObject).name.Contains("M_"))
					{
						SpawnBodyPart(((Object)((Component)val2).gameObject).name);
					}
				}
				Object.Destroy((Object)(object)((Component)unit).gameObject);
			}
		}

		private void CleanUp(GameObject gameObject)
		{
			gameObject.AddComponent<CleanUp>();
			gameObject.AddComponent<KillAfterSeconds>().seconds = 0.1f;
			gameObject.AddComponent<SinkOnDeath>().time = Loader.Duration;
			GooglyEye[] componentsInChildren = gameObject.GetComponentsInChildren<GooglyEye>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].SetState((EyeState)2);
			}
			GoreMechanic componentInChildren = gameObject.GetComponentInChildren<GoreMechanic>();
			if (Object.op_Implicit((Object)(object)componentInChildren))
			{
				Object.Destroy((Object)(object)componentInChildren);
			}
			PlacementSpawnEffects componentInChildren2 = gameObject.GetComponentInChildren<PlacementSpawnEffects>();
			if (Object.op_Implicit((Object)(object)componentInChildren2))
			{
				Object.Destroy((Object)(object)((Component)componentInChildren2).gameObject);
			}
			UnitSounds componentInChildren3 = gameObject.GetComponentInChildren<UnitSounds>();
			if (Object.op_Implicit((Object)(object)componentInChildren3))
			{
				Object.Destroy((Object)(object)((Component)componentInChildren3).gameObject);
			}
		}

		private UnitBlueprint GetCorpseBlueprint(UnitBlueprint baseBlueprint)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: 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_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			return new UnitBlueprint((UnitBlueprint)SLMALoader.SDic["blueprints"]["UNIT_TRIBAL_CLUB"])
			{
				health = 0f,
				Mount = null,
				LeftWeapon = null,
				RightWeapon = null,
				holdinigWithTwoHands = false,
				excludeFromWinCondition = true,
				objectsToSpawnAsChildren = null,
				m_props = baseBlueprint.m_props,
				UnitBase = baseBlueprint.UnitBase
			};
		}
	}
	public class CleanUp : MonoBehaviour
	{
		private Unit unit;

		public void Start()
		{
			unit = ((Component)this).gameObject.GetComponentInChildren<Unit>();
		}

		public void Update()
		{
			if (Object.op_Implicit((Object)(object)unit))
			{
				UnitAPI componentInChildren = ((Component)unit).GetComponentInChildren<UnitAPI>();
				if (Object.op_Implicit((Object)(object)componentInChildren))
				{
					Object.Destroy((Object)(object)componentInChildren);
				}
				unit.SetHealthBarActive(false);
				unit.data.health = 0f;
				unit.data.immunityForSeconds = 0f;
				unit.data.healthHandler.Die((Unit)null);
			}
		}
	}
}