Skip to content

Commit

Permalink
Headpart fixes and improvements
Browse files Browse the repository at this point in the history
Fixed a couple potential null reference crashes
Added initialization flag to headpart consistency so patcher doesn't complain about mismatched consistency during first run.
  • Loading branch information
Piranha91 authored and Piranha91 committed Aug 27, 2022
1 parent 2eecee3 commit eda9539
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions SynthEBD/Classes_Aux/Models/HeadPartConsistency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class HeadPartConsistency
public FormKey FormKey { get; set; }
public string EditorID { get; set; }
public bool RandomizedToNone { get; set; } = false;
public bool Initialized { get; set; } = false;
}
}
22 changes: 13 additions & 9 deletions SynthEBD/Patcher/Head Part Patching/HeadPartSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SynthEBD
{
public class HeadPartSelector
{
public static HeadPartSelection AssignHeadParts(NPCInfo npcInfo, Settings_Headparts settings, BlockedNPC blockedNPCentry, BlockedPlugin blockedPluginEntry, BodySlideSetting assignedBodySlide)
public static HeadPartSelection AssignHeadParts(NPCInfo npcInfo, Settings_Headparts settings, BlockedNPC blockedNPCentry, BlockedPlugin blockedPluginEntry, BodySlideSetting? assignedBodySlide)
{
Logger.OpenReportSubsection("HeadParts", npcInfo);
Logger.LogReport("Selecting Head Parts for Current NPC", false, npcInfo);
Expand Down Expand Up @@ -49,11 +49,10 @@ public static HeadPartSelection AssignHeadParts(NPCInfo npcInfo, Settings_Headpa
AllocateHeadPartSelection(selectedFK, headPartType, selectedHeadParts);

// record consistency mismatches
if (currentConsistency != null)
if (currentConsistency != null && currentConsistency.Initialized)
{
var consistencyAssignment = npcInfo.ConsistencyNPCAssignment.HeadParts[headPartType];
var bothNull = consistencyAssignment.FormKey.IsNull && selection == null;
var bothMatch = selection != null && selection.FormKey.Equals(consistencyAssignment.FormKey);
var bothNull = currentConsistency.FormKey.IsNull && selection == null;
var bothMatch = selection != null && selection.FormKey.Equals(currentConsistency.FormKey);
if (!bothNull && !bothMatch)
{
consistencyReportTriggers.Add(headPartType.ToString());
Expand Down Expand Up @@ -81,6 +80,7 @@ public static HeadPartSelection AssignHeadParts(NPCInfo npcInfo, Settings_Headpa
npcInfo.ConsistencyNPCAssignment.HeadParts[headPartType].FormKey = new FormKey();
npcInfo.ConsistencyNPCAssignment.HeadParts[headPartType].RandomizedToNone = false;
}
npcInfo.ConsistencyNPCAssignment.HeadParts[headPartType].Initialized = true;
}

// assign linkage
Expand Down Expand Up @@ -123,7 +123,7 @@ public static void AllocateHeadPartSelection(FormKey? selection, HeadPart.TypeEn
}
}

public static IHeadPartGetter AssignHeadPartType(Settings_HeadPartType currentSettings, HeadPart.TypeEnum type, NPCInfo npcInfo, BodySlideSetting assignedBodySlide, HeadPartConsistency currentConsistency, out bool randomizedToNone)
public static IHeadPartGetter AssignHeadPartType(Settings_HeadPartType currentSettings, HeadPart.TypeEnum type, NPCInfo npcInfo, BodySlideSetting? assignedBodySlide, HeadPartConsistency currentConsistency, out bool randomizedToNone)
{
randomizedToNone = false;
// if there are no head parts of this type at all, don't assign consistency.
Expand Down Expand Up @@ -215,10 +215,14 @@ public static IHeadPartGetter AssignHeadPartType(Settings_HeadPartType currentSe
Logger.LogReport("This NPC's consistency shows it was previously selected to NOT receive a " + type + ". Therefore, one will NOT be assigned unless overriden via a Specific or ForceIf assignment." + type, false, npcInfo);
randomizedToNone = true;
}
else
else if (availableHeadParts.Any())
{
selectedHeadPart = ChooseHeadPart(availableHeadParts, consistencyHeadPart, npcInfo, type, currentSettings.RandomizationPercentage, out randomizedToNone);
}
else
{
Logger.LogReport("No " + type.ToString() + "head parts are available for this NPC", false, npcInfo);
}

return selectedHeadPart;
}
Expand All @@ -245,7 +249,7 @@ public static IHeadPartGetter ChooseHeadPart(IEnumerable<HeadPartSetting> option
}

var selectedAssignment = (HeadPartSetting)ProbabilityWeighting.SelectByProbability(options);
Logger.LogReport("Selected " + type + ": " + (selectedAssignment.EditorID ?? selectedAssignment.HeadPartFormKey.ToString()) + " at random.", false, npcInfo);
Logger.LogReport("Selected " + type + ": " + EditorIDHandler.GetEditorIDSafely(selectedAssignment.ResolvedHeadPart) + " at random.", false, npcInfo);
return selectedAssignment.ResolvedHeadPart;
}
public static bool CanGetThisHeadPartType(Settings_HeadPartType currentSettings, HeadPart.TypeEnum type, NPCInfo npcInfo)
Expand Down Expand Up @@ -373,7 +377,7 @@ public static bool CanGetThisHeadPartType(Settings_HeadPartType currentSettings,
return true;
}

public static bool HeadPartIsValid(HeadPartSetting candidateHeadPart, NPCInfo npcInfo, HeadPart.TypeEnum type, BodySlideSetting assignedBodySlide)
public static bool HeadPartIsValid(HeadPartSetting candidateHeadPart, NPCInfo npcInfo, HeadPart.TypeEnum type, BodySlideSetting? assignedBodySlide)
{
if (npcInfo.SpecificNPCAssignment != null && npcInfo.SpecificNPCAssignment.HeadParts[type].FormKey.Equals(candidateHeadPart.HeadPartFormKey))
{
Expand Down
2 changes: 1 addition & 1 deletion SynthEBD/Patcher/PatcherIO/JContainersDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class JContainersDomain
public static void CreateSynthEBDDomain()
{
string domainPath = Path.Combine(PatcherSettings.General.OutputDataFolder, "SKSE", "Plugins", "JCData", "Domains", "PSM_SynthEBD");
Directory.CreateDirectory(domainPath);
PatcherIO.CreateDirectoryIfNeeded(domainPath, PatcherIO.PathType.Directory);

string domainScriptPath = Path.Combine(PatcherSettings.Paths.ResourcesFolderPath, "JContainers Domain", "PSM_SynthEBD.pex");
string domainScriptDestPath = Path.Combine(PatcherSettings.General.OutputDataFolder, "Scripts", "PSM_SynthEBD.pex");
Expand Down

0 comments on commit eda9539

Please sign in to comment.