9#include <engine2/ivengineserver2.h>
10#include <engine2/networkgameserverbase.h>
11#include <engine2/serversideclient.h>
12#include <networkbasetypes.pb.h>
13#include <networksystem/inetworkmessages.h>
14#include <playerslot.h>
16#include <tier0/utlstring.h>
30 : _engine(engine.
Engine), _bindings(bindings)
39 if (
Status hooked = Install(); !hooked)
41 Log::Warn(
"Addons: {} is not sent to clients: {}", addonId, hooked.error().Detail);
44 return _queue.Add(addonId);
49 _queue.Remove(addonId);
50 RemoveHooksIfUnused();
55 const bool nothingToCheck = _queue.Empty() || !
IsValidSlot(slot) || !_engine;
60 return !_queue.HasMissing(
static_cast<int64_t
>(_engine->GetClientXUID(CPlayerSlot(slot))));
81 for (
const Kick& kick : std::exchange(_kicks, {}))
84 if (
static_cast<int64_t
>(_engine->GetClientXUID(CPlayerSlot(kick.Slot))) == kick.SteamId)
86 _engine->DisconnectClient(CPlayerSlot(kick.Slot), NETWORK_DISCONNECT_TIMEDOUT,
87 "Required workshop addon download was declined");
92Status WorkshopDownloads::Install()
100 if (!_engine || !_engine->IsDedicatedServer())
102 return std::unexpected(
Error::Unsupported(
"addon downloads need a dedicated server"));
104 const auto onJoinMessage = [
this](CServerSideClientBase& client,
const CNetMessage* message, NetChannelBufType_t) {
105 OnJoinMessage(message, client);
107 auto join =
HookTable(
"Workshop addon download", &CServerSideClientBase::SendNetMessage, _bindings.ClientTable,
114 const auto beforeReply = [
this](CNetworkGameServerBase& server, CServerSideClient* client) {
115 AddToReply(server, *client);
117 const auto afterReply = [
this](CNetworkGameServerBase& server, CServerSideClient*) { RestoreReply(server); };
118 auto reply =
HookFunction(
"Workshop addon mount", _bindings.ReplyConnection, beforeReply, afterReply);
124 _joinMessageHook = std::move(*join);
125 _connectionReplyHook = std::move(*reply);
129void WorkshopDownloads::RemoveHooksIfUnused()
137 _joinMessageHook.Reset();
138 _connectionReplyHook.Reset();
139 _addedToReply.clear();
140 _queue.ClearProgress();
144static CUtlString*
AddonList(CNetworkGameServerBase& server)
146 CUtlString* list = &server.m_szAddonName;
148 const char* stored = list->Get();
149 const char* reported = server.GetAddonName();
150 const bool same = stored == reported || (*stored ==
'\0' && *reported ==
'\0');
153 Log::Error(
"Addons: s2sdk's CNetworkGameServerBase::m_szAddonName is stale; not mounting addons.");
159void WorkshopDownloads::AddToReply(CNetworkGameServerBase& server,
const CServerSideClientBase& client)
161 const auto steamId =
static_cast<int64_t
>(client.GetClientSteamID().ConvertToUint64());
167 const std::vector<uint64_t> mountList = _queue.ClientMountList(steamId);
168 if (mountList.empty())
179 std::string field = list->Get();
181 if (_addedToReply.empty())
186 list->Set(field.c_str());
187 Log::Info(
"Addons: telling {} to mount {}.", steamId, field);
190void WorkshopDownloads::RestoreReply(CNetworkGameServerBase& server)
192 if (_addedToReply.empty())
201 _addedToReply.clear();
204 std::string field = list->Get();
206 list->Set(field.c_str());
207 _addedToReply.clear();
210void WorkshopDownloads::OnJoinMessage(
const CNetMessage* message,
const CServerSideClientBase& client)
212 NetMessageInfo_t* info = message ? message->GetNetMessage() :
nullptr;
213 if (!info || info->GetMessageId() != net_SignonState)
218 const auto steamId =
static_cast<int64_t
>(client.GetClientSteamID().ConvertToUint64());
225 auto* joinMessage =
const_cast<CNetMessage*
>(message)->ToPB<CNETMsg_SignonState>();
226 const bool reconnect = joinMessage->signon_state() == SIGNONSTATE_CHANGELEVEL;
227 const AddonDecision decision = _queue.DecideJoinMessage(steamId, reconnect, joinMessage->addons(),
230 switch (decision.Action)
235 Log::Info(
"Addons: a reconnect message named {} addons; sending {} and holding the rest.",
236 decision.Remaining + 1, decision.Id);
239 joinMessage->set_addons(std::to_string(decision.Id));
242 Log::Warn(
"Addons: {} did not take addon {} in {} attempts; dropping the client.", steamId, decision.Id,
244 if (
const int slot = client.GetPlayerSlot().Get();
IsValidSlot(slot))
246 _kicks.push_back({.Slot = slot, .SteamId = steamId});
250 joinMessage->set_addons(std::to_string(decision.Id));
251 joinMessage->set_signon_state(SIGNONSTATE_CHANGELEVEL);
252 Log::Info(
"Addons: sending addon {} to {} ({} left after it).", decision.Id, steamId, decision.Remaining);
static bool IsValid(int64_t steamId64)
static double MonotonicSeconds()
void OnClientDisconnected(int64_t steamId)
WorkshopDownloads(const Bindings &bindings, const EngineInterfaces &engine)
void Remove(uint64_t addonId) override
bool IsReady(int slot) override
bool Add(uint64_t addonId) override
void OnClientConnected(int64_t steamId)
void Error(std::format_string< Args... > fmt, Args &&... args)
void Info(std::format_string< Args... > fmt, Args &&... args)
void Warn(std::format_string< Args... > fmt, Args &&... args)
static constexpr double ForgetAfterSeconds
static CUtlString * AddonList(CNetworkGameServerBase &server)
Result< Subscription > HookFunction(std::string_view name, const Fn< Ret(Object *, Args...)> &function, Before &&before, After &&after=nullptr)
Hook a signature-bound function at its entry.
Result< Subscription > HookTable(std::string_view name, Ret(Object::*method)(Args...), const ClassTable &table, Before &&before, After &&after=nullptr)
Hook an SDK-declared virtual on every object sharing a gamedata-found class table.
static constexpr double DownloadTimeoutSeconds
std::expected< void, Error > Status
@ Engine
The engine or SDK refused, returned nothing, or is not where it was expected.
std::vector< uint64_t > AppendToAddonList(std::string &field, const std::vector< uint64_t > &ids)
constexpr bool IsValidSlot(int slot)
void RemoveFromAddonList(std::string &field, const std::vector< uint64_t > &ids)
static constexpr int MaxDownloadAttempts
@ TrimToFirst
the message names several addons; keep only AddonDecision::Id
@ Kick
the client refused AddonDecision::Id too often
@ Unchanged
send the message as it is
@ Send
point the client at AddonDecision::Id and wait for its reconnect
@ KeepMounted
a map change names no addon; name AddonDecision::Id so the client keeps it
static Error Unsupported(std::string detail)