mirror of
https://github.com/diamante0018/BlackOpsPlugin.git
synced 2025-04-20 18:35:42 +00:00
small refactor
This commit is contained in:
parent
af1c9f12d8
commit
3189442299
@ -16,25 +16,30 @@ utils::concurrency::container<client_list> mute_list;
|
|||||||
|
|
||||||
const game::dvar_t* sv_disableChat;
|
const game::dvar_t* sv_disableChat;
|
||||||
|
|
||||||
|
void send_message(const int client_num, const char* msg) {
|
||||||
|
game::Cbuf_AddText(game::LOCAL_CLIENT_0,
|
||||||
|
utils::string::va("tell %i \"%s\"\n", client_num, msg));
|
||||||
|
}
|
||||||
|
|
||||||
void mute_player(const game::client_s* client) {
|
void mute_player(const game::client_s* client) {
|
||||||
const auto xuid = client->xuid;
|
const auto xuid = client->xuid;
|
||||||
mute_list.access([&](client_list& clients) { clients.insert(xuid); });
|
mute_list.access([&xuid](client_list& clients) { clients.insert(xuid); });
|
||||||
|
|
||||||
|
send_message(client->gentity->entnum, "You were muted");
|
||||||
}
|
}
|
||||||
|
|
||||||
void unmute_player(const game::client_s* client) {
|
void unmute_player(const game::client_s* client) {
|
||||||
|
|
||||||
const auto xuid = client->xuid;
|
const auto xuid = client->xuid;
|
||||||
mute_list.access([&](client_list& clients) { clients.erase(xuid); });
|
mute_list.access([&xuid](client_list& clients) { clients.erase(xuid); });
|
||||||
|
|
||||||
game::SV_GameSendServerCommand(
|
send_message(client->gentity->entnum, "You were unmuted");
|
||||||
client->gentity->entnum, game::SV_CMD_CAN_IGNORE,
|
|
||||||
utils::string::va("%c \"You were unmuted\"", 0x65));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void client_command_stub(const int client_number) {
|
void client_command_stub(const int client_number) {
|
||||||
char buf[1024]{};
|
char buf[1024]{};
|
||||||
|
|
||||||
if (game::g_entities[client_number].client == nullptr) {
|
if (!game::g_entities[client_number].client) {
|
||||||
// Not in game
|
// Not in game
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -43,9 +48,7 @@ void client_command_stub(const int client_number) {
|
|||||||
|
|
||||||
if (utils::string::starts_with(buf, "say")) {
|
if (utils::string::starts_with(buf, "say")) {
|
||||||
if (sv_disableChat->current.enabled) {
|
if (sv_disableChat->current.enabled) {
|
||||||
game::SV_GameSendServerCommand(
|
send_message(client_number, "Text chat is disabled");
|
||||||
client_number, game::SV_CMD_CAN_IGNORE,
|
|
||||||
utils::string::va("%c \"Chat is disabled\"", 0x65));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,9 +58,7 @@ void client_command_stub(const int client_number) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (is_muted) {
|
if (is_muted) {
|
||||||
game::SV_GameSendServerCommand(
|
send_message(client_number, "You are muted");
|
||||||
client_number, game::SV_CMD_CAN_IGNORE,
|
|
||||||
utils::string::va("%c \"You are muted\"", 0x65));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,7 +70,7 @@ void client_command_stub(const int client_number) {
|
|||||||
class component final : public component_interface {
|
class component final : public component_interface {
|
||||||
public:
|
public:
|
||||||
void post_unpack() override {
|
void post_unpack() override {
|
||||||
client_command_hook.create(SELECT_VALUE(0x63DB70, 0x4AF770),
|
client_command_hook.create(game::select(0x63DB70, 0x4AF770),
|
||||||
client_command_stub);
|
client_command_stub);
|
||||||
add_chat_commands();
|
add_chat_commands();
|
||||||
|
|
||||||
@ -79,29 +80,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static void add_chat_commands() {
|
static void add_chat_commands() {
|
||||||
command::add("sayAs", [](const command::params_sv& params) {
|
command::add_sv("muteClient", [](const command::params_sv& params) {
|
||||||
if (params.size() < 3) {
|
|
||||||
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER,
|
|
||||||
"Usage: sayAs <client number> <message>\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto* client = game::SV_GetPlayerByNum();
|
|
||||||
|
|
||||||
if (client == nullptr)
|
|
||||||
return;
|
|
||||||
|
|
||||||
auto* const gentity = client->gentity;
|
|
||||||
assert(gentity != nullptr);
|
|
||||||
|
|
||||||
if (gentity->client == nullptr)
|
|
||||||
return;
|
|
||||||
|
|
||||||
const auto message = params.join(2);
|
|
||||||
game::G_Say(gentity, nullptr, 0, message.data());
|
|
||||||
});
|
|
||||||
|
|
||||||
command::add("muteClient", [](const command::params_sv& params) {
|
|
||||||
if (params.size() < 2) {
|
if (params.size() < 2) {
|
||||||
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER,
|
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER,
|
||||||
"Usage: %s <client number> : prevent the player from "
|
"Usage: %s <client number> : prevent the player from "
|
||||||
@ -111,19 +90,20 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
const auto* client = game::SV_GetPlayerByNum();
|
const auto* client = game::SV_GetPlayerByNum();
|
||||||
|
if (!client) {
|
||||||
if (client == nullptr)
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
assert(client->gentity != nullptr);
|
assert(client->gentity);
|
||||||
|
|
||||||
if (client->gentity->client == nullptr)
|
if (!client->gentity->client) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
mute_player(client);
|
mute_player(client);
|
||||||
});
|
});
|
||||||
|
|
||||||
command::add("unmute", [](const command::params_sv& params) {
|
command::add_sv("unmute", [](const command::params_sv& params) {
|
||||||
if (params.size() < 2) {
|
if (params.size() < 2) {
|
||||||
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER,
|
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER,
|
||||||
"Usage: %s <client number>\n", params.get(0));
|
"Usage: %s <client number>\n", params.get(0));
|
||||||
@ -131,18 +111,18 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
const auto* client = game::SV_GetPlayerByNum();
|
const auto* client = game::SV_GetPlayerByNum();
|
||||||
|
if (!client) {
|
||||||
if (client == nullptr) {
|
|
||||||
if (std::strcmp(params.get(1), "all") == 0) {
|
if (std::strcmp(params.get(1), "all") == 0) {
|
||||||
mute_list.access([&](client_list& clients) { clients.clear(); });
|
mute_list.access([](client_list& clients) { clients.clear(); });
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(client->gentity != nullptr);
|
assert(client->gentity);
|
||||||
|
|
||||||
if (client->gentity->client == nullptr)
|
if (!client->gentity->client) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
unmute_player(client);
|
unmute_player(client);
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#include <std_include.hpp>
|
#include <std_include.hpp>
|
||||||
#include "loader/component_loader.hpp"
|
|
||||||
|
|
||||||
#include <utils/string.hpp>
|
#include <utils/string.hpp>
|
||||||
#include <utils/nt.hpp>
|
#include <utils/nt.hpp>
|
||||||
@ -11,36 +10,6 @@ constexpr auto CMD_MAX_NESTING = 8;
|
|||||||
namespace command {
|
namespace command {
|
||||||
std::unordered_map<std::string, std::function<void(const params_sv&)>> handlers;
|
std::unordered_map<std::string, std::function<void(const params_sv&)>> handlers;
|
||||||
|
|
||||||
namespace {
|
|
||||||
void cmd_vstr_f() {
|
|
||||||
const params_sv params;
|
|
||||||
|
|
||||||
if (params.size() < 2) {
|
|
||||||
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER,
|
|
||||||
"vstr <variablename> : execute a variable command\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto* dvar_name = params.get(1);
|
|
||||||
const auto* dvar = game::Dvar_FindVar(dvar_name);
|
|
||||||
|
|
||||||
if (dvar == nullptr) {
|
|
||||||
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER, "%s doesn't exist\n",
|
|
||||||
dvar_name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dvar->type == game::DVAR_TYPE_STRING ||
|
|
||||||
dvar->type == game::DVAR_TYPE_ENUM) {
|
|
||||||
// Adds \n automatically
|
|
||||||
execute(dvar->current.string);
|
|
||||||
} else {
|
|
||||||
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER,
|
|
||||||
"%s is not a string-based dvar\n", dvar->name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void main_handler() {
|
void main_handler() {
|
||||||
params_sv params = {};
|
params_sv params = {};
|
||||||
|
|
||||||
@ -66,7 +35,7 @@ const char* params_sv::get(const int index) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string params_sv::join(const int index) const {
|
std::string params_sv::join(const int index) const {
|
||||||
std::string result = {};
|
std::string result;
|
||||||
|
|
||||||
for (auto i = index; i < this->size(); ++i) {
|
for (auto i = index; i < this->size(); ++i) {
|
||||||
if (i > index)
|
if (i > index)
|
||||||
@ -78,12 +47,15 @@ std::string params_sv::join(const int index) const {
|
|||||||
|
|
||||||
void add_raw(const char* name, void (*callback)()) {
|
void add_raw(const char* name, void (*callback)()) {
|
||||||
game::Cmd_AddCommandInternal(
|
game::Cmd_AddCommandInternal(
|
||||||
|
name, game::Cbuf_AddServerText_f,
|
||||||
|
utils::memory::get_allocator()->allocate<game::cmd_function_s>());
|
||||||
|
game::Cmd_AddServerCommandInternal(
|
||||||
name, callback,
|
name, callback,
|
||||||
utils::memory::get_allocator()->allocate<game::cmd_function_s>());
|
utils::memory::get_allocator()->allocate<game::cmd_function_s>());
|
||||||
}
|
}
|
||||||
|
|
||||||
void add(const char* name,
|
void add_sv(const char* name,
|
||||||
const std::function<void(const params_sv&)>& callback) {
|
const std::function<void(const params_sv&)>& callback) {
|
||||||
const auto command = utils::string::to_lower(name);
|
const auto command = utils::string::to_lower(name);
|
||||||
|
|
||||||
if (!handlers.contains(command)) {
|
if (!handlers.contains(command)) {
|
||||||
@ -102,31 +74,4 @@ void execute(std::string command, const bool sync) {
|
|||||||
game::Cbuf_AddText(game::LOCAL_CLIENT_0, command.data());
|
game::Cbuf_AddText(game::LOCAL_CLIENT_0, command.data());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class component final : public component_interface {
|
|
||||||
public:
|
|
||||||
void post_unpack() override { add_commands_generic(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
static void add_commands_generic() {
|
|
||||||
add("properQuit",
|
|
||||||
[](const params_sv&) { utils::nt::raise_hard_exception(); });
|
|
||||||
|
|
||||||
add("echo", [](const params_sv& params) {
|
|
||||||
for (auto i = 1; i < params.size(); i++) {
|
|
||||||
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER, "%s ", params.get(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
game::Com_Printf(game::CON_CHANNEL_DONT_FILTER, "\n");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Override vstr this way
|
|
||||||
auto* cmd = game::Cmd_FindCommand("vstr");
|
|
||||||
if (cmd != nullptr) {
|
|
||||||
cmd->function = cmd_vstr_f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} // namespace command
|
} // namespace command
|
||||||
|
|
||||||
REGISTER_COMPONENT(command::component)
|
|
||||||
|
@ -17,8 +17,8 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
void add_raw(const char* name, void (*callback)());
|
void add_raw(const char* name, void (*callback)());
|
||||||
void add(const char* name,
|
void add_sv(const char* name,
|
||||||
const std::function<void(const params_sv&)>& callback);
|
const std::function<void(const params_sv&)>& callback);
|
||||||
|
|
||||||
void execute(std::string command, bool sync = false);
|
void execute(std::string command, bool sync = false);
|
||||||
} // namespace command
|
} // namespace command
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define SELECT_VALUE(mp, zm) (game::environment::is_mp() ? (mp) : (zm))
|
|
||||||
|
|
||||||
namespace game {
|
namespace game {
|
||||||
enum gamemode { none, multiplayer, zombies };
|
enum gamemode { none, multiplayer, zombies };
|
||||||
|
|
||||||
@ -33,6 +31,15 @@ private:
|
|||||||
T* t5mp_;
|
T* t5mp_;
|
||||||
T* t5zm_;
|
T* t5zm_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline std::size_t select(const std::size_t mp_val, const std::size_t sp_val) {
|
||||||
|
return environment::is_mp() ? mp_val : sp_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::size_t select(const void* mp_val, const void* sp_val) {
|
||||||
|
return select(reinterpret_cast<std::size_t>(mp_val),
|
||||||
|
reinterpret_cast<std::size_t>(sp_val));
|
||||||
|
}
|
||||||
} // namespace game
|
} // namespace game
|
||||||
|
|
||||||
#include "symbols.hpp"
|
#include "symbols.hpp"
|
||||||
|
@ -54,6 +54,10 @@ WEAK symbol<const dvar_s*(const char*, int, int, int, unsigned __int16,
|
|||||||
WEAK symbol<void(const char* cmdName, void (*function)(),
|
WEAK symbol<void(const char* cmdName, void (*function)(),
|
||||||
cmd_function_s* allocedCmd)>
|
cmd_function_s* allocedCmd)>
|
||||||
Cmd_AddCommandInternal{0x6AD580, 0x661400};
|
Cmd_AddCommandInternal{0x6AD580, 0x661400};
|
||||||
|
WEAK symbol<void(const char* cmdName, void (*function)(),
|
||||||
|
cmd_function_s* allocedCmd)>
|
||||||
|
Cmd_AddServerCommandInternal{0x466930, 0x558290};
|
||||||
|
WEAK symbol<void()> Cbuf_AddServerText_f{0x48F620, 0x651A30};
|
||||||
WEAK symbol<void(const char* cmdName)> Cmd_RemoveCommand{0x527EA0, 0x5F1A90};
|
WEAK symbol<void(const char* cmdName)> Cmd_RemoveCommand{0x527EA0, 0x5F1A90};
|
||||||
WEAK symbol<cmd_function_s*(const char*)> Cmd_FindCommand{0x445B60, 0x479DD0};
|
WEAK symbol<cmd_function_s*(const char*)> Cmd_FindCommand{0x445B60, 0x479DD0};
|
||||||
|
|
||||||
|
@ -8,9 +8,9 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <format>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
@ -17,7 +17,7 @@ std::string info_string::get(const std::string& key) const {
|
|||||||
return value->second;
|
return value->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void info_string::parse(std::string buffer) {
|
void info_string::parse(std::string buffer) {
|
||||||
@ -25,24 +25,25 @@ void info_string::parse(std::string buffer) {
|
|||||||
buffer = buffer.substr(1);
|
buffer = buffer.substr(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto key_values = string::split(buffer, '\\');
|
const auto key_values = string::split(buffer, '\\');
|
||||||
for (size_t i = 0; !key_values.empty() && i < (key_values.size() - 1);
|
for (size_t i = 0; !key_values.empty() && i < (key_values.size() - 1);
|
||||||
i += 2) {
|
i += 2) {
|
||||||
const auto& key = key_values[i];
|
const auto& key = key_values[i];
|
||||||
const auto& value = key_values[i + 1];
|
const auto& value = key_values[i + 1];
|
||||||
this->key_value_pairs_[key] = value;
|
if (!this->key_value_pairs_.contains(key)) {
|
||||||
|
this->key_value_pairs_[key] = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string info_string::build() const {
|
std::string info_string::build() const {
|
||||||
std::string info_string;
|
std::string info_string;
|
||||||
for (auto i = this->key_value_pairs_.begin();
|
for (const auto& [key, value] : this->key_value_pairs_) {
|
||||||
i != this->key_value_pairs_.end(); ++i) {
|
|
||||||
info_string.append("\\");
|
info_string.append("\\");
|
||||||
|
|
||||||
info_string.append(i->first); // Key
|
info_string.append(key);
|
||||||
info_string.append("\\");
|
info_string.append("\\");
|
||||||
info_string.append(i->second); // Value
|
info_string.append(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return info_string;
|
return info_string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user