Decompiled source of Maxwell ScrapItem v1.0.2

BepInEx/plugins/MaxwellScrap.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using MaxwellScrap.NetcodePatcher;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
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: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("Kittenji")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MaxwellScrap")]
[assembly: AssemblyTitle("MaxwellScrap")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: NetcodePatchedAssembly]
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 MaxwellScrap
{
	[BepInPlugin("Kittenji.MaxwellScrap", "Maxwell Scrap", "1.0.2")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(GameNetworkManager))]
		internal class GameNetworkManagerPatch
		{
			[HarmonyPatch("Start")]
			[HarmonyPostfix]
			private static void StartPatch()
			{
				((Component)GameNetworkManager.Instance).GetComponent<NetworkManager>().AddNetworkPrefab(itemEntry.spawnPrefab);
			}
		}

		[HarmonyPatch(typeof(StartOfRound))]
		internal class StartOfRoundPatch
		{
			[HarmonyPatch("Awake")]
			[HarmonyPostfix]
			private static void AwakePatch(ref StartOfRound __instance)
			{
				//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f7: 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)itemEntry))
					{
						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(itemEntry, num);
					val.spawnableScrap.Add(item);
				}
				if (!__instance.allItemsList.itemsList.Contains(itemEntry))
				{
					__instance.allItemsList.itemsList.Add(itemEntry);
				}
			}
		}

		public const string pluginGUID = "Kittenji.MaxwellScrap";

		public const string pluginName = "Maxwell Scrap";

		public const string pluginVersion = "1.0.2";

		public static readonly Harmony harmony = new Harmony("Kittenji.MaxwellScrap");

		public static readonly Assets assets = new Assets();

		public static Item itemEntry;

		private const int DefaultSpawnRarity = 2;

		private void Awake()
		{
			itemEntry = assets.LoadByName<Item>("Maxwell.asset", nameOnly: false);
			harmony.PatchAll();
			InitNetcode();
		}

		private static void InitNetcode()
		{
			Type[] types = Assembly.GetExecutingAssembly().GetTypes();
			for (int i = 0; i < types.Length; i++)
			{
				MethodInfo[] methods = types[i].GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
				foreach (MethodInfo methodInfo in methods)
				{
					if (methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), inherit: false).Length != 0)
					{
						methodInfo.Invoke(null, null);
					}
				}
			}
		}
	}
	public static class ResourceUtils
	{
		public static Stream Get(string name)
		{
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			string[] manifestResourceNames = executingAssembly.GetManifestResourceNames();
			if (manifestResourceNames.Length == 0)
			{
				throw new FileNotFoundException("Assembly does not contain any resource stream names.");
			}
			string text = manifestResourceNames.FirstOrDefault((string n) => n.EndsWith(name));
			if (string.IsNullOrEmpty(text))
			{
				throw new FileNotFoundException("Assembly does not contain a resource stream ending with '" + name + "'");
			}
			return executingAssembly.GetManifestResourceStream(text);
		}

		public static string GetString(string name)
		{
			using Stream stream = Get(name);
			using StreamReader streamReader = new StreamReader(stream);
			return streamReader.ReadToEnd();
		}
	}
	public class Assets
	{
		private readonly AssetBundle mainAssetBundle;

		private readonly string[] allAssetNames;

		public Assets(string name = ".bundle")
		{
			mainAssetBundle = AssetBundle.LoadFromStream(ResourceUtils.Get(name));
			allAssetNames = mainAssetBundle.GetAllAssetNames();
		}

		public T Load<T>(string name) where T : Object
		{
			if (name == null)
			{
				return default(T);
			}
			return mainAssetBundle.LoadAsset<T>(name);
		}

		public T LoadByName<T>(string name, bool nameOnly = false) where T : Object
		{
			string name2 = allAssetNames.FirstOrDefault((string x) => (nameOnly ? Path.GetFileNameWithoutExtension(x) : Path.GetFileName(x)).Equals(name, StringComparison.OrdinalIgnoreCase));
			return Load<T>(name2);
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}
namespace __GEN
{
	internal class NetworkVariableSerializationHelper
	{
		[RuntimeInitializeOnLoadMethod]
		internal static void InitializeSerialization()
		{
		}
	}
}
namespace MaxwellScrap.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}