lazy loading

This commit is contained in:
Justin Aquadro 2011-12-02 22:24:39 -05:00
parent 0dfe8f1e04
commit fb1fbf1580

121
Form1.cs
View file

@ -139,19 +139,22 @@ namespace NBTPlus
public void LoadRegion (TreeNodeCollection parent, string path) public void LoadRegion (TreeNodeCollection parent, string path)
{ {
RegionFile rf = new RegionFile(path);
TreeNode root = new TreeNode(Path.GetFileName(path), 11, 11); TreeNode root = new TreeNode(Path.GetFileName(path), 11, 11);
LoadRegion(root, path);
parent.Add(root);
}
public void LoadRegion (TreeNode node, string path)
{
RegionFile rf = new RegionFile(path);
for (int x = 0; x < 32; x++) { for (int x = 0; x < 32; x++) {
for (int y = 0; y < 32; y++) { for (int y = 0; y < 32; y++) {
if (rf.HasChunk(x, y)) { if (rf.HasChunk(x, y)) {
root.Nodes.Add(CreateLazyChunk(rf, x, y)); node.Nodes.Add(CreateLazyChunk(rf, x, y));
} }
} }
} }
parent.Add(root);
} }
public TreeNode CreateLazyChunk (RegionFile rf, int x, int z) public TreeNode CreateLazyChunk (RegionFile rf, int x, int z)
@ -166,6 +169,18 @@ namespace NBTPlus
return node; return node;
} }
public TreeNode CreateLazyRegion (string path)
{
TreeNode node = new TreeNode();
node.ImageIndex = 11;
node.SelectedImageIndex = 11;
node.Text = Path.GetFileName(path);
node.Tag = new RegionData(path);
node.Nodes.Add(new TreeNode());
return node;
}
public TreeNode CreateLazyNbt (string path, CompressionType cztype) public TreeNode CreateLazyNbt (string path, CompressionType cztype)
{ {
TreeNode node = new TreeNode(); TreeNode node = new TreeNode();
@ -178,6 +193,18 @@ namespace NBTPlus
return node; return node;
} }
public TreeNode CreateLazyDirectory (string path)
{
TreeNode node = new TreeNode();
node.ImageIndex = 10;
node.SelectedImageIndex = 10;
node.Text = Path.GetFileName(path);
node.Tag = new DirectoryData(path);
node.Nodes.Add(new TreeNode());
return node;
}
public void LoadLazyChunk (TreeNode node) public void LoadLazyChunk (TreeNode node)
{ {
RegionChunkData data = node.Tag as RegionChunkData; RegionChunkData data = node.Tag as RegionChunkData;
@ -220,6 +247,53 @@ namespace NBTPlus
node.Nodes.Add(new TreeNode()); node.Nodes.Add(new TreeNode());
} }
public void LoadLazyDirectory (TreeNode node)
{
DirectoryData data = node.Tag as DirectoryData;
if (data == null)
return;
node.Nodes.Clear();
foreach (string dirpath in Directory.EnumerateDirectories(data.Path)) {
node.Nodes.Add(CreateLazyDirectory(dirpath));
}
foreach (string filepath in Directory.EnumerateFiles(data.Path)) {
TryLoadFile(node.Nodes, filepath);
}
}
public void UnloadLazyDirectory (TreeNode node)
{
DirectoryData data = node.Tag as DirectoryData;
if (data == null)
return;
node.Nodes.Clear();
node.Nodes.Add(new TreeNode());
}
public void LoadLazyRegion (TreeNode node)
{
RegionData data = node.Tag as RegionData;
if (data == null)
return;
node.Nodes.Clear();
LoadRegion(node, data.Path);
}
public void UnloadLazyRegion (TreeNode node)
{
RegionData data = node.Tag as RegionData;
if (data == null)
return;
node.Nodes.Clear();
node.Nodes.Add(new TreeNode());
}
private void NodeExpand (object sender, TreeViewCancelEventArgs e) private void NodeExpand (object sender, TreeViewCancelEventArgs e)
{ {
if (e.Node.Tag == null) if (e.Node.Tag == null)
@ -231,6 +305,12 @@ namespace NBTPlus
else if (e.Node.Tag is NbtFileData) { else if (e.Node.Tag is NbtFileData) {
LoadLazyNbt(e.Node); LoadLazyNbt(e.Node);
} }
else if (e.Node.Tag is DirectoryData) {
LoadLazyDirectory(e.Node);
}
else if (e.Node.Tag is RegionData) {
LoadLazyRegion(e.Node);
}
} }
private void NodeCollapse (object sender, TreeViewEventArgs e) private void NodeCollapse (object sender, TreeViewEventArgs e)
@ -244,6 +324,12 @@ namespace NBTPlus
else if (e.Node.Tag is NbtFileData) { else if (e.Node.Tag is NbtFileData) {
UnloadLazyNbt(e.Node); UnloadLazyNbt(e.Node);
} }
else if (e.Node.Tag is DirectoryData) {
UnloadLazyDirectory(e.Node);
}
else if (e.Node.Tag is RegionData) {
UnloadLazyRegion(e.Node);
}
} }
public void LoadDirectory (string path) public void LoadDirectory (string path)
@ -261,7 +347,8 @@ namespace NBTPlus
TreeNode root = new TreeNode(name, 10, 10); TreeNode root = new TreeNode(name, 10, 10);
foreach (string dirpath in Directory.EnumerateDirectories(path)) { foreach (string dirpath in Directory.EnumerateDirectories(path)) {
LoadDirectory(dirpath, root.Nodes, Path.GetFileName(dirpath)); root.Nodes.Add(CreateLazyDirectory(dirpath));
//LoadDirectory(dirpath, root.Nodes, Path.GetFileName(dirpath));
} }
foreach (string filepath in Directory.EnumerateFiles(path)) { foreach (string filepath in Directory.EnumerateFiles(path)) {
@ -274,7 +361,7 @@ namespace NBTPlus
public void TryLoadFile (TreeNodeCollection parent, string path) public void TryLoadFile (TreeNodeCollection parent, string path)
{ {
if (Path.GetExtension(path) == ".mcr") { if (Path.GetExtension(path) == ".mcr") {
LoadRegion(parent, path); parent.Add(CreateLazyRegion(path));
return; return;
} }
@ -399,6 +486,16 @@ namespace NBTPlus
public int Z { get; private set; } public int Z { get; private set; }
} }
public class RegionData
{
public RegionData (string path)
{
Path = path;
}
public string Path { get; private set; }
}
public class NbtFileData public class NbtFileData
{ {
public NbtFileData (string path, CompressionType cztype) public NbtFileData (string path, CompressionType cztype)
@ -410,4 +507,14 @@ namespace NBTPlus
public string Path { get; private set; } public string Path { get; private set; }
public CompressionType CompressionType { get; private set; } public CompressionType CompressionType { get; private set; }
} }
public class DirectoryData
{
public DirectoryData (string path)
{
Path = path;
}
public string Path { get; private set; }
}
} }