From 9080b9b7e82fd4bdb84b9f965f940728144b1925 Mon Sep 17 00:00:00 2001 From: WPMGPRoSToTeMa Date: Sat, 6 Feb 2016 11:29:44 +0300 Subject: [PATCH] Added auto precache sounds in models (event 5004) Added cvar sv_auto_precache_sounds_in_models Moved models auto precache code to SV_ActivateServer --- rehlds/engine/pr_cmds.cpp | 37 ----------------- rehlds/engine/server.h | 1 + rehlds/engine/sv_main.cpp | 83 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 37 deletions(-) diff --git a/rehlds/engine/pr_cmds.cpp b/rehlds/engine/pr_cmds.cpp index c4aac0d..ac51e33 100644 --- a/rehlds/engine/pr_cmds.cpp +++ b/rehlds/engine/pr_cmds.cpp @@ -1456,43 +1456,6 @@ int EXT_FUNC PF_precache_model_I(const char *s) if (!iOptional) g_psv.model_precache_flags[i] |= 1u; -#ifdef REHLDS_FIXES - if (g_psv.models[i]->type == mod_studio) - { - studiohdr_t *pStudioHeader = (studiohdr_t *)Mod_Extradata(g_psv.models[i]); - - size_t modelNameLength = Q_strlen(s); - if (!pStudioHeader->textureindex && modelNameLength < MAX_QPATH - 1) - { - char textureModelName[MAX_QPATH]; - Q_strcpy(textureModelName, s); - - size_t modelExtensionLength = sizeof(".mdl") - 1; - char *modelExtension = &textureModelName[modelNameLength - modelExtensionLength]; - Q_strcpy(modelExtension, "T.mdl"); - - PF_precache_model_I(textureModelName); - } - // 0 seqgroup is reserved for sequences in this model - if (pStudioHeader->numseqgroups > 1) - { - mstudioseqgroup_t *pSeqGroup = (mstudioseqgroup_t *)((uint8_t *)pStudioHeader + pStudioHeader->seqgroupindex); - ++pSeqGroup; - for (int i = 1; i < pStudioHeader->numseqgroups; i++, pSeqGroup++) - { - if (pSeqGroup->name[0] == '\0') - continue; - - char seqGroupName[MAX_QPATH]; - Q_strcpy(seqGroupName, pSeqGroup->name); - ForwardSlashes(seqGroupName); - - PF_precache_generic_I(seqGroupName); - } - } - } -#endif // REHLDS_FIXES - return i; } diff --git a/rehlds/engine/server.h b/rehlds/engine/server.h index aaf0a28..e793a6d 100644 --- a/rehlds/engine/server.h +++ b/rehlds/engine/server.h @@ -528,6 +528,7 @@ extern cvar_t sv_allow_dlfile; extern cvar_t sv_version; #ifdef REHLDS_FIXES extern cvar_t sv_echo_unknown_cmd; +extern cvar_t sv_auto_precache_sounds_in_models; #endif extern int sv_playermodel; diff --git a/rehlds/engine/sv_main.cpp b/rehlds/engine/sv_main.cpp index 0baa695..3775179 100644 --- a/rehlds/engine/sv_main.cpp +++ b/rehlds/engine/sv_main.cpp @@ -197,6 +197,7 @@ cvar_t sv_allow_dlfile = { "sv_allow_dlfile", "1", 0, 0.0f, NULL }; #ifdef REHLDS_FIXES cvar_t sv_version = { "sv_version", "", FCVAR_SERVER, 0.0f, NULL }; cvar_t sv_echo_unknown_cmd = { "sv_echo_unknown_cmd", "0", 0, 0.0f, NULL }; +cvar_t sv_auto_precache_sounds_in_models = { "sv_auto_precache_sounds_in_models", "0", 0, 0.0f, nullptr }; #else cvar_t sv_version = {"sv_version", "", 0, 0.0f, NULL}; #endif @@ -5398,6 +5399,83 @@ void SetCStrikeFlags(void) } } +#ifdef REHLDS_FIXES +void PrecacheModelTexture(const char *s, studiohdr_t *pStudioHeader) +{ + if (pStudioHeader->textureindex) + return; + + size_t modelNameLength = Q_strlen(s); + if (modelNameLength >= MAX_QPATH - 1) + return; + + char textureModelName[MAX_QPATH]; + Q_strcpy(textureModelName, s); + + size_t modelExtensionLength = sizeof(".mdl") - 1; + char *modelExtension = &textureModelName[modelNameLength - modelExtensionLength]; + Q_strcpy(modelExtension, "T.mdl"); + + // Use generic, because model max count is 512... + PF_precache_generic_I(textureModelName); +} + +void PrecacheModelSeqGroups(studiohdr_t *pStudioHeader) +{ + // 0 seqgroup is reserved for sequences in this model + if (pStudioHeader->numseqgroups <= 1) + return; + + mstudioseqgroup_t *pSeqGroup = (mstudioseqgroup_t *)((uint8_t *)pStudioHeader + pStudioHeader->seqgroupindex); + for (int i = 1; i < pStudioHeader->numseqgroups; i++) + { + if (pSeqGroup[i].name[0] == '\0') + continue; + + char seqGroupName[MAX_QPATH]; + Q_strcpy(seqGroupName, pSeqGroup[i].name); + + ForwardSlashes(seqGroupName); + PF_precache_generic_I(seqGroupName); + } +} + +void PrecacheModelSounds(studiohdr_t *pStudioHeader) +{ + mstudioseqdesc_t *pSeqDesc = (mstudioseqdesc_t *)((uintptr_t)pStudioHeader + pStudioHeader->seqindex); + for (int index = 0; index < pStudioHeader->numseq; index++) + { + mstudioevent_t *pEvent = (mstudioevent_t *)((uintptr_t)pStudioHeader + pSeqDesc[index].eventindex); + for (int i = 0; i < pSeqDesc[index].numevents; i++) + { + if (pEvent[i].event != 5004) + continue; + if (pEvent[i].options[0] == '\0') + continue; + + PF_precache_generic_I(va("sound/%s", pEvent[i].options)); + } + } +} + +void PrecacheModelSpecifiedFiles() +{ + const char **s = &g_psv.model_precache[1]; + for (size_t i = 1; i < ARRAYSIZE(g_psv.model_precache) && *s != nullptr; i++, s++) + { + if (g_psv.models[i]->type != mod_studio) + continue; + + studiohdr_t *pStudioHeader = (studiohdr_t *)Mod_Extradata(g_psv.models[i]); + + PrecacheModelTexture(*s, pStudioHeader); + PrecacheModelSeqGroups(pStudioHeader); + if (sv_auto_precache_sounds_in_models.value) + PrecacheModelSounds(pStudioHeader); + } +} +#endif + void SV_ActivateServer(int runPhysics) { g_RehldsHookchains.m_SV_ActivateServer.callChain(SV_ActivateServer_internal, runPhysics); @@ -5427,6 +5505,10 @@ void EXT_FUNC SV_ActivateServer_internal(int runPhysics) gEntityInterface.pfnServerActivate(g_psv.edicts, g_psv.num_edicts, g_psvs.maxclients); Steam_Activate(); ContinueLoadingProgressBar("Server", 9, 0.0f); +#ifdef REHLDS_FIXES + // Precache after all models and sounds is precached, because we use PrecacheGeneric, which checks is that resource already precached as model or sound + PrecacheModelSpecifiedFiles(); +#endif SV_CreateGenericResources(); g_psv.active = TRUE; g_psv.state = ss_active; @@ -7169,6 +7251,7 @@ void SV_Init(void) Cvar_RegisterVariable(&sv_force_ent_intersection); #ifdef REHLDS_FIXES Cvar_RegisterVariable(&sv_echo_unknown_cmd); + Cvar_RegisterVariable(&sv_auto_precache_sounds_in_models); #endif for (int i = 0; i < MAX_MODELS; i++)