Decompiled source of MoCore v1.0.0

BepInEx/plugins/MoSadie-MoCore/com.mosadie.mocore.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
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("com.mosadie.mocore")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Common plugin across MoSadie's plugins")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0039f5e19f03c0964fbab87a64d3df63759096f8")]
[assembly: AssemblyProduct("MoCore")]
[assembly: AssemblyTitle("com.mosadie.mocore")]
[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 MoCore
{
	[BepInPlugin("com.mosadie.mocore", "MoCore", "1.0.0")]
	[BepInProcess("Slipstream_Win.exe")]
	public class MoCore : BaseUnityPlugin
	{
		private static ConfigEntry<bool> overrideVersionCheck;

		private static HttpClient httpClient = new HttpClient();

		private static List<MoPlugin> plugins = new List<MoPlugin>();

		internal static ManualLogSource Log;

		private void Awake()
		{
			try
			{
				Log = ((BaseUnityPlugin)this).Logger;
				Log.LogInfo((object)("Game version: " + Application.version));
				overrideVersionCheck = ((BaseUnityPlugin)this).Config.Bind<bool>("BE CAREFUL", "Override Version Check", false, "This will allow my plugins to run on any version of Slipstream, skipping the version checker. Use at your own risk.");
				httpClient.Timeout = TimeSpan.FromSeconds(5.0);
				Log.LogInfo((object)"Plugin com.mosadie.mocore is loaded!");
			}
			catch (Exception ex)
			{
				Log.LogError((object)("An error occurred during plugin startup: " + ex.Message));
			}
		}

		public static bool RegisterPlugin(MoPlugin plugin)
		{
			Log.LogInfo((object)("Registering plugin " + PluginName(plugin) + " (" + PluginId(plugin) + ")"));
			Log.LogInfo((object)("Version: " + PluginVersion(plugin)));
			if (overrideVersionCheck.Value)
			{
				Log.LogInfo((object)"Version check override is enabled. Skipping version check.");
				return true;
			}
			if (!VersionCheck(plugin, Application.version))
			{
				return false;
			}
			Log.LogInfo((object)("Plugin " + PluginName(plugin) + " (" + PluginId(plugin) + ") is compatible with this version of the game (" + Application.version + ")."));
			plugins.Add(plugin);
			return true;
		}

		public static List<MoPlugin> GetPlugins()
		{
			return plugins;
		}

		private static bool VersionCheck(MoPlugin plugin, string gameVersion)
		{
			try
			{
				Dictionary<string, HashSet<string>> dictionary = JsonConvert.DeserializeObject<Dictionary<string, HashSet<string>>>(httpClient.GetStringAsync(plugin.GetVersionCheckUrl()).GetAwaiter().GetResult());
				if (dictionary.ContainsKey(PluginVersion(plugin)) && dictionary[PluginVersion(plugin)].Contains(Application.version))
				{
					return true;
				}
				if (dictionary.ContainsKey(PluginVersion(plugin)))
				{
					Log.LogError((object)("Version " + PluginVersion(plugin) + " of " + PluginName(plugin) + " (" + PluginId(plugin) + ") is not compatible with this version of the game (" + Application.version + "). Please check for updates."));
					return false;
				}
				Log.LogError((object)("Version " + PluginVersion(plugin) + " of " + PluginName(plugin) + " (" + PluginId(plugin) + ") is not listed in the version check file. Please contact the plugin's creator."));
				return false;
			}
			catch (TaskCanceledException)
			{
				Log.LogError((object)"Version check timed out. Falling back to hardcoded version check.");
				return plugin.GetCompatibleGameVersion().Equals(gameVersion);
			}
			catch (Exception ex2)
			{
				Log.LogError((object)("An error occurred during remote version check, falling back to hardcoded version check: " + ex2.Message));
				return plugin.GetCompatibleGameVersion().Equals(gameVersion);
			}
		}

		private static string PluginName(MoPlugin plugin)
		{
			return plugin.GetPluginObject().Info.Metadata.Name;
		}

		private static string PluginId(MoPlugin plugin)
		{
			return plugin.GetPluginObject().Info.Metadata.GUID;
		}

		private static string PluginVersion(MoPlugin plugin)
		{
			return plugin.GetPluginObject().Info.Metadata.Version.ToString();
		}
	}
	public interface MoPlugin
	{
		string GetCompatibleGameVersion();

		string GetVersionCheckUrl();

		BaseUnityPlugin GetPluginObject();
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.mosadie.mocore";

		public const string PLUGIN_NAME = "MoCore";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}