Skip to content

Commit

Permalink
Merge pull request #5 from fedddddd/master
Browse files Browse the repository at this point in the history
rename command
  • Loading branch information
INSANEMODE authored Nov 22, 2020
2 parents 4034b8f + 60acdf3 commit d4202fc
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 21 deletions.
4 changes: 4 additions & 0 deletions ClanTagRank/ClanTagRank.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="userinfo\userinfo.hpp" />
<ClInclude Include="command\command.hpp" />
<ClInclude Include="game\game.hpp" />
<ClInclude Include="game\structs.hpp" />
<ClInclude Include="utils\hook.hpp" />
Expand All @@ -66,6 +68,8 @@
<ClInclude Include="utils\string.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="userinfo\userinfo.cpp" />
<ClCompile Include="command\command.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="game\game.cpp" />
<ClCompile Include="utils\hook.cpp" />
Expand Down
12 changes: 12 additions & 0 deletions ClanTagRank/ClanTagRank.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<ClInclude Include="utils\string.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="command\command.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="userinfo\userinfo.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
Expand All @@ -50,5 +56,11 @@
<ClCompile Include="utils\string.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="command\command.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="userinfo\userinfo.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
59 changes: 59 additions & 0 deletions ClanTagRank/command/command.cpp
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;
}
}
20 changes: 20 additions & 0 deletions ClanTagRank/command/command.hpp
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);
}
55 changes: 35 additions & 20 deletions ClanTagRank/game/game.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../stdinc.hpp"

namespace game
namespace
{
LPSTR cmdString = GetCommandLineA();

Expand All @@ -14,49 +14,64 @@ namespace game

return std::string(token);
}
}

namespace game
{
std::string gamename = get_gamename();

DWORD select_value(DWORD t6mp, DWORD t6zm, DWORD iw5)
bool is_t6mp()
{
return strcmp(gamename.data(), "t6mp") == 0
? t6mp
: strcmp(gamename.data(), "t6zm") == 0
? t6zm
: iw5;
return strcmp(gamename.data(), "t6mp") == 0;
}

Cmd_AddCommandInternal_t Cmd_AddCommandInternal = Cmd_AddCommandInternal_t(select_value(0x5B3070, 0x004DC2A0, 0x545DF0));
Cmd_Argv_t Cmd_Argv = Cmd_Argv_t(select_value(0x5608F0, 0x6B3D40, 0x467600));
bool is_t6zm()
{
return strcmp(gamename.data(), "t6zm") == 0;
}

//ClientCommand_t ClientCommand = ClientCommand_t(select_value(0x47E590, 0, 0x502CB0));
//ClientDisconnect_t ClientDisconnect = ClientDisconnect_t(select_value(0x4ED6A0, 0, 0));
bool is_iw5()
{
return strcmp(gamename.data(), "iw5mp") == 0;
}

int get_maxclients()
{
const auto t6_maxclients = game::t6::Dvar_FindVar("sv_maxclients")->current.integer;
const auto iw5_maxclients = game::iw5::Dvar_FindVar("sv_maxclients")->current.integer;

return SELECT_VALUE(t6_maxclients, t6_maxclients, iw5_maxclients);
}

Cmd_AddCommandInternal_t Cmd_AddCommandInternal = Cmd_AddCommandInternal_t(SELECT_VALUE(0x5B3070, 0x004DC2A0, 0x545DF0));
Cmd_Argv_t Cmd_Argv = Cmd_Argv_t(SELECT_VALUE(0x5608F0, 0x6B3D40, 0x467600));
//Cmd_ArgvBuffer_t Cmd_ArgvBuffer = Cmd_ArgvBuffer_t(select_value(0x6600A0, 0, 0));
//ConcatArgs_t ConcatArgs = ConcatArgs_t(select_value(0x5FB8B0, 0, 0x502150));

namespace iw5
{
Dvar_FindVar_t Dvar_FindVar = Dvar_FindVar_t(select_value(0x563A70, 0x673C80, 0x5BDCC0));
Dvar_FindVar_t Dvar_FindVar = Dvar_FindVar_t(SELECT_VALUE(0x563A70, 0x673C80, 0x5BDCC0));
}

namespace t6
{
Dvar_FindVar_t Dvar_FindVar = Dvar_FindVar_t(select_value(0x563A70, 0x673C80, 0x5BDCC0));
Dvar_FindVar_t Dvar_FindVar = Dvar_FindVar_t(SELECT_VALUE(0x563A70, 0x673C80, 0x5BDCC0));
}

I_CleanStr_t I_CleanStr = I_CleanStr_t(select_value(0x44F2B0, 0x483F40, 0x5C2C60));
I_CleanStr_t I_CleanStr = I_CleanStr_t(SELECT_VALUE(0x44F2B0, 0x483F40, 0x5C2C60));

//Info_SetValueForKey_t Info_SetValueForKey = Info_SetValueForKey_t(select_value(0x497300, 0, 0x5C3170));
Info_ValueForKey_t Info_ValueForKey = Info_ValueForKey_t(select_value(0x68B420, 0x46AD00, 0x5C2DB0));
Info_ValueForKey_t Info_ValueForKey = Info_ValueForKey_t(SELECT_VALUE(0x68B420, 0x46AD00, 0x5C2DB0));

SV_GetUserInfo_t SV_GetUserInfo = SV_GetUserInfo_t(select_value(0x68BB90, 0x4C10F0, 0x573E00));
SV_GetUserInfo_t SV_GetUserInfo = SV_GetUserInfo_t(SELECT_VALUE(0x68BB90, 0x4C10F0, 0x573E00));
//SV_GetGuid_t SV_GetGuid = SV_GetGuid_t(select_value(0x5DF410, 0, 0));
//SV_Cmd_Argv_t SV_Cmd_Argv = SV_Cmd_Argv_t(select_value(0x6DBF80, 0, 0));

ClientUserInfoChanged_t ClientUserInfoChanged = ClientUserInfoChanged_t(select_value(0x4ED6A0, 0x427DC0, 0x4FADB0));
//Cmd_ArgvBuffer_t Cmd_ArgvBuffer = Cmd_ArgvBuffer_t(select_value(0x6600A0, 0, 0));

//SV_Cmd_ArgvBuffer_t SV_Cmd_ArgvBuffer = SV_Cmd_ArgvBuffer_t(select_value(0x660050, 0, 0));

ClientUserInfoChanged_t ClientUserInfoChanged = ClientUserInfoChanged_t(SELECT_VALUE(0x4ED6A0, 0x427DC0, 0x4FADB0));
//ClientCommand_t ClientCommand = ClientCommand_t(select_value(0x47E590, 0, 0x502CB0));
//ClientDisconnect_t ClientDisconnect = ClientDisconnect_t(select_value(0x4ED6A0, 0, 0));

//gclient_t* g_clients = reinterpret_cast<gclient_t*>(select_value(0x2370BA0, 0, 0));
//iw5::gentity_t* g_entities = reinterpret_cast<iw5::gentity_t*>(select_value(0x21EF7C0, 0x21C13C0, 0x1A66E28));
}
3 changes: 2 additions & 1 deletion ClanTagRank/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace game
{
std::string get_gamename();
extern std::string gamename;

extern int get_maxclients();

typedef void (*Cmd_AddCommandInternal_t)(const char*, void(), cmd_function_t* allocedCmd);
extern Cmd_AddCommandInternal_t Cmd_AddCommandInternal;

Expand Down
5 changes: 5 additions & 0 deletions ClanTagRank/stdinc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#include <iostream>
#include <sstream>
#include <algorithm>
#include <functional>

#include "game/structs.hpp"
#include "game/game.hpp"

#define SELECT_VALUE(t6mp, t6zm, iw5mp) (is_t6mp() ? (t6mp) : is_t6zm() ? (t6zm) : (iw5mp))

#include "utils/string.hpp"
59 changes: 59 additions & 0 deletions ClanTagRank/userinfo/userinfo.cpp
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);
}
}
7 changes: 7 additions & 0 deletions ClanTagRank/userinfo/userinfo.hpp
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();
}

0 comments on commit d4202fc

Please sign in to comment.