upload
This commit is contained in:
commit
1c7a8cf25f
579 files changed
+83196
No files matched your search
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Pipes;
|
||||
|
||||
namespace VibranceHud.Discord;
|
||||
|
||||
public sealed class DiscordPresenceClient : IDisposable
|
||||
{
|
||||
private readonly Func<Stream?> _open;
|
||||
|
||||
private readonly bool _leaveOpen;
|
||||
|
||||
private readonly Func<DateTime> _now;
|
||||
|
||||
private Stream? _pipe;
|
||||
|
||||
internal static readonly TimeSpan RescanAfterMiss = TimeSpan.FromSeconds(30.0);
|
||||
|
||||
private DateTime _dontLookUntil = DateTime.MinValue;
|
||||
|
||||
public bool Connected => _pipe != null;
|
||||
|
||||
public DiscordPresenceClient()
|
||||
: this(OpenDiscordPipe, leaveOpen: false)
|
||||
{
|
||||
}
|
||||
|
||||
public DiscordPresenceClient(Func<Stream?> open, bool leaveOpen, Func<DateTime>? now = null)
|
||||
{
|
||||
_now = now ?? ((Func<DateTime>)(() => DateTime.UtcNow));
|
||||
_open = open;
|
||||
_leaveOpen = leaveOpen;
|
||||
}
|
||||
|
||||
private static Stream? OpenDiscordPipe()
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
NamedPipeClientStream namedPipeClientStream = new NamedPipeClientStream(".", $"discord-ipc-{i}", PipeDirection.InOut, PipeOptions.Asynchronous);
|
||||
namedPipeClientStream.Connect(250);
|
||||
return namedPipeClientStream;
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
if (_pipe != null || _now() < _dontLookUntil)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
_pipe = _open();
|
||||
if (_pipe == null)
|
||||
{
|
||||
_dontLookUntil = _now() + RescanAfterMiss;
|
||||
}
|
||||
else
|
||||
{
|
||||
Write(DiscordRpc.Frame(0, DiscordRpc.HandshakeJson()));
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Drop();
|
||||
_dontLookUntil = _now() + RescanAfterMiss;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetPreset(string? presetName)
|
||||
{
|
||||
if (_pipe == null)
|
||||
{
|
||||
Connect();
|
||||
}
|
||||
if (_pipe == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
Write(DiscordRpc.Frame(1, DiscordRpc.SetActivityJson(Environment.ProcessId, presetName)));
|
||||
}
|
||||
catch
|
||||
{
|
||||
Drop();
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Drop();
|
||||
}
|
||||
|
||||
private void Write(byte[] frame)
|
||||
{
|
||||
_pipe.Write(frame, 0, frame.Length);
|
||||
_pipe.Flush();
|
||||
}
|
||||
|
||||
private void Drop()
|
||||
{
|
||||
Stream pipe = _pipe;
|
||||
_pipe = null;
|
||||
if (!_leaveOpen)
|
||||
{
|
||||
try
|
||||
{
|
||||
pipe?.Dispose();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Drop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace VibranceHud.Discord;
|
||||
|
||||
public static class DiscordRpc
|
||||
{
|
||||
public const string ApplicationId = "1537861543633494146";
|
||||
|
||||
public const int OpHandshake = 0;
|
||||
|
||||
public const int OpFrame = 1;
|
||||
|
||||
public static byte[] Frame(int opcode, string json)
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(json);
|
||||
byte[] array = new byte[8 + bytes.Length];
|
||||
BitConverter.GetBytes(opcode).CopyTo(array, 0);
|
||||
BitConverter.GetBytes(bytes.Length).CopyTo(array, 4);
|
||||
bytes.CopyTo(array, 8);
|
||||
return array;
|
||||
}
|
||||
|
||||
public static string HandshakeJson()
|
||||
{
|
||||
return JsonSerializer.Serialize(new
|
||||
{
|
||||
v = 1,
|
||||
client_id = "1537861543633494146"
|
||||
});
|
||||
}
|
||||
|
||||
public static string SetActivityJson(int pid, string? presetName)
|
||||
{
|
||||
object assets = new
|
||||
{
|
||||
large_image = "logo",
|
||||
large_text = "PlexusX"
|
||||
};
|
||||
object[] buttons = new object[2]
|
||||
{
|
||||
new
|
||||
{
|
||||
label = "View Site",
|
||||
url = "https://plexusx.lol"
|
||||
},
|
||||
new
|
||||
{
|
||||
label = "Download",
|
||||
url = "https://plexusx.lol"
|
||||
}
|
||||
};
|
||||
object activity = ((presetName == null) ? ((object)new
|
||||
{
|
||||
details = "Colour, all the way up",
|
||||
assets = assets,
|
||||
buttons = buttons
|
||||
}) : ((object)new
|
||||
{
|
||||
details = "Colour, all the way up",
|
||||
state = presetName,
|
||||
assets = assets,
|
||||
buttons = buttons
|
||||
}));
|
||||
return JsonSerializer.Serialize(new
|
||||
{
|
||||
cmd = "SET_ACTIVITY",
|
||||
args = new { pid, activity },
|
||||
nonce = Guid.NewGuid().ToString("N")
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user