Skip to content

Commit 059f419

Browse files
committed
ModelSoundsCache impl
1 parent 6046ef2 commit 059f419

3 files changed

Lines changed: 123 additions & 8 deletions

File tree

‎Game.Server/BaseEntity.cs‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,25 +3071,45 @@ public static void PrecacheModelComponents(int modelIndex) {
30713071

30723072
// sounds
30733073
ReadOnlySpan<char> name = modelinfo.GetModelName(model);
3074-
if (!g_ModelSoundsCache.TryGetValue(name.Hash(), out _)) {
3074+
if (!g_ModelSoundsCache.EntryExists(name)) {
30753075
ReadOnlySpan<char> extension = Path.GetExtension(name);
30763076

30773077
if (!stristr(extension, "mdl").IsEmpty)
30783078
DevMsg(2, $"Late precache of {name}, need to rebuild modelsounds.cache\n");
30793079
else {
3080-
if (extension[0] == '\0')
3080+
if (extension.IsEmpty)
30813081
Warning($"Precache of {name} ambigious (no extension specified)\n");
30823082
else
30833083
Warning($"Late precache of {name} (file missing?)\n");
30843084
return;
30853085
}
30863086
}
30873087

3088-
if (g_ModelSoundsCache.TryGetValue(name.Hash(), out ModelSoundsCache? entry))
3089-
entry.PrecacheSoundList();
3088+
ModelSoundsCache? entry = g_ModelSoundsCache.Get(name);
3089+
Assert(entry != null);
3090+
entry?.PrecacheSoundList();
30903091
}
30913092

3092-
static readonly Dictionary<UtlSymId_t, ModelSoundsCache> g_ModelSoundsCache = [];
3093+
public static StudioHdr? ModelSoundsCache_LoadModel(ReadOnlySpan<char> filename) {
3094+
int idx = engine.PrecacheModel(filename, true);
3095+
if (idx != -1) {
3096+
Model? mdl = modelinfo.GetModel(idx);
3097+
if (mdl != null) {
3098+
StudioHdr studioHdr = new();
3099+
studioHdr.Init(modelinfo.GetStudiomodel(mdl), mdlcache);
3100+
if (studioHdr.IsValid())
3101+
return studioHdr;
3102+
}
3103+
}
3104+
return null;
3105+
}
3106+
3107+
public static void ModelSoundsCache_FinishModel(StudioHdr hdr) => Assert(hdr != null);
3108+
3109+
public static void ModelSoundsCache_PrecacheScriptSound(ReadOnlySpan<char> soundname) => PrecacheScriptSound(soundname);
3110+
3111+
const int MODELSOUNDSCACHE_VERSION = 5;
3112+
static readonly UtlCachedFileData<ModelSoundsCache> g_ModelSoundsCache = new("modelsounds.cache", MODELSOUNDSCACHE_VERSION, null, UtlCachedFileDataType.UseFilesize, false);
30933113

30943114
public bool HasSpawnFlags(int flags) => (SpawnFlags & flags) != 0;
30953115

‎Game.Shared/ModelSoundsCache.cs‎

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Source.Common;
1+
using Source;
2+
using Source.Common;
23

34
using System;
45
using System.Collections.Generic;
@@ -8,12 +9,101 @@ namespace Game.Shared;
89

910
public class ModelSoundsCache() : IBaseCacheInfo
1011
{
11-
public void Rebuild(ReadOnlySpan<char> filename) => throw new NotImplementedException();
12+
const int CL_EVENT_SOUND = 5004;
13+
const int CL_EVENT_FOOTSTEP_LEFT = 6004;
14+
const int CL_EVENT_FOOTSTEP_RIGHT = 6005;
15+
const int CL_EVENT_MFOOTSTEP_LEFT = 6006;
16+
const int CL_EVENT_MFOOTSTEP_RIGHT = 6007;
17+
18+
public ReadOnlySpan<char> GetSoundName(int index) => soundemitterbase.GetSoundName(sounds[index]);
19+
20+
public void Rebuild(ReadOnlySpan<char> filename) {
21+
#if GAME_DLL
22+
sounds.Clear();
23+
StudioHdr? hdr = BaseEntity.ModelSoundsCache_LoadModel(filename);
24+
if (hdr != null) {
25+
BuildAnimationEventSoundList(hdr, sounds);
26+
BaseEntity.ModelSoundsCache_FinishModel(hdr);
27+
}
28+
#endif
29+
}
30+
1231
public void Restore(Stream buf) => throw new NotImplementedException();
1332
public void Save(Stream buf) => throw new NotImplementedException();
1433

1534
internal void PrecacheSoundList() {
16-
throw new NotImplementedException();
35+
#if GAME_DLL
36+
for (int i = 0; i < sounds.Count; ++i)
37+
BaseEntity.ModelSoundsCache_PrecacheScriptSound(GetSoundName(i));
38+
#endif
39+
}
40+
41+
static void FindOrAddScriptSound(List<ushort> sounds, ReadOnlySpan<char> soundname) {
42+
int soundindex = soundemitterbase.GetSoundIndex(soundname);
43+
if (soundindex != -1) {
44+
if (!sounds.Contains((ushort)soundindex))
45+
sounds.Add((ushort)soundindex);
46+
}
47+
}
48+
49+
static void FindOrAddScriptSound(List<ushort> sounds, MStudioEvent ev) => FindOrAddScriptSound(sounds, Encoding.ASCII.GetString(((ReadOnlySpan<byte>)ev.Options()).SliceNullTerminatedString()));
50+
51+
public static void BuildAnimationEventSoundList(StudioHdr hdr, List<ushort> sounds) {
52+
Assert(hdr != null);
53+
54+
Animation.VerifySequenceIndex(hdr);
55+
56+
for (int iSeq = 0; iSeq < hdr.GetNumSeq(); iSeq++) {
57+
MStudioSeqDesc seq = hdr.Seqdesc(iSeq);
58+
59+
for (int iEvent = 0; iEvent < (int)seq.NumEvents; iEvent++) {
60+
MStudioEvent? ev = seq.Event(iEvent);
61+
if (ev == null)
62+
continue;
63+
64+
switch (ev.Event) {
65+
default:
66+
if (((AnimEventType)ev.Type & AnimEventType.NewEventSystem) != 0) {
67+
if (ev.Event == (int)AnimEventId.AE_SV_PLAYSOUND)
68+
FindOrAddScriptSound(sounds, ev);
69+
}
70+
break;
71+
case CL_EVENT_SOUND:
72+
FindOrAddScriptSound(sounds, ev);
73+
break;
74+
case CL_EVENT_FOOTSTEP_LEFT:
75+
case CL_EVENT_FOOTSTEP_RIGHT: {
76+
Span<char> soundname = stackalloc char[256];
77+
ReadOnlySpan<char> options = Encoding.ASCII.GetString(((ReadOnlySpan<byte>)ev.Options()).SliceNullTerminatedString());
78+
if (options.IsEmpty)
79+
options = "NPC_CombineS";
80+
sprintf(soundname, "%s.RunFootstepLeft").S(options);
81+
FindOrAddScriptSound(sounds, soundname.SliceNullTerminatedString());
82+
sprintf(soundname, "%s.RunFootstepRight").S(options);
83+
FindOrAddScriptSound(sounds, soundname.SliceNullTerminatedString());
84+
sprintf(soundname, "%s.FootstepLeft").S(options);
85+
FindOrAddScriptSound(sounds, soundname.SliceNullTerminatedString());
86+
sprintf(soundname, "%s.FootstepRight").S(options);
87+
FindOrAddScriptSound(sounds, soundname.SliceNullTerminatedString());
88+
}
89+
break;
90+
case (int)AnimEventId.AE_CL_PLAYSOUND:
91+
if (((AnimEventType)ev.Type & AnimEventType.Client) == 0)
92+
break;
93+
if (ev.Options()[0] != 0)
94+
FindOrAddScriptSound(sounds, ev);
95+
else
96+
Warning($"-- Error --: empty soundname, .qc error on AE_CL_PLAYSOUND in model {hdr.Name()}, sequence {seq.Label()}, animevent # {iEvent + 1}\n");
97+
break;
98+
case SCRIPT_EVENT_SOUND:
99+
FindOrAddScriptSound(sounds, ev);
100+
break;
101+
case SCRIPT_EVENT_SOUND_VOICE:
102+
FindOrAddScriptSound(sounds, ev);
103+
break;
104+
}
105+
}
106+
}
17107
}
18108

19109
public readonly List<ushort> sounds = [];

‎Source.Common/UtlCachedFileData.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public bool Init() {
7878
return true;
7979
}
8080

81+
public bool EntryExists(ReadOnlySpan<char> filename) {
82+
FileNameHandle_t handle = filesystem.FindOrAddFileName(filename);
83+
return Elements.ContainsKey(handle);
84+
}
85+
8186
public T? Get(ReadOnlySpan<char> filename) {
8287
FileNameHandle_t idx = GetIndex(filename);
8388
ElementType e = Elements[idx];

0 commit comments

Comments
 (0)