Skip to content

Commit

Permalink
Fix issue johnathon-walnut#7
Browse files Browse the repository at this point in the history
  • Loading branch information
johnathon-walnut committed Oct 21, 2023
1 parent 9f736ac commit fa7be1a
Show file tree
Hide file tree
Showing 5 changed files with 497 additions and 49 deletions.
1 change: 1 addition & 0 deletions skinchangerStandalone.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
<ClInclude Include="src\sdk\TF2\random.h" />
<ClInclude Include="src\sdk\TF2\utlblockmemory.h" />
<ClInclude Include="src\sdk\TF2\utlmemory.h" />
<ClInclude Include="src\sdk\TF2\UtlSortVector.h" />
<ClInclude Include="src\sdk\TF2\utlvector.h" />
<ClInclude Include="src\sdk\TF2\vector.h" />
<ClInclude Include="src\utils\fnv1a.h" />
Expand Down
3 changes: 3 additions & 0 deletions skinchangerStandalone.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@
<ClInclude Include="src\utils\fnv1a.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\sdk\TF2\UtlSortVector.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="skinchangerStandalone.rc">
Expand Down
95 changes: 46 additions & 49 deletions src/app/SkinChanger/SkinChanger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,52 +681,10 @@ class CAttributeList
}
};

class Weapon;

#define NETVAR(_name, type, table, name) inline type &_name() \
{ \
static int offset = g_Netvars.GetNetvar(table, name); \
return *reinterpret_cast<type *>(reinterpret_cast<DWORD>(this) + offset); \
}

#define NETVAR_OFFSET(_name, type, name, offset) inline type &_name() \
{ \
return *reinterpret_cast<type *>(reinterpret_cast<DWORD>(this) + offset); \
}

class Player
{
public:
NETVAR(m_hActiveWeapon, CHandle<Weapon>, "CBaseCombatCharacter", "m_hActiveWeapon");
};

class Weapon
{
public:
// For wahtever reason CEconEntity -> m_iItemDefinitionIndex only has an offset of like 0x24 bytes
NETVAR_OFFSET(m_iItemDefinitionIndex, int, "m_iItemDefinitionIndex", 2364);
};

#define Redirect(from, to) case from: { nWeaponIndex = to; break; }

void SkinChanger::ApplySkins()
void SkinChanger::RedirectIndex(int& nWeaponIndex)
{
if (!m_bInitialSkinLoad)
{
Load();
m_bInitialSkinLoad = true;
}

auto pLocal = (Player*)I::EntityList->GetClientEntity(I::EngineClient->GetLocalPlayer());
if (!pLocal)
return;

auto pWeapon = pLocal->m_hActiveWeapon().Get();
if (!pWeapon)
return;

int& nWeaponIndex = pWeapon->m_iItemDefinitionIndex();

switch (nWeaponIndex)
{
//Redirect(Misc_t_FryingPan, Misc_t_GoldFryingPan);
Expand Down Expand Up @@ -755,14 +713,15 @@ void SkinChanger::ApplySkins()
Redirect(Sniper_t_Kukri, Sniper_t_KukriR);
default: break;
}
}

if (m_bForceFullUpdate)
{
I::ClientState->ForceFullUpdate();
m_bForceFullUpdate = false;
}
void SkinChanger::ApplySkin(Weapon* pWeapon)
{
if (!pWeapon)
return;

m_nCurrentWeaponIndex = nWeaponIndex;
int& nWeaponIndex = pWeapon->m_iItemDefinitionIndex();
RedirectIndex(nWeaponIndex);

auto attributeList = reinterpret_cast<CAttributeList*>(reinterpret_cast<std::uintptr_t>(pWeapon) + 0x9C4);
if (!attributeList)
Expand Down Expand Up @@ -814,6 +773,44 @@ void SkinChanger::ApplySkins()
attributeList->SetAttribute(attribute.attributeIndex, attribute.attributeValue);
}

void SkinChanger::ApplySkins()
{
if (!m_bInitialSkinLoad)
{
Load();
m_bInitialSkinLoad = true;
}

auto pLocal = (Player*)I::EntityList->GetClientEntity(I::EngineClient->GetLocalPlayer());
if (!pLocal)
return;

auto pWeapon = pLocal->m_hActiveWeapon().Get();
if (m_bForceFullUpdate)
{
I::ClientState->ForceFullUpdate();
m_bForceFullUpdate = false;
}

if (!pWeapon)
return;

int& nWeaponIndex = pWeapon->m_iItemDefinitionIndex();
RedirectIndex(nWeaponIndex);

m_nCurrentWeaponIndex = nWeaponIndex;

auto m_hMyWeapons = pLocal->m_hMyWeapons();
for (int i = 0; m_hMyWeapons[i].IsValid(); i++)
{
auto pWeapon = m_hMyWeapons[i].Get();
if (!pWeapon)
continue;

ApplySkin(pWeapon);
}
}

void SkinChanger::SetAttribute(int index, std::string attributeStr, float value)
{
if (index == -1)
Expand Down
33 changes: 33 additions & 0 deletions src/app/SkinChanger/SkinChanger.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,37 @@ struct SkinInfo
std::vector<Attribute> m_Attributes;
};

class Weapon;

#define NETVAR(_name, type, table, name) inline type &_name() \
{ \
static int offset = g_Netvars.GetNetvar(table, name); \
return *reinterpret_cast<type *>(reinterpret_cast<DWORD>(this) + offset); \
}

#define NETVAR_OFFSET(_name, type, name, offset) inline type &_name() \
{ \
return *reinterpret_cast<type *>(reinterpret_cast<DWORD>(this) + offset); \
}

#define MAX_WEAPONS 48

using MyWeapons = std::array<CHandle<Weapon>, MAX_WEAPONS>;

class Player
{
public:
NETVAR(m_hActiveWeapon, CHandle<Weapon>, "CBaseCombatCharacter", "m_hActiveWeapon");
NETVAR(m_hMyWeapons, MyWeapons, "CBaseCombatCharacter", "m_hMyWeapons");
};

class Weapon
{
public:
// For wahtever reason CEconEntity -> m_iItemDefinitionIndex only has an offset of like 0x24 bytes
NETVAR_OFFSET(m_iItemDefinitionIndex, int, "m_iItemDefinitionIndex", 2364);
};

class SkinChanger
{
std::unordered_map<int, SkinInfo> m_Skins;
Expand All @@ -99,6 +130,8 @@ class SkinChanger
bool m_bInitialSkinLoad = false;

public:
void RedirectIndex(int& weaponIndex);
void ApplySkin(Weapon* pWeapon);
void ApplySkins();

int GetWeaponIndex() const { return m_nCurrentWeaponIndex; }
Expand Down
Loading

0 comments on commit fa7be1a

Please sign in to comment.