Decompiled source of Knight Mylo ScrapItem v1.0.0

BepInEx\plugins\KnightMyloScrap.dll

Decompiled 11 months ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("LaserTweaks")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LaserTweaks")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4963e0ea-7a99-4390-9cae-a4bdd01b6b43")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}
namespace Kittenji.KnightMyloScrap
{
	public class Assets
	{
		public static AssetBundle MainAssetBundle;

		public static Item Item;

		internal static Stream GetEmbededResource(string name)
		{
			return Assembly.GetExecutingAssembly().GetManifestResourceStream("Kittenji.KnightMyloScrap." + name);
		}

		public static void Init()
		{
			MainAssetBundle = AssetBundle.LoadFromStream(GetEmbededResource("mylo.bundle"));
			Item = MainAssetBundle.LoadAsset<Item>("Assets/Mods/_ForFriends/KnightMyloScrapItem/KnightMylo.asset");
		}
	}
	[BepInPlugin("Kittenji.KnightMyloScrap", "Knight Mylo Scrap", "1.0.0")]
	public class Loader : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(GameNetworkManager))]
		internal class GameNetworkManagerPatch
		{
			[HarmonyPatch("Start")]
			[HarmonyPostfix]
			private static void StartPatch()
			{
				((Component)GameNetworkManager.Instance).GetComponent<NetworkManager>().AddNetworkPrefab(Assets.Item.spawnPrefab);
				Debug.Log((object)"[MYLO] Added network prefab");
			}
		}

		[HarmonyPatch(typeof(StartOfRound))]
		internal class StartOfRoundPatch
		{
			[HarmonyPatch("Awake")]
			[HarmonyPostfix]
			private static void AwakePatch(ref StartOfRound __instance)
			{
				//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
				//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
				//IL_0103: Expected O, but got Unknown
				SelectableLevel[] levels = __instance.levels;
				foreach (SelectableLevel val in levels)
				{
					if (val.spawnableScrap.Any((SpawnableItemWithRarity x) => (Object)(object)x.spawnableItem == (Object)(object)Assets.Item))
					{
						continue;
					}
					int num = 2;
					SpawnableItemWithRarity val2 = val.spawnableScrap.Find((SpawnableItemWithRarity s) => Object.op_Implicit((Object)(object)s.spawnableItem) && Object.op_Implicit((Object)(object)s.spawnableItem.spawnPrefab) && ((Object)s.spawnableItem.spawnPrefab).name == "CashRegisterItem");
					if (val2 != null)
					{
						num = val2.rarity;
					}
					else if (val.spawnableScrap.Count > 0)
					{
						int num2 = int.MaxValue;
						foreach (SpawnableItemWithRarity item2 in val.spawnableScrap)
						{
							if (item2.rarity < num2)
							{
								num2 = item2.rarity;
							}
						}
						num = num2;
					}
					num = Mathf.Min(100, Mathf.Max(1, num));
					SpawnableItemWithRarity item = new SpawnableItemWithRarity
					{
						spawnableItem = Assets.Item,
						rarity = num
					};
					val.spawnableScrap.Add(item);
				}
				if (!__instance.allItemsList.itemsList.Contains(Assets.Item))
				{
					__instance.allItemsList.itemsList.Add(Assets.Item);
				}
			}
		}

		private const string modGUID = "Kittenji.KnightMyloScrap";

		private readonly Harmony harmony = new Harmony("Kittenji.KnightMyloScrap");

		private const int DefaultSpawnRarity = 2;

		private void Awake()
		{
			Assets.Init();
			harmony.PatchAll();
		}
	}
	public class MyloProp : GrabbableObject
	{
		public Animator catAnimator;

		public AudioSource audioSource;

		public AudioClip[] musicAudios;

		public AudioClip[] stopAudios;

		private Random musicRandomizer;

		private StartOfRound playersManager;

		private RoundManager roundManager;

		private bool isPlayingMusic;

		private float noiseInterval;

		private int timesPlayedWithoutTurningOff;

		public override void Start()
		{
			((GrabbableObject)this).Start();
			playersManager = Object.FindObjectOfType<StartOfRound>();
			roundManager = Object.FindObjectOfType<RoundManager>();
			musicRandomizer = new Random(playersManager.randomMapSeed - 10);
		}

		public override void ItemActivate(bool used, bool buttonDown = true)
		{
			((GrabbableObject)this).ItemActivate(used, buttonDown);
			StartMusic(used);
		}

		private void StartMusic(bool startMusic, bool pitchDown = false)
		{
			if (startMusic)
			{
				int num = musicRandomizer.Next(0, musicAudios.Length);
				audioSource.clip = musicAudios[num];
				audioSource.pitch = 1f;
				audioSource.Play();
				catAnimator.SetFloat("speed", audioSource.pitch);
				catAnimator.SetBool("animate", true);
				catAnimator.SetInteger("audio", num);
			}
			else if (isPlayingMusic)
			{
				if (pitchDown)
				{
					((MonoBehaviour)this).StartCoroutine(musicPitchDown());
				}
				else
				{
					audioSource.Stop();
					audioSource.PlayOneShot(stopAudios[Random.Range(0, stopAudios.Length)]);
					catAnimator.SetBool("animate", false);
				}
				timesPlayedWithoutTurningOff = 0;
			}
			base.isBeingUsed = startMusic;
			isPlayingMusic = startMusic;
		}

		private IEnumerator musicPitchDown()
		{
			int i = 0;
			while (i < 30)
			{
				yield return null;
				AudioSource obj = audioSource;
				obj.pitch -= 0.033f;
				if (audioSource.pitch <= 0f)
				{
					break;
				}
				int num = i;
				catAnimator.SetFloat("speed", audioSource.pitch);
				i = num + 1;
			}
			audioSource.Stop();
			audioSource.PlayOneShot(stopAudios[Random.Range(0, stopAudios.Length)]);
			catAnimator.SetBool("animate", false);
		}

		public override void UseUpBatteries()
		{
			((GrabbableObject)this).UseUpBatteries();
			StartMusic(startMusic: false, pitchDown: true);
		}

		public override void PocketItem()
		{
			((GrabbableObject)this).PocketItem();
			StartMusic(startMusic: false);
		}

		public override void Update()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			((GrabbableObject)this).Update();
			if (isPlayingMusic)
			{
				if (noiseInterval <= 0f)
				{
					noiseInterval = 1f;
					timesPlayedWithoutTurningOff++;
					roundManager.PlayAudibleNoise(((Component)this).transform.position, 16f, 0.9f, timesPlayedWithoutTurningOff, false, 5);
				}
				else
				{
					noiseInterval -= Time.deltaTime;
				}
				if (base.insertedBattery.charge < 0.05f)
				{
					audioSource.pitch = 1f - (0.05f - base.insertedBattery.charge) * 4f;
					catAnimator.SetFloat("speed", audioSource.pitch);
				}
			}
		}
	}
}