Decompiled source of 67 Leaf Clover v1.0.0

plugins/Clover67/Clover67.dll

Decompiled 10 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using R2API;
using RoR2;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Clover67")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Clover67")]
[assembly: AssemblyTitle("Clover67")]
[assembly: AssemblyVersion("1.0.0.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 Clover67
{
	[BepInPlugin("com.carlo.clover67", "67 Leaf Clover", "0.1.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Id = "com.carlo.clover67";

		private Harmony patches;

		internal static ManualLogSource Diagnostics;

		private void Awake()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			Diagnostics = ((BaseUnityPlugin)this).Logger;
			LanguageAPI.Add("ITEM_CLOVER_NAME", "67 Leaf Clover");
			LanguageAPI.Add("ITEM_CLOVER_PICKUP", "Luck grows every 5 levels gained while holding this item.");
			LanguageAPI.Add("ITEM_CLOVER_DESC", "Gain <style=cIsUtility>+1 luck</style> <style=cStack>(+1 per stack)</style>. Every <style=cIsUtility>5 levels gained</style> after your first pickup grants another <style=cIsUtility>+1 luck</style> <style=cStack>(+1 per stack)</style>. Additional copies share your progress. Losing all copies resets progress.");
			patches = new Harmony("com.carlo.clover67");
			patches.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"67 Leaf Clover 0.1.2 loaded. First in-game validation required.");
		}

		private void OnDestroy()
		{
			Harmony obj = patches;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
	public sealed class CloverProgress : MonoBehaviour
	{
		private bool holding;

		private bool hasLevel;

		private int lastLevel;

		private int levelsGained;

		private int appliedBonus;

		private string lastReport;

		public void Refresh(CharacterMaster master, bool vanillaLuckRebuilt)
		{
			if (vanillaLuckRebuilt)
			{
				appliedBonus = 0;
			}
			int num = (Object.op_Implicit((Object)(object)master.inventory) ? master.inventory.GetItemCountPermanent(Items.Clover) : 0);
			CharacterBody body = master.GetBody();
			if (num <= 0)
			{
				master.luck -= (float)appliedBonus;
				appliedBonus = 0;
				if (holding)
				{
					Plugin.Diagnostics.LogInfo((object)$"[Clover67 TEST] master={((Object)master).GetInstanceID()} All clovers removed; progress reset; actualLuck={master.luck}");
				}
				lastReport = null;
				holding = false;
				hasLevel = false;
				levelsGained = 0;
				return;
			}
			if (!holding)
			{
				holding = true;
				hasLevel = false;
				levelsGained = 0;
			}
			if (Object.op_Implicit((Object)(object)body))
			{
				int num2 = Mathf.FloorToInt(body.level);
				if (hasLevel && num2 > lastLevel)
				{
					levelsGained += num2 - lastLevel;
				}
				lastLevel = num2;
				hasLevel = true;
			}
			int num3 = num * (levelsGained / 5);
			master.luck += (float)(num3 - appliedBonus);
			appliedBonus = num3;
			string text = $"master={((Object)master).GetInstanceID()} level={lastLevel} copies={num} levelsSincePickup={levelsGained} growthBonus={num3} actualLuck={master.luck}";
			if (text != lastReport)
			{
				Plugin.Diagnostics.LogInfo((object)("[Clover67 TEST] " + text));
				lastReport = text;
			}
		}

		private void FixedUpdate()
		{
			CharacterMaster component = ((Component)this).GetComponent<CharacterMaster>();
			if (Object.op_Implicit((Object)(object)component))
			{
				Refresh(component, vanillaLuckRebuilt: false);
			}
		}
	}
	[HarmonyPatch(typeof(CharacterMaster), "OnInventoryChanged")]
	internal static class InventoryPatch
	{
		private static void Postfix(CharacterMaster __instance)
		{
			CloverProgress cloverProgress = ((Component)__instance).GetComponent<CloverProgress>();
			if (!Object.op_Implicit((Object)(object)cloverProgress) && Object.op_Implicit((Object)(object)__instance.inventory) && __instance.inventory.GetItemCountPermanent(Items.Clover) > 0)
			{
				cloverProgress = ((Component)__instance).gameObject.AddComponent<CloverProgress>();
			}
			if (Object.op_Implicit((Object)(object)cloverProgress))
			{
				cloverProgress.Refresh(__instance, vanillaLuckRebuilt: false);
			}
		}
	}
	[HarmonyPatch(typeof(CharacterBody), "RecalculateStats")]
	internal static class StatsPatch
	{
		private static void Postfix(CharacterBody __instance)
		{
			CharacterMaster master = __instance.master;
			if (Object.op_Implicit((Object)(object)master))
			{
				CloverProgress cloverProgress = ((Component)master).GetComponent<CloverProgress>();
				if (!Object.op_Implicit((Object)(object)cloverProgress) && Object.op_Implicit((Object)(object)master.inventory) && master.inventory.GetItemCountPermanent(Items.Clover) > 0)
				{
					cloverProgress = ((Component)master).gameObject.AddComponent<CloverProgress>();
				}
				if (Object.op_Implicit((Object)(object)cloverProgress))
				{
					cloverProgress.Refresh(master, vanillaLuckRebuilt: true);
				}
			}
		}
	}
}