Decompiled source of RainbowPlayer v1.0.1

RainbowPlayer.DLL

Decompiled 7 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Steamworks.Data;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = ".NET Framework 4.6")]
[assembly: AssemblyCompany("RainbowPlayer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Give everyone wacky, eye-straining, changing colors!")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("RainbowPlayer")]
[assembly: AssemblyTitle("RainbowPlayer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.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 RainbowPlayer
{
	[BepInPlugin("me.antimality.RainbowPlayer", "RainbowPlayer", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		private static Harmony harmony;

		private static ManualLogSource logger;

		internal static ConfigEntry<bool> onlyWhite;

		internal static ConfigEntry<int> colorScheme;

		internal static ConfigEntry<uint> framesPerColor;

		private void Awake()
		{
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Expected O, but got Unknown
			logger = ((BaseUnityPlugin)this).Logger;
			Log("Plugin RainbowPlayer is loaded!");
			onlyWhite = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "Only white player", false, "If set to false, all players are rainbow-ed");
			colorScheme = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "Color scheme", 2, "0: Base game colors\n1: Random\n2: Rainbow");
			framesPerColor = ((BaseUnityPlugin)this).Config.Bind<uint>("Settings", "Frames per color", 6u, "How many frames to hold each color. 60 frames means one second");
			if (colorScheme.Value < 0 || colorScheme.Value > 2)
			{
				colorScheme.Value = 2;
			}
			if (framesPerColor.Value == 0)
			{
				framesPerColor.Value = 1u;
			}
			((BaseUnityPlugin)this).Config.Save();
			harmony = new Harmony("me.antimality.RainbowPlayer");
			harmony.PatchAll(typeof(Patch));
		}

		private void OnDestroy()
		{
			harmony.UnpatchSelf();
			Patch.MyPlayer.Clear();
		}

		internal static void Log(object message)
		{
			logger.LogInfo(message);
		}
	}
	[HarmonyPatch]
	public static class Patch
	{
		private enum ColorScheme
		{
			Base,
			Random,
			Rainbow
		}

		internal class MyPlayer
		{
			internal static List<MyPlayer> playerList = new List<MyPlayer>();

			private int playerId;

			private int tick;

			private Color currentColor;

			private static List<Color> baseColors = new List<Color>(12)
			{
				new Color(0.989f, 1f, 0.316f),
				new Color(0.641f, 0.532f, 0.981f, 1f),
				new Color(0.297f, 0.951f, 1f),
				new Color(1f, 1f, 1f),
				new Color(1f, 0.448f, 0.482f),
				new Color(0.875f, 0.463f, 0.795f),
				new Color(1f, 0.675f, 0.761f),
				new Color(0.981f, 0.629f, 0.112f),
				new Color(0.726f, 0.726f, 0.726f),
				new Color(0.281f, 0.915f, 0.446f),
				new Color(0.451f, 0.784f, 1f),
				new Color(0.415f, 0.415f, 0.415f)
			};

			private static Random rand = new Random();

			private static readonly float GRADIENT_FACTOR = 25f;

			private readonly float randomizer = (float)rand.NextDouble();

			internal MyPlayer(int playerId, Color origColor)
			{
				//IL_0026: Unknown result type (might be due to invalid IL or missing references)
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				this.playerId = playerId;
				tick = -1;
				currentColor = origColor;
				playerList.Add(this);
			}

			internal static void UpdateById(int id, SlimeController sc)
			{
				foreach (MyPlayer player in playerList)
				{
					if (player.playerId == id)
					{
						player.UpdatePlayer(sc);
					}
				}
			}

			private void UpdatePlayer(SlimeController sc)
			{
				//IL_002c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0031: Unknown result type (might be due to invalid IL or missing references)
				//IL_0050: Unknown result type (might be due to invalid IL or missing references)
				//IL_0055: Unknown result type (might be due to invalid IL or missing references)
				//IL_0042: Unknown result type (might be due to invalid IL or missing references)
				//IL_0047: Unknown result type (might be due to invalid IL or missing references)
				//IL_005c: Unknown result type (might be due to invalid IL or missing references)
				tick++;
				if (tick % framesPerColor == 0)
				{
					if (colorScheme == ColorScheme.Random)
					{
						currentColor = GetRandomColor();
					}
					else if (colorScheme == ColorScheme.Rainbow)
					{
						currentColor = GetRainbowColor();
					}
					else
					{
						currentColor = GetNextBaseColor();
					}
					sc.SetColor(currentColor);
				}
			}

			private Color GetNextBaseColor()
			{
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0022: Unknown result type (might be due to invalid IL or missing references)
				return baseColors[(baseColors.IndexOf(currentColor) + 1) % baseColors.Count];
			}

			private static Color GetRandomColor()
			{
				//IL_0021: Unknown result type (might be due to invalid IL or missing references)
				return new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble());
			}

			private Color GetRainbowColor()
			{
				//IL_0030: Unknown result type (might be due to invalid IL or missing references)
				return Color.HSVToRGB(Mathf.PingPong(randomizer + (float)(tick / framesPerColor) / GRADIENT_FACTOR, 1f), 1f, 1f);
			}

			internal static void Clear()
			{
				playerList.Clear();
			}
		}

		private static ColorScheme colorScheme;

		private static int whitePlayerId;

		private static bool onlyWhitePlayer = true;

		private static uint framesPerColor;

		[HarmonyPatch(typeof(GameSession), "Init")]
		[HarmonyPostfix]
		public static void OnGameStart()
		{
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			if (GameLobby.isOnlineGame)
			{
				onlyWhitePlayer = bool.Parse(((Lobby)(ref SteamManager.instance.currentLobby)).GetData("onlyWhite"));
				framesPerColor = uint.Parse(((Lobby)(ref SteamManager.instance.currentLobby)).GetData("framesPerColor"));
				colorScheme = (ColorScheme)int.Parse(((Lobby)(ref SteamManager.instance.currentLobby)).GetData("colorScheme"));
			}
			else
			{
				onlyWhitePlayer = Plugin.onlyWhite.Value;
				framesPerColor = Plugin.framesPerColor.Value;
				colorScheme = (ColorScheme)Plugin.colorScheme.Value;
			}
			whitePlayerId = -1;
			MyPlayer.Clear();
			foreach (Player item in PlayerHandler.Get().PlayerList())
			{
				new MyPlayer(item.Id, item.Color.GetColor("_ShadowColor"));
				if (item.Color.GetColor("_ShadowColor") == Color.white)
				{
					whitePlayerId = item.Id;
				}
			}
		}

		[HarmonyPatch(typeof(PlayerBody), "UpdateSim")]
		[HarmonyPostfix]
		public static void ChangeColor(ref IPlayerIdHolder ___idHolder)
		{
			SlimeController[] slimeControllers = GetSlimeControllers();
			foreach (SlimeController val in slimeControllers)
			{
				if (val.GetPlayerId() == ___idHolder.GetPlayerId() && (Object)(object)val != (Object)null)
				{
					if (!onlyWhitePlayer)
					{
						MyPlayer.UpdateById(___idHolder.GetPlayerId(), val);
					}
					if (onlyWhitePlayer && val.GetPlayerId() == whitePlayerId)
					{
						MyPlayer.UpdateById(___idHolder.GetPlayerId(), val);
					}
				}
			}
		}

		private static void SetColor(this SlimeController sc, Color color)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			((Renderer)sc.GetPlayerSprite()).material.SetColor("_ShadowColor", color);
		}

		[HarmonyPatch(typeof(SteamManager), "OnLobbyEnteredCallback")]
		[HarmonyPostfix]
		public static void OnEnterLobby(Lobby lobby)
		{
			if (SteamManager.LocalPlayerIsLobbyOwner)
			{
				((Lobby)(ref lobby)).SetData("onlyWhite", Plugin.onlyWhite.Value.ToString());
				((Lobby)(ref lobby)).SetData("framesPerColor", Plugin.framesPerColor.Value.ToString());
				((Lobby)(ref lobby)).SetData("colorScheme", Plugin.colorScheme.Value.ToString());
			}
		}

		public static GameSessionHandler GetGameSessionHandler()
		{
			object? value = typeof(GameSessionHandler).GetField("selfRef", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
			return (GameSessionHandler)((value is GameSessionHandler) ? value : null);
		}

		public static SlimeController[] GetSlimeControllers()
		{
			return typeof(GameSessionHandler).GetField("slimeControllers", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(GetGameSessionHandler()) as SlimeController[];
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "RainbowPlayer";

		public const string PLUGIN_NAME = "RainbowPlayer";

		public const string PLUGIN_VERSION = "1.0.1";
	}
}