Decompiled source of NameChanger v0.0.1

plugins/NameChanger/NameChanger.dll

Decompiled 3 months ago
using System;
using System.Diagnostics;
using System.IO;
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 RiskOfOptions;
using RiskOfOptions.Options;
using RoR2;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("NameChanger")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: AssemblyInformationalVersion("0.0.1+f6895e17858ee2e9943a81e8b5f93f4662ab20ce")]
[assembly: AssemblyProduct("NameChanger")]
[assembly: AssemblyTitle("NameChanger")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.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 NameChanger
{
	internal sealed class Config
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static UnityAction <0>__UpdateUserNames;
		}

		private readonly ConfigEntry<string> nameReplacement;

		public string NameReplacement => nameReplacement.Value;

		public Config(ConfigFile config)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected O, but got Unknown
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			object obj = <>O.<0>__UpdateUserNames;
			if (obj == null)
			{
				UnityAction val = Plugin.UpdateUserNames;
				<>O.<0>__UpdateUserNames = val;
				obj = (object)val;
			}
			ModSettingsManager.AddOption((BaseOption)new GenericButtonOption("", "Settings", "Force user names to update, in case the name replacement is not applied.", "Refresh User Names", (UnityAction)obj));
			nameReplacement = config.Bind<string>("Settings", "nameReplacement", "", "Input a name to use as a replacement for the name that appears in-game (excluding the lobby list / user connected messages).\n\nLeave empty to disable user name replacement.");
			ModSettingsManager.AddOption((BaseOption)new StringInputFieldOption(nameReplacement));
			nameReplacement.SettingChanged += NameReplacement_SettingChanged;
			ModSettingsManager.SetModDescription("Change your in-game* name without having to change your Steam/Epic profile name.\n\n*Does not affect the lobby player list or \"user connected\" messages.\n\n\n\nThe name replacement may be shown to other players if you are the host and have mods that print NetworkUser.userName (e.g. server broadcast chat messages).\nSimilarly, the name replacement may not be applied on messages from the host that use NetworkUser.userName.");
			SetModIcon();
		}

		private void NameReplacement_SettingChanged(object sender, EventArgs e)
		{
			Plugin.UpdateUserNames();
		}

		private static void SetModIcon()
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			string text = Path.Combine(new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)).Parent.FullName, "icon.png");
			Texture2D val = new Texture2D(256, 256);
			try
			{
				if (ImageConversion.LoadImage(val, File.ReadAllBytes(text)))
				{
					ModSettingsManager.SetModIcon(Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), Vector2.one * 0.5f));
				}
				else
				{
					Plugin.Logger.LogError((object)("Failed to load " + text));
				}
			}
			catch (FileNotFoundException ex)
			{
				Plugin.Logger.LogError((object)ex);
			}
		}
	}
	[HarmonyPatch]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("itsschwer.NameChanger", "NameChanger", "0.0.1")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string GUID = "itsschwer.NameChanger";

		public const string Author = "itsschwer";

		public const string Name = "NameChanger";

		public const string Version = "0.0.1";

		internal static ManualLogSource Logger { get; private set; }

		internal static Config Config { get; private set; }

		private void Awake()
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			Logger.Sources.Remove((ILogSource)(object)((BaseUnityPlugin)this).Logger);
			Logger = Logger.CreateLogSource("itsschwer.NameChanger");
			Config = new Config(((BaseUnityPlugin)this).Config);
			new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID).PatchAll();
			Logger.LogMessage((object)"~awake.");
		}

		public static void UpdateUserNames()
		{
			foreach (NetworkUser readOnlyInstances in NetworkUser.readOnlyInstancesList)
			{
				readOnlyInstances.UpdateUserName();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(NetworkUser), "UpdateUserName")]
		private static void NetworkUser_UpdateUserName(NetworkUser __instance)
		{
			if (((NetworkBehaviour)__instance).isLocalPlayer && !string.IsNullOrWhiteSpace(Config.NameReplacement))
			{
				__instance.userName = Config.NameReplacement;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(LocalUser), "LinkNetworkUser")]
		private static void LocalUser_LinkNetworkUser()
		{
			UpdateUserNames();
		}
	}
}