Decompiled source of ModIoTokenPlugin v1.0.0

ModIoTokenPlugin.dll

Decompiled 5 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Newtonsoft.Json;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ModIoTokenPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ModIoTokenPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("ModIoTokenPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.modiotoken", "Mod Io Token Plugin", "1.0.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class ModIoTokenPlugin : BaseUnityPlugin
{
	public static class OAuth2Token
	{
		public static Exception lastException;

		public static HttpResponseMessage lastResponseMessage;

		public static bool HasOAuth2Token(string[] locations, string filename = "OAuth2.txt")
		{
			return FindOAuth2File(locations, filename) != null;
		}

		public static string FindOAuth2File(string[] locations, string filename = "OAuth2.txt")
		{
			foreach (string location in locations)
			{
				string text = CheckFolder(location, filename);
				if (text != null)
				{
					return text;
				}
			}
			return null;
		}

		public static void RequestOAuth2Token(string api, string api_key, string email)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Expected O, but got Unknown
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Expected O, but got Unknown
			lastException = null;
			lastResponseMessage = null;
			try
			{
				Debug.Log((object)"Mod Io Token Plugin: Creating Http Client");
				HttpClient val = new HttpClient();
				try
				{
					string text = api + "/oauth/emailrequest?api_key=" + api_key;
					Debug.Log((object)("Mod Io Token Plugin: URI = " + text));
					val.BaseAddress = new Uri(text);
					val.Timeout = new TimeSpan(0, 0, 60);
					val.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
					Debug.Log((object)"Mod Io Token Plugin: Setting Request Body");
					HttpContent val2 = (HttpContent)new StringContent("email=" + email);
					val2.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
					Debug.Log((object)"Mod Io Token Plugin: Making POST And Getting Result");
					HttpResponseMessage result = val.PostAsync(text, val2).Result;
					result.EnsureSuccessStatusCode();
					string result2 = result.Content.ReadAsStringAsync().Result;
					Debug.Log((object)("Mod Io Token Plugin: Reading POST Response = " + result.StatusCode.ToString() + ": " + result2));
				}
				finally
				{
					((IDisposable)val)?.Dispose();
				}
			}
			catch (Exception ex)
			{
				lastException = ex;
			}
		}

		public static void CollectOAuth2Token(string api, string api_key, string code, string storage)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Expected O, but got Unknown
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Expected O, but got Unknown
			lastException = null;
			lastResponseMessage = null;
			try
			{
				Debug.Log((object)"Mod Io Token Plugin: Creating Http Client");
				HttpClient val = new HttpClient();
				try
				{
					string text = api + "/oauth/emailexchange?api_key=" + api_key;
					Debug.Log((object)("Mod Io Token Plugin: URI = " + text));
					val.BaseAddress = new Uri(text);
					val.Timeout = new TimeSpan(0, 0, 60);
					val.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
					Debug.Log((object)"Mod Io Token Plugin: Setting Request Body");
					HttpContent val2 = (HttpContent)new StringContent("security_code=" + code);
					val2.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
					Debug.Log((object)"Mod Io Token Plugin: Making POST And Getting Result");
					HttpResponseMessage result = val.PostAsync(text, val2).Result;
					result.EnsureSuccessStatusCode();
					string result2 = result.Content.ReadAsStringAsync().Result;
					Debug.Log((object)("Mod Io Token Plugin: Reading POST Response = " + result.StatusCode.ToString() + ": " + result2));
					Dictionary<string, string> dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(result2);
					File.WriteAllText(storage, dictionary["access_token"]);
				}
				finally
				{
					((IDisposable)val)?.Dispose();
				}
			}
			catch (Exception ex)
			{
				lastException = ex;
			}
		}

		private static string CheckFolder(string location, string filename = "OAuth2.txt")
		{
			if (File.Exists(location + "/" + filename))
			{
				return location;
			}
			foreach (string item in Directory.EnumerateDirectories(location))
			{
				string text = CheckFolder(item, filename);
				if (text != null)
				{
					return text;
				}
			}
			return null;
		}
	}

	public enum DiagnosticLevel
	{
		none,
		error,
		warning,
		info,
		debug,
		ultra
	}

	public const string Name = "Mod Io Token Plugin";

	public const string Guid = "org.lordashes.plugins.modiotoken";

	public const string Version = "1.0.0.0";

	public const string Author = "Lord Ashes";

	private static string api = "https://g-3963.modapi.io:443/v1";

	private static bool failedCheck = false;

	private static Texture2D guide = null;

	private ConfigEntry<DiagnosticLevel> diagnostics { get; set; }

	private void Awake()
	{
		diagnostics = ((BaseUnityPlugin)this).Config.Bind<DiagnosticLevel>("Settings", "Diagnostic Level", DiagnosticLevel.info, (ConfigDescription)null);
		Debug.Log((object)("Mod Io Token Plugin: Active. (Diagnostic Mode = " + diagnostics.Value.ToString() + ")"));
		guide = Image.LoadTexture("ModIoGuide.png", (CacheType)999);
		failedCheck = !OAuth2Token.HasOAuth2Token(new string[2]
		{
			Environment.CurrentDirectory,
			Paths.PluginPath
		});
		if (failedCheck)
		{
			Debug.Log((object)"Mod Io Token Plugin: OAuth2 Token Not Found");
			((MonoBehaviour)this).StartCoroutine(Sequence());
		}
	}

	private void OnGUI()
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		if (failedCheck)
		{
			try
			{
				GUI.DrawTexture(new Rect(0f, 0f, 1920f, 1080f), (Texture)(object)guide);
			}
			catch
			{
			}
		}
	}

	private IEnumerator Sequence()
	{
		yield return (object)new WaitForSeconds(5f);
		failedCheck = false;
		((MonoBehaviour)this).StartCoroutine(AskKey());
	}

	private IEnumerator AskKey()
	{
		yield return (object)new WaitForSeconds(0.1f);
		Debug.Log((object)"Mod Io Token Plugin: Prompting For API Key");
		SystemMessage.AskForTextInput("Mod.Io Access", "Enter Api Key: ", "Apply", (Action<string>)delegate(string key)
		{
			((MonoBehaviour)this).StartCoroutine(AskEmail(key));
		}, (Action)null, "Cancel", (Action)null, "");
	}

	private IEnumerator AskEmail(string key)
	{
		yield return (object)new WaitForSeconds(0.1f);
		Debug.Log((object)"Mod Io Token Plugin: Prompting For Email");
		SystemMessage.AskForTextInput("Mod.Io Access", "Enter E-Mail: ", "Apply", (Action<string>)delegate(string email)
		{
			OAuth2Token.RequestOAuth2Token(api, key, email);
			if (OAuth2Token.lastException != null)
			{
				Debug.Log((object)("Mod Io Token Plugin: " + OAuth2Token.lastException));
				Debug.LogException(OAuth2Token.lastException);
			}
			((MonoBehaviour)this).StartCoroutine(AskCode(key));
		}, (Action)null, "Cancel", (Action)null, "");
	}

	private IEnumerator AskCode(string key)
	{
		yield return (object)new WaitForSeconds(0.1f);
		Debug.Log((object)"Mod Io Token Plugin: Prompting For Code");
		SystemMessage.AskForTextInput("Mod.Io Access", "Enter Security Code: ", "Apply", (Action<string>)delegate(string code)
		{
			OAuth2Token.CollectOAuth2Token(api, key, code, Environment.CurrentDirectory + "/OAuth2.txt");
			if (OAuth2Token.lastException != null)
			{
				Debug.Log((object)("Mod Io Token Plugin: " + OAuth2Token.lastException));
				Debug.LogException(OAuth2Token.lastException);
			}
			failedCheck = !OAuth2Token.HasOAuth2Token(new string[2]
			{
				Environment.CurrentDirectory,
				Paths.PluginPath
			});
			if (failedCheck)
			{
				AskKey();
			}
		}, (Action)null, "Cancel", (Action)null, "");
	}
}