|
| 1 | +using Mutagen.Bethesda; |
| 2 | +using Mutagen.Bethesda.Plugins; |
| 3 | +using Mutagen.Bethesda.Skyrim; |
| 4 | +using Mutagen.Bethesda.Synthesis; |
| 5 | +using Noggog; |
| 6 | +using Synthesis.Util; |
| 7 | +using Synthesis.Util.Quest; |
| 8 | + |
| 9 | +namespace SRCPatcher |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Base class for plugins for ERS, which dynamically add the ERS condition to quest aliases as needed. |
| 13 | + /// |
| 14 | + /// Each specific plugin contains its own rules to determine whether a quest alias (and by extension the quest) should be patched. |
| 15 | + /// </summary> |
| 16 | + /// <param name="mod">The mod to patch</param> |
| 17 | + /// <param name="aliasFilter">Function to determine whether a quest alias should have the ERS condition added to it</param> |
| 18 | + /// <param name="pipeline">Patcher pipeline to patch records</param> |
| 19 | + internal abstract class ERSPlugin( |
| 20 | + ISkyrimModGetter mod, |
| 21 | + Func<IQuestAliasGetter, bool> aliasFilter, |
| 22 | + ConditionalTransformPatcherPipeline<ISkyrimMod, ISkyrimModGetter> pipeline |
| 23 | + ) : IPatcherPlugin<ISkyrimMod, ISkyrimModGetter> |
| 24 | + { |
| 25 | + protected readonly ISkyrimModGetter _mod = mod; |
| 26 | + |
| 27 | + private readonly ConditionalTransformPatcherPipeline< |
| 28 | + ISkyrimMod, |
| 29 | + ISkyrimModGetter |
| 30 | + > _pipeline = pipeline; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The patcher instance to use. |
| 34 | + /// </summary> |
| 35 | + private readonly QuestAliasConditionAdder _patcher = new(SRC_ERS.Condition, aliasFilter); |
| 36 | + |
| 37 | + public void Run(IPatcherState<ISkyrimMod, ISkyrimModGetter> state) |
| 38 | + { |
| 39 | + var newQuests = _mod |
| 40 | + .Quests.Select(quest => |
| 41 | + quest |
| 42 | + .ToLinkGetter() |
| 43 | + .ResolveContext<ISkyrimMod, ISkyrimModGetter, IQuest, IQuestGetter>( |
| 44 | + state.LinkCache |
| 45 | + ) |
| 46 | + ) |
| 47 | + .NotNull(); |
| 48 | + |
| 49 | + _pipeline.Run(_patcher, newQuests); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// ERS Plugin for Missives |
| 55 | + /// </summary> |
| 56 | + internal sealed class MissivesPlugin( |
| 57 | + ISkyrimModGetter mod, |
| 58 | + ConditionalTransformPatcherPipeline<ISkyrimMod, ISkyrimModGetter> pipeline |
| 59 | + ) : ERSPlugin(mod, ShouldHaveCondition, pipeline), IPluginData |
| 60 | + { |
| 61 | + private static readonly ModKey Missives = ModKey.FromNameAndExtension("Missives.esp"); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// The following rules result in the same selection as the existing ERS patch: |
| 65 | + /// |
| 66 | + /// - Location alias type |
| 67 | + /// - Has at least 1 condition |
| 68 | + /// - The alias name is not "OtherHold" (it would probably be fine to leave this out - the extra patched aliases are unlikely to break anything, but aiming for parity here) |
| 69 | + /// </summary> |
| 70 | + /// <param name="alias">The quest alias to evaluate</param> |
| 71 | + /// <returns>True if the quest alias should have the ERS condition added to it</returns> |
| 72 | + private static bool ShouldHaveCondition(IQuestAliasGetter alias) => |
| 73 | + alias.Type == QuestAlias.TypeEnum.Location |
| 74 | + && alias.Conditions.Any() |
| 75 | + && (!alias.Name?.Equals("OtherHold") ?? true); |
| 76 | + |
| 77 | + public static PluginData Data => new(nameof(MissivesPlugin), Missives); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// ERS Plugin for Notice Board |
| 82 | + /// </summary> |
| 83 | + internal sealed class NoticeBoardPlugin( |
| 84 | + ISkyrimModGetter mod, |
| 85 | + ConditionalTransformPatcherPipeline<ISkyrimMod, ISkyrimModGetter> pipeline |
| 86 | + ) : ERSPlugin(mod, ShouldHaveCondition, pipeline), IPluginData |
| 87 | + { |
| 88 | + private static readonly ModKey NoticeBoard = ModKey.FromNameAndExtension( |
| 89 | + "notice board.esp" |
| 90 | + ); |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Notice Board's own location blacklist. Used for alias evaluation. |
| 94 | + /// </summary> |
| 95 | + private static readonly IFormLinkGetter<IFormListGetter> _avoidLocations = FormKey |
| 96 | + .Factory($"02ACAB:{NoticeBoard}") |
| 97 | + .ToLinkGetter<IFormListGetter>(); |
| 98 | + |
| 99 | + private static bool HasAvoidFormList(IConditionGetter cond) => |
| 100 | + cond.Data.Function == Condition.Function.GetInCurrentLocFormList |
| 101 | + && ((IGetInCurrentLocFormListConditionDataGetter)cond.Data).FormList.Link.Equals( |
| 102 | + _avoidLocations |
| 103 | + ); |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// The following rules result in the same selection as the existing ERS patch: |
| 107 | + /// |
| 108 | + /// - Location alias type |
| 109 | + /// - Alias has the "avoid locations" formlist condition (this is a more complex check than the other mods, but other criteria proved to be unreliable) |
| 110 | + /// </summary> |
| 111 | + /// <param name="alias">The quest alias to evaluate</param> |
| 112 | + /// <returns>True if the quest alias should have the ERS condition added to it</returns> |
| 113 | + private static bool ShouldHaveCondition(IQuestAliasGetter alias) => |
| 114 | + alias.Type == QuestAlias.TypeEnum.Location && alias.HasCondition(HasAvoidFormList); |
| 115 | + |
| 116 | + public static PluginData Data => new(nameof(NoticeBoardPlugin), NoticeBoard); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// ERS Plugin for Bounty Hunter |
| 121 | + /// |
| 122 | + /// Note that the few base game quests that BH touches are already handled by the main forwarding patcher |
| 123 | + /// </summary> |
| 124 | + internal sealed class BountyHunterPlugin( |
| 125 | + ISkyrimModGetter mod, |
| 126 | + ConditionalTransformPatcherPipeline<ISkyrimMod, ISkyrimModGetter> pipeline |
| 127 | + ) : ERSPlugin(mod, ShouldHaveCondition, pipeline), IPluginData |
| 128 | + { |
| 129 | + private static readonly ModKey BountyHunter = ModKey.FromNameAndExtension( |
| 130 | + "BountyHunter.esp" |
| 131 | + ); |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// The following rules result in the same selection as the existing ERS patch: |
| 135 | + /// - Location alias type |
| 136 | + /// - Has at least 1 condition |
| 137 | + /// </summary> |
| 138 | + /// <param name="alias">The quest alias to evaluate</param> |
| 139 | + /// <returns>True if the quest alias should have the ERS condition added to it</returns> |
| 140 | + private static bool ShouldHaveCondition(IQuestAliasGetter alias) => |
| 141 | + alias.Type == QuestAlias.TypeEnum.Location && alias.Conditions.Any(); |
| 142 | + |
| 143 | + public static PluginData Data => new(nameof(BountyHunterPlugin), BountyHunter); |
| 144 | + } |
| 145 | +} |
0 commit comments