|
| 1 | +#include "Fixes.h" |
| 2 | + |
| 3 | +//make weapon enchantments support worn restriction list |
| 4 | +//adapted from: https://github.com/Exit-9B/AmmoEnchanting/blob/main/src/Ext/EnchantConstructMenu.cpp |
| 5 | + |
| 6 | +namespace Fixes::WornRestrictionsForWeapons |
| 7 | +{ |
| 8 | + struct detail |
| 9 | + { |
| 10 | + static bool GetCompatibleRestrictions( |
| 11 | + RE::CraftingSubMenus::EnchantConstructMenu::ItemChangeEntry* a_item, |
| 12 | + RE::CraftingSubMenus::EnchantConstructMenu::EnchantmentEntry* a_effect) |
| 13 | + { |
| 14 | + if (!a_item || !a_item->data || !a_item->data->object || !a_effect || !a_effect->data) { |
| 15 | + return true; |
| 16 | + } |
| 17 | + |
| 18 | + auto wornRestrictionList = a_effect->data->data.wornRestrictions; |
| 19 | + auto keywordForm = a_item->data->object->As<RE::BGSKeywordForm>(); |
| 20 | + |
| 21 | + if (!wornRestrictionList || !keywordForm) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + |
| 25 | + bool valid = false; |
| 26 | + |
| 27 | + keywordForm->ForEachKeyword([&](auto* keyword) { |
| 28 | + if (wornRestrictionList->HasForm(keyword)) { |
| 29 | + valid = true; |
| 30 | + return RE::BSContainer::ForEachResult::kStop; |
| 31 | + } |
| 32 | + return RE::BSContainer::ForEachResult::kContinue; |
| 33 | + }); |
| 34 | + |
| 35 | + return valid; |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + struct CanSelectEnchantmentEntry |
| 40 | + { |
| 41 | + static bool func(RE::CraftingSubMenus::EnchantConstructMenu* a_this, std::uint32_t a_index, bool a_showNotification) |
| 42 | + { |
| 43 | + if (a_index >= a_this->listEntries.size()) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + using FilterFlag = RE::CraftingSubMenus::EnchantConstructMenu::FilterFlag; |
| 48 | + |
| 49 | + bool compatibleRestrictions = true; |
| 50 | + auto& entry = a_this->listEntries[a_index]; |
| 51 | + |
| 52 | + switch (entry->filterFlag.get()) { |
| 53 | + case FilterFlag::EnchantArmor: |
| 54 | + case FilterFlag::EnchantWeapon: |
| 55 | + { |
| 56 | + for (auto& effect : a_this->selected.effects) { |
| 57 | + const auto item = static_cast<RE::CraftingSubMenus::EnchantConstructMenu::ItemChangeEntry*>(entry.get()); |
| 58 | + compatibleRestrictions &= detail::GetCompatibleRestrictions(item, effect.get()); |
| 59 | + } |
| 60 | + } |
| 61 | + break; |
| 62 | + case FilterFlag::EffectWeapon: |
| 63 | + case FilterFlag::EffectArmor: |
| 64 | + { |
| 65 | + const auto item = a_this->selected.item.get(); |
| 66 | + const auto effect = static_cast<RE::CraftingSubMenus::EnchantConstructMenu::EnchantmentEntry*>(entry.get()); |
| 67 | + compatibleRestrictions = detail::GetCompatibleRestrictions(item, effect); |
| 68 | + } |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + if (!compatibleRestrictions) { |
| 73 | + if (a_showNotification) { |
| 74 | + static const auto setting = RE::GameSettingCollection::GetSingleton()->GetSetting("sEnchantArmorIncompatible"); |
| 75 | + if (setting) { |
| 76 | + RE::DebugNotification(setting->GetString()); |
| 77 | + } |
| 78 | + } |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + switch (entry->filterFlag.get()) { |
| 83 | + case FilterFlag::EnchantWeapon: |
| 84 | + { |
| 85 | + if (!a_this->selected.effects.empty()) { |
| 86 | + return a_this->selected.effects[0]->filterFlag == FilterFlag::EffectWeapon; |
| 87 | + } |
| 88 | + } |
| 89 | + break; |
| 90 | + case FilterFlag::EnchantArmor: |
| 91 | + { |
| 92 | + if (!a_this->selected.effects.empty()) { |
| 93 | + return a_this->selected.effects[0]->filterFlag == FilterFlag::EffectArmor; |
| 94 | + } |
| 95 | + } |
| 96 | + break; |
| 97 | + case FilterFlag::EffectWeapon: |
| 98 | + { |
| 99 | + if (a_this->selected.item) { |
| 100 | + return a_this->selected.item->filterFlag == FilterFlag::EnchantWeapon; |
| 101 | + } |
| 102 | + } |
| 103 | + break; |
| 104 | + case FilterFlag::EffectArmor: |
| 105 | + { |
| 106 | + if (a_this->selected.item) { |
| 107 | + return a_this->selected.item->filterFlag == FilterFlag::EnchantArmor; |
| 108 | + } |
| 109 | + } |
| 110 | + break; |
| 111 | + default: |
| 112 | + break; |
| 113 | + } |
| 114 | + |
| 115 | + return true; |
| 116 | + } |
| 117 | + static inline constexpr std::size_t size{ OFFSET(0x27D, 0x1E9) }; |
| 118 | + }; |
| 119 | + |
| 120 | + void Install() |
| 121 | + { |
| 122 | + REL::Relocation<std::uintptr_t> func{ REL_ID(50569, 51461) }; |
| 123 | + stl::asm_replace<CanSelectEnchantmentEntry>(func.address()); |
| 124 | + |
| 125 | + logger::info("\t\tInstalled worn restrictions for weapons patch"sv); |
| 126 | + } |
| 127 | +} |
0 commit comments