2022-04-29 05:30:06 +02:00
|
|
|
#ifndef MDLCACHE_H
|
|
|
|
#define MDLCACHE_H
|
2022-05-05 17:53:05 +02:00
|
|
|
#include "tier0/threadtools.h"
|
2022-05-06 02:32:25 +02:00
|
|
|
#include "tier1/utldict.h"
|
2022-08-09 17:18:07 +02:00
|
|
|
#include "datacache/idatacache.h"
|
|
|
|
#include "datacache/imdlcache.h"
|
|
|
|
#include "public/studio.h"
|
2022-04-29 05:30:06 +02:00
|
|
|
|
2022-04-29 18:25:54 +02:00
|
|
|
struct RStaticProp_t
|
|
|
|
{
|
|
|
|
studiohdr_t* m_pStudioHDR{};
|
|
|
|
CStudioHWDataRef* m_pHardWareData{};
|
|
|
|
const char* m_szPropName{};
|
|
|
|
uint8_t m_pUnknown[0x62]{};
|
|
|
|
};
|
|
|
|
|
2022-10-15 00:07:51 +02:00
|
|
|
struct RMDLFallBack_t
|
2022-04-29 05:30:06 +02:00
|
|
|
{
|
2022-10-15 00:07:51 +02:00
|
|
|
studiohdr_t* m_pErrorHDR;
|
|
|
|
studiohdr_t* m_pEmptyHDR;
|
|
|
|
MDLHandle_t m_hErrorMDL;
|
|
|
|
MDLHandle_t m_hEmptyMDL;
|
2022-04-29 05:30:06 +02:00
|
|
|
|
2022-10-15 00:07:51 +02:00
|
|
|
RMDLFallBack_t(void)
|
|
|
|
: m_pErrorHDR(nullptr)
|
|
|
|
, m_pEmptyHDR(nullptr)
|
|
|
|
, m_hErrorMDL(NULL)
|
|
|
|
, m_hEmptyMDL(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// This must be cleared if 'common.rpak' is getting unloaded!
|
2022-04-29 05:30:06 +02:00
|
|
|
void Clear(void)
|
|
|
|
{
|
2022-10-15 00:07:51 +02:00
|
|
|
m_pErrorHDR = nullptr;
|
2022-04-29 05:30:06 +02:00
|
|
|
m_pEmptyHDR = nullptr;
|
|
|
|
m_hErrorMDL = NULL;
|
|
|
|
m_hEmptyMDL = NULL;
|
|
|
|
}
|
|
|
|
};
|
2022-05-05 02:54:17 +02:00
|
|
|
|
|
|
|
// only models with type "mod_studio" have this data
|
|
|
|
struct studiodata_t
|
|
|
|
{
|
2022-05-06 00:51:49 +02:00
|
|
|
DataCacheHandle_t m_MDLCache;
|
2022-05-05 17:53:05 +02:00
|
|
|
void* m_pAnimData; // !TODO: reverse struct.
|
2022-05-05 02:54:17 +02:00
|
|
|
unsigned short m_nRefCount;
|
|
|
|
unsigned short m_nFlags;
|
|
|
|
MDLHandle_t m_Handle;
|
|
|
|
void* Unk3; // ptr to flags and model string.
|
2022-05-06 00:51:49 +02:00
|
|
|
CStudioHWDataRef* m_pHardwareRef;
|
2022-05-06 02:32:25 +02:00
|
|
|
void* m_pMaterialTable; // contains a large table of CMaterialGlue objects.
|
2022-05-05 17:53:05 +02:00
|
|
|
int Unk5;
|
|
|
|
char pad[72];
|
|
|
|
CThreadFastMutex m_Mutex;
|
|
|
|
int m_nGuidLock; // always -1, set to 1 and 0 in CMDLCache::FindUncachedMDL.
|
2022-05-05 02:54:17 +02:00
|
|
|
};
|
|
|
|
|
2023-06-18 23:55:58 +02:00
|
|
|
extern RMDLFallBack_t* g_pMDLFallback;
|
|
|
|
extern std::unordered_set<MDLHandle_t> g_vBadMDLHandles;
|
2022-04-29 05:30:06 +02:00
|
|
|
|
2023-06-19 13:53:56 +02:00
|
|
|
class CMDLCache : public IMDLCache
|
2022-04-29 05:30:06 +02:00
|
|
|
{
|
|
|
|
public:
|
2022-04-29 18:25:54 +02:00
|
|
|
static studiohdr_t* FindMDL(CMDLCache* cache, MDLHandle_t handle, void* a3);
|
2022-05-05 17:53:05 +02:00
|
|
|
static void FindCachedMDL(CMDLCache* cache, studiodata_t* pStudioData, void* a3);
|
|
|
|
static studiohdr_t* FindUncachedMDL(CMDLCache* cache, MDLHandle_t handle, studiodata_t* pStudioData, void* a4);
|
2022-04-29 18:25:54 +02:00
|
|
|
static studiohdr_t* GetStudioHDR(CMDLCache* cache, MDLHandle_t handle);
|
2022-05-06 02:32:25 +02:00
|
|
|
static studiohwdata_t* GetHardwareData(CMDLCache* cache, MDLHandle_t handle);
|
2022-05-06 13:19:06 +02:00
|
|
|
static studiohdr_t* GetErrorModel(void);
|
|
|
|
static bool IsKnownBadModel(MDLHandle_t handle);
|
2022-04-29 05:30:06 +02:00
|
|
|
|
2023-06-19 13:53:56 +02:00
|
|
|
|
|
|
|
studiodata_t* GetStudioData(MDLHandle_t handle)
|
|
|
|
{
|
2023-06-25 14:56:30 +02:00
|
|
|
EnterCriticalSection(&m_MDLMutex);
|
2023-06-19 13:53:56 +02:00
|
|
|
studiodata_t* pStudioData = m_MDLDict.Element(handle);
|
2023-06-25 14:56:30 +02:00
|
|
|
LeaveCriticalSection(&m_MDLMutex);
|
2023-06-19 13:53:56 +02:00
|
|
|
|
|
|
|
return pStudioData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* GetModelName(MDLHandle_t handle)
|
|
|
|
{
|
2023-06-25 14:56:30 +02:00
|
|
|
EnterCriticalSection(&m_MDLMutex);
|
2023-06-19 13:53:56 +02:00
|
|
|
const char* szModelName = m_MDLDict.GetElementName(handle);
|
2023-06-25 14:56:30 +02:00
|
|
|
LeaveCriticalSection(&m_MDLMutex);
|
2023-06-19 13:53:56 +02:00
|
|
|
|
|
|
|
return szModelName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* GetMaterialTable(MDLHandle_t handle)
|
|
|
|
{
|
2023-06-25 14:56:30 +02:00
|
|
|
EnterCriticalSection(&m_MDLMutex);
|
2023-06-19 13:53:56 +02:00
|
|
|
studiodata_t* pStudioData = m_MDLDict.Element(handle);
|
2023-06-25 14:56:30 +02:00
|
|
|
LeaveCriticalSection(&m_MDLMutex);
|
2023-06-19 13:53:56 +02:00
|
|
|
|
|
|
|
return &pStudioData->m_pMaterialTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CUtlDict<studiodata_t*, MDLHandle_t> m_MDLDict;
|
2023-06-25 14:56:30 +02:00
|
|
|
CRITICAL_SECTION m_MDLMutex;
|
2023-06-19 13:53:56 +02:00
|
|
|
// !TODO: reverse the rest
|
2022-04-29 05:30:06 +02:00
|
|
|
};
|
|
|
|
|
2024-01-02 15:21:36 +01:00
|
|
|
inline studiohdr_t*(*CMDLCache__FindMDL)(CMDLCache* pCache, MDLHandle_t handle, void* a3);
|
|
|
|
inline void(*CMDLCache__FindCachedMDL)(CMDLCache* pCache, studiodata_t* pStudioData, void* a3);
|
|
|
|
|
|
|
|
inline studiohdr_t*(*CMDLCache__FindUncachedMDL)(CMDLCache* pCache, MDLHandle_t handle, studiodata_t* pStudioData, void* a4);
|
|
|
|
inline studiohdr_t*(*CMDLCache__GetStudioHDR)(CMDLCache* pCache, MDLHandle_t handle);
|
|
|
|
|
|
|
|
inline studiohwdata_t*(*CMDLCache__GetHardwareData)(CMDLCache* pCache, MDLHandle_t handle);
|
|
|
|
inline bool(*CStudioHWDataRef__SetFlags)(CStudioHWDataRef* ref, int64_t flags); // Probably incorrect name.
|
|
|
|
|
2023-06-19 13:53:56 +02:00
|
|
|
inline CMDLCache* g_pMDLCache = nullptr;
|
2023-07-08 02:45:08 +02:00
|
|
|
inline PSRWLOCK g_pMDLLock = nullptr; // Possibly a member? research required.
|
2022-04-29 05:30:06 +02:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2022-05-13 14:53:25 +02:00
|
|
|
class VMDLCache : public IDetour
|
2022-04-29 05:30:06 +02:00
|
|
|
{
|
|
|
|
virtual void GetAdr(void) const
|
|
|
|
{
|
2024-01-02 15:21:36 +01:00
|
|
|
LogFunAdr("CMDLCache::FindMDL", CMDLCache__FindMDL);
|
|
|
|
LogFunAdr("CMDLCache::FindCachedMDL", CMDLCache__FindCachedMDL);
|
|
|
|
LogFunAdr("CMDLCache::FindUncachedMDL", CMDLCache__FindUncachedMDL);
|
|
|
|
LogFunAdr("CMDLCache::GetStudioHDR", CMDLCache__GetStudioHDR);
|
|
|
|
LogFunAdr("CMDLCache::GetHardwareData", CMDLCache__GetHardwareData);
|
|
|
|
LogFunAdr("CStudioHWDataRef::SetFlags", CStudioHWDataRef__SetFlags);
|
|
|
|
|
|
|
|
LogVarAdr("g_MDLCache", g_pMDLCache);
|
|
|
|
LogVarAdr("g_MDLLock", g_pMDLLock);
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
|
|
|
virtual void GetFun(void) const
|
|
|
|
{
|
2024-01-02 15:21:36 +01:00
|
|
|
g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 48 8B F1 0F B7 EA").GetPtr(CMDLCache__FindMDL);
|
|
|
|
g_GameDll.FindPatternSIMD("4D 85 C0 74 7A 48 89 6C 24 ??").GetPtr(CMDLCache__FindCachedMDL);
|
|
|
|
g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 41 56 48 83 EC 20 48 8B E9 0F B7 FA").GetPtr(CMDLCache__FindUncachedMDL);
|
|
|
|
g_GameDll.FindPatternSIMD("40 53 48 83 EC 20 48 8D 0D ?? ?? ?? ?? 0F B7 DA FF 15 ?? ?? ?? ?? 48 8B 05 ?? ?? ?? ?? 48 8D 14 5B 48 8D 0D ?? ?? ?? ?? 48 8B 5C D0 ?? FF 15 ?? ?? ?? ?? 48 8B 03 48 8B 48 08").GetPtr(CMDLCache__GetStudioHDR);
|
|
|
|
g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 57 48 83 EC 20 48 8D 0D ?? ?? ?? ?? 0F B7 DA FF 15 ?? ?? ?? ?? 48 8B 05 ?? ?? ?? ?? 48 8D 14 5B 48 8D 0D ?? ?? ?? ?? 48 8B 7C D0 ?? FF 15 ?? ?? ?? ?? 48 8B 1F").GetPtr(CMDLCache__GetHardwareData);
|
|
|
|
g_GameDll.FindPatternSIMD("48 83 EC 08 4C 8D 14 12").GetPtr(CStudioHWDataRef__SetFlags);
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
|
|
|
virtual void GetVar(void) const
|
|
|
|
{
|
2023-06-19 13:53:56 +02:00
|
|
|
// Get MDLCache singleton from CStudioRenderContext::Connect
|
|
|
|
g_pMDLCache = g_GameDll.FindPatternSIMD("48 83 EC 28 48 8B 05 ?? ?? ?? ?? 48 8D 0D ?? ?? ?? ?? 48 85 C0 48 0F 45 C8 FF 05 ?? ?? ?? ?? 48 83 3D ?? ?? ?? ?? ?? 48 8D 05 ?? ?? ?? ??")
|
|
|
|
.FindPatternSelf("48 8D 05").ResolveRelativeAddressSelf(0x3, 0x7).RCast<CMDLCache*>();
|
2022-04-29 05:30:06 +02:00
|
|
|
|
2024-01-02 15:21:36 +01:00
|
|
|
g_pMDLLock = CMemory(CMDLCache__GetHardwareData).Offset(0x35).FindPatternSelf("48 8D 0D").ResolveRelativeAddressSelf(0x3, 0x7).RCast<PSRWLOCK>();
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
|
|
|
virtual void GetCon(void) const { }
|
2023-11-26 13:21:20 +01:00
|
|
|
virtual void Detour(const bool bAttach) const;
|
2022-04-29 05:30:06 +02:00
|
|
|
};
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#endif // MDLCACHE_H
|