generated from YimMenu/YimMenuV2
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Player Database, Presence Kick Blocking, Net Messages, and More (#151)
- Loading branch information
Showing
28 changed files
with
1,022 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
#include "PlayerDatabase.hpp" | ||
|
||
namespace YimMenu | ||
{ | ||
using json = nlohmann::json; | ||
|
||
PlayerDatabase::PlayerDatabase() : | ||
m_File(std::filesystem::path(std::getenv("appdata")) / "HorseMenu" / "database.json") | ||
{ | ||
Load(); | ||
|
||
g_PlayerDatabase = this; | ||
|
||
LOG(INFO) << "Player Database Initialized"; | ||
} | ||
|
||
PlayerDatabase::~PlayerDatabase() | ||
{ | ||
LOG(INFO) << "Player Database Uninitialized"; | ||
|
||
g_PlayerDatabase = nullptr; | ||
} | ||
|
||
void PlayerDatabase::Load() | ||
{ | ||
m_Selected = nullptr; | ||
if (!std::filesystem::exists(m_File)) | ||
{ | ||
LOG(VERBOSE) << "Player Database Doesn't Exist"; | ||
std::ofstream file(m_File, std::ios::out | std::ios::trunc); | ||
file << "{}" << std::endl; | ||
file.close(); | ||
|
||
if (!std::filesystem::exists(m_File)) | ||
{ | ||
LOG(VERBOSE) << "Player Database unable to open!"; | ||
} | ||
else | ||
LOG(VERBOSE) << "Player Database Reset!"; | ||
} | ||
|
||
std::ifstream fileStream(m_File); | ||
if (fileStream.is_open()) | ||
{ | ||
json jsonData; | ||
fileStream >> jsonData; | ||
fileStream.close(); | ||
|
||
for (auto& [key, value] : jsonData.items()) | ||
{ | ||
auto player = value.get<std::shared_ptr<persistent_player>>(); | ||
m_Data[std::stoll(key)] = player; | ||
} | ||
} | ||
} | ||
|
||
void PlayerDatabase::Save() | ||
{ | ||
json data; | ||
|
||
for (auto& [rid, player] : m_Data) | ||
{ | ||
data[std::to_string(rid)] = player; | ||
} | ||
|
||
std::ofstream fileStream(m_File); | ||
if (fileStream.is_open()) | ||
{ | ||
fileStream << std::setw(4) << data << std::endl; | ||
fileStream.close(); | ||
} | ||
else | ||
LOG(WARNING) << "Unable to save Player Database!"; | ||
} | ||
|
||
std::shared_ptr<persistent_player> PlayerDatabase::GetPlayer(uint64_t rid) | ||
{ | ||
auto it = m_Data.find(rid); | ||
if (it != m_Data.end()) | ||
{ | ||
return it->second; | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
void PlayerDatabase::AddPlayer(uint64_t rid, std::string name) | ||
{ | ||
if (GetPlayer(rid) == nullptr) | ||
{ | ||
auto player = std::make_shared<persistent_player>(); | ||
player->rid = rid; | ||
player->name = name; | ||
m_Data[rid] = player; | ||
Save(); | ||
} | ||
} | ||
|
||
std::shared_ptr<persistent_player> PlayerDatabase::GetOrCreatePlayer(uint64_t rid, std::string name) | ||
{ | ||
auto player = GetPlayer(rid); | ||
if (!player) | ||
{ | ||
player = std::make_shared<persistent_player>(); | ||
player->rid = rid; | ||
player->name = name; | ||
m_Data[rid] = player; | ||
Save(); | ||
} | ||
return player; | ||
} | ||
|
||
std::unordered_map<uint64_t, std::shared_ptr<persistent_player>>& PlayerDatabase::GetAllPlayers() | ||
{ | ||
return m_Data; | ||
} | ||
|
||
void PlayerDatabase::SetSelected(std::shared_ptr<persistent_player> player) | ||
{ | ||
m_Selected = player; | ||
} | ||
|
||
std::shared_ptr<persistent_player> PlayerDatabase::GetSelected() | ||
{ | ||
return m_Selected; | ||
} | ||
|
||
std::string PlayerDatabase::ConvertInfractionToDescription(int infraction) | ||
{ | ||
return InfractionDescriptions[static_cast<eInfraction>(infraction)]; | ||
} | ||
|
||
void PlayerDatabase::AddInfraction(std::shared_ptr<persistent_player> player, int infraction) | ||
{ | ||
player->infractions.insert((int)infraction); | ||
if (!player->is_modder) | ||
{ | ||
player->is_modder = true; | ||
} | ||
Save(); | ||
} | ||
|
||
void PlayerDatabase::RemoveRID(uint64_t rockstar_id) | ||
{ | ||
if (m_Selected && m_Selected->rid == rockstar_id) | ||
{ | ||
m_Selected = nullptr; | ||
} | ||
|
||
if (auto it = m_Data.find(rockstar_id); it != m_Data.end()) | ||
{ | ||
m_Data.erase(it); | ||
Save(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#pragma once | ||
#include "core/filemgr/File.hpp" | ||
#include "core/filemgr/FileMgr.hpp" | ||
#include "game/features/Features.hpp" | ||
#include "game/rdr/Player.hpp" | ||
|
||
#include <memory> | ||
#include <nlohmann/json.hpp> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
|
||
|
||
namespace nlohmann | ||
{ | ||
template<typename T> | ||
struct adl_serializer<std::shared_ptr<T>> | ||
{ | ||
static void to_json(json& j, const std::shared_ptr<T>& value) | ||
{ | ||
j = *value; | ||
} | ||
|
||
static void from_json(const json& j, std::shared_ptr<T>& value) | ||
{ | ||
value = std::make_shared<T>(); | ||
*value = j.get<T>(); | ||
} | ||
}; | ||
} | ||
|
||
namespace YimMenu | ||
{ | ||
struct persistent_player; | ||
|
||
class PlayerDatabase | ||
{ | ||
private: | ||
std::filesystem::path m_File; | ||
std::unordered_map<uint64_t /*rid*/, std::shared_ptr<persistent_player>> m_Data; | ||
std::shared_ptr<persistent_player> m_Selected = nullptr; | ||
|
||
public: | ||
PlayerDatabase(); | ||
~PlayerDatabase(); | ||
|
||
void Load(); | ||
void Save(); | ||
|
||
std::shared_ptr<persistent_player> GetPlayer(uint64_t rid); | ||
void AddPlayer(uint64_t rid, std::string name); | ||
std::shared_ptr<persistent_player> GetOrCreatePlayer(uint64_t rid, std::string name = "Unknown Player"); | ||
std::unordered_map<uint64_t, std::shared_ptr<persistent_player>>& GetAllPlayers(); | ||
void SetSelected(std::shared_ptr<persistent_player> player); | ||
std::shared_ptr<persistent_player> GetSelected(); | ||
std::string ConvertInfractionToDescription(int infraction); | ||
void AddInfraction(std::shared_ptr<persistent_player> player, int infraction); | ||
void RemoveRID(uint64_t rockstar_id); | ||
|
||
enum class eInfraction | ||
{ | ||
TRIED_CRASH_PLAYER, | ||
TRIED_KICK_PLAYER, | ||
REMOTE_NATIVE_CALL, | ||
TRIED_ATTACH, | ||
REMOTE_TELEPORT, | ||
}; | ||
|
||
std::unordered_map<eInfraction, std::string> InfractionDescriptions = { | ||
{eInfraction::TRIED_CRASH_PLAYER, "Tried to crash you!"}, | ||
{eInfraction::TRIED_KICK_PLAYER, "Tried to kick you!"}, | ||
{eInfraction::REMOTE_NATIVE_CALL, "Tried to call a native!"}, | ||
{eInfraction::TRIED_ATTACH, "Tried to attach to you!"}, | ||
{eInfraction::REMOTE_TELEPORT, "Tried to teleport to you!"}, | ||
}; | ||
}; | ||
|
||
struct persistent_player | ||
{ | ||
uint64_t rid; | ||
std::string name; | ||
bool is_modder = false; | ||
bool is_admin = false; | ||
bool block_join = false; | ||
bool trust = false; | ||
std::unordered_set<int> infractions; | ||
|
||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(persistent_player, rid, name, is_modder, is_admin, block_join, trust, infractions); | ||
}; | ||
|
||
|
||
inline PlayerDatabase* g_PlayerDatabase; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.