Skip to content

Commit

Permalink
Player Database, Presence Kick Blocking, Net Messages, and More (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rxann authored Jun 25, 2024
1 parent 8025d4f commit d15a1de
Show file tree
Hide file tree
Showing 28 changed files with 1,022 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmake/rdr-classes.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include(FetchContent)
FetchContent_Declare(
rdr_classes
GIT_REPOSITORY https://github.com/YimMenu/RDR-Classes.git
GIT_TAG 70091e27d2e0e515f0e34cb1983ab965421dab0e
GIT_TAG 99bae26b377a1075172caf4842889d24f8344054
GIT_PROGRESS TRUE
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
Expand Down
3 changes: 3 additions & 0 deletions src/core/hooking/Hooking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ namespace YimMenu
BaseHook::Add<Hooks::Protections::ResetSyncNodes>(new DetourHook("ResetSyncNodes", Pointers.ResetSyncNodes, Hooks::Protections::ResetSyncNodes));
BaseHook::Add<Hooks::Protections::HandleScriptedGameEvent>(new DetourHook("HandleScriptedGameEvent", Pointers.HandleScriptedGameEvent, Hooks::Protections::HandleScriptedGameEvent));
BaseHook::Add<Hooks::Protections::AddObjectToCreationQueue>(new DetourHook("AddObjectToCreationQueue", Pointers.AddObjectToCreationQueue, Hooks::Protections::AddObjectToCreationQueue));
BaseHook::Add<Hooks::Protections::ReceiveNetMessage>(new DetourHook("ReceiveNetMessage", Pointers.ReceiveNetMessage, Hooks::Protections::ReceiveNetMessage));
BaseHook::Add<Hooks::Protections::HandlePresenceEvent>(new DetourHook("HandlePresenceEvent", Pointers.HandlePresenceEvent, Hooks::Protections::HandlePresenceEvent));
BaseHook::Add<Hooks::Protections::PPostMessage>(new DetourHook("PostMessage", Pointers.PostPresenceMessage, Hooks::Protections::PPostMessage));
BaseHook::Add<Hooks::Protections::SerializeServerRPC>(new DetourHook("SerializeServerRPC", Pointers.SerializeServerRPC, Hooks::Protections::SerializeServerRPC));
BaseHook::Add<Hooks::Protections::ReceiveServerMessage>(new DetourHook("ReceiveServerMessage", Pointers.ReceiveServerMessage, Hooks::Protections::ReceiveServerMessage));

Expand Down
156 changes: 156 additions & 0 deletions src/core/player_database/PlayerDatabase.cpp
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();
}
}
}
95 changes: 95 additions & 0 deletions src/core/player_database/PlayerDatabase.hpp
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;


}
1 change: 1 addition & 0 deletions src/game/features/self/OffTheRadar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace YimMenu::Features
{
//TODO: fix this
class OffTheRadar : public LoopedCommand
{
using LoopedCommand::LoopedCommand;
Expand Down
14 changes: 14 additions & 0 deletions src/game/frontend/submenus/Debug.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "Debug.hpp"

#include "core/filemgr/FileMgr.hpp"
#include "game/backend/FiberPool.hpp"
#include "game/features/features.hpp"
#include "game/frontend/items/Items.hpp"
#include "game/pointers/Pointers.hpp"
#include "game/rdr/Natives.hpp"
#include "game/rdr/ScriptGlobal.hpp"


Expand Down Expand Up @@ -271,7 +274,18 @@ namespace YimMenu::Submenus
debug->AddItem(std::make_shared<BoolCommandItem>("logevents"_J));
debug->AddItem(std::make_shared<BoolCommandItem>("logtses"_J));
debug->AddItem(std::make_shared<BoolCommandItem>("logmetrics"_J));
debug->AddItem(std::make_shared<BoolCommandItem>("logpackets"_J));
debug->AddItem(std::make_shared<BoolCommandItem>("logpresenceevents"_J));
debug->AddItem(std::make_shared<BoolCommandItem>("logpostmessage"_J));
debug->AddItem(std::make_shared<BoolCommandItem>("logservermessages"_J));
debug->AddItem(std::make_shared<ImGuiItem>([] {
if (ImGui::Button("Bail to Loading Screen"))
{
FiberPool::Push([] {
SCRIPTS::BAIL_TO_LANDING_PAGE(0);
});
}
}));
AddCategory(std::move(debug));
}
}
Loading

0 comments on commit d15a1de

Please sign in to comment.