From e3562203831a0fa5fed61c8148b460ad516a8f1d Mon Sep 17 00:00:00 2001 From: Huoji's <1296564236@qq.com> Date: Tue, 3 Oct 2023 04:50:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csgo2/native_sdk.h | 12 ++- csgo2/script_apis.cpp | 159 +++++++++++++++++++++++++++++++++++++--- csgo2/script_engine.cpp | 12 +++ 3 files changed, 172 insertions(+), 11 deletions(-) diff --git a/csgo2/native_sdk.h b/csgo2/native_sdk.h index 2a991e5..a7ed4e0 100644 --- a/csgo2/native_sdk.h +++ b/csgo2/native_sdk.h @@ -465,6 +465,7 @@ public: SCHEMA_FIELD(int, m_iAccount) }; + class CBasePlayerPawn : public CBaseEntity { public: DECLARE_CLASS(CBasePlayerPawn); @@ -473,7 +474,16 @@ public: SCHEMA_FIELD(CPlayer_WeaponServices*, m_pWeaponServices) SCHEMA_FIELD(uint8_t**, m_pItemServices) }; -class CCSPlayerPawn : public CBasePlayerPawn { +// Size: 0x1568 +class CCSPlayerPawnBase : public CBasePlayerPawn +{ +public: + DECLARE_CLASS(CCSPlayerPawnBase); + SCHEMA_FIELD(bool, m_bRespawning) + SCHEMA_FIELD(int, m_ArmorValue) + +}; +class CCSPlayerPawn : public CCSPlayerPawnBase { public: DECLARE_CLASS(CCSPlayerPawn); SCHEMA_FIELD(const char*, m_szLastPlaceName) diff --git a/csgo2/script_apis.cpp b/csgo2/script_apis.cpp index 91e7d09..1ed7841 100644 --- a/csgo2/script_apis.cpp +++ b/csgo2/script_apis.cpp @@ -61,11 +61,14 @@ auto luaApi_SetPlayerCurrentWeaponAmmo(lua_State* luaVm) -> int { break; } auto playerController = reinterpret_cast(player); - const auto weaponServices = playerController->m_hPawn().Get()->m_pWeaponServices(); + const auto weaponServices = playerController->m_hPawn() + .Get() + ->m_pWeaponServices(); if (weaponServices == nullptr) { break; } - const auto activeWeapon = weaponServices->m_hActiveWeapon().Get(); + const auto activeWeapon = + weaponServices->m_hActiveWeapon().Get(); if (activeWeapon == nullptr) { break; } @@ -79,6 +82,124 @@ auto luaApi_SetPlayerCurrentWeaponAmmo(lua_State* luaVm) -> int { lua_pop(luaVm, 3); return 0; } +auto luaApi_RespawnPlayer(lua_State* luaVm) -> int { + const auto playerIndex = lua_tointeger(luaVm, 1); + int playerArmorValue = 0; + CGameEntitySystem* EntitySystem = CGameEntitySystem::GetInstance(); + do { + if (EntitySystem == nullptr || playerIndex == 0) { + break; + } + auto player = EntitySystem->GetBaseEntity(playerIndex); + if (player == nullptr) { + break; + } + if (player->IsBasePlayerController() == false) { + break; + } + auto playerController = reinterpret_cast(player); + auto playerPawn = playerController->m_hPawn().Get(); + playerPawn->m_bRespawning(false); + } while (false); + lua_pop(luaVm, 1); + return 0; +} +auto luaApi_SetPlayerArmorValue(lua_State* luaVm) -> int { + const auto playerIndex = lua_tointeger(luaVm, 1); + const auto playerArmorValue = lua_tointeger(luaVm, 2); + + CGameEntitySystem* EntitySystem = CGameEntitySystem::GetInstance(); + do { + if (EntitySystem == nullptr || playerIndex == 0) { + break; + } + auto player = EntitySystem->GetBaseEntity(playerIndex); + if (player == nullptr) { + break; + } + if (player->IsBasePlayerController() == false) { + break; + } + auto playerController = reinterpret_cast(player); + auto playerPawn = playerController->m_hPawn().Get(); + playerPawn->m_ArmorValue(playerArmorValue); + } while (false); + lua_pop(luaVm, 2); + return 0; +} +auto luaApi_GetPlayerArmorValue(lua_State* luaVm) -> int { + const auto playerIndex = lua_tointeger(luaVm, 1); + int playerArmorValue = 0; + + CGameEntitySystem* EntitySystem = CGameEntitySystem::GetInstance(); + do { + if (EntitySystem == nullptr || playerIndex == 0) { + break; + } + auto player = EntitySystem->GetBaseEntity(playerIndex); + if (player == nullptr) { + break; + } + if (player->IsBasePlayerController() == false) { + break; + } + auto playerController = reinterpret_cast(player); + auto playerPawn = playerController->m_hPawn().Get(); + playerArmorValue = playerPawn->m_ArmorValue(); + } while (false); + lua_pop(luaVm, 1); + lua_pushinteger(luaVm, playerArmorValue); + return 1; +} +auto luaApi_GetPlayerHealth(lua_State* luaVm) -> int { + const auto playerIndex = lua_tointeger(luaVm, 1); + int playerHealth = 0; + + CGameEntitySystem* EntitySystem = CGameEntitySystem::GetInstance(); + do { + if (EntitySystem == nullptr || playerIndex == 0) { + break; + } + auto player = EntitySystem->GetBaseEntity(playerIndex); + if (player == nullptr) { + break; + } + if (player->IsBasePlayerController() == false) { + break; + } + auto playerController = reinterpret_cast(player); + playerHealth = playerController->m_iHealth(); + } while (false); + lua_pop(luaVm, 1); + lua_pushinteger(luaVm, playerHealth); + return 1; +} +auto luaApi_SetPlayerHealth(lua_State* luaVm) -> int { + const auto playerIndex = lua_tointeger(luaVm, 1); + const auto playerHealth = lua_tointeger(luaVm, 2); + + CGameEntitySystem* EntitySystem = CGameEntitySystem::GetInstance(); + do { + if (EntitySystem == nullptr || playerIndex == 0) { + break; + } + LOG("luaApi_SetPlayerHealth :2 \n"); + + auto player = EntitySystem->GetBaseEntity(playerIndex); + if (player == nullptr) { + break; + } + if (player->IsBasePlayerController() == false) { + break; + } + LOG("luaApi_SetPlayerHealth :3 \n"); + + auto playerController = reinterpret_cast(player); + playerController->m_iHealth(playerHealth); + } while (false); + lua_pop(luaVm, 2); + return 0; +} auto luaApi_GetPlayerCurrentWeaponInfo(lua_State* luaVm) -> _luaApi_WeaponInfo { // param: playerIndex:int const auto playerIndex = lua_tointeger(luaVm, 1); @@ -97,11 +218,14 @@ auto luaApi_GetPlayerCurrentWeaponInfo(lua_State* luaVm) -> _luaApi_WeaponInfo { break; } auto playerController = reinterpret_cast(player); - const auto weaponServices = playerController->m_hPawn().Get()->m_pWeaponServices(); + const auto weaponServices = playerController->m_hPawn() + .Get() + ->m_pWeaponServices(); if (weaponServices == nullptr) { break; } - const auto activeWeapon = weaponServices->m_hActiveWeapon().Get(); + const auto activeWeapon = + weaponServices->m_hActiveWeapon().Get(); if (activeWeapon == nullptr) { break; } @@ -117,7 +241,8 @@ auto luaApi_GetPlayerCurrentWeaponInfo(lua_State* luaVm) -> _luaApi_WeaponInfo { if (itemView == nullptr) { break; } - const char* checkWeaponName = Offset::InterFaces::ILocalize->FindSafe(itemStaticData->m_pszItemBaseName); + const char* checkWeaponName = Offset::InterFaces::ILocalize->FindSafe( + itemStaticData->m_pszItemBaseName); if (checkWeaponName == nullptr || strlen(checkWeaponName) < 1) { break; } @@ -125,24 +250,38 @@ auto luaApi_GetPlayerCurrentWeaponInfo(lua_State* luaVm) -> _luaApi_WeaponInfo { info.Ammo = activeWeapon->m_iClip1(); info.ReserveAmmo = activeWeapon->m_pReserveAmmo(); info.weaponName = itemStaticData->GetSimpleWeaponName(); - info.weaponType = static_cast(itemStaticData->IsKnife(false) ? _luaApi_WeaponType::kKnife : (itemStaticData->IsWeapon() ? _luaApi_WeaponType::kGun : _luaApi_WeaponType::kOther)); + info.weaponType = static_cast( + itemStaticData->IsKnife(false) + ? _luaApi_WeaponType::kKnife + : (itemStaticData->IsWeapon() ? _luaApi_WeaponType::kGun + : _luaApi_WeaponType::kOther)); } while (false); return info; } auto initFunciton(lua_State* luaVm) -> void { lua_register(luaVm, "ListenToGameEvent", luaApi_ListenToGameEvent); - lua_register(luaVm, "luaApi_SetPlayerCurrentWeaponAmmo", luaApi_SetPlayerCurrentWeaponAmmo); - //我不喜欢他 + lua_register(luaVm, "luaApi_SetPlayerCurrentWeaponAmmo", + luaApi_SetPlayerCurrentWeaponAmmo); + lua_register(luaVm, "luaApi_GetPlayerHealth", luaApi_GetPlayerHealth); + lua_register(luaVm, "luaApi_SetPlayerHealth", luaApi_SetPlayerHealth); + lua_register(luaVm, "luaApi_GetPlayerArmorValue", + luaApi_GetPlayerArmorValue); + lua_register(luaVm, "luaApi_SetPlayerArmorValue", + luaApi_SetPlayerArmorValue); + lua_register(luaVm, "luaApi_RespawnPlayer", luaApi_RespawnPlayer); + + // 我不喜欢他 luabridge::getGlobalNamespace(luaVm) .beginClass<_luaApi_WeaponInfo>("WeaponInfo") - .addConstructor() + .addConstructor() .addData("isSuccess", &_luaApi_WeaponInfo::isSuccess) .addData("Ammo", &_luaApi_WeaponInfo::Ammo) .addData("ReserveAmmo", &_luaApi_WeaponInfo::ReserveAmmo) .addData("weaponName", &_luaApi_WeaponInfo::weaponName) .addData("weaponType", &_luaApi_WeaponInfo::weaponType) .endClass() - .addFunction("luaApi_GetPlayerCurrentWeaponInfo", &luaApi_GetPlayerCurrentWeaponInfo); + .addFunction("luaApi_GetPlayerCurrentWeaponInfo", + &luaApi_GetPlayerCurrentWeaponInfo); } }; // namespace ScriptApis diff --git a/csgo2/script_engine.cpp b/csgo2/script_engine.cpp index eccf79d..dcdeb5e 100644 --- a/csgo2/script_engine.cpp +++ b/csgo2/script_engine.cpp @@ -54,6 +54,18 @@ auto initLuaScripts() -> void { } LOG("execute: %s\n", file.c_str()); + std::string scriptDir = dirPath; + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); // get field "path" from table at top of stack (-1) + std::string cur_path = lua_tostring(L, -1); // grab path string from top of stack + cur_path.append(";"); // do your path magic here + cur_path.append(scriptDir); + cur_path.append("\\?.lua"); + lua_pop(L, 1); // get rid of the string on the stack we just pushed on line 5 + lua_pushstring(L, cur_path.c_str()); // push the new one + lua_setfield(L, -2, "path"); // set the field "path" in table at -2 with value at top of stack + lua_pop(L, 1); // get rid of package table from top of stack + if (luaL_dofile(L, file.c_str())) { LOG("dofile_err:%s\n\n", lua_tostring(L, -1)); continue;