From 8b9ae3fcaf49f4e2757a5a202184aed5461baace Mon Sep 17 00:00:00 2001 From: Sun Date: Sat, 18 Jul 2020 14:19:04 -0700 Subject: [PATCH] fix CLI and fix overrides --- PresenceClient/PresenceClient-CLI/Program.cs | 12 ++-- PresenceClient/PresenceClient-GUI/MainForm.cs | 2 +- PresenceClient/PresenceCommon/Types/Title.cs | 27 +++++++- PresenceClient/PresenceCommon/Utils.cs | 61 ++++++------------- Resource/QuestApplicationOverrides.json | 4 +- Resource/SwitchApplicationOverrides.json | 5 ++ 6 files changed, 58 insertions(+), 53 deletions(-) diff --git a/PresenceClient/PresenceClient-CLI/Program.cs b/PresenceClient/PresenceClient-CLI/Program.cs index 3d90817..2e06655 100644 --- a/PresenceClient/PresenceClient-CLI/Program.cs +++ b/PresenceClient/PresenceClient-CLI/Program.cs @@ -15,7 +15,7 @@ class Program { static Timer timer; static Socket client; - static ulong LastProgramId = 0; + static string LastProgramName = string.Empty; static Timestamps time = null; static DiscordRpcClient rpc; static ConsoleOptions Arguments; @@ -98,13 +98,13 @@ private static void DataListen() Title title = new Title(bytes); if (title.Magic == 0xffaadd23) { - if (LastProgramId != title.ProgramId) + if (LastProgramName != title.Name) { time = Timestamps.Now; } - if ((rpc != null && rpc.CurrentPresence == null) || LastProgramId != title.ProgramId) + if ((rpc != null && rpc.CurrentPresence == null) || LastProgramName != title.Name) { - if (Arguments.IgnoreHomeScreen && title.ProgramId == 0) + if (Arguments.IgnoreHomeScreen && title.Name == "Home Menu") { rpc.ClearPresence(); } @@ -112,7 +112,7 @@ private static void DataListen() { rpc.SetPresence(Utils.CreateDiscordPresence(title, time)); } - LastProgramId = title.ProgramId; + LastProgramName = title.Name; } } else @@ -133,7 +133,7 @@ private static void DataListen() private static void OnConnectTimeout(object sender, ElapsedEventArgs e) { - LastProgramId = 0; + LastProgramName = string.Empty; time = null; } diff --git a/PresenceClient/PresenceClient-GUI/MainForm.cs b/PresenceClient/PresenceClient-GUI/MainForm.cs index aa20740..385224b 100644 --- a/PresenceClient/PresenceClient-GUI/MainForm.cs +++ b/PresenceClient/PresenceClient-GUI/MainForm.cs @@ -236,7 +236,7 @@ private void DataListen() { if (rpc != null) { - if (checkMainMenu.Checked == false && title.ProgramId == 0) + if (checkMainMenu.Checked == false && title.Name == "Main Menu") rpc.ClearPresence(); else { diff --git a/PresenceClient/PresenceCommon/Types/Title.cs b/PresenceClient/PresenceCommon/Types/Title.cs index 4ec8ef8..231ba1c 100644 --- a/PresenceClient/PresenceCommon/Types/Title.cs +++ b/PresenceClient/PresenceCommon/Types/Title.cs @@ -24,8 +24,31 @@ public Title(byte[] bytes) { TitlePacket title = DataHandler.ByteArrayToStructure(bytes); Magic = title.magic; - ProgramId = title.programId; - Name = Encoding.UTF8.GetString(title.name, 0, title.name.Length).Split('\0')[0]; + + if (title.programId == 0) + { + ProgramId = 0x0100000000001000; + Name = "Home Menu"; + } + else + { + ProgramId = title.programId; + Name = Encoding.UTF8.GetString(title.name, 0, title.name.Length).Split('\0')[0]; + } + if (title.programId == 0xffaadd23) + { + if (Utils.QuestOverrides.ContainsKey(Name) && Utils.QuestOverrides[Name].CustomName != null) + { + Name = Utils.QuestOverrides[Name].CustomName; + } + } + else + { + if (Utils.SwitchOverrides.ContainsKey(Name) && Utils.SwitchOverrides[Name].CustomName != null) + { + Name = Utils.SwitchOverrides[Name].CustomName; + } + } } } } diff --git a/PresenceClient/PresenceCommon/Utils.cs b/PresenceClient/PresenceCommon/Utils.cs index 369d48a..f839a3e 100644 --- a/PresenceClient/PresenceCommon/Utils.cs +++ b/PresenceClient/PresenceCommon/Utils.cs @@ -9,8 +9,8 @@ namespace PresenceCommon { public static class Utils { - private static readonly Dictionary QuestOverrides; - private static readonly Dictionary SwitchOverrides; + public static readonly Dictionary QuestOverrides; + public static readonly Dictionary SwitchOverrides; static Utils() { WebClient client = new WebClient(); @@ -32,75 +32,52 @@ public static RichPresence CreateDiscordPresence(Title title, Timestamps time, s Assets assets = new Assets { - SmallImageKey = smallImageKey, + SmallImageKey = smallImageKey }; + assets.LargeImageText = title.Name; if (title.ProgramId != 0xffaadd23) { assets.SmallImageText = "SwitchPresence-Rewritten"; + if (!SwitchOverrides.ContainsKey(title.Name)) { - if (title.Name == "SNULL") - { - assets.LargeImageText = !string.IsNullOrWhiteSpace(largeImageText) ? largeImageText : "Home Menu"; - assets.LargeImageKey = !string.IsNullOrWhiteSpace(largeImageKey) ? largeImageKey : $"0{0x0100000000001000:x}"; - presence.Details = "In the home menu"; - } - else - { - assets.LargeImageText = !string.IsNullOrWhiteSpace(largeImageText) ? largeImageText : title.Name; - assets.LargeImageKey = !string.IsNullOrWhiteSpace(largeImageKey) ? largeImageKey : $"0{title.ProgramId:x}"; - presence.Details = $"Playing {title.Name}"; - } + assets.LargeImageKey = $"0{title.ProgramId:x}"; + presence.Details = $"Playing {title.Name}"; } else { OverrideInfo pkgInfo = SwitchOverrides[title.Name]; - assets.LargeImageKey = pkgInfo.CustomKey ?? (!string.IsNullOrWhiteSpace(largeImageKey) ? largeImageKey : $"0{title.ProgramId:x}"); + assets.LargeImageKey = pkgInfo.CustomKey ?? $"0{title.ProgramId:x}"; presence.Details = pkgInfo.CustomPrefix ?? "Playing"; - - if (pkgInfo.CustomName != null) - { - presence.Details += $" {pkgInfo.CustomName}"; - assets.LargeImageText = pkgInfo.CustomName; - } - else - { - presence.Details += $" {title.Name}"; - assets.LargeImageText = title.Name; - } + presence.Details += $" {title.Name}"; } } else { assets.SmallImageText = "QuestPresence"; + if (!QuestOverrides.ContainsKey(title.Name)) { - assets.LargeImageText = !string.IsNullOrWhiteSpace(largeImageText) ? largeImageText : title.Name; - assets.LargeImageKey = !string.IsNullOrWhiteSpace(largeImageKey) ? largeImageKey : title.Name.ToLower().Replace(" ", ""); + assets.LargeImageKey = title.Name.ToLower().Replace(" ", ""); presence.Details = $"Playing {title.Name}"; } else { OverrideInfo pkgInfo = QuestOverrides[title.Name]; - assets.LargeImageKey = pkgInfo.CustomKey ?? (pkgInfo.CustomName != null ? pkgInfo.CustomName.ToLower().Replace(" ", "") : title.Name.ToLower().Replace(" ", "")); + assets.LargeImageKey = pkgInfo.CustomKey ?? title.Name.ToLower().Replace(" ", ""); presence.Details = pkgInfo.CustomPrefix ?? "Playing"; - - if (pkgInfo.CustomName != null) - { - presence.Details += $" {pkgInfo.CustomName}"; - assets.LargeImageText = pkgInfo.CustomName; - } - else - { - presence.Details += $" {title.Name}"; - assets.LargeImageText = title.Name; - } + presence.Details += $" {title.Name}"; } } + if (!string.IsNullOrEmpty(largeImageKey)) + assets.LargeImageKey = largeImageKey; + + if (!string.IsNullOrEmpty(largeImageText)) + assets.LargeImageText = largeImageText; presence.Assets = assets; presence.Timestamps = time; @@ -124,7 +101,7 @@ public static byte[] ReceiveExactly(Socket handler, int length = 628) return buffer; } - private partial class OverrideInfo + public partial class OverrideInfo { public string CustomName { set; get; } public string CustomPrefix { set; get; } diff --git a/Resource/QuestApplicationOverrides.json b/Resource/QuestApplicationOverrides.json index fc4632e..0b863b2 100644 --- a/Resource/QuestApplicationOverrides.json +++ b/Resource/QuestApplicationOverrides.json @@ -1,12 +1,12 @@ { "QuestPresence": { "CustomPrefix": "On the", - "CustomName": "Main Menu", + "CustomName": "Home Menu", "CustomKey": null }, "Oculus App Runtime": { "CustomPrefix": "On the", - "CustomName": "Main Menu", + "CustomName": "Home Menu", "CustomKey": null }, "Netflix": { diff --git a/Resource/SwitchApplicationOverrides.json b/Resource/SwitchApplicationOverrides.json index e0b5d8b..9fe5b01 100644 --- a/Resource/SwitchApplicationOverrides.json +++ b/Resource/SwitchApplicationOverrides.json @@ -3,5 +3,10 @@ "CustomPrefix": "Watching", "CustomName": null, "CustomKey": null + }, + "Home Menu": { + "CustomPrefix": "In the", + "CustomName": null, + "CustomKey": null } } \ No newline at end of file