Decompiled source of XL Scrap v0.2.0

XLScrap.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LethalLevelLoader;
using Microsoft.CodeAnalysis;
using UnityEngine;
using XLScrap.NetcodePatcher;
using XLScrapApi.Models;

[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("Henit3")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Adds example XL Scrap")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyInformationalVersion("0.2.0+a07148bff75cd0b7114f4a517d257cc94d66572e")]
[assembly: AssemblyProduct("XL Scrap")]
[assembly: AssemblyTitle("XLScrap")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.2.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: NetcodePatchedAssembly]
internal class <Module>
{
	static <Module>()
	{
	}
}
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 XlScrap
{
	public class ConfigValues
	{
		public ConfigEntry<string> CrtTvSpawnWeight { get; private set; }

		public ConfigEntry<string> CrtTvValueRange { get; private set; }

		public ConfigEntry<string> CouchSpawnWeight { get; private set; }

		public ConfigEntry<string> CouchValueRange { get; private set; }

		public ConfigEntry<string> LCouchSpawnWeight { get; private set; }

		public ConfigEntry<string> LCouchValueRange { get; private set; }

		public ConfigEntry<bool> SensibleSpawns { get; private set; }

		public ConfigValues(ConfigFile configFile)
		{
			BindSpawnWeights(configFile);
			BindScrapValues(configFile);
			BindMisc(configFile);
			ClearUnusedEntries(configFile);
		}

		private void BindSpawnWeights(ConfigFile configFile)
		{
			CrtTvSpawnWeight = configFile.Bind<string>("Spawn Weights", "CRT TV", "Vanilla:50, Custom:50", "Spawn Weight of CRT TV specified as comma-separated \"key:weight\" pairs");
			CouchSpawnWeight = configFile.Bind<string>("Spawn Weights", "Couch", "Vanilla:50, Custom:50", "Spawn Weight of Couch specified as comma-separated \"key:weight\" pairs");
			LCouchSpawnWeight = configFile.Bind<string>("Spawn Weights", "L Couch", "Vanilla:0, Custom:0", "Spawn Weight of L Couch (UNFINISHED) specified as comma-separated \"key:weight\" pairs");
		}

		private void BindScrapValues(ConfigFile configFile)
		{
			CrtTvValueRange = configFile.Bind<string>("Scrap Values", "CRT TV", "160-240", "Scrap Value of CRT TV specified as a \"min-max\" range");
			CouchValueRange = configFile.Bind<string>("Scrap Values", "Couch", "180-260", "Scrap Value of Couch specified as a \"min-max\" range");
			LCouchValueRange = configFile.Bind<string>("Scrap Values", "L Couch", "280-360", "Scrap Value of L Couch (UNFINISHED) specified as a \"min-max\" range");
		}

		private void BindMisc(ConfigFile configFile)
		{
			SensibleSpawns = configFile.Bind<bool>("Miscellaneous", "Sensible Spawn Positions", true, "Whether these items should make use of two handed spawn positions");
		}

		private void ClearUnusedEntries(ConfigFile configFile)
		{
			PropertyInfo property = ((object)configFile).GetType().GetProperty("OrphanedEntries", BindingFlags.Instance | BindingFlags.NonPublic);
			Dictionary<ConfigDefinition, string> dictionary = (Dictionary<ConfigDefinition, string>)property.GetValue(configFile, null);
			dictionary.Clear();
			configFile.Save();
		}
	}
	[BepInPlugin("XLScrap", "XL Scrap", "0.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static ConfigValues Config;

		internal static ManualLogSource Logger;

		private static readonly Harmony Harmony = new Harmony("XLScrap");

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin XLScrap (0.2.0) is loading...");
			Config = new ConfigValues(((BaseUnityPlugin)this).Config);
			AssetBundleLoader.AddOnExtendedModLoadedListener((Action<ExtendedMod>)OnExtendedModRegistered, (string)null, "XlScrap");
			AssetBundleLoader.AddOnLethalBundleLoadedListener((Action<AssetBundle>)OnLethalBundleLoaded, "xl_scrap.lethalbundle");
			NetcodePatcher();
			Logger.LogInfo((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogInfo((object)"Patching complete!");
			Logger.LogInfo((object)"Plugin XLScrap (0.2.0) is loaded!");
		}

		private static void NetcodePatcher()
		{
			Type[] array;
			try
			{
				array = Assembly.GetExecutingAssembly().GetTypes();
			}
			catch (ReflectionTypeLoadException ex)
			{
				array = ex.Types.Where((Type t) => t != null).ToArray();
			}
			Type[] array2 = array;
			foreach (Type type in array2)
			{
				MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
				MethodInfo[] array3 = methods;
				foreach (MethodInfo methodInfo in array3)
				{
					object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), inherit: false);
					if (customAttributes.Length != 0)
					{
						methodInfo.Invoke(null, null);
					}
				}
			}
		}

		private static void OnExtendedModRegistered(ExtendedMod extendedMod)
		{
			if ((Object)(object)extendedMod == (Object)null)
			{
				return;
			}
			Logger.LogInfo((object)"OnExtendedModRegistered");
			foreach (ExtendedItem extendedItem in extendedMod.ExtendedItems)
			{
				if (extendedItem.Item.itemName == "XL Holder")
				{
					continue;
				}
				List<StringWithRarity> configSpawnWeightForItem = GetConfigSpawnWeightForItem(extendedItem.Item.itemName);
				if (configSpawnWeightForItem != null)
				{
					extendedItem.LevelMatchingProperties.levelTags.AddRange(configSpawnWeightForItem);
					extendedItem.LevelMatchingProperties.planetNames.AddRange(configSpawnWeightForItem);
					Logger.LogInfo((object)$"Updated matching properties for {extendedItem.Item}.");
				}
				(int, int)? configValueRangeForItem = GetConfigValueRangeForItem(extendedItem.Item.itemName);
				if (configValueRangeForItem.HasValue)
				{
					var (num, num2) = configValueRangeForItem.GetValueOrDefault();
					if (true)
					{
						extendedItem.Item.minValue = (int)((double)num / 0.4);
						extendedItem.Item.maxValue = (int)((double)num2 / 0.4);
						Logger.LogInfo((object)$"Updated value range properties for {extendedItem.Item}.");
					}
				}
			}
		}

		private static List<StringWithRarity> GetConfigSpawnWeightForItem(string itemName)
		{
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Expected O, but got Unknown
			if (1 == 0)
			{
			}
			string text = itemName switch
			{
				"CRT TV" => Config.CrtTvSpawnWeight.Value, 
				"Couch" => Config.CouchSpawnWeight.Value, 
				"L Couch" => Config.LCouchSpawnWeight.Value, 
				_ => null, 
			};
			if (1 == 0)
			{
			}
			string text2 = text;
			if (text2 == null)
			{
				Logger.LogWarning((object)("No spawn weight configuration found for item type: " + itemName));
				return null;
			}
			List<StringWithRarity> list = new List<StringWithRarity>();
			string[] array = text2.Split(',');
			foreach (string text3 in array)
			{
				string[] array2 = text3.Trim().Split(':');
				if (array2.Length == 2 && int.TryParse(array2[1], out var result))
				{
					string text4 = array2[0];
					list.Add(new StringWithRarity(text4, result));
					Logger.LogInfo((object)$"Registered spawn rate for {text4} to {result}");
				}
			}
			return list;
		}

		private static (int Min, int Max)? GetConfigValueRangeForItem(string itemName)
		{
			if (1 == 0)
			{
			}
			string text = itemName switch
			{
				"CRT TV" => Config.CrtTvValueRange.Value, 
				"Couch" => Config.CouchValueRange.Value, 
				"L Couch" => Config.LCouchValueRange.Value, 
				_ => null, 
			};
			if (1 == 0)
			{
			}
			string text2 = text;
			if (text2 == null)
			{
				Logger.LogWarning((object)("No value range configuration found for item type: " + itemName));
				return null;
			}
			string[] array = text2.Split('-');
			if (array.Length != 2 || !int.TryParse(array[0].Trim(), out var result) || !int.TryParse(array[1].Trim(), out var result2) || result > result2)
			{
				return null;
			}
			return (result, result2);
		}

		private static void OnLethalBundleLoaded(AssetBundle assetBundle)
		{
			_ = (Object)(object)assetBundle == (Object)null;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "XLScrap";

		public const string PLUGIN_NAME = "XL Scrap";

		public const string PLUGIN_VERSION = "0.2.0";
	}
}
namespace XlScrap.Patches.StartOfRoundPatch
{
	[HarmonyPatch(typeof(StartOfRound), "Awake")]
	public class AwakePatch
	{
		private static readonly string[] _xlItemsInThisMod = new string[3] { "CRT TV", "Couch", "L Couch" };

		[HarmonyPostfix]
		public static void Postfix(StartOfRound __instance)
		{
			if (!Plugin.Config.SensibleSpawns.Value)
			{
				return;
			}
			ItemGroup item = __instance.allItemsList.itemsList.SelectMany((Item x) => x.spawnPositionTypes).First((ItemGroup x) => ((Object)x).name == "GeneralItemClass");
			IEnumerable<XLMainItem> enumerable = from x in Resources.FindObjectsOfTypeAll<XLMainItem>()
				where _xlItemsInThisMod.Contains(((GrabbableObject)x).itemProperties.itemName)
				select x;
			foreach (XLMainItem item2 in enumerable)
			{
				((GrabbableObject)item2).itemProperties.spawnPositionTypes.Clear();
				((GrabbableObject)item2).itemProperties.spawnPositionTypes.Add(item);
			}
		}
	}
}
namespace XLScrap.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}