-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDiscordRichPresence.cs
161 lines (131 loc) · 5.1 KB
/
DiscordRichPresence.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
using System.Linq;
using BonelabMultiplayerMockup.Utils;
using Discord;
using MelonLoader;
using Steamworks;
using Result = Discord.Result;
namespace BonelabMultiplayerMockup
{
public class DiscordRichPresence
{
private static global::Discord.Discord discord;
public static LobbyManager lobbyManager;
public static ActivityManager activityManager;
public static Activity activity;
public static Lobby currentLobby;
public static bool hasLobby;
public static void Init()
{
discord = new global::Discord.Discord(1026695411144081518, (ulong)CreateFlags.Default);
activityManager = discord.GetActivityManager();
lobbyManager = discord.GetLobbyManager();
DefaultRichPresence();
activityManager.OnActivityJoin += secret =>
lobbyManager.ConnectLobbyWithActivitySecret(secret, DiscordJoinLobby);
}
private static void DiscordJoinLobby(Result result, ref Lobby lobby)
{
if (hasLobby)
{
MelonLogger.Error("You are already in a lobby!");
return;
}
if (result != Result.Ok)
return;
currentLobby = lobby;
MelonLogger.Msg("Joined DISCORD lobby with id: " + lobby.Id);
lobbyManager.OnMemberConnect += UserConnectedEvent;
lobbyManager.OnMemberDisconnect += UserDisconnectEvent;
var users = lobbyManager.GetMemberUsers(lobby.Id);
activity.Party = new ActivityParty
{
Id = lobby.Id.ToString(),
Size = new PartySize { CurrentSize = users.Count(), MaxSize = (int)lobby.Capacity }
};
activity.Details = "This user is connected to a BLMP server!";
activity.State = "Killing with friends";
activity.Secrets = new ActivitySecrets
{
Join = lobbyManager.GetLobbyActivitySecret(lobby.Id)
};
activity.Instance = true;
SteamId steamId = ulong.Parse(lobbyManager.GetLobbyMetadataValue(lobby.Id, "steamLobbyId"));
MelonLogger.Msg("JOINING STEAM LOBBY.");
SteamMatchmaking.JoinLobbyAsync(steamId);
UpdateActivity();
}
private static void UserConnectedEvent(long lobbyId, long userId)
{
activity.Party.Size.CurrentSize = 1 + activity.Party.Size.CurrentSize;
UpdateActivity();
}
private static void UserDisconnectEvent(long lobbyId, long userId)
{
activity.Party.Size.CurrentSize -= 1;
UpdateActivity();
}
public static void Update()
{
discord.RunCallbacks();
}
public static void MakeDiscordLobby()
{
var lobbyTransaction = lobbyManager.GetLobbyCreateTransaction();
lobbyTransaction.SetCapacity(10);
lobbyTransaction.SetLocked(false);
lobbyTransaction.SetType(LobbyType.Private);
lobbyManager.CreateLobby(lobbyTransaction, onDiscordLobbyCreate);
}
private static void onDiscordLobbyCreate(Result result, ref Lobby lobby)
{
if (result != Result.Ok) return;
currentLobby = lobby;
HostRichPresence(lobby);
}
public static void HostRichPresence(Lobby lobby)
{
activity.Party = new ActivityParty
{
Id = lobby.Id.ToString(),
Size = new PartySize { CurrentSize = 1, MaxSize = 10 }
};
activity.Details = "This user is hosting a BLMP server!";
activity.State = "Killing with friends";
activity.Secrets = new ActivitySecrets
{
Join = lobbyManager.GetLobbyActivitySecret(lobby.Id)
};
LobbyTransaction transaction = lobbyManager.GetLobbyUpdateTransaction(lobby.Id);
transaction.SetMetadata("steamLobbyId", SteamIntegration.Instance.currentLobby.Id.ToString());
lobbyManager.UpdateLobby(lobby.Id, transaction, (result) =>
{
if (result == Result.Ok)
{
DebugLogger.Msg("Lobby metadata updated!");
}
});
hasLobby = true;
UpdateActivity();
}
public static void DefaultRichPresence()
{
activity = new Activity
{
State = "Playing alone",
Details = "Not connected to a server",
Instance = true,
Assets =
{
LargeImage = "blmp",
LargeText = "Steam Networking Build"
}
};
activity.Instance = false;
UpdateActivity();
}
public static void UpdateActivity()
{
activityManager.UpdateActivity(activity, result => { });
}
}
}