-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDiscordRPMod.cs
401 lines (347 loc) · 11.4 KB
/
DiscordRPMod.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using System;
using System.Collections.Generic;
using DiscordRPC;
using Terraria;
using Terraria.ModLoader;
namespace DiscordRP {
/// <summary>
/// Rich Presence Status class, currently only used for custom main menu
/// </summary>
internal class DRPStatus {
public string details = "", additionalDetails = "";
public string largeKey = "", largeImage = "";
public string smallKey = "", smallImage = "";
public string GetState() => additionalDetails;
public string GetDetails() => details;
}
/// <summary>
/// Class for custom biomes
/// </summary>
internal class BiomeStatus {
public Func<bool> checker = null;
public string largeKey = "biome_placeholder";
public string largeText = "???";
public string client = "default";
public float priority = 0f;
}
public class DiscordRPMod : Mod {
//Mod Helper Issues report
public static string GithubUserName => "PurplefinNeptuna";
public static string GithubProjectName => "DiscordRP-tModLoader";
public static DiscordRPMod Instance = null;
public string currentClient = "default";
internal uint Cooldown => config.timer * 60u;
internal DiscordRpcClient Client {
get; set;
}
internal RichPresence RichPresenceInstance {
get; private set;
}
internal uint prevCount = 0;
internal bool pauseUpdate = false;
internal bool canCreateClient;
internal ClientConfig config = null;
internal Timestamps timestamp = null;
internal Dictionary<int, (string, string, string, float)> exBossIDtoDetails = new Dictionary<int, (string, string, string, float)>();
internal DRPStatus customStatus = null;
internal List<BiomeStatus> exBiomeStatus = new List<BiomeStatus>();
internal string worldStaticInfo = null;
//internal static Dictionary<string, DiscordRpcClient> discordRPCs;
internal Dictionary<string, string> savedDiscordAppId;
public DiscordRPMod() {
Properties = new ModProperties() {
Autoload = true,
AutoloadBackgrounds = true,
AutoloadGores = true,
AutoloadSounds = true
};
Instance = this;
}
public override void Load() {
if (!Main.dedServ) {
currentClient = "default";
canCreateClient = true;
pauseUpdate = false;
exBiomeStatus = new List<BiomeStatus>();
exBossIDtoDetails = new Dictionary<int, (string, string, string, float)>();
savedDiscordAppId = new Dictionary<string, string>();
RichPresenceInstance = new RichPresence {
Secrets = new Secrets()
};
timestamp = Timestamps.Now;
CreateNewDiscordRPCRichPresenceInstance();
//CreateNewDiscordRPCRichPresenceInstance("716207249902796810", "angryslimey");
//AddDiscordAppID("angryslimey", "716207249902796810");
}
}
public override void AddRecipes() {
if (!Main.dedServ) {
DRPX.AddVanillaBosses();
DRPX.AddVanillaBiomes();
DRPX.AddVanillaEvents();
Main.OnTick += ClientUpdate;
//finished
canCreateClient = false;
ClientOnMainMenu();
}
}
/// <summary>
/// Change the Discord App ID, currently takes 3s to change
/// </summary>
/// <param name="newClient">New Discord App ID key</param>
public void ChangeDiscordClient(string newClient) {
if (newClient == currentClient) {
return;
}
if (!savedDiscordAppId.ContainsKey(newClient)) {
return;
}
currentClient = newClient;
if (Client.ApplicationID != savedDiscordAppId[newClient]) {
Client.ApplicationID = savedDiscordAppId[newClient];
}
}
/// <summary>
/// Create new DiscordRP client, currently only used once
/// </summary>
/// <param name="appId">Discord App ID</param>
/// <param name="key">key for App ID</param>
internal void CreateNewDiscordRPCRichPresenceInstance(string key = "default") {
string appId = "404654478072086529";
if (!savedDiscordAppId.ContainsKey(key)) {
savedDiscordAppId.Add(key, appId);
}
Client = new DiscordRpcClient(applicationID: appId, autoEvents: false);
bool failedToRegisterScheme = false;
try {
Client.RegisterUriScheme("1281930");
}
catch (Exception) {
failedToRegisterScheme = true;
}
if (!failedToRegisterScheme) {
Client.OnJoinRequested += ClientOnJoinRequested;
Client.OnJoin += ClientOnJoin;
}
if (config.enable) {
Client.Initialize();
}
}
/// <summary>
/// Add other Discord App ID
/// </summary>
/// <param name="key">the key</param>
/// <param name="appID">Discord App ID</param>
public void AddDiscordAppID(string key, string appID) {
if (!savedDiscordAppId.ContainsKey(key)) {
savedDiscordAppId.Add(key, appID);
}
}
/// <summary>
/// Discord OnJoin event, called on the joiner
/// </summary>
private void ClientOnJoin(object sender, DiscordRPC.Message.JoinMessage args) {
//this is empty lol
//SocialAPI.Network.Connect(new SteamAddress(new CSteamID(Convert.ToUInt64(args.Secret))));
}
/// <summary>
/// Discord OnJoinRequested event, called on the host, currently deny everything lol
/// </summary>
private void ClientOnJoinRequested(object sender, DiscordRPC.Message.JoinRequestMessage args) {
Client.Respond(args, false);
}
/// <summary>
/// Change the status to main menu
/// </summary>
private void ClientOnMainMenu() {
ChangeDiscordClient("default");
pauseUpdate = false;
if (customStatus == null) {
ClientSetStatus("", "In Main Menu", "payload_test", "tModLoader");
}
else {
ClientSetStatus(customStatus.GetState(), customStatus.GetDetails(),
customStatus.largeKey, customStatus.largeImage,
customStatus.smallKey, customStatus.smallImage);
}
ClientSetParty();
ClientForceUpdate();
}
/// <summary>
/// override this because i can only find this method that called when going to main menu
/// </summary>
public override void PreSaveAndQuit() {
if (!Main.dedServ) {
ClientOnMainMenu();
}
}
/// <summary>
/// Change the status
/// </summary>
/// <param name="state">lower status string</param>
/// <param name="details">upper status string</param>
/// <param name="largeImageKey">key for large image</param>
/// <param name="largeImageText">text for large image</param>
/// <param name="smallImageKey">key for small image</param>
/// <param name="smallImageText">text for small image</param>
public void ClientSetStatus(string state = "", string details = "", string largeImageKey = null, string largeImageText = null, string smallImageKey = null, string smallImageText = null) {
RichPresenceInstance.Assets = RichPresenceInstance.Assets ?? new Assets();
RichPresenceInstance.State = state;
RichPresenceInstance.Details = details;
if (largeImageKey == null) {
RichPresenceInstance.Assets.LargeImageKey = null;
RichPresenceInstance.Assets.LargeImageText = null;
}
else {
RichPresenceInstance.Assets.LargeImageKey = largeImageKey;
RichPresenceInstance.Assets.LargeImageText = largeImageText;
}
if (smallImageKey == null) {
RichPresenceInstance.Assets.SmallImageKey = null;
RichPresenceInstance.Assets.SmallImageText = null;
}
else {
RichPresenceInstance.Assets.SmallImageKey = smallImageKey;
RichPresenceInstance.Assets.SmallImageText = smallImageText;
}
}
/// <summary>
/// set the party settings
/// </summary>
/// <param name="secret">party secret</param>
/// <param name="id">party id</param>
/// <param name="partysize">party current size</param>
public void ClientSetParty(string secret = null, string id = null, int partysize = 0) {
if (partysize == 0 || id == null) {
RichPresenceInstance.Secrets.JoinSecret = null;
RichPresenceInstance.Party = null;
}
else {
//RichPresenceInstance.Secrets.JoinSecret = secret;
//RichPresenceInstance.Party = RichPresenceInstance.Party ?? new Party();
//RichPresenceInstance.Party.Size = partysize;
//RichPresenceInstance.Party.Max = 256;
//RichPresenceInstance.Party.ID = id;
RichPresenceInstance.Secrets.JoinSecret = null;
RichPresenceInstance.Party = null;
}
}
/// <summary>
/// Forcing update rich presence
/// </summary>
public void ClientForceUpdate() {
if (Client != null && !Client.IsDisposed) {
if (!Client.IsInitialized && config.enable) {
Client.Initialize();
}
RichPresenceInstance.Timestamps = config.showTime ? timestamp : null;
Client.SetPresence(RichPresenceInstance);
Client.Invoke();
}
}
/// <summary>
/// run this everytick to update
/// </summary>
public void ClientUpdate() {
if (!Main.gameMenu && !Main.dedServ) {
if (Main.gamePaused || Main.gameInactive) {
pauseUpdate = true;
}
else {
prevCount++;
pauseUpdate = false;
}
if ((prevCount % Cooldown == 0) && !pauseUpdate) {
ClientUpdatePlayer();
ClientForceUpdate();
}
}
}
public override void Unload() {
Main.OnTick -= ClientUpdate;
Client?.Dispose();
Instance = null;
config = null;
}
public override object Call(params object[] args) {
if (!Main.dedServ) {
return DRPX.Call(args);
}
return "Can't call on server";
}
/// <summary>
/// update the party info
/// </summary>
internal void UpdateLobbyInfo() {
if (Main.LobbyId != 0UL) {
//string sId = SteamUser.GetSteamID().ToString();
ClientSetParty(null, Main.LocalPlayer.name, Main.ActivePlayersCount);
}
}
/// <summary>
/// method for update the status, checking from item to biome/boss/events
/// </summary>
internal void ClientUpdatePlayer() {
if (Main.LocalPlayer != null) {
(string itemKey, string itemText) = GetItemStat();
(string bigKey, string bigText, string selectedClient) = DRPX.GetBoss();
string state;
if (!Main.LocalPlayer.GetModPlayer<ClientPlayer>().dead) {
state = $"{(config.showHealth ? $"HP: {Main.LocalPlayer.statLife} " : "")}";
state += $"{(config.showMana ? $"MP: {Main.LocalPlayer.statMana} " : "")}";
state += $"{(config.showDefense ? $"DEF: {Main.LocalPlayer.statDefense}" : "")}";
if (string.IsNullOrWhiteSpace(state)) {
state = null;
}
}
else {
state = (config.showHealth || config.showMana || config.showDefense) ? string.Format("Dead") : null;
}
ClientSetStatus(state, bigText, bigKey, worldStaticInfo, itemKey, itemText);
UpdateLobbyInfo();
Instance.ChangeDiscordClient(selectedClient);
if (Main.LocalPlayer.GetModPlayer<ClientPlayer>().dead)
ClientForceUpdate();
}
}
/// <summary>
/// Get the player's item stat
/// </summary>
/// <returns>key and text for small images</returns>
internal (string, string) GetItemStat() {
int atk = -1;
string key = null;
string text = null;
string atkType = "";
Item item = Main.LocalPlayer?.HeldItem;
if (item != null) {
text = item.Name;
if (item.melee) {
atk = (int) Math.Ceiling(item.damage * Main.LocalPlayer.meleeDamage);
atkType = "Melee";
}
else if (item.ranged) {
atk = (int) Math.Ceiling(item.damage * Main.LocalPlayer.rangedDamage);
atkType = "Range";
}
else if (item.magic) {
atk = (int) Math.Ceiling(item.damage * Main.LocalPlayer.magicDamage);
atkType = "Magic";
}
else if (item.thrown) {
atk = (int) Math.Ceiling(item.damage * Main.LocalPlayer.thrownDamage);
atkType = "Throw";
}
else if (item.summon) {
atk = (int) Math.Ceiling(item.damage * Main.LocalPlayer.minionDamage);
atkType = "Summon";
}
}
if (atk >= 0) {
key = "atk_" + atkType.ToLower();
text += (config.showDamage ? $" ({atk} Damage)" : "");
}
return (key, text);
}
}
}