Decompiled source of Paprikas Shop v1.2.1

plugins/PaprikasShop.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Kolgk.PaprikasShop;

[BepInPlugin("kolgk.repo.paprikasshop", "Paprika's Shop", "1.2.1")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "kolgk.repo.paprikasshop";

	internal static ManualLogSource Log;

	internal static ConfigEntry<int> Items;

	internal static ConfigEntry<int> Consumables;

	internal static ConfigEntry<int> Upgrades;

	internal static ConfigEntry<int> HealthPacks;

	internal static ConfigEntry<int> MinStock;

	internal static ConfigEntry<bool> IgnoreMoney;

	internal static ConfigEntry<float> SlotMultiplier;

	private void Awake()
	{
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Items = ((BaseUnityPlugin)this).Config.Bind<int>("Quantités", "Objets", 20, "Nombre d'objets (armes, drones, outils…). Jeu de base : 8.");
		Consumables = ((BaseUnityPlugin)this).Config.Bind<int>("Quantités", "Consommables", 12, "Grenades, mines, cristaux… Jeu de base : 4 à 6.");
		Upgrades = ((BaseUnityPlugin)this).Config.Bind<int>("Quantités", "Améliorations", 10, "Améliorations de joueur. Jeu de base : 3.");
		HealthPacks = ((BaseUnityPlugin)this).Config.Bind<int>("Quantités", "KitsDeSoin", 8, "Kits de soin. Jeu de base : 3.");
		MinStock = ((BaseUnityPlugin)this).Config.Bind<int>("Stock", "StockMinimumParObjet", 3, "Chaque objet peut apparaître au moins ce nombre de fois (le jeu limite souvent à 1-2).");
		SlotMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Emplacements", "Multiplicateur", 1.5f, "Ajoute des emplacements dans la pièce du shop (copies posées à côté des emplacements d'origine, seulement là où il y a de la place sur la même étagère). 1 = jeu de base, 1.5 = 50 % de plus.");
		IgnoreMoney = ((BaseUnityPlugin)this).Config.Bind<bool>("Stock", "IgnorerArgent", true, "Le jeu écarte 3 fois sur 4 les objets plus chers que ton argent. true = on les garde tous.");
		Harmony val = new Harmony("kolgk.repo.paprikasshop");
		val.PatchAll(typeof(ShopPatches));
		val.PatchAll(typeof(SpawnCount));
		val.PatchAll(typeof(PopulateDone));
		Log.LogInfo((object)"Paprika's Shop chargé (actif seulement quand tu es l'hôte).");
	}
}
internal static class ShopPatches
{
	private struct State
	{
		public int currency;

		public Dictionary<Item, int> stock;
	}

	private static readonly FieldInfo fTotalCurrency = AccessTools.Field(typeof(ShopManager), "totalCurrency");

	private static readonly FieldInfo fItemsAmount = AccessTools.Field(typeof(ShopManager), "itemSpawnTargetAmount");

	private static readonly FieldInfo fConsAmount = AccessTools.Field(typeof(ShopManager), "itemConsumablesAmount");

	private static readonly FieldInfo fUpgAmount = AccessTools.Field(typeof(ShopManager), "itemUpgradesAmount");

	private static readonly FieldInfo fHpAmount = AccessTools.Field(typeof(ShopManager), "itemHealthPacksAmount");

	private static readonly FieldInfo fItemVolumes = AccessTools.Field(typeof(ShopManager), "itemVolumes");

	private static readonly FieldInfo fMaxInShop = AccessTools.Field(typeof(Item), "maxAmountInShop");

	private static readonly FieldInfo fItemDict = AccessTools.Field(typeof(StatsManager), "itemDictionary");

	[HarmonyPatch(typeof(ShopManager), "GetAllItemsFromStatsManager")]
	[HarmonyPrefix]
	private static void Before(ShopManager __instance, out State __state)
	{
		__state = new State
		{
			currency = (int)fTotalCurrency.GetValue(__instance),
			stock = new Dictionary<Item, int>()
		};
		if (SemiFunc.IsNotMasterClient())
		{
			return;
		}
		if (Plugin.IgnoreMoney.Value)
		{
			fTotalCurrency.SetValue(__instance, int.MaxValue);
		}
		if (!(fItemDict.GetValue(StatsManager.instance) is Dictionary<string, Item> dictionary))
		{
			return;
		}
		foreach (Item value in dictionary.Values)
		{
			if (!((Object)(object)value == (Object)null))
			{
				int num = (int)fMaxInShop.GetValue(value);
				__state.stock[value] = num;
				if (num > 0 && num < Plugin.MinStock.Value)
				{
					fMaxInShop.SetValue(value, Plugin.MinStock.Value);
				}
			}
		}
	}

	[HarmonyPatch(typeof(ShopManager), "GetAllItemsFromStatsManager")]
	[HarmonyPostfix]
	private static void After(ShopManager __instance, State __state)
	{
		fTotalCurrency.SetValue(__instance, __state.currency);
		if (__state.stock != null)
		{
			foreach (KeyValuePair<Item, int> item in __state.stock)
			{
				if ((Object)(object)item.Key != (Object)null)
				{
					fMaxInShop.SetValue(item.Key, item.Value);
				}
			}
		}
		if (!SemiFunc.IsNotMasterClient())
		{
			fItemsAmount.SetValue(__instance, Plugin.Items.Value);
			fConsAmount.SetValue(__instance, Plugin.Consumables.Value);
			fUpgAmount.SetValue(__instance, Plugin.Upgrades.Value);
			fHpAmount.SetValue(__instance, Plugin.HealthPacks.Value);
			Plugin.Log.LogInfo((object)($"Paprika's Shop : quotas {Plugin.Items.Value}/{Plugin.Consumables.Value}/{Plugin.Upgrades.Value}/{Plugin.HealthPacks.Value}, " + $"candidats objets={__instance.potentialItems.Count} conso={__instance.potentialItemConsumables.Count} " + $"amélio={__instance.potentialItemUpgrades.Count} soin={__instance.potentialItemHealthPacks.Count}"));
		}
	}

	[HarmonyPatch(typeof(ShopManager), "GetAllItemVolumesInScene")]
	[HarmonyPostfix]
	private static void Volumes(ShopManager __instance)
	{
		if (SemiFunc.IsNotMasterClient())
		{
			return;
		}
		List<ItemVolume> list = fItemVolumes.GetValue(__instance) as List<ItemVolume>;
		int num = list?.Count ?? 0;
		int num2 = ((list != null) ? ExtraSlots.Add(list, Plugin.SlotMultiplier.Value) : 0);
		Plugin.Log.LogInfo((object)$"Paprika's Shop : emplacements d'origine {num}, ajoutés {num2}.");
		SortedDictionary<string, int> sortedDictionary = new SortedDictionary<string, int>();
		if (list != null)
		{
			foreach (ItemVolume item in list)
			{
				if (!((Object)(object)item == (Object)null))
				{
					string key = ((object)Unsafe.As<itemVolume, itemVolume>(ref item.itemVolume)/*cast due to .constrained prefix*/).ToString();
					sortedDictionary[key] = ((!sortedDictionary.TryGetValue(key, out var value)) ? 1 : (value + 1));
				}
			}
		}
		List<string> list2 = new List<string>();
		foreach (KeyValuePair<string, int> item2 in sortedDictionary)
		{
			list2.Add(item2.Key + "=" + item2.Value);
		}
		Plugin.Log.LogInfo((object)string.Format("Paprika's Shop : {0} emplacements ({1}).", list?.Count ?? 0, string.Join(", ", list2)));
		SpawnCount.Reset();
	}
}
[HarmonyPatch(typeof(PunManager), "SpawnShopItem")]
internal static class SpawnCount
{
	internal static int items;

	internal static int cons;

	internal static int upg;

	internal static int hp;

	internal static int secret;

	internal static void Reset()
	{
		items = (cons = (upg = (hp = (secret = 0))));
	}

	private static void Postfix(bool __result, List<Item> itemList, bool isSecret)
	{
		if (!__result)
		{
			return;
		}
		ShopManager instance = ShopManager.instance;
		if (isSecret)
		{
			secret++;
		}
		else if (!((Object)(object)instance == (Object)null))
		{
			if (itemList == instance.potentialItems)
			{
				items++;
			}
			else if (itemList == instance.potentialItemConsumables)
			{
				cons++;
			}
			else if (itemList == instance.potentialItemUpgrades)
			{
				upg++;
			}
			else if (itemList == instance.potentialItemHealthPacks)
			{
				hp++;
			}
		}
	}
}
[HarmonyPatch]
internal static class PopulateDone
{
	private static MethodBase TargetMethod()
	{
		MethodInfo[] methods = typeof(PunManager).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		foreach (MethodInfo methodInfo in methods)
		{
			if (methodInfo.Name == "ShopPopulateItemVolumes" && methodInfo.GetParameters().Length == 1)
			{
				return methodInfo;
			}
		}
		return null;
	}

	private static void Postfix()
	{
		if (!SemiFunc.IsNotMasterClient())
		{
			int num = SpawnCount.items + SpawnCount.cons + SpawnCount.upg + SpawnCount.hp + SpawnCount.secret;
			Plugin.Log.LogInfo((object)($"Paprika's Shop : posés {num} — objets {SpawnCount.items}, conso {SpawnCount.cons}, " + $"amélio {SpawnCount.upg}, soin {SpawnCount.hp}, secrets {SpawnCount.secret}"));
		}
	}
}
internal static class ExtraSlots
{
	private static float Radius(itemVolume t)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Expected I4, but got Unknown
		return (int)t switch
		{
			0 => 0.15f, 
			6 => 0.14f, 
			7 => 0.18f, 
			4 => 0.18f, 
			1 => 0.24f, 
			2 => 0.38f, 
			5 => 0.38f, 
			3 => 0.5f, 
			8 => 0.5f, 
			_ => 0.9f, 
		};
	}

	private static bool Ground(Vector3 p, out float gap)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		gap = 0f;
		RaycastHit val = default(RaycastHit);
		if (Physics.Raycast(p + Vector3.up * 0.05f, Vector3.down, ref val, 1.5f, -1, (QueryTriggerInteraction)1))
		{
			gap = ((RaycastHit)(ref val)).distance;
			return true;
		}
		return false;
	}

	internal static int Add(List<ItemVolume> volumes, float mult)
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: Invalid comparison between Unknown and I4
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01db: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0201: Unknown result type (might be due to invalid IL or missing references)
		//IL_0206: Unknown result type (might be due to invalid IL or missing references)
		//IL_023a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0241: Unknown result type (might be due to invalid IL or missing references)
		//IL_0248: Unknown result type (might be due to invalid IL or missing references)
		//IL_024d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0252: Unknown result type (might be due to invalid IL or missing references)
		//IL_0254: Unknown result type (might be due to invalid IL or missing references)
		//IL_027b: Unknown result type (might be due to invalid IL or missing references)
		//IL_027d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0290: Unknown result type (might be due to invalid IL or missing references)
		//IL_0295: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0329: Unknown result type (might be due to invalid IL or missing references)
		//IL_032d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0377: Unknown result type (might be due to invalid IL or missing references)
		if (mult <= 1.001f || volumes.Count == 0)
		{
			return 0;
		}
		int num = Mathf.RoundToInt((float)volumes.Count * (mult - 1f));
		List<KeyValuePair<Vector3, float>> list = new List<KeyValuePair<Vector3, float>>();
		foreach (ItemVolume volume in volumes)
		{
			if ((Object)(object)volume != (Object)null)
			{
				list.Add(new KeyValuePair<Vector3, float>(((Component)volume).transform.position, Radius(volume.itemVolume)));
			}
		}
		List<ItemVolume> list2 = new List<ItemVolume>(volumes);
		Random random = new Random();
		for (int num2 = list2.Count - 1; num2 > 0; num2--)
		{
			int index = random.Next(num2 + 1);
			ItemVolume value = list2[num2];
			list2[num2] = list2[index];
			list2[index] = value;
		}
		int num3 = 0;
		Vector3 val2 = default(Vector3);
		for (int i = 0; i < 3; i++)
		{
			if (num3 >= num)
			{
				break;
			}
			foreach (ItemVolume item in list2)
			{
				if (num3 >= num)
				{
					break;
				}
				if ((Object)(object)item == (Object)null || (int)item.itemVolume == 9)
				{
					continue;
				}
				float num4 = Radius(item.itemVolume);
				if (!Ground(((Component)item).transform.position, out var gap))
				{
					continue;
				}
				Transform transform = ((Component)item).transform;
				float num5 = 2f * num4 + 0.08f * (float)(i + 1);
				Vector3[] array = (Vector3[])(object)new Vector3[4]
				{
					transform.right,
					-transform.right,
					transform.forward,
					-transform.forward
				};
				Vector3[] array2 = array;
				for (int j = 0; j < array2.Length; j++)
				{
					Vector3 val = array2[j];
					((Vector3)(ref val2))..ctor(val.x, 0f, val.z);
					if (((Vector3)(ref val2)).sqrMagnitude < 0.01f)
					{
						continue;
					}
					Vector3 val3 = transform.position + ((Vector3)(ref val2)).normalized * num5;
					if (!Ground(val3, out var gap2) || Mathf.Abs(gap2 - gap) > 0.06f || Physics.CheckSphere(val3 + Vector3.up * (num4 * 0.9f + 0.04f), num4 * 0.85f, -1, (QueryTriggerInteraction)1))
					{
						continue;
					}
					bool flag = false;
					foreach (KeyValuePair<Vector3, float> item2 in list)
					{
						if (Vector3.Distance(item2.Key, val3) < (item2.Value + num4) * 0.95f)
						{
							flag = true;
							break;
						}
					}
					if (!flag)
					{
						GameObject val4 = Object.Instantiate<GameObject>(((Component)item).gameObject, val3, transform.rotation, transform.parent);
						((Object)val4).name = "Paprika Volume";
						ItemVolume component = val4.GetComponent<ItemVolume>();
						if (!((Object)(object)component == (Object)null))
						{
							volumes.Add(component);
							list.Add(new KeyValuePair<Vector3, float>(val3, num4));
							num3++;
							break;
						}
						Object.Destroy((Object)(object)val4);
					}
				}
			}
		}
		return num3;
	}
}