import { parseSimple } from "./parseSimple" ; import { binaryStream } from "../../save-file/binary" ; import { parseQuality } from "./parseQuality" ; import { parseQuantified } from "./parseQuantified" ; import { parseModifiers } from "./parseModifiers" ; import { ItemParsingError } from "../../errors/ItemParsingError" ; import { SaveFileReader } from "../../save-file/SaveFileReader" ; import { LAST_LEGACY } from "../../character/parsing/versions" ; import { ItemsOwner } from "../../save-file/ownership" ; export function parseItem ( reader , owner ) { const stream = binaryStream ( reader ) ; if ( owner . version <= LAST_LEGACY ) { const header = String . fromCharCode ( stream . readInt ( 8 ) , stream . readInt ( 8 ) ) ; if ( header !== "JM" ) { throw new Error ( `Unexpected header ${ header } for an item` ) ; } } const item = parseSimple ( stream , owner ) ; if ( ! item . simple ) { try { parseQuality ( stream , item ) ; parseQuantified ( stream , item ) ; parseModifiers ( stream , item ) ; } catch ( e ) { if ( e instanceof ItemParsingError ) { throw e ; } throw new ItemParsingError ( item , ( e as Error ) . message ) ; } } item . raw = stream . done ( ) ; return item ; }