Decompiled source of DmbaiScrap v0.2.0

BepInEx/plugins/DmbaiScrap/DmbaiScrap.dll

Decompiled 11 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using LethalLib.Modules;
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: AssemblyCompany("DmbaiScrap")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DmbaiScrap")]
[assembly: AssemblyTitle("DmbaiScrap")]
[assembly: AssemblyVersion("1.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 DmbaiScrap
{
	[BepInPlugin("dmbai.dmbaiscrap", "Dmbai Scrap", "0.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInProcess("Lethal Company.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private sealed class ItemSettings
		{
			public ConfigEntry<bool> Enabled;

			public ConfigEntry<int> Rarity;
		}

		public const string ModGuid = "dmbai.dmbaiscrap";

		public const string ModName = "Dmbai Scrap";

		public const string ModVersion = "0.2.0";

		private const string BundleFileName = "dmbaiscrapassets";

		private const int DefaultRarity = 20;

		private AssetBundle assetBundle;

		private readonly Dictionary<string, ItemSettings> settingsByAssetName = new Dictionary<string, ItemSettings>(StringComparer.OrdinalIgnoreCase);

		private void Awake()
		{
			try
			{
				LoadAndRegisterAllScrap();
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Dmbai Scrap 0.2.0 loaded successfully.");
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)string.Format("Failed to load {0}:\n{1}", "Dmbai Scrap", arg));
			}
		}

		private void LoadAndRegisterAllScrap()
		{
			string? directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			if (string.IsNullOrWhiteSpace(directoryName))
			{
				throw new DirectoryNotFoundException("Could not determine the plugin folder.");
			}
			string text = Path.Combine(directoryName, "dmbaiscrapassets");
			if (!File.Exists(text))
			{
				throw new FileNotFoundException("AssetBundle was not found: " + text);
			}
			assetBundle = AssetBundle.LoadFromFile(text);
			if ((Object)(object)assetBundle == (Object)null)
			{
				throw new InvalidOperationException("Unity could not load AssetBundle: " + text);
			}
			Item[] array = (from @group in (from item in assetBundle.LoadAllAssets<Item>()
					where (Object)(object)item != (Object)null
					select item).GroupBy<Item, string>((Item item) => ((Object)item).name, StringComparer.OrdinalIgnoreCase)
				select @group.First()).OrderBy<Item, string>((Item item) => ((Object)item).name, StringComparer.OrdinalIgnoreCase).ToArray();
			if (array.Length == 0)
			{
				throw new FileNotFoundException("No Item assets were found inside dmbaiscrapassets.");
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("Found {0} Item assets inside {1}.", array.Length, "dmbaiscrapassets"));
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			Item[] array2 = array;
			foreach (Item val in array2)
			{
				try
				{
					ItemSettings itemSettings = BindItemSettings(val);
					ValidateItem(val);
					Utilities.FixMixerGroups(val.spawnPrefab);
					NetworkPrefabs.RegisterNetworkPrefab(val.spawnPrefab);
					if (!itemSettings.Enabled.Value)
					{
						num2++;
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Disabled scrap '" + GetDisplayName(val) + "' through config."));
						continue;
					}
					int num5 = Math.Max(0, itemSettings.Rarity.Value);
					if (num5 == 0)
					{
						num2++;
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Scrap '" + GetDisplayName(val) + "' has rarity 0 and will not spawn naturally."));
					}
					else
					{
						Items.RegisterScrap(val, num5, (LevelTypes)(-1));
						num++;
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Registered scrap '" + GetDisplayName(val) + "'. " + $"Asset: {((Object)val).name}; rarity: {num5}."));
					}
				}
				catch (Exception ex)
				{
					num3++;
					((BaseUnityPlugin)this).Logger.LogError((object)("Failed to register Item asset '" + ((val != null) ? ((Object)val).name : null) + "':\n" + ex));
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Scrap registration finished. " + $"Registered: {num}; " + $"disabled: {num2}; " + $"failed: {num3}."));
			if (num == 0 && num3 > 0)
			{
				throw new InvalidOperationException("No scrap items were registered successfully. Check the BepInEx log for item-specific errors.");
			}
		}

		private ItemSettings BindItemSettings(Item item)
		{
			string text = MakeConfigId(((Object)item).name);
			string text2 = "Items." + text;
			if (settingsByAssetName.TryGetValue(text, out var value))
			{
				return value;
			}
			string displayName = GetDisplayName(item);
			ItemSettings itemSettings = new ItemSettings
			{
				Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>(text2, "Enabled", true, "Whether '" + displayName + "' can spawn naturally. The network prefab is still registered when disabled."),
				Rarity = ((BaseUnityPlugin)this).Config.Bind<int>(text2, "Rarity", 20, "Relative spawn weight of '" + displayName + "' on all moons. This is not a percentage. 0 disables natural spawning; higher values make the item more common.")
			};
			settingsByAssetName.Add(text, itemSettings);
			return itemSettings;
		}

		private static void ValidateItem(Item item)
		{
			if ((Object)(object)item == (Object)null)
			{
				throw new ArgumentNullException("item");
			}
			if ((Object)(object)item.spawnPrefab == (Object)null)
			{
				throw new InvalidOperationException("Item asset '" + ((Object)item).name + "' has no Spawn Prefab assigned.");
			}
		}

		private static string GetDisplayName(Item item)
		{
			if (!((Object)(object)item != (Object)null) || string.IsNullOrWhiteSpace(item.itemName))
			{
				return ((item != null) ? ((Object)item).name : null) ?? "Unnamed Scrap";
			}
			return item.itemName.Trim();
		}

		private static string MakeConfigId(string assetName)
		{
			if (string.IsNullOrWhiteSpace(assetName))
			{
				return "UnnamedScrap";
			}
			string text = new string(assetName.Where(char.IsLetterOrDigit).ToArray());
			if (text.EndsWith("Item", StringComparison.OrdinalIgnoreCase))
			{
				text = text.Substring(0, text.Length - "Item".Length);
			}
			if (!string.IsNullOrWhiteSpace(text))
			{
				return text;
			}
			return "UnnamedScrap";
		}
	}
}