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.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using IntChangeName.Patches;
using Microsoft.CodeAnalysis;
using PeakStats.MonoBehaviours;
using Photon.Pun;
using Photon.Realtime;
using TMPro;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ChangeName")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ChangeName")]
[assembly: AssemblyTitle("ChangeName")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
}
namespace IntChangeName
{
[BepInPlugin("Int.ChangeName", "Change Name", "1.0.0")]
public class ChangeName : BaseUnityPlugin
{
public const string GUID = "Int.ChangeName";
public const string NAME = "Change Name";
public const string VERSION = "1.0.0";
private static PhotonView? photonView;
internal static ConfigEntry<string> NickName;
internal static ManualLogSource Logger { get; private set; }
internal static Harmony? Harmony { get; set; }
public static ChangeName Instance { get; private set; }
private static PhotonView PhotonView
{
get
{
return photonView;
}
set
{
photonView = value;
}
}
private void Awake()
{
Instance = this;
CreatePhotonView();
NickName = ((BaseUnityPlugin)this).Config.Bind<string>("General", "NickName", "", "Set your nickname");
NickName.SettingChanged += NickName_SettingChanged;
Logger = ((BaseUnityPlugin)this).Logger;
Patch();
Logger.LogInfo((object)"Int.ChangeName v1.0.0 has loaded!");
}
public void CreatePhotonView()
{
if ((Object)(object)((Component)this).gameObject.GetComponent<PhotonView>() == (Object)null)
{
PhotonView = ((Component)this).gameObject.AddComponent<PhotonView>();
PhotonView.ViewID = 872681969;
}
else if ((Object)(object)PhotonView == (Object)null)
{
PhotonView = ((Component)this).gameObject.GetComponent<PhotonView>();
}
}
private void NickName_SettingChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(NickName.Value))
{
PhotonNetwork.LocalPlayer.NickName = NickName.Value;
Logger.LogInfo((object)("NickName has changed to [" + NickName.Value + "]"));
if (PhotonNetwork.InRoom)
{
SyncPassport();
SyncNewNameToOthers();
}
}
}
internal static void Patch()
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Expected O, but got Unknown
if (Harmony == null)
{
Harmony = new Harmony("Int.ChangeName");
}
Logger.LogDebug((object)"Patching...");
Harmony.PatchAll(typeof(ChangeNamePatches));
Logger.LogDebug((object)"Finished patching!");
}
private void SyncNewNameToOthers()
{
if (PhotonNetwork.LocalPlayer != null)
{
PhotonView.RPC("ChangeNickNameRPC", (RpcTarget)1, new object[2]
{
PhotonNetwork.LocalPlayer.ActorNumber,
NickName.Value
});
}
}
[PunRPC]
private void ChangeNickNameRPC(int ActorNumber, string NickName)
{
IsLookedAt[] array = Object.FindObjectsByType<IsLookedAt>((FindObjectsSortMode)0);
Character val = null;
IsLookedAt[] array2 = array;
foreach (IsLookedAt val2 in array2)
{
if (((MonoBehaviourPun)val2.characterInteractible.character).photonView.Owner.ActorNumber == ActorNumber)
{
FieldInfo fieldInfo = AccessTools.Field(typeof(IsLookedAt), "index");
int num = (int)fieldInfo.GetValue(val2);
Logger.LogInfo((object)("[" + ((TMP_Text)GUIManager.instance.playerNames.playerNameText[num].text).text + "]->[" + NickName + "]"));
((TMP_Text)GUIManager.instance.playerNames.playerNameText[num].text).text = NickName;
val = val2.characterInteractible.character;
break;
}
}
if (!((Object)(object)val == (Object)null) && Chainloader.PluginInfos.ContainsKey("nickklmao.peakstats"))
{
SyncPeakStats(val);
}
}
private void SyncPassport()
{
PassportManager val = Object.FindFirstObjectByType<PassportManager>();
if ((Object)(object)val != (Object)null && !((TMP_Text)val.nameText).text.Equals(Character.localCharacter.characterName))
{
MethodInfo methodInfo = AccessTools.Method(typeof(PassportManager), "Initialize", (Type[])null, (Type[])null);
methodInfo.Invoke(val, new object[0]);
}
}
private void SyncPeakStats(Character character)
{
if (!((Object)(object)Character.localCharacter != (Object)null))
{
return;
}
ProximityStaminaManager component = ((Component)Character.localCharacter).gameObject.GetComponent<ProximityStaminaManager>();
if ((Object)(object)component != (Object)null)
{
FieldInfo fieldInfo = AccessTools.Field(typeof(ProximityStaminaManager), "staminaBars");
Dictionary<Character, CharacterStaminaBar> dictionary = (Dictionary<Character, CharacterStaminaBar>)fieldInfo.GetValue(component);
if (dictionary.TryGetValue(character, out var value))
{
Object.Destroy((Object)(object)((Component)value).gameObject);
dictionary.Remove(character);
}
}
}
}
}
namespace IntChangeName.Patches
{
[HarmonyPatch(/*Could not decode attribute arguments.*/)]
internal class ChangeNamePatches
{
private static void Postfix(Player __instance)
{
if (__instance != null)
{
string nickName = __instance.NickName;
if (__instance.IsLocal && !string.IsNullOrEmpty(nickName) && !string.IsNullOrEmpty(ChangeName.NickName.Value) && !nickName.Equals(ChangeName.NickName.Value))
{
Debug.Log((object)("Current NickName: [" + nickName + "] New NickName: [" + ChangeName.NickName.Value + "]"));
__instance.NickName = ChangeName.NickName.Value;
}
}
}
}
}