Skip to content

Commit

Permalink
Reimplemented Directory
Browse files Browse the repository at this point in the history
DirectoryTree renamed to DirectoryNode. New code using SortedDictionary as tree node.
  • Loading branch information
THGSCST committed Sep 22, 2021
1 parent 0f3369f commit 72a8d0c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 108 deletions.
55 changes: 55 additions & 0 deletions HPIZ/DirectoryNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace HPIZ
{
public class DirectoryNode
{
public SortedDictionary<string, DirectoryNode> Children { get; set; }
public DirectoryNode()
{
Children = new SortedDictionary<string, DirectoryNode>(StringComparer.OrdinalIgnoreCase);
}

public void AddEntry(string entry)
{
var sections = entry.Split('\\');
AddEntryRecursively(sections, 0);
}

private void AddEntryRecursively(string[] entry, int level)
{
if (entry.Length > level)
{
if (Children.ContainsKey(entry[level]))
Children[entry[level]].AddEntryRecursively(entry, level + 1);
else
OnlyAddRecursively(entry, level);
}
}
private void OnlyAddRecursively(string[] entry, int level)
{
if (entry.Length > level)
{
Children.Add(entry[level], new DirectoryNode());
Children[entry[level]].OnlyAddRecursively(entry, level + 1);
}
}

public int CalculateSize()
{
int totalSize = 8;
foreach (var item in Children)
{
totalSize += 9;
totalSize += item.Key.Length + 1;
if (item.Value.Children.Count != 0)
totalSize += item.Value.CalculateSize();
else
totalSize += 9;
}
return totalSize;
}
}
}
82 changes: 0 additions & 82 deletions HPIZ/DirectoryTree.cs

This file was deleted.

2 changes: 1 addition & 1 deletion HPIZ/HPIZ.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Compression\Strategy.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Compression\ZLibDeflater.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Compression\LZ77.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectoryTree.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectoryNode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FileEntry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HpiArchive.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HpiFile.cs" />
Expand Down
44 changes: 20 additions & 24 deletions HPIZ/HpiArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public HpiArchive(Stream stream)

archiveReader = new BinaryReader(archiveStream);


foreach (var entry in entriesDictionary.Keys)
{
if (entriesDictionary[entry].FlagCompression != CompressionMethod.None)
Expand All @@ -84,8 +83,6 @@ public HpiArchive(Stream stream)

entriesDictionary[entry].compressedChunkSizes = size;
}
else
entriesDictionary[entry].compressedChunkSizes = new int[] { entriesDictionary[entry].UncompressedSize}; //To optimize
}
}

Expand All @@ -96,52 +93,51 @@ private void GetEntries(int NumberOfEntries, int EntryListOffset, BinaryReader r
reader.BaseStream.Position = EntryListOffset + (i * 9);

int nameOffset = reader.ReadInt32();
int dataOffset = reader.ReadInt32(); //Unused?
int dataOffset = reader.ReadInt32();
bool IsDirectory = reader.ReadBoolean();
reader.BaseStream.Position = nameOffset;
var fullPath = Path.Combine(parentPath, ReadStringCP437NullTerminated(reader));
reader.BaseStream.Position = dataOffset;

if (IsDirectory)
GetEntries(reader.ReadInt32(), reader.ReadInt32(), reader, fullPath);
else
{
FileEntry fd = new FileEntry(reader, this);
entriesDictionary.Add(fullPath, fd);
}
entriesDictionary.Add(fullPath, new FileEntry(reader, this));
}
}

internal static void SetEntries(DirectoryTree tree, BinaryWriter bw, IEnumerator<FileEntry> sequence)
internal static void SetEntries(DirectoryNode node, BinaryWriter bw, IEnumerator<FileEntry> sequence)
{
bw.Write(tree.Count); //Root Entries number in directory

bw.Write(node.Children.Count); //Root Entries number in directory
bw.Write((int)bw.BaseStream.Position + 4); //Entries Offset point to next

for (int i = 0; i < tree.Count; ++i)
bool first = true;
foreach (var item in node.Children)
{
int posString;
if (i == 0)
posString = (int)bw.BaseStream.Position + (tree.Count - i) * 9;
else
posString = (int)bw.BaseStream.Length;
int posString = (int)bw.BaseStream.Length;
if (first)
{
first = false;
posString = (int)bw.BaseStream.Position + node.Children.Count * 9;
}
bw.Write(posString); //NameOffset; /* points to the file name */
int posNext = posString + tree[i].Key.Length + 1;
bw.Write(posNext); //DirDataOffset; /* points to directory data */
bool isDir = tree[i].Children.Count != 0;
bw.Write(isDir);
int posNext = posString + item.Key.Length + 1;
bw.Write(posNext); //DataOffset; /* points to directory data */
bool isDir = item.Value.Children.Count != 0;
bw.Write(isDir);

int previousPos = (int)bw.BaseStream.Position;
bw.BaseStream.Position = posString;
WriteStringCP437NullTerminated(bw, tree[i].Key);
WriteStringCP437NullTerminated(bw, item.Key);
if (isDir)
SetEntries(tree[i].Children, bw, sequence);
SetEntries(item.Value, bw, sequence);
else
{
sequence.MoveNext();
bw.Write(sequence.Current.OffsetOfCompressedData); //OffsetOfData
bw.Write(sequence.Current.UncompressedSize); //UncompressedSize
bw.Write((byte)sequence.Current.FlagCompression); //FlagCompression

}
bw.BaseStream.Position = previousPos;
}
Expand Down
2 changes: 1 addition & 1 deletion HPIZ/HpiFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static void Merge(PathCollection archivesFiles, string destinationArchive

private static void WriteToFile(string destinationArchiveFileName, SortedDictionary<string, FileEntry> entries)
{
var tree = new DirectoryTree();
var tree = new DirectoryNode();
foreach (var fileName in entries.Keys)
tree.AddEntry(fileName);
int chunkStartPosition = tree.CalculateSize() + HpiArchive.HeaderSize;
Expand Down

0 comments on commit 72a8d0c

Please sign in to comment.