Add message commands for presence timestamp/icon

also fix a bug with the server location query thingy
This commit is contained in:
pizzaboxer 2023-07-26 22:36:24 +01:00
parent 81161be984
commit d618ff350c
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
2 changed files with 91 additions and 7 deletions

View File

@ -26,7 +26,7 @@
public event EventHandler? OnGameLeave; public event EventHandler? OnGameLeave;
public event EventHandler<GameMessage>? OnGameMessage; public event EventHandler<GameMessage>? OnGameMessage;
private Dictionary<string, string> GeolcationCache = new(); private Dictionary<string, string> GeolocationCache = new();
public string LogLocation = null!; public string LogLocation = null!;
@ -268,8 +268,8 @@
{ {
const string LOG_IDENT = "ActivityWatcher::GetServerLocation"; const string LOG_IDENT = "ActivityWatcher::GetServerLocation";
if (GeolcationCache.ContainsKey(ActivityMachineAddress)) if (GeolocationCache.ContainsKey(ActivityMachineAddress))
return GeolcationCache[ActivityMachineAddress]; return GeolocationCache[ActivityMachineAddress];
string location, locationCity, locationRegion, locationCountry = ""; string location, locationCity, locationRegion, locationCountry = "";
@ -298,7 +298,10 @@
else else
location = $"{locationCity}, {locationRegion}, {locationCountry}"; location = $"{locationCity}, {locationRegion}, {locationCountry}";
GeolcationCache[ActivityMachineAddress] = location; if (!ActivityInGame)
return "N/A (left game)";
GeolocationCache[ActivityMachineAddress] = location;
return location; return location;
} }

View File

@ -1,4 +1,5 @@
using DiscordRPC; using DiscordRPC;
using System.Reflection.Metadata.Ecma335;
namespace Bloxstrap.Integrations namespace Bloxstrap.Integrations
{ {
@ -47,10 +48,23 @@ namespace Bloxstrap.Integrations
public void OnGameMessage(GameMessage message) public void OnGameMessage(GameMessage message)
{ {
if (message.Command == "SetPresenceStatus") switch (message.Command)
{
case "SetPresenceStatus":
SetStatus(message.Data); SetStatus(message.Data);
break;
case "SetPresenceTimestamp":
SetTimestamp(message.Data);
break;
case "SetPresenceIcon":
SetIcon(message.Data);
break;
}
} }
#region Game message commands
public void SetStatus(string status) public void SetStatus(string status)
{ {
const string LOG_IDENT = "DiscordRichPresence::SetStatus"; const string LOG_IDENT = "DiscordRichPresence::SetStatus";
@ -74,7 +88,7 @@ namespace Bloxstrap.Integrations
string finalStatus; string finalStatus;
if (string.IsNullOrEmpty(status)) if (String.IsNullOrEmpty(status))
{ {
App.Logger.WriteLine(LOG_IDENT, $"Status is empty, reverting to initial status"); App.Logger.WriteLine(LOG_IDENT, $"Status is empty, reverting to initial status");
finalStatus = _initialStatus; finalStatus = _initialStatus;
@ -94,6 +108,73 @@ namespace Bloxstrap.Integrations
UpdatePresence(); UpdatePresence();
} }
public void SetTimestamp(string data)
{
const string LOG_IDENT = "DiscordRichPresence::SetTimestamp";
if (_currentPresence is null)
{
App.Logger.WriteLine(LOG_IDENT, "Presence is not set, aborting");
return;
}
if (String.IsNullOrEmpty(data))
{
App.Logger.WriteLine(LOG_IDENT, "Clearing timestamp");
_currentPresence.Timestamps.Start = null;
_currentPresence.Timestamps.End = null;
}
else
{
var parameters = data.Split(';');
if (parameters.Length >= 1 && ulong.TryParse(parameters[0], out ulong startTimestamp))
_currentPresence.Timestamps.StartUnixMilliseconds = startTimestamp * 1000;
if (parameters.Length >= 2 && ulong.TryParse(parameters[1], out ulong endTimestamp))
_currentPresence.Timestamps.EndUnixMilliseconds = endTimestamp * 1000;
}
UpdatePresence();
}
public void SetIcon(string data)
{
const string LOG_IDENT = "DiscordRichPresence::SetIcon";
if (_currentPresence is null)
{
App.Logger.WriteLine(LOG_IDENT, "Presence is not set, aborting");
return;
}
if (String.IsNullOrEmpty(data))
{
_currentPresence.Assets.SmallImageKey = "roblox";
_currentPresence.Assets.SmallImageText = "Roblox";
}
else
{
var parameters = data.Split(';');
if (parameters.Length < 2 || !ulong.TryParse(parameters[0], out ulong assetId))
return;
if (parameters[1].Length > 128)
{
App.Logger.WriteLine(LOG_IDENT, $"Icon text cannot be longer than 128 characters, aborting");
return;
}
_currentPresence.Assets.SmallImageKey = $"https://assetdelivery.roblox.com/v1/asset/?id={assetId}";
_currentPresence.Assets.SmallImageText = parameters[1];
}
UpdatePresence();
}
#endregion
public void SetVisibility(bool visible) public void SetVisibility(bool visible)
{ {
App.Logger.WriteLine("DiscordRichPresence::SetVisibility", $"Setting presence visibility ({visible})"); App.Logger.WriteLine("DiscordRichPresence::SetVisibility", $"Setting presence visibility ({visible})");