Update playerlist.cpp

This commit is contained in:
BenCat07 2018-03-03 11:53:55 +01:00 committed by GitHub
parent 9bffc05cd3
commit d505af9213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,225 +1,227 @@
/* /*
* playerlist.cpp * playerlist.cpp
* *
* Created on: Apr 11, 2017 * Created on: Apr 11, 2017
* Author: nullifiedcat * Author: nullifiedcat
*/ */
#include "playerlist.hpp" #include "playerlist.hpp"
#include "common.hpp" #include "common.hpp"
#include <stdint.h> #include <stdint.h>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
namespace playerlist namespace playerlist
{ {
std::unordered_map<unsigned, userdata> data{}; std::unordered_map<unsigned, userdata> data{};
const userdata null_data{}; const userdata null_data{};
rgba_t k_Colors[] = { colors::empty, colors::FromRGBA8(99, 226, 161, 255), rgba_t k_Colors[] = { colors::empty, colors::FromRGBA8(99, 226, 161, 255),
colors::FromRGBA8(226, 204, 99, 255), colors::FromRGBA8(226, 204, 99, 255),
colors::FromRGBA8(232, 134, 6, 255), colors::empty }; colors::FromRGBA8(232, 134, 6, 255), colors::empty };
bool ShouldSave(const userdata &data) bool ShouldSave(const userdata &data)
{ {
return data.color || (data.state != k_EState::DEFAULT); return data.color || (data.state != k_EState::DEFAULT);
} }
void Save() void Save()
{ {
DIR *cathook_directory = opendir(DATA_PATH); DIR *cathook_directory = opendir(DATA_PATH);
if (!cathook_directory) if (!cathook_directory)
{ {
logging::Info("[ERROR] cathook data directory doesn't exist! How did " logging::Info("[ERROR] cathook data directory doesn't exist! How did "
"the cheat even get injected?"); "the cheat even get injected?");
return; return;
} }
else else
closedir(cathook_directory); closedir(cathook_directory);
try try
{ {
std::ofstream file(DATA_PATH "/plist", std::ofstream file(DATA_PATH "/plist",
std::ios::out | std::ios::binary); std::ios::out | std::ios::binary);
file.write(reinterpret_cast<const char *>(&SERIALIZE_VERSION), file.write(reinterpret_cast<const char *>(&SERIALIZE_VERSION),
sizeof(SERIALIZE_VERSION)); sizeof(SERIALIZE_VERSION));
int size = 0; int size = 0;
for (const auto &item : data) for (const auto &item : data)
{ {
if (ShouldSave(item.second)) if (ShouldSave(item.second))
size++; size++;
} }
file.write(reinterpret_cast<const char *>(&size), sizeof(size)); file.write(reinterpret_cast<const char *>(&size), sizeof(size));
for (const auto &item : data) for (const auto &item : data)
{ {
if (!ShouldSave(item.second)) if (!ShouldSave(item.second))
continue; continue;
file.write(reinterpret_cast<const char *>(&item.first), file.write(reinterpret_cast<const char *>(&item.first),
sizeof(item.first)); sizeof(item.first));
file.write(reinterpret_cast<const char *>(&item.second), file.write(reinterpret_cast<const char *>(&item.second),
sizeof(item.second)); sizeof(item.second));
} }
file.close(); file.close();
logging::Info("Writing successful"); logging::Info("Writing successful");
} }
catch (std::exception &e) catch (std::exception &e)
{ {
logging::Info("Writing unsuccessful: %s", e.what()); logging::Info("Writing unsuccessful: %s", e.what());
} }
} }
void Load() void Load()
{ {
data.clear(); data.clear();
DIR *cathook_directory = opendir(DATA_PATH); DIR *cathook_directory = opendir(DATA_PATH);
if (!cathook_directory) if (!cathook_directory)
{ {
logging::Info("[ERROR] cathook data directory doesn't exist! How did " logging::Info("[ERROR] cathook data directory doesn't exist! How did "
"the cheat even get injected?"); "the cheat even get injected?");
return; return;
} }
else else
closedir(cathook_directory); closedir(cathook_directory);
try try
{ {
std::ifstream file(DATA_PATH "/plist", std::ios::in | std::ios::binary); std::ifstream file(DATA_PATH "/plist", std::ios::in | std::ios::binary);
int file_serialize = 0; int file_serialize = 0;
file.read(reinterpret_cast<char *>(&file_serialize), file.read(reinterpret_cast<char *>(&file_serialize),
sizeof(file_serialize)); sizeof(file_serialize));
if (file_serialize != SERIALIZE_VERSION) if (file_serialize != SERIALIZE_VERSION)
{ {
logging::Info( logging::Info(
"Outdated/corrupted playerlist file! Cannot load this."); "Outdated/corrupted playerlist file! Cannot load this.");
file.close(); file.close();
return; return;
} }
int count = 0; int count = 0;
file.read(reinterpret_cast<char *>(&count), sizeof(count)); file.read(reinterpret_cast<char *>(&count), sizeof(count));
logging::Info("Reading %i entries...", count); logging::Info("Reading %i entries...", count);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
int steamid; int steamid;
userdata udata; userdata udata;
file.read(reinterpret_cast<char *>(&steamid), sizeof(steamid)); file.read(reinterpret_cast<char *>(&steamid), sizeof(steamid));
file.read(reinterpret_cast<char *>(&udata), sizeof(udata)); file.read(reinterpret_cast<char *>(&udata), sizeof(udata));
data.emplace(steamid, udata); data.emplace(steamid, udata);
} }
file.close(); file.close();
logging::Info("Reading successful!"); logging::Info("Reading successful!");
} }
catch (std::exception &e) catch (std::exception &e)
{ {
logging::Info("Reading unsuccessful: %s", e.what()); logging::Info("Reading unsuccessful: %s", e.what());
} }
} }
rgba_t Color(unsigned steamid) rgba_t Color(unsigned steamid)
{ {
if (AccessData(steamid).state == k_EState::DEVELOPER) if (AccessData(steamid).state == k_EState::DEVELOPER)
return colors::RainbowCurrent(); return colors::RainbowCurrent();
if (AccessData(steamid).color.a) if (AccessData(steamid).state == k_EState::CAT)
{ return colors::RainbowCurrent();
return AccessData(steamid).color; if (AccessData(steamid).color.a)
} {
else return AccessData(steamid).color;
{ }
return k_Colors[static_cast<int>(AccessData(steamid).state)]; else
} {
} return k_Colors[static_cast<int>(AccessData(steamid).state)];
}
}
rgba_t Color(CachedEntity *player) rgba_t Color(CachedEntity *player)
{ {
if (CE_GOOD(player)) if (CE_GOOD(player))
return Color(player->player_info.friendsID); return Color(player->player_info.friendsID);
return colors::empty; return colors::empty;
} }
userdata &AccessData(unsigned steamid) userdata &AccessData(unsigned steamid)
{ {
try try
{ {
return data.at(steamid); return data.at(steamid);
} }
catch (std::out_of_range &oor) catch (std::out_of_range &oor)
{ {
data.emplace(steamid, userdata{}); data.emplace(steamid, userdata{});
return data.at(steamid); return data.at(steamid);
} }
} }
// Assume player is non-null // Assume player is non-null
userdata &AccessData(CachedEntity *player) userdata &AccessData(CachedEntity *player)
{ {
if (CE_GOOD(player)) if (CE_GOOD(player))
return AccessData(player->player_info.friendsID); return AccessData(player->player_info.friendsID);
return AccessData(0U); return AccessData(0U);
} }
bool IsDefault(unsigned steamid) bool IsDefault(unsigned steamid)
{ {
const userdata &data = AccessData(steamid); const userdata &data = AccessData(steamid);
return data.state == k_EState::DEFAULT && !data.color.a; return data.state == k_EState::DEFAULT && !data.color.a;
} }
bool IsDefault(CachedEntity *entity) bool IsDefault(CachedEntity *entity)
{ {
if (CE_GOOD(entity)) if (CE_GOOD(entity))
return IsDefault(entity->player_info.friendsID); return IsDefault(entity->player_info.friendsID);
return true; return true;
} }
CatCommand pl_save("pl_save", "Save playerlist", Save); CatCommand pl_save("pl_save", "Save playerlist", Save);
CatCommand pl_load("pl_load", "Load playerlist", Load); CatCommand pl_load("pl_load", "Load playerlist", Load);
CatCommand pl_set_state( CatCommand pl_set_state(
"pl_set_state", "pl_set_state",
"pl_set_state uniqueid state\nfor example pl_set_state 306902159 0", "pl_set_state uniqueid state\nfor example pl_set_state 306902159 0",
[](const CCommand &args) { [](const CCommand &args) {
if (args.ArgC() < 3) if (args.ArgC() < 3)
{ {
logging::Info("Invalid call"); logging::Info("Invalid call");
return; return;
} }
unsigned steamid = strtoul(args.Arg(1), nullptr, 10); unsigned steamid = strtoul(args.Arg(1), nullptr, 10);
k_EState state = k_EState state =
static_cast<k_EState>(strtol(args.Arg(2), nullptr, 10)); static_cast<k_EState>(strtol(args.Arg(2), nullptr, 10));
if (state < k_EState::DEFAULT || state > k_EState::STATE_LAST) if (state < k_EState::DEFAULT || state > k_EState::STATE_LAST)
state = k_EState::DEFAULT; state = k_EState::DEFAULT;
AccessData(steamid).state = state; AccessData(steamid).state = state;
logging::Info("Set %d to %d", steamid, state); logging::Info("Set %d to %d", steamid, state);
}); });
CatCommand pl_set_color("pl_set_color", "pl_set_color uniqueid r g b", CatCommand pl_set_color("pl_set_color", "pl_set_color uniqueid r g b",
[](const CCommand &args) { [](const CCommand &args) {
if (args.ArgC() < 5) if (args.ArgC() < 5)
{ {
logging::Info("Invalid call"); logging::Info("Invalid call");
return; return;
} }
unsigned steamid = unsigned steamid =
strtoul(args.Arg(1), nullptr, 10); strtoul(args.Arg(1), nullptr, 10);
int r = strtol(args.Arg(2), nullptr, 10); int r = strtol(args.Arg(2), nullptr, 10);
int g = strtol(args.Arg(3), nullptr, 10); int g = strtol(args.Arg(3), nullptr, 10);
int b = strtol(args.Arg(4), nullptr, 10); int b = strtol(args.Arg(4), nullptr, 10);
rgba_t color = colors::FromRGBA8(r, g, b, 255); rgba_t color = colors::FromRGBA8(r, g, b, 255);
AccessData(steamid).color = color; AccessData(steamid).color = color;
logging::Info("Changed %d's color", steamid); logging::Info("Changed %d's color", steamid);
}); });
CatCommand pl_info("pl_info", "pl_info uniqueid", [](const CCommand &args) { CatCommand pl_info("pl_info", "pl_info uniqueid", [](const CCommand &args) {
if (args.ArgC() < 2) if (args.ArgC() < 2)
{ {
logging::Info("Invalid call"); logging::Info("Invalid call");
return; return;
} }
unsigned steamid = strtoul(args.Arg(1), nullptr, 10); unsigned steamid = strtoul(args.Arg(1), nullptr, 10);
logging::Info("Data for %i: ", steamid); logging::Info("Data for %i: ", steamid);
logging::Info(" State: %i", AccessData(steamid).state); logging::Info(" State: %i", AccessData(steamid).state);
/*int clr = AccessData(steamid).color; /*int clr = AccessData(steamid).color;
if (clr) { if (clr) {
ConColorMsg(*reinterpret_cast<::Color*>(&clr), "[CUSTOM COLOR]\n"); ConColorMsg(*reinterpret_cast<::Color*>(&clr), "[CUSTOM COLOR]\n");
}*/ }*/
}); });
} }