2021-12-05 17:54:36 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <ctime>
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include "MD5.h"
|
|
|
|
|
|
|
|
//DLU Includes:
|
|
|
|
#include "dCommonVars.h"
|
|
|
|
#include "dServer.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
#include "Database.h"
|
|
|
|
#include "dConfig.h"
|
|
|
|
#include "dpWorld.h"
|
|
|
|
#include "dZoneManager.h"
|
|
|
|
#include "Metrics.hpp"
|
|
|
|
#include "PerformanceManager.h"
|
|
|
|
#include "Diagnostics.h"
|
2022-11-27 11:59:59 +00:00
|
|
|
#include "BinaryPathFinder.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//RakNet includes:
|
|
|
|
#include "RakNetDefines.h"
|
|
|
|
#include "RakNetworkFactory.h"
|
|
|
|
#include "RakString.h"
|
|
|
|
|
|
|
|
//World includes:
|
|
|
|
#include <csignal>
|
|
|
|
|
|
|
|
#include "AuthPackets.h"
|
|
|
|
#include "PacketUtils.h"
|
|
|
|
#include "WorldPackets.h"
|
|
|
|
#include "UserManager.h"
|
|
|
|
#include "CDClientManager.h"
|
|
|
|
#include "CDClientDatabase.h"
|
|
|
|
#include "GeneralUtils.h"
|
|
|
|
#include "ObjectIDManager.h"
|
|
|
|
#include "ZoneInstanceManager.h"
|
|
|
|
#include "dChatFilter.h"
|
|
|
|
#include "ClientPackets.h"
|
|
|
|
#include "CharacterComponent.h"
|
|
|
|
|
|
|
|
#include "EntityManager.h"
|
2023-01-07 05:17:05 +00:00
|
|
|
#include "EntityInfo.h"
|
|
|
|
#include "User.h"
|
|
|
|
#include "Loot.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "Entity.h"
|
|
|
|
#include "Character.h"
|
|
|
|
#include "ChatPackets.h"
|
|
|
|
#include "GameMessageHandler.h"
|
|
|
|
#include "GameMessages.h"
|
|
|
|
#include "Mail.h"
|
|
|
|
#include "TeamManager.h"
|
|
|
|
#include "SkillComponent.h"
|
|
|
|
#include "DestroyableComponent.h"
|
|
|
|
#include "Game.h"
|
|
|
|
#include "MasterPackets.h"
|
|
|
|
#include "Player.h"
|
|
|
|
#include "PropertyManagementComponent.h"
|
2022-11-01 18:21:26 +00:00
|
|
|
#include "AssetManager.h"
|
2022-12-19 19:45:50 +00:00
|
|
|
#include "LevelProgressionComponent.h"
|
2022-11-28 00:48:46 +00:00
|
|
|
#include "eBlueprintSaveResponseType.h"
|
2023-05-13 22:22:00 +00:00
|
|
|
#include "Amf3.h"
|
2023-01-07 05:17:05 +00:00
|
|
|
#include "NiPoint3.h"
|
2023-02-19 12:29:14 +00:00
|
|
|
#include "eServerDisconnectIdentifiers.h"
|
2023-04-25 18:17:40 +00:00
|
|
|
#include "eObjectBits.h"
|
2023-05-03 21:38:32 +00:00
|
|
|
#include "eConnectionType.h"
|
|
|
|
#include "eServerMessageType.h"
|
|
|
|
#include "eChatInternalMessageType.h"
|
|
|
|
#include "eWorldMessageType.h"
|
|
|
|
#include "eMasterMessageType.h"
|
|
|
|
#include "eGameMessageType.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "ZCompression.h"
|
|
|
|
|
|
|
|
namespace Game {
|
2022-12-15 14:13:49 +00:00
|
|
|
dLogger* logger = nullptr;
|
|
|
|
dServer* server = nullptr;
|
|
|
|
dpWorld* physicsWorld = nullptr;
|
|
|
|
dChatFilter* chatFilter = nullptr;
|
|
|
|
dConfig* config = nullptr;
|
|
|
|
AssetManager* assetManager = nullptr;
|
|
|
|
RakPeerInterface* chatServer = nullptr;
|
2021-12-05 17:54:36 +00:00
|
|
|
std::mt19937 randomEngine;
|
|
|
|
SystemAddress chatSysAddr;
|
2022-12-15 12:02:38 +00:00
|
|
|
bool shouldShutdown = false;
|
|
|
|
} // namespace Game
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
bool chatDisabled = false;
|
2021-12-20 12:50:53 +00:00
|
|
|
bool chatConnected = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
bool worldShutdownSequenceComplete = false;
|
|
|
|
void WorldShutdownSequence();
|
2022-04-11 00:00:03 +00:00
|
|
|
void WorldShutdownProcess(uint32_t zoneId);
|
2022-04-10 01:35:41 +00:00
|
|
|
void FinalizeShutdown();
|
|
|
|
void SendShutdownMessageToMaster();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-16 10:24:02 +00:00
|
|
|
dLogger* SetupLogger(uint32_t zoneID, uint32_t instanceID);
|
2021-12-05 17:54:36 +00:00
|
|
|
void HandlePacketChat(Packet* packet);
|
|
|
|
void HandlePacket(Packet* packet);
|
|
|
|
|
|
|
|
struct tempSessionInfo {
|
|
|
|
SystemAddress sysAddr;
|
|
|
|
std::string hash;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<std::string, tempSessionInfo> m_PendingUsers;
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t instanceID = 0;
|
|
|
|
uint32_t g_CloneID = 0;
|
2021-12-16 22:11:51 +00:00
|
|
|
std::string databaseChecksum = "";
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
Diagnostics::SetProcessName("World");
|
|
|
|
Diagnostics::SetProcessFileName(argv[0]);
|
|
|
|
Diagnostics::Initialize();
|
|
|
|
|
|
|
|
// Triggers the shutdown sequence at application exit
|
|
|
|
std::atexit(WorldShutdownSequence);
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2022-01-11 21:47:29 +00:00
|
|
|
signal(SIGINT, [](int) { WorldShutdownSequence(); });
|
|
|
|
signal(SIGTERM, [](int) { WorldShutdownSequence(); });
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t zoneID = 1000;
|
|
|
|
uint32_t cloneID = 0;
|
|
|
|
uint32_t maxClients = 8;
|
|
|
|
uint32_t ourPort = 2007;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Check our arguments:
|
2022-12-16 10:24:02 +00:00
|
|
|
for (int32_t i = 0; i < argc; ++i) {
|
2021-12-05 17:54:36 +00:00
|
|
|
std::string argument(argv[i]);
|
|
|
|
|
|
|
|
if (argument == "-zone") zoneID = atoi(argv[i + 1]);
|
|
|
|
if (argument == "-instance") instanceID = atoi(argv[i + 1]);
|
|
|
|
if (argument == "-clone") cloneID = atoi(argv[i + 1]);
|
|
|
|
if (argument == "-maxclients") maxClients = atoi(argv[i + 1]);
|
|
|
|
if (argument == "-port") ourPort = atoi(argv[i + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create all the objects we need to run our service:
|
|
|
|
Game::logger = SetupLogger(zoneID, instanceID);
|
2022-12-15 14:13:49 +00:00
|
|
|
if (!Game::logger) return EXIT_FAILURE;
|
|
|
|
|
|
|
|
//Read our config:
|
|
|
|
Game::config = new dConfig((BinaryPathFinder::GetBinaryDir() / "worldconfig.ini").string());
|
|
|
|
Game::logger->SetLogToConsole(Game::config->GetValue("log_to_console") != "0");
|
|
|
|
Game::logger->SetLogDebugStatements(Game::config->GetValue("log_debug_statements") == "1");
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Starting World server...");
|
|
|
|
Game::logger->Log("WorldServer", "Version: %i.%i", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR);
|
|
|
|
Game::logger->Log("WorldServer", "Compiled on: %s", __TIMESTAMP__);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-15 14:13:49 +00:00
|
|
|
if (Game::config->GetValue("disable_chat") == "1") chatDisabled = true;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-11-01 18:21:26 +00:00
|
|
|
try {
|
2022-12-15 14:13:49 +00:00
|
|
|
std::string clientPathStr = Game::config->GetValue("client_location");
|
2022-11-27 11:59:59 +00:00
|
|
|
if (clientPathStr.empty()) clientPathStr = "./res";
|
|
|
|
std::filesystem::path clientPath = std::filesystem::path(clientPathStr);
|
|
|
|
if (clientPath.is_relative()) {
|
|
|
|
clientPath = BinaryPathFinder::GetBinaryDir() / clientPath;
|
|
|
|
}
|
|
|
|
Game::assetManager = new AssetManager(clientPath);
|
2022-11-01 18:21:26 +00:00
|
|
|
} catch (std::runtime_error& ex) {
|
|
|
|
Game::logger->Log("WorldServer", "Got an error while setting up assets: %s", ex.what());
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Connect to CDClient
|
|
|
|
try {
|
2022-12-22 06:34:11 +00:00
|
|
|
CDClientDatabase::Connect((BinaryPathFinder::GetBinaryDir() / "resServer" / "CDServer.sqlite").string());
|
2021-12-05 17:54:36 +00:00
|
|
|
} catch (CppSQLite3Exception& e) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Unable to connect to CDServer SQLite Database");
|
|
|
|
Game::logger->Log("WorldServer", "Error: %s", e.errorMessage());
|
|
|
|
Game::logger->Log("WorldServer", "Error Code: %i", e.errorCode());
|
2022-12-15 14:13:49 +00:00
|
|
|
return EXIT_FAILURE;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2023-03-17 14:36:21 +00:00
|
|
|
CDClientManager::Instance();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Connect to the MySQL Database
|
2022-12-15 14:13:49 +00:00
|
|
|
std::string mysql_host = Game::config->GetValue("mysql_host");
|
|
|
|
std::string mysql_database = Game::config->GetValue("mysql_database");
|
|
|
|
std::string mysql_username = Game::config->GetValue("mysql_username");
|
|
|
|
std::string mysql_password = Game::config->GetValue("mysql_password");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-15 14:13:49 +00:00
|
|
|
Diagnostics::SetProduceMemoryDump(Game::config->GetValue("generate_dump") == "1");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-15 14:13:49 +00:00
|
|
|
if (!Game::config->GetValue("dump_folder").empty()) {
|
|
|
|
Diagnostics::SetOutDirectory(Game::config->GetValue("dump_folder"));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Database::Connect(mysql_host, mysql_database, mysql_username, mysql_password);
|
|
|
|
} catch (sql::SQLException& ex) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Got an error while connecting to the database: %s", ex.what());
|
2022-12-15 14:13:49 +00:00
|
|
|
return EXIT_FAILURE;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Find out the master's IP:
|
|
|
|
std::string masterIP = "localhost";
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t masterPort = 1000;
|
2021-12-05 17:54:36 +00:00
|
|
|
sql::PreparedStatement* stmt = Database::CreatePreppedStmt("SELECT ip, port FROM servers WHERE name='master';");
|
|
|
|
auto res = stmt->executeQuery();
|
|
|
|
while (res->next()) {
|
|
|
|
masterIP = res->getString(1).c_str();
|
|
|
|
masterPort = res->getInt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete res;
|
|
|
|
delete stmt;
|
|
|
|
|
|
|
|
ObjectIDManager::Instance()->Initialize();
|
|
|
|
UserManager::Instance()->Initialize();
|
2021-12-20 10:25:45 +00:00
|
|
|
LootGenerator::Instance();
|
2022-12-15 14:13:49 +00:00
|
|
|
Game::chatFilter = new dChatFilter(Game::assetManager->GetResPath().string() + "/chatplus_en_us", bool(std::stoi(Game::config->GetValue("dont_generate_dcf"))));
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-15 13:46:03 +00:00
|
|
|
Game::server = new dServer(masterIP, ourPort, instanceID, maxClients, false, true, Game::logger, masterIP, masterPort, ServerType::World, Game::config, &Game::shouldShutdown, zoneID);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Connect to the chat server:
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t chatPort = 1501;
|
2022-12-15 14:13:49 +00:00
|
|
|
if (Game::config->GetValue("chat_server_port") != "") chatPort = std::atoi(Game::config->GetValue("chat_server_port").c_str());
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
auto chatSock = SocketDescriptor(uint16_t(ourPort + 2), 0);
|
|
|
|
Game::chatServer = RakNetworkFactory::GetRakPeerInterface();
|
|
|
|
Game::chatServer->Startup(1, 30, &chatSock, 1);
|
|
|
|
Game::chatServer->Connect(masterIP.c_str(), chatPort, "3.25 ND1", 8);
|
|
|
|
|
|
|
|
//Set up other things:
|
|
|
|
Game::randomEngine = std::mt19937(time(0));
|
|
|
|
|
|
|
|
//Run it until server gets a kill message from Master:
|
|
|
|
auto lastTime = std::chrono::high_resolution_clock::now();
|
|
|
|
auto t = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
Packet* packet = nullptr;
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t framesSinceLastFlush = 0;
|
|
|
|
uint32_t framesSinceMasterDisconnect = 0;
|
|
|
|
uint32_t framesSinceChatDisconnect = 0;
|
|
|
|
uint32_t framesSinceLastUsersSave = 0;
|
|
|
|
uint32_t framesSinceLastSQLPing = 0;
|
|
|
|
uint32_t framesSinceLastUser = 0;
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
const float maxPacketProcessingTime = 1.5f; //0.015f;
|
2022-12-16 10:24:02 +00:00
|
|
|
const uint32_t maxPacketsToProcess = 1024;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
bool ready = false;
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t framesSinceMasterStatus = 0;
|
|
|
|
uint32_t framesSinceShutdownSequence = 0;
|
2022-12-16 11:46:38 +00:00
|
|
|
uint32_t currentFramerate = highFramerate;
|
2022-02-09 09:38:09 +00:00
|
|
|
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t ghostingStepCount = 0;
|
2021-12-05 17:54:36 +00:00
|
|
|
auto ghostingLastTime = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
PerformanceManager::SelectProfile(zoneID);
|
|
|
|
|
|
|
|
//Load our level:
|
|
|
|
if (zoneID != 0) {
|
|
|
|
dpWorld::Instance().Initialize(zoneID);
|
2022-08-06 02:20:11 +00:00
|
|
|
dZoneManager::Instance()->Initialize(LWOZONEID(zoneID, instanceID, cloneID));
|
2021-12-05 17:54:36 +00:00
|
|
|
g_CloneID = cloneID;
|
2021-12-12 15:14:04 +00:00
|
|
|
|
|
|
|
// pre calculate the FDB checksum
|
2021-12-14 18:12:44 +00:00
|
|
|
if (Game::config->GetValue("check_fdb") == "1") {
|
2021-12-12 15:14:04 +00:00
|
|
|
std::ifstream fileStream;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-16 22:51:37 +00:00
|
|
|
static const std::vector<std::string> aliases = {
|
2022-11-01 18:21:26 +00:00
|
|
|
"CDServers.fdb",
|
|
|
|
"cdserver.fdb",
|
|
|
|
"CDClient.fdb",
|
|
|
|
"cdclient.fdb",
|
2021-12-16 22:11:51 +00:00
|
|
|
};
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-16 22:51:37 +00:00
|
|
|
for (const auto& file : aliases) {
|
2022-11-01 18:21:26 +00:00
|
|
|
fileStream.open(Game::assetManager->GetResPath() / file, std::ios::binary | std::ios::in);
|
2021-12-16 22:11:51 +00:00
|
|
|
if (fileStream.is_open()) {
|
|
|
|
break;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-16 22:11:51 +00:00
|
|
|
|
2022-12-16 10:24:02 +00:00
|
|
|
const int32_t bufferSize = 1024;
|
2021-12-12 15:14:04 +00:00
|
|
|
MD5* md5 = new MD5();
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-16 22:11:51 +00:00
|
|
|
char fileStreamBuffer[1024] = {};
|
2021-12-12 15:53:19 +00:00
|
|
|
|
2021-12-12 15:14:04 +00:00
|
|
|
while (!fileStream.eof()) {
|
2021-12-16 22:11:51 +00:00
|
|
|
memset(fileStreamBuffer, 0, bufferSize);
|
|
|
|
fileStream.read(fileStreamBuffer, bufferSize);
|
|
|
|
md5->update(fileStreamBuffer, fileStream.gcount());
|
2021-12-12 15:14:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 22:11:51 +00:00
|
|
|
fileStream.close();
|
2021-12-12 15:53:19 +00:00
|
|
|
|
2021-12-12 15:14:04 +00:00
|
|
|
const char* nullTerminateBuffer = "\0";
|
|
|
|
md5->update(nullTerminateBuffer, 1); // null terminate the data
|
|
|
|
md5->finalize();
|
2021-12-16 22:11:51 +00:00
|
|
|
databaseChecksum = md5->hexdigest();
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-12 15:14:04 +00:00
|
|
|
delete md5;
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "FDB Checksum calculated as: %s", databaseChecksum.c_str());
|
2021-12-12 15:14:04 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 12:02:54 +00:00
|
|
|
uint32_t currentFrameDelta = highFrameDelta;
|
2022-12-16 11:46:38 +00:00
|
|
|
// These values are adjust them selves to the current framerate should it update.
|
|
|
|
uint32_t logFlushTime = 15 * currentFramerate; // 15 seconds in frames
|
|
|
|
uint32_t shutdownTimeout = 10 * 60 * currentFramerate; // 10 minutes in frames
|
|
|
|
uint32_t noMasterConnectionTimeout = 5 * currentFramerate; // 5 seconds in frames
|
|
|
|
uint32_t chatReconnectionTime = 30 * currentFramerate; // 30 seconds in frames
|
|
|
|
uint32_t saveTime = 10 * 60 * currentFramerate; // 10 minutes in frames
|
|
|
|
uint32_t sqlPingTime = 10 * 60 * currentFramerate; // 10 minutes in frames
|
2022-12-22 13:16:35 +00:00
|
|
|
uint32_t emptyShutdownTime = (cloneID == 0 ? 30 : 5) * 60 * currentFramerate; // 30 minutes for main worlds, 5 for all others.
|
2021-12-05 17:54:36 +00:00
|
|
|
while (true) {
|
|
|
|
Metrics::StartMeasurement(MetricVariable::Frame);
|
|
|
|
Metrics::StartMeasurement(MetricVariable::GameLoop);
|
|
|
|
|
|
|
|
std::clock_t metricCPUTimeStart = std::clock();
|
|
|
|
|
|
|
|
const auto currentTime = std::chrono::high_resolution_clock::now();
|
|
|
|
float deltaTime = std::chrono::duration<float>(currentTime - lastTime).count();
|
|
|
|
lastTime = currentTime;
|
|
|
|
|
|
|
|
const auto occupied = UserManager::Instance()->GetUserCount() != 0;
|
|
|
|
|
2022-12-16 11:46:38 +00:00
|
|
|
uint32_t newFrameDelta = currentFrameDelta;
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!ready) {
|
2022-12-16 11:46:38 +00:00
|
|
|
newFrameDelta = highFrameDelta;
|
2021-12-05 17:54:36 +00:00
|
|
|
} else {
|
2022-12-16 11:46:38 +00:00
|
|
|
newFrameDelta = PerformanceManager::GetServerFrameDelta();
|
|
|
|
}
|
|
|
|
|
2022-12-16 12:02:54 +00:00
|
|
|
// Update to the new framerate and scale all timings to said new framerate
|
2022-12-16 11:46:38 +00:00
|
|
|
if (newFrameDelta != currentFrameDelta) {
|
2022-12-16 12:02:54 +00:00
|
|
|
float_t ratioBeforeToAfter = (float)currentFrameDelta / (float)newFrameDelta;
|
2022-12-16 11:46:38 +00:00
|
|
|
currentFrameDelta = newFrameDelta;
|
|
|
|
currentFramerate = MS_TO_FRAMES(newFrameDelta);
|
|
|
|
Game::logger->LogDebug("WorldServer", "Framerate for zone/instance/clone %i/%i/%i is now %i", zoneID, instanceID, cloneID, currentFramerate);
|
|
|
|
logFlushTime = 15 * currentFramerate; // 15 seconds in frames
|
2022-12-16 12:02:54 +00:00
|
|
|
framesSinceLastFlush *= ratioBeforeToAfter;
|
2022-12-16 11:46:38 +00:00
|
|
|
shutdownTimeout = 10 * 60 * currentFramerate; // 10 minutes in frames
|
2022-12-16 12:02:54 +00:00
|
|
|
framesSinceLastUser *= ratioBeforeToAfter;
|
2022-12-16 11:46:38 +00:00
|
|
|
noMasterConnectionTimeout = 5 * currentFramerate; // 5 seconds in frames
|
2022-12-16 12:02:54 +00:00
|
|
|
framesSinceMasterDisconnect *= ratioBeforeToAfter;
|
2022-12-16 11:46:38 +00:00
|
|
|
chatReconnectionTime = 30 * currentFramerate; // 30 seconds in frames
|
2022-12-16 12:02:54 +00:00
|
|
|
framesSinceChatDisconnect *= ratioBeforeToAfter;
|
2022-12-16 11:46:38 +00:00
|
|
|
saveTime = 10 * 60 * currentFramerate; // 10 minutes in frames
|
2022-12-16 12:02:54 +00:00
|
|
|
framesSinceLastUsersSave *= ratioBeforeToAfter;
|
2022-12-16 11:46:38 +00:00
|
|
|
sqlPingTime = 10 * 60 * currentFramerate; // 10 minutes in frames
|
2022-12-16 12:02:54 +00:00
|
|
|
framesSinceLastSQLPing *= ratioBeforeToAfter;
|
2022-12-22 13:16:35 +00:00
|
|
|
emptyShutdownTime = (cloneID == 0 ? 30 : 5) * 60 * currentFramerate; // 30 minutes for main worlds, 5 for all others.
|
|
|
|
framesSinceLastUser *= ratioBeforeToAfter;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Warning if we ran slow
|
2022-12-16 11:46:38 +00:00
|
|
|
if (deltaTime > currentFrameDelta) {
|
|
|
|
Game::logger->Log("WorldServer", "We're running behind, dT: %f > %f (framerate %i)", deltaTime, currentFrameDelta, currentFramerate);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Check if we're still connected to master:
|
|
|
|
if (!Game::server->GetIsConnectedToMaster()) {
|
|
|
|
framesSinceMasterDisconnect++;
|
|
|
|
|
2022-12-16 11:46:38 +00:00
|
|
|
if (framesSinceMasterDisconnect >= noMasterConnectionTimeout && !Game::shouldShutdown) {
|
|
|
|
Game::logger->Log("WorldServer", "Game loop running but no connection to master for %d frames, shutting down", noMasterConnectionTimeout);
|
2022-12-15 12:02:38 +00:00
|
|
|
Game::shouldShutdown = true;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
} else framesSinceMasterDisconnect = 0;
|
|
|
|
|
2021-12-20 12:50:53 +00:00
|
|
|
// Check if we're still connected to chat:
|
|
|
|
if (!chatConnected) {
|
|
|
|
framesSinceChatDisconnect++;
|
|
|
|
|
2022-12-16 11:46:38 +00:00
|
|
|
if (framesSinceChatDisconnect >= chatReconnectionTime) {
|
2021-12-20 12:50:53 +00:00
|
|
|
framesSinceChatDisconnect = 0;
|
|
|
|
|
|
|
|
Game::chatServer->Connect(masterIP.c_str(), chatPort, "3.25 ND1", 8);
|
|
|
|
}
|
|
|
|
} else framesSinceChatDisconnect = 0;
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//In world we'd update our other systems here.
|
|
|
|
|
|
|
|
if (zoneID != 0 && deltaTime > 0.0f) {
|
|
|
|
Metrics::StartMeasurement(MetricVariable::Physics);
|
2022-02-09 09:38:09 +00:00
|
|
|
dpWorld::Instance().StepWorld(deltaTime);
|
2021-12-05 17:54:36 +00:00
|
|
|
Metrics::EndMeasurement(MetricVariable::Physics);
|
|
|
|
|
|
|
|
Metrics::StartMeasurement(MetricVariable::UpdateEntities);
|
|
|
|
EntityManager::Instance()->UpdateEntities(deltaTime);
|
|
|
|
Metrics::EndMeasurement(MetricVariable::UpdateEntities);
|
|
|
|
|
|
|
|
Metrics::StartMeasurement(MetricVariable::Ghosting);
|
|
|
|
if (std::chrono::duration<float>(currentTime - ghostingLastTime).count() >= 1.0f) {
|
|
|
|
EntityManager::Instance()->UpdateGhosting();
|
|
|
|
ghostingLastTime = currentTime;
|
|
|
|
}
|
|
|
|
Metrics::EndMeasurement(MetricVariable::Ghosting);
|
|
|
|
|
|
|
|
Metrics::StartMeasurement(MetricVariable::UpdateSpawners);
|
|
|
|
dZoneManager::Instance()->Update(deltaTime);
|
|
|
|
Metrics::EndMeasurement(MetricVariable::UpdateSpawners);
|
|
|
|
}
|
|
|
|
|
|
|
|
Metrics::StartMeasurement(MetricVariable::PacketHandling);
|
|
|
|
|
|
|
|
//Check for packets here:
|
|
|
|
packet = Game::server->ReceiveFromMaster();
|
|
|
|
if (packet) { //We can get messages not handle-able by the dServer class, so handle them if we returned anything.
|
2022-05-09 22:33:43 +00:00
|
|
|
HandlePacket(packet);
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::server->DeallocateMasterPacket(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Handle our chat packets:
|
|
|
|
packet = Game::chatServer->Receive();
|
|
|
|
if (packet) {
|
|
|
|
HandlePacketChat(packet);
|
|
|
|
Game::chatServer->DeallocatePacket(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Handle world-specific packets:
|
|
|
|
float timeSpent = 0.0f;
|
|
|
|
|
|
|
|
UserManager::Instance()->DeletePendingRemovals();
|
|
|
|
|
|
|
|
auto t1 = std::chrono::high_resolution_clock::now();
|
2022-12-16 10:24:02 +00:00
|
|
|
for (uint32_t curPacket = 0; curPacket < maxPacketsToProcess && timeSpent < maxPacketProcessingTime; curPacket++) {
|
2021-12-05 17:54:36 +00:00
|
|
|
packet = Game::server->Receive();
|
|
|
|
if (packet) {
|
|
|
|
auto t1 = std::chrono::high_resolution_clock::now();
|
|
|
|
HandlePacket(packet);
|
|
|
|
auto t2 = std::chrono::high_resolution_clock::now();
|
|
|
|
|
2022-12-29 14:34:53 +00:00
|
|
|
timeSpent += std::chrono::duration_cast<std::chrono::duration<float>>(t2 - t1).count();
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::server->DeallocatePacket(packet);
|
|
|
|
packet = nullptr;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Metrics::EndMeasurement(MetricVariable::PacketHandling);
|
|
|
|
|
|
|
|
Metrics::StartMeasurement(MetricVariable::UpdateReplica);
|
|
|
|
|
|
|
|
//Update our replica objects:
|
|
|
|
Game::server->UpdateReplica();
|
|
|
|
|
|
|
|
Metrics::EndMeasurement(MetricVariable::UpdateReplica);
|
|
|
|
|
|
|
|
//Push our log every 15s:
|
2022-12-16 11:46:38 +00:00
|
|
|
if (framesSinceLastFlush >= logFlushTime) {
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::logger->Flush();
|
|
|
|
framesSinceLastFlush = 0;
|
|
|
|
} else framesSinceLastFlush++;
|
|
|
|
|
|
|
|
if (zoneID != 0 && !occupied) {
|
|
|
|
framesSinceLastUser++;
|
|
|
|
|
|
|
|
//If we haven't had any players for a while, time out and shut down:
|
2022-12-22 13:16:35 +00:00
|
|
|
if (framesSinceLastUser >= emptyShutdownTime) {
|
2022-12-15 12:02:38 +00:00
|
|
|
Game::shouldShutdown = true;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
framesSinceLastUser = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Save all connected users every 10 minutes:
|
2022-12-16 11:46:38 +00:00
|
|
|
if (framesSinceLastUsersSave >= saveTime && zoneID != 0) {
|
2021-12-05 17:54:36 +00:00
|
|
|
UserManager::Instance()->SaveAllActiveCharacters();
|
|
|
|
framesSinceLastUsersSave = 0;
|
|
|
|
|
|
|
|
if (PropertyManagementComponent::Instance() != nullptr) {
|
|
|
|
PropertyManagementComponent::Instance()->Save();
|
|
|
|
}
|
|
|
|
} else framesSinceLastUsersSave++;
|
|
|
|
|
|
|
|
//Every 10 min we ping our sql server to keep it alive hopefully:
|
2022-12-16 11:46:38 +00:00
|
|
|
if (framesSinceLastSQLPing >= sqlPingTime) {
|
2021-12-05 17:54:36 +00:00
|
|
|
//Find out the master's IP for absolutely no reason:
|
|
|
|
std::string masterIP;
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t masterPort;
|
2021-12-05 17:54:36 +00:00
|
|
|
sql::PreparedStatement* stmt = Database::CreatePreppedStmt("SELECT ip, port FROM servers WHERE name='master';");
|
|
|
|
auto res = stmt->executeQuery();
|
|
|
|
while (res->next()) {
|
|
|
|
masterIP = res->getString(1).c_str();
|
|
|
|
masterPort = res->getInt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete res;
|
|
|
|
delete stmt;
|
|
|
|
|
|
|
|
framesSinceLastSQLPing = 0;
|
|
|
|
} else framesSinceLastSQLPing++;
|
|
|
|
|
|
|
|
Metrics::EndMeasurement(MetricVariable::GameLoop);
|
|
|
|
|
|
|
|
Metrics::StartMeasurement(MetricVariable::Sleep);
|
|
|
|
|
2022-12-16 11:46:38 +00:00
|
|
|
t += std::chrono::milliseconds(currentFrameDelta);
|
2021-12-05 17:54:36 +00:00
|
|
|
std::this_thread::sleep_until(t);
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
Metrics::EndMeasurement(MetricVariable::Sleep);
|
|
|
|
|
|
|
|
if (!ready && Game::server->GetIsConnectedToMaster()) {
|
|
|
|
// Some delay is required here or else we crash the client?
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
framesSinceMasterStatus++;
|
|
|
|
|
|
|
|
if (framesSinceMasterStatus >= 200) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Finished loading world with zone (%i), ready up!", Game::server->GetZoneID());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
MasterPackets::SendWorldReady(Game::server, Game::server->GetZoneID(), Game::server->GetInstanceID());
|
|
|
|
|
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:02:38 +00:00
|
|
|
if (Game::shouldShutdown && !worldShutdownSequenceComplete) {
|
2022-04-10 01:35:41 +00:00
|
|
|
WorldShutdownProcess(zoneID);
|
2022-04-09 23:02:27 +00:00
|
|
|
break;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Metrics::AddMeasurement(MetricVariable::CPUTime, (1e6 * (1000.0 * (std::clock() - metricCPUTimeStart))) / CLOCKS_PER_SEC);
|
|
|
|
Metrics::EndMeasurement(MetricVariable::Frame);
|
|
|
|
}
|
2022-04-10 01:35:41 +00:00
|
|
|
FinalizeShutdown();
|
2022-04-09 23:02:27 +00:00
|
|
|
return EXIT_SUCCESS;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 10:24:02 +00:00
|
|
|
dLogger* SetupLogger(uint32_t zoneID, uint32_t instanceID) {
|
2022-11-27 11:59:59 +00:00
|
|
|
std::string logPath = (BinaryPathFinder::GetBinaryDir() / ("logs/WorldServer_" + std::to_string(zoneID) + "_" + std::to_string(instanceID) + "_" + std::to_string(time(nullptr)) + ".log")).string();
|
2021-12-05 17:54:36 +00:00
|
|
|
bool logToConsole = false;
|
2021-12-11 12:29:34 +00:00
|
|
|
bool logDebugStatements = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
logToConsole = true;
|
2021-12-11 12:29:34 +00:00
|
|
|
logDebugStatements = true;
|
2021-12-05 17:54:36 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-11 12:29:34 +00:00
|
|
|
return new dLogger(logPath, logToConsole, logDebugStatements);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HandlePacketChat(Packet* packet) {
|
|
|
|
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Lost our connection to chat, zone(%i), instance(%i)", Game::server->GetZoneID(), Game::server->GetInstanceID());
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-20 12:50:53 +00:00
|
|
|
chatConnected = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->data[0] == ID_CONNECTION_REQUEST_ACCEPTED) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Established connection to chat, zone(%i), instance (%i)", Game::server->GetZoneID(), Game::server->GetInstanceID());
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::chatSysAddr = packet->systemAddress;
|
2021-12-20 12:50:53 +00:00
|
|
|
|
|
|
|
chatConnected = true;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->data[0] == ID_USER_PACKET_ENUM) {
|
2023-05-03 21:38:32 +00:00
|
|
|
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::CHAT_INTERNAL) {
|
|
|
|
switch (static_cast<eChatInternalMessageType>(packet->data[3])) {
|
|
|
|
case eChatInternalMessageType::ROUTE_TO_PLAYER: {
|
2023-05-08 11:31:10 +00:00
|
|
|
CINSTREAM_SKIP_HEADER;
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID playerID;
|
|
|
|
inStream.Read(playerID);
|
|
|
|
|
|
|
|
auto player = EntityManager::Instance()->GetEntity(playerID);
|
|
|
|
if (!player) return;
|
|
|
|
|
|
|
|
auto sysAddr = player->GetSystemAddress();
|
|
|
|
|
|
|
|
//Write our stream outwards:
|
|
|
|
CBITSTREAM;
|
2022-12-16 10:24:02 +00:00
|
|
|
for (BitSize_t i = 0; i < inStream.GetNumberOfBytesUsed(); i++) {
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write(packet->data[i + 16]); //16 bytes == header + playerID to skip
|
|
|
|
}
|
|
|
|
|
|
|
|
SEND_PACKET; //send routed packet to player
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatInternalMessageType::ANNOUNCEMENT: {
|
2023-05-08 11:31:10 +00:00
|
|
|
CINSTREAM_SKIP_HEADER;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-17 03:40:46 +00:00
|
|
|
std::string title;
|
|
|
|
std::string msg;
|
|
|
|
|
|
|
|
uint32_t len;
|
|
|
|
inStream.Read<uint32_t>(len);
|
2022-12-16 10:24:02 +00:00
|
|
|
for (uint32_t i = 0; len > i; i++) {
|
2022-07-17 03:40:46 +00:00
|
|
|
char character;
|
|
|
|
inStream.Read<char>(character);
|
|
|
|
title += character;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-17 03:40:46 +00:00
|
|
|
len = 0;
|
|
|
|
inStream.Read<uint32_t>(len);
|
2022-12-16 10:24:02 +00:00
|
|
|
for (uint32_t i = 0; len > i; i++) {
|
2022-07-17 03:40:46 +00:00
|
|
|
char character;
|
|
|
|
inStream.Read<char>(character);
|
|
|
|
msg += character;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Send to our clients:
|
|
|
|
AMFArrayValue args;
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
args.Insert("title", title);
|
|
|
|
args.Insert("message", msg);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
GameMessages::SendUIMessageServerToAllClients("ToggleAnnounce", args);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatInternalMessageType::MUTE_UPDATE: {
|
2023-05-08 11:31:10 +00:00
|
|
|
CINSTREAM_SKIP_HEADER;
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID playerId;
|
|
|
|
time_t expire = 0;
|
|
|
|
inStream.Read(playerId);
|
|
|
|
inStream.Read(expire);
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* entity = EntityManager::Instance()->GetEntity(playerId);
|
|
|
|
|
|
|
|
if (entity != nullptr) {
|
|
|
|
entity->GetParentUser()->SetMuteExpire(expire);
|
|
|
|
|
|
|
|
entity->GetCharacter()->SendMuteNotice();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatInternalMessageType::TEAM_UPDATE: {
|
2023-05-08 11:31:10 +00:00
|
|
|
CINSTREAM_SKIP_HEADER;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
LWOOBJID teamID = 0;
|
|
|
|
char lootOption = 0;
|
|
|
|
char memberCount = 0;
|
|
|
|
std::vector<LWOOBJID> members;
|
|
|
|
|
|
|
|
inStream.Read(teamID);
|
|
|
|
bool deleteTeam = inStream.ReadBit();
|
|
|
|
|
|
|
|
if (deleteTeam) {
|
|
|
|
TeamManager::Instance()->DeleteTeam(teamID);
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Deleting team (%llu)", teamID);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
inStream.Read(lootOption);
|
|
|
|
inStream.Read(memberCount);
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Updating team (%llu), (%i), (%i)", teamID, lootOption, memberCount);
|
2021-12-05 17:54:36 +00:00
|
|
|
for (char i = 0; i < memberCount; i++) {
|
|
|
|
LWOOBJID member = LWOOBJID_EMPTY;
|
|
|
|
inStream.Read(member);
|
|
|
|
members.push_back(member);
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Updating team member (%llu)", member);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TeamManager::Instance()->UpdateTeam(teamID, lootOption, members);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Received an unknown chat internal: %i", int(packet->data[3]));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandlePacket(Packet* packet) {
|
|
|
|
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
|
|
|
|
auto user = UserManager::Instance()->GetUser(packet->systemAddress);
|
|
|
|
if (!user) return;
|
|
|
|
|
|
|
|
auto c = user->GetLastUsedChar();
|
|
|
|
if (!c) {
|
|
|
|
UserManager::Instance()->DeleteUser(packet->systemAddress);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* entity = EntityManager::Instance()->GetEntity(c->GetObjectID());
|
|
|
|
|
|
|
|
if (!entity) {
|
|
|
|
entity = Player::GetPlayer(packet->systemAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity) {
|
2023-06-07 07:23:50 +00:00
|
|
|
auto skillComponent = entity->GetComponent<SkillComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (skillComponent != nullptr) {
|
|
|
|
skillComponent->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
entity->GetCharacter()->SaveXMLToDatabase();
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Deleting player %llu", entity->GetObjectID());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
EntityManager::Instance()->DestroyEntity(entity);
|
2022-07-13 03:36:06 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-13 03:36:06 +00:00
|
|
|
{
|
2021-12-05 17:54:36 +00:00
|
|
|
CBITSTREAM;
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::PLAYER_REMOVED_NOTIFICATION);
|
2022-07-13 03:36:06 +00:00
|
|
|
bitStream.Write(user->GetLoggedInChar());
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, Game::chatSysAddr, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
UserManager::Instance()->DeleteUser(packet->systemAddress);
|
|
|
|
|
|
|
|
if (PropertyManagementComponent::Instance() != nullptr) {
|
|
|
|
PropertyManagementComponent::Instance()->Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
CBITSTREAM;
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::PLAYER_REMOVED);
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write((LWOMAPID)Game::server->GetZoneID());
|
|
|
|
bitStream.Write((LWOINSTANCEID)instanceID);
|
|
|
|
Game::server->SendToMaster(&bitStream);
|
|
|
|
}
|
|
|
|
|
2023-05-06 18:32:53 +00:00
|
|
|
if (packet->data[0] != ID_USER_PACKET_ENUM || packet->length < 4) return;
|
2023-05-03 21:38:32 +00:00
|
|
|
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::SERVER) {
|
|
|
|
if (static_cast<eServerMessageType>(packet->data[3]) == eServerMessageType::VERSION_CONFIRM) {
|
2021-12-05 17:54:36 +00:00
|
|
|
AuthPackets::HandleHandshake(Game::server, packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::MASTER) {
|
|
|
|
switch (static_cast<eMasterMessageType>(packet->data[3])) {
|
|
|
|
case eMasterMessageType::REQUEST_PERSISTENT_ID_RESPONSE: {
|
2021-12-05 17:54:36 +00:00
|
|
|
uint64_t requestID = PacketUtils::ReadPacketU64(8, packet);
|
|
|
|
uint32_t objectID = PacketUtils::ReadPacketU32(16, packet);
|
|
|
|
ObjectIDManager::Instance()->HandleRequestPersistentIDResponse(requestID, objectID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eMasterMessageType::REQUEST_ZONE_TRANSFER_RESPONSE: {
|
2021-12-05 17:54:36 +00:00
|
|
|
uint64_t requestID = PacketUtils::ReadPacketU64(8, packet);
|
|
|
|
ZoneInstanceManager::Instance()->HandleRequestZoneTransferResponse(requestID, packet);
|
|
|
|
break;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eMasterMessageType::SESSION_KEY_RESPONSE: {
|
2021-12-05 17:54:36 +00:00
|
|
|
//Read our session key and to which user it belongs:
|
|
|
|
RakNet::BitStream inStream(packet->data, packet->length, false);
|
|
|
|
uint64_t header = inStream.Read(header);
|
|
|
|
uint32_t sessionKey = 0;
|
|
|
|
std::string username;
|
|
|
|
|
|
|
|
inStream.Read(sessionKey);
|
|
|
|
username = PacketUtils::ReadString(12, packet, false);
|
|
|
|
|
|
|
|
//Find them:
|
|
|
|
auto it = m_PendingUsers.find(username);
|
|
|
|
if (it == m_PendingUsers.end()) return;
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
//Convert our key:
|
|
|
|
std::string userHash = std::to_string(sessionKey);
|
|
|
|
userHash = md5(userHash);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Verify it:
|
|
|
|
if (userHash != it->second.hash) {
|
|
|
|
Game::logger->Log("WorldServer", "SOMEONE IS TRYING TO HACK? SESSION KEY MISMATCH: ours: %s != master: %s", userHash.c_str(), it->second.hash.c_str());
|
2023-02-19 12:29:14 +00:00
|
|
|
Game::server->Disconnect(it->second.sysAddr, eServerDisconnectIdentifiers::INVALID_SESSION_KEY);
|
2022-07-28 13:39:57 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::logger->Log("WorldServer", "User %s authenticated with correct key.", username.c_str());
|
|
|
|
|
|
|
|
UserManager::Instance()->DeleteUser(packet->systemAddress);
|
|
|
|
|
|
|
|
//Create our user and send them in:
|
|
|
|
UserManager::Instance()->CreateUser(it->second.sysAddr, username, userHash);
|
|
|
|
|
|
|
|
auto zone = dZoneManager::Instance()->GetZone();
|
|
|
|
if (zone) {
|
|
|
|
float x = 0.0f;
|
|
|
|
float y = 0.0f;
|
|
|
|
float z = 0.0f;
|
|
|
|
|
|
|
|
if (zone->GetZoneID().GetMapID() == 1100) {
|
|
|
|
auto pos = zone->GetSpawnPos();
|
|
|
|
x = pos.x;
|
|
|
|
y = pos.y;
|
|
|
|
z = pos.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorldPackets::SendLoadStaticZone(it->second.sysAddr, x, y, z, zone->GetChecksum());
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (Game::server->GetZoneID() == 0) {
|
|
|
|
//Since doing this reroute breaks the client's request, we have to call this manually.
|
|
|
|
UserManager::Instance()->RequestCharacterList(it->second.sysAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_PendingUsers.erase(username);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//Notify master:
|
2022-07-28 13:39:57 +00:00
|
|
|
{
|
2021-12-05 17:54:36 +00:00
|
|
|
CBITSTREAM;
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::PLAYER_ADDED);
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write((LWOMAPID)Game::server->GetZoneID());
|
|
|
|
bitStream.Write((LWOINSTANCEID)instanceID);
|
|
|
|
Game::server->SendToMaster(&bitStream);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-05-03 21:38:32 +00:00
|
|
|
case eMasterMessageType::AFFIRM_TRANSFER_REQUEST: {
|
2021-12-05 17:54:36 +00:00
|
|
|
const uint64_t requestID = PacketUtils::ReadPacketU64(8, packet);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("MasterServer", "Got affirmation request of transfer %llu", requestID);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
CBITSTREAM;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::AFFIRM_TRANSFER_RESPONSE);
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write(requestID);
|
|
|
|
Game::server->SendToMaster(&bitStream);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eMasterMessageType::SHUTDOWN: {
|
2022-12-15 12:02:38 +00:00
|
|
|
Game::shouldShutdown = true;
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Got shutdown request from master, zone (%i), instance (%i)", Game::server->GetZoneID(), Game::server->GetInstanceID());
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eMasterMessageType::NEW_SESSION_ALERT: {
|
2021-12-05 17:54:36 +00:00
|
|
|
RakNet::BitStream inStream(packet->data, packet->length, false);
|
|
|
|
uint64_t header = inStream.Read(header);
|
|
|
|
uint32_t sessionKey = inStream.Read(sessionKey);
|
2022-07-17 05:57:00 +00:00
|
|
|
|
|
|
|
std::string username;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-17 05:57:00 +00:00
|
|
|
uint32_t len;
|
|
|
|
inStream.Read(len);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-12-16 10:24:02 +00:00
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
2022-07-17 05:57:00 +00:00
|
|
|
char character; inStream.Read<char>(character);
|
|
|
|
username += character;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Find them:
|
2022-07-17 05:57:00 +00:00
|
|
|
User* user = UserManager::Instance()->GetUser(username.c_str());
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!user) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Got new session alert for user %s, but they're not logged in.", username.c_str());
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check the key:
|
|
|
|
if (sessionKey != std::atoi(user->GetSessionKey().c_str())) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Got new session alert for user %s, but the session key is invalid.", username.c_str());
|
2023-02-19 12:29:14 +00:00
|
|
|
Game::server->Disconnect(user->GetSystemAddress(), eServerDisconnectIdentifiers::INVALID_SESSION_KEY);
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
default:
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Unknown packet ID from master %i", int(packet->data[3]));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
if (static_cast<eConnectionType>(packet->data[1]) != eConnectionType::WORLD) return;
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
switch (static_cast<eWorldMessageType>(packet->data[3])) {
|
|
|
|
case eWorldMessageType::VALIDATION: {
|
2021-12-05 17:54:36 +00:00
|
|
|
std::string username = PacketUtils::ReadString(0x08, packet, true);
|
|
|
|
std::string sessionKey = PacketUtils::ReadString(74, packet, true);
|
2021-12-16 22:11:51 +00:00
|
|
|
std::string clientDatabaseChecksum = PacketUtils::ReadString(packet->length - 33, packet, false);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2021-12-14 19:25:30 +00:00
|
|
|
// sometimes client puts a null terminator at the end of the checksum and sometimes doesn't, weird
|
|
|
|
clientDatabaseChecksum = clientDatabaseChecksum.substr(0, 32);
|
|
|
|
|
2021-12-16 22:11:51 +00:00
|
|
|
// If the check is turned on, validate the client's database checksum.
|
|
|
|
if (Game::config->GetValue("check_fdb") == "1" && !databaseChecksum.empty()) {
|
|
|
|
uint32_t gmLevel = 0;
|
2022-07-25 02:26:51 +00:00
|
|
|
auto* stmt = Database::CreatePreppedStmt("SELECT gm_level FROM accounts WHERE name=? LIMIT 1;");
|
2021-12-16 22:11:51 +00:00
|
|
|
stmt->setString(1, username.c_str());
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* res = stmt->executeQuery();
|
|
|
|
while (res->next()) {
|
|
|
|
gmLevel = res->getInt(1);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
delete stmt;
|
|
|
|
delete res;
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-16 22:11:51 +00:00
|
|
|
// Developers may skip this check
|
|
|
|
if (gmLevel < 8 && clientDatabaseChecksum != databaseChecksum) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Client's database checksum does not match the server's, aborting connection.");
|
2023-02-19 12:29:14 +00:00
|
|
|
Game::server->Disconnect(packet->systemAddress, eServerDisconnectIdentifiers::WRONG_GAME_VERSION);
|
2022-07-28 13:39:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Request the session info from Master:
|
|
|
|
CBITSTREAM;
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::REQUEST_SESSION_KEY);
|
2021-12-05 17:54:36 +00:00
|
|
|
PacketUtils::WriteString(bitStream, username, 64);
|
|
|
|
Game::server->SendToMaster(&bitStream);
|
|
|
|
|
|
|
|
//Insert info into our pending list
|
|
|
|
tempSessionInfo info;
|
|
|
|
info.sysAddr = SystemAddress(packet->systemAddress);
|
|
|
|
info.hash = sessionKey;
|
|
|
|
m_PendingUsers.insert(std::make_pair(username, info));
|
2022-07-28 13:39:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::CHARACTER_LIST_REQUEST: {
|
2021-12-05 17:54:36 +00:00
|
|
|
//We need to delete the entity first, otherwise the char list could delete it while it exists in the world!
|
|
|
|
if (Game::server->GetZoneID() != 0) {
|
2022-07-13 03:36:06 +00:00
|
|
|
auto user = UserManager::Instance()->GetUser(packet->systemAddress);
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!user) return;
|
|
|
|
EntityManager::Instance()->DestroyEntity(user->GetLastUsedChar()->GetEntity());
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//This loops prevents users who aren't authenticated to double-request the char list, which
|
2022-05-09 22:33:43 +00:00
|
|
|
//would make the login screen freeze sometimes.
|
2021-12-05 17:54:36 +00:00
|
|
|
if (m_PendingUsers.size() > 0) {
|
|
|
|
for (auto it : m_PendingUsers) {
|
|
|
|
if (it.second.sysAddr == packet->systemAddress) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UserManager::Instance()->RequestCharacterList(packet->systemAddress);
|
|
|
|
break;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::GAME_MSG: {
|
2021-12-05 17:54:36 +00:00
|
|
|
RakNet::BitStream bitStream(packet->data, packet->length, false);
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
uint64_t header;
|
|
|
|
LWOOBJID objectID;
|
2023-05-03 21:38:32 +00:00
|
|
|
eGameMessageType messageID;
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Read(header);
|
|
|
|
bitStream.Read(objectID);
|
|
|
|
bitStream.Read(messageID);
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
RakNet::BitStream dataStream;
|
|
|
|
bitStream.Read(dataStream, bitStream.GetNumberOfUnreadBits());
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
GameMessageHandler::HandleMessage(&dataStream, packet->systemAddress, objectID, messageID);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::CHARACTER_CREATE_REQUEST: {
|
2021-12-05 17:54:36 +00:00
|
|
|
UserManager::Instance()->CreateCharacter(packet->systemAddress, packet);
|
|
|
|
break;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::LOGIN_REQUEST: {
|
2021-12-05 17:54:36 +00:00
|
|
|
RakNet::BitStream inStream(packet->data, packet->length, false);
|
|
|
|
uint64_t header = inStream.Read(header);
|
|
|
|
|
|
|
|
LWOOBJID playerID = 0;
|
2022-07-13 03:36:06 +00:00
|
|
|
inStream.Read(playerID);
|
2023-04-25 18:17:40 +00:00
|
|
|
GeneralUtils::ClearBit(playerID, eObjectBits::CHARACTER);
|
|
|
|
GeneralUtils::ClearBit(playerID, eObjectBits::PERSISTENT);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-13 03:36:06 +00:00
|
|
|
auto user = UserManager::Instance()->GetUser(packet->systemAddress);
|
|
|
|
|
|
|
|
if (user) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto lastCharacter = user->GetLoggedInChar();
|
|
|
|
// This means we swapped characters and we need to remove the previous player from the container.
|
|
|
|
if (static_cast<uint32_t>(lastCharacter) != playerID) {
|
2022-07-13 03:36:06 +00:00
|
|
|
CBITSTREAM;
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::PLAYER_REMOVED_NOTIFICATION);
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write(lastCharacter);
|
|
|
|
Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, Game::chatSysAddr, false);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UserManager::Instance()->LoginCharacter(packet->systemAddress, static_cast<uint32_t>(playerID));
|
|
|
|
break;
|
|
|
|
}
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::CHARACTER_DELETE_REQUEST: {
|
2021-12-05 17:54:36 +00:00
|
|
|
UserManager::Instance()->DeleteCharacter(packet->systemAddress, packet);
|
|
|
|
UserManager::Instance()->RequestCharacterList(packet->systemAddress);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::CHARACTER_RENAME_REQUEST: {
|
2021-12-05 17:54:36 +00:00
|
|
|
UserManager::Instance()->RenameCharacter(packet->systemAddress, packet);
|
|
|
|
break;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::LEVEL_LOAD_COMPLETE: {
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::logger->Log("WorldServer", "Received level load complete from user.");
|
|
|
|
User* user = UserManager::Instance()->GetUser(packet->systemAddress);
|
2022-07-13 03:36:06 +00:00
|
|
|
if (user) {
|
2021-12-05 17:54:36 +00:00
|
|
|
Character* c = user->GetLastUsedChar();
|
2022-05-09 22:33:43 +00:00
|
|
|
if (c != nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
std::u16string username = GeneralUtils::ASCIIToUTF16(c->GetName());
|
|
|
|
Game::server->GetReplicaManager()->AddParticipant(packet->systemAddress);
|
|
|
|
|
|
|
|
EntityInfo info{};
|
|
|
|
info.lot = 1;
|
2022-04-01 01:17:43 +00:00
|
|
|
Entity* player = EntityManager::Instance()->CreateEntity(info, UserManager::Instance()->GetUser(packet->systemAddress));
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
WorldPackets::SendCreateCharacter(packet->systemAddress, player, c->GetXMLData(), username, c->GetGMLevel());
|
|
|
|
WorldPackets::SendServerState(packet->systemAddress);
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto respawnPoint = player->GetCharacter()->GetRespawnPoint(dZoneManager::Instance()->GetZone()->GetWorldID());
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
EntityManager::Instance()->ConstructEntity(player, UNASSIGNED_SYSTEM_ADDRESS, true);
|
|
|
|
|
|
|
|
if (respawnPoint != NiPoint3::ZERO) {
|
2022-05-09 00:57:36 +00:00
|
|
|
GameMessages::SendPlayerReachedRespawnCheckpoint(player, respawnPoint, NiQuaternion::IDENTITY);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2022-05-09 00:57:36 +00:00
|
|
|
|
|
|
|
EntityManager::Instance()->ConstructAllEntities(packet->systemAddress);
|
|
|
|
|
2023-06-07 07:23:50 +00:00
|
|
|
auto characterComponent = player->GetComponent<CharacterComponent>();
|
2022-05-09 00:57:36 +00:00
|
|
|
if (characterComponent) {
|
2022-01-20 01:34:53 +00:00
|
|
|
player->GetComponent<CharacterComponent>()->RocketUnEquip(player);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2022-01-19 01:51:12 +00:00
|
|
|
|
2022-12-19 19:45:50 +00:00
|
|
|
// Do charxml fixes here
|
2023-06-07 07:23:50 +00:00
|
|
|
auto levelComponent = player->GetComponent<LevelProgressionComponent>();
|
2022-12-19 19:45:50 +00:00
|
|
|
if (!levelComponent) return;
|
|
|
|
|
|
|
|
auto version = levelComponent->GetCharacterVersion();
|
|
|
|
switch(version) {
|
|
|
|
case eCharacterVersion::RELEASE:
|
|
|
|
// TODO: Implement, super low priority
|
|
|
|
case eCharacterVersion::LIVE:
|
|
|
|
Game::logger->Log("WorldServer", "Updating Character Flags");
|
|
|
|
c->SetRetroactiveFlags();
|
|
|
|
levelComponent->SetCharacterVersion(eCharacterVersion::PLAYER_FACTION_FLAGS);
|
|
|
|
case eCharacterVersion::PLAYER_FACTION_FLAGS:
|
|
|
|
Game::logger->Log("WorldServer", "Updating Vault Size");
|
|
|
|
player->RetroactiveVaultSize();
|
|
|
|
levelComponent->SetCharacterVersion(eCharacterVersion::VAULT_SIZE);
|
|
|
|
case eCharacterVersion::VAULT_SIZE:
|
|
|
|
Game::logger->Log("WorldServer", "Updaing Speedbase");
|
|
|
|
levelComponent->SetRetroactiveBaseSpeed();
|
|
|
|
levelComponent->SetCharacterVersion(eCharacterVersion::UP_TO_DATE);
|
|
|
|
case eCharacterVersion::UP_TO_DATE:
|
|
|
|
break;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
player->GetCharacter()->SetTargetScene("");
|
|
|
|
|
|
|
|
// Fix the destroyable component
|
2023-06-07 07:23:50 +00:00
|
|
|
auto destroyableComponent = player->GetComponent<DestroyableComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (destroyableComponent != nullptr) {
|
|
|
|
destroyableComponent->FixStats();
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Tell the player to generate BBB models, if any:
|
|
|
|
if (g_CloneID != 0) {
|
|
|
|
const auto& worldId = dZoneManager::Instance()->GetZone()->GetZoneID();
|
|
|
|
|
2022-01-13 03:48:27 +00:00
|
|
|
const auto zoneId = Game::server->GetZoneID();
|
|
|
|
const auto cloneId = g_CloneID;
|
|
|
|
|
|
|
|
auto query = CDClientDatabase::CreatePreppedStmt(
|
|
|
|
"SELECT id FROM PropertyTemplate WHERE mapID = ?;");
|
|
|
|
query.bind(1, (int)zoneId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
auto result = query.execQuery();
|
|
|
|
|
|
|
|
if (result.eof() || result.fieldIsNull(0)) {
|
|
|
|
Game::logger->Log("WorldServer", "No property templates found for zone %d, not sending BBB", zoneId);
|
|
|
|
goto noBBB;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Check for BBB models:
|
|
|
|
auto stmt = Database::CreatePreppedStmt("SELECT ugc_id FROM properties_contents WHERE lot=14 AND property_id=?");
|
|
|
|
|
2022-12-16 10:24:02 +00:00
|
|
|
int32_t templateId = result.getIntField(0);
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
result.finalize();
|
|
|
|
|
|
|
|
auto* propertyLookup = Database::CreatePreppedStmt("SELECT * FROM properties WHERE template_id = ? AND clone_id = ?;");
|
|
|
|
|
|
|
|
propertyLookup->setInt(1, templateId);
|
|
|
|
propertyLookup->setInt64(2, g_CloneID);
|
|
|
|
|
|
|
|
auto* propertyEntry = propertyLookup->executeQuery();
|
|
|
|
uint64_t propertyId = 0;
|
|
|
|
|
|
|
|
if (propertyEntry->next()) {
|
|
|
|
propertyId = propertyEntry->getUInt64(1);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
delete propertyLookup;
|
|
|
|
|
|
|
|
stmt->setUInt64(1, propertyId);
|
|
|
|
auto res = stmt->executeQuery();
|
|
|
|
while (res->next()) {
|
|
|
|
Game::logger->Log("UGC", "Getting lxfml ugcID: %u", res->getUInt(1));
|
|
|
|
|
|
|
|
//Get lxfml:
|
|
|
|
auto stmtL = Database::CreatePreppedStmt("SELECT lxfml from ugc where id=?");
|
|
|
|
stmtL->setUInt(1, res->getUInt(1));
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
auto lxres = stmtL->executeQuery();
|
|
|
|
|
|
|
|
while (lxres->next()) {
|
|
|
|
auto lxfml = lxres->getBlob(1);
|
|
|
|
|
|
|
|
lxfml->seekg(0, std::ios::end);
|
|
|
|
size_t lxfmlSize = lxfml->tellg();
|
|
|
|
lxfml->seekg(0);
|
|
|
|
|
|
|
|
//Send message:
|
2022-07-28 13:39:57 +00:00
|
|
|
{
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID blueprintID = res->getUInt(1);
|
2023-04-25 18:17:40 +00:00
|
|
|
GeneralUtils::SetBit(blueprintID, eObjectBits::CHARACTER);
|
|
|
|
GeneralUtils::SetBit(blueprintID, eObjectBits::PERSISTENT);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
CBITSTREAM;
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::BLUEPRINT_SAVE_RESPONSE);
|
2022-11-28 00:48:46 +00:00
|
|
|
bitStream.Write<LWOOBJID>(LWOOBJID_EMPTY); //always zero so that a check on the client passes
|
|
|
|
bitStream.Write(eBlueprintSaveResponseType::EverythingWorked);
|
|
|
|
bitStream.Write<uint32_t>(1);
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write(blueprintID);
|
|
|
|
|
|
|
|
bitStream.Write<uint32_t>(lxfmlSize);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < lxfmlSize; ++i)
|
|
|
|
bitStream.Write<uint8_t>(lxfml->get());
|
|
|
|
|
|
|
|
SystemAddress sysAddr = packet->systemAddress;
|
|
|
|
SEND_PACKET;
|
|
|
|
PacketUtils::SavePacket("lxfml packet " + std::to_string(res->getUInt(1)) + ".bin", (char*)bitStream.GetData(), bitStream.GetNumberOfBytesUsed());
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete stmtL;
|
|
|
|
delete lxres;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete stmt;
|
|
|
|
delete res;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
noBBB:
|
|
|
|
|
|
|
|
// Tell the client it's done loading:
|
|
|
|
GameMessages::SendInvalidZoneTransferList(player, packet->systemAddress, GeneralUtils::ASCIIToUTF16(Game::config->GetValue("source")), u"", false, false);
|
|
|
|
GameMessages::SendServerDoneLoadingAllObjects(player, packet->systemAddress);
|
|
|
|
|
|
|
|
//Send the player it's mail count:
|
|
|
|
//update: this might not be needed so im going to try disabling this here.
|
|
|
|
//Mail::HandleNotificationRequest(packet->systemAddress, player->GetObjectID());
|
2022-05-09 22:33:43 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//Notify chat that a player has loaded:
|
2022-07-28 13:39:57 +00:00
|
|
|
{
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& playerName = player->GetCharacter()->GetName();
|
|
|
|
//RakNet::RakString playerName(player->GetCharacter()->GetName().c_str());
|
|
|
|
|
|
|
|
CBITSTREAM;
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::PLAYER_ADDED_NOTIFICATION);
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write(player->GetObjectID());
|
|
|
|
bitStream.Write<uint32_t>(playerName.size());
|
|
|
|
for (size_t i = 0; i < playerName.size(); i++) {
|
|
|
|
bitStream.Write(playerName[i]);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
auto zone = dZoneManager::Instance()->GetZone()->GetZoneID();
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write(zone.GetMapID());
|
2022-07-25 02:26:51 +00:00
|
|
|
bitStream.Write(zone.GetInstanceID());
|
|
|
|
bitStream.Write(zone.GetCloneID());
|
|
|
|
bitStream.Write(player->GetParentUser()->GetMuteExpire());
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, Game::chatSysAddr, false);
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Couldn't find character to log in with for user %s (%i)!", user->GetUsername().c_str(), user->GetAccountID());
|
2023-02-19 12:29:14 +00:00
|
|
|
Game::server->Disconnect(packet->systemAddress, eServerDisconnectIdentifiers::CHARACTER_NOT_FOUND);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Couldn't get user for level load complete!");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::POSITION_UPDATE: {
|
2021-12-05 17:54:36 +00:00
|
|
|
ClientPackets::HandleClientPositionUpdate(packet->systemAddress, packet);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::MAIL: {
|
2021-12-05 17:54:36 +00:00
|
|
|
RakNet::BitStream bitStream(packet->data, packet->length, false);
|
|
|
|
LWOOBJID space;
|
|
|
|
bitStream.Read(space);
|
|
|
|
Mail::HandleMailStuff(&bitStream, packet->systemAddress, UserManager::Instance()->GetUser(packet->systemAddress)->GetLastUsedChar()->GetEntity());
|
2022-07-28 13:39:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::ROUTE_PACKET: {
|
2022-07-25 02:26:51 +00:00
|
|
|
//Yeet to chat
|
2023-05-08 11:31:10 +00:00
|
|
|
CINSTREAM_SKIP_HEADER;
|
2022-07-25 02:26:51 +00:00
|
|
|
uint32_t size = 0;
|
|
|
|
inStream.Read(size);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (size > 20000) {
|
|
|
|
Game::logger->Log("WorldServer", "Tried to route a packet with a read size > 20000, so likely a false packet.");
|
2022-07-28 13:39:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
CBITSTREAM;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::CHAT, packet->data[14]);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//We need to insert the player's objectID so the chat server can find who originated this request:
|
|
|
|
LWOOBJID objectID = 0;
|
|
|
|
auto user = UserManager::Instance()->GetUser(packet->systemAddress);
|
|
|
|
if (user) {
|
|
|
|
objectID = user->GetLastUsedChar()->GetObjectID();
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
bitStream.Write(objectID);
|
|
|
|
|
|
|
|
//Now write the rest of the data:
|
|
|
|
auto data = inStream.GetData();
|
|
|
|
for (uint32_t i = 0; i < size; ++i) {
|
|
|
|
bitStream.Write(data[i + 23]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE_ORDERED, 0, Game::chatSysAddr, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::STRING_CHECK: {
|
2021-12-05 17:54:36 +00:00
|
|
|
ClientPackets::HandleChatModerationRequest(packet->systemAddress, packet);
|
2022-07-28 13:39:57 +00:00
|
|
|
break;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::GENERAL_CHAT_MESSAGE: {
|
2021-12-05 17:54:36 +00:00
|
|
|
if (chatDisabled) {
|
|
|
|
ChatPackets::SendMessageFail(packet->systemAddress);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
ClientPackets::HandleChatMessage(packet->systemAddress, packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eWorldMessageType::HANDLE_FUNNESS: {
|
2021-12-05 17:54:36 +00:00
|
|
|
//This means the client is running slower or faster than it should.
|
|
|
|
//Could be insane lag, but I'mma just YEET them as it's usually speedhacking.
|
|
|
|
//This is updated to now count the amount of times we've been caught "speedhacking" to kick with a delay
|
|
|
|
//This is hopefully going to fix the random disconnects people face sometimes.
|
|
|
|
if (Game::config->GetValue("disable_anti_speedhack") == "1") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
User* user = UserManager::Instance()->GetUser(packet->systemAddress);
|
|
|
|
if (user) {
|
|
|
|
user->UserOutOfSync();
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2023-02-19 12:29:14 +00:00
|
|
|
Game::server->Disconnect(packet->systemAddress, eServerDisconnectIdentifiers::KICK);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
default:
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::server->GetLogger()->Log("HandlePacket", "Unknown world packet received: %i", int(packet->data[3]));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 01:35:41 +00:00
|
|
|
void WorldShutdownProcess(uint32_t zoneId) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Saving map %i instance %i", zoneId, instanceID);
|
2022-04-10 01:35:41 +00:00
|
|
|
for (auto i = 0; i < Game::server->GetReplicaManager()->GetParticipantCount(); ++i) {
|
|
|
|
const auto& player = Game::server->GetReplicaManager()->GetParticipantAtIndex(i);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-04-10 01:35:41 +00:00
|
|
|
auto* entity = Player::GetPlayer(player);
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Saving data!");
|
2022-04-10 01:35:41 +00:00
|
|
|
if (entity != nullptr && entity->GetCharacter() != nullptr) {
|
2023-06-07 07:23:50 +00:00
|
|
|
auto skillComponent = entity->GetComponent<SkillComponent>();
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-04-10 01:35:41 +00:00
|
|
|
if (skillComponent != nullptr) {
|
|
|
|
skillComponent->Reset();
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Saving character %s...", entity->GetCharacter()->GetName().c_str());
|
2022-04-10 01:35:41 +00:00
|
|
|
entity->GetCharacter()->SaveXMLToDatabase();
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Character data for %s was saved!", entity->GetCharacter()->GetName().c_str());
|
2022-04-10 01:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-04-10 01:35:41 +00:00
|
|
|
if (PropertyManagementComponent::Instance() != nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Saving ALL property data for zone %i clone %i!", zoneId, PropertyManagementComponent::Instance()->GetCloneId());
|
2022-04-10 01:35:41 +00:00
|
|
|
PropertyManagementComponent::Instance()->Save();
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "ALL property data saved for zone %i clone %i!", zoneId, PropertyManagementComponent::Instance()->GetCloneId());
|
2022-04-10 01:35:41 +00:00
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "ALL DATA HAS BEEN SAVED FOR ZONE %i INSTANCE %i!", zoneId, instanceID);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-04-10 01:35:41 +00:00
|
|
|
while (Game::server->GetReplicaManager()->GetParticipantCount() > 0) {
|
|
|
|
const auto& player = Game::server->GetReplicaManager()->GetParticipantAtIndex(0);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-02-19 12:29:14 +00:00
|
|
|
Game::server->Disconnect(player, eServerDisconnectIdentifiers::SERVER_SHUTDOWN);
|
2022-04-10 01:35:41 +00:00
|
|
|
}
|
|
|
|
SendShutdownMessageToMaster();
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-04-10 01:35:41 +00:00
|
|
|
void WorldShutdownSequence() {
|
2022-12-15 12:02:38 +00:00
|
|
|
if (Game::shouldShutdown || worldShutdownSequenceComplete) {
|
2022-04-10 01:35:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:02:38 +00:00
|
|
|
Game::shouldShutdown = true;
|
2022-04-10 01:35:41 +00:00
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Zone (%i) instance (%i) shutting down outside of main loop!", Game::server->GetZoneID(), instanceID);
|
2022-04-10 01:35:41 +00:00
|
|
|
WorldShutdownProcess(Game::server->GetZoneID());
|
|
|
|
FinalizeShutdown();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-04-10 01:35:41 +00:00
|
|
|
|
|
|
|
void FinalizeShutdown() {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("WorldServer", "Shutdown complete, zone (%i), instance (%i)", Game::server->GetZoneID(), instanceID);
|
2022-04-10 01:35:41 +00:00
|
|
|
|
2022-12-15 14:13:49 +00:00
|
|
|
//Delete our objects here:
|
2022-04-10 01:35:41 +00:00
|
|
|
Metrics::Clear();
|
|
|
|
Database::Destroy("WorldServer");
|
2022-12-15 14:13:49 +00:00
|
|
|
if (Game::chatFilter) delete Game::chatFilter;
|
|
|
|
if (Game::server) delete Game::server;
|
|
|
|
if (Game::logger) delete Game::logger;
|
|
|
|
if (Game::config) delete Game::config;
|
2022-04-10 01:35:41 +00:00
|
|
|
|
|
|
|
worldShutdownSequenceComplete = true;
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendShutdownMessageToMaster() {
|
|
|
|
CBITSTREAM;
|
2023-05-03 21:38:32 +00:00
|
|
|
PacketUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::SHUTDOWN_RESPONSE);
|
2022-04-10 01:35:41 +00:00
|
|
|
Game::server->SendToMaster(&bitStream);
|
2022-07-27 06:52:53 +00:00
|
|
|
}
|