From 419ec29c18bdd975095a12f837c817133515244b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20Sch=C3=B6ner?= <tobias.stonehead@gmail.com>
Date: Sat, 21 Jan 2023 19:22:43 +0100
Subject: [PATCH] fix: FactExplorer use factState methods for getting child and
 parent facts

---
 .../Scripts/UI/FactExplorer/FactExplorer.cs   | 34 ++-----------------
 1 file changed, 3 insertions(+), 31 deletions(-)

diff --git a/Assets/Scripts/UI/FactExplorer/FactExplorer.cs b/Assets/Scripts/UI/FactExplorer/FactExplorer.cs
index 439eaf68..f47cbf1e 100644
--- a/Assets/Scripts/UI/FactExplorer/FactExplorer.cs
+++ b/Assets/Scripts/UI/FactExplorer/FactExplorer.cs
@@ -51,27 +51,13 @@ public void Initialize(Fact fact, Vector3 factPosition)
     #region Implementation
     private List<Fact> GetParentFacts()
     {   
-        //TODO: TSC how to get all current facts without this dumb workaround
-        var allFacts = DisplayFacts.displayedFacts.Values.Select(x => x.GetComponent<FactWrapper>().fact);
-        List<Fact> _parentFacts = new();
-
-        // get all facts that reference the mainFact
-        foreach (Fact f in allFacts)
-            if (GetReferencedPids(f).Contains(mainFact.Id)) // if f contains reference to mainFact
-                _parentFacts.Add(f);
-
-        return _parentFacts;
+        _ = StageStatic.stage.factState.safe_dependencies(mainFact.Id, out var parentFactIds);
+        return parentFactIds.Distinct().Select(factId => StageStatic.stage.factState[factId]).Where(f => f != mainFact).ToList();
     }
 
     private List<Fact> GetChildFacts()
     {
-        List<Fact> childFacts = new();
-
-        // get all properties of Type mainFactType that are strings and start with "pid"
-        foreach (string factId in GetReferencedPids(mainFact))
-            childFacts.Add(StageStatic.stage.factState[factId]);
-
-        return childFacts;
+        return mainFact.getDependentFactIds().Distinct().Select(factId => StageStatic.stage.factState[factId]).ToList();
     }
 
     private void UpdateFactExplorerUI()
@@ -175,18 +161,4 @@ private void SpawnChildLines(GameObject parent, Transform mainFactUI)
         });
     }
     #endregion Spawner
-
-    #region Helper
-    /// <summary>
-    /// Check all fields of a Fact for references to other facts and return their ids
-    /// </summary>
-    /// <param name="f"></param>
-    /// <returns>An Enumerable containing the ids of facts referenced by the input fact</returns>
-    private static IEnumerable<string> GetReferencedPids(Fact f)
-    {
-        return (f.GetType().GetFields()
-            .Where(field => field.FieldType == typeof(string) && field.Name.ToLower().StartsWith("pid"))
-        ).Select(fi => (string)fi.GetValue(f));
-    }
-    #endregion Helper
 }
-- 
GitLab