Fix alignemnt bug & set cleanup

This commit is contained in:
2023-10-26 16:50:49 +02:00
parent f8a628a65f
commit 4da8e096fe
2 changed files with 13 additions and 17 deletions

View File

@@ -184,7 +184,7 @@ def parse_item(data: bytes) -> tuple[bytes, Item]:
sockets_end = itembase_end
item, enchantments_end = parse_enchantments(bits[sockets_end:], item)
enchantments_end += itembase_end
enchantments_end += sockets_end
extended_byte_size = int((enchantments_end + 7) / 8)
@@ -294,6 +294,7 @@ def parse_affix(bits: bitarray) -> tuple[int | None, int]:
def parse_runeword(bits: bitarray) -> tuple[int, int]:
id = ba2int(bits[0:12])
# + 4 unknown bits
# FIXME: Could this be the same field that set has? I.e. extra enchantment lists
return id, 16
@@ -342,22 +343,8 @@ def parse_enchantments(bits: bitarray, item: Item) -> tuple[Item, int]:
ptr = 0
if item.quality == Quality.SET:
val = ba2int(bits[0:5])
# credit to https://github.com/nokka/d2s/blob/426ae713940b7474a5f7872f16dddb02ced8a241/item.go#L1139
# for table
table = {
0: 0,
1: 1,
2: 1,
3: 2,
4: 1,
6: 2,
7: 3,
10: 2,
12: 2,
15: 4,
31: 5,
}
num_lists = 1 + table[val]
# FIXME: how exactly this value works is unconfirmed
num_lists = 1 + val.bit_count()
ptr += 5
elif item.is_runeword:
# Runewords have one empty dummy list of only 0x1ff followed by the runeword stats

View File

@@ -205,3 +205,12 @@ class ParseItemTest(unittest.TestCase):
data, item = parse_item(data)
self.assertEqual(data, b"")
self.assertEqual(item.stats[6].id, 140) # Extra Blood special stat
def test_byte_alignment(self):
# Fix for issue involving e.g. Natalya's Shadow
data = bytes.fromhex(
"100880000500148a1be03d81e1ae0510a00f0986001f4cd1229c9b2f5e3100c286fc07"
)
data, _ = parse_item(data)
self.assertEqual(data, b"")