@@ -335,98 +335,117 @@ protected void ReloadPrototypes(IEnumerable<ResPath> filePaths)
335
335
}
336
336
337
337
/// <inheritdoc />
338
- public void ReloadPrototypes ( Dictionary < Type , HashSet < string > > modified ,
338
+ public void ReloadPrototypes (
339
+ Dictionary < Type , HashSet < string > > modified ,
339
340
Dictionary < Type , HashSet < string > > ? removed = null )
340
341
{
341
- #if TOOLS
342
342
var prototypeTypeOrder = modified . Keys . ToList ( ) ;
343
343
prototypeTypeOrder . Sort ( SortPrototypesByPriority ) ;
344
344
345
- var pushed = new Dictionary < Type , HashSet < string > > ( ) ;
345
+ var byType = new Dictionary < Type , PrototypesReloadedEventArgs . PrototypeChangeSet > ( ) ;
346
346
var modifiedKinds = new HashSet < KindData > ( ) ;
347
+ var toProcess = new HashSet < string > ( ) ;
348
+ var processQueue = new Queue < string > ( ) ;
347
349
348
350
foreach ( var kind in prototypeTypeOrder )
349
351
{
352
+ var modifiedInstances = new Dictionary < string , IPrototype > ( ) ;
350
353
var kindData = _kinds [ kind ] ;
351
- if ( ! kind . IsAssignableTo ( typeof ( IInheritingPrototype ) ) )
352
- {
353
- foreach ( var id in modified [ kind ] )
354
- {
355
- var prototype = ( IPrototype ) _serializationManager . Read ( kind , kindData . Results [ id ] ) ! ;
356
- kindData . UnfrozenInstances ??= kindData . Instances . ToDictionary ( ) ;
357
- kindData . UnfrozenInstances [ id ] = prototype ;
358
- modifiedKinds . Add ( kindData ) ;
359
- }
360
354
361
- continue ;
362
- }
355
+ var tree = kindData . Inheritance ;
356
+ toProcess . Clear ( ) ;
357
+ processQueue . Clear ( ) ;
358
+
359
+ DebugTools . AssertEqual ( kind . IsAssignableTo ( typeof ( IInheritingPrototype ) ) , tree != null ) ;
360
+ DebugTools . Assert ( tree != null || kindData . RawResults == kindData . Results ) ;
363
361
364
- var tree = kindData . Inheritance ! ;
365
- var processQueue = new Queue < string > ( ) ;
366
362
foreach ( var id in modified [ kind ] )
367
363
{
364
+ AddToQueue ( id ) ;
365
+ }
366
+
367
+ void AddToQueue ( string id )
368
+ {
369
+ if ( ! toProcess . Add ( id ) )
370
+ return ;
368
371
processQueue . Enqueue ( id ) ;
372
+
373
+ if ( tree == null )
374
+ return ;
375
+
376
+ if ( ! tree . TryGetChildren ( id , out var children ) )
377
+ return ;
378
+
379
+ foreach ( var child in children ! )
380
+ {
381
+ AddToQueue ( child ) ;
382
+ }
369
383
}
370
384
371
385
while ( processQueue . TryDequeue ( out var id ) )
372
386
{
373
- var pushedSet = pushed . GetOrNew ( kind ) ;
374
-
375
- if ( tree . TryGetParents ( id , out var parents ) )
387
+ DebugTools . Assert ( toProcess . Contains ( id ) ) ;
388
+ if ( tree != null )
376
389
{
377
- var nonPushedParent = false ;
378
- foreach ( var parent in parents )
390
+ if ( tree . TryGetParents ( id , out var parents ) )
379
391
{
380
- //our parent has been reloaded and has not been added to the pushedSet yet
381
- if ( modified [ kind ] . Contains ( parent ) && ! pushedSet . Contains ( parent ) )
392
+ DebugTools . Assert ( parents . Length > 0 ) ;
393
+ var nonPushedParent = false ;
394
+ foreach ( var parent in parents )
382
395
{
383
- //we re-queue ourselves at the end of the queue
396
+ if ( ! toProcess . Contains ( parent ) )
397
+ continue ;
398
+
399
+ // our parent has been modified, but has not yet been processed.
400
+ // we re-queue ourselves at the end of the queue.
401
+ DebugTools . Assert ( processQueue . Contains ( parent ) ) ;
384
402
processQueue . Enqueue ( id ) ;
385
403
nonPushedParent = true ;
386
404
break ;
387
405
}
388
- }
389
406
390
- if ( nonPushedParent )
391
- continue ;
407
+ if ( nonPushedParent )
408
+ continue ;
392
409
393
- var parentMaps = new MappingDataNode [ parents . Length ] ;
394
- for ( var i = 0 ; i < parentMaps . Length ; i ++ )
410
+ var parentMaps = new MappingDataNode [ parents . Length ] ;
411
+ for ( var i = 0 ; i < parentMaps . Length ; i ++ )
412
+ {
413
+ parentMaps [ i ] = kindData . Results [ parents [ i ] ] ;
414
+ }
415
+
416
+ kindData . Results [ id ] = _serializationManager . PushCompositionWithGenericNode (
417
+ kind ,
418
+ parentMaps ,
419
+ kindData . RawResults [ id ] ) ;
420
+ }
421
+ else
395
422
{
396
- parentMaps [ i ] = kindData . Results [ parents [ i ] ] ;
423
+ kindData . Results [ id ] = kindData . RawResults [ id ] ;
397
424
}
398
-
399
- kindData . Results [ id ] = _serializationManager . PushCompositionWithGenericNode (
400
- kind ,
401
- parentMaps ,
402
- kindData . Results [ id ] ) ;
403
425
}
404
426
427
+ toProcess . Remove ( id ) ;
405
428
406
429
var prototype = TryReadPrototype ( kind , id , kindData . Results [ id ] , SerializationHookContext . DontSkipHooks ) ;
407
- if ( prototype != null )
408
- {
409
- kindData . UnfrozenInstances ??= kindData . Instances . ToDictionary ( ) ;
410
- kindData . UnfrozenInstances [ id ] = prototype ;
411
- modifiedKinds . Add ( kindData ) ;
412
- }
430
+ if ( prototype == null )
431
+ continue ;
413
432
414
- pushedSet . Add ( id ) ;
433
+ kindData . UnfrozenInstances ??= kindData . Instances . ToDictionary ( ) ;
434
+ kindData . UnfrozenInstances [ id ] = prototype ;
435
+ modifiedInstances . Add ( id , prototype ) ;
415
436
}
437
+
438
+ if ( modifiedInstances . Count == 0 )
439
+ continue ;
440
+
441
+ byType . Add ( kindData . Type , new ( modifiedInstances ) ) ;
442
+ modifiedKinds . Add ( kindData ) ;
416
443
}
417
444
418
445
Freeze ( modifiedKinds ) ;
446
+
419
447
if ( modifiedKinds . Any ( x => x . Type == typeof ( EntityPrototype ) || x . Type == typeof ( EntityCategoryPrototype ) ) )
420
448
UpdateCategories ( ) ;
421
- #endif
422
-
423
- //todo paul i hate it but i am not opening that can of worms in this refactor
424
- var byType = modified
425
- . ToDictionary (
426
- g => g . Key ,
427
- g => new PrototypesReloadedEventArgs . PrototypeChangeSet (
428
- g . Value . Where ( x => _kinds [ g . Key ] . Instances . ContainsKey ( x ) )
429
- . ToDictionary ( a => a , a => _kinds [ g . Key ] . Instances [ a ] ) ) ) ;
430
449
431
450
var modifiedTypes = new HashSet < Type > ( byType . Keys ) ;
432
451
if ( removed != null )
@@ -592,11 +611,9 @@ private async Task PushKindInheritance(Type kind, KindData data)
592
611
593
612
// var sw = RStopwatch.StartNew();
594
613
595
- var results = new Dictionary < string , InheritancePushDatum > (
596
- data . Results . Select ( k => new KeyValuePair < string , InheritancePushDatum > (
597
- k . Key ,
598
- new InheritancePushDatum ( k . Value , tree . GetParentsCount ( k . Key ) ) ) )
599
- ) ;
614
+ var results = data . RawResults . ToDictionary (
615
+ k => k . Key ,
616
+ k => new InheritancePushDatum ( k . Value , tree . GetParentsCount ( k . Key ) ) ) ;
600
617
601
618
using var countDown = new CountdownEvent ( results . Count ) ;
602
619
@@ -1015,6 +1032,8 @@ private void RegisterKind(Type kind, Dictionary<Type, KindData> kinds)
1015
1032
1016
1033
if ( kind . IsAssignableTo ( typeof ( IInheritingPrototype ) ) )
1017
1034
kindData . Inheritance = new MultiRootInheritanceGraph < string > ( ) ;
1035
+ else
1036
+ kindData . Results = kindData . RawResults ;
1018
1037
}
1019
1038
1020
1039
/// <inheritdoc />
@@ -1026,7 +1045,13 @@ private sealed class KindData(Type kind, string name)
1026
1045
1027
1046
public FrozenDictionary < string , IPrototype > Instances = FrozenDictionary < string , IPrototype > . Empty ;
1028
1047
1029
- public readonly Dictionary < string , MappingDataNode > Results = new ( ) ;
1048
+ public Dictionary < string , MappingDataNode > Results = new ( ) ;
1049
+
1050
+ /// <summary>
1051
+ /// Variant of <see cref="Results"/> prior to inheritance pushing. If the kind does not have inheritance,
1052
+ /// then this is just the same dictionary.
1053
+ /// </summary>
1054
+ public readonly Dictionary < string , MappingDataNode > RawResults = new ( ) ;
1030
1055
1031
1056
public readonly Type Type = kind ;
1032
1057
public readonly string Name = name ;
0 commit comments