Decompiled source of SwapperPlugin v1.2.1

Plugins/SwapperPlugin.dll

Decompiled a week 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 MelonLoader;
using MelonLoader.Utils;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Collections.Generic;
using SwapperPlugin;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(Plugin), "SwapperPlugin", "1.0.0", "coolpaca", null)]
[assembly: MelonColor(255, 149, 82, 255)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SwapperPlugin")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+d6eeb8e3a7fc17539ef7c99444f7d4e2674a65f4")]
[assembly: AssemblyProduct("SwapperPlugin")]
[assembly: AssemblyTitle("SwapperPlugin")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 SwapperPlugin
{
	internal enum Backend
	{
		Unknown,
		Il2cpp,
		Mono,
		S1API
	}
	internal class AssemblyChecker
	{
		public static string[] monoRefs = new string[2] { "scheduleone", "system" };

		public static string[] il2cppRefs = new string[2] { "il2cppscheduleone", "il2cppsystem" };

		public static Backend CheckAtPath(string path)
		{
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			string directoryName = Path.GetDirectoryName(path);
			if (!File.Exists(path))
			{
				return Backend.Unknown;
			}
			if (!path.Contains(".dll"))
			{
				return Backend.Unknown;
			}
			if (Path.GetFileName(path).ToLower().Contains("mono"))
			{
				return Backend.Mono;
			}
			if (Path.GetFileName(path).ToLower().Contains("il2cpp"))
			{
				return Backend.Il2cpp;
			}
			AssemblyDefinition val = AssemblyDefinition.ReadAssembly(path);
			Enumerator<ModuleDefinition> enumerator = val.Modules.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					ModuleDefinition current = enumerator.Current;
					IEnumerable<TypeReference> typeReferences = current.GetTypeReferences();
					if (typeReferences.Any((TypeReference x) => x.Namespace.ToLower().Contains("s1api")))
					{
						return Backend.S1API;
					}
					if (typeReferences.Any((TypeReference x) => il2cppRefs.Contains(x.Namespace.ToLower())))
					{
						return Backend.Il2cpp;
					}
					if (typeReferences.Any((TypeReference x) => monoRefs.Contains(x.Namespace.ToLower())))
					{
						return Backend.Mono;
					}
				}
			}
			finally
			{
				((IDisposable)enumerator).Dispose();
			}
			return Backend.Unknown;
		}
	}
	public class Plugin : MelonPlugin
	{
		private static Backend backend;

		public override void OnPreInitialization()
		{
			((MelonPlugin)this).OnPreInitialization();
			string path = Path.GetDirectoryName(MelonEnvironment.ModsDirectory) + "\\Mods";
			Plugin.backend = (MelonUtils.IsGameIl2Cpp() ? Backend.Il2cpp : Backend.Mono);
			if (!Directory.Exists(path))
			{
				return;
			}
			string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
			for (int i = 0; i < files.Length; i++)
			{
				if (files[i].Contains(".dll"))
				{
					File.Move(files[i], files[i].Replace(".di", ""));
					files[i] = files[i].Replace(".di", "");
				}
			}
			string[] array = files;
			foreach (string text in array)
			{
				if (!text.Contains(".dll") || text.Contains(".old") || Path.GetFileName(text).Contains("S1API"))
				{
					continue;
				}
				Backend backend = AssemblyChecker.CheckAtPath(text);
				switch (backend)
				{
				case Backend.Unknown:
					MelonLogger.Error("Found a DLL, but couldn't determine backend!\nTell coolpaca in the S1 modding discord and\nprovide the mod name! File: " + Path.GetFileName(text));
					continue;
				case Backend.S1API:
					continue;
				}
				if (backend != Plugin.backend)
				{
					GC.Collect();
					GC.WaitForPendingFinalizers();
					File.Move(text, text + ".di");
				}
			}
		}
	}
}