diff --git a/Patches/PlayerCosmeticsLoadedPatch.cs b/Patches/PlayerCosmeticsLoadedPatch.cs new file mode 100644 index 0000000..5422220 --- /dev/null +++ b/Patches/PlayerCosmeticsLoadedPatch.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using ExitGames.Client.Photon; +using HarmonyLib; +using PlayFab; +using PlayFab.ClientModels; +using TooMuchInfo.Tools; +using UnityEngine; + +namespace TooMuchInfo.Patches; + +[HarmonyPatch(typeof(VRRig))] +public static class OnCosmeticsLoadedPatch +{ + private static readonly DateTime OculusPayDay = new(2023, 02, 06); + + [HarmonyPatch("IUserCosmeticsCallback.OnGetUserCosmetics")] + [HarmonyPostfix] + private static void OnGetRigCosmetics(VRRig __instance) => OnLoad(__instance); + + private static async void OnLoad(VRRig rig) + { + Extensions.PlayerWithCosmetics.Add(rig); + + DateTime playerCreationDate; + + if (!Extensions.AccountCreationDates.TryGetValue(rig.creator.UserId, out playerCreationDate)) + { + try + { + TaskCompletionSource tcs = new(); + + PlayFabClientAPI.GetPlayerProfile( + new GetPlayerProfileRequest + { + PlayFabId = rig.creator.UserId, + ProfileConstraints = new PlayerProfileViewConstraints + { + ShowCreated = true, + }, + }, + result => tcs.SetResult(result), + error => + { + Debug.LogError("Failed to get account info: " + error.ErrorMessage); + tcs.SetException(new Exception(error.ErrorMessage)); + }); + + GetPlayerProfileResult result = await tcs.Task; + + if (result.PlayerProfile?.Created == null) + return; + + DateTime creationDate = result.PlayerProfile.Created.Value; + + Extensions.AccountCreationDates[rig.creator.UserId] = creationDate; + playerCreationDate = creationDate; + } + catch + { + // ignored + } + } + + Hashtable properties = rig.creator.GetPlayerRef().CustomProperties; + List mods = []; + + foreach (string key in properties.Keys) + { + if (Plugin.KnownCheats.TryGetValue(key, out string cheat)) + mods.Add($"[{cheat.ToUpper()}]"); + + if (Plugin.KnownMods.TryGetValue(key, out string mod)) + mods.Add($"[{mod.ToUpper()}]"); + } + + Extensions.PlayerMods[rig] = mods; + + string platformProperty = ((string)properties["platform"]).ToLower(); + + if (string.IsNullOrEmpty(platformProperty)) + { + Extensions.PlayerPlatforms[rig] = GamePlatform.Unknown; + + return; + } + + Extensions.PlayerPlatforms[rig] = platformProperty switch + { + "steam" => GamePlatform.Steam, + "oculus pc" => GamePlatform.OculusPC, + "psvr" => GamePlatform.PSVR, + "pico" => GamePlatform.Pico, + "pc" => GamePlatform.PC, + "quest" => GamePlatform.Standalone, + var _ => GamePlatform.Unknown, + }; + } +} diff --git a/Patches/UpdateName.cs b/Patches/UpdateName.cs new file mode 100644 index 0000000..03284cd --- /dev/null +++ b/Patches/UpdateName.cs @@ -0,0 +1,75 @@ +using System.Collections.Generic; +using System.Linq; +using HarmonyLib; +using TooMuchInfo.Tools; +using UnityEngine; + +namespace TooMuchInfo.Patches; + +[HarmonyPatch(typeof(VRRig), "UpdateName", typeof(bool))] +public static class NamePatch +{ + private static void Postfix(VRRig __instance, bool isNamePermissionEnabled) + { + if (__instance.isLocal || !__instance.HasCosmetics()) + return; + + List specialCosmetics = []; + specialCosmetics.AddRange(from key in Plugin.KnownCosmetics.Keys + where __instance._playerOwnedCosmetics.Contains(key) + select Plugin.KnownCosmetics[key]); + + string fpsColour = __instance.fps < 72 ? __instance.fps < 60 ? "red" : "orange" : "green"; + string pingColour = __instance.GetPing() > 100 ? __instance.GetPing() > 250 ? "red" : "orange" : "green"; + + List lines = []; + + string nameLine = + __instance.creator.SanitizedNickName + .AnimateGradiant(__instance.playerColor) + + (Plugin.ShowKnownPeople && + Plugin.KnownPeople.TryGetValue(__instance.creator.UserId, out string known) + ? $" : {known}" + : ""); + + string fpsLine = + $"{__instance.fps} FPS : " + + $"{__instance.GetPlatform().ParsePlatform()} : " + + $"{__instance.GetPing()} MS"; + + string dateLine = __instance.GetAccountCreationDate().ToString("dd/MM/yyyy"); + string modsLine = __instance.GetPlayerMods().Join(" "); + + Dictionary sections = new() + { + { "SpecialCosmetics", specialCosmetics.Count > 0 ? specialCosmetics.Join(" ").Trim() : "" }, + { "Name", nameLine }, + { "FpsPlatformPing", fpsLine }, + { "CreationDate", dateLine }, + { "Mods", modsLine }, + }; + + foreach (string key in Plugin.LineOrder.Where(key => key != "SpecialCosmetics")) + { + if (!sections.TryGetValue(key, out string value)) + continue; + + switch (key) + { + case "SpecialCosmetics" when !Plugin.ShowSpecialCosmetics: + case "FpsPlatformPing" when !Plugin.ShowFpsPlatformPing: + case "CreationDate" when !Plugin.ShowCreationDate: + case "Mods" when !Plugin.ShowMods: + continue; + + default: + lines.Add(value); + + break; + } + } + + __instance.playerText1.transform.localPosition = new Vector3(0.0085f, 0.2f, 0.1408f); + __instance.playerText1.text = lines.Join("\n").Trim(); + } +} \ No newline at end of file