Skip to content

Commit

Permalink
Fix redefinitions of soundscripts and visuals being processed incorre…
Browse files Browse the repository at this point in the history
…ctly in the entity template named after the classname
  • Loading branch information
FreeSlave committed Dec 13, 2024
1 parent 203a69e commit 4904374
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 44 deletions.
115 changes: 76 additions & 39 deletions dlls/cbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,30 +779,37 @@ void CBaseEntity::StopSound(int channel, const char *sample)
STOP_SOUND(edict(), channel, sample);
}

const char* CBaseEntity::GetSoundScriptNameForTemplate(const char *name, string_t templateName)
const char* CBaseEntity::GetSoundScriptNameForTemplate(const char *name, const EntTemplate* entTemplate)
{
if (FStringNull(!templateName))
{
const EntTemplate* entTemplate = GetEntTemplate(STRING(templateName));
if (entTemplate)
{
return entTemplate->GetSoundScriptNameOverride(name);
}
}
return nullptr;
return entTemplate ? entTemplate->GetSoundScriptNameOverride(name) : nullptr;
}

const char* CBaseEntity::GetSoundScriptNameForMyTemplate(const char *name)
const char* CBaseEntity::GetSoundScriptNameForMyTemplate(const char *name, string_t* usedTemplate)
{
const char* nameOverride = nullptr;
if (usedTemplate)
*usedTemplate = iStringNull;

nameOverride = GetSoundScriptNameForTemplate(name, m_entTemplate);
nameOverride = GetSoundScriptNameForTemplate(name, GetMyEntTemplate());
if (nameOverride)
{
if (usedTemplate)
{
if (m_entTemplate)
*usedTemplate = m_entTemplate;
else
*usedTemplate = pev->classname;
}
return nameOverride;
}

nameOverride = GetSoundScriptNameForTemplate(name, m_ownerEntTemplate);
nameOverride = GetSoundScriptNameForTemplate(name, GetOwnerEntTemplate());
if (nameOverride)
{
if (usedTemplate)
*usedTemplate = m_ownerEntTemplate;
return nameOverride;
}

return name;
}
Expand Down Expand Up @@ -968,7 +975,12 @@ void CBaseEntity::RegisterAndPrecacheSoundScript(const NamedSoundScript &default

void CBaseEntity::RegisterAndPrecacheSoundScript(const char* derivative, const char* base, const SoundScript& defaultSoundScript, const SoundScriptParamOverride paramsOverride)
{
const SoundScript* soundScript = g_SoundScriptSystem.ProvideDefaultSoundScript(GetSoundScriptNameForMyTemplate(derivative), GetSoundScriptNameForMyTemplate(base), defaultSoundScript, paramsOverride);
string_t baseTemplate = iStringNull;
const char* baseName = GetSoundScriptNameForMyTemplate(base, &baseTemplate);
if (baseTemplate)
EnsureSoundScriptReplacementForTemplate(STRING(baseTemplate), derivative);

const SoundScript* soundScript = g_SoundScriptSystem.ProvideDefaultSoundScript(GetSoundScriptNameForMyTemplate(derivative), baseName, defaultSoundScript, paramsOverride);
if (soundScript)
{
PrecacheSoundScript(*soundScript);
Expand All @@ -980,32 +992,31 @@ void CBaseEntity::RegisterAndPrecacheSoundScript(const char* derivative, const
RegisterAndPrecacheSoundScript(derivative, defaultSoundScript.name, defaultSoundScript, paramsOverride);
}

const char* CBaseEntity::GetVisualNameForTemplate(const char *name, string_t templateName)
const char* CBaseEntity::GetVisualNameForTemplate(const char *name, const EntTemplate* entTemplate)
{
if (FStringNull(!templateName))
{
const EntTemplate* entTemplate = GetEntTemplate(STRING(templateName));
if (entTemplate)
{
return entTemplate->GetVisualNameOverride(name);
}
}
return nullptr;
return entTemplate ? entTemplate->GetVisualNameOverride(name) : nullptr;
}

const char* CBaseEntity::GetVisualNameForMyTemplate(const char *name, string_t* usedTemplate)
{
const char* nameOverride = nullptr;
if (usedTemplate)
*usedTemplate = iStringNull;

nameOverride = GetVisualNameForTemplate(name, m_entTemplate);
nameOverride = GetVisualNameForTemplate(name, GetMyEntTemplate());
if (nameOverride)
{
if (usedTemplate)
*usedTemplate = m_entTemplate;
{
if (m_entTemplate)
*usedTemplate = m_entTemplate;
else
*usedTemplate = pev->classname;
}
return nameOverride;
}

nameOverride = GetVisualNameForTemplate(name, m_ownerEntTemplate);
nameOverride = GetVisualNameForTemplate(name, GetOwnerEntTemplate());
if (nameOverride)
{
if (usedTemplate)
Expand Down Expand Up @@ -1047,10 +1058,10 @@ void CBaseEntity::AssignEntityOverrides(EntityOverrides entityOverrides)
m_soundList = entityOverrides.soundList;
}

EntityOverrides CBaseEntity::GetProjectileOverrides() const
EntityOverrides CBaseEntity::GetProjectileOverrides()
{
EntityOverrides entityOverrides;
entityOverrides.ownerEntTemplate = m_entTemplate;
entityOverrides.ownerEntTemplate = GetMyTemplateName();
entityOverrides.soundList = m_soundList;
return entityOverrides;
}
Expand Down Expand Up @@ -1112,24 +1123,50 @@ void CBaseEntity::ApplyVisual(const Visual *visual, const char* modelOverride)
pev->framerate = RandomizeNumberFromRange(visual->framerate);
}

const EntTemplate* CBaseEntity::GetMyEntTemplate()
const EntTemplate* CBaseEntity::GetCacheableEntTemplate(entvars_t* pev, string_t templateName, const EntTemplate*& entTemplate, bool& templateChecked, bool checkByClassname)
{
if (m_entTemplateChecked)
if (templateChecked)
{
return entTemplate;
}
if (!FStringNull(templateName))
{
return m_cachedEntTemplate;
entTemplate = GetEntTemplate(STRING(templateName));
templateChecked = true;
return entTemplate;
}
if (!FStringNull(m_entTemplate))
else if (checkByClassname)
{
m_cachedEntTemplate = GetEntTemplate(STRING(m_entTemplate));
m_entTemplateChecked = true;
return m_cachedEntTemplate;
entTemplate = GetEntTemplate(STRING(pev->classname));
templateChecked = true;
return entTemplate;
}
else
{
m_cachedEntTemplate = GetEntTemplate(STRING(pev->classname));
m_entTemplateChecked = true;
return m_cachedEntTemplate;
entTemplate = nullptr;
templateChecked = true;
return entTemplate;
}
}

const EntTemplate* CBaseEntity::GetMyEntTemplate()
{
return GetCacheableEntTemplate(pev, m_entTemplate, m_cachedEntTemplate, m_entTemplateChecked, true);
}

string_t CBaseEntity::GetMyTemplateName()
{
if (FStringNull(m_entTemplate))
{
if (GetMyEntTemplate())
return pev->classname;
}
return m_entTemplate;
}

const EntTemplate* CBaseEntity::GetOwnerEntTemplate()
{
return GetCacheableEntTemplate(pev, m_ownerEntTemplate, m_cachedOwnerEntTemplate, m_ownerEntTemplateChecked, false);
}

void CBaseEntity::SetMyHealth(const float defaultHealth)
Expand Down
14 changes: 10 additions & 4 deletions dlls/cbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,11 @@ class CBaseEntity
string_t m_soundList; // sound replacement list file name
string_t m_objectHint; // the name of the spritehint template

// Don't save those:
const EntTemplate* m_cachedEntTemplate;
const EntTemplate* m_cachedOwnerEntTemplate;
bool m_entTemplateChecked;
bool m_ownerEntTemplateChecked;

int PRECACHE_SOUND(const char* soundName);
static int PRECACHE_SOUND(const char* soundName, string_t soundList, const EntTemplate* entTemplate);
Expand All @@ -273,8 +276,8 @@ class CBaseEntity
void EmitAmbientSound( const Vector &vecOrigin, const char *sample, float vol, float attenuation, int iFlags, int pitch );
void StopSound( int channel, const char* sample );

static const char* GetSoundScriptNameForTemplate(const char* name, string_t templateName);
const char* GetSoundScriptNameForMyTemplate(const char* name);
static const char* GetSoundScriptNameForTemplate(const char* name, const EntTemplate* entTemplate);
const char* GetSoundScriptNameForMyTemplate(const char* name, string_t* usedTemplate = nullptr);
const SoundScript* GetSoundScript(const char* name);
bool EmitSoundScript(const SoundScript* soundScript, const SoundScriptParamOverride paramsOverride = SoundScriptParamOverride(), int flags = 0);
bool EmitSoundScript(const char* name, const SoundScriptParamOverride paramsOverride = SoundScriptParamOverride(), int flags = 0);
Expand All @@ -296,18 +299,21 @@ class CBaseEntity
void RegisterAndPrecacheSoundScript(const char* derivative, const char* base, const SoundScript& defaultSoundScript, const SoundScriptParamOverride paramsOverride = SoundScriptParamOverride());
void RegisterAndPrecacheSoundScript(const char* derivative, const NamedSoundScript& defaultSoundScript, const SoundScriptParamOverride paramsOverride = SoundScriptParamOverride());

static const char* GetVisualNameForTemplate(const char* name, string_t templateName);
static const char* GetVisualNameForTemplate(const char* name, const EntTemplate* entTemplate);
const char* GetVisualNameForMyTemplate(const char* name, string_t* usedTemplate = nullptr);
const Visual* GetVisual(const char* name);
const Visual* RegisterVisual(const NamedVisual& defaultVisual, bool precache = true, string_t* usedTemplate = nullptr);
void AssignEntityOverrides(EntityOverrides entityOverrides);
EntityOverrides GetProjectileOverrides() const;
EntityOverrides GetProjectileOverrides();

int OverridenRenderProps();
virtual void ApplyDefaultRenderProps(int overridenRenderProps) {}
void ApplyVisual(const Visual* visual, const char* modelOverride = nullptr);

static const EntTemplate* GetCacheableEntTemplate(entvars_t* pev, string_t templateName, const EntTemplate*& entTemplate, bool& templateChecked, bool checkByClassname);
const EntTemplate* GetMyEntTemplate();
string_t GetMyTemplateName();
const EntTemplate* GetOwnerEntTemplate();
void SetMyHealth(const float defaultHealth);
const Visual* MyOwnVisual();
const char* MyOwnModel(const char* defaultModel);
Expand Down
26 changes: 25 additions & 1 deletion dlls/ent_templates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class EntTemplateSystem
bool ReadFromFile(const char* fileName);
const EntTemplate* GetTemplate(const char* name);
void EnsureVisualReplacementForTemplate(const char* templateName, const char* visualName);
void EnsureSoundScriptReplacementForTemplate(const char* templateName, const char* soundScriptName);

private:
std::map<std::string, EntTemplate, CaseInsensitiveCompare> _entTemplates;
Expand Down Expand Up @@ -251,7 +252,7 @@ bool EntTemplateSystem::ReadFromFile(const char *fileName)
}
else if (scriptIt->value.IsObject())
{
std::string replacement = GenerateResourceName(templateName, + soundScriptName);
std::string replacement = GenerateResourceName(templateName, soundScriptName);
entTemplate.SetSoundScriptReplacement(soundScriptName, replacement);
g_SoundScriptSystem.AddSoundScriptFromJsonValue(replacement.c_str(), scriptIt->value);
}
Expand Down Expand Up @@ -540,6 +541,24 @@ void EntTemplateSystem::EnsureVisualReplacementForTemplate(const char* templateN
}
}

void EntTemplateSystem::EnsureSoundScriptReplacementForTemplate(const char* templateName, const char* soundScriptName)
{
if (!templateName || *templateName == '\0')
return;
_temp = templateName;
auto it = _entTemplates.find(_temp);
if (it != _entTemplates.end())
{
EntTemplate* entTemplate = &it->second;
if (!entTemplate->GetSoundScriptNameOverride(soundScriptName))
{
std::string replacement = GenerateResourceName(it->first, soundScriptName);
entTemplate->SetSoundScriptReplacement(soundScriptName, replacement);
g_SoundScriptSystem.EnsureSoundScriptExists(replacement);
}
}
}

EntTemplateSystem g_EntTemplateSystem;

void ReadEntTemplates()
Expand All @@ -556,3 +575,8 @@ void EnsureVisualReplacementForTemplate(const char* templateName, const char* vi
{
g_EntTemplateSystem.EnsureVisualReplacementForTemplate(templateName, visualName);
}

void EnsureSoundScriptReplacementForTemplate(const char* templateName, const char* soundScriptName)
{
g_EntTemplateSystem.EnsureSoundScriptReplacementForTemplate(templateName, soundScriptName);
}
1 change: 1 addition & 0 deletions dlls/ent_templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,6 @@ void ReadEntTemplates();
const EntTemplate* GetEntTemplate(const char* name);

void EnsureVisualReplacementForTemplate(const char* templateName, const char* visualName);
void EnsureSoundScriptReplacementForTemplate(const char* templateName, const char* soundScriptName);

#endif
7 changes: 7 additions & 0 deletions dlls/soundscripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ void SoundScriptSystem::AddSoundScriptFromJsonValue(const char *name, Value &val
_soundScripts[name] = std::make_pair(soundScript, soundScriptMeta);
}

void SoundScriptSystem::EnsureSoundScriptExists(const std::string& name)
{
auto it = _soundScripts.find(name);
if (it == _soundScripts.end())
_soundScripts[name] = std::make_pair(SoundScript(), SoundScriptMeta());
}

const SoundScript* SoundScriptSystem::GetSoundScript(const char *name)
{
if (!name || *name == '\0')
Expand Down
1 change: 1 addition & 0 deletions dlls/soundscripts.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class SoundScriptSystem : public JSONConfig
{
public:
void AddSoundScriptFromJsonValue(const char* name, rapidjson::Value& value);
void EnsureSoundScriptExists(const std::string& name);
const SoundScript* GetSoundScript(const char* name);
const SoundScript* ProvideDefaultSoundScript(const char* name, const SoundScript& soundScript);
const SoundScript* ProvideDefaultSoundScript(const char* derivative, const char* base, const SoundScript& soundScript, const SoundScriptParamOverride paramOverride = SoundScriptParamOverride());
Expand Down

0 comments on commit 4904374

Please sign in to comment.