private static void AlignGroupsByRules( MutagenBinaryReadStream inputStream, MutagenWriter writer, AlignmentRules alignmentRules, RecordLocatorResults fileLocs) { while (!inputStream.Complete) { // Import until next listed major record long noRecordLength; if (fileLocs.GrupLocations.TryGetInDirection( inputStream.Position, higher: true, result: out var nextRec)) { noRecordLength = nextRec.Value.Location.Min - inputStream.Position; } else { noRecordLength = inputStream.Remaining; } inputStream.WriteTo(writer.BaseStream, (int)noRecordLength); // If complete overall, return if (inputStream.Complete) { break; } var groupMeta = inputStream.GetGroup(); if (!groupMeta.IsGroup) { throw new ArgumentException(); } writer.Write(inputStream.ReadSpan(groupMeta.HeaderLength)); if (!alignmentRules.GroupAlignment.TryGetValue(groupMeta.GroupType, out var groupRules)) { continue; } var storage = new Dictionary <RecordType, List <ReadOnlyMemorySlice <byte> > >(); var rest = new List <ReadOnlyMemorySlice <byte> >(); using (var frame = MutagenFrame.ByLength(inputStream, groupMeta.ContentLength)) { while (!frame.Complete) { var majorMeta = inputStream.GetMajorRecord(); var bytes = inputStream.ReadMemory(checked ((int)majorMeta.TotalLength)); var type = majorMeta.RecordType; if (groupRules.Contains(type)) { storage.GetOrAdd(type).Add(bytes); } else { rest.Add(bytes); } } } foreach (var rule in groupRules) { if (storage.TryGetValue(rule, out var storageBytes)) { foreach (var item in storageBytes) { writer.Write(item); } } } foreach (var item in rest) { writer.Write(item); } } }
public static void Sort( Func <Stream> streamCreator, Stream outputStream, GameMode gameMode) { var meta = new ParsingBundle(GameConstants.Get(gameMode)); using var inputStream = new MutagenBinaryReadStream(streamCreator(), meta); using var locatorStream = new MutagenBinaryReadStream(streamCreator(), meta); using var writer = new MutagenWriter(outputStream, gameMode, dispose: false); while (!inputStream.Complete) { long noRecordLength; foreach (var grupLoc in RecordLocator.IterateBaseGroupLocations(locatorStream)) { noRecordLength = grupLoc.Value - inputStream.Position; inputStream.WriteTo(writer.BaseStream, (int)noRecordLength); // If complete overall, return if (inputStream.Complete) { return; } var groupMeta = inputStream.GetGroup(); if (!groupMeta.IsGroup) { throw new ArgumentException(); } var storage = new Dictionary <FormID, List <ReadOnlyMemorySlice <byte> > >(); using (var grupFrame = new MutagenFrame(inputStream).SpawnWithLength(groupMeta.TotalLength)) { inputStream.WriteTo(writer.BaseStream, meta.Constants.GroupConstants.HeaderLength); locatorStream.Position = grupLoc.Value; foreach (var rec in RecordLocator.ParseTopLevelGRUP(locatorStream)) { MajorRecordHeader majorMeta = inputStream.GetMajorRecord(); storage.TryCreateValue(rec.FormID).Add(inputStream.ReadMemory(checked ((int)majorMeta.TotalLength), readSafe: true)); if (grupFrame.Complete) { continue; } GroupHeader subGroupMeta = inputStream.GetGroup(); if (subGroupMeta.IsGroup) { storage.TryCreateValue(rec.FormID).Add(inputStream.ReadMemory(checked ((int)subGroupMeta.TotalLength), readSafe: true)); } } } foreach (var item in storage.OrderBy((i) => i.Key.ID)) { foreach (var bytes in item.Value) { writer.Write(bytes); } } } inputStream.WriteTo(writer.BaseStream, (int)inputStream.Remaining); } }