Skip to content

Commit

Permalink
Add custom application scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
cswendrowski committed Oct 25, 2019
1 parent 4e057bb commit 3890f0c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 72 deletions.
12 changes: 6 additions & 6 deletions LocalApi/RichPresenceApi/Controllers/PlayerStatusController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public PlayerStatusController()
DiscordManager.CreateDiscord();
}

public static byte[] GetHash(string inputString)
public static string GetHash(string inputString)
{
var algorithm = SHA256.Create();
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString)).ToString();
}

private string ToUf8(string strFrom)
{
byte[] bytes = Encoding.UTF8.GetBytes(strFrom);
byte[] bytes = Encoding.Default.GetBytes(strFrom);
return Encoding.UTF8.GetString(bytes);
}

Expand All @@ -34,7 +34,7 @@ public void PutPlayerStatus([FromBody] PlayerStatus playerStatus)
{
var activity = new Activity()
{
Details = ToUf8(string.IsNullOrEmpty(playerStatus.ActorName) ? $"Playing {playerStatus.SystemName}" : $"Playing as {playerStatus.ActorName}"),
Details = string.IsNullOrEmpty(playerStatus.ActorName) ? $"Playing {playerStatus.SystemName}" : $"Playing as {playerStatus.ActorName}",
State = $"Exploring {playerStatus.SceneName}",
Party = new ActivityParty { Id = playerStatus.WorldUniqueId, Size = new PartySize { CurrentSize = playerStatus.CurrentPlayerCount, MaxSize = playerStatus.MaxPlayerCount } },
Assets = new ActivityAssets
Expand All @@ -43,7 +43,7 @@ public void PutPlayerStatus([FromBody] PlayerStatus playerStatus)
LargeText = "D20"
},
Instance = false,
Secrets = new ActivitySecrets { Join = $"{GetHash(playerStatus.WorldUniqueId)}" }
Secrets = new ActivitySecrets { Join = $"{playerStatus.FoundryUrl}/join" }
};

if (playerStatus.IsGm)
Expand All @@ -52,7 +52,7 @@ public void PutPlayerStatus([FromBody] PlayerStatus playerStatus)
activity.State = $"Playing {playerStatus.SystemName}";
}

DiscordManager.SetActivity(activity, playerStatus);
DiscordManager.SetActivity(activity);
}

[Route("leave")]
Expand Down
21 changes: 15 additions & 6 deletions LocalApi/RichPresenceApi/DiscordManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DiscordSdk;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using TestApi.Models;
Expand All @@ -24,6 +25,10 @@ static DiscordManager()
Console.WriteLine("Created DiscordManager " + WhoAmI);
_discordTimer = new Timer(OnDiscordUpdate, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
CreateDiscord();

var command = "cmd.exe /c start foundryvtt-richpresence://run";
_discord.GetActivityManager().RegisterCommand(command);
Console.WriteLine("Registered command " + command);
}

public static void CreateDiscord()
Expand All @@ -32,9 +37,18 @@ public static void CreateDiscord()

Console.WriteLine(WhoAmI + " - Creating new Discord instance");
_discord = new Discord(long.Parse("635971834499563530"), (ulong)CreateFlags.Default);

_discord.GetActivityManager().OnActivityJoin += HandleActivityJoin;

_discordTimer.Change(DiscordRefreshRate, DiscordRefreshRate);
}

private static void HandleActivityJoin(string secret)
{
Console.WriteLine("Handling Activity Join with secret " + secret);
Process.Start(secret);
}

public static Discord GetDiscord()
{
if (_discord == null)
Expand All @@ -45,18 +59,14 @@ public static Discord GetDiscord()
return _discord;
}

internal static void SetActivity(Activity activity, PlayerStatus playerStatus)
internal static void SetActivity(DiscordSdk.Activity activity)
{
if (IsCurrentlyDisposing) return;

var guid = Guid.NewGuid().ToString();

var activityManager = GetDiscord().GetActivityManager();

var command = $"cmd.exe /c start {playerStatus.FoundryUrl}/join";
activityManager.RegisterCommand(command);
Console.WriteLine("Registered command " + command);

activityManager.UpdateActivity(activity, result =>
{
Console.WriteLine(guid + " - Set activity result: " + result);
Expand All @@ -76,7 +86,6 @@ public static void Dispose()
{
if (_discord == null)
{
Console.WriteLine("Discord instance is null while clearing! This is probably wrong");
return;
};

Expand Down
26 changes: 26 additions & 0 deletions LocalApi/RichPresenceApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Win32;
using System;

namespace TestApi
{
public class Program
{
const string UriScheme = "foundryvtt-richpresence";
const string FriendlyName = "FoundryVTT Discord Rich Presence";

public static void Main(string[] args)
{
using (var key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Classes\\" + UriScheme))
{
string applicationLocation = typeof(Startup).Assembly.Location.Replace(".dll", ".exe");

key.SetValue("", "URL:" + FriendlyName);
key.SetValue("URL Protocol", "");

using (var defaultIcon = key.CreateSubKey("DefaultIcon"))
{
defaultIcon.SetValue("", applicationLocation + ",1");
}

using (var commandKey = key.CreateSubKey(@"shell\open\command"))
{
commandKey.SetValue("", "\"" + applicationLocation + "\" \"%1\"");
}
Console.WriteLine($"Registered URI scheme {UriScheme}:// to location {applicationLocation}");
}

DiscordManager.CreateDiscord();

CreateHostBuilder(args)
.Build()
.Run();
Expand Down
61 changes: 1 addition & 60 deletions LocalApi/RichPresenceApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Startup
public Startup(IConfiguration configuration)
{
Configuration = configuration;
AppDomain.CurrentDomain.ProcessExit += (s, e) => DiscordManager.Dispose();
}

public IConfiguration Configuration { get; }
Expand All @@ -38,68 +39,8 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddControllers();

//var discord = new Discord(long.Parse("635971834499563530"), (ulong)CreateFlags.Default);

//RegisterActivityManager(discord);

//RunCallbacks(discord);

//services.AddSingleton(discord);
}

private static void RunCallbacks(Discord discord)
{
Task.Run(() =>
{
try
{
while (true)
{
discord.RunCallbacks();
Thread.Sleep(1000 / 60);
}
}
finally
{
discord.Dispose();
}
});
}

private static void RegisterActivityManager(Discord discord)
{
var activityManager = discord.GetActivityManager();

// Received when someone accepts a request to join or invite.
// Use secrets to receive back the information needed to add the user to the group/party/match
activityManager.OnActivityJoin += secret =>
{
Console.WriteLine("OnJoin {0}", secret);
};

// Received when someone accepts a request to spectate
activityManager.OnActivitySpectate += secret =>
{
Console.WriteLine("OnSpectate {0}", secret);
};

// A join request has been received. Render the request on the UI.
activityManager.OnActivityJoinRequest += (ref User user) =>
{
Console.WriteLine("OnJoinRequest {0} {1}", user.Id, user.Username);
};

// An invite has been received. Consider rendering the user / activity on the UI.
activityManager.OnActivityInvite += (ActivityActionType Type, ref User user, ref Activity activity2) =>
{
Console.WriteLine("OnInvite {0} {1} {2}", Type, user.Username, activity2.Name);
// activityManager.AcceptInvite(user.Id, result =>
// {
// Console.WriteLine("AcceptInvite {0}", result);
// });
};
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand Down

0 comments on commit 3890f0c

Please sign in to comment.