Skip to content

Commit

Permalink
VTable: Offset loading refactoring
Browse files Browse the repository at this point in the history
Some offsets are actually already provided by AMXX. The rest are moved into separate files based on the class name that defines the method.
  • Loading branch information
tmp64 committed Aug 20, 2023
1 parent d26258c commit 621c3b9
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 89 deletions.
20 changes: 18 additions & 2 deletions gamedir/amxmodx/data/gamedata/weaponmod.games/master.games.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
"Game Master"
{
//
//
// Half-Life: Deathmatch
//

"valve/offsets-vtable.txt"
"valve/offsets-virtual-cbaseplayerammo.txt"
{
"game" "valve"
}

// Already exists in AMXX. Provided for reference.
// "valve/offsets-virtual-cbaseplayeritem.txt"
// {
// "game" "valve"
// }

"valve/offsets-virtual-cbaseplayerweapon.txt"
{
"game" "valve"
}

"valve/settings.txt"
{
"game" "valve"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"Games"
{
"#default"
{
"Classes"
{
"CBasePlayerAmmo"
{
"Offsets"
{
"AddAmmo"
{
"windows" "57"
"linux" "57"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"Games"
{
"#default"
{
"Classes"
{
"CBasePlayerItem"
{
"Offsets"
{
"AddToPlayer"
{
"windows" "58"
"linux" "58"
}

"CanDeploy"
{
"windows" "61"
"linux" "61"
}

"Deploy"
{
"windows" "62"
"linux" "62"
}

"CanHolster"
{
"windows" "63"
"linux" "63"
}

"Holster"
{
"windows" "64"
"linux" "64"
}

"ItemPostFrame"
{
"windows" "67"
"linux" "67"
}

"ItemSlot"
{
"windows" "75"
"linux" "75"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"Games"
{
"#default"
{
"Classes"
{
"CBasePlayerWeapon"
{
"Offsets"
{
"ItemPostFrame"
{
"windows" "67"
"linux" "67"
}

"IsUseable"
{
"windows" "82"
"linux" "82"
}
}
}
}
}
}

This file was deleted.

11 changes: 11 additions & 0 deletions gamedir/amxmodx/data/gamedata/weaponmod.games/valve/settings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"Games"
{
"#default"
{
"Keys"
{
"reference_weapon" "weapon_crowbar"
"reference_ammo" "ammo_rpgclip"
}
}
}
76 changes: 51 additions & 25 deletions src/wpnmod_vtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ namespace
//! Method name which a VTable offset must be loaded.
struct OffsetInitializer
{
const char* szMethodName;
const char* szClassName = nullptr;
const char* szMethodName = nullptr;
const char* szAmxxMethodName = nullptr;
};

//! List class and field name for each offset.
Expand All @@ -60,23 +62,32 @@ OffsetInitializer g_OffsetInitializers[VO_End];

void Vtable_Init(void)
{
g_OffsetInitializers[VO_Spawn] = { "Spawn" };
g_OffsetInitializers[VO_Precache] = { "Precache" };
g_OffsetInitializers[VO_Classify] = { "Classify" };
g_OffsetInitializers[VO_TraceAttack] = { "TraceAttack" };
g_OffsetInitializers[VO_TakeDamage] = { "TakeDamage" };
g_OffsetInitializers[VO_DamageDecal] = { "DamageDecal" };
g_OffsetInitializers[VO_Respawn] = { "Respawn" };
g_OffsetInitializers[VO_AddAmmo] = { "AddAmmo" };
g_OffsetInitializers[VO_AddToPlayer] = { "AddToPlayer" };
g_OffsetInitializers[VO_CanDeploy] = { "CanDeploy" };
g_OffsetInitializers[VO_Deploy] = { "Deploy" };
g_OffsetInitializers[VO_CanHolster] = { "CanHolster" };
g_OffsetInitializers[VO_Holster] = { "Holster" };
g_OffsetInitializers[VO_ItemPostFrame] = { "ItemPostFrame" };
g_OffsetInitializers[VO_ItemSlot] = { "ItemSlot" };
g_OffsetInitializers[VO_IsUseable] = { "IsUseable" };
g_OffsetInitializers[VO_Player_PostThink] = { "Player_PostThink" };
// CBaseEntity
g_OffsetInitializers[VO_Spawn] = { "CBaseEntity", "Spawn", "spawn" };
g_OffsetInitializers[VO_Precache] = { "CBaseEntity", "Precache", "precache" };
g_OffsetInitializers[VO_Classify] = { "CBaseEntity", "Classify", "classify" };
g_OffsetInitializers[VO_TraceAttack] = { "CBaseEntity", "TraceAttack", "traceattack" };
g_OffsetInitializers[VO_TakeDamage] = { "CBaseEntity", "TakeDamage", "takedamage" };
g_OffsetInitializers[VO_DamageDecal] = { "CBaseEntity", "DamageDecal", "damagedecal" };
g_OffsetInitializers[VO_Respawn] = { "CBaseEntity", "Respawn", "respawn" };

// CBasePlayer
g_OffsetInitializers[VO_Player_PostThink] = { "CBasePlayer", "PostThink", "player_postthink" };

// CBasePlayerItem
g_OffsetInitializers[VO_AddToPlayer] = { "CBasePlayerItem", "AddToPlayer", "item_addtoplayer" };
g_OffsetInitializers[VO_CanDeploy] = { "CBasePlayerItem", "CanDeploy", "item_candeploy" };
g_OffsetInitializers[VO_Deploy] = { "CBasePlayerItem", "Deploy", "item_deploy" };
g_OffsetInitializers[VO_CanHolster] = { "CBasePlayerItem", "CanHolster", "item_canholster" };
g_OffsetInitializers[VO_Holster] = { "CBasePlayerItem", "Holster", "item_holster" };
g_OffsetInitializers[VO_ItemSlot] = { "CBasePlayerItem", "ItemSlot", "item_itemslot" };

// CBasePlayerWeapon
g_OffsetInitializers[VO_ItemPostFrame] = { "CBasePlayerWeapon", "ItemPostFrame" };
g_OffsetInitializers[VO_IsUseable] = { "CBasePlayerWeapon", "IsUseable" };

// CBasePlayerAmmo
g_OffsetInitializers[VO_AddAmmo] = { "CBasePlayerAmmo", "AddAmmo" };

// Load offsets
IGameConfigPtr pWpnModCfg = WpnMod_LoadGameConfigFile("weaponmod.games");
Expand All @@ -87,18 +98,33 @@ void Vtable_Init(void)
{
const OffsetInitializer& init = g_OffsetInitializers[i];
TypeDescription& offset = GameVirtualOffsets[i];
bool isFound = false;

// Convert to lower case
std::string methodNameLower = init.szMethodName;
std::for_each(methodNameLower.begin(), methodNameLower.end(), [](char& c) { c = tolower(c); });
// Try to load from WpnMod config
isFound = pWpnModCfg->GetOffsetByClass(init.szClassName, init.szMethodName, &offset);

bool isFound =
pWpnModCfg->GetOffset(init.szMethodName, &offset) ||
pAmxxCfg->GetOffset(methodNameLower.c_str(), &offset);
if (!isFound)
{
// Try to load from AMXX config
std::string amxxName;

if (init.szAmxxMethodName)
{
amxxName = init.szAmxxMethodName;
}
else
{
// Convert name to lower case
amxxName = init.szMethodName;
std::for_each(amxxName.begin(), amxxName.end(), [](char& c) { c = tolower(c); });
}

isFound = pAmxxCfg->GetOffset(amxxName.c_str(), &offset);
}

if (!isFound)
{
WPNMOD_LOG("VTable Offset not found: %s\n", init.szMethodName);
WPNMOD_LOG("VTable Offset not found: %s::%s (amxx: %s)\n", init.szClassName, init.szMethodName, init.szAmxxMethodName);
anyNotFound = true;
continue;
}
Expand Down

0 comments on commit 621c3b9

Please sign in to comment.