Decompiled source of Equillibrium v1.0.2

plugins/Equillibrium.dll

Decompiled 3 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Gungeon;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Mod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Mod")]
[assembly: AssemblyCopyright("Copyright ©  2020")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("57445610-0892-47c3-be16-453172104123")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Equillibrium;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("somebunny.etg.equillibrium", "Equillibrium", "1.0.2")]
public class EquillibriumModule : BaseUnityPlugin
{
	public const string GUID = "somebunny.etg.equillibrium";

	public const string NAME = "Equillibrium";

	public const string VERSION = "1.0.2";

	public const string TEXT_COLOR = "#ffa024";

	public static List<ModdedItemTracker> moddedItemTrackers = new List<ModdedItemTracker>();

	public static List<PickupObject> allitems = new List<PickupObject>();

	public static List<string> allIDs = new List<string>();

	public void Start()
	{
		ETGModMainBehaviour.WaitForGameManagerStart((Action<GameManager>)GMStart);
	}

	public void GMStart(GameManager g)
	{
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		EquillibriumConfig.configurationFile = ((BaseUnityPlugin)this).Config;
		EquillibriumConfig.Init();
		if (EquillibriumConfig.advancedMode.Value)
		{
			SeparateClassForAHookBecauseGodHatesMe.Init();
			new Hook((MethodBase)typeof(LootData).GetMethod("GetItemsForPlayer", BindingFlags.Instance | BindingFlags.Public), typeof(EquillibriumModule).GetMethod("GetItemsForPlayerHook"));
		}
		((MonoBehaviour)g).StartCoroutine(DelayedStart());
	}

	public static float VanillaMultiplier()
	{
		return (float)AmountOfModsOn() * EquillibriumConfig.VanillaChanceMultiplier.Value;
	}

	public static int AmountOfModsOn()
	{
		return (!EquillibriumConfig.ModAmountScaling.Value) ? 1 : allIDs.Count;
	}

	public static float DoHyperBolicScaling(float EffectOfOne, int Stack)
	{
		return 1f - 1f / (1f + EffectOfOne * (float)Stack);
	}

	public static List<PickupObject> GetItemsForPlayerHook(Func<LootData, PlayerController, int, GenericLootTable, Random, List<PickupObject>> orig, LootData self, PlayerController player, int tierShift = 0, GenericLootTable OverrideDropTable = null, Random generatorRandom = null)
	{
		List<PickupObject> list = orig.Invoke(self, player, tierShift, OverrideDropTable, generatorRandom);
		foreach (PickupObject item in list)
		{
			foreach (ModdedItemTracker moddedItemTracker in moddedItemTrackers)
			{
				ReprocessWeights(moddedItemTracker, (moddedItemTracker.ID == "gungeon") ? VanillaMultiplier() : 1f, (!moddedItemTracker.ItemIDs.Contains(item.PickupObjectId)) ? true : false);
			}
		}
		return list;
	}

	public static void ReprocessWeights(ModdedItemTracker Tracker, float additionalMultiplier = 1f, bool Tickdown = false)
	{
		if (!Tickdown)
		{
			Tracker.PickupCount = EquillibriumConfig.PickupCoefficient.Value + AmountOfModsOn();
		}
		else if (Tracker.PickupCount > 1)
		{
			Tracker.PickupCount--;
		}
		float num = 1f - (float)Tracker.ItemIDs.Count / (float)allitems.Count;
		foreach (WeightedGameObject element in GameManager.Instance.RewardManager.GunsLootTable.defaultItemDrops.elements)
		{
			if (Tracker.ItemIDs.Contains(element.pickupId))
			{
				element.weight = num * additionalMultiplier / (float)Tracker.PickupCount;
			}
		}
		foreach (WeightedGameObject element2 in GameManager.Instance.RewardManager.ItemsLootTable.defaultItemDrops.elements)
		{
			if (Tracker.ItemIDs.Contains(element2.pickupId))
			{
				element2.weight = num * additionalMultiplier / (float)Tracker.PickupCount;
			}
		}
	}

	public IEnumerator DelayedStart()
	{
		yield return null;
		List<string> IDs = new List<string>();
		List<PickupObject> items = new List<PickupObject>();
		items.AddRange(Game.Items.Entries.ToList());
		for (int i = items.Count - 1; i >= 0; i--)
		{
			if ((int)items[i].quality == -100)
			{
				items.Remove(items[i]);
			}
			else if ((int)items[i].quality == -50)
			{
				items.Remove(items[i]);
			}
			else if ((int)items[i].quality == 0)
			{
				items.Remove(items[i]);
			}
		}
		string[] array = Game.Items.AllIDs;
		foreach (string o in array)
		{
			string ID = o.Split(new string[1] { ":" }, StringSplitOptions.None)[0];
			if (!IDs.Contains(ID))
			{
				IDs.Add(ID);
			}
		}
		foreach (string ID3 in IDs)
		{
			List<int> itemIDs = new List<int>();
			string[] array2 = Game.Items.AllIDs;
			foreach (string o2 in array2)
			{
				if (o2.Contains(ID3))
				{
					PickupObject p = Game.Items.Get(o2);
					if ((int)p.quality != -100 && (int)p.quality != -50 && (int)p.quality != 0)
					{
						itemIDs.Add(p.PickupObjectId);
					}
				}
			}
			moddedItemTrackers.Add(new ModdedItemTracker
			{
				ID = ID3,
				ItemIDs = itemIDs,
				PickupCount = 1
			});
		}
		allitems = items;
		allIDs = IDs;
		foreach (ModdedItemTracker ID2 in moddedItemTrackers)
		{
			ProcessnewItemDatabaseOfID(ID2, (ID2.ID == "gungeon") ? VanillaMultiplier() : 1f);
		}
		Log("Equillibrium v1.0.2 started successfully.", "#ffa024");
	}

	public static void ProcessnewItemDatabaseOfID(ModdedItemTracker ID, float additionalMultiplier = 1f)
	{
		float num = 1f - (float)ID.ItemIDs.Count / (float)allitems.Count;
		List<WeightedGameObject> list = GameManager.Instance.RewardManager.ItemsLootTable.defaultItemDrops.elements.Where((WeightedGameObject self) => ID.ItemIDs.Contains(self.pickupId)).ToList();
		list.AddRange(GameManager.Instance.RewardManager.GunsLootTable.defaultItemDrops.elements.Where((WeightedGameObject self) => ID.ItemIDs.Contains(self.pickupId)).ToList());
		foreach (WeightedGameObject item in list)
		{
			item.weight *= num * additionalMultiplier;
		}
	}

	public static void Log(string text, string color = "FFFFFF")
	{
		ETGModConsole.Log((object)("<color=" + color + ">" + text + "</color>"), false);
	}
}
public class ModdedItemTracker
{
	public List<int> ItemIDs;

	public string ID;

	public int PickupCount = 0;
}
public class EquillibriumConfig
{
	public static ConfigEntry<int> PickupCoefficient;

	public static ConfigEntry<bool> ModAmountScaling;

	public static ConfigEntry<float> VanillaChanceMultiplier;

	public static ConfigEntry<bool> advancedMode;

	public static ConfigFile configurationFile;

	public static void Init()
	{
		CreateConfig(configurationFile);
	}

	public static void CreateConfig(ConfigFile config)
	{
		VanillaChanceMultiplier = config.Bind<float>("Eqiullibrium:", "Vanilla Item Chance Multiplier", 0.1f, "(Default of 0.1f) Multiplies the weight of Vanilla/base game items when certain calcualtions are done.");
		advancedMode = config.Bind<bool>("Eqiullibrium:", "Advanced Mode", false, "(Default as false) If enabled, also re-processes the weights of items when they appear.");
		PickupCoefficient = config.Bind<int>("Eqiullibrium:", "Pickup Co-effecient", 1, "(Default: 1, Requires Advanced Mode) When an item is spawned, divides the weight of its pool by the Pickup Co-effecient, and reduces the Pickup Co-effecient by 1 every time a new item that isn't of its pool is spawned.");
		ModAmountScaling = config.Bind<bool>("Eqiullibrium:", "Mod Quantity Scaling", true, "(Default: true, Requires Advanced Mode)  When enabled, certain calculations will scale depending on how many separate mod pools the mod is able to detect.");
	}
}
public class SeparateClassForAHookBecauseGodHatesMe
{
	public static void Init()
	{
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		new Hook((MethodBase)typeof(LootData).GetMethod("GetSingleItemForPlayer", BindingFlags.Instance | BindingFlags.Public), typeof(SeparateClassForAHookBecauseGodHatesMe).GetMethod("GetSingleItemForPlayerHook"));
	}

	public static PickupObject GetSingleItemForPlayerHook(Func<LootData, PlayerController, int, PickupObject> orig, LootData self, PlayerController player, int tierShift = 0)
	{
		PickupObject val = orig(self, player, tierShift);
		try
		{
			foreach (ModdedItemTracker moddedItemTracker in EquillibriumModule.moddedItemTrackers)
			{
				EquillibriumModule.ReprocessWeights(moddedItemTracker, (moddedItemTracker.ID == "gungeon") ? EquillibriumModule.VanillaMultiplier() : 1f, (!moddedItemTracker.ItemIDs.Contains(val.PickupObjectId)) ? true : false);
			}
			return val;
		}
		catch
		{
			return val;
		}
	}
}