Refactored items to proper classes

This commit is contained in:
Hash Borgir
2022-06-26 13:41:06 -06:00
parent eb3bc1cb7d
commit 6c4feed124
5 changed files with 215 additions and 60 deletions

View File

@@ -5,6 +5,7 @@ require_once 'D2Files.php';
require_once 'D2BitReader.php';
require_once 'D2Strings.php';
require_once 'D2Item.php';
require_once 'D2ByteReader.php';
class D2Char {
@@ -31,32 +32,25 @@ class D2Char {
}
public function parseItems() {
$_data = file_get_contents($this->filePath);
// get offset of first JM and skip it
$_offset = strpos($_data, "JM") + 2;
// seek to items_total offset
fseek($this->fp, $_offset);
// item total is a SHORT 16 bits, 2 bytes
$_total = unpack('S*', (fread($this->fp, 2)))[1];
$data = file_get_contents($this->filePath);
$ByteReader = new D2ByteReader($data);
$i_TotalOffset = strpos($data, "JM");
fseek($this->fp, $i_TotalOffset + 2);
$i_Total = unpack('S*', (fread($this->fp, 2)))[1];
// Items start from 2nd JM
for ($i = 2; $i <= $_total; $i++) {
// seek to Each JM (every item begins with JM
fseek($this->fp, strposX($_data, 'JM', $i));
// read and unpack 21 bytes as array of byte/char (8 bit each)
$_items[] = unpack('C*', fread($this->fp, 21));
$i_Offsets = [];
for ($i = 0; $i <= $i_Total; $i++) {
$i_Offsets[] = strposX($data, "JM", $i + 2);
}
// Convert item bytes to 21x8 bitfield
// each byte is reversed with strrev() for correct bit position
// to convert back to ascii, read bits from any position, reverse, bindec(chr())
foreach ($_items as $_item) {
$item = null;
foreach ($_item as $i_bytes) {
$item .= strrev(str_pad(decbin($i_bytes), 8, 0, STR_PAD_LEFT));
// $item .= (str_pad(dechex($i_bytes), 2, 0, STR_PAD_LEFT));
}
$this->items[] = new D2Item($item); // return an array of item details
foreach ($i_Offsets as $k => $v) {
$itemOffsets[$v] = $i_Offsets[$k + 1] - $i_Offsets[$k];
}
array_pop($itemOffsets);
$_items=[];
foreach ($itemOffsets as $offset => $bytes) {
$this->items[] = new D2Item($ByteReader->toBits($ByteReader->readh($offset, $bytes)));
}
}
public function parseChar() {