Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
update sdk, fix weapon drop bug, update gamevent struct from hl2sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
huoji120 committed Oct 10, 2023
1 parent 2f654c5 commit 79bc2cf
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 161 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"random": "cpp",
"hash_map": "cpp",
"hash_set": "cpp",
"filesystem": "cpp"
"filesystem": "cpp",
"regex": "cpp"
}
}
151 changes: 63 additions & 88 deletions csgo2/events.cpp
Original file line number Diff line number Diff line change
@@ -1,57 +1,55 @@
#include "events.h"

namespace events {
auto OnPlayerTeamChangeEevent(IGameEvent* event) -> void {
GameEventKeySymbol_t userIdNameParams{ "userid" };
GameEventKeySymbol_t teamNameParams{ "team" };
GameEventKeySymbol_t oldteamNameParams{ "oldteam" };
GameEventKeySymbol_t disconnectNameParams{ "disconnect"};
GameEventKeySymbol_t silentNameParams{ "silent" };
GameEventKeySymbol_t isbotParams{ "isbot"};

const auto PlayerPawn = reinterpret_cast<CCSPlayerPawn*>(
event->GetPlayerPawn(userIdNameParams));
if (PlayerPawn == nullptr) {
return;
}
if (PlayerPawn->IsBasePlayerController() == false) {
return;
}
const auto Player = PlayerPawn->GetPlayerController();
if (Player == nullptr) {
return;
}
const auto playerIndex = Player->GetRefEHandle().GetEntryIndex();
auto team = event->GetInt(teamNameParams);
auto oldTeam = event->GetInt(oldteamNameParams);
auto disconnect = event->GetBool(disconnectNameParams);
auto slient = event->GetBool(silentNameParams);
auto isBot = event->GetBool(isbotParams);
if (ScriptCallBacks::luaCall_onPlayerTeamChange(playerIndex, team, oldTeam, disconnect, slient, isBot) == true) {
event->SetBool(silentNameParams, true);
}
}
auto OnPlayerHurtEvent(IGameEvent* event) -> void {
/*
auto luaCall_onPlayerHurt(int userid, int attacker, int health, int armor,
const char* weapon, int dmg_health, int dmg_armor,
int hitgroup) -> void
*/
UnkGameEventStruct_t userIdNameParams{"userid"};
UnkGameEventStruct_t attackerNameParams{"attacker"};
UnkGameEventStruct_t healthNameParams{0};
UnkGameEventStruct_t armorNameParams{0};
UnkGameEventStruct_t weaponNameParams{0};
UnkGameEventStruct_t dmg_healthNameParams{0};
UnkGameEventStruct_t dmg_armorNameParams{0};
UnkGameEventStruct_t hitgroupNameParams{0};

static const auto healthStr = "health";
static const auto armorStr = "armor";
static const auto weaponStr = "weapon";
static const auto dmg_healthStr = "dmg_health";
static const auto dmg_armorStr = "dmg_armor";
static const auto hitgroupStr = "hitgroup";

healthNameParams.m_Unk = Offset::FnServerHashFunction(
healthStr, sizeof healthStr, SERVER_HASH_FUCNTION_KEY);
healthNameParams.m_Key = healthStr;

armorNameParams.m_Unk = Offset::FnServerHashFunction(
armorStr, sizeof armorStr, SERVER_HASH_FUCNTION_KEY);
armorNameParams.m_Key = armorStr;

weaponNameParams.m_Unk = Offset::FnServerHashFunction(
weaponStr, sizeof weaponStr, SERVER_HASH_FUCNTION_KEY);

weaponNameParams.m_Key = weaponStr;

dmg_healthNameParams.m_Unk = Offset::FnServerHashFunction(
dmg_healthStr, sizeof dmg_healthStr, SERVER_HASH_FUCNTION_KEY);
dmg_healthNameParams.m_Key = dmg_healthStr;

dmg_armorNameParams.m_Unk = Offset::FnServerHashFunction(
dmg_armorStr, sizeof dmg_armorStr, SERVER_HASH_FUCNTION_KEY);
dmg_armorNameParams.m_Key = dmg_armorStr;

hitgroupNameParams.m_Unk = Offset::FnServerHashFunction(
hitgroupStr, sizeof hitgroupStr, SERVER_HASH_FUCNTION_KEY);
hitgroupNameParams.m_Key = hitgroupStr;
GameEventKeySymbol_t userIdNameParams{"userid"};
GameEventKeySymbol_t attackerNameParams{"attacker"};
GameEventKeySymbol_t healthNameParams{"health"};
GameEventKeySymbol_t armorNameParams{"armor"};
GameEventKeySymbol_t weaponNameParams{"weapon"};
GameEventKeySymbol_t dmg_healthNameParams{"dmg_health"};
GameEventKeySymbol_t dmg_armorNameParams{"dmg_armor"};
GameEventKeySymbol_t hitgroupNameParams{"hitgroup"};

const auto victimPawn = reinterpret_cast<CCSPlayerPawn*>(
event->GetPlayerPawn(&userIdNameParams));
event->GetPlayerPawn(userIdNameParams));
const auto attackerPawn = reinterpret_cast<CCSPlayerPawn*>(
event->GetPlayerPawn(&attackerNameParams));
event->GetPlayerPawn(attackerNameParams));
if (victimPawn == nullptr || attackerPawn == nullptr) {
return;
}
Expand All @@ -67,12 +65,12 @@ auto OnPlayerHurtEvent(IGameEvent* event) -> void {
const auto victimIndex = victim->GetRefEHandle().GetEntryIndex();
const auto attackerIndex = attacker->GetRefEHandle().GetEntryIndex();

auto health = event->GetInt(&healthNameParams);
auto armor = event->GetInt(&armorNameParams);
auto weapon = event->GetString(&weaponNameParams);
auto dmg_health = event->GetInt(&dmg_healthNameParams);
auto dmg_armor = event->GetInt(&dmg_armorNameParams);
auto hitgroup = event->GetInt(&hitgroupNameParams);
auto health = event->GetInt(healthNameParams);
auto armor = event->GetInt(armorNameParams);
auto weapon = event->GetString(weaponNameParams);
auto dmg_health = event->GetInt(dmg_healthNameParams);
auto dmg_armor = event->GetInt(dmg_armorNameParams);
auto hitgroup = event->GetInt(hitgroupNameParams);
ScriptCallBacks::luaCall_onPlayerHurt(victimIndex, attackerIndex, health,
armor, weapon, dmg_health, dmg_armor,
hitgroup);
Expand All @@ -84,45 +82,25 @@ auto OnRoundEndEvent(IGameEvent* event) -> void {
"message" "string" // end round message
*/

UnkGameEventStruct_t winnerNameParams{0};
UnkGameEventStruct_t reasonNameParams{0};
UnkGameEventStruct_t messageNameParams{0};

static const auto winnerStr = "winner";
static const auto reasonStr = "reason";
static const auto messageStr = "message";

winnerNameParams.m_Unk = Offset::FnServerHashFunction(
winnerStr, sizeof winnerStr, SERVER_HASH_FUCNTION_KEY);
winnerNameParams.m_Key = winnerStr;

reasonNameParams.m_Unk = Offset::FnServerHashFunction(
reasonStr, sizeof reasonStr, SERVER_HASH_FUCNTION_KEY);
reasonNameParams.m_Key = reasonStr;

messageNameParams.m_Unk = Offset::FnServerHashFunction(
messageStr, sizeof messageStr, SERVER_HASH_FUCNTION_KEY);
messageNameParams.m_Key = messageStr;
GameEventKeySymbol_t winnerNameParams{"winner"};
GameEventKeySymbol_t reasonNameParams{"reason"};
GameEventKeySymbol_t messageNameParams{"message"};

const auto message = event->GetString(&messageNameParams);
const auto winner = event->GetInt(&winnerNameParams);
const auto reason = event->GetInt(&reasonNameParams);
const auto message = event->GetString(messageNameParams);
const auto winner = event->GetInt(winnerNameParams);
const auto reason = event->GetInt(reasonNameParams);

ScriptCallBacks::luaCall_onRoundEnd(winner, reason, message);
}
auto OnRoundStartEvent(IGameEvent* event) -> void {
UnkGameEventStruct_t timelimitNameParams{0};
static const auto timelimitStr = "timelimit";
timelimitNameParams.m_Unk = Offset::FnServerHashFunction(
timelimitStr, sizeof timelimitStr, SERVER_HASH_FUCNTION_KEY);
timelimitNameParams.m_Key = timelimitStr;
const auto timelimit = event->GetInt(&timelimitNameParams);
GameEventKeySymbol_t timelimitNameParams{"timelimit"};
const auto timelimit = event->GetInt(timelimitNameParams);
ScriptCallBacks::luaCall_onRoundStart(timelimit);
}
auto OnPlayerSpawnEvent(IGameEvent* event) -> void {
UnkGameEventStruct_t userIdNameParams{"userid"};
GameEventKeySymbol_t userIdNameParams{"userid"};
const auto playerPawn = reinterpret_cast<CCSPlayerPawn*>(
event->GetPlayerPawn(&userIdNameParams));
event->GetPlayerPawn(userIdNameParams));
if (playerPawn == nullptr) {
return;
}
Expand All @@ -134,18 +112,15 @@ auto OnPlayerSpawnEvent(IGameEvent* event) -> void {
ScriptCallBacks::luaCall_onPlayerSpawn(playerIndex);
}
auto OnPlayerDeathEvent(IGameEvent* event) -> void {
UnkGameEventStruct_t userIdNameParams{"userid"};
UnkGameEventStruct_t attackerNameParams{"attacker"};
UnkGameEventStruct_t headshotNameParams{0};
static const auto headShotStr = "headshot";
headshotNameParams.m_Unk = Offset::FnServerHashFunction(
headShotStr, sizeof headShotStr, SERVER_HASH_FUCNTION_KEY);
headshotNameParams.m_Key = headShotStr;
GameEventKeySymbol_t userIdNameParams{"userid"};
GameEventKeySymbol_t attackerNameParams{"attacker"};
GameEventKeySymbol_t headshotNameParams{"headshot"};

const auto victimPawn = reinterpret_cast<CCSPlayerPawn*>(
event->GetPlayerPawn(&userIdNameParams));
event->GetPlayerPawn(userIdNameParams));
const auto attackerPawn = reinterpret_cast<CCSPlayerPawn*>(
event->GetPlayerPawn(&attackerNameParams));
const auto isHeadShot = event->GetBool(&headshotNameParams);
event->GetPlayerPawn(attackerNameParams));
const auto isHeadShot = event->GetBool(headshotNameParams);
if (victimPawn == nullptr || attackerPawn == nullptr) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions csgo2/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ auto OnPlayerSpawnEvent(IGameEvent* event) -> void;
auto OnRoundStartEvent(IGameEvent* event) -> void;
auto OnRoundEndEvent(IGameEvent* event) -> void;
auto OnPlayerHurtEvent(IGameEvent* event) -> void;
auto OnPlayerTeamChangeEevent(IGameEvent* event) -> void;
} // namespace events
33 changes: 24 additions & 9 deletions csgo2/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Host_Say_t original_Host_Say = NULL;
StartupServer_t origin_StartServer = NULL;
GameFrame_t origin_GameFrame = NULL;
CCSWeaponBase_Spawn_t origin_CCSWeaponBase_Spawn = NULL;

// https://github.com/Source2ZE/CS2Fixes/blob/main/src/commands.cpp#L494
void __fastcall hook_CCSWeaponBase_Spawn(CBaseEntity* pThis, void* a2) {
const char* pszClassName = pThis->m_pEntity()->m_designerName;

Expand All @@ -31,14 +31,26 @@ void __fastcall hook_CCSWeaponBase_Spawn(CBaseEntity* pThis, void* a2) {
pWeapon->m_AttributeManager()->m_Item()->m_bInitialized()) {
break;
}
if (GameWeapons::WeaponMap.find(pszClassName) ==
GameWeapons::WeaponMap.end()) {
const auto weaponName = Tools::toLower(std::string(pszClassName));
auto lookupWeaponSimpleName = std::string();
for (const auto& weapon : GameWeapons::WeaponMap) {
const auto& key = weapon.first;
const auto& [fullWeaponName, weaponItemDefIndex] = weapon.second;
if (fullWeaponName.find(weaponName) ==
std::string::npos) {
continue;
}
lookupWeaponSimpleName = key;
break;
}
if (lookupWeaponSimpleName.size() <= 1) {
break;
}
// lazy , fix me
const auto [fullWeaponName, weaponiItemDefIndex] =
GameWeapons::WeaponMap.at(pszClassName);
GameWeapons::WeaponMap.at(lookupWeaponSimpleName);

LOG("Fixing a %s with index = %d and initialized = %d\n", pszClassName,
LOG("Fixing a %s with index = %d and initialized = %d\n", fullWeaponName.c_str(),
pWeapon->m_AttributeManager()->m_Item()->m_iItemDefinitionIndex(),
pWeapon->m_AttributeManager()->m_Item()->m_bInitialized());

Expand All @@ -60,8 +72,7 @@ void __fastcall hook_GameFrame(void* rcx, bool simulating, bool bFirstTick,
if (simulating && global::HasTicked) {
global::m_flUniversalTime +=
global::GlobalVars->curtime - global::m_flLastTickedTime;
}
else {
} else {
global::m_flUniversalTime += global::GlobalVars->interval_per_tick;
}

Expand All @@ -75,7 +86,7 @@ void __fastcall hook_GameFrame(void* rcx, bool simulating, bool bFirstTick,
GameTimer::ExcuteTimers();
GameTickRunTime::ExcuteTickFunctions();
}

return origin_GameFrame(rcx, simulating, bFirstTick, bLastTick);
}
void __fastcall hook_StartServer(void* rcx,
Expand Down Expand Up @@ -173,6 +184,7 @@ bool __fastcall hook_FireEventServerSide(CGameEventManager* rcx,
static constexpr auto round_start = hash_32_fnv1a_const("round_start");
static constexpr auto round_end = hash_32_fnv1a_const("round_end");
static constexpr auto player_hurt = hash_32_fnv1a_const("player_hurt");
static constexpr auto player_team = hash_32_fnv1a_const("player_team");

switch (hash_32_fnv1a_const(eventName)) {
case player_death:
Expand All @@ -190,6 +202,9 @@ bool __fastcall hook_FireEventServerSide(CGameEventManager* rcx,
case player_hurt:
events::OnPlayerHurtEvent(event);
break;
case player_team:
events::OnPlayerTeamChangeEevent(event);
break;
// V社bug,这不会有用
/*
case player_chat:
Expand Down Expand Up @@ -263,7 +278,7 @@ auto initVmtHook() -> bool {
origin_StartServer && origin_GameFrame;
}
auto initConVarHooks() -> void {
// Offset::InterFaces::IVEngineCvar->RegisterConVar
// Offset::InterFaces::IVEngineCvar->RegisterConVar
}
auto init() -> bool {
bool isSuccess = initMinHook() && initVmtHook();
Expand Down
7 changes: 3 additions & 4 deletions csgo2/offset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ auto Init() -> bool {
server.FindPattern(pattern_FnEntityRemove).Get(FnEntityRemove);
server.FindPattern(pattern_FnGiveNamedItemPtr).Get(FnGiveNamedItem);
server.FindPattern(pattern_fnHost_SayPtr).Get(Host_SayPtr);
server.FindPattern(pattern_ServerHashFunctionPtr).Get(FnServerHashFunction);
//server.FindPattern(pattern_ServerHashFunctionPtr).Get(FnServerHashFunction);
server.FindPattern(pattern_UTIL_ClientPrintAll).Get(FnUTIL_ClientPrintAll);
server.FindPattern(pattern_FnClientPrint).Get(FnClientPrint);

Expand Down Expand Up @@ -124,7 +124,7 @@ auto Init() -> bool {
LOG("[huoji]FireEventServerSidePtr : %llx \n", FireEventServerSidePtr);
LOG("[huoji]Host_SayPtr : %llx \n", Host_SayPtr);
LOG("[huoji]FnNetworkStateChanged : %llx \n", FnNetworkStateChanged);
LOG("[huoji]FnServerHashFunction : %llx \n", FnServerHashFunction);
//LOG("[huoji]FnServerHashFunction : %llx \n", FnServerHashFunction);
LOG("[huoji]FnStateChanged : %llx \n", FnStateChanged);
LOG("[huoji]FnRespawnPlayerInDeathMatch : %llx \n", FnRespawnPlayerInDeathMatch);
LOG("[huoji]FnGiveNamedItem : %llx \n", FnGiveNamedItem);
Expand Down Expand Up @@ -157,8 +157,7 @@ auto Init() -> bool {
0, NULL);
// LOG("FnServerHashFunction: %llx \n", FnServerHashFunction("here",
// sizeof("here") - 1, 0x31415926));
return FnCCSWeaponBase_Spawn && FnEntityRemove && FnRespawnPlayerInDeathMatch && FnGiveNamedItem &&
FnServerHashFunction && Host_SayPtr && InterFaces::IVEngineServer &&
return FnCCSWeaponBase_Spawn && FnEntityRemove && FnRespawnPlayerInDeathMatch && FnGiveNamedItem && Host_SayPtr && InterFaces::IVEngineServer &&
InterFaces::GameResourceServiceServer &&
InterFaces::IServerGameClient && InterFaces::GameEventManager &&
InterFaces::SchemaSystem && FireEventServerSidePtr &&
Expand Down
2 changes: 2 additions & 0 deletions csgo2/script_apis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ auto luaApi_HttpPost(lua_State* luaVm) -> int {
lua_pushstring(luaVm, response.c_str());
return 2;
}

auto initFunciton(lua_State* luaVm) -> void {
lua_register(luaVm, "ListenToGameEvent", luaApi_ListenToGameEvent);
lua_register(luaVm, "luaApi_SetPlayerCurrentWeaponAmmo",
Expand Down Expand Up @@ -838,6 +839,7 @@ auto initFunciton(lua_State* luaVm) -> void {
lua_register(luaVm, "luaApi_HttpGet", luaApi_HttpGet);
lua_register(luaVm, "luaApi_HttpPost", luaApi_HttpPost);
lua_register(luaVm, "luaApi_GetPlayerSteamId", luaApi_GetPlayerSteamId);
//lua_register(luaVm, "luaApi_TeleportPlayer", luaApi_TeleportPlayer);

luabridge::getGlobalNamespace(luaVm)
.beginClass<_luaApi_WeaponInfo>("WeaponInfo")
Expand Down
27 changes: 27 additions & 0 deletions csgo2/script_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ std::unordered_map<uint32_t, _CallbackNames> callbackNameWithEnumMap{
{hash_32_fnv1a_const("round_start"), _CallbackNames::kOnRoundStart},
{hash_32_fnv1a_const("round_end"), _CallbackNames::kOnRoundEnd},
{hash_32_fnv1a_const("player_hurt"), _CallbackNames::kOnPlayerHurt},
{hash_32_fnv1a_const("player_team"), _CallbackNames::kOnPlayerTeamChange},
};
auto CallBackNameToEnum(const char* name) -> _CallbackNames {
if (name == nullptr) {
Expand Down Expand Up @@ -205,4 +206,30 @@ auto luaCall_onPlayerHurt(int userid, int attacker, int health, int armor,
}
});
}
auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam, bool disconnect, bool slient, bool isBot) -> bool {
bool result = false;
ExcuteCallbackInAllLuaVm(_CallbackNames::kOnPlayerTeamChange,
[&](lua_State* luaVm, int refIndex) -> void {
lua_rawgeti(luaVm, LUA_REGISTRYINDEX,
refIndex);
if (lua_isfunction(luaVm, -1)) {
lua_pushinteger(luaVm, userid);
lua_pushinteger(luaVm, team);
lua_pushinteger(luaVm, oldteam);
lua_pushboolean(luaVm, disconnect);
lua_pushboolean(luaVm, slient);
lua_pushboolean(luaVm, isBot);

if (lua_pcall(luaVm, 6, 1, 0) != LUA_OK) {
LOG("Error calling Lua callback: %s\n",
lua_tostring(luaVm, -1));
lua_pop(luaVm, 1);
}
if (lua_isboolean(luaVm, -1)) {
result = lua_toboolean(luaVm, -1);
}
}
});
return result;
}
} // namespace ScriptCallBacks
4 changes: 3 additions & 1 deletion csgo2/script_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ enum class _CallbackNames {
kOnPlayerSpawn,
kOnRoundStart,
kOnRoundEnd,
kOnPlayerHurt
kOnPlayerHurt,
kOnPlayerTeamChange
};
extern std::unordered_map<lua_State*, std::unordered_map<_CallbackNames, int>>
callbackList;
Expand All @@ -33,4 +34,5 @@ auto luaCall_onRoundEnd(int winnerTeam, int reason, const char* message)
auto luaCall_onPlayerHurt(int userid, int attacker, int health, int armor,
const char* weapon, int dmg_health, int dmg_armor,
int hitgroup) -> void;
auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam, bool disconnect, bool slient, bool isBot) -> bool;
} // namespace ScriptCallBacks
Loading

0 comments on commit 79bc2cf

Please sign in to comment.