From 9532b3be50f55abf3c5df895cc784e9b2e56a72a Mon Sep 17 00:00:00 2001 From: erri120 Date: Sun, 17 May 2020 16:28:31 +0200 Subject: [PATCH] Updated Enchantment extensions --- Plugin.cs | 2 +- UtilityLibrary.cs | 15 ++++ UtilityLibrary.csproj | 4 + UtilityLibrary/ItemEntryExtensions.cs | 114 ++++++++++---------------- 4 files changed, 62 insertions(+), 73 deletions(-) diff --git a/Plugin.cs b/Plugin.cs index 75635b1..4aba54b 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -22,7 +22,7 @@ protected override bool Initialize(bool loadedAny) Events.OnMainMenu.Register(e => { UtilityLibrary.IsInMainMenu = e.Entering; - }); + }, -1); return true; } diff --git a/UtilityLibrary.cs b/UtilityLibrary.cs index cd62cd4..9aeac4e 100644 --- a/UtilityLibrary.cs +++ b/UtilityLibrary.cs @@ -15,5 +15,20 @@ public static partial class UtilityLibrary /// the game is not paused. /// public static bool IsInGame => !IsInMainMenu && Main.Instance != null && !Main.Instance.IsGamePaused; + + public static bool TryGetFormFromFile(uint formID, string fileName, out T value) + { + value = default; + + var form = TESForm.LookupFormFromFile(formID, fileName); + if (form == null) + return false; + + if (!(form is T tForm)) return false; + + value = tForm; + return true; + + } } } diff --git a/UtilityLibrary.csproj b/UtilityLibrary.csproj index f20e86c..e0849ff 100644 --- a/UtilityLibrary.csproj +++ b/UtilityLibrary.csproj @@ -82,4 +82,8 @@ + + copy "$(TargetDir)$(TargetName)$(TargetExt)" "N:\MO2 for Mod Creation\mods\Utility Library\NetScriptFramework\Plugins" +copy "$(TargetDir)$(TargetName).pdb" "N:\MO2 for Mod Creation\mods\Utility Library\NetScriptFramework\Plugins" + \ No newline at end of file diff --git a/UtilityLibrary/ItemEntryExtensions.cs b/UtilityLibrary/ItemEntryExtensions.cs index e0877f0..94f081e 100644 --- a/UtilityLibrary/ItemEntryExtensions.cs +++ b/UtilityLibrary/ItemEntryExtensions.cs @@ -9,24 +9,52 @@ namespace UtilityLibrary public static partial class UtilityLibrary { /// - /// Tries to get the on an armor. + /// Tries to get the base enchantment of an item. The base enchantment is different + /// from an extra enchantment in the way that the extra enchantment is what you apply + /// when enchanting the item using the enchantment table and the base enchantment being + /// the enchantment set in the actual form. /// - /// The armor - /// The of the armor or null if returned false - /// True if getting the armor enchantment was successful, false if not - public static bool TryGetArmorEnchantment([NotNull] this ExtraContainerChanges.ItemEntry itemEntry, + /// The item + /// The pointer to the base enchantment if return true, else + /// True if getting the base enchantment was successful, false if not + public static bool TryGetBaseEnchantment([NotNull] this ExtraContainerChanges.ItemEntry itemEntry, out IntPtr enchantment) + { + enchantment = IntPtr.Zero; + var item = itemEntry.Template; + if (item == null) + return false; + + var formPtr = item.Cast(); + if (formPtr == IntPtr.Zero) + return false; + + var baseEnchantmentPtr = Memory.InvokeCdecl(AddressLibrary.GetEnchantmentFunc, formPtr); + enchantment = baseEnchantmentPtr; + return baseEnchantmentPtr != IntPtr.Zero; + } + + /// + /// Tries to get the extra enchantment of an item. The extra enchantment is different + /// from a base enchantment in the way that the extra enchantment is what you apply + /// when enchanting the item using the enchantment table and the base enchantment being + /// the enchantment set in the actual form. + /// + /// The item + /// The of an item if return true + /// True if getting the extra enchantment as successful, false if not + public static bool TryGetExtraEnchantment([NotNull] this ExtraContainerChanges.ItemEntry itemEntry, out ExtraEnchantment enchantment) { enchantment = null; var item = itemEntry.Template; + if (item == null) + return false; BSSimpleList extraData = itemEntry.ExtraData; if (extraData == null) return false; - if (item.FormType != FormTypes.Armor) return false; - ExtraEnchantment extraEnchantment = null; extraData.Where(x => x != null).Do(x => @@ -48,60 +76,6 @@ public static partial class UtilityLibrary return extraEnchantment != null; } - /// - /// Tries to get the enchantment of a weapon. This returns an - /// to the enchantment and not a . - /// - /// The Weapon - /// The of the enchantment if returned true - /// True if getting the weapon enchantment was successful, false if not - public static bool TryGetWeaponEnchantment([NotNull] this ExtraContainerChanges.ItemEntry itemEntry, - out IntPtr enchantment) - { - enchantment = IntPtr.Zero; - var item = itemEntry.Template; - - BSSimpleList extraData = itemEntry.ExtraData; - if (extraData == null) - return false; - - if (item.FormType != FormTypes.Weapon) return false; - - var enchantmentPtr = IntPtr.Zero; - - extraData.Where(x => x != null).Do(x => - { - var ptr = x.Cast(); - if (ptr == IntPtr.Zero) - return; - - var formPtr = item.Cast(); - if (formPtr == IntPtr.Zero) - return; - - enchantmentPtr = Memory.InvokeCdecl(AddressLibrary.GetEnchantmentFunc, formPtr, ptr); - }); - - enchantment = enchantmentPtr; - return enchantmentPtr != IntPtr.Zero; - } - - /// - /// Checks if an is enchanted or not. - /// Only works for items of form type and . - /// - /// The Item - /// true if enchanted, false if not - public static bool IsEnchanted([NotNull] this ExtraContainerChanges.ItemEntry itemEntry) - { - var item = itemEntry.Template; - - if (item.FormType == FormTypes.Armor) - return itemEntry.TryGetArmorEnchantment(out _); - - return item.FormType == FormTypes.Weapon && itemEntry.TryGetWeaponEnchantment(out _); - } - /// /// Checks if the item has the given enchantment. Only works with weapons and /// armors. @@ -116,21 +90,17 @@ public static bool IsEnchanted([NotNull] this ExtraContainerChanges.ItemEntry it if (item == null) return false; - if (item.FormType == FormTypes.Armor) + if (itemEntry.TryGetBaseEnchantment(out var baseEnchantment)) { - if (!itemEntry.TryGetArmorEnchantment(out var armorEnchantment)) - return false; - - return armorEnchantment.Enchantment == enchantment; + return enchantment.Address == baseEnchantment; } - if (item.FormType != FormTypes.Weapon) return false; - - if (!itemEntry.TryGetWeaponEnchantment(out var weaponEnchantment)) - return false; - - return weaponEnchantment == enchantment.Address; + if (itemEntry.TryGetExtraEnchantment(out var extraEnchantment)) + { + return extraEnchantment.Enchantment == enchantment; + } + return false; } } }