Decompiled source of Buildable Forge of Potential v1.0.1

BuildableForgeOfPotential.dll

Decompiled 2 hours ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("JotunnModStub")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("JotunnModStub")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BuildableForgeOfPotential;

[BepInPlugin("com.LynxMods.buildableforgeofpotential", "Buildable Forge of Potential", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
internal class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Character), "Damage")]
	private static class PreventForgeCharacterDamagePatch
	{
		private static bool Prefix(Character __instance, HitData hit)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			if (!_preventDamageConfig.Value)
			{
				return true;
			}
			if (hit == null || (Object)(object)hit.GetAttacker() != (Object)null)
			{
				return true;
			}
			return !IsNearForgeOfPotential(((Component)__instance).transform.position);
		}
	}

	[HarmonyPatch(typeof(Aoe), "OnEnable")]
	private static class PreventForgeAoeDamagePatch
	{
		private static void Postfix(Aoe __instance)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			if (_preventDamageConfig.Value && !((Object)(object)__instance == (Object)null) && IsNearForgeOfPotential(((Component)__instance).transform.position))
			{
				__instance.m_damage = default(DamageTypes);
				__instance.m_damagePerLevel = default(DamageTypes);
				__instance.m_toolTier = 0;
				__instance.m_attackForce = 0f;
				__instance.m_hitProps = false;
				__instance.m_hitCharacters = false;
				__instance.m_hitFriendly = false;
				__instance.m_hitOwner = false;
			}
		}
	}

	[HarmonyPatch(typeof(WearNTear), "Damage")]
	private static class PreventForgeStructureDamagePatch
	{
		private static bool Prefix(WearNTear __instance, HitData hit)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			if (!_preventDamageConfig.Value)
			{
				return true;
			}
			if (hit == null || (Object)(object)hit.GetAttacker() != (Object)null)
			{
				return true;
			}
			return !IsNearForgeOfPotential(((Component)__instance).transform.position);
		}
	}

	public const string PluginGUID = "com.LynxMods.buildableforgeofpotential";

	public const string PluginName = "Buildable Forge of Potential";

	public const string PluginVersion = "1.0.0";

	private static ConfigEntry<bool> _preventDamageConfig;

	private ConfigEntry<string> _prefabNameConfig;

	private ConfigEntry<string> _craftingStationConfig;

	private ConfigEntry<string> _recipeConfig;

	private readonly Harmony _harmony = new Harmony("com.LynxMods.buildableforgeofpotential");

	private void Awake()
	{
		CreateConfigValues();
		_harmony.PatchAll();
		PrefabManager.OnVanillaPrefabsAvailable += AddForgeOfPotentialPiece;
	}

	private void OnDestroy()
	{
		_harmony.UnpatchSelf();
	}

	private void CreateConfigValues()
	{
		//IL_002b: 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: Expected O, but got Unknown
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Expected O, but got Unknown
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Expected O, but got Unknown
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_0082: Expected O, but got Unknown
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Expected O, but got Unknown
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Expected O, but got Unknown
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Expected O, but got Unknown
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Expected O, but got Unknown
		((BaseUnityPlugin)this).Config.SaveOnConfigSet = true;
		_preventDamageConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "PreventUpgradeDamage", true, new ConfigDescription("Keeps the lightning visual effects on upgrade, but prevents damage to players, pets, and nearby structures.", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		_prefabNameConfig = ((BaseUnityPlugin)this).Config.Bind<string>("General", "TargetPrefab", "AncientUpgradeStation", new ConfigDescription("Internal prefab name to clone. Change only if testing sub-prefabs.", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		_craftingStationConfig = ((BaseUnityPlugin)this).Config.Bind<string>("Building", "RequiredStation", CraftingStations.ArtisanTable, new ConfigDescription("Crafting station required nearby to build (e.g., Workbench, Forge, ArtisanTable, or leave empty for none).", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		_recipeConfig = ((BaseUnityPlugin)this).Config.Bind<string>("Building", "Recipe", "Stone:40,BlackCore:5,SurtlingCore:10", new ConfigDescription("Comma-separated ItemPrefab:Amount pairs required to build.", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
	}

	private void AddForgeOfPotentialPiece()
	{
		//IL_011d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0122: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_015e: Expected O, but got Unknown
		//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e7: Expected O, but got Unknown
		//IL_0340: Unknown result type (might be due to invalid IL or missing references)
		//IL_0384: Unknown result type (might be due to invalid IL or missing references)
		//IL_0389: Unknown result type (might be due to invalid IL or missing references)
		//IL_0394: Unknown result type (might be due to invalid IL or missing references)
		//IL_039f: Unknown result type (might be due to invalid IL or missing references)
		//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_03dc: Expected O, but got Unknown
		//IL_03e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e7: Expected O, but got Unknown
		//IL_0210: Unknown result type (might be due to invalid IL or missing references)
		//IL_0215: Unknown result type (might be due to invalid IL or missing references)
		//IL_0222: Unknown result type (might be due to invalid IL or missing references)
		//IL_022a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0231: Unknown result type (might be due to invalid IL or missing references)
		//IL_023d: Expected O, but got Unknown
		PrefabManager.OnVanillaPrefabsAvailable -= AddForgeOfPotentialPiece;
		GameObject prefab = PrefabManager.Instance.GetPrefab(_prefabNameConfig.Value);
		if ((Object)(object)prefab == (Object)null)
		{
			Logger.LogError((object)("Could not find '" + _prefabNameConfig.Value + "'!"));
			return;
		}
		GameObject val = prefab;
		Transform[] componentsInChildren = prefab.GetComponentsInChildren<Transform>(true);
		foreach (Transform val2 in componentsInChildren)
		{
			if ((Object)(object)val2 != (Object)(object)prefab.transform && (((Component)val2).GetComponent<Interactable>() != null || ((Component)val2).GetComponent<Hoverable>() != null))
			{
				val = ((Component)val2).gameObject;
				break;
			}
		}
		GameObject val3 = PrefabManager.Instance.CreateClonedPrefab("Player_ForgeOfPotential", val);
		Location[] componentsInChildren2 = val3.GetComponentsInChildren<Location>(true);
		for (int i = 0; i < componentsInChildren2.Length; i++)
		{
			Object.DestroyImmediate((Object)(object)componentsInChildren2[i]);
		}
		TerrainModifier[] componentsInChildren3 = val3.GetComponentsInChildren<TerrainModifier>(true);
		for (int i = 0; i < componentsInChildren3.Length; i++)
		{
			Object.DestroyImmediate((Object)(object)componentsInChildren3[i]);
		}
		if ((Object)(object)val3.GetComponent<ZNetView>() == (Object)null)
		{
			val3.AddComponent<ZNetView>().m_persistent = true;
		}
		Sprite icon = RenderManager.Instance.Render(new RenderRequest(val3)
		{
			Rotation = RenderManager.IsometricRotation,
			FieldOfView = 35f,
			DistanceMultiplier = 1.1f,
			Width = 128,
			Height = 128
		});
		List<RequirementConfig> list = new List<RequirementConfig>();
		List<Requirement> list2 = new List<Requirement>();
		string[] array = _recipeConfig.Value.Split(new char[1] { ',' });
		for (int i = 0; i < array.Length; i++)
		{
			string[] array2 = array[i].Trim().Split(new char[1] { ':' });
			if (array2.Length == 2 && int.TryParse(array2[1], out var result) && result > 0)
			{
				string text = array2[0].Trim();
				list.Add(new RequirementConfig(text, result, 0, true));
				GameObject prefab2 = PrefabManager.Instance.GetPrefab(text);
				if ((Object)(object)prefab2 != (Object)null && (Object)(object)prefab2.GetComponent<ItemDrop>() != (Object)null)
				{
					list2.Add(new Requirement
					{
						m_resItem = prefab2.GetComponent<ItemDrop>(),
						m_amount = result,
						m_amountPerLevel = 0,
						m_recover = true
					});
				}
			}
		}
		Piece val4 = val3.GetComponent<Piece>();
		if ((Object)(object)val4 == (Object)null)
		{
			val4 = val3.AddComponent<Piece>();
		}
		val4.m_name = "Forge of Potential";
		val4.m_description = "Refines equipment beyond its normal limits using Idols.";
		val4.m_groundOnly = false;
		val4.m_canBeRemoved = true;
		val4.m_clipGround = false;
		val4.m_resources = list2.ToArray();
		val4.m_icon = icon;
		GameObject prefab3 = PrefabManager.Instance.GetPrefab("forge");
		if ((Object)(object)prefab3 != (Object)null)
		{
			Piece component = prefab3.GetComponent<Piece>();
			if ((Object)(object)component != (Object)null)
			{
				if ((Object)(object)val4.m_icon == (Object)null)
				{
					val4.m_icon = component.m_icon;
				}
				val4.m_placeEffect = component.m_placeEffect;
			}
		}
		WearNTear val5 = val3.GetComponent<WearNTear>();
		if ((Object)(object)val5 == (Object)null)
		{
			val5 = val3.AddComponent<WearNTear>();
		}
		val5.m_health = 1000f;
		val5.m_noRoofWear = false;
		val5.m_noSupportWear = true;
		val5.m_materialType = (MaterialType)1;
		if ((Object)(object)prefab3 != (Object)null && (Object)(object)prefab3.GetComponent<WearNTear>() != (Object)null)
		{
			val5.m_destroyedEffect = prefab3.GetComponent<WearNTear>().m_destroyedEffect;
			val5.m_hitEffect = prefab3.GetComponent<WearNTear>().m_hitEffect;
		}
		PieceConfig val6 = new PieceConfig
		{
			Name = "Forge of Potential",
			Description = "Refines equipment beyond its normal limits using Idols.",
			PieceTable = PieceTables.Hammer,
			Category = PieceCategories.Crafting,
			CraftingStation = _craftingStationConfig.Value,
			Icon = icon,
			Requirements = list.ToArray()
		};
		CustomPiece val7 = new CustomPiece(val3, false, val6);
		PieceManager.Instance.AddPiece(val7);
		Logger.LogInfo((object)("Buildable Forge of Potential registered cleanly from '" + ((Object)val).name + "'!"));
	}

	private static bool IsNearForgeOfPotential(Vector3 position, float radius = 8f)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		Collider[] array = Physics.OverlapSphere(position, radius);
		for (int i = 0; i < array.Length; i++)
		{
			Transform root = ((Component)array[i]).transform.root;
			if ((Object)(object)root != (Object)null && (((Object)root).name.Contains("ForgeOfPotential") || ((Object)root).name.Contains("AncientUpgradeStation") || ((Object)root).name.Contains("UpgradeStation")))
			{
				return true;
			}
		}
		return false;
	}
}