2021-12-05 17:54:36 +00:00
|
|
|
#include "InstanceManager.h"
|
|
|
|
#include <string>
|
|
|
|
#include "Game.h"
|
|
|
|
#include "dServer.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "dConfig.h"
|
|
|
|
#include "CDClientDatabase.h"
|
|
|
|
#include "CDClientManager.h"
|
|
|
|
#include "CDZoneTableTable.h"
|
|
|
|
#include "MasterPackets.h"
|
2023-09-21 01:06:28 +00:00
|
|
|
#include "BitStreamUtils.h"
|
2023-05-03 21:38:32 +00:00
|
|
|
#include "eConnectionType.h"
|
|
|
|
#include "eMasterMessageType.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-12-30 07:00:43 +00:00
|
|
|
#include "Start.h"
|
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
InstanceManager::InstanceManager(Logger* logger, const std::string& externalIP) {
|
2021-12-05 17:54:36 +00:00
|
|
|
mLogger = logger;
|
|
|
|
mExternalIP = externalIP;
|
2024-01-05 00:21:03 +00:00
|
|
|
GeneralUtils::TryParse(Game::config->GetValue("world_port_start"), m_LastPort);
|
2021-12-05 17:54:36 +00:00
|
|
|
m_LastInstanceID = LWOINSTANCEID_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstanceManager::~InstanceManager() {
|
|
|
|
for (Instance* i : m_Instances) {
|
|
|
|
delete i;
|
|
|
|
i = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Instance* InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneID) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Searching for an instance for mapID %i/%i", mapID, cloneID);
|
2021-12-05 17:54:36 +00:00
|
|
|
Instance* instance = FindInstance(mapID, isFriendTransfer, cloneID);
|
|
|
|
if (instance) return instance;
|
|
|
|
|
2022-12-16 04:39:29 +00:00
|
|
|
// If we are shutting down, return a nullptr so a new instance is not created.
|
|
|
|
if (m_IsShuttingDown) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Tried to create a new instance map/instance/clone %i/%i/%i, but Master is shutting down.",
|
2022-12-16 04:39:29 +00:00
|
|
|
mapID,
|
|
|
|
m_LastInstanceID + 1,
|
|
|
|
cloneID);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
//TODO: Update this so that the IP is read from a configuration file instead
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
int softCap = 8;
|
|
|
|
int maxPlayers = 12;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mapID == 0) {
|
|
|
|
softCap = 999;
|
|
|
|
maxPlayers = softCap;
|
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
softCap = GetSoftCap(mapID);
|
|
|
|
maxPlayers = GetHardCap(mapID);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
uint32_t port = GetFreePort();
|
|
|
|
instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, softCap, maxPlayers);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
//Start the actual process:
|
2023-12-30 07:00:43 +00:00
|
|
|
StartWorldServer(mapID, port, m_LastInstanceID, maxPlayers, cloneID);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Instances.push_back(instance);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (instance) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Created new instance: %i/%i/%i with min/max %i/%i", mapID, m_LastInstanceID, cloneID, softCap, maxPlayers);
|
2022-07-28 13:39:57 +00:00
|
|
|
return instance;
|
2023-10-21 23:31:55 +00:00
|
|
|
} else LOG("Failed to create a new instance!");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return nullptr;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InstanceManager::IsPortInUse(uint32_t port) {
|
|
|
|
for (Instance* i : m_Instances) {
|
|
|
|
if (i && i->GetPort() == port) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-14 03:05:33 +00:00
|
|
|
uint32_t InstanceManager::GetFreePort() {
|
2022-07-28 13:39:57 +00:00
|
|
|
uint32_t port = m_LastPort;
|
|
|
|
std::vector<uint32_t> usedPorts;
|
|
|
|
for (Instance* i : m_Instances) {
|
|
|
|
usedPorts.push_back(i->GetPort());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(usedPorts.begin(), usedPorts.end());
|
|
|
|
|
|
|
|
int portIdx = 0;
|
|
|
|
while (portIdx < usedPorts.size() && port == usedPorts[portIdx]) {
|
|
|
|
//increment by 3 since each instance uses 3 ports (instance, world-server, world-chat)
|
|
|
|
port += 3;
|
|
|
|
portIdx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return port;
|
2021-12-14 03:05:33 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
void InstanceManager::AddPlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID) {
|
|
|
|
Instance* inst = FindInstance(mapID, instanceID);
|
|
|
|
if (inst) {
|
|
|
|
Player player;
|
|
|
|
player.addr = systemAddr;
|
|
|
|
player.id = 0; //TODO: Update this to include the LWOOBJID of the player's character.
|
|
|
|
inst->AddPlayer(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceManager::RemovePlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID) {
|
|
|
|
Instance* inst = FindInstance(mapID, instanceID);
|
|
|
|
if (inst) {
|
|
|
|
Player player;
|
|
|
|
player.addr = systemAddr;
|
|
|
|
player.id = 0; //TODO: Update this to include the LWOOBJID of the player's character.
|
|
|
|
inst->RemovePlayer(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::vector<Instance*> InstanceManager::GetInstances() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return m_Instances;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceManager::AddInstance(Instance* instance) {
|
|
|
|
if (instance == nullptr) return;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
m_Instances.push_back(instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceManager::RemoveInstance(Instance* instance) {
|
2022-07-28 13:39:57 +00:00
|
|
|
for (uint32_t i = 0; i < m_Instances.size(); ++i) {
|
|
|
|
if (m_Instances[i] == instance) {
|
2021-12-05 17:54:36 +00:00
|
|
|
instance->SetShutdownComplete(true);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2024-01-02 03:50:00 +00:00
|
|
|
if (!Game::ShouldShutdown()) RedirectPendingRequests(instance);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
delete m_Instances[i];
|
|
|
|
|
|
|
|
m_Instances.erase(m_Instances.begin() + i);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void InstanceManager::ReadyInstance(Instance* instance) {
|
2021-12-05 17:54:36 +00:00
|
|
|
instance->SetIsReady(true);
|
|
|
|
|
|
|
|
auto& pending = instance->GetPendingRequests();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& request : pending) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& zoneId = instance->GetZoneID();
|
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Responding to pending request %llu -> %i (%i)", request, zoneId.GetMapID(), zoneId.GetCloneID());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
MasterPackets::SendZoneTransferResponse(
|
|
|
|
Game::server,
|
|
|
|
request.sysAddr,
|
|
|
|
request.id,
|
|
|
|
request.mythranShift,
|
|
|
|
zoneId.GetMapID(),
|
|
|
|
zoneId.GetInstanceID(),
|
|
|
|
zoneId.GetCloneID(),
|
|
|
|
instance->GetIP(),
|
|
|
|
instance->GetPort()
|
|
|
|
);
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
pending.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void InstanceManager::RequestAffirmation(Instance* instance, const PendingInstanceRequest& request) {
|
2021-12-05 17:54:36 +00:00
|
|
|
instance->GetPendingAffirmations().push_back(request);
|
|
|
|
|
|
|
|
CBITSTREAM;
|
|
|
|
|
2023-09-21 01:06:28 +00:00
|
|
|
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::AFFIRM_TRANSFER_REQUEST);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
bitStream.Write(request.id);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::server->Send(&bitStream, instance->GetSysAddr(), false);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Sent affirmation request %llu to %i/%i", request.id,
|
2021-12-05 17:54:36 +00:00
|
|
|
static_cast<int>(instance->GetZoneID().GetMapID()),
|
|
|
|
static_cast<int>(instance->GetZoneID().GetCloneID())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void InstanceManager::AffirmTransfer(Instance* instance, const uint64_t transferID) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto& pending = instance->GetPendingAffirmations();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto i = 0u; i < pending.size(); ++i) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& request = pending[i];
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (request.id != transferID) continue;
|
|
|
|
|
|
|
|
const auto& zoneId = instance->GetZoneID();
|
|
|
|
|
|
|
|
MasterPackets::SendZoneTransferResponse(
|
|
|
|
Game::server,
|
|
|
|
request.sysAddr,
|
|
|
|
request.id,
|
|
|
|
request.mythranShift,
|
|
|
|
zoneId.GetMapID(),
|
|
|
|
zoneId.GetInstanceID(),
|
|
|
|
zoneId.GetCloneID(),
|
|
|
|
instance->GetIP(),
|
|
|
|
instance->GetPort()
|
|
|
|
);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
pending.erase(pending.begin() + i);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void InstanceManager::RedirectPendingRequests(Instance* instance) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& zoneId = instance->GetZoneID();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& request : instance->GetPendingAffirmations()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* in = Game::im->GetInstance(zoneId.GetMapID(), false, zoneId.GetCloneID());
|
|
|
|
|
2022-12-16 04:39:29 +00:00
|
|
|
if (in && !in->GetIsReady()) // Instance not ready, make a pending request
|
2021-12-05 17:54:36 +00:00
|
|
|
{
|
|
|
|
in->GetPendingRequests().push_back(request);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::im->RequestAffirmation(in, request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Instance* InstanceManager::GetInstanceBySysAddr(SystemAddress& sysAddr) {
|
|
|
|
for (uint32_t i = 0; i < m_Instances.size(); ++i) {
|
|
|
|
if (m_Instances[i] && m_Instances[i]->GetSysAddr() == sysAddr) {
|
|
|
|
return m_Instances[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstanceManager::IsInstanceFull(Instance* instance, bool isFriendTransfer) {
|
2022-07-25 02:26:51 +00:00
|
|
|
if (!isFriendTransfer && instance->GetSoftCap() > instance->GetCurrentClientCount())
|
2021-12-05 17:54:36 +00:00
|
|
|
return false;
|
2022-07-25 02:26:51 +00:00
|
|
|
else if (isFriendTransfer && instance->GetHardCap() > instance->GetCurrentClientCount())
|
2021-12-05 17:54:36 +00:00
|
|
|
return false;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Instance* InstanceManager::FindInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneId) {
|
2021-12-05 17:54:36 +00:00
|
|
|
for (Instance* i : m_Instances) {
|
2022-07-18 23:08:33 +00:00
|
|
|
if (i && i->GetMapID() == mapID && i->GetCloneID() == cloneId && !IsInstanceFull(i, isFriendTransfer) && !i->GetIsPrivate() && !i->GetShutdownComplete() && !i->GetIsShuttingDown()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Instance* InstanceManager::FindInstance(LWOMAPID mapID, LWOINSTANCEID instanceID) {
|
2021-12-05 17:54:36 +00:00
|
|
|
for (Instance* i : m_Instances) {
|
2022-07-28 13:39:57 +00:00
|
|
|
if (i && i->GetMapID() == mapID && i->GetInstanceID() == instanceID && !i->GetIsPrivate() && !i->GetShutdownComplete() && !i->GetIsShuttingDown()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID cloneID, const std::string& password) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* instance = FindPrivateInstance(password);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (instance != nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2022-12-16 04:39:29 +00:00
|
|
|
if (m_IsShuttingDown) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Tried to create a new private instance map/instance/clone %i/%i/%i, but Master is shutting down.",
|
2022-12-16 04:39:29 +00:00
|
|
|
mapID,
|
|
|
|
m_LastInstanceID + 1,
|
|
|
|
cloneID);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
int maxPlayers = 999;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-14 03:05:33 +00:00
|
|
|
uint32_t port = GetFreePort();
|
|
|
|
instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, maxPlayers, maxPlayers, true, password);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
//Start the actual process:
|
2023-12-30 07:00:43 +00:00
|
|
|
StartWorldServer(mapID, port, m_LastInstanceID, maxPlayers, cloneID);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
m_Instances.push_back(instance);
|
|
|
|
|
|
|
|
if (instance) return instance;
|
2023-10-21 23:31:55 +00:00
|
|
|
else LOG("Failed to create a new instance!");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Instance* InstanceManager::FindPrivateInstance(const std::string& password) {
|
|
|
|
for (auto* instance : m_Instances) {
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!instance) continue;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!instance->GetIsPrivate()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Password: %s == %s => %d", password.c_str(), instance->GetPassword().c_str(), password == instance->GetPassword());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (instance->GetPassword() == password) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int InstanceManager::GetSoftCap(LWOMAPID mapID) {
|
2024-02-09 05:40:43 +00:00
|
|
|
CDZoneTableTable* zoneTable = CDClientManager::GetTable<CDZoneTableTable>();
|
2021-12-05 17:54:36 +00:00
|
|
|
if (zoneTable) {
|
|
|
|
const CDZoneTable* zone = zoneTable->Query(mapID);
|
|
|
|
|
|
|
|
if (zone != nullptr) {
|
|
|
|
return zone->population_soft_cap;
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
int InstanceManager::GetHardCap(LWOMAPID mapID) {
|
2024-02-09 05:40:43 +00:00
|
|
|
CDZoneTableTable* zoneTable = CDClientManager::GetTable<CDZoneTableTable>();
|
2021-12-05 17:54:36 +00:00
|
|
|
if (zoneTable) {
|
|
|
|
const CDZoneTable* zone = zoneTable->Query(mapID);
|
|
|
|
|
|
|
|
if (zone != nullptr) {
|
|
|
|
return zone->population_hard_cap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Instance::SetShutdownComplete(const bool value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_Shutdown = value;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool Instance::GetShutdownComplete() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return m_Shutdown;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Instance::Shutdown() {
|
2021-12-05 17:54:36 +00:00
|
|
|
CBITSTREAM;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2023-09-21 01:06:28 +00:00
|
|
|
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::SHUTDOWN);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
Game::server->Send(&bitStream, this->m_SysAddr, false);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Triggered world shutdown for zone/clone/instance %i/%i/%i", GetMapID(), GetCloneID(), GetInstanceID());
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|