Skip to content

Commit 5e96684

Browse files
committed
update
1 parent ea3bae3 commit 5e96684

5 files changed

Lines changed: 101 additions & 134 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

-512 Bytes
Binary file not shown.

src/RW_NodeTree/NodeContainer.cs

Lines changed: 101 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ namespace RW_NodeTree
2222
public class NodeContainer : ThingOwner, IReadOnlyList<string>, IReadOnlyList<Thing>, IReadOnlyDictionary<string, Thing?>, IReadOnlyDictionary<Thing, string?>, IReadOnlyList<(string, Thing)>
2323
{
2424

25-
public NodeContainer(Thing processer) : base(processer as INodeProcesser)
26-
{
27-
if (processer is not INodeProcesser)
28-
{
29-
throw new InvalidCastException("Invalid processer type");
30-
}
31-
}
25+
public NodeContainer(Thing processer) : base((INodeProcesser)processer) {}
26+
3227

3328
public Thing? this[string key]
3429
{
@@ -103,16 +98,21 @@ public INodeProcesser RootNode
10398
if (!isUpgradeableReadLockHeld) readerWriterLockSlim.EnterUpgradeableReadLock();
10499
try
105100
{
106-
if (cachedRootNode != null) return cachedRootNode;
107-
INodeProcesser processer = Processer;
108-
INodeProcesser? next = ParentProcesser;
109-
while (next != null)
101+
if (cachedRootNode == null)
110102
{
111-
processer = next;
112-
next = next.ParentHolder as INodeProcesser;
103+
INodeProcesser processer = ParentProcesser?.ChildNodes.RootNode ?? Processer;
104+
bool isWriteLockHeld = readerWriterLockSlim.IsWriteLockHeld;
105+
if (!isWriteLockHeld) readerWriterLockSlim.EnterWriteLock();
106+
try
107+
{
108+
cachedRootNode = processer;
109+
}
110+
finally
111+
{
112+
if (!isWriteLockHeld) readerWriterLockSlim.ExitWriteLock();
113+
}
113114
}
114-
cachedRootNode = processer;
115-
return processer;
115+
return cachedRootNode;
116116
}
117117
finally
118118
{
@@ -156,7 +156,7 @@ public INodeProcesser RootNode
156156
public override void ExposeData()
157157
{
158158
base.ExposeData();
159-
159+
Scribe_Values.Look(ref needUpdate, "needUpdate");
160160
if (Scribe.EnterNode("InnerList"))
161161
{
162162
bool isWriteLockHeld = readerWriterLockSlim.IsWriteLockHeld;
@@ -256,7 +256,6 @@ private void CachedRootNodeNeedUpdate()
256256

257257
internal List<RenderInfo> GetNodeRenderingInfos(Rot4 rot, Graphic_ChildNode invokeSource)
258258
{
259-
UpdateNode();
260259
if (!UnityData.IsInMainThread) throw new InvalidOperationException("not in main thread");
261260
Dictionary<string, List<RenderInfo>> nodeRenderingInfos = new Dictionary<string, List<RenderInfo>>(Count + 1);
262261
Thing parent = (Thing)Processer;
@@ -342,129 +341,111 @@ private void internal_UpdateNode(INodeProcesser? actionNode = null)
342341
readerWriterLockSlim.EnterUpgradeableReadLock();
343342
try
344343
{
345-
if (!NeedUpdate) return;
346-
347-
NeedUpdate = false;
348-
349-
readerWriterLockSlim.EnterWriteLock();
350-
try
344+
while (NeedUpdate)
351345
{
352-
enableWrite = true;
353-
for (int i = Count - 1; i >= 0; i--)
346+
NeedUpdate = false;
347+
348+
Dictionary<string, object?> cachingData = new Dictionary<string, object?>();
349+
Dictionary<string, Thing> nextChilds;
350+
try
354351
{
355-
if (this[i].Destroyed)
356-
{
357-
RemoveAt(i);
358-
}
352+
nextChilds = proccess.GenChilds(actionNode, cachingData) ?? new Dictionary<string, Thing>();
359353
}
360-
enableWrite = false;
361-
}
362-
finally
363-
{
364-
readerWriterLockSlim.ExitWriteLock();
365-
}
366-
367-
368-
Dictionary<string, object?> cachingData = new Dictionary<string, object?>();
369-
Dictionary<string, Thing> nextChilds;
370-
try
371-
{
372-
nextChilds = proccess.GenChilds(actionNode, cachingData) ?? new Dictionary<string, Thing>();
373-
}
374-
catch (Exception ex)
375-
{
376-
nextChilds = new Dictionary<string, Thing>();
377-
Log.Error(ex.ToString());
378-
}
379-
380-
Dictionary<string, Thing> prveChilds = new Dictionary<string, Thing>(innerList.Count);
381-
foreach (var child in innerList)
382-
{
383-
prveChilds[child.Item1] = child.Item2;
384-
}
385-
ReadOnlyDictionary<string, Thing> prveChildsReadOnly = new ReadOnlyDictionary<string, Thing>(prveChilds);
354+
catch (Exception ex)
355+
{
356+
nextChilds = new Dictionary<string, Thing>();
357+
Log.Error(ex.ToString());
358+
}
359+
360+
Dictionary<string, Thing> prveChilds = new Dictionary<string, Thing>(innerList.Count);
361+
foreach (var child in innerList)
362+
{
363+
prveChilds[child.Item1] = child.Item2;
364+
}
365+
ReadOnlyDictionary<string, Thing> prveChildsReadOnly = new ReadOnlyDictionary<string, Thing>(prveChilds);
386366

387-
388-
readerWriterLockSlim.EnterWriteLock();
389-
try
390-
{
391-
enableWrite = true;
392-
foreach (KeyValuePair<string, Thing> pair in prveChilds)
367+
368+
readerWriterLockSlim.EnterWriteLock();
369+
try
393370
{
394-
if (!nextChilds.ContainsKey(pair.Key))
371+
enableWrite = true;
372+
foreach (KeyValuePair<string, Thing> pair in prveChilds)
395373
{
396-
Remove(pair.Value);
374+
if (!nextChilds.ContainsKey(pair.Key))
375+
{
376+
Remove(pair.Value);
377+
}
397378
}
398-
}
399-
foreach (KeyValuePair<string, Thing> pair in nextChilds)
400-
{
401-
if(!prveChilds.TryGetValue(pair.Key, out Thing? prveNode) || prveNode != pair.Value)
379+
foreach (KeyValuePair<string, Thing> pair in nextChilds)
402380
{
403-
CurrentKey = (pair.Key, -1);
404-
TryAdd(pair.Value);
381+
if(!prveChilds.TryGetValue(pair.Key, out Thing? prveNode) || prveNode != pair.Value)
382+
{
383+
CurrentKey = (pair.Key, -1);
384+
TryAdd(pair.Value);
385+
}
405386
}
387+
enableWrite = false;
388+
}
389+
catch (Exception ex)
390+
{
391+
Log.Error(ex.ToString());
392+
}
393+
finally
394+
{
395+
readerWriterLockSlim.ExitWriteLock();
406396
}
407-
enableWrite = false;
408-
}
409-
catch (Exception ex)
410-
{
411-
Log.Error(ex.ToString());
412-
}
413-
finally
414-
{
415-
readerWriterLockSlim.ExitWriteLock();
416-
}
417397

418398

419-
try
420-
{
421-
proccess.PreUpdateChilds(actionNode, cachingData, prveChildsReadOnly);
422-
}
423-
catch (Exception ex)
424-
{
425-
Log.Error(ex.ToString());
426-
}
399+
try
400+
{
401+
proccess.PreUpdateChilds(actionNode, cachingData, prveChildsReadOnly);
402+
}
403+
catch (Exception ex)
404+
{
405+
Log.Error(ex.ToString());
406+
}
427407

428408

429-
foreach (Thing? node in prveChilds.Values)
430-
{
431-
NodeContainer? container = (node as INodeProcesser)?.ChildNodes;
432-
if (container != null && container.NeedUpdate && !this.Contains(node))
409+
foreach (Thing? node in prveChilds.Values)
433410
{
434-
try
435-
{
436-
container.internal_UpdateNode(actionNode);
437-
}
438-
catch (Exception ex)
411+
NodeContainer? container = (node as INodeProcesser)?.ChildNodes;
412+
if (container != null && !this.Contains(node))
439413
{
440-
Log.Error(ex.ToString());
414+
try
415+
{
416+
container.internal_UpdateNode(actionNode);
417+
}
418+
catch (Exception ex)
419+
{
420+
Log.Error(ex.ToString());
421+
}
441422
}
442423
}
443-
}
444424

445-
foreach (Thing? node in this.Values)
446-
{
447-
NodeContainer? container = (node as INodeProcesser)?.ChildNodes;
448-
if (container != null && container.NeedUpdate)
425+
foreach (Thing? node in this.Values)
449426
{
450-
try
451-
{
452-
container.internal_UpdateNode(actionNode);
453-
}
454-
catch (Exception ex)
427+
NodeContainer? container = (node as INodeProcesser)?.ChildNodes;
428+
if (container != null)
455429
{
456-
Log.Error(ex.ToString());
430+
try
431+
{
432+
container.internal_UpdateNode(actionNode);
433+
}
434+
catch (Exception ex)
435+
{
436+
Log.Error(ex.ToString());
437+
}
457438
}
458439
}
459-
}
460440

461-
try
462-
{
463-
proccess.PostUpdateChilds(actionNode, cachingData, prveChildsReadOnly);
464-
}
465-
catch (Exception ex)
466-
{
467-
Log.Error(ex.ToString());
441+
try
442+
{
443+
proccess.PostUpdateChilds(actionNode, cachingData, prveChildsReadOnly);
444+
}
445+
catch (Exception ex)
446+
{
447+
Log.Error(ex.ToString());
448+
}
468449
}
469450
}
470451
finally
@@ -508,7 +489,6 @@ public override int GetCountCanAccept(Thing? item, bool canMergeWithExistingStac
508489
{
509490
if (!isReadLockHeld) readerWriterLockSlim.ExitReadLock();
510491
}
511-
if (item?.Destroyed ?? false) return 0;
512492
if (item?.holdingOwner != null && item.holdingOwner != this) return 0;
513493
if (IsChildOf(item)) return 0;
514494
if (Processer.AllowNode(item, currentKey.Item1))
@@ -532,20 +512,14 @@ public override int TryAdd(Thing? item, int count, bool canMergeWithExistingStac
532512
return 0;
533513
}
534514

535-
if (item.Destroyed)
536-
{
537-
Log.Warning("Tried to add destroyed item to ThingOwner.");
538-
return 0;
539-
}
540-
541515
bool isUpgradeableReadLockHeld = readerWriterLockSlim.IsUpgradeableReadLockHeld || readerWriterLockSlim.IsWriteLockHeld;
542516
if (!isUpgradeableReadLockHeld) readerWriterLockSlim.EnterUpgradeableReadLock();
543517
try
544518
{
545519

546520
if (!enableWrite)
547521
{
548-
Log.Warning("Tried to add item out side of preUpdate. Blocked...");
522+
NeedUpdate = true;
549523
return 0;
550524
}
551525

@@ -591,20 +565,14 @@ public override bool TryAdd(Thing? item, bool canMergeWithExistingStacks = true)
591565
Log.Warning("Tried to add null item to ThingOwner.");
592566
return false;
593567
}
594-
595-
if (item.Destroyed)
596-
{
597-
Log.Warning("Tried to add destroyed item to ThingOwner.");
598-
return false;
599-
}
600568
bool isUpgradeableReadLockHeld = readerWriterLockSlim.IsUpgradeableReadLockHeld || readerWriterLockSlim.IsWriteLockHeld;
601569
if (!isUpgradeableReadLockHeld) readerWriterLockSlim.EnterUpgradeableReadLock();
602570
try
603571
{
604572

605573
if (!enableWrite)
606574
{
607-
Log.Warning("Tried to add item out side of preUpdate. Blocked...");
575+
NeedUpdate = true;
608576
return false;
609577
}
610578

@@ -724,8 +692,7 @@ public override bool Remove(Thing? item)
724692
{
725693
if (!enableWrite)
726694
{
727-
if (item.Destroyed && Contains(item)) NeedUpdate = true;
728-
else Log.Warning("Tried to remove item out side of preUpdate. Blocked...");
695+
NeedUpdate = true;
729696
return false;
730697
}
731698

@@ -912,7 +879,7 @@ IEnumerator<Thing> IEnumerable<Thing>.GetEnumerator()
912879
}
913880
}
914881

915-
private bool needUpdate = true;
882+
private bool needUpdate = false;
916883

917884
private bool enableWrite = false;
918885

0 commit comments

Comments
 (0)