Decompiled source of VargheimWeaponsmithing v1.0.0

VargheimWeaponsmithing.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using VargheimProfessions;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace VargheimWeaponsmithing
{
	[BepInPlugin("digi.vargheim.weaponsmithing", "VargheimWeaponsmithing", "1.0.0")]
	[BepInDependency("digi.vargheim.professions", "0.4.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal sealed class Craft
		{
			public string Name;

			public HashSet<ItemData> Before;
		}

		[HarmonyPatch]
		internal static class Patches
		{
			[HarmonyPatch(typeof(Humanoid), "UseItem")]
			[HarmonyPrefix]
			private static bool UseItem(Humanoid __instance, Inventory inventory, ItemData item)
			{
				if (item == null || (Object)(object)item.m_dropPrefab == (Object)null || ((Object)item.m_dropPrefab).name != "VP_Whetstone" || (Object)(object)__instance != (Object)(object)Player.m_localPlayer)
				{
					return true;
				}
				try
				{
					Sharpened.m_ttl = StoneMinutes.Value * 60f;
					((Character)__instance).GetSEMan().AddStatusEffect(Sharpened, true, 0, 0f, (short)0);
					(inventory ?? __instance.GetInventory()).RemoveItem(item, 1);
					Api.Note("Your weapons are sharpened.");
				}
				catch (Exception ex)
				{
					Debug.LogWarning((object)("VargheimWeaponsmithing: " + ex.Message));
				}
				return false;
			}

			[HarmonyPatch(typeof(SEMan), "ModifyAttack")]
			[HarmonyPostfix]
			private static void ModifyAttack(SEMan __instance, ref HitData hitData)
			{
				try
				{
					float num = 1f;
					if (__instance.HaveStatusEffect(Sharpened.NameHash()))
					{
						num += StoneBonus.Value;
					}
					Character value = Traverse.Create((object)__instance).Field("m_character").GetValue<Character>();
					Humanoid val = (Humanoid)(object)((value is Humanoid) ? value : null);
					if (Tag(((Object)(object)val != (Object)null) ? val.GetCurrentWeapon() : null, "vp_masterwork") != null)
					{
						num += MasterBonus.Value;
					}
					if (num != 1f)
					{
						((DamageTypes)(ref hitData.m_damage)).Modify(num);
					}
				}
				catch
				{
				}
			}

			[HarmonyPatch(typeof(Attack), "GetAttackStamina")]
			[HarmonyPostfix]
			private static void Stamina(Attack __instance, ref float __result)
			{
				try
				{
					if (Tag(Traverse.Create((object)__instance).Field("m_weapon").GetValue<ItemData>(), "vp_balanced") != null)
					{
						__result *= 1f - BalanceCut.Value;
					}
				}
				catch
				{
				}
			}

			[HarmonyPatch(typeof(InventoryGui), "DoCrafting")]
			[HarmonyPrefix]
			private static void CraftPre(InventoryGui __instance, Player player, out Craft __state)
			{
				__state = null;
				try
				{
					Recipe value = Traverse.Create((object)__instance).Field("m_craftRecipe").GetValue<Recipe>();
					ItemData value2 = Traverse.Create((object)__instance).Field("m_craftUpgradeItem").GetValue<ItemData>();
					if (!((Object)(object)value == (Object)null) && !((Object)(object)value.m_item == (Object)null) && value2 == null && !((Object)(object)player != (Object)(object)Player.m_localPlayer) && IsWeapon(value.m_item.m_itemData) && value.m_item.m_itemData.m_shared.m_maxStackSize <= 1)
					{
						__state = new Craft
						{
							Name = value.m_item.m_itemData.m_shared.m_name,
							Before = new HashSet<ItemData>(((Humanoid)player).GetInventory().GetAllItems())
						};
					}
				}
				catch
				{
				}
			}

			[HarmonyPatch(typeof(InventoryGui), "DoCrafting")]
			[HarmonyPostfix]
			private static void CraftPost(Player player, Craft __state)
			{
				if (__state == null || (Object)(object)player == (Object)null)
				{
					return;
				}
				try
				{
					bool flag = Api.HasPerk("Weaponsmithing", BalanceLevel.Value);
					bool flag2 = Api.HasPerk("Weaponsmithing", MasterLevel.Value);
					if (!flag && !flag2)
					{
						return;
					}
					foreach (ItemData allItem in ((Humanoid)player).GetInventory().GetAllItems())
					{
						if (allItem != null && !__state.Before.Contains(allItem) && !(allItem.m_shared.m_name != __state.Name))
						{
							if (flag)
							{
								allItem.m_customData["vp_balanced"] = "1";
							}
							if (flag2)
							{
								allItem.m_customData["vp_masterwork"] = player.GetPlayerName();
							}
						}
					}
				}
				catch
				{
				}
			}
		}

		public const string Guid = "digi.vargheim.weaponsmithing";

		public const string Version = "1.0.0";

		private const string Prof = "Weaponsmithing";

		internal const string Stone = "VP_Whetstone";

		internal static ConfigEntry<int> StoneLevel;

		internal static ConfigEntry<int> BalanceLevel;

		internal static ConfigEntry<int> FieldLevel;

		internal static ConfigEntry<int> MasterLevel;

		internal static ConfigEntry<float> StoneBonus;

		internal static ConfigEntry<float> StoneMinutes;

		internal static ConfigEntry<float> BalanceCut;

		internal static ConfigEntry<float> MasterBonus;

		internal static ConfigEntry<string> StoneRecipe;

		internal static StatusEffect Sharpened;

		private void Awake()
		{
			//IL_0341: Unknown result type (might be due to invalid IL or missing references)
			//IL_0347: Expected O, but got Unknown
			//IL_0384: Unknown result type (might be due to invalid IL or missing references)
			//IL_038a: Expected O, but got Unknown
			//IL_03a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a6: Expected O, but got Unknown
			//IL_03e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e9: Expected O, but got Unknown
			//IL_03f3: Unknown result type (might be due to invalid IL or missing references)
			StoneLevel = ((BaseUnityPlugin)this).Config.Bind<int>("Perks", "WhetstonesLevel", 30, "Whetstones: level to craft them (anyone can use them).");
			StoneRecipe = ((BaseUnityPlugin)this).Config.Bind<string>("Perks", "WhetstoneRecipe", "Stone:2,Flint:2", "Materials for two whetstones (crafted at the workbench).");
			StoneBonus = ((BaseUnityPlugin)this).Config.Bind<float>("Perks", "WhetstoneDamageBonus", 0.1f, "Whetstone: extra weapon damage.");
			StoneMinutes = ((BaseUnityPlugin)this).Config.Bind<float>("Perks", "WhetstoneMinutes", 10f, "Whetstone: how long it lasts (real minutes).");
			BalanceLevel = ((BaseUnityPlugin)this).Config.Bind<int>("Perks", "BalancedMakeLevel", 50, "Balanced make level.");
			BalanceCut = ((BaseUnityPlugin)this).Config.Bind<float>("Perks", "BalancedMakeStaminaReduction", 0.1f, "Balanced make: weapons you craft use this much less stamina per swing.");
			FieldLevel = ((BaseUnityPlugin)this).Config.Bind<int>("Perks", "FieldSharpeningLevel", 70, "Field sharpening: repair the weapons in your inventory anywhere (button in the Professions window).");
			MasterLevel = ((BaseUnityPlugin)this).Config.Bind<int>("Perks", "MasterworkLevel", 90, "Masterwork level.");
			MasterBonus = ((BaseUnityPlugin)this).Config.Bind<float>("Perks", "MasterworkDamageBonus", 0.05f, "Masterwork: extra damage for weapons you craft.");
			Sharpened = ScriptableObject.CreateInstance<StatusEffect>();
			((Object)Sharpened).name = "VP_Sharpened";
			Sharpened.m_name = "Sharpened";
			Sharpened.m_tooltip = "+" + Mathf.RoundToInt(StoneBonus.Value * 100f) + "% weapon damage.";
			Sharpened.m_ttl = StoneMinutes.Value * 60f;
			Content.AddStatusEffect(Sharpened);
			Content.AddItem("Flint", "VP_Whetstone", "Whetstone", "A weaponsmith's sharpening stone. Use it for +" + Mathf.RoundToInt(StoneBonus.Value * 100f) + "% weapon damage for " + StoneMinutes.Value + " minutes.", (Action<SharedData>)delegate(SharedData s)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				s.m_itemType = (ItemType)2;
				s.m_maxStackSize = 20;
				s.m_weight = 0.5f;
				s.m_consumeStatusEffect = null;
				if (s.m_icons != null && s.m_icons.Length != 0)
				{
					Sharpened.m_icon = s.m_icons[0];
				}
			});
			Content.AddRecipe("VP_Whetstone", "piece_workbench", 1, 2, StoneRecipe.Value, "Weaponsmithing", StoneLevel.Value);
			Content.AddTooltip((Func<ItemData, string>)Tooltip);
			Content.AddAction("Weaponsmithing", FieldLevel.Value, "Field sharpening: repair my weapons", (Func<string>)FieldSharpen);
			Api.RegisterLadder("Weaponsmithing", "VargheimWeaponsmithing 1.0.0", "Crafts and upgrades modded weapons, shields, bows, crossbows and ammo by tier. Gains experience from crafting and upgrading them.", (Perk[])(object)new Perk[4]
			{
				new Perk(StoneLevel.Value, "Whetstones", "Craft whetstones: +" + Mathf.RoundToInt(StoneBonus.Value * 100f) + "% damage for " + StoneMinutes.Value + " minutes. Anyone can use them."),
				new Perk(BalanceLevel.Value, "Balanced make", "Weapons you craft use " + Mathf.RoundToInt(BalanceCut.Value * 100f) + "% less stamina per swing."),
				new Perk(FieldLevel.Value, "Field sharpening", "Repair the weapons in your inventory anywhere (button below)."),
				new Perk(MasterLevel.Value, "Masterwork", "Weapons you craft get +" + Mathf.RoundToInt(MasterBonus.Value * 100f) + "% damage and show \"Forged by <name>\".")
			});
			new Harmony("digi.vargheim.weaponsmithing").PatchAll(typeof(Patches));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"VargheimWeaponsmithing 1.0.0 loaded.");
		}

		internal static bool IsWeapon(ItemData i)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: 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_001c: Invalid comparison between Unknown and I4
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Invalid comparison between Unknown and I4
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Invalid comparison between Unknown and I4
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Invalid comparison between Unknown and I4
			if (i == null || i.m_shared == null)
			{
				return false;
			}
			ItemType itemType = i.m_shared.m_itemType;
			if ((int)itemType <= 14)
			{
				if (itemType - 3 <= 2 || (int)itemType == 14)
				{
					goto IL_0035;
				}
			}
			else if ((int)itemType == 19 || (int)itemType == 22)
			{
				goto IL_0035;
			}
			return false;
			IL_0035:
			return true;
		}

		private static string Tag(ItemData i, string key)
		{
			if (i == null || i.m_customData == null || !i.m_customData.TryGetValue(key, out var value))
			{
				return null;
			}
			return value;
		}

		private static string Tooltip(ItemData i)
		{
			string text = "";
			if (Tag(i, "vp_balanced") != null)
			{
				text = text + "<color=#ffd27a>Balanced</color>: " + Mathf.RoundToInt(BalanceCut.Value * 100f) + "% less stamina per swing";
			}
			string text2 = Tag(i, "vp_masterwork");
			if (text2 != null)
			{
				text = text + ((text.Length > 0) ? "\n" : "") + "<color=#ffd27a>Masterwork</color>: +" + Mathf.RoundToInt(MasterBonus.Value * 100f) + "% damage. Forged by " + text2;
			}
			return text;
		}

		private static string FieldSharpen()
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return null;
			}
			int num = 0;
			foreach (ItemData allItem in ((Humanoid)localPlayer).GetInventory().GetAllItems())
			{
				if (IsWeapon(allItem) && allItem.m_shared.m_useDurability && allItem.m_durability < allItem.GetMaxDurability())
				{
					allItem.m_durability = allItem.GetMaxDurability();
					num++;
				}
			}
			if (num != 0)
			{
				return "Sharpened and repaired " + num + " weapon" + ((num == 1) ? "" : "s") + ".";
			}
			return "Your weapons are already sharp.";
		}
	}
}