Skip to content

Commit

Permalink
Merge branch 'dev' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
Noggog committed Mar 12, 2021
2 parents b16e2ee + b43fcb0 commit e631289
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,36 @@ public static class DuplicateFromMixIn

// Compile list of things to duplicate
HashSet<FormLinkInformation> identifiedLinks = new();
var enumer = typesToInspect == null || typesToInspect.Length == 0
? modToDuplicateInto.EnumerateMajorRecords()
: typesToInspect.SelectMany(x => modToDuplicateInto.EnumerateMajorRecords(x));
foreach (var rec in enumer)
HashSet<FormKey> passedLinks = new();

void AddAllLinks(FormLinkInformation link)
{
if (rec.FormKey.ModKey == modKeyToDuplicateFrom)
if (link.FormKey.IsNull || !passedLinks.Add(link.FormKey)) return;

if (link.FormKey.ModKey == modKeyToDuplicateFrom)
{
identifiedLinks.Add(link);
}

if (!linkCache.TryResolve(link.FormKey, link.Type, out var linkRec))
{
identifiedLinks.Add(new FormLinkInformation(rec.FormKey, rec.Registration.GetterType));
throw new KeyNotFoundException($"Could not locate record to make self contained: {link.FormKey}");
}

foreach (var containedLink in rec.ContainedFormLinks)
foreach (var containedLink in linkRec.ContainedFormLinks)
{
if (containedLink.FormKey.ModKey == modKeyToDuplicateFrom)
{
identifiedLinks.Add(containedLink);
}
AddAllLinks(containedLink);
}
}

var enumer = typesToInspect == null || typesToInspect.Length == 0
? modToDuplicateInto.EnumerateMajorRecords()
: typesToInspect.SelectMany(x => modToDuplicateInto.EnumerateMajorRecords(x));
foreach (var rec in enumer)
{
AddAllLinks(new FormLinkInformation(rec.FormKey, rec.Registration.GetterType));
}

// Duplicate in the records
mapping = new();
foreach (var identifiedRec in identifiedLinks)
Expand Down
2 changes: 1 addition & 1 deletion Mutagen.Bethesda.Core/Mutagen.Bethesda.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<Compile Include="Exceptions\RecordCollisionException.cs" />
<Compile Include="Exceptions\RecordException.cs" />
<Compile Include="Exceptions\SubrecordException.cs" />
<Compile Include="Extensions\DuplicateOutModMixIn.cs" />
<Compile Include="Extensions\DuplicateFromMixIn.cs" />
<Compile Include="Extensions\EnumerableExt.cs" />
<Compile Include="Extensions\FormLinkListMixIn.cs" />
<Compile Include="Extensions\FormLinkMixIn.cs" />
Expand Down
40 changes: 38 additions & 2 deletions Mutagen.Bethesda.UnitTests/DuplicateFrom_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void TypicalExtraction()
var modToExtract = new SkyrimMod(Utility.PluginModKey, SkyrimRelease.SkyrimSE);
var npc = modToExtract.Npcs.AddNew();
var race = modToExtract.Races.AddNew();
race.CloseLootSound = modToExtract.SoundDescriptors.AddNew();
var unneededRace = modToExtract.Races.AddNew();

var targetMod = new SkyrimMod(Utility.PluginModKey2, SkyrimRelease.SkyrimSE);
Expand All @@ -48,22 +49,55 @@ public void TypicalExtraction()
var linkCache = new MutableLoadOrderLinkCache<ISkyrimMod, ISkyrimModGetter>(modToExtract, targetMod);

targetMod.DuplicateFromOnlyReferenced(linkCache, modToExtract.ModKey, out var mapping);
targetMod.EnumerateMajorRecords().Should().HaveCount(3);
targetMod.Npcs.Should().HaveCount(2);
targetMod.Races.Should().HaveCount(1);
targetMod.SoundDescriptors.Should().HaveCount(1);
targetMod.EnumerateMajorRecords().Should().HaveCount(4);
targetMod.Npcs.Should().Contain(safeNpc);
var newRaceFormKey = mapping[race.FormKey];
var newRace = targetMod.Races.First();
newRace.FormKey.Should().Be(newRaceFormKey);
safeNpc.Race.FormKey.Should().Be(newRaceFormKey);
}

[Fact]
public void DuplicateReference()
{
var modToExtract = new SkyrimMod(Utility.PluginModKey, SkyrimRelease.SkyrimSE);
var npc = modToExtract.Npcs.AddNew();
var race = modToExtract.Races.AddNew();
race.CloseLootSound = modToExtract.SoundDescriptors.AddNew();
var unneededRace = modToExtract.Races.AddNew();

var targetMod = new SkyrimMod(Utility.PluginModKey2, SkyrimRelease.SkyrimSE);
targetMod.Npcs.Add(npc);
var safeNpc = targetMod.Npcs.AddNew();
safeNpc.Race = new FormLink<IRaceGetter>(race.FormKey);
var safeNpc2 = targetMod.Npcs.AddNew();
safeNpc2.Race = new FormLink<IRaceGetter>(race.FormKey);

var linkCache = new MutableLoadOrderLinkCache<ISkyrimMod, ISkyrimModGetter>(modToExtract, targetMod);

targetMod.DuplicateFromOnlyReferenced(linkCache, modToExtract.ModKey, out var mapping);
targetMod.Npcs.Should().HaveCount(3);
targetMod.Races.Should().HaveCount(1);
targetMod.SoundDescriptors.Should().HaveCount(1);
targetMod.EnumerateMajorRecords().Should().HaveCount(5);
targetMod.Npcs.Should().Contain(safeNpc);
targetMod.Npcs.Should().Contain(safeNpc2);
var newRaceFormKey = mapping[race.FormKey];
var newRace = targetMod.Races.First();
newRace.FormKey.Should().Be(newRaceFormKey);
safeNpc.Race.FormKey.Should().Be(newRaceFormKey);
}

[Fact]
public void TypedExtraction()
{
var modToExtract = new SkyrimMod(Utility.PluginModKey, SkyrimRelease.SkyrimSE);
var npc = modToExtract.Npcs.AddNew();
var race = modToExtract.Races.AddNew();
race.CloseLootSound = modToExtract.SoundDescriptors.AddNew();
var unneededRace = modToExtract.Races.AddNew();

var targetMod = new SkyrimMod(Utility.PluginModKey2, SkyrimRelease.SkyrimSE);
Expand All @@ -74,9 +108,10 @@ public void TypedExtraction()
var linkCache = new MutableLoadOrderLinkCache<ISkyrimMod, ISkyrimModGetter>(modToExtract, targetMod);

targetMod.DuplicateFromOnlyReferenced(linkCache, modToExtract.ModKey, out var mapping, typeof(INpcGetter));
targetMod.EnumerateMajorRecords().Should().HaveCount(3);
targetMod.Npcs.Should().HaveCount(2);
targetMod.Races.Should().HaveCount(1);
targetMod.SoundDescriptors.Should().HaveCount(1);
targetMod.EnumerateMajorRecords().Should().HaveCount(4);
targetMod.Npcs.Should().Contain(safeNpc);
var newRaceFormKey = mapping[race.FormKey];
var newRace = targetMod.Races.First();
Expand All @@ -90,6 +125,7 @@ public void MistypedExtraction()
var modToExtract = new SkyrimMod(Utility.PluginModKey, SkyrimRelease.SkyrimSE);
var npc = modToExtract.Npcs.AddNew();
var race = modToExtract.Races.AddNew();
race.CloseLootSound = modToExtract.SoundDescriptors.AddNew();
var unneededRace = modToExtract.Races.AddNew();

var targetMod = new SkyrimMod(Utility.PluginModKey2, SkyrimRelease.SkyrimSE);
Expand Down

0 comments on commit e631289

Please sign in to comment.