Decompiled source of EssenceForAll v1.0.1

EssenceForAll.dll

Decompiled 2 days 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.Text;
using BepInEx;
using BepInEx.Configuration;
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.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("EssenceForAll")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+a45c570d17c512d77d75166ff42bc1dee72ef243")]
[assembly: AssemblyProduct("EssenceForAll")]
[assembly: AssemblyTitle("EssenceForAll")]
[assembly: AssemblyVersion("1.0.1.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 EssenceForAll
{
	[HarmonyPatch]
	internal static class SpawnPatches
	{
		internal const string PlayersKey = "EssenceForAll_Players";

		[HarmonyPatch(typeof(Character), "Awake")]
		[HarmonyPostfix]
		private static void Awake_Postfix(Character __instance)
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			if (__instance.IsPlayer())
			{
				return;
			}
			CharacterDrop component = ((Component)__instance).GetComponent<CharacterDrop>();
			if (!((Object)(object)component == (Object)null) && DropsEssence(component))
			{
				ZNetView component2 = ((Component)__instance).GetComponent<ZNetView>();
				ZDO val = (((Object)(object)component2 != (Object)null && component2.IsValid()) ? component2.GetZDO() : null);
				if (val != null && component2.IsOwner() && val.GetInt("EssenceForAll_Players", 0) <= 0)
				{
					val.Set("EssenceForAll_Players", CountPlayers(((Component)__instance).transform.position));
				}
			}
		}

		private static bool DropsEssence(CharacterDrop drops)
		{
			foreach (Drop drop in drops.m_drops)
			{
				if (EssenceForAllPlugin.IsEssence(drop))
				{
					return true;
				}
			}
			return false;
		}

		internal static int CountPlayers(Vector3 position)
		{
			//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)
			float range = EssenceForAllPlugin.PlayerRange.Value;
			if (range <= 0f && (Object)(object)ZNet.instance != (Object)null)
			{
				return Mathf.Max(1, ZNet.instance.GetNrOfPlayers());
			}
			int num = Player.GetAllPlayers().Count((Player p) => (Object)(object)p != (Object)null && Vector3.Distance(((Component)p).transform.position, position) <= range);
			return Mathf.Max(1, num);
		}
	}
	[HarmonyPatch(typeof(CharacterDrop), "GenerateDropList")]
	internal static class DropPatches
	{
		[HarmonyPrefix]
		private static void Prefix(CharacterDrop __instance, out List<KeyValuePair<Drop, float>> __state)
		{
			__state = null;
			if (!EssenceForAllPlugin.Enabled.Value)
			{
				return;
			}
			foreach (Drop drop in __instance.m_drops)
			{
				if (EssenceForAllPlugin.IsEssence(drop))
				{
					(__state ?? (__state = new List<KeyValuePair<Drop, float>>())).Add(new KeyValuePair<Drop, float>(drop, drop.m_chance));
					drop.m_chance = 1f;
				}
			}
		}

		[HarmonyPostfix]
		private static void Postfix(CharacterDrop __instance, List<KeyValuePair<GameObject, int>> __result, List<KeyValuePair<Drop, float>> __state)
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			if (__state == null)
			{
				return;
			}
			foreach (KeyValuePair<Drop, float> item in __state)
			{
				item.Key.m_chance = item.Value;
			}
			if (__result == null)
			{
				return;
			}
			int num = 1;
			if (EssenceForAllPlugin.MultiplyByPlayers.Value)
			{
				ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
				int num2 = (((Object)(object)component != (Object)null && component.IsValid()) ? component.GetZDO().GetInt("EssenceForAll_Players", 0) : 0);
				num = ((num2 > 0) ? num2 : SpawnPatches.CountPlayers(((Component)__instance).transform.position));
			}
			StringBuilder stringBuilder = (EssenceForAllPlugin.LogDrops.Value ? new StringBuilder() : null);
			for (int i = 0; i < __result.Count; i++)
			{
				GameObject prefab = __result[i].Key;
				Drop key = __state.FirstOrDefault((KeyValuePair<Drop, float> s) => (Object)(object)s.Key.m_prefab == (Object)(object)prefab).Key;
				if (key != null && !key.m_onePerPlayer)
				{
					int num3 = __result[i].Value * num;
					__result[i] = new KeyValuePair<GameObject, int>(prefab, num3);
					stringBuilder?.Append($" {((Object)prefab).name} x{num3}");
				}
			}
			if (stringBuilder != null)
			{
				EssenceForAllPlugin.Log.LogInfo((object)$"{((Object)__instance).name}: {num} player(s):{stringBuilder}");
			}
		}
	}
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	internal static class ScenePatches
	{
		[HarmonyPostfix]
		private static void Postfix(ZNetScene __instance)
		{
			foreach (GameObject prefab in __instance.m_prefabs)
			{
				CharacterDrop val = (((Object)(object)prefab != (Object)null) ? prefab.GetComponent<CharacterDrop>() : null);
				if (!((Object)(object)val == (Object)null))
				{
					string text = string.Join(", ", from d in val.m_drops.Where(EssenceForAllPlugin.IsEssence)
						select $"{((Object)d.m_prefab).name} {d.m_amountMin}-{d.m_amountMax} at {d.m_chance:P0}");
					if (text.Length > 0)
					{
						EssenceForAllPlugin.Log.LogInfo((object)(((Object)prefab).name + " drops " + text));
					}
				}
			}
		}
	}
	[BepInPlugin("com.koendertjh.essenceforall", "EssenceForAll", "1.0.1")]
	public class EssenceForAllPlugin : BaseUnityPlugin
	{
		public const string ModGuid = "com.koendertjh.essenceforall";

		public const string ModName = "EssenceForAll";

		public const string ModVersion = "1.0.1";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<string> EssenceItems;

		internal static ConfigEntry<bool> MultiplyByPlayers;

		internal static ConfigEntry<float> PlayerRange;

		internal static ConfigEntry<bool> LogDrops;

		private static HashSet<string> s_essences = new HashSet<string>();

		private Harmony _harmony;

		private void Awake()
		{
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch.");
			EssenceItems = ((BaseUnityPlugin)this).Config.Bind<string>("General", "EssenceItems", "OrbThunderBlood,OrbFrostFire", "Comma-separated item prefab names that always drop and are multiplied by the player count.");
			MultiplyByPlayers = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "MultiplyByPlayers", true, "If true, the essence amount is multiplied by the number of players near the creature when it spawned.");
			PlayerRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PlayerRange", 200f, "Players within this many meters of a creature when it spawns are counted. 0 = every player in the world.");
			LogDrops = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "LogDrops", false, "Logs every boosted essence drop (for troubleshooting).");
			ParseEssences();
			EssenceItems.SettingChanged += delegate
			{
				ParseEssences();
			};
			_harmony = new Harmony("com.koendertjh.essenceforall");
			_harmony.PatchAll(typeof(EssenceForAllPlugin).Assembly);
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private static void ParseEssences()
		{
			s_essences = new HashSet<string>(from s in EssenceItems.Value.Split(new char[1] { ',' })
				select s.Trim() into s
				where s.Length > 0
				select s, StringComparer.OrdinalIgnoreCase);
		}

		internal static bool IsEssence(Drop drop)
		{
			if ((Object)(object)drop?.m_prefab != (Object)null)
			{
				return s_essences.Contains(((Object)drop.m_prefab).name);
			}
			return false;
		}
	}
	internal static class PluginInfo
	{
		public const string Version = "1.0.1";
	}
}