import { Item } from "../types/Item" ; import { FIRST_D2R , LAST_LEGACY } from "../../character/parsing/versions" ; import { encodeHuffman } from "../parsing/huffman" ; import { fromInt , fromString } from "../../save-file/binary" ; export const D2R_OFFSET = - 23 ; const D2_ITEM_VERSION_START = 48 ; const D2_ITEM_VERSION_END = D2_ITEM_VERSION_START + 10 ; const D2R_ITEM_VERSION_START = D2_ITEM_VERSION_START - 16 ; const D2R_ITEM_VERSION_END = D2R_ITEM_VERSION_START + 3 ; const D2_ITEM_CODE_START = 76 ; const D2_ITEM_CODE_END = D2_ITEM_CODE_START + 32 ; const D2R_ITEM_CODE_START = D2_ITEM_CODE_START + D2R_OFFSET ; const JM_HEADER = fromString ( "JM" ) ; export function toD2R ( item ) { if ( item . owner . version >= FIRST_D2R ) { return ; } item . raw = item . raw . slice ( 16 , D2_ITEM_VERSION_START ) + item . version + item . raw . slice ( D2_ITEM_VERSION_END , D2_ITEM_CODE_START ) + encodeHuffman ( item . code . padEnd ( 4 , " " ) ) + item . raw . slice ( D2_ITEM_CODE_END ) ; if ( item . filledSockets ) { for ( const socket of item . filledSockets ) { toD2R ( socket ) ; } } } export function toD2 ( item ) { if ( item . owner . version <= LAST_LEGACY ) { return ; } const codeLength = encodeHuffman ( item . code . padEnd ( 4 , " " ) ) . length ; item . raw = JM_HEADER + item . raw . slice ( 0 , D2R_ITEM_VERSION_START ) + fromInt ( Number ( item . version ) , 10 ) + item . raw . slice ( D2R_ITEM_VERSION_END , D2R_ITEM_CODE_START ) + fromString ( item . code . padEnd ( 4 , " " ) ) + item . raw . slice ( D2R_ITEM_CODE_START + codeLength ) ; if ( item . filledSockets ) { for ( const socket of item . filledSockets ) { toD2 ( socket ) ; } } }