forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscord.cs
222 lines (204 loc) · 6.75 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright(c) 2019-2022 pypy, Natsumi and individual contributors.
// 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.Text;
using System.Threading;
namespace VRCX
{
public class Discord
{
public static readonly Discord Instance;
private readonly ReaderWriterLockSlim m_Lock;
private readonly RichPresence m_Presence;
private DiscordRpcClient m_Client;
private readonly Timer m_Timer;
private bool m_Active;
public static string DiscordAppId;
static Discord()
{
Instance = new Discord();
}
public Discord()
{
m_Lock = new ReaderWriterLockSlim();
m_Presence = new RichPresence();
m_Timer = new Timer(TimerCallback, null, -1, -1);
}
internal void Init()
{
m_Timer.Change(0, 1000);
}
internal void Exit()
{
lock (this)
{
m_Timer.Change(-1, -1);
m_Client?.Dispose();
}
}
private void TimerCallback(object state)
{
lock (this)
{
try
{
Update();
}
catch
{
}
}
}
private void Update()
{
if (m_Client == null && m_Active)
{
m_Client = new DiscordRpcClient(DiscordAppId);
if (m_Client.Initialize() == false)
{
m_Client.Dispose();
m_Client = null;
}
}
if (m_Client != null && !m_Active)
{
m_Client.Dispose();
m_Client = null;
}
if (m_Client != null && !m_Lock.IsWriteLockHeld)
{
m_Lock.EnterReadLock();
try
{
m_Client.SetPresence(m_Presence);
}
finally
{
m_Lock.ExitReadLock();
}
m_Client.Invoke();
}
}
public bool SetActive(bool active)
{
m_Active = active;
return m_Active;
}
// https://stackoverflow.com/questions/1225052/best-way-to-shorten-utf8-string-based-on-byte-length
private static string LimitByteLength(string str, int maxBytesLength)
{
if (str == null)
return string.Empty;
var bytesArr = Encoding.UTF8.GetBytes(str);
var bytesToRemove = 0;
var lastIndexInString = str.Length - 1;
while (bytesArr.Length - bytesToRemove > maxBytesLength)
{
bytesToRemove += Encoding.UTF8.GetByteCount(new char[] { str[lastIndexInString] });
--lastIndexInString;
}
return Encoding.UTF8.GetString(bytesArr, 0, bytesArr.Length - bytesToRemove);
}
public void SetText(string details, string state)
{
if (m_Client != null && !m_Lock.IsReadLockHeld)
{
m_Lock.EnterWriteLock();
try
{
m_Presence.Details = LimitByteLength(details, 127);
m_Presence.State = LimitByteLength(state, 127);
}
finally
{
m_Lock.ExitWriteLock();
}
}
}
public void SetAssets(string largeKey, string largeText, string smallKey, string smallText, string partyId, int partySize, int partyMax, string buttonText, string buttonUrl, string appId)
{
m_Lock.EnterWriteLock();
try
{
if (string.IsNullOrEmpty(largeKey) == true &&
string.IsNullOrEmpty(smallKey) == true)
{
m_Presence.Assets = null;
}
else
{
if (m_Presence.Assets == null)
m_Presence.Assets = new Assets();
if (m_Presence.Party == null)
m_Presence.Party = new Party();
m_Presence.Assets.LargeImageKey = largeKey;
m_Presence.Assets.LargeImageText = largeText;
m_Presence.Assets.SmallImageKey = smallKey;
m_Presence.Assets.SmallImageText = smallText;
m_Presence.Party.ID = partyId;
m_Presence.Party.Size = partySize;
m_Presence.Party.Max = partyMax;
Button[] Buttons = { };
if (!string.IsNullOrEmpty(buttonUrl))
{
Buttons = new Button[]
{
new Button() { Label = buttonText, Url = buttonUrl }
};
}
m_Presence.Buttons = Buttons;
if (DiscordAppId != appId)
{
DiscordAppId = appId;
if (m_Client != null)
{
m_Client.Dispose();
m_Client = null;
}
Update();
}
}
}
finally
{
m_Lock.ExitWriteLock();
}
}
public void SetTimestamps(double startUnixMilliseconds, double endUnixMilliseconds)
{
var _startUnixMilliseconds = (ulong)startUnixMilliseconds;
var _endUnixMilliseconds = (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();
}
}
}
}