Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
tweak(server): add unified OneSync variables
This is to reduce confusion for new installs and make 'Infinity/Beyond'
modes considered default.

Mapping of new to old:
- `onesync on`: onesync_enabled true + onesync_enableInfinity true
- `onesync legacy`: onesync_enabled true + onesync_enableInfinity false
- `onesync off`: onesync_enabled false
- `onesync_population true` (default): onesync_enableBeyond true (if on)
- `onesync_population false`: onesync_enableBeyond false

A future commit will also enable `onesync_population` for legacy OneSync.
  • Loading branch information
blattersturm committed Jul 25, 2020
1 parent e89d675 commit 4572da2
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 21 deletions.
62 changes: 62 additions & 0 deletions code/components/citizen-server-impl/include/OneSyncVars.h
@@ -0,0 +1,62 @@
#pragma once

#include <console/Console.Commands.h>

namespace fx
{
enum class OneSyncState
{
Off,
Legacy,
On
};
}

template<>
struct ConsoleArgumentType<fx::OneSyncState>
{
static std::string Unparse(const fx::OneSyncState& input)
{
switch (input)
{
case fx::OneSyncState::Off:
return "off";
case fx::OneSyncState::On:
return "on";
case fx::OneSyncState::Legacy:
return "legacy";
default:
return "unk";
}
}

static bool Parse(const std::string& input, fx::OneSyncState* out)
{
if (_stricmp(input.c_str(), "on") == 0 || _stricmp(input.c_str(), "true") == 0)
{
*out = fx::OneSyncState::On;
return true;
}
else if (_stricmp(input.c_str(), "legacy") == 0)
{
*out = fx::OneSyncState::Legacy;
return true;
}
else if (_stricmp(input.c_str(), "off") == 0 || _stricmp(input.c_str(), "false") == 0)
{
*out = fx::OneSyncState::Off;
return true;
}

return false;
}
};

template<>
struct ConsoleArgumentName<fx::OneSyncState>
{
inline static const char* Get()
{
return "fx::OneSyncState";
}
};
8 changes: 4 additions & 4 deletions code/components/citizen-server-impl/src/ClientRegistry.cpp
Expand Up @@ -11,10 +11,10 @@
#include <ResourceManagerImpl.h>
#include <ResourceEventComponent.h>

extern std::shared_ptr<ConVar<bool>> g_oneSyncVar;

namespace fx
{
extern bool IsOneSync();

ClientRegistry::ClientRegistry()
: m_hostNetId(-1), m_curNetId(1), m_instance(nullptr)
{
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace fx

m_clientsByPeer[client->GetPeer()] = weakClient;

if (!g_oneSyncVar->GetValue())
if (!IsOneSync())
{
return;
}
Expand Down Expand Up @@ -168,7 +168,7 @@ namespace fx
}
else
{
if (!g_oneSyncVar->GetValue())
if (!IsOneSync())
{
m_hostNetId = client->GetNetId();
}
Expand Down
14 changes: 7 additions & 7 deletions code/components/citizen-server-impl/src/GameServer.cpp
Expand Up @@ -43,12 +43,12 @@

static fx::GameServer* g_gameServer;

extern std::shared_ptr<ConVar<bool>> g_oneSyncVar;

extern fwEvent<> OnEnetReceive;

namespace fx
{
extern bool IsOneSync();

GameServer::GameServer()
: m_residualTime(0), m_serverTime(msec().count()), m_nextHeartbeatTime(0), m_hasSettled(false)
{
Expand Down Expand Up @@ -683,7 +683,7 @@ namespace fx

client->SetPeer(peerId, peer->GetAddress());

if (g_oneSyncVar->GetValue())
if (IsOneSync())
{
if (client->GetSlotId() == -1)
{
Expand Down Expand Up @@ -718,12 +718,12 @@ namespace fx
client->GetNetId(),
(host) ? host->GetNetId() : -1,
(host) ? host->GetNetBase() : -1,
(g_oneSyncVar->GetValue())
(IsOneSync())
? ((fx::IsBigMode())
? 128
: client->GetSlotId())
: -1,
(g_oneSyncVar->GetValue()) ? msec().count() : -1);
(IsOneSync()) ? msec().count() : -1);

outMsg.Write(outStr.c_str(), outStr.size());

Expand All @@ -738,7 +738,7 @@ namespace fx
m_clientRegistry->HandleConnectedClient(client, oldNetID);
});

if (g_oneSyncVar->GetValue())
if (IsOneSync())
{
m_instance->GetComponent<fx::ServerGameState>()->SendObjectIds(client, fx::IsBigMode() ? 4 : 64);
}
Expand Down Expand Up @@ -1374,7 +1374,7 @@ namespace fx
{
inline static void Handle(ServerInstanceBase* instance, const std::shared_ptr<fx::Client>& client, net::Buffer& packet)
{
if (g_oneSyncVar->GetValue())
if (IsOneSync())
{
return;
}
Expand Down
7 changes: 4 additions & 3 deletions code/components/citizen-server-impl/src/InitConnectMethod.cpp
Expand Up @@ -57,6 +57,7 @@ void RegisterServerIdentityProvider(ServerIdentityProviderBase* provider)
g_providersByType.insert({ provider->GetIdentifierPrefix(), provider });
}

extern bool IsOneSync();
extern bool IsLengthHack();
}

Expand Down Expand Up @@ -379,7 +380,7 @@ static InitFunction initFunction([]()
return;
}

if (g_oneSyncVar->GetValue())
if (fx::IsOneSync())
{
if (protocol < 9)
{
Expand Down Expand Up @@ -468,8 +469,8 @@ static InitFunction initFunction([]()
data["protocol"] = 5;
data["bitVersion"] = 0x202007151853;
data["sH"] = shVar->GetValue();
data["enhancedHostSupport"] = ehVar->GetValue() && !g_oneSyncVar->GetValue();
data["onesync"] = g_oneSyncVar->GetValue();
data["enhancedHostSupport"] = ehVar->GetValue() && !fx::IsOneSync();
data["onesync"] = fx::IsOneSync();
data["onesync_big"] = fx::IsBigMode();
data["onesync_lh"] = fx::IsLengthHack();
data["token"] = token;
Expand Down
52 changes: 45 additions & 7 deletions code/components/citizen-server-impl/src/state/ServerGameState.cpp
Expand Up @@ -27,6 +27,7 @@

#include <boost/range/adaptors.hpp>

#include <OneSyncVars.h>
#include <DebugAlias.h>

static bool g_bigMode;
Expand Down Expand Up @@ -56,7 +57,7 @@ namespace rl
CPool<fx::ScriptGuid>* g_scriptHandlePool;
std::shared_mutex g_scriptHandlePoolMutex;

std::shared_ptr<ConVar<bool>> g_oneSyncVar;
std::shared_ptr<ConVar<bool>> g_oneSyncEnabledVar;
std::shared_ptr<ConVar<bool>> g_oneSyncCulling;
std::shared_ptr<ConVar<bool>> g_oneSyncVehicleCulling;
std::shared_ptr<ConVar<bool>> g_oneSyncForceMigration;
Expand All @@ -65,6 +66,16 @@ std::shared_ptr<ConVar<std::string>> g_oneSyncLogVar;
std::shared_ptr<ConVar<bool>> g_oneSyncWorkaround763185;
std::shared_ptr<ConVar<bool>> g_oneSyncBigMode;
std::shared_ptr<ConVar<bool>> g_oneSyncLengthHack;
std::shared_ptr<ConVar<fx::OneSyncState>> g_oneSyncVar;
std::shared_ptr<ConVar<bool>> g_oneSyncPopulation;

namespace fx
{
bool IsOneSync()
{
return g_oneSyncEnabledVar->GetValue() || g_oneSyncVar->GetValue() != fx::OneSyncState::Off;
}
}

extern tbb::concurrent_unordered_map<uint32_t, fx::EntityCreationState> g_entityCreationList;

Expand Down Expand Up @@ -2003,7 +2014,7 @@ bool ServerGameState::MoveEntityToCandidate(const std::shared_ptr<sync::SyncEnti

void ServerGameState::HandleClientDrop(const std::shared_ptr<fx::Client>& client)
{
if (!g_oneSyncVar->GetValue())
if (!IsOneSync())
{
return;
}
Expand Down Expand Up @@ -2715,7 +2726,7 @@ static std::tuple<std::optional<net::Buffer>, uint32_t> UncompressClonePacket(co

void ServerGameState::ParseGameStatePacket(const std::shared_ptr<fx::Client>& client, const std::vector<uint8_t>& packetData)
{
if (!g_oneSyncVar->GetValue())
if (!IsOneSync())
{
return;
}
Expand Down Expand Up @@ -3589,20 +3600,47 @@ static InitFunction initFunction([]()

fx::ServerInstanceBase::OnServerCreate.Connect([](fx::ServerInstanceBase* instance)
{
g_oneSyncVar = instance->AddVariable<fx::OneSyncState>("onesync", ConVar_ReadOnly, fx::OneSyncState::Off);
g_oneSyncPopulation = instance->AddVariable<bool>("onesync_population", ConVar_ReadOnly, true);

// .. to infinity?
g_oneSyncBigMode = instance->AddVariable<bool>("onesync_enableInfinity", ConVar_None, false);
g_oneSyncBigMode = instance->AddVariable<bool>("onesync_enableInfinity", ConVar_ReadOnly, false);

g_bigMode = g_oneSyncBigMode->GetValue();

// or maybe, beyond?
g_oneSyncLengthHack = instance->AddVariable<bool>("onesync_enableBeyond", ConVar_None, false);
g_oneSyncLengthHack = instance->AddVariable<bool>("onesync_enableBeyond", ConVar_ReadOnly, false);

g_lengthHack = g_oneSyncLengthHack->GetValue();

if (g_oneSyncVar->GetValue() == fx::OneSyncState::On)
{
g_bigMode = true;
g_lengthHack = g_oneSyncPopulation->GetValue();

g_oneSyncBigMode->GetHelper()->SetRawValue(true);
g_oneSyncLengthHack->GetHelper()->SetRawValue(g_lengthHack);
}

instance->OnInitialConfiguration.Connect([]()
{
if (g_oneSyncEnabledVar->GetValue() && g_oneSyncVar->GetValue() == fx::OneSyncState::Off)
{
g_oneSyncVar->GetHelper()->SetRawValue(g_bigMode ? fx::OneSyncState::On : fx::OneSyncState::Legacy);

console::PrintWarning("server", "`onesync_enabled` is deprecated. Please use `onesync %s` instead.\n",
g_bigMode ? "on" : "legacy");
}
else if (!g_oneSyncEnabledVar->GetValue() && g_oneSyncVar->GetValue() != fx::OneSyncState::Off)
{
g_oneSyncEnabledVar->GetHelper()->SetRawValue(true);
}
});
}, INT32_MIN);

fx::ServerInstanceBase::OnServerCreate.Connect([](fx::ServerInstanceBase* instance)
{
g_oneSyncVar = instance->AddVariable<bool>("onesync_enabled", ConVar_ServerInfo, false);
g_oneSyncEnabledVar = instance->AddVariable<bool>("onesync_enabled", ConVar_ServerInfo, false);
g_oneSyncCulling = instance->AddVariable<bool>("onesync_distanceCulling", ConVar_None, true);
g_oneSyncVehicleCulling = instance->AddVariable<bool>("onesync_distanceCullVehicles", ConVar_None, false);
g_oneSyncForceMigration = instance->AddVariable<bool>("onesync_forceMigration", ConVar_None, false);
Expand All @@ -3614,7 +3652,7 @@ static InitFunction initFunction([]()

instance->GetComponent<fx::GameServer>()->OnSyncTick.Connect([=]()
{
if (!g_oneSyncVar->GetValue())
if (!fx::IsOneSync())
{
return;
}
Expand Down

0 comments on commit 4572da2

Please sign in to comment.