Decompiled source of Terbium v0.0.4

BepInEx/patchers/me.loaforc.terbium.preloader.dll

Decompiled 6 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Terbium.Patches;
using Terbium.Util;

[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: AssemblyCompany("me.loaforc.terbium.preloader")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.0.4.0")]
[assembly: AssemblyInformationalVersion("0.0.4+cf44cec7887a913062fadb7de44f387df1c4c958")]
[assembly: AssemblyProduct("Terbium")]
[assembly: AssemblyTitle("me.loaforc.terbium.preloader")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.4.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 Terbium
{
	public static class TerbiumPreloader
	{
		internal static Stopwatch ChainloaderTimer;

		public static IEnumerable<string> TargetDLLs { get; } = new string[1] { "Assembly-CSharp.dll" };


		internal static ManualLogSource Logger { get; } = Logger.CreateLogSource("me.loaforc.terbium.preloader");


		public static void Patch(AssemblyDefinition assembly)
		{
		}

		public static void Finish()
		{
			Logger.LogInfo((object)"hewwo from the preloader! doing patches :3");
			Harmony harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "me.loaforc.terbium.preloader");
			ConfigFilePatch.ApplyRemoveSaveOnBindPatch(harmony);
			Logger.LogDebug((object)"Testing path privacy:");
			Logger.LogDebug((object)(Path.Combine(Path.GetTempPath(), "test_path") ?? ""));
			ChainloaderTimer = Stopwatch.StartNew();
			Logger.LogInfo((object)"preloader for terbium has loaded!1!1 yipee!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "me.loaforc.terbium.preloader";

		public const string PLUGIN_NAME = "Terbium";

		public const string PLUGIN_VERSION = "0.0.4";
	}
}
namespace Terbium.Util
{
	public static class ExtensionMethods
	{
		public static string GetName(this ConfigFile file)
		{
			return Path.GetFileNameWithoutExtension(file.ConfigFilePath);
		}
	}
}
namespace Terbium.Patches
{
	[HarmonyPatch(typeof(ConfigFile))]
	internal static class ConfigFilePatch
	{
		private static readonly List<ConfigFile> Configs = new List<ConfigFile>();

		internal static bool SavingAllowed = false;

		private static int SavesBlocked = 0;

		[HarmonyPrefix]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static void RegisterConfig(ConfigFile __instance)
		{
			Configs.Add(__instance);
		}

		internal static void ApplyRemoveSaveOnBindPatch(Harmony harmony)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Expected O, but got Unknown
			TerbiumPreloader.Logger.LogInfo((object)"doing silly stuff to patch ConfigFile.Bind :3");
			MethodInfo method = typeof(ConfigFilePatch).GetMethod("RemoveSaveOnBind");
			MethodInfo[] methods = typeof(ConfigFile).GetMethods();
			foreach (MethodInfo methodInfo in methods)
			{
				if (!(methodInfo.Name != "Bind") && methodInfo.IsGenericMethod)
				{
					harmony.Patch((MethodBase)methodInfo.MakeGenericMethod(typeof(float)), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(method), (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
		}

		public static IEnumerable<CodeInstruction> RemoveSaveOnBind(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			TerbiumPreloader.Logger.LogDebug((object)"Transpiler was fired!");
			CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
			{
				new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Call, (object)AccessTools.Method(typeof(ConfigFile), "Save", (Type[])null, (Type[])null), (string)null)
			});
			TerbiumPreloader.Logger.LogDebug((object)("IsValid? " + val.IsValid));
			if (val.IsValid)
			{
				TerbiumPreloader.Logger.LogInfo((object)"Patching the correct ConfigFile.Bind!");
				val = val.Advance(1).Set(OpCodes.Call, (object)AccessTools.Method(typeof(ConfigFilePatch), "Save", (Type[])null, (Type[])null));
			}
			return val.InstructionEnumeration();
		}

		internal static void Save(ConfigFile instance)
		{
			if (instance.SaveOnConfigSet)
			{
				if (SavingAllowed)
				{
					instance.Save();
				}
				else
				{
					SavesBlocked++;
				}
			}
		}

		internal static void SaveAll()
		{
			TerbiumPreloader.Logger.LogWarning((object)"saving ALL configs, lag spike probably!");
			Stopwatch stopwatch = Stopwatch.StartNew();
			if (!SavingAllowed)
			{
				TerbiumPreloader.Logger.LogError((object)"WOAH, Saving was disabled but .SaveAll() was called! Forcing it to TRUE.");
				SavingAllowed = true;
			}
			foreach (ConfigFile config in Configs)
			{
				if (config.Count != 0)
				{
					TerbiumPreloader.Logger.LogInfo((object)("Saving config: " + config.GetName()));
					config.Save();
				}
			}
			stopwatch.Stop();
			TerbiumPreloader.Logger.LogInfo((object)"===============");
			TerbiumPreloader.Logger.LogInfo((object)$"Took {stopwatch.ElapsedMilliseconds}ms to save {Configs.Count} config(s).");
			TerbiumPreloader.Logger.LogInfo((object)$"Blocked {SavesBlocked} un-necessary saves.");
		}
	}
	[HarmonyPatch(typeof(Logger))]
	internal static class LoggerPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch("LogMessage")]
		private static void ReenableAndSaveConfigs(object data)
		{
			if (data is string text && text == "Chainloader startup complete")
			{
				TerbiumPreloader.Logger.LogInfo((object)"Chainloader is done! Saving all configs and re-enabling saving!");
				ConfigFilePatch.SavingAllowed = true;
				ConfigFilePatch.SaveAll();
				TerbiumPreloader.ChainloaderTimer.Stop();
				TerbiumPreloader.Logger.LogInfo((object)$"Chainloader took {TerbiumPreloader.ChainloaderTimer.ElapsedMilliseconds}ms to load.");
			}
		}
	}
	[HarmonyPatch(typeof(ManualLogSource))]
	internal static class ManualLogSourcePatch
	{
		[HarmonyPrefix]
		[HarmonyWrapSafe]
		[HarmonyPatch("Log")]
		private static void HidePersonalInfoInPaths(ref object data)
		{
			data = data.ToString().Replace(Path.GetTempPath(), "%TEMP%" + Path.DirectorySeparatorChar).Replace(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "%USERPROFILE%" + Path.DirectorySeparatorChar)
				.Replace(Environment.UserName, "<user>");
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}