Skip to content

Commit

Permalink
Sort MorphBlends
Browse files Browse the repository at this point in the history
  • Loading branch information
Noggog committed Oct 11, 2024
1 parent a567d11 commit 35ebb11
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Spriggit.CLI.Lib/Commands/Sort/SortStarfield.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public bool HasWorkToDo(
.WithFileSystem(_fileSystem)
.Construct();
if (VirtualMachineAdapterHasWorkToDo(mod)) return true;
if (MorphGroupHasWorkToDo(mod)) return true;
if (RaceMorphGroupHasWorkToDo(mod)) return true;
if (NpcsHaveWorkToDo(mod)) return true;

return false;
}

private bool MorphGroupHasWorkToDo(IStarfieldModDisposableGetter mod)
private bool RaceMorphGroupHasWorkToDo(IStarfieldModDisposableGetter mod)
{
foreach (var race in mod
.EnumerateMajorRecords<IRaceGetter>()
Expand All @@ -42,13 +43,20 @@ private bool MorphGroupHasWorkToDo(IStarfieldModDisposableGetter mod)
if (ChargenHasWorkToDo(charGen?.Male)) return true;
if (ChargenHasWorkToDo(charGen?.Female)) return true;
}


return false;
}

private bool NpcsHaveWorkToDo(IStarfieldModDisposableGetter mod)
{
foreach (var npc in mod
.EnumerateMajorRecords<INpcGetter>()
.AsParallel())
{
var names = npc.FaceMorphs.SelectMany(x => x.MorphGroups).Select(x => x.MorphGroup).ToArray();
if (!names.SequenceEqual(names.OrderBy(x => x))) return true;
var morphGroupNames = npc.FaceMorphs.SelectMany(x => x.MorphGroups).Select(x => x.MorphGroup).ToArray();
if (!morphGroupNames.SequenceEqual(morphGroupNames.OrderBy(x => x))) return true;
var morphBlendNames = npc.MorphBlends.Select(x => x.BlendName).ToArray();
if (!morphBlendNames.SequenceEqual(morphBlendNames.OrderBy(x => x))) return true;
}

return false;
Expand Down Expand Up @@ -152,6 +160,7 @@ public async Task Run(
.Construct();
SortVirtualMachineAdapter(mod);
SortMorphGroups(mod);
SortMorphBlends(mod);

foreach (var maj in mod.EnumerateMajorRecords())
{
Expand Down Expand Up @@ -250,6 +259,17 @@ private void SortMorphGroups(IStarfieldMod mod)
}
}

private void SortMorphBlends(IStarfieldMod mod)
{
foreach (var npc in mod
.EnumerateMajorRecords<INpc>()
.AsParallel())
{
npc.MorphBlends.SetTo(
npc.MorphBlends.ToArray().OrderBy(x => x.BlendName));
}
}

private void SortNpcFaceMorphs(INpcFaceMorph npcFaceMorph)
{
npcFaceMorph.MorphGroups.SetTo(
Expand Down
49 changes: 49 additions & 0 deletions Spriggit.Tests/SortTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO.Abstractions;
using DynamicData;
using FluentAssertions;
using Mutagen.Bethesda;
using Mutagen.Bethesda.Fallout4;
Expand Down Expand Up @@ -286,6 +287,54 @@ await mod.BeginWrite
.Equal("Abc", "Xyz");
}

[Theory, MutagenModAutoData(GameRelease.Starfield)]
public async Task StarfieldMorphBlends(
IFileSystem fileSystem,
DirectoryPath existingDir,
DirectoryPath existingDir2,
StarfieldMod mod,
Mutagen.Bethesda.Starfield.Npc npc,
SortStarfield sort)
{
npc.MorphBlends.Add(new []
{
new NpcMorphBlend()
{
BlendName = "Xyz"
},
new NpcMorphBlend()
{
BlendName = "Abc"
},
});

var modPath = Path.Combine(existingDir, mod.ModKey.FileName);

await mod.BeginWrite
.ToPath(modPath)
.WithLoadOrderFromHeaderMasters()
.WithNoDataFolder()
.WithFileSystem(fileSystem)
.WriteAsync();

var modPath2 = Path.Combine(existingDir2, mod.ModKey.FileName);

sort.HasWorkToDo(modPath, GameRelease.Starfield, null)
.Should().BeTrue();
await sort.Run(modPath, GameRelease.Starfield, modPath2, null);

using var reimport = StarfieldMod.Create(StarfieldRelease.Starfield)
.FromPath(modPath2)
.WithLoadOrderFromHeaderMasters()
.WithNoDataFolder()
.WithFileSystem(fileSystem)
.Construct();
reimport.Npcs.Records.SelectMany(x => x.MorphBlends)
.Select(x => x.BlendName)
.Should()
.Equal("Abc", "Xyz");
}

[Theory, MutagenModAutoData(GameRelease.Starfield)]
public async Task SpecificStarfieldTest(
IFileSystem fileSystem,
Expand Down

0 comments on commit 35ebb11

Please sign in to comment.