Skip to content

Commit

Permalink
Improving SortChunks function
Browse files Browse the repository at this point in the history
  • Loading branch information
Hampo committed Aug 20, 2024
1 parent 2477553 commit c7f599d
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions NetP3DLib/NetP3DLib/P3D/P3DFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public byte[] Bytes
}
}

private static readonly HashSet<uint> ChunkSortPriority = [
private static readonly List<uint> ChunkSortPriority = [
(uint)ChunkIdentifier.History,
(uint)ChunkIdentifier.Export_Info,
(uint)ChunkIdentifier.Image,
Expand Down Expand Up @@ -179,36 +179,35 @@ public void SortChunks(bool includeSectionHeaders = false, bool alphabetical = f
{
List<Chunk> newChunks = new(Chunks.Count);

foreach (uint id in ChunkSortPriority)
HashSet<uint> chunkIDs = new(Chunks.Select(x => x.ID));
foreach (uint id in chunkIDs.OrderBy(x => ChunkSortPriority.Contains(x) ? ChunkSortPriority.IndexOf(x) : int.MaxValue))
{
var chunks = Chunks.Where(x => x.ID == id);
if (!chunks.Any())
continue;

if (includeSectionHeaders)
if (chunks.First() is LocatorChunk)
{
var chunkIdentifier = (ChunkIdentifier)id;
newChunks.Add(new HistoryChunk([$"{chunkIdentifier} (0x{(uint)chunkIdentifier:X})"]));
}
var locatorChunks = chunks.Cast<LocatorChunk>();
HashSet<uint> types = new(locatorChunks.Select(x => x.LocatorType));
foreach (uint type in types.OrderBy(x => x))
{
var typeChunks = locatorChunks.Where(x => x.LocatorType == type);

if (alphabetical && chunks.First() is NamedChunk)
chunks = chunks.Cast<NamedChunk>().OrderBy(x => x.Name);
if (includeSectionHeaders)
newChunks.Add(new HistoryChunk([$"Locator Type {type} (0x{id:X})"]));

newChunks.AddRange(chunks);
}
if (alphabetical)
typeChunks = typeChunks.OrderBy(x => x.Name);

newChunks.AddRange(typeChunks);
}

HashSet<uint> remainingIDs = new(Chunks.Where(x => !ChunkSortPriority.Contains(x.ID)).Select(x => x.ID));
foreach (uint id in remainingIDs)
{
var chunks = Chunks.Where(x => x.ID == id);
if (!chunks.Any())
continue;
}

if (includeSectionHeaders)
{
var chunkIdentifier = (ChunkIdentifier)id;
newChunks.Add(new HistoryChunk([$"{chunkIdentifier} (0x{(uint)chunkIdentifier:X})"]));
}
newChunks.Add(new HistoryChunk([$"{((ChunkIdentifier)id).ToString().Replace("_", " ")} (0x{id:X})"]));

if (alphabetical && chunks.First() is NamedChunk)
chunks = chunks.Cast<NamedChunk>().OrderBy(x => x.Name);
Expand Down

0 comments on commit c7f599d

Please sign in to comment.