From 40426548565b748d24399201a827f62231bdb0aa Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Mon, 9 Sep 2024 17:37:13 +0200 Subject: [PATCH] refactor(wonderking): item reader cleanup --- Wonderking/Game/Reader/ItemReader.cs | 185 ++++++++++++++++----------- 1 file changed, 111 insertions(+), 74 deletions(-) diff --git a/Wonderking/Game/Reader/ItemReader.cs b/Wonderking/Game/Reader/ItemReader.cs index 88eef50..e9aaf8b 100644 --- a/Wonderking/Game/Reader/ItemReader.cs +++ b/Wonderking/Game/Reader/ItemReader.cs @@ -1,6 +1,7 @@ // Licensed to Timothy Schenk under the GNU AGPL Version 3 License. using System.Buffers.Binary; +using System.Runtime.CompilerServices; using System.Text; using Wonderking.Game.Data; using Wonderking.Game.Data.Item; @@ -46,34 +47,8 @@ public class ItemReader(string path) : DataReader(path) item.Unknown8 = data.Slice(92, 4).ToArray(); // 92 -> 96 item.R2C = data.Slice(96, 16).ToArray(); // 96 -> 112 item.Unknown9 = data.Slice(112, 4).ToArray(); // 112 -> 116 - item.Stats = new Stats - { - Strength = BitConverter.ToInt32(data.Slice(116, 4)), // 116 -> 120 - Dexterity = BitConverter.ToInt32(data.Slice(120, 4)), // 120 -> 124 - Intelligence = BitConverter.ToInt32(data.Slice(124, 4)), // 124 -> 128 - Vitality = BitConverter.ToInt32(data.Slice(128, 4)), // 128 -> 132 - Luck = BitConverter.ToInt32(data.Slice(132, 4)), // 132 -> 136 - Wisdom = BitConverter.ToInt32(data.Slice(136, 4)) // 136 -> 140 - }; // 116 -> 140 - item.ElementalStats = new ElementalStats - { - MinimumFireDamage = BitConverter.ToInt32(data.Slice(140, 4)), // 140 -> 144 - MinimumWaterDamage = BitConverter.ToInt32(data.Slice(144, 4)), // 144 -> 148 - MinimumDarkDamage = BitConverter.ToInt32(data.Slice(148, 4)), // 148 -> 152 - MinimumHolyDamage = BitConverter.ToInt32(data.Slice(152, 4)), // 152 -> 156 - MaximumFireDamage = BitConverter.ToInt32(data.Slice(156, 4)), // 156 -> 160 - MaximumWaterDamage = BitConverter.ToInt32(data.Slice(160, 4)), // 160 -> 164 - MaximumDarkDamage = BitConverter.ToInt32(data.Slice(164, 4)), // 164 -> 168 - MaximumHolyDamage = BitConverter.ToInt32(data.Slice(168, 4)), // 168 -> 172 - ElementFire = BitConverter.ToUInt32(data.Slice(172, 4)), // 172 -> 176 - ElementWater = BitConverter.ToUInt32(data.Slice(176, 4)), // 176 -> 180 - ElementDark = BitConverter.ToUInt32(data.Slice(180, 4)), // 180 -> 184 - ElementHoly = BitConverter.ToUInt32(data.Slice(184, 4)), // 184 -> 188 - FireResistance = BitConverter.ToInt32(data.Slice(188, 4)), // 188 -> 192 - WaterResistance = BitConverter.ToInt32(data.Slice(192, 4)), // 192 -> 196 - DarkResistance = BitConverter.ToInt32(data.Slice(196, 4)), // 196 -> 200 - HolyResistance = BitConverter.ToInt32(data.Slice(200, 4)) // 200 -> 204 - }; // 140 -> 204 + item.Stats = ReadStats(ref data); // 116 -> 140 + item.ElementalStats = ReadElementalStats(ref data); // 140 -> 204 item.R7C = data.Slice(204, 4).ToArray(); // 204 -> 208 item.R8C = data.Slice(208, 8).ToArray(); // 208 -> 216 item.Speed = BinaryPrimitives.ReadSingleLittleEndian(data.Slice(216, 4)); // 216 -> 220 @@ -101,29 +76,7 @@ public class ItemReader(string path) : DataReader(path) item.MinimalAttackDamage = BitConverter.ToInt32(data.Slice(308, 4)); // 308 -> 312 item.MaximalAttackDamage = BitConverter.ToInt32(data.Slice(312, 4)); // 312 -> 316 item.PhysicalDamage = BitConverter.ToInt32(data.Slice(316, 4)); // 316 -> 320 - item.CraftMaterial = new CraftMaterial[] - { - new() - { - ID = BitConverter.ToUInt32(data.Slice(320, 4)), // 320 -> 324 - Amount = BitConverter.ToUInt32(data.Slice(336, 4)) // 336 -> 340 - }, - new() - { - ID = BitConverter.ToUInt32(data.Slice(324, 4)), // 324 -> 328 - Amount = BitConverter.ToUInt32(data.Slice(340, 4)) // 340 -> 344 - }, - new() - { - ID = BitConverter.ToUInt32(data.Slice(328, 4)), // 328 -> 332 - Amount = BitConverter.ToUInt32(data.Slice(344, 4)) // 344 -> 348 - }, - new() - { - ID = BitConverter.ToUInt32(data.Slice(332, 4)), // 332 -> 336 - Amount = BitConverter.ToUInt32(data.Slice(348, 4)) // 348 -> 352 - } - }; // 320 -> 352 + item.CraftMaterial = ReadCraftMaterial(ref data); // 320 -> 352 item.CraftResultAmount = BitConverter.ToUInt32(data.Slice(352, 4)); // 352 -> 356 item.R14C = data.Slice(356, 4).ToArray(); // 356 -> 360 item.CraftResultItem = BitConverter.ToUInt32(data.Slice(360, 4)); // 360 -> 364 @@ -139,33 +92,68 @@ public class ItemReader(string path) : DataReader(path) item.Unknown1 = data.Slice(513, 175).ToArray(); // 513 -> 688 item.IsEnchantable = BitConverter.ToBoolean(data.Slice(688, 4)); // 688 -> 672 item.Unknown1_2 = data.Slice(692, 104).ToArray(); // 692 -> 796 - item.SetItems = new[] - { - BitConverter.ToUInt32(data.Slice(796, 4)), // 796 -> 800 - BitConverter.ToUInt32(data.Slice(800, 4)), // 800 -> 804 - BitConverter.ToUInt32(data.Slice(804, 4)), // 804 -> 808 - BitConverter.ToUInt32(data.Slice(808, 4)), // 808 -> 812 - BitConverter.ToUInt32(data.Slice(812, 4)) // 812 -> 816 - }; // 796 -> 816 + item.SetItems = ReadSetItems(ref data); // 796 -> 816 item.SetID = BitConverter.ToUInt32(data.Slice(816, 4)); // 816 -> 820 - item.Options = new ItemOptions - { - OptionIDs = new[] - { - BitConverter.ToUInt32(data.Slice(824, 4)), // 824 -> 828 - BitConverter.ToUInt32(data.Slice(828, 4)), // 828 -> 832 - BitConverter.ToUInt32(data.Slice(832, 4)), // 832 -> 836 - BitConverter.ToUInt32(data.Slice(836, 4)) // 836 -> 840 - }, - OptionAvailable = BitConverter.ToBoolean(data.Slice(820, 4)) // 820 -> 824 - }; // 820 -> 840 + item.Options = ReadItemOptions(ref data); // 820 -> 840 item.Unknown19 = data.Slice(840, 23).ToArray(); // 840 -> 863 item.PetID = data[863]; // 863 -> 864 item.Unknown20 = data.Slice(864, 20).ToArray(); // 864 -> 884 item.HitBoxScaling = data[884]; // 884 -> 885 item.Unknown20_2 = data.Slice(885, 13).ToArray(); // 885 -> 898 - item.ContainedItems = new[] + item.ContainedItems = ReadContainedItems(ref data); // 898 -> 928 + item.MinimumLevelRequirement = data[928]; // 928 -> 929 + item.Unknown21_2 = data.Slice(929, 3).ToArray(); // 929 -> 932 + return item; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Stats ReadStats(ref Span data) + { + return new Stats { + Strength = BitConverter.ToInt32(data.Slice(116, 4)), // 116 -> 120 + Dexterity = BitConverter.ToInt32(data.Slice(120, 4)), // 120 -> 124 + Intelligence = BitConverter.ToInt32(data.Slice(124, 4)), // 124 -> 128 + Vitality = BitConverter.ToInt32(data.Slice(128, 4)), // 128 -> 132 + Luck = BitConverter.ToInt32(data.Slice(132, 4)), // 132 -> 136 + Wisdom = BitConverter.ToInt32(data.Slice(136, 4)) // 136 -> 140 + }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint[] ReadSetItems(ref Span data) + { + return + [ + BitConverter.ToUInt32(data.Slice(796, 4)), // 796 -> 800 + BitConverter.ToUInt32(data.Slice(800, 4)), // 800 -> 804 + BitConverter.ToUInt32(data.Slice(804, 4)), // 804 -> 808 + BitConverter.ToUInt32(data.Slice(808, 4)), // 808 -> 812 + BitConverter.ToUInt32(data.Slice(812, 4)) // 812 -> 816 + ]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ItemOptions ReadItemOptions(ref Span data) + { + return new ItemOptions + { + OptionAvailable = BitConverter.ToBoolean(data.Slice(820, 4)), // 820 -> 824 + OptionIDs = + [ + BitConverter.ToUInt32(data.Slice(824, 4)), // 824 -> 828 + BitConverter.ToUInt32(data.Slice(828, 4)), // 828 -> 832 + BitConverter.ToUInt32(data.Slice(832, 4)), // 832 -> 836 + BitConverter.ToUInt32(data.Slice(836, 4)) // 836 -> 840 + ] + }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ContainedItem[] ReadContainedItems(ref Span data) + { + return + [ new ContainedItem { ID = BitConverter.ToInt16(data.Slice(898, 2)), // 898 -> 900 @@ -191,9 +179,58 @@ public class ItemReader(string path) : DataReader(path) ID = BitConverter.ToInt16(data.Slice(906, 2)), // 906 -> 908 ObtainChance = BitConverter.ToSingle(data.Slice(924, 4)) // 924 -> 928 } + ]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static CraftMaterial[] ReadCraftMaterial(ref Span data) + { + return + [ + new CraftMaterial + { + ID = BitConverter.ToUInt32(data.Slice(320, 4)), // 320 -> 324 + Amount = BitConverter.ToUInt32(data.Slice(336, 4)) // 336 -> 340 + }, + new CraftMaterial + { + ID = BitConverter.ToUInt32(data.Slice(324, 4)), // 324 -> 328 + Amount = BitConverter.ToUInt32(data.Slice(340, 4)) // 340 -> 344 + }, + new CraftMaterial + { + ID = BitConverter.ToUInt32(data.Slice(328, 4)), // 328 -> 332 + Amount = BitConverter.ToUInt32(data.Slice(344, 4)) // 344 -> 348 + }, + new CraftMaterial + { + ID = BitConverter.ToUInt32(data.Slice(332, 4)), // 332 -> 336 + Amount = BitConverter.ToUInt32(data.Slice(348, 4)) // 348 -> 352 + } + ]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ElementalStats ReadElementalStats(ref Span data) + { + return new ElementalStats + { + MinimumFireDamage = BitConverter.ToInt32(data.Slice(140, 4)), // 140 -> 144 + MinimumWaterDamage = BitConverter.ToInt32(data.Slice(144, 4)), // 144 -> 148 + MinimumDarkDamage = BitConverter.ToInt32(data.Slice(148, 4)), // 148 -> 152 + MinimumHolyDamage = BitConverter.ToInt32(data.Slice(152, 4)), // 152 -> 156 + MaximumFireDamage = BitConverter.ToInt32(data.Slice(156, 4)), // 156 -> 160 + MaximumWaterDamage = BitConverter.ToInt32(data.Slice(160, 4)), // 160 -> 164 + MaximumDarkDamage = BitConverter.ToInt32(data.Slice(164, 4)), // 164 -> 168 + MaximumHolyDamage = BitConverter.ToInt32(data.Slice(168, 4)), // 168 -> 172 + ElementFire = BitConverter.ToUInt32(data.Slice(172, 4)), // 172 -> 176 + ElementWater = BitConverter.ToUInt32(data.Slice(176, 4)), // 176 -> 180 + ElementDark = BitConverter.ToUInt32(data.Slice(180, 4)), // 180 -> 184 + ElementHoly = BitConverter.ToUInt32(data.Slice(184, 4)), // 184 -> 188 + FireResistance = BitConverter.ToInt32(data.Slice(188, 4)), // 188 -> 192 + WaterResistance = BitConverter.ToInt32(data.Slice(192, 4)), // 192 -> 196 + DarkResistance = BitConverter.ToInt32(data.Slice(196, 4)), // 196 -> 200 + HolyResistance = BitConverter.ToInt32(data.Slice(200, 4)) // 200 -> 204 }; - item.MinimumLevelRequirement = data[928]; // 928 -> 929 - item.Unknown21_2 = data.Slice(929, 3).ToArray(); // 929 -> 932 - return item; } }