Skip to content

Commit 357fbac

Browse files
committed
repo: do some cleanup
1 parent 2e41400 commit 357fbac

4 files changed

Lines changed: 84 additions & 356 deletions

File tree

source/cache.cpp

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
#include "istudiorender.h"
99
#include "cache.h"
1010

11-
#if Cache_Experimental == 1
11+
#if Cache_Experimental
1212
#include <unordered_map>
1313
#include "materialsystem/imaterialsystem.h"
1414
#endif
1515

1616
static SourceSDK::FactoryLoader vphysics_loader("vphysics");
1717
static SourceSDK::FactoryLoader datacache_loader("datacache");
18-
#if Cache_Experimental == 1
18+
#if Cache_Experimental
1919
static SourceSDK::FactoryLoader materialsystem_loader("materialsystem");
2020
#endif
2121

2222
static IMDLCache* MDLCache = nullptr;
2323
static IPhysicsCollision* PhysicsCollision = nullptr;
2424

25-
#if Cache_Experimental == 1
25+
#if Cache_Experimental
2626
static IMaterialSystem* MaterialSystem = nullptr;
2727
static std::unordered_map<const char*, bool> material_cache;
2828
#endif
@@ -73,7 +73,7 @@ class IMDLCacheUpdate : public IMDLCacheNotify
7373
if (type == MDLCACHE_STUDIOHWDATA) {
7474
//MDLCache->RestoreHardwareData(handle, (FSAsyncControl_t*)0, (FSAsyncControl_t*)0); // This fixes some weird engine issue. As soon as you add a IMDLCacheNotify, the Lightning and collision of most models will break.
7575
} else if (type == MDLCACHE_VCOLLIDE) {
76-
MDLCache->ReloadVCollide(handle);
76+
// MDLCache->ReloadVCollide(handle);
7777
}
7878
}
7979

@@ -92,8 +92,11 @@ class IMDLCacheUpdate : public IMDLCacheNotify
9292
{
9393
return false;
9494
}
95+
96+
private:
97+
IMDLCacheNotify* m_pOriginalNotify = nullptr;
9598
};
96-
static IMDLCacheUpdate* MDLCacheUpdate = new IMDLCacheUpdate;
99+
static IMDLCacheUpdate MDLCacheUpdate;
97100

98101
/*
99102
* ClearCache
@@ -107,7 +110,7 @@ unsigned ClearCache(void* params) {
107110
Msg("[Cache] Starting to clear the Cache\n");
108111
CThreadMutex().Lock();
109112

110-
#if Cache_Debug == 1
113+
#if Cache_Debug
111114
Msg("----Cache----\n");
112115
for (auto& [mdl, entry] : vars->cache) {
113116
Msg(MDLCache->GetModelName(mdl));
@@ -133,21 +136,21 @@ unsigned ClearCache(void* params) {
133136
vars->MDLCache->Flush(MDLCACHE_FLUSH_VIRTUALMODEL);
134137
vars->MDLCache->Flush(MDLCACHE_FLUSH_AUTOPLAY);
135138
vars->MDLCache->Flush(MDLCACHE_FLUSH_VERTEXES);
136-
vars->MDLCache->Flush(MDLCACHE_FLUSH_COMBINED_DATA);
139+
//vars->MDLCache->Flush(MDLCACHE_FLUSH_COMBINED_DATA);
137140
//vars->MDLCache->Flush(MDLCACHE_FLUSH_IGNORELOCK);
138141
}
139142

140-
#if Cache_Experimental == 1
143+
#if Cache_Experimental
141144
bool loop = true;
142145
MaterialHandle_t last = MaterialSystem->FirstMaterial();
143-
#if Cache_Debug == 1
146+
#if Cache_Debug
144147
Msg("----Materials----\n");
145148
#endif
146149
while (loop) {
147150
IMaterial* mat = MaterialSystem->GetMaterial(last);
148151
const char* name = mat->GetName();
149152
if (!vars->material_cache[name]) {
150-
#if Cache_Debug == 1
153+
#if Cache_Debug
151154
Msg(mat->GetName());
152155
Msg("\n");
153156
#endif
@@ -164,7 +167,7 @@ unsigned ClearCache(void* params) {
164167
#endif
165168

166169
if (vars->fullflush) {
167-
#if Cache_Debug == 1
170+
#if Cache_Debug
168171
Msg("----DataCache----\n");
169172
Msg("Cleared: ");
170173
Msg(std::to_string(vars->DataCache->Flush(true) / 1024 / 1024).c_str());
@@ -185,7 +188,7 @@ unsigned ClearCache(void* params) {
185188
*/
186189
void CacheMgr::Connect(IGameEvent* event)
187190
{
188-
#if Cache_AwaysFlush == 0 // We don't need to clear the Cache because it got already cleared when the client disconnected from the server if Cache_AwaysFlush is enabled.
191+
#if !Cache_AwaysFlush // We don't need to clear the Cache because it got already cleared when the client disconnected from the server if Cache_AwaysFlush is enabled.
189192
const char* new_address = event->GetString("address");
190193
const char* new_map = event->GetString("mapname");
191194
if (strcmp(this->last_server, "") == 1 && strcmp(this->last_map, new_map) == 1) {
@@ -202,7 +205,7 @@ void CacheMgr::Connect(IGameEvent* event)
202205
this->last_map = strdup(new_map);
203206
#endif
204207

205-
#if Cache_Experimental == 1
208+
#if Cache_Experimental
206209
// This will create a list of all Materials that exist before connecting to a server.
207210
bool loop = true;
208211
MaterialHandle_t last = MaterialSystem->FirstMaterial();
@@ -275,7 +278,7 @@ bool CacheMgr::Flush(int delay, bool full, bool threaded, bool force)
275278
vars->fullflush = full;
276279
vars->MDLCache = MDLCache;
277280
//vars->DataCache = DataCache;
278-
#if Cache_Experimental == 1
281+
#if Cache_Experimental
279282
vars->material_cache = material_cache;
280283
vars->MaterialSystem = MaterialSystem;
281284
#endif
@@ -308,30 +311,28 @@ CacheMgr::CacheMgr()
308311
if (PhysicsCollision == nullptr)
309312
ThrowError("unable to initialize IPhysicsCollision");
310313

311-
#if Cache_Experimental == 1
314+
#if Cache_Experimental
312315
MaterialSystem = (IMaterialSystem*)materialsystem_loader.GetFactory()(MATERIAL_SYSTEM_INTERFACE_VERSION, nullptr);
313316
if (MaterialSystem == nullptr)
314317
ThrowError("unable to initialize IMaterialSystem");
315318
#endif
316319

317-
//MDLCache->SetCacheNotify(MDLCacheUpdate); Why is the MDLCache broken!?!?! using any Set* function breaks the entire cache -> crashes the game.
318-
319-
// this->SetAsyncCacheDataType(MDLCACHE_STUDIOHDR, true, false, "MDLCACHE_STUDIOHDR"); cannot be activated
320-
// this->SetAsyncCacheDataType(MDLCACHE_STUDIOHWDATA, true, false, "MDLCACHE_STUDIOHWDATA");
321-
// this->SetAsyncCacheDataType(MDLCACHE_VCOLLIDE, true, false, "MDLCACHE_VCOLLIDE");
322-
// this->SetAsyncCacheDataType(MDLCACHE_ANIMBLOCK, true, false, "MDLCACHE_ANIMBLOCK"); if activated, it breaks some playermodels
323-
// this->SetAsyncCacheDataType(MDLCACHE_VIRTUALMODEL, true, false, "MDLCACHE_VIRTUALMODEL"); cannot be activated
324-
// this->SetAsyncCacheDataType(MDLCACHE_VERTEXES, true, false, "MDLCACHE_VERTEXES");
325-
// this->SetAsyncCacheDataType(MDLCACHE_DECODEDANIMBLOCK, true, false, "MDLCACHE_DECODEDANIMBLOCK"); cannot be activated
320+
// SetAsyncCacheDataType(MDLCACHE_STUDIOHDR, true, false, "MDLCACHE_STUDIOHDR"); cannot be activated
321+
// SetAsyncCacheDataType(MDLCACHE_STUDIOHWDATA, true, false, "MDLCACHE_STUDIOHWDATA");
322+
// SetAsyncCacheDataType(MDLCACHE_VCOLLIDE, true, false, "MDLCACHE_VCOLLIDE");
323+
// SetAsyncCacheDataType(MDLCACHE_ANIMBLOCK, true, false, "MDLCACHE_ANIMBLOCK"); if activated, it breaks some playermodels
324+
// SetAsyncCacheDataType(MDLCACHE_VIRTUALMODEL, true, false, "MDLCACHE_VIRTUALMODEL"); cannot be activated
325+
// SetAsyncCacheDataType(MDLCACHE_VERTEXES, true, false, "MDLCACHE_VERTEXES");
326+
// SetAsyncCacheDataType(MDLCACHE_DECODEDANIMBLOCK, true, false, "MDLCACHE_DECODEDANIMBLOCK"); cannot be activated
326327
}
327328

328329
void CacheMgr::Shutdown()
329330
{
330-
// this->SetAsyncCacheDataType(MDLCACHE_STUDIOHDR, false, false, "MDLCACHE_STUDIOHDR");
331-
this->SetAsyncCacheDataType(MDLCACHE_STUDIOHWDATA, false, false, "MDLCACHE_STUDIOHWDATA");
332-
this->SetAsyncCacheDataType(MDLCACHE_VCOLLIDE, false, false, "MDLCACHE_VCOLLIDE");
333-
// this->SetAsyncCacheDataType(MDLCACHE_ANIMBLOCK, false, false, "MDLCACHE_ANIMBLOCK");
334-
// this->SetAsyncCacheDataType(MDLCACHE_VIRTUALMODEL, false, false, "MDLCACHE_VIRTUALMODEL");
335-
this->SetAsyncCacheDataType(MDLCACHE_VERTEXES, false, false, "MDLCACHE_VERTEXES");
336-
// this->SetAsyncCacheDataType(MDLCACHE_DECODEDANIMBLOCK, false, false, "MDLCACHE_DECODEDANIMBLOCK");
331+
// SetAsyncCacheDataType(MDLCACHE_STUDIOHDR, false, false, "MDLCACHE_STUDIOHDR");
332+
SetAsyncCacheDataType(MDLCACHE_STUDIOHWDATA, false, false, "MDLCACHE_STUDIOHWDATA");
333+
SetAsyncCacheDataType(MDLCACHE_VCOLLIDE, false, false, "MDLCACHE_VCOLLIDE");
334+
// SetAsyncCacheDataType(MDLCACHE_ANIMBLOCK, false, false, "MDLCACHE_ANIMBLOCK");
335+
// SetAsyncCacheDataType(MDLCACHE_VIRTUALMODEL, false, false, "MDLCACHE_VIRTUALMODEL");
336+
SetAsyncCacheDataType(MDLCACHE_VERTEXES, false, false, "MDLCACHE_VERTEXES");
337+
// SetAsyncCacheDataType(MDLCACHE_DECODEDANIMBLOCK, false, false, "MDLCACHE_DECODEDANIMBLOCK");
337338
}

source/declarations.h

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include <engine/ivmodelinfo.h>
77
#include <cmodel.h>
88

9+
#include "tier1/utldict.h"
10+
#include "tier1/utllinkedlist.h"
11+
#include "filesystem.h"
12+
913
class ITexture;
1014
struct mtexinfo_t
1115
{
@@ -378,4 +382,51 @@ struct model_t
378382
#define SURFDRAW_SORTGROUP_MASK 0x00C00000 // 2 bits of sortgroup
379383

380384
#define SURFDRAW_VERTCOUNT_SHIFT 24
381-
#define SURFDRAW_SORTGROUP_SHIFT 22
385+
#define SURFDRAW_SORTGROUP_SHIFT 22
386+
387+
#define MDLCACHE_NONE ((MDLCacheDataType_t)-1)
388+
struct AsyncInfo_t
389+
{
390+
AsyncInfo_t() : hControl( NULL ), hModel( MDLHANDLE_INVALID ), type( MDLCACHE_NONE ), iAnimBlock( 0 ) {}
391+
392+
FSAsyncControl_t hControl;
393+
MDLHandle_t hModel;
394+
MDLCacheDataType_t type;
395+
int iAnimBlock;
396+
};
397+
398+
const int NO_ASYNC = CUtlLinkedList< AsyncInfo_t >::InvalidIndex();
399+
400+
class IDataCacheSection;
401+
struct studiodata_t;
402+
//-----------------------------------------------------------------------------
403+
// Implementation of the simple studio data cache (no caching)
404+
//-----------------------------------------------------------------------------
405+
class CMDLCache : public IMDLCache
406+
{
407+
public:
408+
CMDLCache();
409+
410+
// No functions since we only want the members
411+
412+
public:
413+
IDataCacheSection *m_pModelCacheSection;
414+
IDataCacheSection *m_pMeshCacheSection;
415+
IDataCacheSection *m_pAnimBlockCacheSection;
416+
417+
int m_nModelCacheFrameLocks;
418+
int m_nMeshCacheFrameLocks;
419+
420+
CUtlDict< studiodata_t*, MDLHandle_t > m_MDLDict;
421+
422+
IMDLCacheNotify *m_pCacheNotify;
423+
424+
CUtlFixedLinkedList< AsyncInfo_t > m_PendingAsyncs;
425+
426+
CThreadFastMutex m_QueuedLoadingMutex;
427+
CThreadFastMutex m_AsyncMutex;
428+
429+
bool m_bLostVideoMemory : 1;
430+
bool m_bConnected : 1;
431+
bool m_bInitialized : 1;
432+
};

0 commit comments

Comments
 (0)