-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDargonNodeUtilities.cs
34 lines (32 loc) · 1.23 KB
/
DargonNodeUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
namespace Dargon.IO {
internal static class DargonNodeUtilities {
/// <summary>
/// Treeifies the given collection, converting it into a resource tree.
/// </summary>
/// <returns></returns>
public static void TreeifyInto<TCollectionEntry>(
this IEnumerable<TCollectionEntry> leafEntries,
WritableDargonNode root,
Func<TCollectionEntry, string[]> getBreadcrumbs,
Func<string, WritableDargonNode> createInnerNode,
Func<TCollectionEntry, WritableDargonNode> createLeaf
) {
foreach (var leafEntry in leafEntries) {
var breadcrumbs = getBreadcrumbs(leafEntry);
var currentNode = root;
for (int i = 1; i < breadcrumbs.Length - 1; i++) {
string nextDirectoryName = breadcrumbs[i];
WritableDargonNode nextNode;
if (!currentNode.TryGetChild(nextDirectoryName, out nextNode)) {
nextNode = createInnerNode(nextDirectoryName);
currentNode.AddChild(nextNode);
}
currentNode = nextNode;
}
currentNode.AddChild(createLeaf(leafEntry));
}
}
}
}