1- using Source . Common ;
1+ using Source ;
2+ using Source . Common ;
23
34using System ;
45using System . Collections . Generic ;
@@ -8,12 +9,101 @@ namespace Game.Shared;
89
910public 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 = [ ] ;
0 commit comments