Decompiled source of RealFakeTromboneFacts v1.1.1

RealFakeTromboneFacts.dll

Decompiled 4 months ago
using System;
using System.Collections;
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;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("Steven \ud83c\udf80, DerpyChap")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Adds more custom facts to Trombone Champ (forked from Steven's CustomFacts).")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("RealFakeTromboneFacts")]
[assembly: AssemblyTitle("RealFakeTromboneFacts")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/DerpyChap/MoreCustomFacts")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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;
		}
	}
}
[JsonObject]
public class Fact
{
	[JsonRequired]
	public string fact;

	[JsonRequired]
	public string author;

	public int up;

	public int down;

	public int delta;

	public long message_id;

	public float time;
}
[JsonObject]
public class DownloadedFacts
{
	[JsonRequired]
	public List<Fact> facts;
}
namespace CustomFacts
{
	[HarmonyPatch]
	[BepInPlugin("derpychap.realfaketrombonefacts", "Real Fake Trombone Facts", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		private static Plugin Instance;

		private const string PLUGIN_GUID = "derpychap.realfaketrombonefacts";

		private const string PLUGIN_NAME = "Real Fake Trombone Facts";

		private const string PLUGIN_VERSION = "1.1.0";

		private const string SETFACT_METHOD_NAME = "setFact";

		private const string CUSTOM_FACTS_FILENAME = "facts.txt";

		private const string RESOURCE_NAME = "CustomFacts.Resources.facts.json";

		private const string API_URL = "https://guardelo.la/api/facts/all/";

		public ConfigEntry<bool> basegameFacts;

		public ConfigEntry<bool> loadModFacts;

		public ConfigEntry<bool> showAuthor;

		public ConfigEntry<bool> downloadFacts;

		public ConfigEntry<int> minimumUpvotes;

		public ConfigEntry<int> minimumDelta;

		private List<Fact> AllFacts = new List<Fact>();

		private bool firstRun = true;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			ConfigFile val = new ConfigFile(Path.Combine(Paths.ConfigPath, "facts.cfg"), true);
			basegameFacts = val.Bind<bool>("General", "Enable Base Game Facts", true, "Controls if facts from the base game should be included in the rotation.");
			loadModFacts = val.Bind<bool>("General", "Load Built-In Facts", true, "Controls if facts should be loaded from the mod's built-in curated facts list.");
			showAuthor = val.Bind<bool>("General", "Show Fact Author", true, "Shows who wrote the fact in the bottom left corner of the loading screen, if available.");
			downloadFacts = val.Bind<bool>("Web", "Download Latest Facts", true, "If enabled, will download the latest facts from the #real-fake-trombone-facts channel on the Modding Discord.");
			minimumUpvotes = val.Bind<int>("Web", "Mininum Upvotes", 1, "The minimum amount of upvotes needed before a fact from the Discord is added to the rotation.");
			minimumDelta = val.Bind<int>("Web", "Minimum Vote Delta", 3, "The minimum vote delta (the difference between upvotes and downvotes) before a fact is added to the rotation.");
			Instance = this;
			if (loadModFacts.Value)
			{
				LoadFacts();
			}
			LoadCustomFacts();
			if (downloadFacts.Value)
			{
				((MonoBehaviour)this).StartCoroutine(DownloadFacts());
			}
			new Harmony("derpychap.realfaketrombonefacts").PatchAll();
		}

		private IEnumerator DownloadFacts()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Downloading facts...");
			UnityWebRequest request = UnityWebRequest.Get("https://guardelo.la/api/facts/all/");
			try
			{
				yield return request.SendWebRequest();
				if (request.isHttpError || request.isNetworkError)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Failed to download facts from the API!");
					yield break;
				}
				((BaseUnityPlugin)this).Logger.LogDebug((object)"Downloaded facts!");
				DownloadedFacts downloadedFacts = JsonConvert.DeserializeObject<DownloadedFacts>(request.downloadHandler.text);
				int num = 0;
				foreach (Fact fact in downloadedFacts.facts)
				{
					if (fact.delta >= minimumDelta.Value && fact.up >= minimumUpvotes.Value && !AllFacts.Any((Fact a) => a.message_id == fact.message_id))
					{
						num++;
						AllFacts.Add(fact);
					}
				}
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Adding {num} facts from the interwebs...");
			}
			finally
			{
				((IDisposable)request)?.Dispose();
			}
		}

		private void LoadFacts()
		{
			((BaseUnityPlugin)this).Logger.LogDebug((object)"Loading facts...");
			using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CustomFacts.Resources.facts.json");
			using StreamReader streamReader = new StreamReader(stream);
			List<Fact> collection = JsonConvert.DeserializeObject<List<Fact>>(streamReader.ReadToEnd());
			AllFacts.AddRange(collection);
			((BaseUnityPlugin)this).Logger.LogDebug((object)$"Finished loading {AllFacts.Count()} facts!");
		}

		private void LoadCustomFacts()
		{
			string path = Path.Combine(Paths.ConfigPath, "facts.txt");
			if (File.Exists(path))
			{
				((BaseUnityPlugin)this).Logger.LogDebug((object)"Loading custom facts.txt...");
				string[] array = File.ReadAllLines(path);
				string[] array2 = array;
				foreach (string fact in array2)
				{
					Fact item = new Fact
					{
						author = null,
						fact = fact
					};
					AllFacts.Add(item);
				}
				((BaseUnityPlugin)this).Logger.LogDebug((object)$"Finished loading {array.Count()} custom facts from facts.txt!");
			}
		}

		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static void Postfix(string[] ___tfacts)
		{
			if (Instance.firstRun && Instance.basegameFacts.Value)
			{
				((BaseUnityPlugin)Instance).Logger.LogDebug((object)$"Adding {___tfacts.Length} default facts.");
				foreach (string fact in ___tfacts)
				{
					Fact item = new Fact
					{
						author = null,
						fact = fact
					};
					Instance.AllFacts.Add(item);
				}
				Instance.firstRun = false;
			}
		}

		[HarmonyPatch(typeof(LoadController), "setFact")]
		private static bool Prefix(LoadController __instance)
		{
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Expected O, but got Unknown
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			if (Instance.AllFacts.Count() == 0)
			{
				((BaseUnityPlugin)Instance).Logger.LogWarning((object)"There are no facts to load!");
				__instance.facttext.text = "<size=20%>But nobody came...</size>";
				__instance.facttext.resizeTextMinSize = 1;
			}
			else
			{
				int num = Random.Range(0, Instance.AllFacts.Count());
				((BaseUnityPlugin)Instance).Logger.LogDebug((object)$"Loading fact at index: {num}");
				((BaseUnityPlugin)Instance).Logger.LogDebug((object)Instance.AllFacts[num]);
				__instance.facttext.text = Instance.AllFacts[num].fact;
				__instance.facttext.resizeTextMinSize = 1;
				if (Instance.AllFacts[num].author != null && Instance.showAuthor.Value)
				{
					GameObject val = new GameObject("Author");
					Canvas val2 = val.AddComponent<Canvas>();
					val2.renderMode = (RenderMode)0;
					val.AddComponent<CanvasScaler>();
					val.AddComponent<GraphicRaycaster>();
					GameObject val3 = new GameObject("Author Text");
					val3.transform.parent = val.transform;
					Text obj = val3.AddComponent<Text>();
					RectTransform component = ((Component)obj).GetComponent<RectTransform>();
					float num2 = val2.renderingDisplaySize.x / 1280f;
					float value = 20f * num2;
					component.pivot = new Vector2(-0.01f, -0.1f);
					component.sizeDelta = new Vector2(1280f, 100f);
					obj.fontSize = Convert.ToInt32(value);
					obj.supportRichText = false;
					obj.alignment = (TextAnchor)6;
					obj.font = __instance.facttext.font;
					((Graphic)obj).color = new Color(0.5f, 0.5f, 0.5f, 0.8f);
					obj.text = "Fact verified by " + Instance.AllFacts[num].author;
				}
			}
			return false;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "RealFakeTromboneFacts";

		public const string PLUGIN_NAME = "RealFakeTromboneFacts";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}