Decompiled source of WarfareTweaksThrowingAxeStandalone v1.0.3

plugins\inam1069-WarfareTweaksThrowingAxeStandalone\WarfareTweaksThrowingAxeStandalone.dll

Decompiled 27 minutes ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[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 WarfareThrowingAxeStandalone
{
	[BepInPlugin("inam1069.WarfareTweaksThrowingAxeStandalone", "WarfareTweaksThrowingAxeStandalone", "1.0.3")]
	public class WarfareThrowingAxeStandalonePlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(ObjectDB), "Awake")]
		private static class ObjectDB_Awake_Patch
		{
			[HarmonyPostfix]
			[HarmonyPriority(0)]
			private static void Postfix(ObjectDB __instance)
			{
				ThrowingAxeUpgraderHelper.ApplyTierMatchedUpgraders(__instance, "ObjectDB.Awake");
			}
		}

		[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
		private static class ObjectDB_CopyOtherDB_Patch
		{
			[HarmonyPostfix]
			[HarmonyPriority(0)]
			private static void Postfix(ObjectDB __instance)
			{
				ThrowingAxeUpgraderHelper.ApplyTierMatchedUpgraders(__instance, "ObjectDB.CopyOtherDB");
			}
		}

		public const string PluginGuid = "inam1069.WarfareTweaksThrowingAxeStandalone";

		public const string PluginName = "WarfareTweaksThrowingAxeStandalone";

		public const string PluginVersion = "1.0.3";

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("inam1069.WarfareTweaksThrowingAxeStandalone");
			_harmony.PatchAll(typeof(WarfareThrowingAxeStandalonePlugin).Assembly);
			Log.LogInfo((object)"WarfareTweaksThrowingAxeStandalone v1.0.3 loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class ThrowingAxeUpgraderHelper
	{
		internal static void ApplyTierMatchedUpgraders(ObjectDB objectDb, string hookSource)
		{
			if ((Object)(object)objectDb == (Object)null || objectDb.m_recipes == null || objectDb.m_recipes.Count == 0)
			{
				return;
			}
			List<Recipe> list = objectDb.m_recipes.Where((Recipe r) => (Object)(object)r != (Object)null && (Object)(object)r.m_item != (Object)null && r.m_resources != null && r.m_resources.Any((Requirement req) => req != null && req.m_upgraderResource && (Object)(object)req.m_resItem != (Object)null)).ToList();
			if (list.Count == 0)
			{
				return;
			}
			int num = 0;
			foreach (Recipe recipe in objectDb.m_recipes)
			{
				if ((Object)(object)recipe == (Object)null || (Object)(object)recipe.m_item == (Object)null || (Object)(object)((Component)recipe.m_item).gameObject == (Object)null)
				{
					continue;
				}
				SharedData val = recipe.m_item.m_itemData?.m_shared;
				if (val == null)
				{
					continue;
				}
				string text = StripClone(((Object)((Component)recipe.m_item).gameObject).name);
				string recipeName = ((Object)recipe).name ?? string.Empty;
				string sharedName = val.m_name ?? string.Empty;
				if (text.IndexOf("projectile", StringComparison.OrdinalIgnoreCase) >= 0 || !IsWarfareItem(text, recipeName, sharedName) || !IsThrowingAxeItem(text, recipeName, sharedName, val))
				{
					continue;
				}
				Recipe val2 = recipe;
				if (val2.m_resources == null)
				{
					val2.m_resources = Array.Empty<Requirement>();
				}
				if (!recipe.m_resources.Any((Requirement r) => r?.m_upgraderResource ?? false))
				{
					Requirement val3 = FindMatchingTierUpgrader(recipe, text, list);
					if (val3 == null || (Object)(object)val3.m_resItem == (Object)null)
					{
						WarfareThrowingAxeStandalonePlugin.Log.LogWarning((object)("[ThrowAxeUpgrader][" + hookSource + "] '" + text + "'에 맞는 티어의 우상 템플릿을 찾지 못했습니다."));
						continue;
					}
					Requirement val4 = CloneRequirement(val3);
					List<Requirement> list2 = new List<Requirement>(recipe.m_resources.Length + 1);
					list2.AddRange(recipe.m_resources);
					list2.Add(val4);
					recipe.m_resources = list2.ToArray();
					num++;
					WarfareThrowingAxeStandalonePlugin.Log.LogInfo((object)$"[ThrowAxeUpgrader][{hookSource}] 티어 매칭 패치 완료: '{text}' -> '{StripClone(((Object)((Component)val4.m_resItem).gameObject).name)}' (amount={val4.m_amount}, perLevel={val4.m_amountPerLevel})");
				}
			}
			if (num > 0)
			{
				WarfareThrowingAxeStandalonePlugin.Log.LogInfo((object)$"[ThrowAxeUpgrader][{hookSource}] 총 {num}개 워페어 투척 도끼 티어별 우상 패치 완료.");
			}
		}

		private static Requirement FindMatchingTierUpgrader(Recipe targetRecipe, string targetPrefabName, List<Recipe> referenceRecipes)
		{
			HashSet<string> hashSet = new HashSet<string>(from r in targetRecipe.m_resources ?? Array.Empty<Requirement>()
				where r != null && !r.m_upgraderResource && (Object)(object)r.m_resItem != (Object)null
				select StripClone(((Object)((Component)r.m_resItem).gameObject).name), StringComparer.OrdinalIgnoreCase);
			string[] array = ExtractTierKeywords(targetPrefabName, hashSet);
			Recipe val = null;
			int num = 0;
			foreach (Recipe referenceRecipe in referenceRecipes)
			{
				string text = StripClone(((Object)((Component)referenceRecipe.m_item).gameObject).name);
				HashSet<string> hashSet2 = new HashSet<string>(from r in referenceRecipe.m_resources
					where r != null && !r.m_upgraderResource && (Object)(object)r.m_resItem != (Object)null
					select StripClone(((Object)((Component)r.m_resItem).gameObject).name), StringComparer.OrdinalIgnoreCase);
				int num2 = 0;
				if ((Object)(object)targetRecipe.m_craftingStation != (Object)null && (Object)(object)referenceRecipe.m_craftingStation == (Object)(object)targetRecipe.m_craftingStation)
				{
					num2 += 2;
				}
				string[] array2 = array;
				foreach (string text2 in array2)
				{
					if (hashSet2.Contains(text2))
					{
						num2 += 10;
					}
					if (text.IndexOf(text2, StringComparison.OrdinalIgnoreCase) >= 0)
					{
						num2 += 5;
					}
				}
				foreach (string item in hashSet)
				{
					if (hashSet2.Contains(item))
					{
						num2 += 3;
					}
				}
				if (num2 > num)
				{
					num = num2;
					val = referenceRecipe;
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				return ((IEnumerable<Requirement>)val.m_resources).FirstOrDefault((Func<Requirement, bool>)((Requirement r) => r != null && r.m_upgraderResource && (Object)(object)r.m_resItem != (Object)null));
			}
			return null;
		}

		private static string[] ExtractTierKeywords(string prefabName, HashSet<string> materialNames)
		{
			List<string> list = new List<string>();
			if (prefabName.IndexOf("Flint", StringComparison.OrdinalIgnoreCase) >= 0 || prefabName.IndexOf("Bronze", StringComparison.OrdinalIgnoreCase) >= 0 || materialNames.Contains("Bronze"))
			{
				list.Add("Bronze");
			}
			if (prefabName.IndexOf("Iron", StringComparison.OrdinalIgnoreCase) >= 0 || materialNames.Contains("Iron"))
			{
				list.Add("Iron");
			}
			if (prefabName.IndexOf("Silver", StringComparison.OrdinalIgnoreCase) >= 0 || materialNames.Contains("Silver"))
			{
				list.Add("Silver");
			}
			if (prefabName.IndexOf("Blackmetal", StringComparison.OrdinalIgnoreCase) >= 0 || materialNames.Contains("BlackMetal"))
			{
				list.Add("BlackMetal");
			}
			if (prefabName.IndexOf("Dvergr", StringComparison.OrdinalIgnoreCase) >= 0 || materialNames.Contains("Carapace") || materialNames.Contains("Eitr"))
			{
				list.Add("Dvergr");
				list.Add("Carapace");
				list.Add("Eitr");
			}
			if (prefabName.IndexOf("Flametal", StringComparison.OrdinalIgnoreCase) >= 0 || materialNames.Contains("FlametalNew") || materialNames.Contains("Flametal"))
			{
				list.Add("Flametal");
				list.Add("FlametalNew");
			}
			return list.ToArray();
		}

		private static bool IsWarfareItem(string prefabName, string recipeName, string sharedName)
		{
			if (!prefabName.EndsWith("_TW", StringComparison.OrdinalIgnoreCase) && !recipeName.EndsWith("_TW", StringComparison.OrdinalIgnoreCase) && sharedName.IndexOf("_TW", StringComparison.OrdinalIgnoreCase) < 0 && prefabName.IndexOf("Warfare", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return recipeName.IndexOf("Warfare", StringComparison.OrdinalIgnoreCase) >= 0;
			}
			return true;
		}

		private static bool IsThrowingAxeItem(string prefabName, string recipeName, string sharedName, SharedData shared)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Invalid comparison between Unknown and I4
			bool num = prefabName.IndexOf("ThrowAxe", StringComparison.OrdinalIgnoreCase) >= 0 || prefabName.IndexOf("ThrowingAxe", StringComparison.OrdinalIgnoreCase) >= 0 || recipeName.IndexOf("ThrowAxe", StringComparison.OrdinalIgnoreCase) >= 0 || recipeName.IndexOf("ThrowingAxe", StringComparison.OrdinalIgnoreCase) >= 0 || sharedName.IndexOf("throw_axe", StringComparison.OrdinalIgnoreCase) >= 0;
			bool flag = (int)shared.m_skillType == 7 && (prefabName.IndexOf("Throw", StringComparison.OrdinalIgnoreCase) >= 0 || recipeName.IndexOf("Throw", StringComparison.OrdinalIgnoreCase) >= 0 || sharedName.IndexOf("throw", StringComparison.OrdinalIgnoreCase) >= 0);
			return num || flag;
		}

		private static Requirement CloneRequirement(Requirement source)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: 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_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			return new Requirement
			{
				m_resItem = source.m_resItem,
				m_amount = source.m_amount,
				m_extraAmountOnlyOneIngredient = source.m_extraAmountOnlyOneIngredient,
				m_amountPerLevel = source.m_amountPerLevel,
				m_recover = source.m_recover,
				m_upgraderResource = true
			};
		}

		private static string StripClone(string rawName)
		{
			if (string.IsNullOrEmpty(rawName))
			{
				return string.Empty;
			}
			int num = rawName.IndexOf("(Clone)", StringComparison.Ordinal);
			if (num < 0)
			{
				return rawName;
			}
			return rawName.Substring(0, num);
		}
	}
}