|
| 1 | +#include <StdInc.h> |
| 2 | + |
| 3 | +#include <CoreConsole.h> |
| 4 | + |
| 5 | +#include <ClientRegistry.h> |
| 6 | +#include <GameServer.h> |
| 7 | + |
| 8 | +#include <ServerInstanceBase.h> |
| 9 | + |
| 10 | +#include <msgpack.hpp> |
| 11 | + |
| 12 | +static InitFunction initFunction([]() |
| 13 | +{ |
| 14 | + fx::ServerInstanceBase::OnServerCreate.Connect([](fx::ServerInstanceBase* instance) |
| 15 | + { |
| 16 | + auto console = instance->GetComponent<console::Context>(); |
| 17 | + auto gameServer = instance->GetComponent<fx::GameServer>().GetRef(); |
| 18 | + auto clientRegistry = instance->GetComponent<fx::ClientRegistry>().GetRef(); |
| 19 | + |
| 20 | + auto sendVariableList = [](fx::Client* client, const std::map<std::string, std::string>& list) |
| 21 | + { |
| 22 | + msgpack::sbuffer buf; |
| 23 | + msgpack::packer<msgpack::sbuffer> packer(buf); |
| 24 | + |
| 25 | + packer.pack(list); |
| 26 | + |
| 27 | + // build the target event |
| 28 | + net::Buffer outBuffer; |
| 29 | + outBuffer.Write(HashRageString("msgConVars")); |
| 30 | + |
| 31 | + // payload |
| 32 | + outBuffer.Write(buf.data(), buf.size()); |
| 33 | + |
| 34 | + client->SendPacket(0, outBuffer, ENET_PACKET_FLAG_RELIABLE); |
| 35 | + }; |
| 36 | + |
| 37 | + clientRegistry->OnConnectedClient.Connect([console, sendVariableList](fx::Client* client) |
| 38 | + { |
| 39 | + std::map<std::string, std::string> variableList; |
| 40 | + |
| 41 | + console->GetVariableManager()->ForAllVariables([&variableList](const std::string& name, int flags, const std::shared_ptr<internal::ConsoleVariableEntryBase>& var) |
| 42 | + { |
| 43 | + variableList.emplace(name, var->GetValue()); |
| 44 | + }, ConVar_Replicated); |
| 45 | + |
| 46 | + sendVariableList(client, variableList); |
| 47 | + }); |
| 48 | + |
| 49 | + gameServer->OnTick.Connect([=]() |
| 50 | + { |
| 51 | + std::map<std::string, std::string> variableList; |
| 52 | + |
| 53 | + console->GetVariableManager()->ForAllVariables([console, &variableList](const std::string& name, int flags, const std::shared_ptr<internal::ConsoleVariableEntryBase>& var) |
| 54 | + { |
| 55 | + variableList.emplace(name, var->GetValue()); |
| 56 | + |
| 57 | + console->GetVariableManager()->RemoveEntryFlags(name, ConVar_Modified); |
| 58 | + }, ConVar_Replicated | ConVar_Modified); |
| 59 | + |
| 60 | + if (!variableList.empty()) |
| 61 | + { |
| 62 | + clientRegistry->ForAllClients([&variableList, sendVariableList](const std::shared_ptr<fx::Client>& client) |
| 63 | + { |
| 64 | + sendVariableList(client.get(), variableList); |
| 65 | + }); |
| 66 | + } |
| 67 | + }); |
| 68 | + }, 999999); |
| 69 | +}); |
0 commit comments