From ade44ff42727f1591b14c4324f3cb6b3f3892a3f Mon Sep 17 00:00:00 2001 From: Hash Borgir Date: Mon, 20 Jun 2022 20:56:21 -0600 Subject: [PATCH] D2Char refactored/cleaned up --- src/D2Char.php | 247 ++++++++++++++++++++----------------------------- 1 file changed, 101 insertions(+), 146 deletions(-) diff --git a/src/D2Char.php b/src/D2Char.php index 4569bd9..fcb4103 100644 --- a/src/D2Char.php +++ b/src/D2Char.php @@ -2,212 +2,167 @@ require_once 'D2CharStructureData.php'; require_once 'D2Files.php'; +require_once 'D2BitReader.php'; class D2Char { - public $cData; - private $sData; + public $cData; // char data output + private $sData; // char file structure data + private $bData; // char binary data from d2s + private $filePath; // .d2s file path + private $fp; // file pointer public function __construct($file) { $this->sData = new D2CharStructureData(); - $filePath = $_SESSION['savepath'] . $file; - $fp = fopen($filePath, "rb+"); + $this->filePath = $_SESSION['savepath'] . $file; + $this->fp = fopen($this->filePath, "rb+"); - // read offsets here from sData and put into $data which will be used for cData + // read offsets here from sData and put into $this->bData + // which will be used for cData output foreach ($this->sData->offsets as $k => $v) { - fseek($fp, $k); - $data[$k] = fread($fp, $v); + fseek($this->fp, $k); + $this->bData[$k] = fread($this->fp, $v); } - $cData['Identifier'] = bin2hex($data[0]); - - $cData['VersionID'] = $sData->version[unpack('l', $data[4])[1]]; // 96 is v1.10+ - checks out - - $cData['Filesize'] = round(unpack('l', $data[8])[1] / 1024, 2) . " KB"; // 1.41 KB (1,447 bytes) - checks out - // $cData['Checksum'] = bin2hex($data['12']); - // $cData['Activeweapon'] = unpack('l', $data['16']); - - $cData['CharacterName'] = str_replace("\0", "", $data[20]); - - // ddump(str_replace("\0x00", '', $cData['CharacterName'])); - - - - $cData['CharacterStatus'] = array_filter(str_split(strtobits($data[36]))); + return $this->parseChar(); + } + public function parseChar() { + $cData = null; + $cData['Identifier'] = bin2hex($this->bData[0]); + // 96 is v1.10+ - checks out + $cData['VersionID'] = $sData->version[unpack('l', $this->bData[4])[1]]; + // 1.41 KB (1,447 bytes) - checks out + $cData['Filesize'] = round(unpack('l', $this->bData[8])[1] / 1024, 2) . " KB"; + $cData['Checksum'] = bin2hex($this->bData['12']); + $cData['Activeweapon'] = unpack('l', $this->bData['16']); + $cData['CharacterName'] = str_replace("\0", "", $this->bData[20]); + $cData['CharacterStatus'] = array_filter(str_split(strtobits($this->bData[36]))); foreach ($cData['CharacterStatus'] as $k => $v) { $str .= ($characterStatus[$k]) . " "; } - $cData['CharacterStatus'] = $str; + $cData['Characterprogression'] = bindec($this->bData['37']); + $cData['CharacterClass'] = $this->sData->class[unpack('C', $this->bData[40])[1]]; + $cData['CharacterLevel'] = unpack('C', $this->bData[43])[1]; + $cData['Lastplayed'] = gmdate("Y-m-d\TH:i:s\Z", unpack('I', $this->bData[48])[0]); + $cData['Assignedskills'] = (unpack('i16', $this->bData['56'])); + $cData['LeftmousebuttonskillID'] = $this->sData->skills[unpack('i', $this->bData[120])[1]]; + $cData['RightmousebuttonskillID'] = $this->sData->skills[unpack('i', $this->bData[124])[1]]; + $cData['LeftswapmousebuttonskillID'] = $this->sData->skills[unpack('i', $this->bData[128])[1]]; + $cData['RightswapmousebuttonskillID'] = $this->sData->skills[unpack('i', $this->bData[132])[1]]; + $cData['Charactermenuappearance'] = unpack('i', $this->bData[136]); - // $cData['Characterprogression'] = bindec($data['37']); - - $cData['CharacterClass'] = $this->sData->class[unpack('C', $data[40])[1]]; - - $cData['CharacterLevel'] = unpack('C', $data[43])[1]; - - $cData['Lastplayed'] = gmdate("Y-m-d\TH:i:s\Z", unpack('I', $data[48])[0]); - - // $cData['Assignedskills'] = (unpack('i16', $data['56'])); - - $cData['LeftmousebuttonskillID'] = $this->sData->skills[unpack('i', $data[120])[1]]; - $cData['RightmousebuttonskillID'] = $this->sData->skills[unpack('i', $data[124])[1]]; - $cData['LeftswapmousebuttonskillID'] = $this->sData->skills[unpack('i', $data[128])[1]]; - $cData['RightswapmousebuttonskillID'] = $this->sData->skills[unpack('i', $data[132])[1]]; - - // $cData['Charactermenuappearance'] = unpack('i', $data[136]); - - $x = str_split(strtobits($data[168]), 8); - + $x = str_split(strtobits($this->bData[168]), 8); $onDifficulty['Norm'] = $x[0][0]; $onDifficulty['NM'] = $x[1][0]; $onDifficulty['Hell'] = $x[2][0]; - $cData['Difficulty'] = array_filter($onDifficulty); - - //$cData['MapID'] = $data['171']; - //$cData['Mercenarydead'] = unpack('i', $data['177']); - //$cData['MercenaryID'] = $data['179']; - //$cData['MercenaryNameID'] = $data['183']; - //$cData['Mercenarytype'] = $data['185']; - //$cData['Mercenaryexperience'] = $data['187']; - - + $cData['MapID'] = $this->bData['171']; + $cData['Mercenarydead'] = unpack('i', $this->bData['177']); + $cData['MercenaryID'] = $this->bData['179']; + $cData['MercenaryNameID'] = $this->bData['183']; + $cData['Mercenarytype'] = $this->bData['185']; + $cData['Mercenaryexperience'] = $this->bData['187']; $cData['Quests'][] = $this->getQuestData($file); - $cData['Waypoints'] = $this->getWaypointsData($file); - $cData['NPCIntroductions'] = $data[714]; - - $cData['filePath'] = $filePath; - - + $cData['NPCIntroductions'] = $this->bData[714]; + $cData['filePath'] = $this->filePath; $this->cData = $cData; - - unset($this->sData); return $this->cData; } public function getQuestData($file) { - - - $questsNorm = null; $questsNM = null; $questsHell = null; - $quests = null; - - $filePath = $_SESSION['savepath'] . $file; - $fp = fopen($filePath, "rb+"); - foreach ($this->sData->qNorm as $k => $v) { - fseek($fp, $k); - $questsNorm[$k] = fread($fp, 2); + fseek($this->fp, $k); + $questsNorm[$k] = fread($this->fp, 2); } foreach ($this->sData->qNM as $k => $v) { - fseek($fp, $k); - $questsNM[$k] = fread($fp, 2); + fseek($this->fp, $k); + $questsNM[$k] = fread($this->fp, 2); } - foreach ($this->sData->qHell as $k => $v) { - fseek($fp, $k); - $questsHell[$k] = fread($fp, 2); + fseek($this->fp, $k); + $questsHell[$k] = fread($this->fp, 2); } - - foreach ($questsNorm as $k => $v) { $x = (str_split(strtobits($v), 8)); - // if ($x[0][0]) { - $quests['Norm'][$this->sData->qNorm[$k]] = $x[0][0]; - // } + // if ($x[0][0]) { + $quests['Norm'][$this->sData->qNorm[$k]] = $x[0][0]; + // } } foreach ($questsNM as $k => $v) { $x = array_filter(str_split(strtobits($v), 8)); - // if ($x[0][0]) { - $quests['NM'][$this->sData->qNM[$k]] = $x[0][0]; - // } + // if ($x[0][0]) { + $quests['NM'][$this->sData->qNM[$k]] = $x[0][0]; + // } } foreach ($questsHell as $k => $v) { $x = array_filter(str_split(strtobits($v), 8)); - // if ($x[0][0]) { - $quests['Hell'][$this->sData->qHell[$k]] = $x[0][0]; - // } + // if ($x[0][0]) { + $quests['Hell'][$this->sData->qHell[$k]] = $x[0][0]; + // } } - return $quests; } public function getWaypointsData($file) { - - $filePath = $_SESSION['savepath'] . $file; - $fp = fopen($filePath, "rb+"); - $wp = null; + fseek($this->fp, $this->sData->wpOffsetsNorm); + $a1 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsNorm + 1); + $a2 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsNorm + 2); + $a3 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsNorm + 3); + $a4 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsNorm + 4); + $a5 = strrev(strtobits(fread($this->fp, 1))); + $wp['Norm'] = str_split($a1 . $a2 . $a3 . $a4 . $a5); - fseek($fp, $this->sData->wpOffsetsNorm); - $a1 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsNorm + 1); - $a2 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsNorm + 2); - $a3 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsNorm + 3); - $a4 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsNorm + 4); - $a5 = strrev(strtobits(fread($fp, 1))); - - $wp['Norm'] = str_split($a1.$a2.$a3.$a4.$a5); - - fseek($fp, $this->sData->wpOffsetsNM); - $a1 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsNM + 1); - $a2 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsNM + 2); - $a3 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsNM + 3); - $a4 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsNM + 4); - $a5 = strrev(strtobits(fread($fp, 1))); - - $wp['NM'] = str_split($a1.$a2.$a3.$a4.$a5); - + fseek($this->fp, $this->sData->wpOffsetsNM); + $a1 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsNM + 1); + $a2 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsNM + 2); + $a3 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsNM + 3); + $a4 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsNM + 4); + $a5 = strrev(strtobits(fread($this->fp, 1))); + $wp['NM'] = str_split($a1 . $a2 . $a3 . $a4 . $a5); - fseek($fp, $this->sData->wpOffsetsHell); - $a1 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsHell + 1); - $a2 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsHell + 2); - $a3 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsHell + 3); - $a4 = strrev(strtobits(fread($fp, 1))); - fseek($fp, $this->sData->wpOffsetsHell + 4); - $a5 = strrev(strtobits(fread($fp, 1))); - - $wp['Hell'] = str_split($a1.$a2.$a3.$a4.$a5); - - // ddump(str_split($wp['Norm'])); + fseek($this->fp, $this->sData->wpOffsetsHell); + $a1 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsHell + 1); + $a2 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsHell + 2); + $a3 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsHell + 3); + $a4 = strrev(strtobits(fread($this->fp, 1))); + fseek($this->fp, $this->sData->wpOffsetsHell + 4); + $a5 = strrev(strtobits(fread($this->fp, 1))); + $wp['Hell'] = str_split($a1 . $a2 . $a3 . $a4 . $a5); foreach ($wp['Norm'] as $k => $v) { - // if ($v == 1) { - $waypoints['Norm'][$this->sData->wpNames[$k]] = $v; - // } + // if ($v == 1) { + $waypoints['Norm'][$this->sData->wpNames[$k]] = $v; + // } } - foreach ($wp['NM'] as $k => $v) { - // if ($v == 1) { - $waypoints['NM'][$this->sData->wpNames[$k]] = $v; - // } + // if ($v == 1) { + $waypoints['NM'][$this->sData->wpNames[$k]] = $v; + // } } - foreach ($wp['Hell'] as $k => $v) { - // if ($v == 1) { - $waypoints['Hell'][$this->sData->wpNames[$k]] = $v; - // } + // if ($v == 1) { + $waypoints['Hell'][$this->sData->wpNames[$k]] = $v; + // } } - - - // ddump($waypoints); - return $waypoints; }