From 7a07559cde51b916e88fbf552fe0ced2fad331c2 Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 15 Feb 2021 18:17:12 -0800 Subject: [PATCH] Add destination enum for tweaking result behavior --- NHSE.Core/Editing/ItemRequest/ItemParser.cs | 56 +++++++++++++-------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/NHSE.Core/Editing/ItemRequest/ItemParser.cs b/NHSE.Core/Editing/ItemRequest/ItemParser.cs index 99d5e34..cff8408 100644 --- a/NHSE.Core/Editing/ItemRequest/ItemParser.cs +++ b/NHSE.Core/Editing/ItemRequest/ItemParser.cs @@ -28,20 +28,21 @@ public static class ItemParser /// /// 8 byte hex item values (u64 format) /// Options for packaging items - public static IReadOnlyCollection GetItemsFromUserInput(string requestHex, IConfigItem cfg) + /// End destination of the item + public static IReadOnlyCollection GetItemsFromUserInput(string requestHex, IConfigItem cfg, ItemDestination type = ItemDestination.PlayerDropped) { try { // having a language 2char code will cause an exception in parsing; this is fine and is handled by our catch statement. var split = requestHex.Split(SplittersHex, StringSplitOptions.RemoveEmptyEntries); - return GetItemsHexCode(split, cfg); + return GetItemsHexCode(split, cfg, type); } #pragma warning disable CA1031 // Do not catch general exception types catch #pragma warning restore CA1031 // Do not catch general exception types { var split = requestHex.Split(SplittersName, StringSplitOptions.RemoveEmptyEntries); - return GetItemsLanguage(split, cfg, GameLanguage.DefaultLanguage); + return GetItemsLanguage(split, cfg, type, GameLanguage.DefaultLanguage); } } @@ -132,8 +133,9 @@ public static IReadOnlyCollection GetDIYItemsLanguage(IReadOnlyList /// List of item names /// Item packaging options + /// Destination where the item will end up at /// Language code to parse with. If the first entry in is a language code, it will be used instead of . - public static IReadOnlyCollection GetItemsLanguage(IReadOnlyList split, IConfigItem config, string lang = GameLanguage.DefaultLanguage) + public static IReadOnlyCollection GetItemsLanguage(IReadOnlyList split, IConfigItem config, ItemDestination type = ItemDestination.PlayerDropped, string lang = GameLanguage.DefaultLanguage) { if (split.Count > 1 && split[0].Length < 3) { @@ -147,7 +149,7 @@ public static IReadOnlyCollection GetItemsLanguage(IReadOnlyList s for (int i = 0; i < result.Length; i++) { var text = split[i].Trim(); - var item = CreateItem(text, i, config, lang); + var item = CreateItem(text, i, config, type, lang); if (item.ItemId >= strings.Length) throw new Exception($"Item requested is out of expected range ({item.ItemId:X4} > {strings.Length:X4})."); @@ -167,7 +169,8 @@ public static IReadOnlyCollection GetItemsLanguage(IReadOnlyList s /// /// List of recipe IDs as u16 hex /// Item packaging options - public static IReadOnlyCollection GetItemsHexCode(IReadOnlyList split, IConfigItem config) + /// Destination where the item will end up at + public static IReadOnlyCollection GetItemsHexCode(IReadOnlyList split, IConfigItem config, ItemDestination type = ItemDestination.PlayerDropped) { var strings = GameInfo.Strings.itemlistdisplay; var result = new Item[split.Count]; @@ -175,7 +178,7 @@ public static IReadOnlyCollection GetItemsHexCode(IReadOnlyList sp { var text = split[i].Trim(); var convert = GetBytesFromString(text); - var item = CreateItem(convert, i, config); + var item = CreateItem(convert, i, config, type); if (item.ItemId >= strings.Length) throw new Exception($"Item requested is out of expected range ({item.ItemId:X4} > {strings.Length:X4})."); @@ -194,26 +197,22 @@ private static byte[] GetBytesFromString(string text) return BitConverter.GetBytes(value); } - private static Item CreateItem(string name, int requestIndex, IConfigItem config, string lang = "en") + private static Item CreateItem(string name, int requestIndex, IConfigItem config, ItemDestination type, string lang = "en") { var item = GetItem(name, lang); if (item.IsNone) throw new Exception($"Failed to convert item (index {requestIndex}: {name}) for Language {lang}."); - if (!ItemInfo.IsSaneItemForDrop(item)) - throw new Exception($"Unsupported item: (index {requestIndex}: {name})."); - - if (config.WrapAllItems && item.ShouldWrapItem()) - item.SetWrapping(ItemWrapping.WrappingPaper, config.WrappingPaper, true); - - return item; + return FinalizeItem(requestIndex, config, type, item); } - private static Item CreateItem(byte[] convert, int requestIndex, IConfigItem config) + private static Item CreateItem(byte[] convert, int requestIndex, IConfigItem config, ItemDestination type) { Item item; try { + if (convert.Length != Item.SIZE) + throw new Exception(); item = convert.ToClass(); } catch (Exception ex) @@ -221,11 +220,21 @@ private static Item CreateItem(byte[] convert, int requestIndex, IConfigItem con throw new Exception($"Failed to convert item (index {requestIndex}: {ex.Message})."); } - if (!ItemInfo.IsSaneItemForDrop(item) || convert.Length != Item.SIZE) - throw new Exception($"Unsupported item: (index {requestIndex})."); + return FinalizeItem(requestIndex, config, type, item); + } + + private static Item FinalizeItem(int requestIndex, IConfigItem config, ItemDestination type, Item item) + { + if (type == ItemDestination.PlayerDropped) + { + if (!ItemInfo.IsSaneItemForDrop(item)) + throw new Exception($"Unsupported item: (index {requestIndex})."); + if (config.WrapAllItems && item.ShouldWrapItem()) + item.SetWrapping(ItemWrapping.WrappingPaper, config.WrappingPaper, true); + } + + item.IsDropped = type == ItemDestination.FieldItemDropped; - if (config.WrapAllItems && item.ShouldWrapItem()) - item.SetWrapping(ItemWrapping.WrappingPaper, config.WrappingPaper, true); return item; } @@ -342,4 +351,11 @@ public static ushort GetID(string text) return (ushort)value; } } + + public enum ItemDestination + { + PlayerDropped, + FieldItemDropped, + HeldItem, + } }