Decompiled source of Shields v0.1.1

Shields.dll

Decompiled 6 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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 BelowTheStone;
using BelowTheStone.Animation;
using BelowTheStone.Crafting;
using BelowTheStone.InventorySystem;
using BelowTheStone.NewDatabase;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
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("Shields")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Shields")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("542DE3E1-64DA-42F8-AC30-0D02E37BDE1A")]
[assembly: AssemblyFileVersion("0.1.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.1.0")]
[module: UnverifiableCode]
namespace Shields;

public static class Mocks
{
	public static void FixMocks(ItemStack itemStack)
	{
		if (itemStack != null && Object.op_Implicit((Object)(object)itemStack.ItemType) && ((Object)itemStack.ItemType).name.StartsWith("Mock_"))
		{
			string key = ((Object)itemStack.ItemType).name.Substring(5);
			AccessTools.FieldRefAccess<Item, ItemType>(itemStack.Item, "itemType") = ItemPatches.vanillaItems[key];
		}
	}

	public static void FixMocks(Ingredient ingredient)
	{
		if (ingredient != null && Object.op_Implicit((Object)(object)ingredient.ItemType) && ((Object)ingredient.ItemType).name.StartsWith("Mock_"))
		{
			string key = ((Object)ingredient.ItemType).name.Substring(5);
			AccessTools.FieldRefAccess<Ingredient, ItemType>(ingredient, "itemType") = ItemPatches.vanillaItems[key];
		}
	}

	public static void FixMocks(ItemType itemType)
	{
		if ((Object)(object)itemType != (Object)null && Object.op_Implicit((Object)(object)itemType.EquipItemPrefab))
		{
			FixMocks(itemType.EquipItemPrefab);
		}
	}

	public static void FixMocks(GameObject gameObject)
	{
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Expected O, but got Unknown
		if (!Object.op_Implicit((Object)(object)gameObject))
		{
			return;
		}
		EquippedItem equippedItem = default(EquippedItem);
		if (gameObject.TryGetComponent<EquippedItem>(ref equippedItem))
		{
			FixMocks(equippedItem);
		}
		Renderer renderer = default(Renderer);
		if (gameObject.TryGetComponent<Renderer>(ref renderer))
		{
			FixMocks(renderer);
		}
		foreach (Transform item in gameObject.transform)
		{
			Transform val = item;
			FixMocks(((Component)val).gameObject);
		}
	}

	public static void FixMocks(Renderer renderer)
	{
		if (Object.op_Implicit((Object)(object)renderer) && Object.op_Implicit((Object)(object)renderer.sharedMaterial) && ((Object)renderer.sharedMaterial).name.StartsWith("Mock_"))
		{
			string materialName = ((Object)renderer.sharedMaterial).name.Substring(5);
			if (materialName.EndsWith(" (Instance)"))
			{
				materialName = materialName.Substring(0, materialName.Length - 11);
			}
			Material val = Resources.FindObjectsOfTypeAll<Material>().First((Material mat) => ((Object)mat).name == materialName);
			AccessTools.PropertySetter(typeof(Renderer), "sharedMaterial").Invoke(renderer, new object[1] { val });
		}
	}

	public static void FixMocks(EquippedItem equippedItem)
	{
		if (Object.op_Implicit((Object)(object)equippedItem))
		{
			if (Object.op_Implicit((Object)(object)equippedItem.ItemTypeData) && ((Object)equippedItem.ItemTypeData).name.StartsWith("Mock_"))
			{
				string key = ((Object)equippedItem.ItemTypeData).name.Substring(5);
				AccessTools.FieldRefAccess<EquippedItem, ItemType>(equippedItem, "itemTypeData") = ItemPatches.vanillaItems[key];
			}
			if (Object.op_Implicit((Object)(object)equippedItem.HandAnimations) && ((Object)equippedItem.HandAnimations).name.StartsWith("Mock_"))
			{
				string key2 = ((Object)equippedItem.HandAnimations).name.Substring(5);
				AccessTools.FieldRefAccess<EquippedItem, RuntimeAnimatorController>(equippedItem, "handAnimations") = ItemPatches.vanillaAnimations[key2];
			}
		}
	}
}
[HarmonyPatch]
public static class ItemPatches
{
	public static Dictionary<string, ItemType> vanillaItems = new Dictionary<string, ItemType>();

	public static Dictionary<string, RuntimeAnimatorController> vanillaAnimations = new Dictionary<string, RuntimeAnimatorController>();

	[HarmonyPatch(typeof(SODatabase), "Init", new Type[] { })]
	[HarmonyPrefix]
	public static void SODatabaseInit(SODatabase __instance)
	{
		EquippedItem val2 = default(EquippedItem);
		foreach (DatabaseElement master in __instance.MasterList)
		{
			ItemType val = (ItemType)(object)((master is ItemType) ? master : null);
			if (val != null)
			{
				vanillaItems[((DatabaseElement)val).NameID] = val;
				if (Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)val.EquipItemPrefab) && val.EquipItemPrefab.TryGetComponent<EquippedItem>(ref val2) && Object.op_Implicit((Object)(object)val2.HandAnimations))
				{
					vanillaAnimations[((Object)val2.HandAnimations).name] = val2.HandAnimations;
				}
			}
		}
		AssetBundle val3 = LoadAssetBundleFromResources("shields");
		ShieldItemType shieldItemType = val3.LoadAsset<ShieldItemType>("MS_shield_tin");
		Mocks.FixMocks((ItemType)(object)shieldItemType);
		__instance.MasterList.Add((DatabaseElement)(object)shieldItemType);
		__instance.MasterList.Add((DatabaseElement)(object)val3.LoadAsset<CraftingRecipe>("MS_shield_tin_recipe"));
		ShieldItemType shieldItemType2 = val3.LoadAsset<ShieldItemType>("MS_shield_copper");
		Mocks.FixMocks((ItemType)(object)shieldItemType2);
		__instance.MasterList.Add((DatabaseElement)(object)shieldItemType2);
		__instance.MasterList.Add((DatabaseElement)(object)val3.LoadAsset<CraftingRecipe>("MS_shield_copper_recipe"));
		ShieldItemType shieldItemType3 = val3.LoadAsset<ShieldItemType>("MS_shield_iron");
		Mocks.FixMocks((ItemType)(object)shieldItemType3);
		__instance.MasterList.Add((DatabaseElement)(object)shieldItemType3);
		__instance.MasterList.Add((DatabaseElement)(object)val3.LoadAsset<CraftingRecipe>("MS_shield_iron_recipe"));
	}

	[HarmonyPatch(typeof(CraftingRecipe), "OnEnable")]
	[HarmonyPrefix]
	public static void CraftingRecipeOnEnable(CraftingRecipe __instance)
	{
		Mocks.FixMocks(__instance.OutputItem);
		foreach (Ingredient ingredient in __instance.Ingredients)
		{
			Mocks.FixMocks(ingredient);
		}
	}

	public static AssetBundle LoadAssetBundleFromResources(string bundleName)
	{
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		string name = executingAssembly.GetManifestResourceNames().Single((string str) => str.EndsWith(bundleName));
		using Stream stream = executingAssembly.GetManifestResourceStream(name);
		return AssetBundle.LoadFromStream(stream);
	}
}
[HarmonyPatch]
public static class PlayerPatches
{
	private static Dictionary<ClothingVisualController, SpriteRenderer> shieldRenderer = new Dictionary<ClothingVisualController, SpriteRenderer>();

	private static Dictionary<ClothingVisualController, ClothingItem> shieldItem = new Dictionary<ClothingVisualController, ClothingItem>();

	private static bool shieldActive = false;

	[HarmonyPatch(typeof(PlayerObject), "Update")]
	[HarmonyPostfix]
	private static void PlayerObjectUpdate(PlayerObject __instance)
	{
		shieldActive = Input.GetMouseButton(1);
	}

	[HarmonyPatch(typeof(PlayerObject), "FixedUpdate")]
	[HarmonyPostfix]
	private static void PlayerObjectUpdate2(PlayerObject __instance)
	{
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Expected O, but got Unknown
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		if (shieldActive && Object.op_Implicit((Object)(object)__instance) && Object.op_Implicit((Object)(object)__instance.mover))
		{
			ArmorInventory armorInventory = __instance.PlayerInventory.ArmorInventory;
			InventorySlot accessorySlot = armorInventory.GetAccessorySlot(-1);
			object obj;
			if (accessorySlot == null)
			{
				obj = null;
			}
			else
			{
				ItemStack itemStack = accessorySlot.ItemStack;
				obj = ((itemStack != null) ? itemStack.ItemType : null);
			}
			ShieldItemType shieldItemType = ((ClothingItem)obj) as ShieldItemType;
			if (Object.op_Implicit((Object)(object)shieldItemType))
			{
				CharacterMover mover = __instance.mover;
				((VelocityMover)mover).TargetVelocity = ((VelocityMover)mover).TargetVelocity / 3f;
			}
		}
	}

	[HarmonyPatch(typeof(DamageReciever), "CalculateNetDamage")]
	[HarmonyPostfix]
	private static void DamageRecieverCalculateNetDamage(DamageReciever __instance, ref int __result)
	{
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Expected O, but got Unknown
		if (shieldActive && Object.op_Implicit((Object)(object)__instance) && Object.op_Implicit((Object)(object)__instance.plyInvScr))
		{
			ArmorInventory armorInventory = __instance.plyInvScr.PlayerInventory.ArmorInventory;
			InventorySlot accessorySlot = armorInventory.GetAccessorySlot(-1);
			object obj;
			if (accessorySlot == null)
			{
				obj = null;
			}
			else
			{
				ItemStack itemStack = accessorySlot.ItemStack;
				obj = ((itemStack != null) ? itemStack.ItemType : null);
			}
			ShieldItemType shieldItemType = ((ClothingItem)obj) as ShieldItemType;
			if (Object.op_Implicit((Object)(object)shieldItemType))
			{
				__result = CalculateDamageAfterShield(shieldItemType.BlockValue, __result);
			}
		}
	}

	private static int CalculateDamageAfterShield(int block, int damage)
	{
		return Mathf.FloorToInt(Mathf.Max(0f, (float)damage * (1f / ((float)block * 0.1f + 1f)) - (float)block));
	}

	[HarmonyPatch(typeof(DamageReciever), "DealDamage", new Type[] { typeof(DamageInfo) })]
	[HarmonyPostfix]
	private static void DamageRecieverDealDamage1(DamageReciever __instance, DamageInfo damageInfo)
	{
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Expected O, but got Unknown
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		if (shieldActive && Object.op_Implicit((Object)(object)__instance) && Object.op_Implicit((Object)(object)__instance.plyInvScr))
		{
			ArmorInventory armorInventory = __instance.plyInvScr.PlayerInventory.ArmorInventory;
			InventorySlot accessorySlot = armorInventory.GetAccessorySlot(-1);
			object obj;
			if (accessorySlot == null)
			{
				obj = null;
			}
			else
			{
				ItemStack itemStack = accessorySlot.ItemStack;
				obj = ((itemStack != null) ? itemStack.ItemType : null);
			}
			ShieldItemType shieldItemType = ((ClothingItem)obj) as ShieldItemType;
			if (Object.op_Implicit((Object)(object)shieldItemType))
			{
				int block = (int)damageInfo.baseDamage - CalculateDamageAfterShield(shieldItemType.BlockValue, (int)damageInfo.baseDamage);
				EmitBlockParticle(((Component)__instance).transform.position, block);
			}
		}
	}

	public static void EmitBlockParticle(Vector3 position, int block)
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)GameState.current.gameDefines.damageIndicator))
		{
			DamageParticle component = Object.Instantiate<GameObject>(GameState.current.gameDefines.damageIndicator).GetComponent<DamageParticle>();
			((Component)component).transform.position = position + new Vector3(0f, 0.1f, 0.1f);
			component.InitParticle(block.ToString());
			((TMP_Text)component.textMesh).colorGradient = new VertexGradient(Color.white, Color.gray, Color.gray, Color.gray);
		}
	}

	[HarmonyPatch(typeof(ClothingVisualController), "Start")]
	[HarmonyPostfix]
	private static void ClothingVisualControllerStart(ClothingVisualController __instance)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		shieldRenderer[__instance] = new GameObject("Shield").AddComponent<SpriteRenderer>();
		((Component)shieldRenderer[__instance]).transform.SetParent(((Component)__instance).transform);
		((Component)shieldRenderer[__instance]).transform.localPosition = new Vector3(0.3f, 0.15f, 0f);
		((Component)shieldRenderer[__instance]).transform.localRotation = Quaternion.identity;
		((Component)shieldRenderer[__instance]).transform.localScale = Vector3.one;
		((Renderer)shieldRenderer[__instance]).sortingOrder = 2;
		shieldItem[__instance] = null;
	}

	[HarmonyPatch(typeof(ClothingVisualController), "SetClothingItem")]
	[HarmonyPostfix]
	private static void ClothingVisualControllerSetClothingItem(ClothingVisualController __instance, ClothingBodyPart bodyPart, ClothingItem clothingItem)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Invalid comparison between Unknown and I4
		if ((int)bodyPart == 5)
		{
			shieldItem[__instance] = clothingItem;
		}
	}

	[HarmonyPatch(typeof(ClothingVisualController), "UpdateCharacterSprites")]
	[HarmonyPostfix]
	private static void ClothingVisualControllerUpdateCharacterSprites(ClothingVisualController __instance)
	{
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)shieldItem[__instance]) && Object.op_Implicit((Object)(object)shieldItem[__instance].ClothingSprites))
		{
			shieldRenderer[__instance].sprite = shieldItem[__instance].ClothingSprites.GetSprite(((SpriteAnimationVisualReciever)__instance).clipName, ((SpriteAnimationVisualReciever)__instance).spriteIndex);
		}
		else
		{
			shieldRenderer[__instance].sprite = null;
		}
		if (shieldActive)
		{
			((Component)shieldRenderer[__instance]).transform.localPosition = new Vector3(0.25f, 0.2f, 0f);
		}
		else
		{
			((Component)shieldRenderer[__instance]).transform.localPosition = new Vector3(0.2f, 0.15f, 0f);
		}
	}

	[HarmonyPatch(typeof(CharacterClothing), "Update")]
	[HarmonyPostfix]
	private static void CharacterClothingUpdate(CharacterClothing __instance)
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Expected O, but got Unknown
		ArmorInventory armorInventory = __instance.plyInv.PlayerInventory.ArmorInventory;
		InventorySlot accessorySlot = armorInventory.GetAccessorySlot(-1);
		object obj;
		if (accessorySlot == null)
		{
			obj = null;
		}
		else
		{
			ItemStack itemStack = accessorySlot.ItemStack;
			obj = ((itemStack != null) ? itemStack.ItemType : null);
		}
		ClothingItem val = (ClothingItem)obj;
		__instance.clothingVis.SetClothingItem((ClothingBodyPart)5, val);
	}
}
[BepInPlugin("com.maxsch.BelowTheStone.Shields", "Shields", "0.1.1")]
public class Plugin : BaseUnityPlugin
{
	public const string PluginGUID = "com.maxsch.BelowTheStone.Shields";

	public const string PluginName = "Shields";

	public const string PluginVersion = "0.1.1";

	public static ManualLogSource Log { get; private set; }

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		Harmony val = new Harmony("com.maxsch.BelowTheStone.Shields");
		val.PatchAll();
		Log = ((BaseUnityPlugin)this).Logger;
	}
}
[CreateAssetMenu(fileName = "New Shield Item Type", menuName = "Below The Stone/Database Objects/Shield Item Type")]
public class ShieldItemType : ClothingItem
{
	[SerializeField]
	private int blockValue;

	public int BlockValue => blockValue;

	public override string Stats => $"Block: {BlockValue}";
}