Decompiled source of ComponentBundler v1.0.1

BepInEx/patchers/ComponentBundler.Preloader.dll

Decompiled 5 months 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 System.Security;
using System.Security.Permissions;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Newtonsoft.Json;

[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("Kesomannen")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Preload patcher for ComponentBundler.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ComponentBundler")]
[assembly: AssemblyTitle("ComponentBundler.Preloader")]
[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 ComponentBundler.Preloader
{
	public static class ComponentBundlingPreloader
	{
		internal static readonly ManualLogSource Logger = Logger.CreateLogSource("ComponentBundler.Preloader");

		internal static readonly Dictionary<string, List<string>> BundledComponents = new Dictionary<string, List<string>>();

		public const string TargetMethodName = "Awake";

		public static IReadOnlyCollection<string> TargettedComponentFullNames => BundledComponents.Keys;

		public static bool Bundle(AssemblyDefinition targetAssembly, string targetFullName, string toAddFullName)
		{
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Expected O, but got Unknown
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			if (BundledComponents.TryGetValue(targetFullName, out var value))
			{
				value.Add(toAddFullName);
				Logger.LogInfo((object)("Bundled " + toAddFullName + " with " + targetFullName));
				return true;
			}
			TypeDefinition type = targetAssembly.MainModule.GetType(targetFullName);
			if (type == null)
			{
				string text = "Type " + targetFullName + " not found in assembly " + ((AssemblyNameReference)targetAssembly.Name).Name + ".";
				if (!targetFullName.Contains('.'))
				{
					text += " Did you forget to include the namespace?";
				}
				Logger.LogError((object)text);
				return false;
			}
			if (((IEnumerable<MethodDefinition>)type.Methods).All((MethodDefinition m) => ((MemberReference)m).Name != "Awake"))
			{
				MethodDefinition val = new MethodDefinition("Awake", (MethodAttributes)129, targetAssembly.MainModule.TypeSystem.Void);
				type.Methods.Add(val);
				val.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
				Logger.LogInfo((object)("Added Awake to " + targetFullName));
			}
			BundledComponents.Add(targetFullName, new List<string> { toAddFullName });
			Logger.LogInfo((object)("Bundled " + toAddFullName + " with " + targetFullName));
			return true;
		}

		public static IEnumerable<string> GetBundle(string targetComponentFullName)
		{
			if (!BundledComponents.TryGetValue(targetComponentFullName, out var value))
			{
				return Enumerable.Empty<string>();
			}
			return value;
		}

		public static bool TryGetBundle(string targetComponentFullName, out IEnumerable<string> bundle)
		{
			if (BundledComponents.TryGetValue(targetComponentFullName, out var value))
			{
				bundle = value;
				return true;
			}
			bundle = Enumerable.Empty<string>();
			return false;
		}
	}
	public static class Patcher
	{
		public static IEnumerable<string> TargetDLLs
		{
			get
			{
				yield return "Assembly-CSharp.dll";
			}
		}

		public static void Patch(AssemblyDefinition assembly)
		{
			string text = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			do
			{
				text = Path.Combine(text, "..");
			}
			while (!Directory.Exists(Path.Combine(text, "plugins")));
			string[] directories = Directory.GetDirectories(text);
			foreach (string obj in directories)
			{
				SearchDirectory(obj);
				string[] directories2 = Directory.GetDirectories(obj);
				for (int j = 0; j < directories2.Length; j++)
				{
					SearchDirectory(directories2[j]);
				}
			}
			void SearchDirectory(string directory)
			{
				//IL_0087: Expected O, but got Unknown
				string[] files = Directory.GetFiles(directory, "bundler_config.json");
				if (files.Length == 0)
				{
					return;
				}
				try
				{
					foreach (KeyValuePair<string, string[]> item in JsonConvert.DeserializeObject<Dictionary<string, string[]>>(File.ReadAllText(files[0])))
					{
						item.Deconstruct(out var key, out var value);
						string targetFullName = key;
						value = value;
						foreach (string toAddFullName in value)
						{
							ComponentBundlingPreloader.Bundle(assembly, targetFullName, toAddFullName);
						}
					}
				}
				catch (JsonException val)
				{
					JsonException val2 = val;
					ComponentBundlingPreloader.Logger.LogError((object)("Failed to parse config file in " + directory + ": " + ((Exception)(object)val2).Message));
				}
			}
		}
	}
}

BepInEx/plugins/ComponentBundler.dll

Decompiled 5 months 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.Logging;
using ComponentBundler.Preloader;
using HarmonyLib;
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("Kesomannen")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Allows you to bundle your own components with built-in ones.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ComponentBundler")]
[assembly: AssemblyTitle("ComponentBundler")]
[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 ComponentBundler
{
	[BepInPlugin("ComponentBundler", "ComponentBundler", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private static readonly Dictionary<Type, Type[]> _loadedBundles = new Dictionary<Type, Type[]>();

		internal static ManualLogSource Log { get; } = Logger.CreateLogSource("ComponentBundler");


		private void Awake()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			Harmony val = new Harmony("ComponentBundler");
			HarmonyMethod val2 = new HarmonyMethod(typeof(Plugin), "Awake_Prefix", (Type[])null);
			IEnumerable<string> source = default(IEnumerable<string>);
			foreach (string targettedComponentFullName in ComponentBundlingPreloader.TargettedComponentFullNames)
			{
				Type type = AccessTools.TypeByName(targettedComponentFullName);
				MethodInfo methodInfo = AccessTools.Method(type, "Awake", (Type[])null, (Type[])null);
				if (methodInfo == null)
				{
					Log.LogError((object)("Method " + targettedComponentFullName + ".Awake not found, please report this to the mod author"));
					continue;
				}
				if (!ComponentBundlingPreloader.TryGetBundle(targettedComponentFullName, ref source))
				{
					Log.LogWarning((object)("No bundle found for " + targettedComponentFullName + ". Skipping..."));
					continue;
				}
				_loadedBundles[type] = (from t in source.Select(ValidateBundledType)
					where t != null
					select t).ToArray();
				val.Patch((MethodBase)methodInfo, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			Log.LogInfo((object)"Plugin loaded!");
		}

		private Type ValidateBundledType(string fullName)
		{
			Type type = AccessTools.TypeByName(fullName);
			if (type == null)
			{
				Log.LogError((object)("Bundled type " + fullName + " not found!"));
				return null;
			}
			if (!type.IsSubclassOf(typeof(MonoBehaviour)))
			{
				Log.LogError((object)$"Bundled type {type} is not a MonoBehaviour!");
				return null;
			}
			return type;
		}

		private static void Awake_Prefix(Component __instance)
		{
			Type[] array = _loadedBundles[((object)__instance).GetType()];
			foreach (Type type in array)
			{
				__instance.gameObject.AddComponent(type);
			}
		}
	}
	public class TestComponent
	{
		private void Awake()
		{
			Debug.Log((object)"TestComponent.Awake");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "ComponentBundler";

		public const string PLUGIN_NAME = "ComponentBundler";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}