Decompiled source of IronwoodStakes v1.0.0

BepInEx/plugins/IronwoodStakes/IronwoodStakes.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("IronwoodStakes")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("IronwoodStakes")]
[assembly: AssemblyTitle("IronwoodStakes")]
[assembly: AssemblyVersion("1.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 IronwoodStakes
{
	[BepInPlugin("marek.ironwoodstakes", "Ironwood Stakes", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		private const float BronzeMultiplier = 2f;

		private const float IronMultiplier = 3f;

		private void Awake()
		{
			AddLocalization();
			PrefabManager.OnVanillaPrefabsAvailable += AddStakes;
		}

		private void AddStakes()
		{
			PrefabManager.OnVanillaPrefabsAvailable -= AddStakes;
			AddVariant("piece_bronze_sharpstakes", "piece_sharpstakes", "Bronze", "Bronze", 4, "Wood", 6, 2f);
			AddVariant("piece_iron_sharpstakes", "piece_sharpstakes", "Iron", "Iron", 4, "CoreWood", 4, 3f);
			AddVariant("piece_bronze_stakewall", "stake_wall", "Bronze", "Bronze", 6, "Wood", 8, 2f);
			AddVariant("piece_iron_stakewall", "stake_wall", "Iron", "Iron", 6, "CoreWood", 6, 3f);
		}

		private static void AddVariant(string prefab, string source, string material, string itemA, int amountA, string itemB, int amountB, float multiplier)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: 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_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Expected O, but got Unknown
			PieceConfig val = new PieceConfig
			{
				Name = "$piece_" + prefab,
				Description = "$piece_" + prefab + "_description",
				PieceTable = "Hammer",
				Category = "Defence"
			};
			val.AddRequirement(itemA, amountA, true);
			val.AddRequirement(itemB, amountB, true);
			CustomPiece val2 = new CustomPiece(prefab, source, val);
			TintMaterials(val2.PiecePrefab, material);
			WearNTear component = val2.PiecePrefab.GetComponent<WearNTear>();
			if ((Object)(object)component != (Object)null)
			{
				component.m_health *= multiplier;
			}
			ScaleStakeDamage(val2.PiecePrefab, multiplier);
			PieceManager.Instance.AddPiece(val2);
		}

		private static void ScaleStakeDamage(GameObject root, float multiplier)
		{
			Component[] componentsInChildren = root.GetComponentsInChildren<Component>(true);
			foreach (Component val in componentsInChildren)
			{
				if ((Object)(object)val == (Object)null || ((object)val).GetType().Name != "Aoe")
				{
					continue;
				}
				FieldInfo field = ((object)val).GetType().GetField("m_damage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field == null)
				{
					continue;
				}
				if (field.FieldType == typeof(float))
				{
					field.SetValue(val, (float)field.GetValue(val) * multiplier);
					continue;
				}
				object value = field.GetValue(val);
				if (value == null)
				{
					continue;
				}
				bool flag = false;
				FieldInfo[] fields = value.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (FieldInfo fieldInfo in fields)
				{
					if (!(fieldInfo.FieldType != typeof(float)))
					{
						fieldInfo.SetValue(value, (float)fieldInfo.GetValue(value) * multiplier);
						flag = true;
					}
				}
				if (flag)
				{
					field.SetValue(val, value);
				}
			}
		}

		private static void TintMaterials(GameObject root, string metal)
		{
			//IL_00bd: 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)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			Renderer[] componentsInChildren = root.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val in componentsInChildren)
			{
				Material[] materials = val.materials;
				foreach (Material val2 in materials)
				{
					string text = ((Object)val2).name.ToLowerInvariant();
					bool flag = text.Contains("wood") || text.Contains("branch") || text.Contains("bark");
					if (!(metal == "Bronze" && flag))
					{
						Color val3 = ((metal == "Bronze") ? new Color(0.68f, 0.43f, 0.2f, 1f) : new Color(0.48f, 0.53f, 0.57f, 1f));
						if (val2.HasProperty("_Color"))
						{
							val2.color *= val3;
						}
						if (val2.HasProperty("_Metallic"))
						{
							val2.SetFloat("_Metallic", (metal == "Bronze") ? 0.55f : 0.82f);
						}
						if (val2.HasProperty("_Glossiness"))
						{
							val2.SetFloat("_Glossiness", 0.38f);
						}
					}
				}
			}
		}

		private void AddLocalization()
		{
			CustomLocalization localization = LocalizationManager.Instance.GetLocalization();
			string text = "English";
			localization.AddTranslation(ref text, new Dictionary<string, string>
			{
				["piece_piece_bronze_sharpstakes"] = "Bronze Sharp Stakes",
				["piece_piece_bronze_sharpstakes_description"] = "Wooden stakes capped with bronze. Twice the damage and durability.",
				["piece_piece_iron_sharpstakes"] = "Iron Sharp Stakes",
				["piece_piece_iron_sharpstakes_description"] = "Iron-clad stakes. Three times the damage and durability.",
				["piece_piece_bronze_stakewall"] = "Bronze Stakewall",
				["piece_piece_bronze_stakewall_description"] = "A bronze-reinforced wooden palisade.",
				["piece_piece_iron_stakewall"] = "Iron Stakewall",
				["piece_piece_iron_stakewall_description"] = "A heavy iron-clad palisade."
			});
			text = "Czech";
			localization.AddTranslation(ref text, new Dictionary<string, string>
			{
				["piece_piece_bronze_sharpstakes"] = "Bronzové pichláky",
				["piece_piece_bronze_sharpstakes_description"] = "Dřevěné kůly s bronzovými hroty. Dvojnásobné poškození a životnost.",
				["piece_piece_iron_sharpstakes"] = "Železné pichláky",
				["piece_piece_iron_sharpstakes_description"] = "Železem vyztužené kůly. Trojnásobné poškození a životnost.",
				["piece_piece_bronze_stakewall"] = "Bronzová palisáda",
				["piece_piece_bronze_stakewall_description"] = "Dřevěná palisáda zpevněná bronzem.",
				["piece_piece_iron_stakewall"] = "Železná palisáda",
				["piece_piece_iron_stakewall_description"] = "Masivní železná palisáda."
			});
		}
	}
}