Skip to content

Commit

Permalink
chore: Move static Player functions and internal linkage to manager c…
Browse files Browse the repository at this point in the history
…lass (#1412)

* Moving and organizing Player code

- Move code to CharacterComponent
- Remove extraneous interfaces
- Simplify some code greatly
- Change some types to return and take in const ref (only structs larger than 8 bytes benefit from this change.)
- Update code to use CharacterComponent for sending to zone instead of Player*.

* Moving and organizing Player code

- Move code to CharacterComponent
- Remove extraneous interfaces
- Simplify some code greatly
- Change some types to return and take in const ref (only structs larger than 8 bytes benefit from this change.)
- Update code to use CharacterComponent for sending to zone instead of Player*.
- Remove static storage container (static containers can be destroyed before exit/terminate handler executes)

* remove player cast

* Remove extra includes

* Add a player manager

Used for the static Player functions.  Further removes stuff from the Player class/file.
  • Loading branch information
EmosewaMC authored Jan 13, 2024
1 parent 0bc1214 commit a62f6d6
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 86 deletions.
1 change: 1 addition & 0 deletions dGame/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(DGAME_SOURCES "Character.cpp"
"EntityManager.cpp"
"LeaderboardManager.cpp"
"Player.cpp"
"PlayerManager.cpp"
"TeamManager.cpp"
"TradingManager.cpp"
"User.cpp"
Expand Down
13 changes: 7 additions & 6 deletions dGame/EntityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "eGameMasterLevel.h"
#include "eReplicaComponentType.h"
#include "eReplicaPacketType.h"
#include "PlayerManager.h"

// Configure which zones have ghosting disabled, mostly small worlds.
std::vector<LWOMAPID> EntityManager::m_GhostingExcludedZones = {
Expand Down Expand Up @@ -187,7 +188,7 @@ void EntityManager::SerializeEntities() {
entity->WriteComponents(&stream, eReplicaPacketType::SERIALIZATION);

if (entity->GetIsGhostingCandidate()) {
for (auto* player : Player::GetAllPlayers()) {
for (auto* player : PlayerManager::GetAllPlayers()) {
if (player->IsObserved(toSerialize)) {
Game::server->Send(&stream, player->GetSystemAddress(), false);
}
Expand Down Expand Up @@ -376,7 +377,7 @@ void EntityManager::ConstructEntity(Entity* entity, const SystemAddress& sysAddr
if (skipChecks) {
Game::server->Send(&stream, UNASSIGNED_SYSTEM_ADDRESS, true);
} else {
for (auto* player : Player::GetAllPlayers()) {
for (auto* player : PlayerManager::GetAllPlayers()) {
if (player->GetPlayerReadyForUpdates()) {
Game::server->Send(&stream, player->GetSystemAddress(), false);
} else {
Expand Down Expand Up @@ -405,7 +406,7 @@ void EntityManager::ConstructAllEntities(const SystemAddress& sysAddr) {
}
}

UpdateGhosting(Player::GetPlayer(sysAddr));
UpdateGhosting(PlayerManager::GetPlayer(sysAddr));
}

void EntityManager::DestructEntity(Entity* entity, const SystemAddress& sysAddr) {
Expand All @@ -418,7 +419,7 @@ void EntityManager::DestructEntity(Entity* entity, const SystemAddress& sysAddr)

Game::server->Send(&stream, sysAddr, sysAddr == UNASSIGNED_SYSTEM_ADDRESS);

for (auto* player : Player::GetAllPlayers()) {
for (auto* player : PlayerManager::GetAllPlayers()) {
if (!player->GetPlayerReadyForUpdates()) {
player->RemoveLimboConstruction(entity->GetObjectID());
}
Expand Down Expand Up @@ -465,7 +466,7 @@ void EntityManager::QueueGhostUpdate(LWOOBJID playerID) {

void EntityManager::UpdateGhosting() {
for (const auto playerID : m_PlayersToUpdateGhosting) {
auto* player = Player::GetPlayer(playerID);
auto* player = PlayerManager::GetPlayer(playerID);

if (player == nullptr) {
continue;
Expand Down Expand Up @@ -548,7 +549,7 @@ void EntityManager::CheckGhosting(Entity* entity) {

const auto isAudioEmitter = entity->GetLOT() == 6368;

for (auto* player : Player::GetAllPlayers()) {
for (auto* player : PlayerManager::GetAllPlayers()) {
const auto& entityPoint = player->GetGhostReferencePoint();

const int32_t id = entity->GetObjectID();
Expand Down
57 changes: 7 additions & 50 deletions dGame/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@
#include "CppScripts.h"
#include "Loot.h"
#include "eReplicaComponentType.h"

namespace {
std::vector<Player*> m_Players;
};

const std::vector<Player*>& Player::GetAllPlayers() {
return m_Players;
}
#include "PlayerManager.h"

void Player::SetGhostReferencePoint(const NiPoint3& value) {
m_GhostReferencePoint = value;
Expand Down Expand Up @@ -63,11 +56,7 @@ Player::Player(const LWOOBJID& objectID, const EntityInfo info, User* user, Enti

m_Character->SetEntity(this);

const auto& iter = std::find(m_Players.begin(), m_Players.end(), this);

if (iter == m_Players.end()) {
m_Players.push_back(this);
}
PlayerManager::AddPlayer(this);
}

void Player::AddLimboConstruction(LWOOBJID objectId) {
Expand Down Expand Up @@ -121,38 +110,6 @@ void Player::GhostEntity(int32_t id) {
}
}

Player* Player::GetPlayer(const SystemAddress& sysAddr) {
auto* entity = UserManager::Instance()->GetUser(sysAddr)->GetLastUsedChar()->GetEntity();

return static_cast<Player*>(entity);
}

Player* Player::GetPlayer(const std::string& name) {
const auto characters = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::CHARACTER);

Player* player = nullptr;
for (auto* character : characters) {
if (!character->IsPlayer()) continue;

if (GeneralUtils::CaseInsensitiveStringCompare(name, character->GetCharacter()->GetName())) {
player = dynamic_cast<Player*>(character);
}
}

return player;
}

Player* Player::GetPlayer(LWOOBJID playerID) {
Player* playerToReturn = nullptr;
for (auto* player : m_Players) {
if (player->GetObjectID() == playerID) {
playerToReturn = player;
}
}

return playerToReturn;
}

Player::~Player() {
LOG("Deleted player");

Expand All @@ -166,10 +123,10 @@ Player::~Player() {
}

m_LimboConstructions.clear();

const auto iter = std::find(m_Players.begin(), m_Players.end(), this);

if (iter == m_Players.end()) {
// Make sure the player exists first. Remove afterwards to prevent the OnPlayerExist functions from not being able to find the player.
if (!PlayerManager::GetPlayer(GetObjectID())) {
LOG("Unable to find player to remove from manager.");
return;
}

Expand All @@ -189,5 +146,5 @@ Player::~Player() {
}
}

m_Players.erase(iter);
PlayerManager::RemovePlayer(this);
}
12 changes: 0 additions & 12 deletions dGame/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,6 @@ class Player final : public Entity

void GhostEntity(const int32_t id);

/**
* Static methods
*/

static Player* GetPlayer(const SystemAddress& sysAddr);

static Player* GetPlayer(const std::string& name);

static Player* GetPlayer(LWOOBJID playerID);

static const std::vector<Player*>& GetAllPlayers();

~Player() override;
private:
SystemAddress m_SystemAddress;
Expand Down
65 changes: 65 additions & 0 deletions dGame/PlayerManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "PlayerManager.h"

#include "Character.h"
#include "Player.h"
#include "User.h"
#include "UserManager.h"
#include "eReplicaComponentType.h"

namespace {
std::vector<Player*> m_Players;
};

const std::vector<Player*>& PlayerManager::GetAllPlayers() {
return m_Players;
}

void PlayerManager::AddPlayer(Player* player) {
const auto& iter = std::find(m_Players.begin(), m_Players.end(), player);

if (iter == m_Players.end()) {
m_Players.push_back(player);
}
}

void PlayerManager::RemovePlayer(Player* player) {
const auto iter = std::find(m_Players.begin(), m_Players.end(), player);

if (iter != m_Players.end()) {
m_Players.erase(iter);
}
}

Player* PlayerManager::GetPlayer(const SystemAddress& sysAddr) {
auto* entity = UserManager::Instance()->GetUser(sysAddr)->GetLastUsedChar()->GetEntity();

return static_cast<Player*>(entity);
}

Player* PlayerManager::GetPlayer(const std::string& name) {
const auto characters = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::CHARACTER);

Player* player = nullptr;
for (auto* character : characters) {
if (!character->IsPlayer()) continue;

if (GeneralUtils::CaseInsensitiveStringCompare(name, character->GetCharacter()->GetName())) {
player = dynamic_cast<Player*>(character);
break;
}
}

return player;
}

Player* PlayerManager::GetPlayer(LWOOBJID playerID) {
Player* playerToReturn = nullptr;
for (auto* player : m_Players) {
if (player->GetObjectID() == playerID) {
playerToReturn = player;
break;
}
}

return playerToReturn;
}
25 changes: 25 additions & 0 deletions dGame/PlayerManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __PLAYERMANAGER__H__
#define __PLAYERMANAGER__H__

#include "dCommonVars.h"

#include <string>

class Player;
struct SystemAddress;

namespace PlayerManager {
void AddPlayer(Player* player);

void RemovePlayer(Player* player);

Player* GetPlayer(const SystemAddress& sysAddr);

Player* GetPlayer(const std::string& name);

Player* GetPlayer(LWOOBJID playerID);

const std::vector<Player*>& GetAllPlayers();
};

#endif //!__PLAYERMANAGER__H__
3 changes: 2 additions & 1 deletion dGame/dComponents/PropertyManagementComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "eMissionTaskType.h"
#include "eObjectBits.h"
#include "CharacterComponent.h"
#include "PlayerManager.h"

#include <vector>
#include "CppScripts.h"
Expand Down Expand Up @@ -227,7 +228,7 @@ void PropertyManagementComponent::OnStartBuilding() {

if (ownerEntity == nullptr) return;

const auto players = Player::GetAllPlayers();
const auto players = PlayerManager::GetAllPlayers();

LWOMAPID zoneId = 1100;

Expand Down
3 changes: 2 additions & 1 deletion dGame/dComponents/TriggerComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "QuickBuildComponent.h"
#include "SkillComponent.h"
#include "eEndBehavior.h"
#include "PlayerManager.h"


TriggerComponent::TriggerComponent(Entity* parent, const std::string triggerInfo): Component(parent) {
Expand Down Expand Up @@ -175,7 +176,7 @@ std::vector<Entity*> TriggerComponent::GatherTargets(LUTriggers::Command* comman
}
} else if (command->target == "objGroup") entities = Game::entityManager->GetEntitiesInGroup(command->targetName);
else if (command->target == "allPlayers") {
for (auto* player : Player::GetAllPlayers()) {
for (auto* player : PlayerManager::GetAllPlayers()) {
entities.push_back(player);
}
} else if (command->target == "allNPCs") { /*UNUSED*/ }
Expand Down
11 changes: 6 additions & 5 deletions dGame/dGameMessages/GameMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
#include "eGameMessageType.h"
#include "ePetAbilityType.h"
#include "ActivityManager.h"
#include "PlayerManager.h"

#include "CDComponentsRegistryTable.h"
#include "CDObjectsTable.h"
Expand Down Expand Up @@ -2693,7 +2694,7 @@ void GameMessages::HandlePropertyEntranceSync(RakNet::BitStream* inStream, Entit
filterText.push_back(c);
}

auto* player = Player::GetPlayer(sysAddr);
auto* player = PlayerManager::GetPlayer(sysAddr);

auto* entranceComponent = entity->GetComponent<PropertyEntranceComponent>();

Expand All @@ -2720,7 +2721,7 @@ void GameMessages::HandleEnterProperty(RakNet::BitStream* inStream, Entity* enti
inStream->Read(index);
inStream->Read(returnToZone);

auto* player = Player::GetPlayer(sysAddr);
auto* player = PlayerManager::GetPlayer(sysAddr);

auto* entranceComponent = entity->GetComponent<PropertyEntranceComponent>();
if (entranceComponent != nullptr) {
Expand Down Expand Up @@ -4601,7 +4602,7 @@ void GameMessages::HandleToggleGhostReferenceOverride(RakNet::BitStream* inStrea

inStream->Read(bOverride);

auto* player = Player::GetPlayer(sysAddr);
auto* player = PlayerManager::GetPlayer(sysAddr);

if (player != nullptr) {
player->SetGhostOverride(bOverride);
Expand All @@ -4616,7 +4617,7 @@ void GameMessages::HandleSetGhostReferencePosition(RakNet::BitStream* inStream,

inStream->Read(position);

auto* player = Player::GetPlayer(sysAddr);
auto* player = PlayerManager::GetPlayer(sysAddr);

if (player != nullptr) {
player->SetGhostOverridePoint(position);
Expand Down Expand Up @@ -4880,7 +4881,7 @@ void GameMessages::HandleFireEventServerSide(RakNet::BitStream* inStream, Entity
inStream->Read(senderID);

auto* sender = Game::entityManager->GetEntity(senderID);
auto* player = Player::GetPlayer(sysAddr);
auto* player = PlayerManager::GetPlayer(sysAddr);

if (!player) {
return;
Expand Down
3 changes: 2 additions & 1 deletion dGame/dUtilities/CheatDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "UserManager.h"
#include "dConfig.h"
#include <optional>
#include "PlayerManager.h"

Entity* GetPossessedEntity(const LWOOBJID& objId) {
auto* entity = Game::entityManager->GetEntity(objId);
Expand Down Expand Up @@ -49,7 +50,7 @@ void LogAndSaveFailedAntiCheatCheck(const LWOOBJID& id, const SystemAddress& sys
User* toReport = nullptr;
switch (checkType) {
case CheckType::Entity: {
auto* player = Player::GetPlayer(sysAddr);
auto* player = PlayerManager::GetPlayer(sysAddr);
auto* entity = GetPossessedEntity(id);

// If player exists and entity exists in world, use both for logging info.
Expand Down
Loading

0 comments on commit a62f6d6

Please sign in to comment.