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));
}
}
}
}
}