Skip to content

Commit b03948f

Browse files
committed
fix node update and root node cache
1 parent 079ce31 commit b03948f

7 files changed

Lines changed: 71 additions & 63 deletions

File tree

1.3/Assemblies/RW_NodeTree.dll

512 Bytes
Binary file not shown.

1.4/Assemblies/RW_NodeTree.dll

512 Bytes
Binary file not shown.

1.5/Assemblies/RW_NodeTree.dll

512 Bytes
Binary file not shown.

1.6/Assemblies/RW_NodeTree.dll

0 Bytes
Binary file not shown.

src/RW_NodeTree/Graphic_ChildNode.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using RW_NodeTree.Tools;
33
using System;
44
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
56
using UnityEngine;
67
using Verse;
78

@@ -50,13 +51,13 @@ public override Material MatAt(Rot4 rot, Thing? thing = null)
5051

5152
(Material? material, Texture2D? texture, RenderTexture? cachedRenderTarget) = defaultRenderingCache[rot];
5253

53-
List<(string?, Thing, List<RenderInfo>)> commands = currentProccesser.ChildNodes.GetNodeRenderingInfos(rot, out bool needUpdate, subGraphic);
54+
ReadOnlyDictionary<string, ReadOnlyCollection<RenderInfo>> commands = currentProccesser.ChildNodes.GetNodeRenderingInfos(rot, out bool needUpdate, subGraphic);
5455
if (needUpdate || material == null || texture == null || cachedRenderTarget == null)
5556
{
5657
List<RenderInfo> final = new List<RenderInfo>();
57-
foreach ((string?, Thing, List<RenderInfo>) infos in commands)
58+
foreach (var infos in commands)
5859
{
59-
if (!infos.Item3.NullOrEmpty()) final.AddRange(infos.Item3);
60+
if (!infos.Value.NullOrEmpty()) final.AddRange(infos.Value);
6061
}
6162
RenderingTools.RenderToTarget(final, ref cachedRenderTarget!, ref texture!, default(Vector2Int), currentProccesser.TextureSizeFactor, currentProccesser.ExceedanceFactor, currentProccesser.ExceedanceOffset, currentProccesser.HasPostFX(true) ? currentProccesser.PostFX : default);
6263
Shader shader = subGraphic.Shader;
@@ -105,9 +106,9 @@ public override void DrawWorker(Vector3 loc, Rot4 rot, ThingDef? thingDef, Thing
105106
{
106107
MatAt(rot, thing);
107108
List<RenderInfo> final = new List<RenderInfo>();
108-
foreach ((string?, Thing, List<RenderInfo>) infos in currentProccesser.ChildNodes.GetNodeRenderingInfos(rot, out _, subGraphic))
109+
foreach (var infos in currentProccesser.ChildNodes.GetNodeRenderingInfos(rot, out _, subGraphic))
109110
{
110-
if (!infos.Item3.NullOrEmpty()) final.AddRange(infos.Item3);
111+
if (!infos.Value.NullOrEmpty()) final.AddRange(infos.Value);
111112
}
112113
Matrix4x4 matrix = Matrix4x4.TRS(loc, Quaternion.AngleAxis(extraRotation, Vector3.up), Vector3.one);
113114
for (int i = 0; i < final.Count; i++)

src/RW_NodeTree/INodeProccesser.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using RW_NodeTree.Rendering;
2-
using System;
32
using System.Collections.Generic;
43
using System.Collections.ObjectModel;
54
using UnityEngine;
@@ -70,7 +69,7 @@ public partial interface INodeProccesser : IThingHolder
7069
/// <param name="rot">rotation</param>
7170
/// <param name="graphic">original graphic</param>
7271
/// <returns></returns>
73-
List<(string?, Thing, List<RenderInfo>)> PreGenRenderInfos(List<(string?, Thing, List<RenderInfo>)> nodeRenderingInfos, Rot4 rot, Graphic? graphic, Dictionary<string, object?> cachedDataToPostDrawStep);
72+
HashSet<string> PreGenRenderInfos(Rot4 rot, Dictionary<string, object?> cachedDataToPostDrawStep);
7473

7574
/// <summary>
7675
/// Adapte draw steep of this node
@@ -79,6 +78,6 @@ public partial interface INodeProccesser : IThingHolder
7978
/// <param name="rot">rotation</param>
8079
/// <param name="graphic">original graphic</param>
8180
/// <returns></returns>
82-
List<(string?, Thing, List<RenderInfo>)> PostGenRenderInfos(List<(string?, Thing, List<RenderInfo>)> nodeRenderingInfos, Rot4 rot, Graphic? graphic, Dictionary<string, object?> cachedDataFromPerDrawStep);
81+
Dictionary<string, List<RenderInfo>>? PostGenRenderInfos(Dictionary<string, List<RenderInfo>> nodeRenderingInfos, Rot4 rot, Graphic? graphic, Dictionary<string, object?> cachedDataFromPerDrawStep);
8382
}
8483
}

src/RW_NodeTree/NodeContainer.cs

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class NodeContainer : ThingOwner, IList<string>, IList<Thing>, IDictionar
2121
{
2222
public class NodeRenderingInfoForRot4
2323
{
24-
public List<(string?, Thing, List<RenderInfo>)>? this[Rot4 rot]
24+
public ReadOnlyDictionary<string, ReadOnlyCollection<RenderInfo>>? this[Rot4 rot]
2525
{
2626
get => nodeRenderingInfos[rot.AsInt];
2727
set => nodeRenderingInfos[rot.AsInt] = value;
@@ -30,7 +30,7 @@ public void Reset()
3030
{
3131
for (int i = 0; i < nodeRenderingInfos.Length; i++) nodeRenderingInfos[i] = null;
3232
}
33-
public readonly List<(string?, Thing, List<RenderInfo>)>?[] nodeRenderingInfos = new List<(string?, Thing, List<RenderInfo>)>?[4];
33+
public readonly ReadOnlyDictionary<string, ReadOnlyCollection<RenderInfo>>?[] nodeRenderingInfos = new ReadOnlyDictionary<string, ReadOnlyCollection<RenderInfo>>?[4];
3434
}
3535

3636
public NodeContainer(Thing proccesser) : base(proccesser as INodeProccesser)
@@ -369,16 +369,25 @@ public override void ExposeData()
369369
//if (Scribe.mode == LoadSaveMode.PostLoadInit) needUpdate = false;
370370
}
371371

372+
public void CachedRootNodeNeedUpdate()
373+
{
374+
cachedRootNode = null;
375+
for (int i = Count - 1; i >= 0; i--)
376+
{
377+
(this[i] as INodeProccesser)?.ChildNodes?.CachedRootNodeNeedUpdate();
378+
}
379+
}
380+
372381

373-
public List<(string?, Thing, List<RenderInfo>)> GetNodeRenderingInfos(Rot4 rot, out bool updated, Graphic? subGraphic = null)
382+
public ReadOnlyDictionary<string, ReadOnlyCollection<RenderInfo>> GetNodeRenderingInfos(Rot4 rot, out bool updated, Graphic? subGraphic = null)
374383
{
375384
UpdateNode();
376385
updated = false;
377386
if (!UnityData.IsInMainThread) throw new InvalidOperationException("not in main thread");
378-
List<(string?, Thing, List<RenderInfo>)>? nodeRenderingInfos = this.nodeRenderingInfo[rot];
379-
if (nodeRenderingInfos != null) return nodeRenderingInfos;
387+
ReadOnlyDictionary<string, ReadOnlyCollection<RenderInfo>>? readOnlyNodeRenderingInfos = this.nodeRenderingInfo[rot];
388+
if (readOnlyNodeRenderingInfos != null) return readOnlyNodeRenderingInfos;
380389
updated = true;
381-
nodeRenderingInfos = new List<(string?, Thing, List<RenderInfo>)>(Count + 1);
390+
Dictionary<string, List<RenderInfo>> nodeRenderingInfos = new Dictionary<string, List<RenderInfo>>(Count + 1);
382391
Thing parent = (Thing)Proccesser;
383392

384393
//if (Prefs.DevMode)
@@ -396,67 +405,65 @@ public override void ExposeData()
396405

397406

398407
//ORIGIN
399-
subGraphic = (subGraphic ?? parent.Graphic)?.GetGraphic_ChildNode()?.SubGraphic ?? subGraphic ?? parent.Graphic;
400-
if (subGraphic != null)
401-
{
402-
RenderingTools.StartOrEndDrawCatchingBlock = true;
403-
try
404-
{
405-
subGraphic.Draw(Vector3.zero, rot, parent);
406-
nodeRenderingInfos.Add((null, parent, RenderingTools.RenderInfos!));
407-
}
408-
catch (Exception ex)
409-
{
410-
Log.Error(ex.ToString());
411-
}
412-
RenderingTools.StartOrEndDrawCatchingBlock = false;
413-
}
414-
415-
for (int i = 0; i < this.Count; i++)
416-
{
417-
Thing child = this[i];
418408

419-
if (child != null)
420-
{
421-
nodeRenderingInfos.Add((((IList<string>)this)[i], child, new List<RenderInfo>()));
422-
}
423-
}
424409

425410
Dictionary<string, object?> cachingData = new Dictionary<string, object?>();
426411

427-
nodeRenderingInfos = Proccesser.PreGenRenderInfos(nodeRenderingInfos, rot, subGraphic, cachingData) ?? nodeRenderingInfos;
412+
HashSet<string>? ChildForDraw = Proccesser.PreGenRenderInfos(rot, cachingData);
428413

429-
for (int i = 0; i < nodeRenderingInfos.Count; i++)
414+
subGraphic = (subGraphic ?? parent.Graphic)?.GetGraphic_ChildNode()?.SubGraphic ?? subGraphic ?? parent.Graphic;
415+
if (ChildForDraw != null)
430416
{
431-
string? id = nodeRenderingInfos[i].Item1;
432-
Thing child = nodeRenderingInfos[i].Item2;
433-
List<RenderInfo> infos = nodeRenderingInfos[i].Item3 ?? new List<RenderInfo>();
434-
if (child != null && id != null)
417+
if (subGraphic != null && ChildForDraw.Contains(""))
435418
{
436419
RenderingTools.StartOrEndDrawCatchingBlock = true;
437420
try
438421
{
439-
Rot4 rotCache = child.Rotation;
440-
child.Rotation = new Rot4((rot.AsInt + rotCache.AsInt) & 3);
441-
#if V13 || V14
442-
child.DrawAt(Vector3.zero);
443-
#else
444-
child.DrawNowAt(Vector3.zero);
445-
#endif
446-
child.Rotation = rotCache;
447-
infos.AddRange(RenderingTools.RenderInfos);
422+
subGraphic.Draw(Vector3.zero, rot, parent);
423+
nodeRenderingInfos[""] = RenderingTools.RenderInfos!;
448424
}
449425
catch (Exception ex)
450426
{
451427
Log.Error(ex.ToString());
452428
}
453429
RenderingTools.StartOrEndDrawCatchingBlock = false;
454-
nodeRenderingInfos[i] = (id, child, infos);
430+
}
431+
432+
foreach (string id in ChildForDraw)
433+
{
434+
Thing? child = this[id];
435+
if (child != null && id != null)
436+
{
437+
RenderingTools.StartOrEndDrawCatchingBlock = true;
438+
try
439+
{
440+
Rot4 rotCache = child.Rotation;
441+
child.Rotation = new Rot4((rot.AsInt + rotCache.AsInt) & 3);
442+
#if V13 || V14
443+
child.DrawAt(Vector3.zero);
444+
#else
445+
child.DrawNowAt(Vector3.zero);
446+
#endif
447+
child.Rotation = rotCache;
448+
nodeRenderingInfos[id] = RenderingTools.RenderInfos!;
449+
}
450+
catch (Exception ex)
451+
{
452+
Log.Error(ex.ToString());
453+
}
454+
RenderingTools.StartOrEndDrawCatchingBlock = false;
455+
}
455456
}
456457
}
457458
nodeRenderingInfos = Proccesser.PostGenRenderInfos(nodeRenderingInfos, rot, subGraphic, cachingData) ?? nodeRenderingInfos;
458-
this.nodeRenderingInfo[rot] = nodeRenderingInfos;
459-
return nodeRenderingInfos;
459+
Dictionary<string, ReadOnlyCollection<RenderInfo>> mid = new Dictionary<string, ReadOnlyCollection<RenderInfo>>(nodeRenderingInfos.Count);
460+
foreach (var kv in nodeRenderingInfos)
461+
{
462+
mid[kv.Key] = new ReadOnlyCollection<RenderInfo>(kv.Value);
463+
}
464+
readOnlyNodeRenderingInfos = new ReadOnlyDictionary<string, ReadOnlyCollection<RenderInfo>>(mid);
465+
this.nodeRenderingInfo[rot] = readOnlyNodeRenderingInfos;
466+
return readOnlyNodeRenderingInfos;
460467
}
461468

462469

@@ -466,7 +473,7 @@ public override void ExposeData()
466473
/// <returns></returns>
467474
public void UpdateNode() => internal_UpdateNode();
468475

469-
internal void internal_UpdateNode(INodeProccesser? actionNode = null)
476+
private void internal_UpdateNode(INodeProccesser? actionNode = null)
470477
{
471478

472479

@@ -550,17 +557,17 @@ internal void internal_UpdateNode(INodeProccesser? actionNode = null)
550557
}
551558

552559
bool reset = true;
560+
553561
foreach (Thing? node in prveChilds.Values)
554562
{
555563
NodeContainer? container = (node as INodeProccesser)?.ChildNodes;
556-
if (container != null && container.NeedUpdate)
564+
if (container != null && container.NeedUpdate && !this.Contains(node))
557565
{
558566
container.internal_UpdateNode(actionNode);
559-
reset = false;
560567
}
561568
}
562569

563-
foreach (Thing? node in diff.Values)
570+
foreach (Thing? node in this.Values)
564571
{
565572
NodeContainer? container = (node as INodeProccesser)?.ChildNodes;
566573
if (container != null && container.NeedUpdate)
@@ -569,6 +576,7 @@ internal void internal_UpdateNode(INodeProccesser? actionNode = null)
569576
reset = false;
570577
}
571578
}
579+
572580
ReadOnlyDictionary<string, Thing> prveChildsReadOnly = new ReadOnlyDictionary<string, Thing>(prveChilds);
573581
proccess.PostUpdateChilds(actionNode, cachingData, prveChildsReadOnly, out bool notUpdateTexture);
574582
if (reset && !notUpdateTexture)
@@ -620,7 +628,7 @@ public override int GetCountCanAccept(Thing? item, bool canMergeWithExistingStac
620628
if (item?.Destroyed ?? false) return 0;
621629
if (item?.holdingOwner != null) return 0;
622630
if (IsChildOf(item)) return 0;
623-
if (Proccesser != null && Proccesser.AllowNode(item, currentKey.Item1))
631+
if (Proccesser.AllowNode(item, currentKey.Item1))
624632
{
625633
return base.GetCountCanAccept(item, false);
626634
}
@@ -778,7 +786,7 @@ public override bool TryAdd(Thing? item, bool canMergeWithExistingStacks = true)
778786
}
779787
if (proccesser != null)
780788
{
781-
proccesser.ChildNodes.cachedRootNode = null;
789+
proccesser.ChildNodes.CachedRootNodeNeedUpdate();
782790
proccesser.Added(this, currentKey.Item1, true);
783791
}
784792
//NeedUpdate = true;
@@ -1026,7 +1034,7 @@ public override bool Remove(Thing? item)
10261034
item.holdingOwner = null;
10271035
if (proccesser != null)
10281036
{
1029-
proccesser.ChildNodes.cachedRootNode = null;
1037+
proccesser.ChildNodes.CachedRootNodeNeedUpdate();
10301038
proccesser.Removed(this, key, true);
10311039
}
10321040
//NeedUpdate = true;

0 commit comments

Comments
 (0)