Skip to content

Commit

Permalink
change Groups and Links and Rails to store hashes instead of the dire…
Browse files Browse the repository at this point in the history
…ct actor instances
  • Loading branch information
shibbo committed Nov 9, 2023
1 parent fdfaa59 commit 5659cd4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 109 deletions.
4 changes: 2 additions & 2 deletions Fushigi/course/CourseArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Load()
if (root.ContainsKey("Links"))
{
BymlArrayNode? linksArray = root["Links"] as BymlArrayNode;
mLinkHolder = new(linksArray, mActorHolder);
mLinkHolder = new(linksArray);
}
else
{
Expand All @@ -83,7 +83,7 @@ public void Load()
if (root.ContainsKey("SimultaneousGroups"))
{
BymlArrayNode? groupsArray = root["SimultaneousGroups"] as BymlArrayNode;
mGroups = new CourseGroupHolder(groupsArray, mActorHolder);
mGroups = new CourseGroupHolder(groupsArray);
}
else
{
Expand Down
29 changes: 12 additions & 17 deletions Fushigi/course/CourseGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace Fushigi.course
{
public class CourseGroup
{
public CourseGroup(BymlHashTable table, CourseActorHolder actorHolder)
public CourseGroup(BymlHashTable table)
{
mHash = BymlUtil.GetNodeData<ulong>(table["Hash"]);

BymlArrayNode groups = table["Actors"] as BymlArrayNode;

foreach (BymlBigDataNode<ulong> node in groups.Array)
{
mActors.Add(actorHolder[node.Data]);
mActorHashes.Add(node.Data);
}
}

Expand All @@ -29,12 +29,12 @@ public ulong GetHash()

public bool IsActorValid(ulong hash)
{
return mActors.Any(a => a.GetHash() == hash);
return mActorHashes.Any(a => a == hash);
}

public bool TryGetIndexOfActor(ulong hash, out int index)
{
index = mActors.FindIndex(a => a.GetHash() == hash);
index = mActorHashes.FindIndex(a => a == hash);
return index != -1;
}

Expand All @@ -43,29 +43,24 @@ public BymlHashTable BuildNode()
BymlHashTable tableNode = new();
tableNode.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Hash", mHash), "Hash");

BymlArrayNode actorsArray = new((uint)mActors.Count);
BymlArrayNode actorsArray = new((uint)mActorHashes.Count);

foreach (CourseActor actor in mActors)
foreach (ulong actor in mActorHashes)
{
/* there are levels that were created in non-legit editors and that caused some things to be null when they should not have been */
if (actor == null)
{
continue;
}
actorsArray.AddNodeToArray(BymlUtil.CreateNode<ulong>("", actor.GetHash()));
actorsArray.AddNodeToArray(BymlUtil.CreateNode<ulong>("", actor));
}

tableNode.AddNode(BymlNodeId.Array, actorsArray, "Actors");
return tableNode;
}

public List<CourseActor> GetActors()
public List<ulong> GetActors()
{
return mActors;
return mActorHashes;
}

ulong mHash;
List<CourseActor> mActors = new();
List<ulong> mActorHashes = new();
}

public class CourseGroupHolder
Expand All @@ -75,11 +70,11 @@ public CourseGroupHolder()

}

public CourseGroupHolder(BymlArrayNode array, CourseActorHolder actorHolder)
public CourseGroupHolder(BymlArrayNode array)
{
foreach (BymlHashTable tbl in array.Array)
{
mGroups.Add(new CourseGroup(tbl, actorHolder));
mGroups.Add(new CourseGroup(tbl));
}

}
Expand Down
87 changes: 14 additions & 73 deletions Fushigi/course/CourseLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,87 +13,44 @@ public class CourseLink
{
public CourseLink(string linkName)
{
mSource = 0;
mDest = 0;
mLinkName = linkName;
}

public CourseLink(BymlHashTable table, CourseActorHolder actorHolder)
public CourseLink(BymlHashTable table)
{
mSource = actorHolder[BymlUtil.GetNodeData<ulong>(table["Src"])];
mDest = actorHolder[BymlUtil.GetNodeData<ulong>(table["Dst"])];
mSource = BymlUtil.GetNodeData<ulong>(table["Src"]);
mDest = BymlUtil.GetNodeData<ulong>(table["Dst"]);
mLinkName = BymlUtil.GetNodeData<string>(table["Name"]);
}

public BymlHashTable BuildNode()
{
BymlHashTable tbl = new();
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Dst", mDest.GetHash()), "Dst");
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Dst", mDest), "Dst");
tbl.AddNode(BymlNodeId.String, BymlUtil.CreateNode<string>("Name", mLinkName), "Name");
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Src", mSource.GetHash()), "Src");
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Src", mSource), "Src");
return tbl;
}

public ulong GetSrcHash()
{
if (mSource == null)
{
return 0;
}

return mSource.GetHash();
return mSource;
}

public ulong GetDestHash()
{
if (mDest == null)
{
return 0;
}

return mDest.GetHash();
}

public void SetSrcHash(ulong hash, CourseActorHolder actorHolder)
{
mSource = actorHolder[hash];
}

public void SetDestHash(ulong hash, CourseActorHolder actorHolder)
{
mDest = actorHolder[hash];
}

public bool AreLinksValid(CourseActorHolder actorHolder)
{
return actorHolder.HasHash(mSource.GetHash()) && actorHolder.HasHash(mDest.GetHash());
return mDest;
}

public string GetLinkName()
{
return mLinkName;
}

public bool IsSourceValid(CourseActorHolder actorHolder)
{
return actorHolder.HasHash(mSource.GetHash());
}

public bool IsDestValid(CourseActorHolder actorHolder)
{
return actorHolder.HasHash(mDest.GetHash());
}

public bool IsSourceActorExist()
{
return mSource != null;
}

public bool IsDestActorExist()
{
return mDest != null;
}

CourseActor? mSource;
CourseActor? mDest;
public ulong mSource;
public ulong mDest;
string mLinkName;
}

Expand All @@ -104,11 +61,11 @@ public CourseLinkHolder()

}

public CourseLinkHolder(BymlArrayNode linkArray, CourseActorHolder actorHolder)
public CourseLinkHolder(BymlArrayNode linkArray)
{
foreach (BymlHashTable tbl in linkArray.Array)
{
mLinks.Add(new CourseLink(tbl, actorHolder));
mLinks.Add(new CourseLink(tbl));
}
}

Expand Down Expand Up @@ -193,29 +150,13 @@ public List<CourseLink> GetLinks()
return mLinks;
}

public bool IsAnyLinkInvalid(CourseActorHolder holder)
{
foreach (CourseLink link in mLinks)
{
if (!link.IsSourceValid(holder) || !link.IsDestValid(holder))
{
return true;
}
}

return false;
}

public BymlArrayNode SerializeToArray()
{
BymlArrayNode node = new();

foreach(CourseLink link in mLinks)
{
if (link.IsSourceActorExist() && link.IsDestActorExist())
{
node.AddNodeToArray(link.BuildNode());
}
node.AddNodeToArray(link.BuildNode());
}

return node;
Expand Down
20 changes: 10 additions & 10 deletions Fushigi/course/CourseRail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ public class CourseActorToRailLinks
{
struct Link
{
public CourseActor? Source;
public CourseRail? Dest;
public CourseRail.CourseRailPoint? Point;
public ulong Source;
public ulong Dest;
public ulong Point;
public string Name;
}

Expand All @@ -395,9 +395,9 @@ public CourseActorToRailLinks(BymlArrayNode array, CourseActorHolder actorHolder
string name = BymlUtil.GetNodeData<string>(railLink["Name"]);

Link link = new();
link.Source = actorHolder[sourceHash];
link.Dest = railHolder[destHash];
link.Point = link.Dest[pointHash];
link.Source = sourceHash;
link.Dest = destHash;
link.Point = pointHash;
link.Name = name;

mLinks.Add(link);
Expand All @@ -414,7 +414,7 @@ public void RemoveLinkFromSrc(ulong hash)
int idx = -1;
foreach (Link link in mLinks)
{
if (link.Source.GetHash() == hash)
if (link.Source == hash)
{
idx = mLinks.IndexOf(link);
break;
Expand All @@ -434,10 +434,10 @@ public BymlArrayNode SerializeToArray()
foreach (Link link in mLinks)
{
BymlHashTable tbl = new();
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Dst", link.Dest.GetHash()), "Dst");
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Dst", link.Dest), "Dst");
tbl.AddNode(BymlNodeId.String, BymlUtil.CreateNode<string>("Name", link.Name), "Name");
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Point", link.Point.GetHash()), "Point");
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Src", link.Source.GetHash()), "Src");
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Point", link.Point), "Point");
tbl.AddNode(BymlNodeId.UInt64, BymlUtil.CreateNode<ulong>("Src", link.Source), "Src");
node.AddNodeToArray(tbl);
}

Expand Down
5 changes: 0 additions & 5 deletions Fushigi/ui/CourseAreaEditContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,6 @@ public bool IsActorDestForLink(CourseActor actor)
return area.mLinkHolder.GetLinkWithDestHash(actor.GetHash()) != null;
}

public bool IsAnyLinkInvalid()
{
return area.mLinkHolder.IsAnyLinkInvalid(area.mActorHolder);
}

public void AddLink(CourseLink link)
{
Console.WriteLine($"Adding Link: Source: {link.GetSrcHash()} -- Dest: {link.GetDestHash()}");
Expand Down
4 changes: 2 additions & 2 deletions Fushigi/ui/widgets/LevelViewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Click on any actor to delete it.
{
CurCourseLink = new(mNewLinkType);
CourseActor selActor = mEditContext.GetSelectedObjects<CourseActor>().ElementAt(0);
CurCourseLink.SetSrcHash(selActor.GetHash(), mEditContext.GetActorHolder());
CurCourseLink.mSource = selActor.GetHash();
mIsLinkNew = false;
}

Expand All @@ -431,7 +431,7 @@ Click on any actor to delete it.
/* new links have a destination of 0 because there is no hash associated with a null actor */
bool isNewLink = CurCourseLink.GetDestHash() == 0;
ulong hash = hoveredActor.GetHash();
CurCourseLink.SetDestHash(hash, mEditContext.GetActorHolder());
CurCourseLink.mDest = hash;

if (isNewLink)
{
Expand Down

0 comments on commit 5659cd4

Please sign in to comment.