Decompiled source of Sweet Broken Rifle v1.0.0

SweetBrokenRifle.dll

Decompiled 4 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
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: AssemblyVersion("0.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 SweetBrokenRifle
{
	[BepInPlugin("sweet.brokenrifle", "Sweet Broken Rifle", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "sweet.brokenrifle";

		internal const string RifleAssetName = "ItemRifle";

		private const string Section = "Misfire";

		internal static ConfigEntry<float> Spread;

		internal static ConfigEntry<int> ShotsMin;

		internal static ConfigEntry<int> ShotsMax;

		internal static ConfigEntry<int> PelletsMin;

		internal static ConfigEntry<int> PelletsMax;

		private void Awake()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			Spread = ((BaseUnityPlugin)this).Config.Bind<float>("Misfire", "Spread", 65f, new ConfigDescription("Spread cone angle in degrees.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 65f), Array.Empty<object>()));
			ShotsMin = BindCount("Shots before misfire, min", 4);
			ShotsMax = BindCount("Shots before misfire, max", 7);
			PelletsMin = BindCount("Pellets per misfire, min", 5);
			PelletsMax = BindCount("Pellets per misfire, max", 10);
			new Harmony("sweet.brokenrifle").PatchAll(typeof(BrokenShotPatch));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Sweet Broken Rifle loaded.");
		}

		private ConfigEntry<int> BindCount(string key, int defaultValue)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			return ((BaseUnityPlugin)this).Config.Bind<int>("Misfire", key, defaultValue, new ConfigDescription("Count, one at the least.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 50), Array.Empty<object>()));
		}
	}
	[HarmonyPatch(typeof(ItemGun), "ShootRPC")]
	internal static class BrokenShotPatch
	{
		private static readonly Dictionary<ItemGun, int> ShotsLeft = new Dictionary<ItemGun, int>();

		private static int _savedBullets;

		private static float _savedSpread;

		private static bool _broken;

		[HarmonyPrefix]
		private static void Prefix(ItemGun __instance)
		{
			_broken = false;
			if (SemiFunc.IsMasterClientOrSingleplayer() && IsRifle(__instance))
			{
				if (!ShotsLeft.TryGetValue(__instance, out var value))
				{
					value = NextInterval();
				}
				if (--value > 0)
				{
					ShotsLeft[__instance] = value;
					return;
				}
				ShotsLeft[__instance] = NextInterval();
				_savedBullets = __instance.numberOfBullets;
				_savedSpread = __instance.gunRandomSpread;
				__instance.numberOfBullets = RollBetween(Plugin.PelletsMin, Plugin.PelletsMax);
				__instance.gunRandomSpread = Plugin.Spread.Value;
				_broken = true;
			}
		}

		[HarmonyPostfix]
		private static void Postfix(ItemGun __instance)
		{
			if (_broken)
			{
				_broken = false;
				__instance.numberOfBullets = _savedBullets;
				__instance.gunRandomSpread = _savedSpread;
			}
		}

		private static int NextInterval()
		{
			return RollBetween(Plugin.ShotsMin, Plugin.ShotsMax);
		}

		private static int RollBetween(ConfigEntry<int> min, ConfigEntry<int> max)
		{
			return Random.Range(min.Value, Mathf.Max(min.Value, max.Value) + 1);
		}

		private static bool IsRifle(ItemGun gun)
		{
			ItemAttributes component = ((Component)gun).GetComponent<ItemAttributes>();
			if (Object.op_Implicit((Object)(object)component) && Object.op_Implicit((Object)(object)component.item))
			{
				return ((Object)component.item).name == "ItemRifle";
			}
			return false;
		}
	}
}