This commit is contained in:
Huoji's
2023-10-05 04:24:29 +08:00
parent eba91d741a
commit 40fc60e481

View File

@@ -34,6 +34,25 @@ auto TimerCallBack(_GameTimer* timer) -> void {
luaL_unref(timer->m_luaVm, LUA_REGISTRYINDEX, timer->m_iParamIndex); luaL_unref(timer->m_luaVm, LUA_REGISTRYINDEX, timer->m_iParamIndex);
} }
} }
auto ExcutePlayerAction(int playerIndex,
std::function<void(CCSPlayerController*)> function)
-> void {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
function(playerController);
} while (false);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>Ҫ<EFBFBD>Լ<EFBFBD>push<73><68>stack<63><6B> // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>Ҫ<EFBFBD>Լ<EFBFBD>push<73><68>stack<63><6B>
auto luaApi_ListenToGameEvent(lua_State* luaVm) -> int { auto luaApi_ListenToGameEvent(lua_State* luaVm) -> int {
const auto eventName = lua_tostring(luaVm, 1); const auto eventName = lua_tostring(luaVm, 1);
@@ -69,30 +88,17 @@ auto luaApi_SetPlayerCurrentWeaponAmmo(lua_State* luaVm) -> int {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
const auto playerAmmoNum = lua_tointeger(luaVm, 2); const auto playerAmmoNum = lua_tointeger(luaVm, 2);
const auto playerReserveAmmoNum = lua_tointeger(luaVm, 3); const auto playerReserveAmmoNum = lua_tointeger(luaVm, 3);
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
const auto weaponServices = playerController->m_hPawn() const auto weaponServices = playerController->m_hPawn()
.Get<CCSPlayerPawn>() .Get<CCSPlayerPawn>()
->m_pWeaponServices(); ->m_pWeaponServices();
if (weaponServices == nullptr) { if (weaponServices == nullptr) {
break; return;
} }
const auto activeWeapon = const auto activeWeapon =
weaponServices->m_hActiveWeapon().Get<CBasePlayerWeapon>(); weaponServices->m_hActiveWeapon().Get<CBasePlayerWeapon>();
if (activeWeapon == nullptr) { if (activeWeapon == nullptr) {
break; return;
} }
if (playerAmmoNum != -1) { if (playerAmmoNum != -1) {
activeWeapon->m_iClip1(playerAmmoNum); activeWeapon->m_iClip1(playerAmmoNum);
@@ -100,77 +106,41 @@ auto luaApi_SetPlayerCurrentWeaponAmmo(lua_State* luaVm) -> int {
if (playerReserveAmmoNum != -1) { if (playerReserveAmmoNum != -1) {
activeWeapon->m_pReserveAmmo(playerReserveAmmoNum); activeWeapon->m_pReserveAmmo(playerReserveAmmoNum);
} }
} while (false); });
lua_pop(luaVm, 3); lua_pop(luaVm, 3);
return 0; return 0;
} }
auto luaApi_RespawnPlayer(lua_State* luaVm) -> int { auto luaApi_RespawnPlayer(lua_State* luaVm) -> int {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
int playerArmorValue = 0; int playerArmorValue = 0;
CGameEntitySystem* EntitySystem = global::EntitySystem; ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
do { const auto playerPawn =
if (EntitySystem == nullptr || playerIndex == 0) { playerController->m_hPawn().Get<CCSPlayerPawn>();
break; if (playerPawn == nullptr) {
return;
} }
auto player = EntitySystem->GetBaseEntity(playerIndex);
if (player == nullptr) {
break;
}
if (player->IsBasePlayerController() == false) {
break;
}
auto playerController = reinterpret_cast<CCSPlayerController*>(player);
auto playerPawn = playerController->m_hPawn().Get<CCSPlayerPawn>();
LOG("respawn player: %llx \n", playerPawn); LOG("respawn player: %llx \n", playerPawn);
Offset::FnRespawnPlayer(playerPawn); Offset::FnRespawnPlayer(playerPawn);
// playerPawn->ForceRespawnPlayer(); });
} while (false);
lua_pop(luaVm, 1);
return 0; return 0;
} }
auto luaApi_SetPlayerArmorValue(lua_State* luaVm) -> int { auto luaApi_SetPlayerArmorValue(lua_State* luaVm) -> int {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
const auto playerArmorValue = lua_tointeger(luaVm, 2); const auto playerArmorValue = lua_tointeger(luaVm, 2);
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
auto playerPawn = playerController->m_hPawn().Get<CCSPlayerPawn>(); auto playerPawn = playerController->m_hPawn().Get<CCSPlayerPawn>();
playerPawn->m_ArmorValue(playerArmorValue); playerPawn->m_ArmorValue(playerArmorValue);
} while (false); });
lua_pop(luaVm, 2); lua_pop(luaVm, 2);
return 0; return 0;
} }
auto luaApi_GetPlayerArmorValue(lua_State* luaVm) -> int { auto luaApi_GetPlayerArmorValue(lua_State* luaVm) -> int {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
int playerArmorValue = 0; int playerArmorValue = 0;
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
auto playerPawn = playerController->m_hPawn().Get<CCSPlayerPawn>(); auto playerPawn = playerController->m_hPawn().Get<CCSPlayerPawn>();
playerArmorValue = playerPawn->m_ArmorValue(); playerArmorValue = playerPawn->m_ArmorValue();
} while (false); });
lua_pop(luaVm, 1); lua_pop(luaVm, 1);
lua_pushinteger(luaVm, playerArmorValue); lua_pushinteger(luaVm, playerArmorValue);
return 1; return 1;
@@ -178,23 +148,10 @@ auto luaApi_GetPlayerArmorValue(lua_State* luaVm) -> int {
auto luaApi_GetPlayerHealth(lua_State* luaVm) -> int { auto luaApi_GetPlayerHealth(lua_State* luaVm) -> int {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
int playerHealth = 0; int playerHealth = 0;
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
auto playerPawn = playerController->m_hPawn().Get<CCSPlayerPawn>(); auto playerPawn = playerController->m_hPawn().Get<CCSPlayerPawn>();
playerHealth = playerPawn->m_iHealth(); playerHealth = playerPawn->m_iHealth();
} while (false); });
lua_pop(luaVm, 1); lua_pop(luaVm, 1);
lua_pushinteger(luaVm, playerHealth); lua_pushinteger(luaVm, playerHealth);
return 1; return 1;
@@ -202,24 +159,9 @@ auto luaApi_GetPlayerHealth(lua_State* luaVm) -> int {
auto luaApi_SetPlayerHealth(lua_State* luaVm) -> int { auto luaApi_SetPlayerHealth(lua_State* luaVm) -> int {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
const auto playerHealth = lua_tointeger(luaVm, 2); const auto playerHealth = lua_tointeger(luaVm, 2);
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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;
}
auto playerController = reinterpret_cast<CCSPlayerController*>(player);
playerController->m_hPawn().Get()->m_iHealth(playerHealth); playerController->m_hPawn().Get()->m_iHealth(playerHealth);
} while (false); });
lua_pop(luaVm, 2); lua_pop(luaVm, 2);
return 0; return 0;
} }
@@ -227,141 +169,125 @@ auto luaApi_GetPlayerWeaponInfo(lua_State* luaVm) -> _luaApi_WeaponInfo {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
const auto weaponIndex = lua_tointeger(luaVm, 2); const auto weaponIndex = lua_tointeger(luaVm, 2);
_luaApi_WeaponInfo info{0}; _luaApi_WeaponInfo info{0};
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
const auto weaponServices = playerController->m_hPawn() const auto weaponServices = playerController->m_hPawn()
.Get<CCSPlayerPawn>() .Get<CCSPlayerPawn>()
->m_pWeaponServices(); ->m_pWeaponServices();
if (weaponServices == nullptr) { do {
break; if (weaponServices == nullptr) {
} break;
const auto weapons = weaponServices->m_hMyWeapons();
// Create a new table on the Lua stack
lua_newtable(luaVm);
int index = 1; // Lua tables start at index 1
CBasePlayerWeapon* activeWeapon = nullptr;
for (CHandle* handle = weapons.begin(); handle < weapons.end();
++handle) {
const auto weapon = handle->Get();
if (weapon == nullptr) {
continue;
} }
const auto weaponIndex = weapon->GetRefEHandle().GetEntryIndex(); const auto weapons = weaponServices->m_hMyWeapons();
if (weaponIndex != weaponIndex) {
continue; // Create a new table on the Lua stack
lua_newtable(luaVm);
int index = 1; // Lua tables start at index 1
CBasePlayerWeapon* activeWeapon = nullptr;
for (CHandle* handle = weapons.begin(); handle < weapons.end();
++handle) {
const auto weapon = handle->Get();
if (weapon == nullptr) {
continue;
}
const auto weaponIndex =
weapon->GetRefEHandle().GetEntryIndex();
if (weaponIndex != weaponIndex) {
continue;
}
activeWeapon = handle->Get<CBasePlayerWeapon>();
} }
activeWeapon = handle->Get<CBasePlayerWeapon>(); if (activeWeapon == nullptr) {
} break;
if (activeWeapon == nullptr) { }
break; const auto attributeManager = activeWeapon->m_AttributeManager();
} if (activeWeapon == nullptr) {
const auto attributeManager = activeWeapon->m_AttributeManager(); break;
if (activeWeapon == nullptr) { }
break; const auto itemView = attributeManager->m_Item();
} if (itemView == nullptr) {
const auto itemView = attributeManager->m_Item(); break;
if (itemView == nullptr) { }
break; const auto itemStaticData = itemView->GetStaticData();
} if (itemView == nullptr) {
const auto itemStaticData = itemView->GetStaticData(); break;
if (itemView == nullptr) { }
break; const char* checkWeaponName =
} Offset::InterFaces::ILocalize->FindSafe(
const char* checkWeaponName = Offset::InterFaces::ILocalize->FindSafe( itemStaticData->m_pszItemBaseName);
itemStaticData->m_pszItemBaseName); if (checkWeaponName == nullptr || strlen(checkWeaponName) < 1) {
if (checkWeaponName == nullptr || strlen(checkWeaponName) < 1) { break;
break; }
} info.isSuccess = true;
info.isSuccess = true; info.Ammo = activeWeapon->m_iClip1();
info.Ammo = activeWeapon->m_iClip1(); info.ReserveAmmo = activeWeapon->m_pReserveAmmo();
info.ReserveAmmo = activeWeapon->m_pReserveAmmo(); info.weaponName = itemStaticData->GetSimpleWeaponName();
info.weaponName = itemStaticData->GetSimpleWeaponName(); info.weaponBaseName = itemStaticData->m_pszItemBaseName;
info.weaponBaseName = itemStaticData->m_pszItemBaseName; info.weaponType =
info.weaponType = static_cast<int>( static_cast<int>(itemStaticData->IsKnife(false)
itemStaticData->IsKnife(false) ? _luaApi_WeaponType::kKnife
? _luaApi_WeaponType::kKnife : (itemStaticData->IsWeapon()
: (itemStaticData->IsWeapon() ? _luaApi_WeaponType::kGun ? _luaApi_WeaponType::kGun
: _luaApi_WeaponType::kOther)); : _luaApi_WeaponType::kOther));
info.weaponIndex = weaponIndex; info.weaponIndex = weaponIndex;
} while (false); } while (false);
});
return info; return info;
} }
auto luaApi_GetPlayerCurrentWeaponInfo(lua_State* luaVm) -> _luaApi_WeaponInfo { auto luaApi_GetPlayerCurrentWeaponInfo(lua_State* luaVm) -> _luaApi_WeaponInfo {
// param: playerIndex:int // param: playerIndex:int
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
_luaApi_WeaponInfo info{0}; _luaApi_WeaponInfo info{0};
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
do {
const auto weaponServices = playerController->m_hPawn()
.Get<CCSPlayerPawn>()
->m_pWeaponServices();
if (weaponServices == nullptr) {
break;
}
const auto activeWeapon =
weaponServices->m_hActiveWeapon().Get<CBasePlayerWeapon>();
if (activeWeapon == nullptr) {
break;
}
const auto attributeManager = activeWeapon->m_AttributeManager();
if (activeWeapon == nullptr) {
break;
}
const auto itemView = attributeManager->m_Item();
if (itemView == nullptr) {
break;
}
const auto itemStaticData = itemView->GetStaticData();
if (itemView == nullptr) {
break;
}
const char* checkWeaponName =
Offset::InterFaces::ILocalize->FindSafe(
itemStaticData->m_pszItemBaseName);
if (checkWeaponName == nullptr || strlen(checkWeaponName) < 1) {
break;
}
info.isSuccess = true;
info.Ammo = activeWeapon->m_iClip1();
info.ReserveAmmo = activeWeapon->m_pReserveAmmo();
info.weaponName = itemStaticData->GetSimpleWeaponName();
info.weaponBaseName = itemStaticData->m_pszItemBaseName;
CGameEntitySystem* EntitySystem = global::EntitySystem; info.weaponType =
do { static_cast<int>(itemStaticData->IsKnife(false)
if (EntitySystem == nullptr || playerIndex == 0) { ? _luaApi_WeaponType::kKnife
break; : (itemStaticData->IsWeapon()
} ? _luaApi_WeaponType::kGun
auto player = EntitySystem->GetBaseEntity(playerIndex); : _luaApi_WeaponType::kOther));
if (player == nullptr) { info.weaponIndex = weaponServices->m_hActiveWeapon()
break; .Get()
} ->GetRefEHandle()
if (player->IsBasePlayerController() == false) { .GetEntryIndex();
break; } while (false);
} });
auto playerController = reinterpret_cast<CCSPlayerController*>(player);
const auto weaponServices = playerController->m_hPawn()
.Get<CCSPlayerPawn>()
->m_pWeaponServices();
if (weaponServices == nullptr) {
break;
}
const auto activeWeapon =
weaponServices->m_hActiveWeapon().Get<CBasePlayerWeapon>();
if (activeWeapon == nullptr) {
break;
}
const auto attributeManager = activeWeapon->m_AttributeManager();
if (activeWeapon == nullptr) {
break;
}
const auto itemView = attributeManager->m_Item();
if (itemView == nullptr) {
break;
}
const auto itemStaticData = itemView->GetStaticData();
if (itemView == nullptr) {
break;
}
const char* checkWeaponName = Offset::InterFaces::ILocalize->FindSafe(
itemStaticData->m_pszItemBaseName);
if (checkWeaponName == nullptr || strlen(checkWeaponName) < 1) {
break;
}
info.isSuccess = true;
info.Ammo = activeWeapon->m_iClip1();
info.ReserveAmmo = activeWeapon->m_pReserveAmmo();
info.weaponName = itemStaticData->GetSimpleWeaponName();
info.weaponBaseName = itemStaticData->m_pszItemBaseName;
info.weaponType = static_cast<int>(
itemStaticData->IsKnife(false)
? _luaApi_WeaponType::kKnife
: (itemStaticData->IsWeapon() ? _luaApi_WeaponType::kGun
: _luaApi_WeaponType::kOther));
info.weaponIndex = weaponServices->m_hActiveWeapon()
.Get()
->GetRefEHandle()
.GetEntryIndex();
} while (false);
return info; return info;
} }
auto luaApi_CreateTickRunFunction(lua_State* luaVm) -> int { auto luaApi_CreateTickRunFunction(lua_State* luaVm) -> int {
@@ -422,22 +348,9 @@ auto luaApi_CheckPlayerIsAlive(lua_State* luaVm) -> int {
// param: playerIndex:int // param: playerIndex:int
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
auto isAlive = false; auto isAlive = false;
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
isAlive = playerController->m_bPawnIsAlive(); isAlive = playerController->m_bPawnIsAlive();
} while (false); });
lua_pop(luaVm, 1); lua_pop(luaVm, 1);
lua_pushboolean(luaVm, isAlive); lua_pushboolean(luaVm, isAlive);
return 1; return 1;
@@ -446,22 +359,9 @@ auto luaApi_GetPlayerTeam(lua_State* luaVm) -> int {
// param: playerIndex:int // param: playerIndex:int
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
auto team = 0; auto team = 0;
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
team = playerController->m_iTeamNum(); team = playerController->m_iTeamNum();
} while (false); });
lua_pop(luaVm, 1); lua_pop(luaVm, 1);
lua_pushinteger(luaVm, team); lua_pushinteger(luaVm, team);
return 1; return 1;
@@ -471,23 +371,10 @@ auto luaApi_SetPlayerTeam(lua_State* luaVm) -> int {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
const auto team = lua_tointeger(luaVm, 2); const auto team = lua_tointeger(luaVm, 2);
auto isSuccess = false; auto isSuccess = false;
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
playerController->m_iTeamNum(team); playerController->m_iTeamNum(team);
isSuccess = true; isSuccess = true;
} while (false); });
lua_pop(luaVm, 2); lua_pop(luaVm, 2);
lua_pushboolean(luaVm, isSuccess); lua_pushboolean(luaVm, isSuccess);
return 1; return 1;
@@ -496,21 +383,10 @@ auto luaApi_CheckPlayerIsInServer(lua_State* luaVm) -> int {
// param: playerIndex:int // param: playerIndex:int
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
auto isInServer = false; auto isInServer = false;
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
do {
if (EntitySystem == nullptr || playerIndex == 0) {
break;
}
auto player = EntitySystem->GetBaseEntity(playerIndex);
if (player == nullptr) {
break;
}
if (player->IsBasePlayerController() == false) {
break;
}
isInServer = true; isInServer = true;
} while (false); });
lua_pop(luaVm, 1); lua_pop(luaVm, 1);
lua_pushboolean(luaVm, isInServer); lua_pushboolean(luaVm, isInServer);
return 1; return 1;
@@ -520,23 +396,10 @@ auto luaApi_GivePlayerWeapon(lua_State* luaVm) -> int {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
const auto weaponName = lua_tostring(luaVm, 2); const auto weaponName = lua_tostring(luaVm, 2);
auto isSuccess = false; auto isSuccess = false;
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
isSuccess = isSuccess =
GameWeapons::ParseWeaponCommand(playerController, weaponName); GameWeapons::ParseWeaponCommand(playerController, weaponName);
} while (false); });
lua_pop(luaVm, 2); lua_pop(luaVm, 2);
lua_pushboolean(luaVm, isSuccess); lua_pushboolean(luaVm, isSuccess);
return 1; return 1;
@@ -545,58 +408,46 @@ auto luApi_GetPlayerAllWeaponIndex(lua_State* luaVm) -> int {
// param: playerIndex:int // param: playerIndex:int
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
auto isSuccess = false; auto isSuccess = false;
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
CGameEntitySystem* EntitySystem = global::EntitySystem; do {
do { const auto weaponServices = playerController->m_hPawn()
if (EntitySystem == nullptr || playerIndex == 0) { .Get<CCSPlayerPawn>()
break; ->m_pWeaponServices();
} if (weaponServices == nullptr) {
auto player = EntitySystem->GetBaseEntity(playerIndex); break;
if (player == nullptr) {
break;
}
if (player->IsBasePlayerController() == false) {
break;
}
auto playerController = reinterpret_cast<CCSPlayerController*>(player);
const auto weaponServices = playerController->m_hPawn()
.Get<CCSPlayerPawn>()
->m_pWeaponServices();
if (weaponServices == nullptr) {
break;
}
const auto weapons = weaponServices->m_hMyWeapons();
// Create a new table on the Lua stack
lua_newtable(luaVm);
int index = 1; // Lua tables start at index 1
for (CHandle* handle = weapons.begin(); handle < weapons.end();
++handle) {
const auto weapon = handle->Get();
if (weapon == nullptr) {
continue;
} }
const auto weaponIndex = weapon->GetRefEHandle().GetEntryIndex(); const auto weapons = weaponServices->m_hMyWeapons();
// Push the index and then the value onto the stack // Create a new table on the Lua stack
lua_pushinteger(luaVm, index++); lua_newtable(luaVm);
lua_pushinteger(luaVm, weaponIndex);
// The table is now below the key-value pair in the stack, int index = 1; // Lua tables start at index 1
// so we use -3 to indicate its position for (CHandle* handle = weapons.begin(); handle < weapons.end();
lua_settable(luaVm, -3); ++handle) {
} const auto weapon = handle->Get();
isSuccess = true; if (weapon == nullptr) {
} while (false); continue;
}
const auto weaponIndex =
weapon->GetRefEHandle().GetEntryIndex();
// Push the index and then the value onto the stack
lua_pushinteger(luaVm, index++);
lua_pushinteger(luaVm, weaponIndex);
// The table is now below the key-value pair in the stack,
// so we use -3 to indicate its position
lua_settable(luaVm, -3);
}
isSuccess = true;
} while (false);
});
if (!isSuccess) { if (!isSuccess) {
// If unsuccessful, remove the table from the stack // If unsuccessful, remove the table from the stack
lua_pop(luaVm, 1); lua_pop(luaVm, 1);
// And push false instead
lua_pushboolean(luaVm, isSuccess);
} }
lua_pushboolean(luaVm, isSuccess);
// Return the number of results (either the table or false) // Return the number of results (either the table or false)
return 1; return 1;
} }
@@ -605,46 +456,37 @@ auto luaApi_RemovePlayerWeapon(lua_State* luaVm) -> int {
const auto playerIndex = lua_tointeger(luaVm, 1); const auto playerIndex = lua_tointeger(luaVm, 1);
const auto weaponIndex = lua_tointeger(luaVm, 2); const auto weaponIndex = lua_tointeger(luaVm, 2);
auto isSuccess = false; auto isSuccess = false;
ExcutePlayerAction(playerIndex, [&](CCSPlayerController* playerController) {
do {
const auto weaponServices = playerController->m_hPawn()
.Get<CCSPlayerPawn>()
->m_pWeaponServices();
if (weaponServices == nullptr) {
break;
}
const auto weapons = weaponServices->m_hMyWeapons();
CBasePlayerWeapon* activeWeapon = 0;
for (CHandle* handle = weapons.begin(); handle < weapons.end();
++handle) {
if (handle->GetEntryIndex() != weaponIndex) {
continue;
}
const auto weapon = handle->Get<CBasePlayerWeapon>();
if (weapon == nullptr) {
continue;
}
activeWeapon = weapon;
}
if (activeWeapon == nullptr) {
break;
}
weaponServices->RemoveWeapon(activeWeapon);
Offset::FnEntityRemove(global::EntitySystem, activeWeapon, nullptr,
-1);
isSuccess = true;
} while (false);
});
CGameEntitySystem* EntitySystem = global::EntitySystem;
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<CCSPlayerController*>(player);
const auto weaponServices = playerController->m_hPawn()
.Get<CCSPlayerPawn>()
->m_pWeaponServices();
if (weaponServices == nullptr) {
break;
}
const auto weapons = weaponServices->m_hMyWeapons();
CBasePlayerWeapon* activeWeapon = 0;
for (CHandle* handle = weapons.begin(); handle < weapons.end();
++handle) {
if (handle->GetEntryIndex() != weaponIndex) {
continue;
}
const auto weapon = handle->Get<CBasePlayerWeapon>();
if (weapon == nullptr) {
continue;
}
activeWeapon = weapon;
}
if (activeWeapon == nullptr) {
break;
}
weaponServices->RemoveWeapon(activeWeapon);
Offset::FnEntityRemove(global::EntitySystem, activeWeapon, nullptr, -1);
isSuccess = true;
} while (false);
lua_pop(luaVm, 2); lua_pop(luaVm, 2);
lua_pushboolean(luaVm, isSuccess); lua_pushboolean(luaVm, isSuccess);
return 1; return 1;