From a7993bfc109f09b102eb9160f31dcc9d76b0900e Mon Sep 17 00:00:00 2001 From: dongresource Date: Wed, 13 Sep 2023 22:14:31 +0200 Subject: [PATCH] Replace all instances of time_t with int64_t This solves all of our problems with 32-bit time_t on 32-bit Windows. Only had to revert a single occurance in timeStampToStruct() in src/main.cpp to match the args of localtime(). Invocation: find src/ -type f | xargs sed -i 's/time_t/int64_t/g' --- src/Abilities.cpp | 2 +- src/Combat.cpp | 10 +++++----- src/Combat.hpp | 2 +- src/Eggs.cpp | 10 +++++----- src/Eggs.hpp | 2 +- src/Entities.hpp | 6 +++--- src/Items.cpp | 2 +- src/MobAI.cpp | 20 ++++++++++---------- src/MobAI.hpp | 18 +++++++++--------- src/NPCManager.cpp | 2 +- src/Player.hpp | 4 ++-- src/Racing.hpp | 2 +- src/Transport.cpp | 2 +- src/core/CNProtocol.hpp | 8 ++++---- src/core/CNShared.cpp | 2 +- src/core/CNShared.hpp | 4 ++-- src/core/CNStructs.hpp | 4 ++-- src/db/Database.hpp | 2 +- src/main.cpp | 8 ++++---- src/servers/CNLoginServer.cpp | 4 ++-- src/servers/CNLoginServer.hpp | 2 +- src/servers/CNShardServer.cpp | 6 +++--- src/servers/CNShardServer.hpp | 4 ++-- src/servers/Monitor.cpp | 2 +- src/settings.cpp | 2 +- src/settings.hpp | 2 +- 26 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/Abilities.cpp b/src/Abilities.cpp index baec351..ce0a30c 100644 --- a/src/Abilities.cpp +++ b/src/Abilities.cpp @@ -546,7 +546,7 @@ bool doDamageNDebuff(Mob *mob, sSkillResult_Damage_N_Debuff *respdata, int i, in respdata[i].bProtected = 0; std::pair key = std::make_pair(sock, bitFlag); - time_t until = getTime() + (time_t)duration * 100; + int64_t until = getTime() + (int64_t)duration * 100; Eggs::EggBuffs[key] = until; } respdata[i].iConditionBitFlag = plr->iConditionBitFlag; diff --git a/src/Combat.cpp b/src/Combat.cpp index d14a794..d38c494 100644 --- a/src/Combat.cpp +++ b/src/Combat.cpp @@ -58,7 +58,7 @@ static std::pair getDamage(int attackPower, int defensePower, bool shou static bool checkRapidFire(CNSocket *sock, int targetCount) { Player *plr = PlayerManager::getPlayer(sock); - time_t currTime = getTime(); + int64_t currTime = getTime(); if (currTime - plr->lastShot < plr->fireRate * 80) plr->suspicionRating += plr->fireRate * 100 + plr->lastShot - currTime; // gain suspicion for rapid firing @@ -158,7 +158,7 @@ static void pcAttackNpcs(CNSocket *sock, CNPacketData *data) { PlayerManager::sendToViewable(sock, respbuf, P_FE2CL_PC_ATTACK_NPCs); } -void Combat::npcAttackPc(Mob *mob, time_t currTime) { +void Combat::npcAttackPc(Mob *mob, int64_t currTime) { Player *plr = PlayerManager::getPlayer(mob->target); INITVARPACKET(respbuf, sP_FE2CL_NPC_ATTACK_PCs, pkt, sAttackResult, atk); @@ -654,7 +654,7 @@ static void projectileHit(CNSocket* sock, CNPacketData* data) { } // rapid fire anti-cheat - time_t currTime = getTime(); + int64_t currTime = getTime(); if (currTime - plr->lastShot < plr->fireRate * 80) plr->suspicionRating += plr->fireRate * 100 + plr->lastShot - currTime; // gain suspicion for rapid firing else if (currTime - plr->lastShot < plr->fireRate * 180 && plr->suspicionRating > 0) @@ -726,8 +726,8 @@ static void projectileHit(CNSocket* sock, CNPacketData* data) { Bullets[plr->iID].erase(resp->iBulletID); } -static void playerTick(CNServer *serv, time_t currTime) { - static time_t lastHealTime = 0; +static void playerTick(CNServer *serv, int64_t currTime) { + static int64_t lastHealTime = 0; for (auto& pair : PlayerManager::players) { CNSocket *sock = pair.first; diff --git a/src/Combat.hpp b/src/Combat.hpp index f7ee0f9..fe96981 100644 --- a/src/Combat.hpp +++ b/src/Combat.hpp @@ -23,7 +23,7 @@ namespace Combat { void init(); - void npcAttackPc(Mob *mob, time_t currTime); + void npcAttackPc(Mob *mob, int64_t currTime); int hitMob(CNSocket *sock, Mob *mob, int damage); void killMob(CNSocket *sock, Mob *mob); } diff --git a/src/Eggs.cpp b/src/Eggs.cpp index 683582d..2f01827 100644 --- a/src/Eggs.cpp +++ b/src/Eggs.cpp @@ -11,7 +11,7 @@ using namespace Eggs; /// sock, CBFlag -> until -std::map, time_t> Eggs::EggBuffs; +std::map, int64_t> Eggs::EggBuffs; std::unordered_map Eggs::EggTypes; int Eggs::eggBuffPlayer(CNSocket* sock, int skillId, int eggId, int duration) { @@ -79,15 +79,15 @@ int Eggs::eggBuffPlayer(CNSocket* sock, int skillId, int eggId, int duration) { // save the buff serverside; // if you get the same buff again, new duration will override the previous one - time_t until = getTime() + (time_t)duration * 1000; + int64_t until = getTime() + (int64_t)duration * 1000; EggBuffs[key] = until; return 0; } -static void eggStep(CNServer* serv, time_t currTime) { +static void eggStep(CNServer* serv, int64_t currTime) { // tick buffs - time_t timeStamp = currTime; + int64_t timeStamp = currTime; auto it = EggBuffs.begin(); while (it != EggBuffs.end()) { // check remaining time @@ -254,7 +254,7 @@ static void eggPickup(CNSocket* sock, CNPacketData* data) { else { Chunking::removeEntityFromChunks(Chunking::getViewableChunks(egg->chunkPos), eggRef); egg->dead = true; - egg->deadUntil = getTime() + (time_t)type->regen * 1000; + egg->deadUntil = getTime() + (int64_t)type->regen * 1000; egg->appearanceData.iHP = 0; } } diff --git a/src/Eggs.hpp b/src/Eggs.hpp index 2155272..fdc08be 100644 --- a/src/Eggs.hpp +++ b/src/Eggs.hpp @@ -11,7 +11,7 @@ struct EggType { }; namespace Eggs { - extern std::map, time_t> EggBuffs; + extern std::map, int64_t> EggBuffs; extern std::unordered_map EggTypes; void init(); diff --git a/src/Entities.hpp b/src/Entities.hpp index 4975279..69a20ad 100644 --- a/src/Entities.hpp +++ b/src/Entities.hpp @@ -103,14 +103,14 @@ struct CombatNPC : public BaseNPC { int level = 0; int speed = 300; - void (*_stepAI)(CombatNPC*, time_t) = nullptr; + void (*_stepAI)(CombatNPC*, int64_t) = nullptr; // XXX CombatNPC(int x, int y, int z, int angle, uint64_t iID, int t, int id, int maxHP) : BaseNPC(x, y, z, angle, iID, t, id), maxHealth(maxHP) {} - virtual void stepAI(time_t currTime) { + virtual void stepAI(int64_t currTime) { if (_stepAI != nullptr) _stepAI(this, currTime); } @@ -124,7 +124,7 @@ struct CombatNPC : public BaseNPC { struct Egg : public BaseNPC { bool summoned = false; bool dead = false; - time_t deadUntil; + int64_t deadUntil; Egg(int x, int y, int z, uint64_t iID, int t, int32_t id, bool summon) : BaseNPC(x, y, z, 0, iID, t, id) { diff --git a/src/Items.cpp b/src/Items.cpp index a3618dc..d27e260 100644 --- a/src/Items.cpp +++ b/src/Items.cpp @@ -501,7 +501,7 @@ static void itemUseHandler(CNSocket* sock, CNPacketData* data) { player->Inven[resp->iSlotNum] = resp->RemainItem; std::pair key = std::make_pair(sock, value1); - time_t until = getTime() + (time_t)Nanos::SkillTable[144].durationTime[0] * 100; + int64_t until = getTime() + (int64_t)Nanos::SkillTable[144].durationTime[0] * 100; Eggs::EggBuffs[key] = until; } diff --git a/src/MobAI.cpp b/src/MobAI.cpp index c31e19c..0df0b48 100644 --- a/src/MobAI.cpp +++ b/src/MobAI.cpp @@ -14,7 +14,7 @@ using namespace MobAI; bool MobAI::simulateMobs = settings::SIMULATEMOBS; -static void roamingStep(Mob *mob, time_t currTime); +static void roamingStep(Mob *mob, int64_t currTime); /* * Dynamic lerp; distinct from Transport::lerp(). This one doesn't care about height and @@ -119,7 +119,7 @@ void MobAI::groupRetreat(Mob *mob) { * Even if they're in range, we can't assume they're all in the same one chunk * as the mob, since it might be near a chunk boundary. */ -bool MobAI::aggroCheck(Mob *mob, time_t currTime) { +bool MobAI::aggroCheck(Mob *mob, int64_t currTime) { CNSocket *closest = nullptr; int closestDistance = INT_MAX; @@ -281,7 +281,7 @@ static void dealCorruption(Mob *mob, std::vector targetData, int skillID, i NPCManager::sendToViewable(mob, (void*)&respbuf, P_FE2CL_NPC_SKILL_CORRUPTION_HIT, resplen); } -static void useAbilities(Mob *mob, time_t currTime) { +static void useAbilities(Mob *mob, int64_t currTime) { /* * targetData approach * first integer is the count @@ -441,7 +441,7 @@ static void drainMobHP(Mob *mob, int amount) { Combat::killMob(mob->target, mob); } -static void deadStep(Mob *mob, time_t currTime) { +static void deadStep(Mob *mob, int64_t currTime) { // despawn the mob after a short delay if (mob->killedTime != 0 && !mob->despawned && currTime - mob->killedTime > 2000) { mob->despawned = true; @@ -500,7 +500,7 @@ static void deadStep(Mob *mob, time_t currTime) { NPCManager::sendToViewable(mob, &pkt, P_FE2CL_NPC_NEW, sizeof(sP_FE2CL_NPC_NEW)); } -static void combatStep(Mob *mob, time_t currTime) { +static void combatStep(Mob *mob, int64_t currTime) { assert(mob->target != nullptr); // lose aggro if the player lost connection @@ -542,7 +542,7 @@ static void combatStep(Mob *mob, time_t currTime) { return; // unbuffing - std::unordered_map::iterator it = mob->unbuffTimes.begin(); + std::unordered_map::iterator it = mob->unbuffTimes.begin(); while (it != mob->unbuffTimes.end()) { if (currTime >= it->second) { @@ -640,7 +640,7 @@ static void combatStep(Mob *mob, time_t currTime) { } } -void MobAI::incNextMovement(Mob *mob, time_t currTime) { +void MobAI::incNextMovement(Mob *mob, int64_t currTime) { if (currTime == 0) currTime = getTime(); @@ -648,7 +648,7 @@ void MobAI::incNextMovement(Mob *mob, time_t currTime) { mob->nextMovement = currTime + delay/2 + Rand::rand(delay/2); } -static void roamingStep(Mob *mob, time_t currTime) { +static void roamingStep(Mob *mob, int64_t currTime) { /* * We reuse nextAttack to avoid scanning for players all the time, but to still * do so more often than if we waited for nextMovement (which is way too slow). @@ -736,7 +736,7 @@ static void roamingStep(Mob *mob, time_t currTime) { } } -static void retreatStep(Mob *mob, time_t currTime) { +static void retreatStep(Mob *mob, int64_t currTime) { if (mob->nextMovement != 0 && currTime < mob->nextMovement) return; @@ -781,7 +781,7 @@ static void retreatStep(Mob *mob, time_t currTime) { } } -void MobAI::step(CombatNPC *npc, time_t currTime) { +void MobAI::step(CombatNPC *npc, int64_t currTime) { assert(npc->type == EntityType::MOB); auto mob = (Mob*)npc; diff --git a/src/MobAI.hpp b/src/MobAI.hpp index c526561..de852f3 100644 --- a/src/MobAI.hpp +++ b/src/MobAI.hpp @@ -13,32 +13,32 @@ enum class MobState { namespace MobAI { // needs to be declared before Mob's constructor - void step(CombatNPC*, time_t); + void step(CombatNPC*, int64_t); }; struct Mob : public CombatNPC { // general MobState state = MobState::INACTIVE; - std::unordered_map unbuffTimes = {}; + std::unordered_map unbuffTimes = {}; // dead - time_t killedTime = 0; - time_t regenTime = 0; + int64_t killedTime = 0; + int64_t regenTime = 0; bool summoned = false; bool despawned = false; // for the sake of death animations // roaming int idleRange = 0; const int sightRange = 0; - time_t nextMovement = 0; + int64_t nextMovement = 0; bool staticPath = false; int roamX = 0, roamY = 0, roamZ = 0; // combat CNSocket *target = nullptr; - time_t nextAttack = 0; - time_t lastDrainTime = 0; + int64_t nextAttack = 0; + int64_t lastDrainTime = 0; int skillStyle = -1; // -1 for nothing, 0-2 for corruption, -2 for eruption int hitX = 0, hitY = 0, hitZ = 0; // for use in ability targeting @@ -98,8 +98,8 @@ namespace MobAI { extern bool simulateMobs; // TODO: make this internal later - void incNextMovement(Mob *mob, time_t currTime=0); - bool aggroCheck(Mob *mob, time_t currTime); + void incNextMovement(Mob *mob, int64_t currTime=0); + bool aggroCheck(Mob *mob, int64_t currTime); void clearDebuff(Mob *mob); void followToCombat(Mob *mob); void groupRetreat(Mob *mob); diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 901bfb3..757283c 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -350,7 +350,7 @@ void NPCManager::queueNPCRemoval(int32_t id) { RemovalQueue.push(id); } -static void step(CNServer *serv, time_t currTime) { +static void step(CNServer *serv, int64_t currTime) { for (auto& pair : NPCs) { if (pair.second->type != EntityType::COMBAT_NPC && pair.second->type != EntityType::MOB) continue; diff --git a/src/Player.hpp b/src/Player.hpp index baa77b1..5ad73bb 100644 --- a/src/Player.hpp +++ b/src/Player.hpp @@ -79,10 +79,10 @@ struct Player : public Entity { bool isBuddyBlocked[50] = {}; uint64_t iFirstUseFlag[2] = {}; - time_t lastHeartbeat = 0; + int64_t lastHeartbeat = 0; int suspicionRating = 0; - time_t lastShot = 0; + int64_t lastShot = 0; std::vector buyback = {}; Player() { type = EntityType::PLAYER; } diff --git a/src/Racing.hpp b/src/Racing.hpp index f6e6acb..f59dc89 100644 --- a/src/Racing.hpp +++ b/src/Racing.hpp @@ -11,7 +11,7 @@ struct EPInfo { struct EPRace { std::set collectedRings; int mode, ticketSlot; - time_t startTime; + int64_t startTime; }; namespace Racing { diff --git a/src/Transport.cpp b/src/Transport.cpp index 0fe4f10..0ee7572 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -319,7 +319,7 @@ static void stepNPCPathing() { } } -static void tickTransportationSystem(CNServer* serv, time_t currTime) { +static void tickTransportationSystem(CNServer* serv, int64_t currTime) { stepNPCPathing(); stepSkywaySystem(); } diff --git a/src/core/CNProtocol.hpp b/src/core/CNProtocol.hpp index 782190f..4390380 100644 --- a/src/core/CNProtocol.hpp +++ b/src/core/CNProtocol.hpp @@ -208,15 +208,15 @@ public: }; class CNServer; -typedef void (*TimerHandler)(CNServer* serv, time_t time); +typedef void (*TimerHandler)(CNServer* serv, int64_t time); // timer struct struct TimerEvent { TimerHandler handlr; - time_t delta; // time to be added to the current time on reset - time_t scheduledEvent; // time to call handlr() + int64_t delta; // time to be added to the current time on reset + int64_t scheduledEvent; // time to call handlr() - TimerEvent(TimerHandler h, time_t d): handlr(h), delta(d) { + TimerEvent(TimerHandler h, int64_t d): handlr(h), delta(d) { scheduledEvent = 0; } }; diff --git a/src/core/CNShared.cpp b/src/core/CNShared.cpp index 98c2f18..16f1a9e 100644 --- a/src/core/CNShared.cpp +++ b/src/core/CNShared.cpp @@ -24,7 +24,7 @@ LoginMetadata* CNShared::getLoginMetadata(int64_t sk) { return lm; } -void CNShared::pruneLoginMetadata(CNServer *serv, time_t currTime) { +void CNShared::pruneLoginMetadata(CNServer *serv, int64_t currTime) { std::lock_guard lock(mtx); auto it = logins.begin(); diff --git a/src/core/CNShared.hpp b/src/core/CNShared.hpp index 8fa291c..f9d05a5 100644 --- a/src/core/CNShared.hpp +++ b/src/core/CNShared.hpp @@ -19,11 +19,11 @@ struct LoginMetadata { uint64_t FEKey; int32_t playerId; - time_t timestamp; + int64_t timestamp; }; namespace CNShared { void storeLoginMetadata(int64_t sk, LoginMetadata *lm); LoginMetadata* getLoginMetadata(int64_t sk); - void pruneLoginMetadata(CNServer *serv, time_t currTime); + void pruneLoginMetadata(CNServer *serv, int64_t currTime); } diff --git a/src/core/CNStructs.hpp b/src/core/CNStructs.hpp index fbadff2..f2f8f07 100644 --- a/src/core/CNStructs.hpp +++ b/src/core/CNStructs.hpp @@ -45,8 +45,8 @@ std::string U16toU8(char16_t* src, size_t max); size_t U8toU16(std::string src, char16_t* des, size_t max); // returns number of char16_t that was written at des -time_t getTime(); -time_t getTimestamp(); +int64_t getTime(); +int64_t getTimestamp(); void terminate(int); // The PROTOCOL_VERSION definition is defined by the build system. diff --git a/src/db/Database.hpp b/src/db/Database.hpp index 48fd0f6..e0778a4 100644 --- a/src/db/Database.hpp +++ b/src/db/Database.hpp @@ -13,7 +13,7 @@ namespace Database { int AccountID; std::string Password; int Selected; - time_t BannedUntil; + int64_t BannedUntil; std::string BanReason; }; diff --git a/src/main.cpp b/src/main.cpp index 288e537..448a1eb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -201,21 +201,21 @@ size_t U8toU16(std::string src, char16_t* des, size_t max) { return tmp.length(); } -time_t getTime() { +int64_t getTime() { using namespace std::chrono; milliseconds value = duration_cast((time_point_cast(steady_clock::now())).time_since_epoch()); - return (time_t)value.count(); + return (int64_t)value.count(); } // returns system time in seconds -time_t getTimestamp() { +int64_t getTimestamp() { using namespace std::chrono; seconds value = duration_cast((time_point_cast(system_clock::now())).time_since_epoch()); - return (time_t)value.count(); + return (int64_t)value.count(); } // convert integer timestamp (in s) to FF systime struct diff --git a/src/servers/CNLoginServer.cpp b/src/servers/CNLoginServer.cpp index 41e4b04..2a4592f 100644 --- a/src/servers/CNLoginServer.cpp +++ b/src/servers/CNLoginServer.cpp @@ -574,8 +574,8 @@ void CNLoginServer::killConnection(CNSocket* cns) { } void CNLoginServer::onStep() { - time_t currTime = getTime(); - static time_t lastCheck = 0; + int64_t currTime = getTime(); + static int64_t lastCheck = 0; if (currTime - lastCheck < 16000) return; diff --git a/src/servers/CNLoginServer.hpp b/src/servers/CNLoginServer.hpp index 6547b44..8eba753 100644 --- a/src/servers/CNLoginServer.hpp +++ b/src/servers/CNLoginServer.hpp @@ -7,7 +7,7 @@ struct CNLoginData { int userID; - time_t lastHeartbeat; + int64_t lastHeartbeat; }; enum class LoginError { diff --git a/src/servers/CNShardServer.cpp b/src/servers/CNShardServer.cpp index c56657e..9491d7b 100644 --- a/src/servers/CNShardServer.cpp +++ b/src/servers/CNShardServer.cpp @@ -59,7 +59,7 @@ void CNShardServer::handlePacket(CNSocket* sock, CNPacketData* data) { PlayerManager::players[sock]->lastHeartbeat = getTime(); } -void CNShardServer::keepAliveTimer(CNServer* serv, time_t currTime) { +void CNShardServer::keepAliveTimer(CNServer* serv, int64_t currTime) { for (auto& pair : PlayerManager::players) { if (pair.second->lastHeartbeat != 0 && currTime - pair.second->lastHeartbeat > settings::TIMEOUT) { // if the client hasn't responded in 60 seconds, its a dead connection so throw it out @@ -72,7 +72,7 @@ void CNShardServer::keepAliveTimer(CNServer* serv, time_t currTime) { } } -void CNShardServer::periodicSaveTimer(CNServer* serv, time_t currTime) { +void CNShardServer::periodicSaveTimer(CNServer* serv, int64_t currTime) { if (PlayerManager::players.empty()) return; @@ -114,7 +114,7 @@ void CNShardServer::kill() { } void CNShardServer::onStep() { - time_t currTime = getTime(); + int64_t currTime = getTime(); // do not evaluate timers if the server is shutting down if (!active) diff --git a/src/servers/CNShardServer.hpp b/src/servers/CNShardServer.hpp index a16f38a..6d60e65 100644 --- a/src/servers/CNShardServer.hpp +++ b/src/servers/CNShardServer.hpp @@ -11,8 +11,8 @@ class CNShardServer : public CNServer { private: static void handlePacket(CNSocket* sock, CNPacketData* data); - static void keepAliveTimer(CNServer*, time_t); - static void periodicSaveTimer(CNServer* serv, time_t currTime); + static void keepAliveTimer(CNServer*, int64_t); + static void periodicSaveTimer(CNServer* serv, int64_t currTime); public: static std::map ShardPackets; diff --git a/src/servers/Monitor.cpp b/src/servers/Monitor.cpp index 4b7819f..7f1f05a 100644 --- a/src/servers/Monitor.cpp +++ b/src/servers/Monitor.cpp @@ -72,7 +72,7 @@ static int process_email(char *buff, std::string email) { return i; } -static void tick(CNServer *serv, time_t delta) { +static void tick(CNServer *serv, int64_t delta) { std::lock_guard lock(sockLock); char buff[BUFSIZE]; int n; diff --git a/src/settings.cpp b/src/settings.cpp index 14d8de4..f1f4194 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -17,7 +17,7 @@ int settings::DBSAVEINTERVAL = 240; int settings::SHARDPORT = 23001; std::string settings::SHARDSERVERIP = "127.0.0.1"; bool settings::LOCALHOSTWORKAROUND = true; -time_t settings::TIMEOUT = 60000; +int64_t settings::TIMEOUT = 60000; int settings::VIEWDISTANCE = 25600; bool settings::SIMULATEMOBS = true; bool settings::ANTICHEAT = true; diff --git a/src/settings.hpp b/src/settings.hpp index 1ea3a43..7573aea 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -11,7 +11,7 @@ namespace settings { extern std::string SHARDSERVERIP; extern bool LOCALHOSTWORKAROUND; extern bool ANTICHEAT; - extern time_t TIMEOUT; + extern int64_t TIMEOUT; extern int VIEWDISTANCE; extern bool SIMULATEMOBS; extern int SPAWN_X;