Skip to content

Commit e1b4c17

Browse files
authored
Merge pull request #281 from spacevx/feat/voice-update-player
Feat/voice update player
2 parents 4004a64 + d4d9426 commit e1b4c17

8 files changed

Lines changed: 617 additions & 2 deletions

File tree

‎Game.Server.Tests/VoiceGameMgrTests.cs‎

Lines changed: 423 additions & 0 deletions
Large diffs are not rendered by default.

‎Game.Server/GameInterface.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,8 @@ public TimeUnit_t ProcessUsercmds(Edict player, bf_read buf, int numCmds, int to
912912

913913
public void GMOD_SentClientStringTables(int userID) => Game.Server.GarrysMod.GModDataPack.DataPack().SendFileRequestRequest(userID);
914914

915+
public void GMOD_OnReceivedVoicePacket(Edict? player) => GetVoiceGameMgr().OnReceivedVoicePacket(BaseEntity.Instance(player));
916+
915917
public void GMOD_ReceiveClientMessage(int userID, Edict player, bf_read msg, int bits) {
916918
int type = msg.ReadByte();
917919
if (bits <= 7 || type > 5) {

‎Game.Server/Player.cs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,39 @@ public virtual void ForceDropOfCarriedPhysObjects(BaseEntity? ground) { }
23702370

23712371
internal int GetUserID() => engine.GetPlayerUserId(Edict());
23722372

2373+
#if GMOD_DLL
2374+
static readonly ConVar sv_maxvoicepacketsperframe = new("sv_maxvoicepacketsperframe", "48", 0, "How many voice packets the server will accept from a single client per frame/tick, before the client is kicked. 0 to disable. Higher values will make the server more vulnerable to DoS attacks.");
2375+
2376+
public double LastVoicePacketTime;
2377+
ushort VoicePacketsThisFrame;
2378+
public bool Kicked;
2379+
2380+
public void OnVoicePacketReceived() {
2381+
double curTime = gpGlobals.CurTime;
2382+
ushort packets;
2383+
if (LastVoicePacketTime == curTime)
2384+
packets = (ushort)(VoicePacketsThisFrame + 1);
2385+
else {
2386+
packets = 1;
2387+
VoicePacketsThisFrame = 0;
2388+
}
2389+
2390+
LastVoicePacketTime = curTime;
2391+
VoicePacketsThisFrame = packets;
2392+
2393+
int maxPackets = sv_maxvoicepacketsperframe.GetInt();
2394+
if (maxPackets <= 0)
2395+
return;
2396+
2397+
if ((VoicePacketsThisFrame > 65000 || maxPackets < VoicePacketsThisFrame) && !Kicked) {
2398+
Span<char> cmd = stackalloc char[600];
2399+
sprintf(cmd, "kickid %d \"Too many voice packets (%i) in a single frame!\"\n").D(engine.GetPlayerUserId(Edict())).I(VoicePacketsThisFrame);
2400+
engine.ServerCommand(cmd.SliceNullTerminatedString());
2401+
Kicked = true;
2402+
}
2403+
}
2404+
#endif
2405+
23732406
public bool GetSteamID(out Steamworks.CSteamID id) {
23742407
Steamworks.CSteamID? steamID = engine.GetClientSteamID(Edict());
23752408
id = steamID ?? default;

‎Game.Server/VoiceGameMgr.cs‎

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public static class VoiceGameMgrGlobals
4242

4343
// Set game rules to allow all clients to talk to each other.
4444
// Muted players still can't talk to each other.
45+
#if GMOD_DLL
46+
public static readonly ConVar sv_alltalk = new("sv_alltalk", "0", FCvar.Notify, "Players can hear all other players, no team restrictions");
47+
#else
4548
public static readonly ConVar sv_alltalk = new("sv_alltalk", "0", FCvar.Notify | FCvar.Replicated, "Players can hear all other players, no team restrictions");
49+
#endif
4650

4751
public static readonly VoiceGameMgr g_VoiceGameMgr = new();
4852

@@ -67,9 +71,15 @@ public static void VoiceServerDebug(ReadOnlySpan<char> msg) {
6771
// VoiceGameMgr manages which clients can hear which other clients.
6872
public class VoiceGameMgr
6973
{
74+
#if GMOD_DLL
75+
double LastVoicePacketTime;
76+
#endif
7077
IVoiceGameMgrHelper? Helper;
7178
int MaxPlayers;
7279
double UpdateInterval; // How long since the last update.
80+
#if GMOD_DLL
81+
double FullUpdateInterval;
82+
#endif
7383
int ProximityDistance;
7484

7585
public VoiceGameMgr() {
@@ -94,17 +104,67 @@ public void SetHelper(IVoiceGameMgrHelper? helper) {
94104
// If gameplay mode is teamplay, then only players on the same team can hear each other.
95105
// Player masks are always applied.
96106
public void Update(double frametime) {
107+
#if GMOD_DLL
108+
UpdateInterval += frametime;
109+
FullUpdateInterval += frametime;
110+
if (UpdateInterval < UPDATE_INTERVAL)
111+
return;
112+
113+
if (gpGlobals.CurTime - LastVoicePacketTime > UPDATE_INTERVAL && UpdateInterval < 5.0)
114+
return;
115+
116+
if (UpdateInterval > 3.0 || FullUpdateInterval > 5.0) {
117+
FullUpdateInterval = 0;
118+
UpdatePlayers(true, null);
119+
}
120+
else
121+
UpdatePlayers(false, null);
122+
#else
97123
// Only update periodically.
98124
UpdateInterval += frametime;
99125
if (UpdateInterval < UPDATE_INTERVAL)
100126
return;
101127

102128
UpdateMasks();
129+
#endif
130+
}
131+
132+
#if GMOD_DLL
133+
public void OnReceivedVoicePacket(BaseEntity? entity) {
134+
if (entity == null || !entity.IsPlayer())
135+
return;
136+
137+
BasePlayer player = (BasePlayer)entity;
138+
bool startedTalking = gpGlobals.CurTime - player.LastVoicePacketTime > UPDATE_INTERVAL;
139+
player.OnVoicePacketReceived();
140+
LastVoicePacketTime = gpGlobals.CurTime;
141+
142+
if (startedTalking)
143+
UpdatePlayers(false, player);
144+
}
145+
146+
void UpdatePlayers(bool full, BasePlayer? talker) {
147+
UpdateInterval = 0;
148+
149+
for (int iClient = 0; iClient < MaxPlayers; iClient++) {
150+
BaseEntity? ent = Util.PlayerByIndex(iClient + 1);
151+
if (ent != null && ent.IsPlayer())
152+
UpdatePlayer((BasePlayer)ent, full, talker);
153+
}
103154
}
155+
#endif
104156

105157
// Called when a new client connects (unsquelches its entity for everyone).
106-
public void ClientConnected(Edict edict) {
107-
int index = edict.EdictIndex - 1;
158+
public void ClientConnected(Edict? edict) {
159+
#if GMOD_DLL
160+
if (edict == null)
161+
return;
162+
#endif
163+
int index = edict!.EdictIndex - 1;
164+
#if GMOD_DLL
165+
if ((uint)index >= VOICE_MAX_PLAYERS)
166+
return;
167+
#endif
108168

109169
// Clear out everything we use for deltas on this guy.
110170
g_bWantModEnable.Set(index, true);
@@ -164,6 +224,74 @@ public bool IsPlayerIgnoringPlayer(int talker, int listener) {
164224
return g_BanMasks[listener - 1].IsBitSet(talker - 1);
165225
}
166226

227+
#if GMOD_DLL
228+
void UpdatePlayer(BasePlayer player, bool full, BasePlayer? talker) {
229+
int playerIndex = player.EntIndex();
230+
int iClient = playerIndex - 1;
231+
232+
SingleUserRecipientFilter user = new(player);
233+
234+
// Request the state of their "VModEnable" cvar
235+
if (g_bWantModEnable.IsBitSet(iClient)) {
236+
UserMessageBegin(user, "RequestState");
237+
MessageEnd();
238+
// Only send it once
239+
g_bWantModEnable.Set(iClient, false);
240+
}
241+
242+
PlayerBitSet gameRulesMask = default;
243+
PlayerBitSet proximityMask = default;
244+
if (g_PlayerModEnable.IsBitSet(iClient)) {
245+
bool proximity = false;
246+
247+
// Build a mask of who they can hear based on the game rules
248+
for (int iOtherClient = 0; iOtherClient < MaxPlayers; iOtherClient++) {
249+
BaseEntity? otherEnt = Util.PlayerByIndex(iOtherClient + 1);
250+
if (otherEnt == null || !otherEnt.IsPlayer())
251+
continue;
252+
253+
BasePlayer other = (BasePlayer)otherEnt;
254+
bool canHear;
255+
if (full || other == talker || gpGlobals.CurTime - other.LastVoicePacketTime < 1.0)
256+
canHear = Helper!.CanPlayerHearPlayer(player, other, ref proximity);
257+
else {
258+
canHear = g_pVoiceServer.GetClientListening(playerIndex, iOtherClient + 1);
259+
proximity = g_pVoiceServer.GetClientProximity(playerIndex, iOtherClient + 1);
260+
}
261+
262+
if (canHear) {
263+
gameRulesMask.Set(iOtherClient, true);
264+
proximityMask.Set(iOtherClient, proximity);
265+
}
266+
}
267+
}
268+
269+
// If this is different from what the client has, send an update
270+
if (gameRulesMask != g_SentGameRulesMasks[iClient] ||
271+
g_BanMasks[iClient] != g_SentBanMasks[iClient]) {
272+
g_SentGameRulesMasks[iClient] = gameRulesMask;
273+
g_SentBanMasks[iClient] = g_BanMasks[iClient];
274+
275+
UserMessageBegin(user, "VoiceMask");
276+
int dw;
277+
for (dw = 0; dw < VOICE_MAX_PLAYERS_DW; dw++) {
278+
MessageWriteLong((int)gameRulesMask.GetWord(dw));
279+
MessageWriteLong((int)g_BanMasks[iClient].GetWord(dw));
280+
}
281+
MessageWriteByte(g_PlayerModEnable.IsBitSet(iClient) ? 1 : 0);
282+
MessageEnd();
283+
}
284+
285+
// Tell the engine.
286+
for (int iOtherClient = 0; iOtherClient < MaxPlayers; iOtherClient++) {
287+
bool canHear = gameRulesMask.IsBitSet(iOtherClient) && !g_BanMasks[iClient].IsBitSet(iOtherClient);
288+
g_pVoiceServer.SetClientListening(playerIndex, iOtherClient + 1, canHear);
289+
290+
if (canHear)
291+
g_pVoiceServer.SetClientProximity(playerIndex, iOtherClient + 1, proximityMask.IsBitSet(iOtherClient));
292+
}
293+
}
294+
#else
167295
// Force it to update the client masks.
168296
void UpdateMasks() {
169297
UpdateInterval = 0;
@@ -228,4 +356,5 @@ void UpdateMasks() {
228356
}
229357
}
230358
}
359+
#endif
231360
}

‎Source.Common/Server/IServerGameDLL.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public interface IServerGameClients
163163
void GMOD_ReceiveClientMessage(int userID, Edict player, bf_read msg, int bits);
164164
void GMOD_ClientConnected(int userID);
165165
void GMOD_SentClientStringTables(int userID);
166+
void GMOD_OnReceivedVoicePacket(Edict? player);
166167
}
167168

168169
public interface IServerGCLobby

‎Source.Common/Server/IVoiceServer.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ public interface IVoiceServer
1212
bool GetClientListening(int receiver, int sender);
1313
bool SetClientListening(int receiver, int sender, bool listen);
1414
bool SetClientProximity(int receiver, int sender, bool useProximity);
15+
#if GMOD_DLL
16+
bool GetClientProximity(int receiver, int sender);
17+
#endif
1518
}

‎Source.Engine/Server/GameClient.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ protected override bool ProcessMove(CLC_Move m) {
112112
}
113113

114114
protected override bool ProcessVoiceData(CLC_VoiceData msg) {
115+
#if GMOD_DLL
116+
if ((uint)(msg.Length - 9) >= 0x3FF8) {
117+
Disconnect($"Invalid voice packet length {msg.Length}.");
118+
return false;
119+
}
120+
121+
if (!IsActive())
122+
return false;
123+
124+
serverGameClients.GMOD_OnReceivedVoicePacket(Edict);
125+
#endif
126+
115127
byte[] voiceDataBuffer = new byte[4096];
116128
int bitsRead = (int)msg.DataIn.ReadBitsClamped(voiceDataBuffer, (uint)msg.Length);
117129

‎Source.Engine/Server/VoiceServerImpl.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ public bool SetClientProximity(int receiver, int sender, bool useProximity) {
4747

4848
return true;
4949
}
50+
51+
#if GMOD_DLL
52+
public bool GetClientProximity(int receiver, int sender) {
53+
--receiver;
54+
--sender;
55+
56+
if (receiver < 0 || receiver >= sv.GetClientCount() || sender < 0 || sender >= sv.GetClientCount())
57+
return false;
58+
59+
return sv.Client(sender).VoiceProximity.Get(receiver) != 0;
60+
}
61+
#endif
5062
}

0 commit comments

Comments
 (0)