11using System ;
2+ using System . Collections . Frozen ;
23using System . Collections . Generic ;
34using Robust . Shared . EntitySerialization ;
45using Robust . Shared . GameObjects ;
@@ -8,7 +9,8 @@ namespace Robust.Shared.Prototypes;
89
910public abstract partial class PrototypeManager
1011{
11- private readonly Dictionary < string , PrototypeComponentRegistration [ ] > _entityComponentRegistrations = new ( ) ;
12+ private FrozenDictionary < string , PrototypeComponentRegistration [ ] > _entityComponentRegistrations
13+ = FrozenDictionary < string , PrototypeComponentRegistration [ ] > . Empty ;
1214
1315 void IPrototypeManagerInternal . LoadEntity ( Entity < MetaDataComponent > ent , IEntityLoadContext ? context )
1416 {
@@ -34,6 +36,34 @@ private PrototypeComponentRegistration[] GetComponentRegistrations(EntityPrototy
3436 if ( _entityComponentRegistrations . TryGetValue ( prototype . ID , out var cached ) )
3537 return cached ;
3638
39+ // This should only happen if an entity is being loaded before prototype resolution has rebuilt the cache.
40+ return BuildComponentRegistrations ( prototype ) ;
41+ }
42+
43+ private void RebuildEntityComponentRegistrationCache ( )
44+ {
45+ if ( ! TryGetInstances < EntityPrototype > ( out var prototypes ) )
46+ {
47+ ClearEntityComponentRegistrationCache ( ) ;
48+ return ;
49+ }
50+
51+ var registrations = new Dictionary < string , PrototypeComponentRegistration [ ] > ( prototypes . Count ) ;
52+ foreach ( var ( id , prototype ) in prototypes )
53+ {
54+ registrations [ id ] = BuildComponentRegistrations ( prototype ) ;
55+ }
56+
57+ _entityComponentRegistrations = registrations . ToFrozenDictionary ( ) ;
58+ }
59+
60+ private void ClearEntityComponentRegistrationCache ( )
61+ {
62+ _entityComponentRegistrations = FrozenDictionary < string , PrototypeComponentRegistration [ ] > . Empty ;
63+ }
64+
65+ private PrototypeComponentRegistration [ ] BuildComponentRegistrations ( EntityPrototype prototype )
66+ {
3767 var registrations = new PrototypeComponentRegistration [ prototype . Components . Count ] ;
3868 var i = 0 ;
3969
@@ -42,7 +72,6 @@ private PrototypeComponentRegistration[] GetComponentRegistrations(EntityPrototy
4272 registrations [ i ++ ] = new PrototypeComponentRegistration ( name , entry , _factory . GetRegistration ( name ) ) ;
4373 }
4474
45- _entityComponentRegistrations [ prototype . ID ] = registrations ;
4675 return registrations ;
4776 }
4877
0 commit comments