Decompiled source of SpikeQuestItems v1.0.2

BepInEx/plugins/SpikeQuestItems/SpikeQuestItems.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 SpikeQuestItems
{
	internal static class Utils
	{
		public static FieldInfo pInventory_Field = AccessTools.Field(typeof(PlayerQuesting), "_pInventory");

		public static PlayerInventory GetPlayerInventory_PlayerQuesting(PlayerQuesting playerQuesting)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			return (PlayerInventory)pInventory_Field.GetValue(playerQuesting);
		}

		public static void ItemStorage_Load()
		{
			if (!ItemStorageManager._current._isOpen)
			{
				ProfileDataManager._current.Load_ItemStorageData();
			}
		}

		public static void ItemStorage_Save()
		{
			if (ProfileDataManager._current._itemStorageProfile == null)
			{
				ProfileDataManager._current.Load_ItemStorageData();
			}
			ProfileDataManager._current.Save_ItemStorageData();
		}

		public static int Find_Item_In_ItemStorage(string itemName)
		{
			ItemStorage_Load();
			return 0 + Count_Item_In_ItemDatas(ItemStorageManager._current._itemDatas, itemName) + Count_Item_In_ItemDatas(ItemStorageManager._current._itemDatas_01, itemName) + Count_Item_In_ItemDatas(ItemStorageManager._current._itemDatas_02, itemName);
		}

		public static int Count_Item_In_ItemDatas(List<ItemData> itemList, string itemName)
		{
			int num = 0;
			foreach (ItemData item in itemList)
			{
				if (item._itemName == itemName)
				{
					num += item._quantity;
				}
			}
			return num;
		}

		public static int Count_Item_In_PlayerInventory(PlayerInventory inventory, string itemName)
		{
			return Count_Item_In_ItemDatas(inventory._heldItems, itemName);
		}

		public static int Count_Total_Items_In_Player_And_ItemStorage(PlayerInventory inventory, string itemName)
		{
			int num = Count_Item_In_PlayerInventory(inventory, itemName);
			int num2 = Find_Item_In_ItemStorage(itemName);
			return num + num2;
		}

		public static void Remove_Item_In_ItemStorage(string itemName, int remove_count)
		{
			ItemStorage_Load();
			int num = remove_count;
			num -= Remove_Item_In_ItemDatas(itemName, num, ref ItemStorageManager._current._itemDatas);
			num -= Remove_Item_In_ItemDatas(itemName, num, ref ItemStorageManager._current._itemDatas_01);
			num -= Remove_Item_In_ItemDatas(itemName, num, ref ItemStorageManager._current._itemDatas_02);
			ItemStorage_Save();
		}

		public static int Remove_Item_In_ItemDatas(string itemName, int count, ref List<ItemData> item_list)
		{
			if (count == 0)
			{
				return 0;
			}
			int num = count;
			for (int i = 0; i < item_list.Count; i++)
			{
				if (num <= 0)
				{
					break;
				}
				ItemData val = item_list[i];
				if (!(val._itemName != itemName))
				{
					int quantity = val._quantity;
					val._quantity -= num;
					num -= quantity;
					if (val._quantity <= 0)
					{
						item_list.RemoveAt(i);
						i--;
					}
				}
			}
			if (num < 0)
			{
				num = 0;
			}
			return count - num;
		}
	}
	[HarmonyPatch]
	internal static class Dynamic_QuestItemRequirement_Patch
	{
		[HarmonyTargetMethod]
		public static MethodBase Find_Iterate_QuestItemRequirement()
		{
			foreach (MethodInfo declaredMethod in AccessTools.GetDeclaredMethods(typeof(PlayerQuesting)))
			{
				if (declaredMethod.Name.Contains("Iterate_QuestItemRequirement"))
				{
					return declaredMethod;
				}
			}
			throw new Exception("Unable to find 'Iterate_QuestItemRequirement'!!!");
		}

		[HarmonyPrefix]
		private static void Client_CompleteQuest__Iterate_QuestItemRequirement_Prefix_Patch(ref PlayerQuesting __instance, ref QuestItemRequirement _qIR)
		{
			int num = Utils.Count_Item_In_PlayerInventory(Utils.GetPlayerInventory_PlayerQuesting(__instance), _qIR._questItem._itemName);
			int remove_count = _qIR._itemsNeeded - num;
			Utils.Remove_Item_In_ItemStorage(_qIR._questItem._itemName, remove_count);
		}
	}
	[HarmonyPatch]
	internal static class Patch
	{
		public const string QuestProgressNote_ItemProgress_Pattern = "^(?<Itemname>.+?): \\((?<A>\\d+) \\/ (?<B>\\d+)\\)$";

		[HarmonyPatch(typeof(PlayerQuesting), "Check_QuestCompletion")]
		[HarmonyPrefix]
		private static void Check_QuestCompletion_Prefix_Patch(ref PlayerQuesting __instance, int _index)
		{
			ScriptableQuest val = GameManager._current.LocateQuest(__instance._questProgressData[_index]._questTag);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			QuestItemRequirement[] questItemRequirements = val._questObjective._questItemRequirements;
			int[] itemProgressValues = __instance._questProgressData[_index]._itemProgressValues;
			for (int i = 0; i < questItemRequirements.Length; i++)
			{
				string itemName = questItemRequirements[i]._questItem._itemName;
				int itemsNeeded = questItemRequirements[i]._itemsNeeded;
				if (itemProgressValues[i] != itemsNeeded)
				{
					int num = Utils.Count_Item_In_PlayerInventory(Utils.GetPlayerInventory_PlayerQuesting(__instance), itemName);
					int num2 = Utils.Find_Item_In_ItemStorage(itemName);
					int num3 = num + num2;
					if (num3 > itemsNeeded)
					{
						num3 = itemsNeeded;
					}
					__instance._questProgressData[_index]._itemProgressValues[i] = num3;
				}
			}
		}

		[HarmonyPatch(typeof(PlayerQuesting), "Apply_QuestProgressNote")]
		[HarmonyPrefix]
		private static void Apply_QuestProgressNote_Prefix_Patch(ref PlayerQuesting __instance, ref string _string)
		{
			Match match = Regex.Match(_string, "^(?<Itemname>.+?): \\((?<A>\\d+) \\/ (?<B>\\d+)\\)$");
			if (!match.Success)
			{
				return;
			}
			string value = match.Groups["Itemname"].Value;
			int num = Utils.Count_Total_Items_In_Player_And_ItemStorage(Utils.GetPlayerInventory_PlayerQuesting(__instance), value);
			int num2 = int.Parse(match.Groups["A"].Value);
			int num3 = int.Parse(match.Groups["B"].Value);
			if (num2 < num)
			{
				num2 = num;
				if (num2 > num3)
				{
					num2 = num3;
				}
				_string = $"{value}: ({num2} / {num3})*";
			}
		}
	}
	public static class PluginInfo
	{
		public const string GUID = "scottyfoxart.atlyss.spikequestitems";

		public const string NAME = "Spike Quest Items";

		public const string VERSION = "1.0.2";

		public const string WEBSITE = "https://github.com/ScottyFox/AtlyssModTesting/tree/main/MimicQuestItems";
	}
	[BepInPlugin("scottyfoxart.atlyss.spikequestitems", "Spike Quest Items", "1.0.2")]
	public class Plugin : BaseUnityPlugin
	{
		internal static Harmony instance = new Harmony("scottyfoxart.atlyss.spikequestitems");

		private void Awake()
		{
			try
			{
				instance.PatchAll(Assembly.GetExecutingAssembly());
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin scottyfoxart.atlyss.spikequestitems is loaded!");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin scottyfoxart.atlyss.spikequestitems failed to load...");
				((BaseUnityPlugin)this).Logger.LogError((object)ex);
			}
		}
	}
}