2022-04-29 05:30:06 +02:00
|
|
|
//=====================================================================================//
|
|
|
|
//
|
|
|
|
// model loading and caching
|
|
|
|
//
|
|
|
|
// $NoKeywords: $
|
|
|
|
//=====================================================================================//
|
|
|
|
|
|
|
|
#include "core/stdafx.h"
|
2022-04-30 03:30:16 +02:00
|
|
|
#include "tier0/threadtools.h"
|
|
|
|
#include "tier1/cvar.h"
|
2022-04-29 05:30:06 +02:00
|
|
|
#include "datacache/mdlcache.h"
|
|
|
|
#include "datacache/imdlcache.h"
|
|
|
|
#include "engine/sys_utils.h"
|
|
|
|
#include "rtech/rtech_utils.h"
|
|
|
|
#include "public/include/studio.h"
|
2022-05-01 23:03:20 +02:00
|
|
|
#include "tier1/utldict.h"
|
2022-04-29 05:30:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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-04-29 05:30:06 +02:00
|
|
|
{
|
2022-05-05 02:54:17 +02:00
|
|
|
studiodata_t* pStudioData; // rbx
|
|
|
|
void* pMDLCache; // rax
|
|
|
|
studiohdr_t* result; // rax
|
2022-04-29 05:30:06 +02:00
|
|
|
|
|
|
|
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));
|
2022-04-29 05:30:06 +02:00
|
|
|
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);
|
2022-04-29 05:30:06 +02:00
|
|
|
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)
|
2022-04-29 05:30:06 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-05 02:54:17 +02:00
|
|
|
if ((pStudioData->m_nFlags & STUDIOHDR_FLAGS_NEEDS_DEFERRED_ADDITIVE | STUDIOHDR_FLAGS_OBSOLETE) != 0)
|
2022-04-29 05:30:06 +02:00
|
|
|
{
|
2022-05-05 02:54:17 +02:00
|
|
|
pMDLCache = *reinterpret_cast<void**>(pStudioData);
|
|
|
|
if (pStudioData->m_MDLCache)
|
2022-04-29 05:30:06 +02:00
|
|
|
{
|
|
|
|
if (a3)
|
|
|
|
{
|
2022-05-05 02:54:17 +02:00
|
|
|
FindCachedMDL(cache, pStudioData, a3);
|
|
|
|
pMDLCache = *reinterpret_cast<void**>(pStudioData);
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
|
|
|
LABEL_6:
|
2022-05-05 02:54:17 +02:00
|
|
|
result = *reinterpret_cast<studiohdr_t**>(pMDLCache);
|
2022-04-29 05:30:06 +02:00
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
2022-05-05 02:54:17 +02:00
|
|
|
return FindUncachedMDL(cache, handle, pStudioData, a3);
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
2022-05-05 02:54:17 +02:00
|
|
|
pMDLCache = pStudioData->Unk0;
|
|
|
|
if (pMDLCache)
|
2022-04-29 05:30:06 +02:00
|
|
|
goto LABEL_6;
|
|
|
|
}
|
2022-05-05 02:54:17 +02:00
|
|
|
return FindUncachedMDL(cache, handle, pStudioData, a3);
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: finds an MDL cached
|
|
|
|
// Input : *this -
|
|
|
|
// *a2 -
|
|
|
|
// *a3 -
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-30 03:30:16 +02:00
|
|
|
void CMDLCache::FindCachedMDL(CMDLCache* cache, void* a2, void* a3)
|
2022-04-29 05:30:06 +02:00
|
|
|
{
|
2022-04-30 03:30:16 +02:00
|
|
|
__int64 v6; // rax
|
|
|
|
|
|
|
|
if (a3)
|
|
|
|
{
|
|
|
|
CThreadFastMutex::WaitForLock((CThreadFastMutex*)a2 + 128);
|
|
|
|
*(_QWORD*)((int64_t)a3 + 2176) = *(_QWORD*)((int64_t)a2 + 88);
|
|
|
|
v6 = *(_QWORD*)((int64_t)a2 + 88);
|
|
|
|
if (v6)
|
|
|
|
*(_QWORD*)(v6 + 2168) = (int64_t)a3;
|
|
|
|
*(_QWORD*)((int64_t)a2 + 88) = (int64_t)a3;
|
|
|
|
*(_QWORD*)((int64_t)a3 + 2160) = (int64_t)cache;
|
|
|
|
*(_WORD*)((int64_t)a3 + 2184) = *(_WORD*)((int64_t)a2 + 20);
|
|
|
|
CThreadFastMutex::ReleaseWaiter((CThreadFastMutex*)a2 + 128);
|
|
|
|
}
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: finds an MDL uncached
|
|
|
|
// Input : *this -
|
|
|
|
// handle -
|
|
|
|
// *a3 -
|
2022-04-30 03:30:16 +02:00
|
|
|
// *a4 -
|
2022-04-29 05:30:06 +02:00
|
|
|
// Output : a pointer to the studiohdr_t object
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
studiohdr_t* CMDLCache::FindUncachedMDL(CMDLCache* cache, MDLHandle_t handle, void* a3, void* a4)
|
|
|
|
{
|
|
|
|
const char* v8; // rdi
|
|
|
|
__int64 v9; // rax
|
|
|
|
int v10; // eax
|
|
|
|
const char* v11; // r11
|
|
|
|
int v12; // eax
|
|
|
|
const char* v13; // r11
|
|
|
|
int v14; // eax
|
|
|
|
bool v16; // zf
|
|
|
|
studiohdr_t* v17; // rdi
|
|
|
|
studiohdr_t** v18; // rax
|
2022-05-01 05:38:51 +02:00
|
|
|
bool bOldModel {};
|
|
|
|
bool bInvalidHandle{};
|
2022-04-29 05:30:06 +02:00
|
|
|
|
2022-04-30 03:30:16 +02:00
|
|
|
CThreadFastMutex::WaitForLock((CThreadFastMutex*)a3 + 0x80);
|
2022-04-29 05:30:06 +02:00
|
|
|
EnterCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
|
|
|
|
void* modelCache = cache->m_pModelCacheSection;
|
|
|
|
v8 = (const char*)(*(_QWORD*)((int64)modelCache + 24 * static_cast<int64>(handle) + 8));
|
|
|
|
LeaveCriticalSection(reinterpret_cast<LPCRITICAL_SECTION>(&*m_MDLMutex));
|
2022-05-01 05:38:51 +02:00
|
|
|
if (IsBadReadPtrV2((void*)v8))
|
|
|
|
{
|
|
|
|
bInvalidHandle = true;
|
|
|
|
goto LABEL_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:30:06 +02:00
|
|
|
v9 = -1i64;
|
|
|
|
do
|
|
|
|
++v9;
|
|
|
|
while (v8[v9]);
|
|
|
|
|
|
|
|
if (v9 < 5 ||
|
|
|
|
(_stricmp(&v8[v9 - 5], ".rmdl") != 0) &&
|
|
|
|
(_stricmp(&v8[v9 - 5], ".rrig") != 0) &&
|
|
|
|
(_stricmp(&v8[v9 - 5], ".rpak") != 0))
|
|
|
|
{
|
2022-04-30 05:06:59 +02:00
|
|
|
bOldModel = true;
|
2022-04-29 05:30:06 +02:00
|
|
|
goto LABEL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_pRTech->StringToGuid(v8);
|
|
|
|
v16 = *(_QWORD*)a3 == 0i64;
|
|
|
|
*(_BYTE*)((int64)a3 + 152) = 0;
|
|
|
|
if (v16)
|
|
|
|
{
|
|
|
|
v18 = *(studiohdr_t***)((int64)a3 + 8);
|
|
|
|
if (v18)
|
|
|
|
{
|
|
|
|
v17 = *v18;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-29 22:46:37 +02:00
|
|
|
LABEL_ERROR:
|
2022-05-04 02:25:27 +02:00
|
|
|
if (std::find(g_BadMDLHandles.begin(), g_BadMDLHandles.end(), handle) == g_BadMDLHandles.end())
|
2022-04-29 22:46:37 +02:00
|
|
|
{
|
2022-05-01 05:38:51 +02:00
|
|
|
if (bInvalidHandle)
|
|
|
|
Error(eDLL_T::ENGINE, "Model with handle \"hu\" not found; replacing with \"%s\".\n", handle, ERROR_MODEL);
|
|
|
|
else if (bOldModel)
|
|
|
|
Error(eDLL_T::ENGINE, "Attempted to load old model \"%s\"; replace with rmdl.\n", v8);
|
|
|
|
else
|
2022-04-30 05:06:59 +02:00
|
|
|
{
|
|
|
|
if (g_pMDLFallback->m_hErrorMDL)
|
|
|
|
Error(eDLL_T::ENGINE, "Model \"%s\" not found; replacing with \"%s\".\n", v8, ERROR_MODEL);
|
|
|
|
else
|
|
|
|
Error(eDLL_T::ENGINE, "Model \"%s\" not found and \"%s\" couldn't be loaded.\n", v8, ERROR_MODEL);
|
|
|
|
}
|
2022-04-30 03:30:16 +02:00
|
|
|
|
2022-05-04 02:25:27 +02:00
|
|
|
g_BadMDLHandles.push_back(handle);
|
2022-04-29 22:46:37 +02:00
|
|
|
}
|
2022-04-29 05:30:06 +02:00
|
|
|
v17 = g_pMDLFallback->m_pErrorHDR;
|
2022-04-30 03:30:16 +02:00
|
|
|
old_gather_props->SetValue(true); // mdl/error.rmdl fallback is not supported (yet) in the new GatherProps solution!
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v_CMDLCache__FindCachedMDL(cache, a3, a4);
|
2022-05-04 12:44:01 +02:00
|
|
|
if ((__int64)*(studiohdr_t**)a3)
|
|
|
|
{
|
|
|
|
if ((__int64)*(studiohdr_t**)a3 == 0xDEADFEEDDEADFEED)
|
|
|
|
v17 = g_pMDLFallback->m_pErrorHDR;
|
|
|
|
else
|
|
|
|
v17 = **(studiohdr_t***)a3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
v17 = g_pMDLFallback->m_pErrorHDR;
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
2022-04-30 03:30:16 +02:00
|
|
|
CThreadFastMutex::ReleaseWaiter((CThreadFastMutex*)a3 + 128);
|
2022-04-29 05:30:06 +02:00
|
|
|
return v17;
|
|
|
|
}
|
|
|
|
|
2022-04-29 18:25:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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)
|
2022-04-29 05:30:06 +02:00
|
|
|
{
|
|
|
|
__int64 v2; // rbx
|
|
|
|
__int64 v3; // rbx
|
|
|
|
__int64 v4; // rdx
|
2022-04-29 21:05:26 +02:00
|
|
|
studiohdr_t* result = nullptr; // rax
|
2022-04-29 05:30:06 +02:00
|
|
|
|
|
|
|
if (!handle)
|
|
|
|
{
|
2022-04-29 21:05:26 +02:00
|
|
|
LABEL_ERROR:
|
2022-04-29 05:30:06 +02:00
|
|
|
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));
|
2022-04-29 22:11:35 +02:00
|
|
|
if (*(_QWORD*)(v3))
|
2022-04-29 21:05:26 +02:00
|
|
|
{
|
2022-04-29 22:11:35 +02:00
|
|
|
v4 = *(_QWORD*)(*(_QWORD*)(*(_QWORD*)v3 + 8i64) + 24i64);
|
|
|
|
if (v4)
|
|
|
|
result = (studiohdr_t*)(v4 + 16);
|
2022-04-29 21:05:26 +02:00
|
|
|
}
|
2022-04-29 05:30:06 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-29 18:25:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: gets the studio hardware data reference from cache pool by handle
|
|
|
|
// Input : *this -
|
|
|
|
// handle -
|
|
|
|
// Output : a pointer to the CStudioHWDataRef object
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
CStudioHWDataRef* CMDLCache::GetStudioHardwareRef(CMDLCache* cache, MDLHandle_t handle)
|
|
|
|
{
|
|
|
|
__int64 v2; // rbx
|
|
|
|
__int64 v3; // rdi
|
|
|
|
__int64 v4; // rbx
|
|
|
|
__int64 result; // rax
|
|
|
|
|
|
|
|
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 (!v3)
|
|
|
|
{
|
2022-04-29 22:11:35 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-05-04 12:44:01 +02:00
|
|
|
v3 = *(_QWORD*)(m_MDLDict.Deref().GetPtr() + 24i64 * g_pMDLFallback->m_hErrorMDL + 16);
|
2022-04-29 18:25:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*(_QWORD*)v3)
|
|
|
|
{
|
2022-05-04 12:44:01 +02:00
|
|
|
if (*(_QWORD*)v3 == 0xDEADFEEDDEADFEED)
|
|
|
|
return nullptr;
|
|
|
|
|
2022-04-29 18:25:54 +02:00
|
|
|
v4 = *(_QWORD*)(*(_QWORD*)v3 + 8i64);
|
|
|
|
AcquireSRWLockExclusive(reinterpret_cast<PSRWLOCK>(&*m_MDLLock));
|
|
|
|
v_CStudioHWDataRef__SetFlags(reinterpret_cast<CStudioHWDataRef*>(v4), 1i64); // !!! DECLARED INLINE IN < S3 !!!
|
|
|
|
ReleaseSRWLockExclusive(reinterpret_cast<PSRWLOCK>(&*m_MDLLock));
|
|
|
|
}
|
|
|
|
if ((*(_BYTE*)(v3 + 18) & 1) != 0)
|
|
|
|
result = *(_QWORD*)(v3 + 0x20) + 16i64;
|
|
|
|
else
|
|
|
|
result = 0i64;
|
|
|
|
return reinterpret_cast<CStudioHWDataRef*>(result);
|
|
|
|
}
|
|
|
|
|
2022-05-01 05:38:51 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:30:06 +02:00
|
|
|
void MDLCache_Attach()
|
|
|
|
{
|
|
|
|
DetourAttach((LPVOID*)&v_CMDLCache__FindMDL, &CMDLCache::FindMDL);
|
2022-04-30 03:30:16 +02:00
|
|
|
DetourAttach((LPVOID*)&v_CMDLCache__FindCachedMDL, &CMDLCache::FindCachedMDL);
|
2022-04-29 05:30:06 +02:00
|
|
|
DetourAttach((LPVOID*)&v_CMDLCache__FindUncachedMDL, &CMDLCache::FindUncachedMDL);
|
2022-04-29 18:25:54 +02:00
|
|
|
DetourAttach((LPVOID*)&v_CMDLCache__GetStudioHardwareRef, &CMDLCache::GetStudioHardwareRef);
|
2022-04-29 21:05:26 +02:00
|
|
|
DetourAttach((LPVOID*)&v_CMDLCache__GetStudioHDR, &CMDLCache::GetStudioHDR);
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MDLCache_Detach()
|
|
|
|
{
|
|
|
|
DetourDetach((LPVOID*)&v_CMDLCache__FindMDL, &CMDLCache::FindMDL);
|
2022-04-30 03:30:16 +02:00
|
|
|
DetourDetach((LPVOID*)&v_CMDLCache__FindCachedMDL, &CMDLCache::FindCachedMDL);
|
2022-04-29 05:30:06 +02:00
|
|
|
DetourDetach((LPVOID*)&v_CMDLCache__FindUncachedMDL, &CMDLCache::FindUncachedMDL);
|
2022-04-29 18:25:54 +02:00
|
|
|
DetourDetach((LPVOID*)&v_CMDLCache__GetStudioHardwareRef, &CMDLCache::GetStudioHardwareRef);
|
2022-04-29 21:05:26 +02:00
|
|
|
DetourDetach((LPVOID*)&v_CMDLCache__GetStudioHDR, &CMDLCache::GetStudioHDR);
|
2022-04-29 05:30:06 +02:00
|
|
|
}
|