Decompiled source of BonBon v1.1.2

BepInEx/plugins/BonBon/BonBon.dll

Decompiled 3 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BonBon.Config;
using BonBon.Scripts;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
using Unity.Collections;
using Unity.Netcode;
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("BonBon")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BonBon")]
[assembly: AssemblyTitle("BonBon")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 BonBon
{
	[BepInPlugin("Entity378.BonBon", "BonBon", "1.1.2")]
	[BepInDependency("evaisa.lethallib", "0.14.2")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony harmony = new Harmony("Entity378.BonBon");

		private const string GUID = "Entity378.BonBon";

		private const string NAME = "BonBon";

		private const string VERSION = "1.1.2";

		private void Awake()
		{
			BonBonConfigs.LoadConfig(((BaseUnityPlugin)this).Config);
			Harmony.CreateAndPatchAll(typeof(BonBonConfigs), (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BonBonLog: Config loaded");
			string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "bonbon");
			AssetBundle val = AssetBundle.LoadFromFile(text);
			List<AudioClip> useVoicelinesSFX = new List<AudioClip>
			{
				val.LoadAsset<AudioClip>("Assets/SFX/BonBonUse01.ogg"),
				val.LoadAsset<AudioClip>("Assets/SFX/BonBonUse02.ogg"),
				val.LoadAsset<AudioClip>("Assets/SFX/BonBonUse03.ogg"),
				val.LoadAsset<AudioClip>("Assets/SFX/BonBonUse04.ogg"),
				val.LoadAsset<AudioClip>("Assets/SFX/BonBonUse05.ogg"),
				val.LoadAsset<AudioClip>("Assets/SFX/BonBonUse06.ogg"),
				val.LoadAsset<AudioClip>("Assets/SFX/BonBonUse07.ogg")
			};
			AudioClip errorSFX = val.LoadAsset<AudioClip>("Assets/SFX/BonBonError.ogg");
			AudioClip useButtonSFX = val.LoadAsset<AudioClip>("Assets/SFX/BonBonUseButton.ogg");
			AudioClip dropSFX = val.LoadAsset<AudioClip>("Assets/SFX/BonBonDrop.ogg");
			AudioClip grabSFX = val.LoadAsset<AudioClip>("Assets/SFX/BonBonGrab.ogg");
			Item val2 = val.LoadAsset<Item>("Assets/Items/BonBonItem.asset");
			BonBonScript bonBonScript = val2.spawnPrefab.AddComponent<BonBonScript>();
			((GrabbableObject)bonBonScript).itemProperties = val2;
			bonBonScript.UseVoicelinesSFX = useVoicelinesSFX;
			bonBonScript.errorSFX = errorSFX;
			bonBonScript.useButtonSFX = useButtonSFX;
			bonBonScript.dropSFX = dropSFX;
			bonBonScript.grabSFX = grabSFX;
			NetworkPrefabs.RegisterNetworkPrefab(val2.spawnPrefab);
			Utilities.FixMixerGroups(val2.spawnPrefab);
			Items.RegisterScrap(val2, BonBonConfigs.BonBonSpawnRate, (LevelTypes)(-1));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BonBonLog: BonBon, go get Him!");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "BonBon";

		public const string PLUGIN_NAME = "BonBon";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace BonBon.Scripts
{
	internal class BonBonScript : PhysicsProp
	{
		public List<AudioClip> UseVoicelinesSFX;

		public AudioClip errorSFX;

		public AudioClip useButtonSFX;

		public AudioClip dropSFX;

		public AudioClip grabSFX;

		private AudioSource audio;

		private float range = BonBonConfigs.BonBonRange;

		private int indexSFX = 0;

		public override void Start()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			((GrabbableObject)this).grabbable = true;
			((GrabbableObject)this).grabbableToEnemies = false;
			((GrabbableObject)this).insertedBattery = new Battery(false, 1f);
			((GrabbableObject)this).mainObjectRenderer = ((Component)this).GetComponent<MeshRenderer>();
			((GrabbableObject)this).useCooldown = BonBonConfigs.BonBonCooldown;
			audio = ((Component)this).GetComponent<AudioSource>();
			((GrabbableObject)this).Start();
		}

		public override void DiscardItem()
		{
			audio.Stop();
			audio.PlayOneShot(dropSFX);
			((GrabbableObject)this).DiscardItem();
		}

		public override void GrabItem()
		{
			audio.Stop();
			audio.PlayOneShot(grabSFX);
			((GrabbableObject)this).GrabItem();
		}

		public override void ItemActivate(bool used, bool buttonDown = true)
		{
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			audio.Stop();
			((GrabbableObject)this).ItemActivate(used, buttonDown);
			if (!buttonDown || !((Object)(object)((GrabbableObject)this).playerHeldBy != (Object)null))
			{
				return;
			}
			if (((GrabbableObject)this).insertedBattery.charge > 0f)
			{
				((GrabbableObject)this).insertedBattery.charge = ((GrabbableObject)this).insertedBattery.charge - BonBonConfigs.BonBonBatteryUsage;
				if (((GrabbableObject)this).insertedBattery.charge < 0f)
				{
					((GrabbableObject)this).insertedBattery.charge = 0f;
				}
				audio.PlayOneShot(useButtonSFX);
				audio.PlayOneShot(UseVoicelinesSFX[indexSFX]);
				if (indexSFX == 6)
				{
					indexSFX = 0;
				}
				else
				{
					indexSFX++;
				}
				Vector3 position = ((Component)this).transform.position;
				float bonBonStunTime = BonBonConfigs.BonBonStunTime;
				Collider[] array = Physics.OverlapSphere(position, range);
				for (int i = 0; i < array.Length; i++)
				{
					EnemyAI componentInParent = ((Component)array[i]).gameObject.GetComponentInParent<EnemyAI>();
					if ((Object)(object)componentInParent != (Object)null)
					{
						Debug.Log((object)("BonBonLog: BonBon stunned enemy " + ((Object)componentInParent).name));
						((MonoBehaviour)this).StartCoroutine(StunEnemy(componentInParent, bonBonStunTime));
					}
				}
				Debug.Log((object)"BonBonLog: BonBon used");
			}
			else
			{
				audio.PlayOneShot(errorSFX);
				Debug.Log((object)"BonBonLog: BonBon can't be used");
			}
		}

		private IEnumerator StunEnemy(EnemyAI Enemy, float StunTime)
		{
			Enemy.stunnedIndefinitely = 1;
			Enemy.SetEnemyStunned(true, 1f, (PlayerControllerB)null);
			Debug.Log((object)("BonBonLog:" + ((Object)Enemy).name + "Stunned"));
			yield return (object)new WaitForSeconds(StunTime);
			Enemy.stunnedIndefinitely = 0;
			Enemy.SetEnemyStunned(true, 0f, (PlayerControllerB)null);
			Debug.Log((object)("BonBonLog: " + ((Object)Enemy).name + "UnStunned"));
		}
	}
}
namespace BonBon.Config
{
	internal class BonBonConfigs
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static HandleNamedMessageDelegate <0>__OnRequestSync;

			public static HandleNamedMessageDelegate <1>__OnReceiveSync;
		}

		private const int byteDim = 4;

		private const int byteDimConfig = 21;

		private static int BonBonSpawnRateLocal = 10;

		private static float BonBonRangeLocal = 35f;

		private static float BonBonCooldownLocal = 10f;

		private static float BonBonStunTimeLocal = 7f;

		private static float BonBonBatteryUsageLocal = 0.34f;

		public static int BonBonSpawnRate = 10;

		public static float BonBonRange = 35f;

		public static float BonBonCooldown = 10f;

		public static float BonBonStunTime = 7f;

		public static float BonBonBatteryUsage = 0.34f;

		private static void SetValues(int SpawnRate, float Range, float Cooldown, float StunTime, float BatteryUsage)
		{
			BonBonSpawnRate = SpawnRate;
			BonBonRange = Range;
			BonBonCooldown = Cooldown;
			BonBonStunTime = StunTime;
			BonBonBatteryUsage = BatteryUsage;
		}

		private static void SetToLocalValues()
		{
			SetValues(BonBonSpawnRateLocal, BonBonRangeLocal, BonBonCooldownLocal, BonBonStunTimeLocal, BonBonBatteryUsageLocal);
		}

		public static void LoadConfig(ConfigFile config)
		{
			BonBonSpawnRateLocal = Math.Clamp(config.Bind<int>("General", "BonBonSpawnRate", 10, "Sets the BonBon spawn rate, the higher the value, the more common it will be (recommended to keep it between 5 and 40).").Value, 0, 9999);
			BonBonRangeLocal = Mathf.Clamp(config.Bind<int>("General", "BonBonRange", 35, "Sets the BonBon stun range.").Value, 0, 9999);
			BonBonCooldownLocal = Mathf.Clamp(config.Bind<int>("General", "BonBonCooldown", 10, "Sets the BonBon cooldown.").Value, 0, 9999);
			BonBonStunTimeLocal = Mathf.Clamp(config.Bind<int>("General", "BonBonStunTime", 7, "Sets the BonBon stun time.").Value, 0, 9999);
			BonBonBatteryUsageLocal = Mathf.Clamp(config.Bind<float>("General", "BonBonBatteryUsage", 0.34f, "Sets the BonBon battery usage (in the case of the default value it removes 34% of the battery with each use).").Value, 0f, 9999f);
			SetToLocalValues();
		}

		public static byte[] GetSettings()
		{
			byte[] array = new byte[21];
			array[0] = 1;
			Array.Copy(BitConverter.GetBytes(BonBonSpawnRateLocal), 0, array, 1, 4);
			Array.Copy(BitConverter.GetBytes(BonBonRangeLocal), 0, array, 5, 4);
			Array.Copy(BitConverter.GetBytes(BonBonCooldownLocal), 0, array, 9, 4);
			Array.Copy(BitConverter.GetBytes(BonBonStunTimeLocal), 0, array, 13, 4);
			Array.Copy(BitConverter.GetBytes(BonBonBatteryUsageLocal), 0, array, 17, 4);
			return array;
		}

		public static void SetSettings(byte[] data)
		{
			byte b = data[0];
			byte b2 = b;
			if (b2 == 1)
			{
				BonBonSpawnRate = BitConverter.ToInt32(data, 1);
				BonBonRange = BitConverter.ToSingle(data, 5);
				BonBonCooldown = BitConverter.ToSingle(data, 9);
				BonBonStunTime = BitConverter.ToSingle(data, 13);
				BonBonBatteryUsage = BitConverter.ToSingle(data, 17);
				Debug.Log((object)"BonBonLog: Host config set successfully");
				return;
			}
			throw new Exception("BonBonLog: Invalid version byte");
		}

		private static bool IsHost()
		{
			return NetworkManager.Singleton.IsHost;
		}

		public static void OnRequestSync(ulong clientID, FastBufferReader reader)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			if (!IsHost())
			{
				return;
			}
			Debug.Log((object)("BonBonLog: Sending config to client " + clientID));
			byte[] settings = GetSettings();
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(settings.Length, (Allocator)2, settings.Length);
			try
			{
				((FastBufferWriter)(ref val)).WriteBytes(settings, -1, 0);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BonBon_OnReceiveConfigSync", clientID, val, (NetworkDelivery)2);
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("BonBonLog: Failed to send config: " + ex));
			}
			finally
			{
				((FastBufferWriter)(ref val)).Dispose();
			}
		}

		public static void OnReceiveSync(ulong clientID, FastBufferReader reader)
		{
			Debug.Log((object)"BonBonLog: Received config from host");
			byte[] settings = new byte[21];
			try
			{
				((FastBufferReader)(ref reader)).ReadBytes(ref settings, 21, 0);
				SetSettings(settings);
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("BonBonLog: Failed to receive config: " + ex));
				SetToLocalValues();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		private static void ServerConnect()
		{
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			if (IsHost())
			{
				Debug.Log((object)"BonBonLog: Started hosting, using local settings");
				SetToLocalValues();
				CustomMessagingManager customMessagingManager = NetworkManager.Singleton.CustomMessagingManager;
				object obj = <>O.<0>__OnRequestSync;
				if (obj == null)
				{
					HandleNamedMessageDelegate val = OnRequestSync;
					<>O.<0>__OnRequestSync = val;
					obj = (object)val;
				}
				customMessagingManager.RegisterNamedMessageHandler("BonBon_OnRequestConfigSync", (HandleNamedMessageDelegate)obj);
				return;
			}
			Debug.Log((object)"BonBonLog: Connected to server, requesting settings");
			CustomMessagingManager customMessagingManager2 = NetworkManager.Singleton.CustomMessagingManager;
			object obj2 = <>O.<1>__OnReceiveSync;
			if (obj2 == null)
			{
				HandleNamedMessageDelegate val2 = OnReceiveSync;
				<>O.<1>__OnReceiveSync = val2;
				obj2 = (object)val2;
			}
			customMessagingManager2.RegisterNamedMessageHandler("BonBon_OnReceiveConfigSync", (HandleNamedMessageDelegate)obj2);
			FastBufferWriter val3 = default(FastBufferWriter);
			((FastBufferWriter)(ref val3))..ctor(4, (Allocator)2, -1);
			NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BonBon_OnRequestConfigSync", 0uL, val3, (NetworkDelivery)2);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
		private static void ServerDisconnect()
		{
			Debug.Log((object)"BonBonLog: Server disconnect");
			SetToLocalValues();
		}
	}
}