Skip to content

Commit

Permalink
Added includeSectionHeaders and alphabetical parameters to `SortC…
Browse files Browse the repository at this point in the history
…hunks`
  • Loading branch information
Hampo committed Aug 20, 2024
1 parent b1051d4 commit 2477553
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions NetP3DLib/NetP3DLib/P3D/P3DFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using NetP3DLib.P3D.Chunks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -172,14 +173,48 @@ public P3DFile (string filePath)
/// Sort <see cref="Chunks"/> in the priority order <c>Simpsons: Hit &amp; Run</c> expects.
/// <para>Chunk IDs without a sort priority will remain in their original order at the end of the file.</para>
/// </summary>
public void SortChunks()
/// <param name="includeSectionHeaders">If <c>true</c>, will add a History chunk before each chunk ID.</param>
/// <param name="alphabetical">If <c>true</c>, named chunks will be further sorted into alphabetical order.</param>
public void SortChunks(bool includeSectionHeaders = false, bool alphabetical = false)
{
List<Chunk> newChunks = new(Chunks.Count);

foreach (uint id in ChunkSortPriority)
newChunks.AddRange(Chunks.Where(x => x.ID == id));
{
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})"]));
}

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

newChunks.AddRange(chunks);
}

newChunks.AddRange(Chunks.Where(x => !ChunkSortPriority.Contains(x.ID)));
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})"]));
}

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

newChunks.AddRange(chunks);
}

Chunks = newChunks;
}
Expand Down

0 comments on commit 2477553

Please sign in to comment.