Decompiled source of MCMP Patch v1.0.9

MCMP_Patch.dll

Decompiled 2 months ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using MCMP_Patch.Patches;
using TooManyEmotes;
using TooManyEmotes.Networking;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("MCMP_Patch")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MCMP_Patch")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("8a2b7984-d984-496c-b2dd-30d38fee4b73")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MCMP_Patch
{
	internal class CustomLogging
	{
		private static ManualLogSource logger;

		public static void InitLogger()
		{
			try
			{
				logger = Logger.CreateLogSource($"{((BaseUnityPlugin)Plugin.instance).Info.Metadata.Name}-{((BaseUnityPlugin)Plugin.instance).Info.Metadata.Version}");
			}
			catch
			{
				logger = Plugin.defaultLogger;
			}
		}

		public static void Log(string message)
		{
			logger.LogInfo((object)message);
		}

		public static void LogError(string message)
		{
			logger.LogError((object)message);
		}

		public static void LogWarning(string message)
		{
			logger.LogWarning((object)message);
		}

		public static bool Assert(bool condition, string failMessage)
		{
			if (!condition)
			{
				LogWarning(failMessage);
			}
			return condition;
		}
	}
	internal static class HelperTools
	{
		public enum Achievements
		{
			DominoEffect,
			SpaceIsTheBest,
			ItIsWhatItIs,
			FallGuy,
			GenerationalCrashout,
			CheddahLaden
		}

		public static PlayerControllerB localPlayerController => StartOfRound.Instance?.localPlayerController;

		public static void TryUnlockEmoteAchievement(Achievements achievement, string playerUsername = null)
		{
			string text = playerUsername ?? localPlayerController.playerUsername;
			GetAchievementData(achievement, out var emoteId, out var achievementName);
			if (emoteId == null)
			{
				CustomLogging.LogWarning("Invalid emote ID for achievement " + achievement);
				return;
			}
			UnlockableEmote val = EmotesManager.allUnlockableEmotesDict[emoteId];
			if (!SessionManager.IsEmoteUnlocked(val, text))
			{
				if (!ConfigSync.instance.syncShareEverything)
				{
					SessionManager.UnlockEmoteLocal(val, false, text);
				}
				SyncManager.SendOnUnlockEmoteUpdate(val.emoteId, -1);
				AddAchievement(achievementName, text);
				AddChatMessage(val.displayNameColorCoded + " emote unlocked!");
			}
		}

		private static void GetAchievementData(Achievements achievement, out string emoteId, out string achievementName)
		{
			switch (achievement)
			{
			case Achievements.DominoEffect:
				emoteId = "dancin'_domino";
				achievementName = "Domino Effect";
				break;
			case Achievements.SpaceIsTheBest:
				emoteId = "planetary_vibes";
				achievementName = "Space Is the Best";
				break;
			case Achievements.ItIsWhatItIs:
				emoteId = "go_with_the_flow";
				achievementName = "It Is What It Is";
				break;
			case Achievements.FallGuy:
				emoteId = "smug_dance";
				achievementName = "Fall Guy";
				break;
			case Achievements.GenerationalCrashout:
				emoteId = "get_down";
				achievementName = "Generational Crashout";
				break;
			case Achievements.CheddahLaden:
				emoteId = "hit_it";
				achievementName = "Cheddah Laden";
				break;
			default:
				emoteId = null;
				achievementName = null;
				break;
			}
		}

		private static void AddChatMessage(string text)
		{
			HUDManager.Instance.AddTextToChatOnServer(text, -1);
		}

		private static void AddAchievement(string achievementName, string playerUsername)
		{
			AddChatMessage(string.Concat("<color=yellow>" + playerUsername + " unlocked an achievement:</color> ", achievementName));
		}
	}
	[BepInPlugin("Vijuhas.MCMP_Patch", "MCMP_Patch", "1.0.9")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private readonly Harmony harmony = new Harmony("Vijuhas.MCMP_Patch");

		public static Plugin instance;

		public static ManualLogSource defaultLogger => ((BaseUnityPlugin)instance).Logger;

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			CustomLogging.InitLogger();
			harmony.PatchAll(typeof(Plugin));
			harmony.PatchAll(typeof(CauseOfDeathPatch));
			harmony.PatchAll(typeof(TerminalPatch));
			harmony.PatchAll(typeof(GrabbableObjectPatch));
			CustomLogging.Log("Vijuhas.MCMP_Patch has loaded successfully.");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "Vijuhas.MCMP_Patch";

		public const string PLUGIN_NAME = "MCMP_Patch";

		public const string PLUGIN_VERSION = "1.0.9";
	}
}
namespace MCMP_Patch.Patches
{
	[HarmonyPatch(typeof(GrabbableObject), "GrabItem")]
	internal class GrabbableObjectPatch
	{
		[HarmonyPostfix]
		private static void Postfix(GrabbableObject __instance)
		{
			if (105f * __instance.playerHeldBy.carryWeight - 105f > 100f)
			{
				HelperTools.TryUnlockEmoteAchievement(HelperTools.Achievements.CheddahLaden, __instance.playerHeldBy.playerUsername);
			}
		}
	}
	[HarmonyPatch(typeof(Terminal), "RunTerminalEvents")]
	internal class InfiniteCreditsPatch
	{
		[HarmonyPostfix]
		private static void Postfix(ref int ___groupCredits)
		{
			___groupCredits = 69420;
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "KillPlayer")]
	internal class CauseOfDeathPatch
	{
		[HarmonyPostfix]
		private static void Postfix(CauseOfDeath causeOfDeath, PlayerControllerB __instance)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Invalid comparison between Unknown and I4
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Invalid comparison between Unknown and I4
			string playerUsername = __instance.playerUsername;
			if ((int)causeOfDeath == 1)
			{
				HelperTools.TryUnlockEmoteAchievement(HelperTools.Achievements.ItIsWhatItIs, playerUsername);
			}
			else if ((int)causeOfDeath == 2)
			{
				HelperTools.TryUnlockEmoteAchievement(HelperTools.Achievements.FallGuy, playerUsername);
			}
			else if ((int)causeOfDeath == 16)
			{
				HelperTools.TryUnlockEmoteAchievement(HelperTools.Achievements.GenerationalCrashout, playerUsername);
			}
		}
	}
	[HarmonyPatch(typeof(Terminal), "ParsePlayerSentence")]
	internal class TerminalPatch
	{
		private static readonly int inverseTpUnlockableID = 19;

		[HarmonyPostfix]
		private static void Postfix(ref TerminalNode __result, Terminal __instance)
		{
			if (__instance.screenText.text.Length <= 0)
			{
				return;
			}
			string text = __instance.screenText.text.Substring(__instance.screenText.text.Length - __instance.textAdded).ToLower();
			if (text.Split(new char[1] { ' ' }).Length != 0 && "confirm".StartsWith(text))
			{
				string playerUsername = GetPlayerUsingTerminal()?.playerUsername;
				if (__result.shipUnlockableID == inverseTpUnlockableID)
				{
					HelperTools.TryUnlockEmoteAchievement(HelperTools.Achievements.DominoEffect, playerUsername);
				}
				else if (__result.buyRerouteToMoon != -1 && __result.buyRerouteToMoon != -2 && __result.itemCost > 0 && StartOfRound.Instance.inShipPhase && !StartOfRound.Instance.travellingToNewLevel)
				{
					HelperTools.TryUnlockEmoteAchievement(HelperTools.Achievements.SpaceIsTheBest, playerUsername);
				}
			}
		}

		public static PlayerControllerB GetPlayerUsingTerminal()
		{
			PlayerControllerB[] allPlayerScripts = StartOfRound.Instance.allPlayerScripts;
			foreach (PlayerControllerB val in allPlayerScripts)
			{
				if (val.inTerminalMenu)
				{
					return val;
				}
			}
			return null;
		}
	}
}