Decompiled source of More Shards And Coins v1.0.4

Erenshor.MoreShardsAndCoins.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
[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 MoreShardsAndCoins
{
	[HarmonyPatch(typeof(LootTable), "InitLootTable")]
	public static class LootTable_InitLootTable_ShardsTranspiler
	{
		private enum ItemKind
		{
			Unknown,
			Sivak,
			PlanarShard
		}

		private const float TargetLiteral = 0.001f;

		private const float Eps = 1E-07f;

		private static readonly MethodInfo MI_ConfigPlanar = AccessTools.Method(typeof(LootTable_InitLootTable_ShardsTranspiler), "ConfigPlanarChance", (Type[])null, (Type[])null);

		private static readonly MethodInfo MI_ConfigSivak = AccessTools.Method(typeof(LootTable_InitLootTable_ShardsTranspiler), "ConfigSivakruxChance", (Type[])null, (Type[])null);

		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Expected O, but got Unknown
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Expected O, but got Unknown
			if (MI_ConfigPlanar == null || MI_ConfigSivak == null)
			{
				Plugin.Log.LogError((object)"[MSC] Transpiler: Could not reflect config methods; aborting.");
				return instructions;
			}
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			List<CodeInstruction> list2 = new List<CodeInstruction>(list.Count);
			bool flag = false;
			bool flag2 = false;
			for (int i = 0; i < list.Count; i++)
			{
				CodeInstruction val = (CodeInstruction)(((object)list[i]) ?? ((object)new CodeInstruction(OpCodes.Nop, (object)null)));
				if (IsLdcR4(val, 0.001f) && i + 2 < list.Count && IsLoadLike(list[i + 1]) && list[i + 2]?.opcode == OpCodes.Mul)
				{
					ItemKind itemKind = PeekAheadForItemKind(list, i, 40);
					if (itemKind == ItemKind.Sivak && !flag)
					{
						list2.Add(new CodeInstruction(OpCodes.Call, (object)MI_ConfigSivak));
						i += 2;
						flag = true;
						continue;
					}
					if (itemKind == ItemKind.PlanarShard && !flag2)
					{
						list2.Add(new CodeInstruction(OpCodes.Call, (object)MI_ConfigPlanar));
						i += 2;
						flag2 = true;
						continue;
					}
				}
				list2.Add(val);
			}
			if (!flag)
			{
				Plugin.Log.LogWarning((object)"[MSC] Transpiler: Sivakrux threshold not replaced (0.001f * serverLootRate tied to GM.Sivak).");
			}
			if (!flag2)
			{
				Plugin.Log.LogWarning((object)"[MSC] Transpiler: Planar Shard threshold not replaced (0.001f * serverLootRate tied to GM.PlanarShard).");
			}
			return list2;
		}

		private static ItemKind PeekAheadForItemKind(List<CodeInstruction> src, int startIndex, int maxLookahead)
		{
			int num = Math.Min(src.Count - 1, startIndex + Math.Max(0, maxLookahead));
			for (int i = startIndex; i <= num; i++)
			{
				CodeInstruction val = src[i];
				if (val == null)
				{
					continue;
				}
				if (val.operand is FieldInfo fieldInfo && fieldInfo != null)
				{
					string name = fieldInfo.Name;
					if (name == "Sivak")
					{
						return ItemKind.Sivak;
					}
					if (name == "PlanarShard")
					{
						return ItemKind.PlanarShard;
					}
				}
				if (val.operand is MethodInfo methodInfo && methodInfo != null)
				{
					string name2 = methodInfo.Name;
					if (name2 == "get_Sivak")
					{
						return ItemKind.Sivak;
					}
					if (name2 == "get_PlanarShard")
					{
						return ItemKind.PlanarShard;
					}
				}
			}
			return ItemKind.Unknown;
		}

		private static bool IsLdcR4(CodeInstruction ci, float target)
		{
			if (ci == null)
			{
				return false;
			}
			if (ci.opcode != OpCodes.Ldc_R4)
			{
				return false;
			}
			if (ci.operand == null)
			{
				return false;
			}
			if (!(ci.operand is float num) || 1 == 0)
			{
				return false;
			}
			return Math.Abs(num - target) <= 1E-07f;
		}

		private static bool IsLoadLike(CodeInstruction ci)
		{
			if (ci == null)
			{
				return false;
			}
			OpCode opcode = ci.opcode;
			return opcode == OpCodes.Ldloc || opcode == OpCodes.Ldloc_S || opcode == OpCodes.Ldloc_0 || opcode == OpCodes.Ldloc_1 || opcode == OpCodes.Ldloc_2 || opcode == OpCodes.Ldloc_3 || opcode == OpCodes.Ldsfld || opcode == OpCodes.Ldfld || opcode == OpCodes.Ldarg || opcode == OpCodes.Ldarg_S || opcode == OpCodes.Ldarg_0 || opcode == OpCodes.Ldarg_1 || opcode == OpCodes.Ldarg_2 || opcode == OpCodes.Ldarg_3 || opcode == OpCodes.Call || opcode == OpCodes.Callvirt || opcode == OpCodes.Dup;
		}

		public static float ConfigPlanarChance()
		{
			try
			{
				float normalizedChance = Plugin.GetNormalizedChance(Plugin.PlanarShardDropChancePercent);
				return Clamp01(normalizedChance);
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"[MSC] ConfigPlanarChance error: {arg}");
				return 0.001f;
			}
		}

		public static float ConfigSivakruxChance()
		{
			try
			{
				float normalizedChance = Plugin.GetNormalizedChance(Plugin.SivakruxDropChancePercent);
				return Clamp01(normalizedChance);
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"[MSC] ConfigSivakruxChance error: {arg}");
				return 0.001f;
			}
		}

		private static float Clamp01(float v)
		{
			return (v < 0f) ? 0f : ((v > 1f) ? 1f : v);
		}
	}
	[BepInPlugin("et508.erenshor.moreshardsandcoins", "More Shards and Coins", "1.0.4")]
	public class Plugin : BaseUnityPlugin
	{
		public static ConfigEntry<float> PlanarShardDropChancePercent;

		public static ConfigEntry<float> SivakruxDropChancePercent;

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			PlanarShardDropChancePercent = ((BaseUnityPlugin)this).Config.Bind<float>("Drop Chance", "PlanarShardDropChancePercent", 0.001f, new ConfigDescription("Chance to drop Planar Stone Shards (0.0–100.0%). Default: 0.1. Reload scene or wait for new respawns for changes to apply.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			SivakruxDropChancePercent = ((BaseUnityPlugin)this).Config.Bind<float>("Drop Chance", "SivakruxDropChancePercent", 0.001f, new ConfigDescription("Chance to drop Sivakrux (0.0–100.0%). Default: 0.01. Reload scene or wait for new respawns for changes to apply.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			Harmony val = new Harmony("et508.erenshor.moreshardsandcoins");
			val.PatchAll();
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"More Shards and Coins loaded.");
		}

		public static float GetNormalizedChance(ConfigEntry<float> entry)
		{
			return Mathf.Clamp01(entry.Value / 100f);
		}
	}
}