Decompiled source of ChangeName v0.1.0

ChangeName.dll

Decompiled 2 years ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using TMPro;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ChangeName")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Change in-game name")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("ChangeName")]
[assembly: AssemblyTitle("ChangeName")]
[assembly: AssemblyVersion("0.1.0.0")]
namespace ChangeName
{
	[BepInPlugin("me.wallen.changename", "Change Player Names", "0.1.0")]
	public class ChangeNamePlugin : BaseUnityPlugin
	{
		private const string GUID = "me.wallen.changename";

		private const string NAME = "Change Player Names";

		private const string VERSION = "0.1.0";

		private static ChangeNamePlugin Instance;

		private void Awake()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			Instance = this;
			LogInfo("Loading...");
			Harmony val = new Harmony("me.wallen.changename");
			val.PatchAll();
			LogInfo("Loading Complete!");
		}

		internal static void LogDebug(string message)
		{
			Instance.Log(message, (LogLevel)32);
		}

		internal static void LogInfo(string message)
		{
			Instance.Log(message, (LogLevel)16);
		}

		internal static void LogWarning(string message)
		{
			Instance.Log(message, (LogLevel)4);
		}

		internal static void LogError(string message)
		{
			Instance.Log(message, (LogLevel)2);
		}

		internal static void LogError(Exception ex)
		{
			Instance.Log(ex.Message + "\n" + ex.StackTrace, (LogLevel)2);
		}

		private void Log(string message, LogLevel logLevel)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			((BaseUnityPlugin)this).Logger.Log(logLevel, (object)message);
		}
	}
	internal static class NameDatabase
	{
		private const string DAT_ND_BROADCAST = "CHANGE_NAME_ND_Broadcast";

		private const string SIG_SEND_NAMES = "CHANGE_NAME_SEND_NAMES";

		private static bool isHost = false;

		private static Dictionary<ulong, string> names = new Dictionary<ulong, string>();

		internal static void NDNetGetString(string data, string signature)
		{
			if (signature == "CHANGE_NAME_SEND_NAMES")
			{
				Dictionary<ulong, string> second = DecodeNames(data);
				if (!names.SequenceEqual(second))
				{
					names = second;
				}
			}
		}

		private static string EncodeNames(Dictionary<ulong, string> names)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (KeyValuePair<ulong, string> name in names)
			{
				stringBuilder.Append($"{name.Key}:{name.Value};");
			}
			return stringBuilder.ToString();
		}

		private static Dictionary<ulong, string> DecodeNames(string encoded)
		{
			string[] array = encoded.Split(new char[1] { ';' }, StringSplitOptions.RemoveEmptyEntries);
			Dictionary<ulong, string> dictionary = new Dictionary<ulong, string>();
			string[] array2 = array;
			foreach (string text in array2)
			{
				string[] array3 = text.Split(':');
				dictionary[ulong.Parse(array3[0])] = array3[1];
			}
			return dictionary;
		}
	}
}
namespace ChangeName.Patches
{
	[HarmonyPatch(typeof(HUDManager), "AddChatMessage")]
	internal class HUDManager_AddChatMessage
	{
		private static void Postfix(HUDManager __instance, string chatMessage, string nameOfUserWhoTyped)
		{
			ChangeNamePlugin.LogInfo(chatMessage);
			ChangeNamePlugin.LogInfo(nameOfUserWhoTyped);
			if (!chatMessage.Contains("!name"))
			{
				return;
			}
			string text = chatMessage.Substring(chatMessage.IndexOf("!name") + "!name".Length).Trim();
			ChangeNamePlugin.LogInfo(text);
			PlayerControllerB[] allPlayerScripts = __instance.playersManager.allPlayerScripts;
			foreach (PlayerControllerB val in allPlayerScripts)
			{
				if (val.playerUsername == nameOfUserWhoTyped)
				{
					val.playerUsername = text;
					((TMP_Text)val.usernameBillboardText).text = text;
				}
			}
		}
	}
}