Decompiled source of CharmDiamond v1.0.1

CharmDiamond.dll

Decompiled 13 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using CharmCore.Api;
using CharmDiamond.Domain;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Pockets.Api;
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: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("Osmar Briones")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+40d10c55d3259a9ffcd6cfb62be58d2126d36b8b")]
[assembly: AssemblyProduct("CharmDiamond")]
[assembly: AssemblyTitle("CharmDiamond")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
	[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 CharmDiamond
{
	[BepInPlugin("com.osmar.CharmDiamond", "CharmDiamond", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class CharmDiamondPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.osmar.CharmDiamond";

		public const string PluginName = "CharmDiamond";

		public const string PluginVersion = "1.0.1";

		internal static ManualLogSource Logger { get; private set; }

		internal static DiamondCharm? Charm { get; private set; }

		private void Awake()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			Charm = new DiamondCharm();
			CharmsApi.Register((Charm)(object)Charm, ((BaseUnityPlugin)this).Config);
			new Harmony("com.osmar.CharmDiamond").PatchAll();
			Logger.LogInfo((object)"CharmDiamond 1.0.1 loaded!");
		}
	}
	internal sealed class DiamondCharm : Charm
	{
		internal const string PrefabName = "Valuable Wizard Diamond";

		public override string? Description => UnbreakableText.Describe();

		internal WearThrottle Throttle { get; } = new WearThrottle();

		internal CharmSetting WearCooldownSeconds { get; }

		internal DiamondCharm()
			: base("Diamond", "Diamond - Unbreakable", 8f, (float?)null, new string[1] { "Valuable Wizard Diamond" })
		{
			WearCooldownSeconds = ((Charm)this).AddSetting("WearCooldownSeconds", 0.5f, 0f, 10f, "Minimum time, in seconds, between wear charges for the same held valuable. Hits are always prevented; hits inside this window cost nothing extra, so one scrape is not billed many times. 0 charges every prevented hit.");
		}

		protected override void OnLevelEnded()
		{
			Throttle.Clear();
		}
	}
	internal static class Unbreakable
	{
		internal static bool ShouldBlock(PhysGrabObjectImpactDetector detector, bool forceBreak)
		{
			DiamondCharm charm = CharmDiamondPlugin.Charm;
			if (charm == null || forceBreak || (Object)(object)detector == (Object)null || !detector.isValuable)
			{
				return false;
			}
			PhysGrabObject grabObject = ((Component)detector).GetComponent<PhysGrabObject>();
			ValuableObject component = ((Component)detector).GetComponent<ValuableObject>();
			if ((Object)(object)grabObject == (Object)null || (Object)(object)component == (Object)null || grabObject.dead || grabObject.playerGrabbing.Count == 0)
			{
				return false;
			}
			bool held = false;
			ValuableObject diamond = null;
			CharmsApi.ForEachCarrier((Charm)(object)charm, (Action<PlayerAvatar, ValuableObject>)delegate(PlayerAvatar avatar, ValuableObject carried)
			{
				if (!held && !((Object)(object)avatar.physGrabber == (Object)null) && grabObject.playerGrabbing.Contains(avatar.physGrabber))
				{
					held = true;
					diamond = carried;
				}
			});
			if (!held || PocketsApi.IsPocketed(component))
			{
				return false;
			}
			if ((Object)(object)diamond != (Object)null && charm.Throttle.TryCharge(((Object)component).GetInstanceID(), Time.time, charm.WearCooldownSeconds.Value))
			{
				((Charm)charm).WearValuable(diamond, ((Charm)charm).WearPercent);
				if (((Charm)charm).DebugMode)
				{
					((Charm)charm).LogDebug($"Unbreakable: prevented a hit on {((Object)component).name} (value {component.dollarValueCurrent:0}); wore the Diamond {((Charm)charm).WearPercent:0.#}%");
				}
			}
			else if (((Charm)charm).DebugMode)
			{
				((Charm)charm).LogDebug("Unbreakable: prevented a hit on " + ((Object)component).name + " (no extra wear, inside the wear cooldown)");
			}
			return true;
		}
	}
}
namespace CharmDiamond.Patches
{
	[HarmonyPatch(typeof(PhysGrabObjectImpactDetector), "Break")]
	internal static class PhysGrabObjectImpactDetector_Break_Patch
	{
		[HarmonyPrefix]
		private static bool Prefix(PhysGrabObjectImpactDetector __instance, bool _forceBreak)
		{
			if (!SemiFunc.IsMasterClientOrSingleplayer())
			{
				return true;
			}
			return !Unbreakable.ShouldBlock(__instance, _forceBreak);
		}
	}
}
namespace CharmDiamond.Domain
{
	internal static class UnbreakableText
	{
		internal static string Describe()
		{
			return "Valuables you hold in your hands take no damage.";
		}
	}
	internal sealed class WearThrottle
	{
		private readonly Dictionary<int, float> lastCharge = new Dictionary<int, float>();

		internal bool TryCharge(int objectId, float now, float cooldownSeconds)
		{
			if (cooldownSeconds > 0f && lastCharge.TryGetValue(objectId, out var value) && now >= value && now - value < cooldownSeconds)
			{
				return false;
			}
			lastCharge[objectId] = now;
			return true;
		}

		internal void Clear()
		{
			lastCharge.Clear();
		}
	}
}