Decompiled source of ErenshorREL v0.0.1

ErenshorREL.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Brumdail")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.0.1")]
[assembly: AssemblyInformationalVersion("0.0.1+676b618d34a5fdfc8b7bb19d7a45bc2fb13472e8")]
[assembly: AssemblyProduct("ErenshorREL")]
[assembly: AssemblyTitle("ErenshorREL")]
[assembly: AssemblyVersion("0.0.1.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace ErenshorREL
{
	[BepInPlugin("Brumdail.ErenshorREL", "Erenshor Random Epic Loot", "0.0.1")]
	[BepInProcess("Erenshor.exe")]
	public class ErenshorREL : BaseUnityPlugin
	{
		public enum Toggle
		{
			Off,
			On
		}

		internal class AcceptableShortcuts : AcceptableValueBase
		{
			public AcceptableShortcuts()
				: base(typeof(KeyboardShortcut))
			{
			}

			public override object Clamp(object value)
			{
				return value;
			}

			public override bool IsValid(object value)
			{
				return true;
			}

			public override string ToDescriptionString()
			{
				return "# Acceptable values: " + string.Join(", ", UnityInput.Current.SupportedKeyCodes);
			}
		}

		[HarmonyPatch(typeof(ItemDatabase))]
		[HarmonyPatch("Start")]
		private class GenerateItemLists
		{
			public static List<Item>[]? ItemsByLevel { get; private set; }

			private static void Postfix()
			{
				if (RandomLootToggle.Value != Toggle.On || GameData.GM.DemoBuild)
				{
					return;
				}
				int num = 100;
				ItemsByLevel = new List<Item>[num + 1];
				for (int i = 0; i <= num; i++)
				{
					ItemsByLevel[i] = new List<Item>();
				}
				Item[] itemDB = GameData.ItemDB.ItemDB;
				foreach (Item val in itemDB)
				{
					if (val.ItemLevel >= 1 && val.ItemLevel <= num && val.ItemValue > 0)
					{
						ItemsByLevel[val.ItemLevel].Add(val);
					}
				}
				if (RandomLootDebug.Value != Toggle.On)
				{
					return;
				}
				string text = Path.Combine(Application.persistentDataPath, "ItemsByLevel.txt");
				using (StreamWriter streamWriter = new StreamWriter(text))
				{
					for (int k = 1; k < ItemsByLevel.Length; k++)
					{
						List<Item> list = ItemsByLevel[k];
						if (list == null || list.Count <= 0)
						{
							continue;
						}
						streamWriter.WriteLine($"{list.Count} Items at level {k}:");
						foreach (Item item in list)
						{
							string text2 = "";
							foreach (Class @class in item.Classes)
							{
								text2 = text2 + " " + ((Object)@class).name;
							}
							streamWriter.WriteLine($"- ID:{((BaseScriptableObject)item).Id} {item.ItemName} Relic:{item.Relic} Unique:{item.Unique} Classes:{text2} Slot:{((object)(SlotType)(ref item.RequiredSlot)).ToString()} Value:{item.ItemValue}");
						}
					}
				}
				Debug.Log((object)("ItemsByLevel written to: " + text));
			}
		}

		[HarmonyPatch(typeof(Character))]
		[HarmonyPatch("DoDeath")]
		[HarmonyPriority(2)]
		[HarmonyBefore(new string[] { "Brumdail.ErenshorQoL" })]
		private class AddRandomLoot
		{
			private static void Postfix(Character __instance)
			{
				//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
				if (RandomLootToggle.Value != Toggle.On || GameData.GM.DemoBuild || GenerateItemLists.ItemsByLevel == null || !__instance.isNPC || __instance.MyStats.Level < 1)
				{
					return;
				}
				bool flag = false;
				bool flag2 = false;
				bool flag3 = false;
				float num = 0f;
				LootTable component = ((Component)__instance).GetComponent<LootTable>();
				float num2 = 60f;
				int num3 = 0;
				int num4 = 0;
				_ = component.MaxNumberDrops;
				num3 = component.MaxNonCommonDrops;
				num4 = component.ActualDrops.Count;
				if (num3 > 2 || num4 > 2)
				{
					flag3 = true;
				}
				int num5 = __instance.MyStats.Level - RandomLootLevelsBelow.Value;
				if (num5 <= 0)
				{
					num5 = 1;
				}
				int num6 = __instance.MyStats.Level + RandomLootLevelsAbove.Value;
				if (num6 > 100)
				{
					num6 = 100;
				}
				if (num6 <= 0)
				{
					num6 = 1;
				}
				float num7 = Vector3.Distance(((Component)GameData.PlayerControl).transform.position, ((Component)__instance).transform.position);
				if (num7 < num2)
				{
					flag2 = true;
				}
				float num8 = Random.Range(0f, 100f);
				num = ((!flag3) ? RandomLootCommonNPCChance.Value : RandomLootRareNPCChance.Value);
				if (num8 <= num)
				{
					flag = true;
				}
				List<Item> list = new List<Item>();
				if (flag2 && flag)
				{
					for (int i = num5; i <= num6; i++)
					{
						list.AddRange(GenerateItemLists.ItemsByLevel[i]);
					}
					if (list != null && list.Count > 0)
					{
						Item val = list[Random.Range(0, list.Count)];
						component.ActualDrops.Add(val);
						if (RandomLootDebug.Value == Toggle.On)
						{
							UpdateSocialLog.LogAdd($"Generate Loot for {((Object)((Component)__instance).transform).name}: Level: {__instance.MyStats.Level} [{num5} to {num6}] containing {list.Count} items ", "yellow");
						}
						UpdateSocialLog.LogAdd(RandomLootText.Value + " + " + val.ItemName + " to " + ((Object)((Component)__instance).transform).name, "green");
					}
				}
				else if (RandomLootDebug.Value == Toggle.On)
				{
					string text = "";
					int num9 = 0;
					if (!flag2)
					{
						num9++;
						text += $"{num9}. NPC too far ({num7}).";
					}
					if (!flag)
					{
						num9++;
						text += $"{num9}. Roll of {num8} does not fall within required chance {num} (Rare Assumed:{flag3})";
					}
					UpdateSocialLog.LogAdd("DO NOT Generate Loot because:" + text, "yellow");
				}
			}
		}

		internal const string ModName = "ErenshorREL";

		internal const string ModVersion = "0.0.1";

		internal const string ModDescription = "Erenshor Random Epic Loot";

		internal const string Author = "Brumdail";

		private const string ModGUID = "Brumdail.ErenshorREL";

		private static string ConfigFileName = "Brumdail.ErenshorREL.cfg";

		private static string ConfigFileFullPath = Paths.ConfigPath + Path.DirectorySeparatorChar + ConfigFileName;

		internal static ErenshorREL context = null;

		private readonly Harmony harmony = new Harmony("Brumdail.ErenshorREL");

		public static readonly ManualLogSource ErenshorRELLogger = Logger.CreateLogSource("ErenshorREL");

		internal static ConfigEntry<Toggle> RandomLootToggle = null;

		internal static ConfigEntry<int> RandomLootLevelsBelow = null;

		internal static ConfigEntry<int> RandomLootLevelsAbove = null;

		internal static ConfigEntry<float> RandomLootCommonNPCChance = null;

		internal static ConfigEntry<float> RandomLootRareNPCChance = null;

		internal static ConfigEntry<string> RandomLootText = null;

		internal static ConfigEntry<Toggle> RandomLootDebug = null;

		internal static bool _configApplied;

		private void Awake()
		{
			context = this;
			Utilities.GenerateConfigs();
			harmony.PatchAll();
			SetupWatcher();
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void Update()
		{
			int num = 4;
			if (RandomLootLevelsBelow.Value < 0)
			{
				RandomLootLevelsBelow.Value = 0;
			}
			if (RandomLootLevelsBelow.Value > 100)
			{
				RandomLootLevelsBelow.Value = 100;
			}
			if (RandomLootLevelsAbove.Value < 0)
			{
				RandomLootLevelsAbove.Value = 0;
			}
			if (RandomLootLevelsAbove.Value > num)
			{
				RandomLootLevelsAbove.Value = num;
			}
			if (RandomLootCommonNPCChance.Value < 0f)
			{
				RandomLootCommonNPCChance.Value = 0f;
			}
			if (RandomLootCommonNPCChance.Value > 100f)
			{
				RandomLootCommonNPCChance.Value = 100f;
			}
			if (RandomLootRareNPCChance.Value < 0f)
			{
				RandomLootRareNPCChance.Value = 0f;
			}
			if (RandomLootRareNPCChance.Value > 100f)
			{
				RandomLootRareNPCChance.Value = 100f;
			}
			if (RandomLootText.Value == "" || RandomLootText.Value == null)
			{
				RandomLootText.Value = "Mysterious forces have added a new treasure: ";
			}
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				ErenshorRELLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				ErenshorRELLogger.LogError((object)("There was an issue loading your " + ConfigFileName));
				ErenshorRELLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		internal ConfigEntry<T> config<T>(string group, string name, T value, ConfigDescription description)
		{
			return ((BaseUnityPlugin)this).Config.Bind<T>(group, name, value, description);
		}

		internal ConfigEntry<T> config<T>(string group, string name, T value, string description)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			return ((BaseUnityPlugin)this).Config.Bind<T>(group, name, value, new ConfigDescription(description, (AcceptableValueBase)null, Array.Empty<object>()));
		}
	}
	public class Utilities
	{
		internal static void ApplyConfig()
		{
			ErenshorREL._configApplied = true;
		}

		internal static void GenerateConfigs()
		{
			ErenshorREL.RandomLootToggle = ErenshorREL.context.config("1 - RandomLoot", "Enable RandomLoot", ErenshorREL.Toggle.On, "This file is automatically generated by the Erenshor Random Epic Loot Mod plugin and is used to configure various mod features. You can revert to the defaults if you experience issues.\nEnable additional Random Loot?");
			ErenshorREL.RandomLootLevelsBelow = ErenshorREL.context.config("1 - RandomLoot", "Number of item levels below", 5, "The number of item levels to choose items from below the target NPC's level. Acceptable Range <int> 0-100");
			ErenshorREL.RandomLootLevelsAbove = ErenshorREL.context.config("1 - RandomLoot", "Number of item levels above", 1, "The number of item levels to choose items from above the target NPC's level. Acceptable Range <int> 0-4");
			ErenshorREL.RandomLootRareNPCChance = ErenshorREL.context.config("1 - RandomLoot", "Rare NPCS: Percent chance of a random loot", 50f, "The chance of adding an additional random drop. Acceptable Range <float> 0.0f-100.0f meaning 10.0f is 10% chance");
			ErenshorREL.RandomLootCommonNPCChance = ErenshorREL.context.config("1 - RandomLoot", "Common NPCS: Percent chance of a random loot", 3f, "The chance of adding an additional random drop. Acceptable Range <float> 0.0f-100.0f meaning 10.0f is 10% chance");
			ErenshorREL.RandomLootText = ErenshorREL.context.config("1 - RandomLoot", "Random Loot Text", "Mysterious forces have added a new treasure: ", "The text displayed when a new random item is added in addition to the item name and NPC name.");
			ErenshorREL.RandomLootDebug = ErenshorREL.context.config("1 - RandomLoot", "Debug Output", ErenshorREL.Toggle.Off, "Display debug messages and output of the random loot functions");
		}
	}
}