From db034686a1daf5a148dd467354b2b75a021281cd Mon Sep 17 00:00:00 2001 From: omicron Date: Fri, 27 Oct 2023 01:04:19 +0200 Subject: [PATCH] Add raw_version to the item so we can keep it in the database --- d2warehouse/fileformat.py | 1 + d2warehouse/item.py | 13 +++++++++---- d2warehouse/parser.py | 5 +++-- d2warehouse/schema.sql | 1 + 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/d2warehouse/fileformat.py b/d2warehouse/fileformat.py index 23369bd..e3bfbaf 100644 --- a/d2warehouse/fileformat.py +++ b/d2warehouse/fileformat.py @@ -16,4 +16,5 @@ # Mercator. If not, see . STASH_TAB_MAGIC = b"\x55\xAA\x55\xAA" +STASH_TAB_VERSION = 99 ITEM_DATA_MAGIC = b"JM" diff --git a/d2warehouse/item.py b/d2warehouse/item.py index ceeb998..2835fda 100644 --- a/d2warehouse/item.py +++ b/d2warehouse/item.py @@ -22,7 +22,7 @@ from bitarray import bitarray from bitarray.util import int2ba from dataclasses import dataclass from enum import IntEnum - +from d2warehouse.fileformat import STASH_TAB_VERSION from d2warehouse.db import get_db _data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data") @@ -90,6 +90,7 @@ def txtbits(bits: bitarray) -> str: @dataclass class Item: raw_data: bytes + raw_version: int is_identified: bool is_socketed: bool is_beginner: bool @@ -221,19 +222,20 @@ class Item: cur = db.cursor() cur.execute( """INSERT INTO item (itembase_name, item_name, - set_name, socketed_into, raw_data, is_identified, is_socketed, + set_name, socketed_into, raw_data, raw_version, is_identified, is_socketed, is_beginner, is_simple, is_ethereal, is_personalized, is_runeword, code, uid, lvl, quality, graphic, implicit, low_quality, set_id, unique_id, nameword1, nameword2, runeword_id, personal_name, defense, durability, max_durability, quantity) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, - ?, ?, ?, ?, ?, ?, ?)""", + ?, ?, ?, ?, ?, ?, ?, ?)""", ( lookup_basetype(self.code)["name"], name, set_name, socketed_into, self.raw_data, + self.raw_version, self.is_identified, self.is_socketed, self.is_beginner, @@ -299,7 +301,7 @@ class Item: def load_from_db(id: int) -> "Item": db = get_db() row = db.execute( - """SELECT raw_data, is_identified, is_socketed, + """SELECT raw_data, raw_version, is_identified, is_socketed, is_beginner, is_simple, is_ethereal, is_personalized, is_runeword, code, uid, lvl, quality, graphic, implicit, low_quality, set_id, unique_id, nameword1, nameword2, runeword_id, personal_name, defense, durability, @@ -307,9 +309,12 @@ class Item: FROM item WHERE id = ?""", (id,), ).fetchone() + if row["raw_version"] != STASH_TAB_VERSION: + raise RuntimeError("Can not load item, the raw version is not supported") item = Item( raw_data=row["raw_data"], + raw_version=row["raw_version"], is_identified=bool(row["is_identified"]), is_socketed=bool(row["is_socketed"]), is_beginner=bool(row["is_beginner"]), diff --git a/d2warehouse/parser.py b/d2warehouse/parser.py index 50f95d5..73f972b 100644 --- a/d2warehouse/parser.py +++ b/d2warehouse/parser.py @@ -27,7 +27,7 @@ from d2warehouse.item import ( lookup_stat, ) import d2warehouse.huffman as huffman -from d2warehouse.fileformat import STASH_TAB_MAGIC, ITEM_DATA_MAGIC +from d2warehouse.fileformat import STASH_TAB_MAGIC, STASH_TAB_VERSION, ITEM_DATA_MAGIC class ParseError(RuntimeError): @@ -82,7 +82,7 @@ def parse_stash_tab(data: bytes) -> tuple[bytes, StashTab]: if unknown != 1: ParseError("Unknown stash tab field is not 1") - if version != 99: + if version != STASH_TAB_VERSION: ParseError(f"Unsupported stash tab version ({version} instead of 99)") tab = StashTab() @@ -133,6 +133,7 @@ def parse_item(data: bytes) -> tuple[bytes, Item]: simple_byte_sz = int((sockets_end + 7) / 8) item = Item( data[:simple_byte_sz], + STASH_TAB_VERSION, is_identified, is_socketed, is_beginner, diff --git a/d2warehouse/schema.sql b/d2warehouse/schema.sql index 4340183..68f7686 100644 --- a/d2warehouse/schema.sql +++ b/d2warehouse/schema.sql @@ -14,6 +14,7 @@ CREATE TABLE item ( -- The following fields match the fields of the item object in item.py raw_data BLOB NOT NULL, + raw_version INTEGER NOT NULL, is_identified BOOLEAN NOT NULL, is_socketed BOOLEAN NOT NULL, is_beginner BOOLEAN NOT NULL,