forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscord.cs
165 lines (156 loc) · 4.9 KB
/
Discord.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright(c) 2019 pypy. All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using DiscordRPC;
using System.Threading;
namespace VRCX
{
public class Discord
{
private static readonly ReaderWriterLockSlim m_Lock = new ReaderWriterLockSlim();
private static readonly RichPresence m_Presence = new RichPresence();
private static Thread m_Thread;
private static bool m_Active;
public static void Init()
{
m_Thread = new Thread(() =>
{
DiscordRpcClient client = null;
while (m_Thread != null)
{
try
{
Thread.Sleep(1000);
}
catch
{
// ThreadInterruptedException
}
if (client != null)
{
m_Lock.EnterReadLock();
try
{
client.SetPresence(m_Presence);
}
finally
{
m_Lock.ExitReadLock();
}
client.Invoke();
}
if (m_Active)
{
if (client == null)
{
client = new DiscordRpcClient("525953831020920832");
if (!client.Initialize())
{
client.Dispose();
client = null;
}
}
}
else if (client != null)
{
client.Dispose();
client = null;
}
}
client?.Dispose();
})
{
IsBackground = true
};
m_Thread.Start();
}
public static void Exit()
{
var T = m_Thread;
m_Thread = null;
T.Interrupt();
T.Join();
}
public void SetActive(bool active)
{
m_Active = active;
}
public void SetText(string details, string state)
{
m_Lock.EnterWriteLock();
try
{
m_Presence.Details = details;
m_Presence.State = state;
}
finally
{
m_Lock.ExitWriteLock();
}
}
public void SetAssets(string largeKey, string largeText, string smallKey, string smallText)
{
m_Lock.EnterWriteLock();
try
{
if (string.IsNullOrEmpty(largeKey) &&
string.IsNullOrEmpty(smallKey))
{
m_Presence.Assets = null;
}
else
{
if (m_Presence.Assets == null)
{
m_Presence.Assets = new Assets();
}
m_Presence.Assets.LargeImageKey = largeKey;
m_Presence.Assets.LargeImageText = largeText;
m_Presence.Assets.SmallImageKey = smallKey;
m_Presence.Assets.SmallImageText = smallText;
}
}
finally
{
m_Lock.ExitWriteLock();
}
}
// JSB Sucks
public void SetTimestamps(double startUnixMilliseconds, double endUnixMilliseconds)
{
SetTimestamps((ulong)startUnixMilliseconds, (ulong)endUnixMilliseconds);
}
public static void SetTimestamps(ulong startUnixMilliseconds, ulong endUnixMilliseconds)
{
m_Lock.EnterWriteLock();
try
{
if (startUnixMilliseconds == 0)
{
m_Presence.Timestamps = null;
}
else
{
if (m_Presence.Timestamps == null)
{
m_Presence.Timestamps = new Timestamps();
}
m_Presence.Timestamps.StartUnixMilliseconds = startUnixMilliseconds;
if (endUnixMilliseconds == 0)
{
m_Presence.Timestamps.End = null;
}
else
{
m_Presence.Timestamps.EndUnixMilliseconds = endUnixMilliseconds;
}
}
}
finally
{
m_Lock.ExitWriteLock();
}
}
}
}