r5sdk/r5dev/datacache/mdlcache.cpp

318 lines
12 KiB
C++
Raw Normal View History

//=====================================================================================//
//
// model loading and caching
//
// $NoKeywords: $
//=====================================================================================//
#include "core/stdafx.h"
#include "tier0/threadtools.h"
#include "tier1/cvar.h"
#include "datacache/mdlcache.h"
#include "datacache/imdlcache.h"
#include "datacache/idatacache.h"
#include "engine/sys_utils.h"
#include "rtech/rtech_utils.h"
#include "public/include/studio.h"
#include "tier1/utldict.h"
//-----------------------------------------------------------------------------
// Purpose: finds an MDL
// Input : *this -
// handle -
// *a3 -
// Output : a pointer to the studiohdr_t object
//-----------------------------------------------------------------------------
2022-05-05 02:54:17 +02:00
studiohdr_t* CMDLCache::FindMDL(CMDLCache* cache, MDLHandle_t handle, void* a3)
{
2022-05-05 02:54:17 +02:00
studiodata_t* pStudioData; // rbx
void* pMDLCache; // rax
studiohdr_t* pStudioHdr; // rax
EnterCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
2022-05-05 02:54:17 +02:00
auto mdlDict = CUtlDict<studiodata_t*, MDLHandle_t>(m_MDLDict.Deref().GetPtr());
pStudioData = mdlDict.Find(static_cast<int64>(handle));
LeaveCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
if (!g_pMDLFallback->m_hErrorMDL || !g_pMDLFallback->m_hEmptyMDL)
{
2022-05-05 02:54:17 +02:00
studiohdr_t* pStudioHDR = **reinterpret_cast<studiohdr_t***>(pStudioData);
string svStudio = ConvertToUnixPath(string(pStudioHDR->name));
if (strcmp(svStudio.c_str(), ERROR_MODEL) == 0)
{
g_pMDLFallback->m_pErrorHDR = pStudioHDR;
g_pMDLFallback->m_hErrorMDL = handle;
}
if (strcmp(svStudio.c_str(), EMPTY_MODEL) == 0)
{
g_pMDLFallback->m_pEmptyHDR = pStudioHDR;
g_pMDLFallback->m_hEmptyMDL = handle;
}
}
2022-05-05 02:54:17 +02:00
if (!pStudioData)
{
if (!g_pMDLFallback->m_hErrorMDL)
Error(eDLL_T::ENGINE, "Model with handle \"%hu\" not found and \"%s\" couldn't be loaded.\n", handle, ERROR_MODEL);
return g_pMDLFallback->m_pErrorHDR;
}
int nFlags = STUDIOHDR_FLAGS_NEEDS_DEFERRED_ADDITIVE | STUDIOHDR_FLAGS_OBSOLETE;
if ((pStudioData->m_nFlags & nFlags))
{
2022-05-05 02:54:17 +02:00
pMDLCache = *reinterpret_cast<void**>(pStudioData);
if (pStudioData->m_MDLCache)
{
if (a3)
{
2022-05-05 02:54:17 +02:00
FindCachedMDL(cache, pStudioData, a3);
pMDLCache = *reinterpret_cast<void**>(pStudioData);
}
LABEL_6:
pStudioHdr = *reinterpret_cast<studiohdr_t**>(pMDLCache);
if (pStudioHdr)
return pStudioHdr;
2022-05-05 02:54:17 +02:00
return FindUncachedMDL(cache, handle, pStudioData, a3);
}
pMDLCache = pStudioData->m_pAnimData;
2022-05-05 02:54:17 +02:00
if (pMDLCache)
goto LABEL_6;
}
2022-05-05 02:54:17 +02:00
return FindUncachedMDL(cache, handle, pStudioData, a3);
}
//-----------------------------------------------------------------------------
// Purpose: finds an MDL cached
// Input : *this -
// *a2 -
// *a3 -
//-----------------------------------------------------------------------------
void CMDLCache::FindCachedMDL(CMDLCache* cache, studiodata_t* pStudioData, void* a3)
{
__int64 v6; // rax
if (a3)
{
pStudioData->m_Mutex.WaitForLock();
*(_QWORD*)((int64_t)a3 + 0x880) = *(_QWORD*)&pStudioData->pad[0x24];
v6 = *(_QWORD*)&pStudioData->pad[0x24];
if (v6)
*(_QWORD*)(v6 + 0x878) = (int64_t)a3;
*(_QWORD*)&pStudioData->pad[0x24] = (int64_t)a3;
*(_QWORD*)((int64_t)a3 + 0x870) = (int64_t)cache;
*(_WORD*)((int64_t)a3 + 0x888) = pStudioData->m_Handle;
pStudioData->m_Mutex.ReleaseWaiter();
}
}
//-----------------------------------------------------------------------------
// Purpose: finds an MDL uncached
// Input : *this -
// handle -
// *a3 -
// *a4 -
// Output : a pointer to the studiohdr_t object
//-----------------------------------------------------------------------------
studiohdr_t* CMDLCache::FindUncachedMDL(CMDLCache* cache, MDLHandle_t handle, studiodata_t* pStudioData, void* a4)
{
2022-05-05 20:36:13 +02:00
const char* szModelName; // rdi
int64_t nExtensionOffset; // rax
studiohdr_t* pStudioHdr; // rdi
studiohdr_t** ppStudioHdr; // rax
void* pModelCache;
bool bOldModel{};
bool bInvalidHandle{};
pStudioData->m_Mutex.WaitForLock();
EnterCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
2022-05-05 20:36:13 +02:00
pModelCache = cache->m_pModelCacheSection;
szModelName = (const char*)(*(_QWORD*)((int64)pModelCache + 24 * static_cast<int64>(handle) + 8));
LeaveCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
2022-05-05 20:36:13 +02:00
if (IsBadReadPtrV2((void*)szModelName))
{
bInvalidHandle = true;
goto LABEL_ERROR;
}
2022-05-05 20:36:13 +02:00
nExtensionOffset = -1i64;
do
2022-05-05 20:36:13 +02:00
++nExtensionOffset;
while (szModelName[nExtensionOffset]);
2022-05-05 20:36:13 +02:00
if (nExtensionOffset < 5 ||
(_stricmp(&szModelName[nExtensionOffset - 5], ".rmdl") != 0) &&
(_stricmp(&szModelName[nExtensionOffset - 5], ".rrig") != 0) &&
(_stricmp(&szModelName[nExtensionOffset - 5], ".rpak") != 0))
{
bOldModel = true;
goto LABEL_ERROR;
}
2022-05-05 20:36:13 +02:00
LOBYTE(pStudioData->m_nGuidLock) = 1;
g_pRTech->StringToGuid(szModelName);
LOBYTE(pStudioData->m_nGuidLock) = 0;
if (!pStudioData->m_MDLCache)
{
2022-05-05 20:36:13 +02:00
ppStudioHdr = (studiohdr_t**)pStudioData->m_pAnimData;
if (ppStudioHdr)
{
2022-05-05 20:36:13 +02:00
pStudioHdr = *ppStudioHdr;
}
else
{
LABEL_ERROR:
if (std::find(g_BadMDLHandles.begin(), g_BadMDLHandles.end(), handle) == g_BadMDLHandles.end())
{
if (bInvalidHandle)
Error(eDLL_T::ENGINE, "Model with handle \"hu\" not found; replacing with \"%s\".\n", handle, ERROR_MODEL);
else if (bOldModel)
2022-05-05 20:36:13 +02:00
Error(eDLL_T::ENGINE, "Attempted to load old model \"%s\"; replace with rmdl.\n", szModelName);
else
{
if (g_pMDLFallback->m_hErrorMDL)
2022-05-05 20:36:13 +02:00
Error(eDLL_T::ENGINE, "Model \"%s\" not found; replacing with \"%s\".\n", szModelName, ERROR_MODEL);
else
2022-05-05 20:36:13 +02:00
Error(eDLL_T::ENGINE, "Model \"%s\" not found and \"%s\" couldn't be loaded.\n", szModelName, ERROR_MODEL);
}
g_BadMDLHandles.push_back(handle);
}
2022-05-05 20:36:13 +02:00
pStudioHdr = g_pMDLFallback->m_pErrorHDR;
old_gather_props->SetValue(true); // mdl/error.rmdl fallback is not supported (yet) in the new GatherProps solution!
}
}
else
{
v_CMDLCache__FindCachedMDL(cache, pStudioData, a4);
if ((__int64)*(studiohdr_t**)pStudioData)
{
if ((__int64)*(studiohdr_t**)pStudioData == 0xDEADFEEDDEADFEED)
2022-05-05 20:36:13 +02:00
pStudioHdr = g_pMDLFallback->m_pErrorHDR;
else
2022-05-05 20:36:13 +02:00
pStudioHdr = **(studiohdr_t***)pStudioData;
}
else
2022-05-05 20:36:13 +02:00
pStudioHdr = g_pMDLFallback->m_pErrorHDR;
}
pStudioData->m_Mutex.ReleaseWaiter();
2022-05-05 20:36:13 +02:00
return pStudioHdr;
}
//-----------------------------------------------------------------------------
// Purpose: gets the studiohdr from cache pool by handle
// Input : *this -
// handle -
// Output : a pointer to the studiohdr_t object
//-----------------------------------------------------------------------------
studiohdr_t* CMDLCache::GetStudioHDR(CMDLCache* pMDLCache, MDLHandle_t handle)
{
__int64 v2; // rbx
__int64 v3; // rbx
__int64 v4; // rdx
studiohdr_t* result = nullptr; // rax
if (!handle)
{
LABEL_ERROR:
if (!g_pMDLFallback->m_hErrorMDL)
Error(eDLL_T::ENGINE, "Model with handle \"%hu\" not found and \"%s\" couldn't be loaded.\n", handle, ERROR_MODEL);
return g_pMDLFallback->m_pErrorHDR;
}
v2 = handle;
EnterCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
v3 = *(_QWORD*)(m_MDLDict.Deref().GetPtr() + 24 * v2 + 16);
LeaveCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
if (*(_QWORD*)(v3))
{
v4 = *(_QWORD*)(*(_QWORD*)(*(_QWORD*)v3 + 8i64) + 24i64);
if (v4)
result = (studiohdr_t*)(v4 + 16);
}
return result;
}
//-----------------------------------------------------------------------------
// Purpose: gets the studio hardware data from cache pool by handle
// Input : *this -
// handle -
// Output : a pointer to the studiohwdata_t object
//-----------------------------------------------------------------------------
studiohwdata_t* CMDLCache::GetStudioHardware(CMDLCache* cache, MDLHandle_t handle)
{
studiodata_t* pStudioData; // rdi
void* pAnimData; // rbx
studiohwdata_t* result; // rax
EnterCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
pStudioData = *(studiodata_t**)(m_MDLDict.Deref().GetPtr() + 24 * static_cast<int64_t>(handle) + 16);
LeaveCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
if (!pStudioData)
{
if (!g_pMDLFallback->m_hErrorMDL)
{
Error(eDLL_T::ENGINE, "Studio hardware with handle \"%hu\" not found and \"%s\" couldn't be loaded.\n", handle, ERROR_MODEL);
return nullptr;
}
pStudioData = *(studiodata_t**)(m_MDLDict.Deref().GetPtr() + 24i64 * g_pMDLFallback->m_hErrorMDL + 16);
}
if (pStudioData->m_MDLCache)
{
if (reinterpret_cast<int64_t>(pStudioData->m_MDLCache) == 0xDEADFEEDDEADFEED)
return nullptr;
pAnimData = (void*)*((_QWORD*)pStudioData->m_MDLCache + 1);
AcquireSRWLockExclusive(reinterpret_cast<PSRWLOCK>(&*m_MDLLock));
v_CStudioHWDataRef__SetFlags(reinterpret_cast<CStudioHWDataRef*>(pAnimData), 1i64); // !!! DECLARED INLINE IN < S3 !!!
ReleaseSRWLockExclusive(reinterpret_cast<PSRWLOCK>(&*m_MDLLock));
}
if ((pStudioData->m_nFlags & STUDIODATA_FLAGS_STUDIOMESH_LOADED))
result = &pStudioData->m_pHardwareRef->m_HardwareData;
else
result = nullptr;
return result;
}
//-----------------------------------------------------------------------------
// Purpose: gets the studio material glue from cache pool by handle
// Input : *this -
// handle -
// Output : a pointer to the CMaterialGlue object
//-----------------------------------------------------------------------------
void* CMDLCache::GetStudioMaterialGlue(CMDLCache* cache, MDLHandle_t handle)
{
__int64 v2; // rbx
__int64 v3; // rbx
v2 = handle;
EnterCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
v3 = *(_QWORD*)(m_MDLDict.Deref().GetPtr() + 24 * v2 + 16);
LeaveCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
return (char*)v3 + 40;
}
void MDLCache_Attach()
{
DetourAttach((LPVOID*)&v_CMDLCache__FindMDL, &CMDLCache::FindMDL);
DetourAttach((LPVOID*)&v_CMDLCache__FindCachedMDL, &CMDLCache::FindCachedMDL);
DetourAttach((LPVOID*)&v_CMDLCache__FindUncachedMDL, &CMDLCache::FindUncachedMDL);
DetourAttach((LPVOID*)&v_CMDLCache__GetStudioHardware, &CMDLCache::GetStudioHardware);
DetourAttach((LPVOID*)&v_CMDLCache__GetStudioHDR, &CMDLCache::GetStudioHDR);
}
void MDLCache_Detach()
{
DetourDetach((LPVOID*)&v_CMDLCache__FindMDL, &CMDLCache::FindMDL);
DetourDetach((LPVOID*)&v_CMDLCache__FindCachedMDL, &CMDLCache::FindCachedMDL);
DetourDetach((LPVOID*)&v_CMDLCache__FindUncachedMDL, &CMDLCache::FindUncachedMDL);
DetourDetach((LPVOID*)&v_CMDLCache__GetStudioHardware, &CMDLCache::GetStudioHardware);
DetourDetach((LPVOID*)&v_CMDLCache__GetStudioHDR, &CMDLCache::GetStudioHDR);
}