-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from fedddddd/master
rename command
- Loading branch information
Showing
9 changed files
with
203 additions
and
21 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,59 @@ | ||
#include "../stdinc.hpp" | ||
#include "command.hpp" | ||
|
||
namespace command | ||
{ | ||
std::unordered_map<std::string, std::function<void(params&)>> handlers; | ||
|
||
void main_handler() | ||
{ | ||
params params = {}; | ||
|
||
const auto command = utils::string::to_lower(params[0]); | ||
|
||
if (handlers.find(command) != handlers.end()) | ||
{ | ||
handlers[command](params); | ||
} | ||
} | ||
|
||
int params::size() | ||
{ | ||
//return game::Cmd_Argc(); | ||
} | ||
|
||
const char* params::get(int index) | ||
{ | ||
return game::Cmd_Argv(index); | ||
} | ||
|
||
std::string params::join(int index) | ||
{ | ||
std::string result = {}; | ||
|
||
for (int i = index; i < this->size(); i++) | ||
{ | ||
if (i > index) result.append(" "); | ||
result.append(this->get(i)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
void add_raw(const char* name, void (*callback)()) | ||
{ | ||
game::Cmd_AddCommandInternal(name, callback, utils::memory::get_allocator()->allocate<game::cmd_function_t>()); | ||
} | ||
|
||
void add(const char* name, std::function<void(params&)> callback) | ||
{ | ||
const auto command = utils::string::to_lower(name); | ||
|
||
if (handlers.find(command) == handlers.end()) | ||
{ | ||
add_raw(name, main_handler); | ||
} | ||
|
||
handlers[command] = callback; | ||
} | ||
} |
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,20 @@ | ||
#pragma once | ||
|
||
namespace command | ||
{ | ||
class params | ||
{ | ||
public: | ||
int size(); | ||
const char* get(int index); | ||
std::string join(int index); | ||
|
||
const char* operator[](const int index) | ||
{ | ||
return this->get(index); | ||
} | ||
}; | ||
|
||
void add_raw(const char* name, void (*callback)()); | ||
void add(const char* name, std::function<void(params&)> callback); | ||
} |
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
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,59 @@ | ||
#include "../stdinc.hpp" | ||
#include "userinfo.hpp" | ||
#include "../utils/hook.hpp" | ||
|
||
namespace | ||
{ | ||
game::Info_ValueForKey_t Info_ValueForKey_hook; | ||
std::unordered_map<std::string, std::function<const char* (const char*, const char*, const char*, int)>> info_callbacks; | ||
|
||
int client_num_from_userinfo(const char* s) | ||
{ | ||
const auto sv_maxclients = game::get_maxclients(); | ||
|
||
for (auto i = 0; i < sv_maxclients; i++) | ||
{ | ||
char buffer[2048]; | ||
game::SV_GetUserInfo(i, buffer, 2048); | ||
|
||
if (strcmp(s, buffer) == 0) | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
const char* Info_ValueForKey_stub(const char* s, const char* key) | ||
{ | ||
const auto result = Info_ValueForKey_hook(s, key); | ||
|
||
if (info_callbacks.find(key) != info_callbacks.end()) | ||
{ | ||
const auto clientNum = client_num_from_userinfo(s); | ||
|
||
if (clientNum == -1) | ||
{ | ||
return result; | ||
} | ||
|
||
return info_callbacks[key](s, key, result, clientNum); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
namespace userinfo | ||
{ | ||
void add(std::string name, std::function<const char* (const char*, const char*, const char*, int)> callback) | ||
{ | ||
info_callbacks[name] = callback; | ||
} | ||
|
||
void init() | ||
{ | ||
Info_ValueForKey_hook = utils::hook::vp::detour(game::Info_ValueForKey, Info_ValueForKey_stub, 5); | ||
} | ||
} |
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,7 @@ | ||
#pragma once | ||
|
||
namespace userinfo | ||
{ | ||
void add(std::string, std::function<const char* (const char*, const char*, const char*, int)>); | ||
void init(); | ||
} |