From c9c7c63abc15e49d529be7166d4ee46410433090 Mon Sep 17 00:00:00 2001 From: Hash Borgir Date: Mon, 19 Jun 2023 21:01:22 -0600 Subject: [PATCH] item parser still not giving right prop vals and while loop freezes php --- CharEditor.php | 2 +- src/D2Item.php | 553 +++++-------------------------------------------- 2 files changed, 57 insertions(+), 498 deletions(-) diff --git a/CharEditor.php b/CharEditor.php index 9f0d07a..54ba458 100644 --- a/CharEditor.php +++ b/CharEditor.php @@ -43,7 +43,7 @@ foreach ($ISCData as $k => $v) { //$filePath = "D:\Diablo II\MODS\MedianXL2012\save\Test.d2s"; -$filePath = "Test110.d2s"; +$filePath = "testa.d2s"; $char = new D2Char($filePath); //$char->setChar("CharacterStatus", "Died", 0); diff --git a/src/D2Item.php b/src/D2Item.php index 190911d..4c7c4ff 100644 --- a/src/D2Item.php +++ b/src/D2Item.php @@ -9,443 +9,8 @@ require_once 'D2ItemData.php'; /** * */ - class D2Item { - /** - * @var null - */ - private $bits = null; - private $charName; - - /** - * @var null - */ - public $iData = null; - - /** - * @param $bits - */ - public function __construct($bits, $charName = null) { - if ($bits == '') - return false; - $this->bits = $bits; - $this->charName = $charName; - return $this->parseItem(); - } - - /* set $this->iData to array of item details - * - * @return array of item details - */ - - /** - * @return null - */ - private function parseItem() { - $b = new D2BitReader($this->bits); - $b->skip(16); // Skip JM - $b->skip(4); // skip unknown 4 bytes - $this->iData['identified'] = $b->read(1); // bit 20, identified - $b->skip(6); // skip unknown 6 bytes - $this->iData['socketed'] = $b->read(1); // bit 27, socketed - $b->skip(1); - // This bit is set on items which you have picked up since the last time the game was saved. - $this->iData['pickedUpSinceLastSave'] = $b->read(1); // bit 29 - $b->skip(2); - $this->iData['ear'] = $b->read(1); // bit 32 bool - $this->iData['startingItem'] = $b->read(1); // bit 33 bool - $b->skip(3); - $this->iData['compact'] = $b->read(1); // bit 37 compact - $this->iData['ethereal'] = $b->read(1); // bit 38 ethereal - $b->skip(1); // unknown, seems always 1 - $this->iData['personalized'] = $b->read(1); // bit 40 Item has been personalized (by Anya in Act V) - $b->skip(1); - $this->iData['runeword'] = $b->read(1); // bit 42 the item has been given a Rune Word. - $b->skip(15); // unknown; some of these bits may be set - // item location - $location = bindec($b->readr(3)); // bit 58 parent Item location. - $body = bindec($b->readr(4)); // bit 61 If the item is equipped - $col = bindec($b->readr(4)); // bit 65 Column number of the left corner of the item - $row = bindec($b->readr(4)); // bit 69 Row number of the top of the item, counting from 0. - $_stored = bindec($b->readr(3)); // bit 73 - // The number of gems (or skulls or jewels) which have been glued to this item (if socketed). There will be this many additional item structures for the gems immediately following this item, in the order that the gems were inserted. - // get item code - $b->seek(76); - $itemCode = ''; - foreach (str_split($b->read(32), 8) as $byte) { - $itemCode .= chr(bindec(strrev($byte))); - } - $this->iData['code'] = trim($itemCode); - $sql = "SELECT * from armor WHERE code = '{$this->iData['code']}'"; - $res = PDO_FetchRow($sql); - if (empty($res)) { - $sql = "SELECT * from misc WHERE code = '{$this->iData['code']}'"; - $res = PDO_FetchRow($sql); - } - if (empty($res)) { - $sql = "SELECT * from weapons WHERE code = '{$this->iData['code']}'"; - $res = PDO_FetchRow($sql); - } - - // set txt data array - $this->iData['txt'] = ($res); - - $sql = " - SELECT code, namestr - FROM armor - WHERE code = '{$this->iData['code']}' - UNION - SELECT code, namestr - FROM misc - WHERE code = '{$this->iData['code']}' - UNION - SELECT code, namestr - FROM weapons - WHERE code = '{$this->iData['code']}' - "; - $res = PDO_FetchAssoc($sql); - - $sql = "SELECT `String` FROM strings WHERE `Key`='{$res[$this->iData['code']]}'"; - $res = PDO_FetchOne($sql); - $this->iData['basename'] = $res; - $this->iData['basename'] = preg_replace('/ÿc[0-9]/', '', $this->iData['basename']); - - $b->seek(108); - $this->iData['gems_in'] = bindec($b->readr(3)); - - // if item is not compact, then we read extended properties - - if (!$this->iData['compact']) { - // bit 111, 32bits, Unique identifier. Diablo II randomly generates a value for this field in order to discourage cheaters from "duping" items. - //This appears to be the item's level; i.e., the level with which the item was created (or 'dropped'). The item level is based on the level of the monster who dropped it, the level of the area you're in if found in a chest, or, in rare cases, your characters level. The item level determines what modifiers are allowed on the item. - $b->seek(143); - $this->iData['ilvl'] = bindec($b->readr(7)); - - $b->seek(150); - $quality = bindec($b->readr(4)); - - switch ($quality) { - case D2ItemQuality::LOW_QUALITY: - $this->iData['iquality'] = "Low Quality"; - break; - case D2ItemQuality::NORMAL: - $this->iData['iquality'] = "Normal"; - break; - case D2ItemQuality::HIGH_QUALITY: - $this->iData['iquality'] = "High Quality"; - break; - case D2ItemQuality::MAGIC: - $this->iData['iquality'] = "Magic"; - break; - case D2ItemQuality::SET: - $this->iData['iquality'] = "Set"; - break; - case D2ItemQuality::RARE: - $this->iData['iquality'] = "Rare"; - break; - case D2ItemQuality::UNIQUE: - $this->iData['iquality'] = "Unique"; - break; - case D2ItemQuality::CRAFTED: - $this->iData['iquality'] = "Crafted"; - break; - default: - $this->iData['iquality'] = "Unknown"; - break; - } - - // after reading item quality, we're at 154 offset. - // ring/amu/jew/char or not. 1 or 0. if 1, next 3 are set - // If this bit is set, the item has one of multiple pictures associated with it; the next field determines which picture a particular item uses. If this bit is 0, the next field is absent. The picture field is used for rings, amulets, jewels, and charms. - // After the above data, if the item is a ring, amulet, jewel, or charm, then it has a 1 bit followed by three more bits. All other items (that I've seen) have just a single 0 bit. - $b->seek(154); // picture bit - $ring = bindec($b->read(1)); - if ($ring) { // we read 1 bit at 154, end up at 155 - $b->seek(155); - $ringPic = bindec($b->readr(3)); // for jew, amu, rin, char - } - // if ring bit is 0, go to 155 and read 1 bit - else { - // This bit apparently is set for certain class-specific Expansion Set items. It indicates the presence of the next 11-bit field. If this bit is 0, the next field is absent. - $b->seek(155); - $_class_specific = bindec($b->read(1)); - } - /* Credit for the discovery of this field's meaning goes entirely to Guillaume Courtin of France. Thanks! :-> - This field indicates magic properties which are inherent in certain class-specific items. A given class-specific item will (almost) always start with the same set of properties, even if its quality is "normal". Other quality ratings may add more properties to the standard set. It appears that items which will have this field are: - Amazon-only bows, spears, and javelins - Voodoo heads (Necromancer-only shields) - Paladin-only shields - Orbs (Sorceress-Only wands) - */ - // if class specific bit (155) is 1, then go to 156 and read 11 bits - if ($_class_specific) { - $b->seek(156); - $class_specific = bindec($b->readr(11)); - } - - //var_dump($b->getOffset()); - - switch ($quality) { - case D2ItemQuality::LOW_QUALITY: - $low_quality_item_data = bindec($b->readr(3)); - switch ($low_quality_item_data) { - case 0: - $this->iData['low_quality_item_data'] = "Crude"; - break; - case 1: - $this->iData['low_quality_item_data'] = "Cracked"; - break; - case 2: - $this->iData['low_quality_item_data'] = "Damaged"; - break; - case 3: - $this->iData['low_quality_item_data'] = "Low Quality"; - break; - default: - $this->iData['low_quality_item_data'] = "Unknown quality"; - break; - } - break; - // Normal items have no extra quality data. - case D2ItemQuality::NORMAL: - break; - case D2ItemQuality::HIGH_QUALITY: - $high_quality_item_data = bindec($b->readr(3)); - break; - case D2ItemQuality::MAGIC: - // read 11 bits, prefix. - // if no prefix, then next 11 bits will be suffix - $this->iData['magic_prefix'] = bindec($b->readr(11)); - if (!$this->iData['magic_prefix']) { - $this->iData['magic_suffix'] = bindec($b->readr(11)); - } - if (!empty($this->iData['magic_prefix'])) { - $sql = "SELECT * FROM magicprefix WHERE ROWID='{$this->iData['magic_prefix']}'"; - $res = PDO_FetchRow($sql); - $this->iData['magic_prefix'] = $res; - } - if (!empty($this->iData['magic_suffix'])) { - $sql = "SELECT * FROM magicsuffix WHERE ROWID='{$this->iData['magic_sufffix']}'"; - $res = PDO_FetchRow($sql); - $this->iData['magic_suffix'] = $res; - } - - break; - case D2ItemQuality::SET: - // Set items have a 12-bit field containing the ID of the set. (Not the set member, but the whole set.) The set member is identified by cross-referencing the item type with the set ID. Also note that set items have an extra field following the item-specific data. - // Set identifier; i.e., all items which are part of the set will have the same value in this field. - //var_dump($b->getOffset()); - $setid = bindec($b->readr(12)); - //var_dump($setid); - // first get set name - $sql = "SELECT `set` from setitems WHERE ROWID=$setid"; - $set = PDO_FetchOne($sql); - $this->iData["setname"] = $set; - - $sql = "SELECT * from setitems WHERE `set`=? AND item=?"; - $res = PDO_FetchRow($sql, [$set, $this->iData['code']]); - - $this->iData["data"] = $res; - - break; - case D2ItemQuality::RARE: - - // this is from rare suffix/prefix.txt - $ID1 = bindec($b->readr(8)); - $ID2 = bindec($b->readr(8)); - - $this->iData['iquality'] = 'Rare'; - - // these are from magix suffix/perfi - - $prefixes = []; - $suffixes = []; - - for ($i = 1; $i <= 3; $i++) { - $prefixBit = $b->read(1); - if ($prefixBit) { - $prefixes[] = bindec($b->readr(11)); - } - - $suffixBit = $b->read(1); - if ($suffixBit) { - $suffixes[] = bindec($b->readr(11)); - } - } - - // now for each prefix/suffix array, we need to - // generate ISC strings - - $this->iData['prefixes'] = $prefixes; - $this->iData['suffixes'] = $suffixes; - - break; - case D2ItemQuality::UNIQUE: - //Unique items have an additional 12 bit field, which in most cases is the unique item ID. The few exceptions are certain quest items (e.g., the Horadric Malus). - $unid = bindec($b->readr(12)); - $sql = "SELECT * from uniqueitems WHERE ROWID=$unid"; - $res = PDO_FetchRow($sql); - $this->iData['data'] = $res; - - break; - case D2ItemQuality::CRAFTED: - $this->iData['iquality'] = "Crafted"; - break; - default: - $this->iData['iquality'] = "Unknown"; - break; - } - } - - if ($this->iData['runeword']) { - $runeName_possibly = bindec($b->readr(12)); - $b->skip(4); - } - - if ($this->iData['personalized']) { - $charName = ""; - - $charBits = $b->readr(7); // Read the first 7 bits - while ($charBits !== "0000000") { // Check for null termination - $char = chr(bindec($charBits)); - $charName .= $char; - - $charBits = $b->readr(7); // Read the next 7 bits - } - } - - $tome = $b->readr(5); - - // Unknown (denoted as 'timestamp' in various places) - $b->skip(1); - - // Now begins item specific data/stats - - $sql = "SELECT code FROM armor WHERE code = ?"; - $res = PDO_FetchOne($sql, [$this->iData['code']]); - - // this is an armor - //Only exists if the item is an armor (i.e. the item code is found in Armor.txt) - // Defense of the armor. Subtract this value by 10 to get the true armor value (note: this -10 matches the "Save Add" column in ItemStatCost.txt for the armor stat). - if (!empty($res)) { - $defense = bindec($b->readr(11)) - 10; - $durability = bindec($b->readr(8)); - } - - $sql = "SELECT code FROM weapons WHERE code = ?"; - $res = PDO_FetchOne($sql, [$this->iData['code']]); - - // this is a weapon - // get durability - if (!empty($res)) { - $durability = bindec($b->readr(8)); - } - - // Only exists if the item's max durability is greater than zero - //The first 8 bits are the item's current durability. The last bit is unknown. - $b->skip(9); - - $sql = "SELECT code FROM armor WHERE code = ?"; - $res = PDO_FetchOne($sql, [$this->iData['code']]); - - // weird behavior/bug - // if item is in a container, bodypart will be NULL - // if item is on bodypart, container will be NULL - switch ($location) { - case D2ItemLocation::BELT: - $this->iData['container'] = 'Belt'; - break; - case D2ItemLocation::CURSOR: - break; - case D2ItemLocation::SOCKET: - break; - case D2ItemLocation::STORED: - switch ($_stored) { - case D2ItemLocationStored::NONE: - - $this->iData['container'] = 'None'; // item is not stored, check bit 58 - break; - case D2ItemLocationStored::INVENTORY: - $this->iData['container'] = 'Inventory'; - break; - case D2ItemLocationStored::CUBE: - $this->iData['container'] = 'Horadric Cube'; - break; - case D2ItemLocationStored::STASH: - $this->iData['container'] = 'Stash'; - break; - default: $this->iData['container'] = 'Unknown'; - break; - } - break; - case D2ItemLocation::EQUIPPED: - switch ($body) { - case D2ItemLocationBody::HELMET: $this->iData['location'] = 'Helmet'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::AMULET: $this->iData['location'] = 'Amulet'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::ARMOR: $this->iData['location'] = 'Armor'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::WEAPONR: $this->iData['location'] = 'Weapon R'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::WEAPONL: $this->iData['location'] = 'Weapon L'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::RINGR: $this->iData['location'] = 'Ring R'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::RINGL: $this->iData['location'] = 'Ring L'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::BELT: $this->iData['location'] = 'Belt'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::BOOTS: $this->iData['location'] = 'Boots'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::GLOVES: $this->iData['location'] = 'Gloves'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::WEAPONR2: $this->iData['location'] = 'Weapon Alt R'; - $this->iData['container'] = 'Body'; - break; - case D2ItemLocationBody::WEAPONL2: $this->iData['location'] = 'Weapon Alt L'; - $this->iData['container'] = 'Body'; - break; - default: $this->iData['location'] = 'Unknown'; - break; - } - break; - } - - // if item is ear - if ($this->iData['ear']) { - // set item code/basename - $this->iData['itemCode'] = 'ear'; - $this->iData['basename'] = 'Ear'; - // get ear class/level - $eclass = bindec($b->readr(3)); // bit 76 - $elevel = bindec($b->readr(7)); // bit 79 - // get ear char's name - } - - - - - - return $this->iData; - } - -} - - -class D2Item_NEW { - private $bits = null; // Item bitstring private $oldbits = null; // Old Item bits for comparison private $charName; // Character Name @@ -489,12 +54,11 @@ class D2Item_NEW { $this->b = new D2BitReader($bits); // D2BitReader object $this->oldbits = $bits; $this->charName = $charName; - - + $this->ISC = new D2ItemData(); //ddump($this->ISC->getIscStrings('mana')); - + $this->parseItem(); } @@ -635,7 +199,6 @@ class D2Item_NEW { // if no prefix, then next 11 bits will be suffix $this->magic_prefix = $this->b->read(11); $this->magic_suffix = $this->b->read(11); - $prefix = bindec(strrev($this->magic_prefix)); $suffix = bindec(strrev($this->magic_suffix)); @@ -730,7 +293,7 @@ class D2Item_NEW { $this->_suffixes[$i] = $this->b->read(11); $suffixes[$i] = bindec(strrev($this->_sufffixes[$i])); } - } + } break; default: $this->quality = "Unknown"; @@ -777,8 +340,8 @@ class D2Item_NEW { $this->defense = $this->b->read(11); $this->_defense = bindec(strrev($this->defense)) - 10; - var_dump("Def:" . $this->defense); - var_dump("Def:" . $this->_defense); + //var_dump("Def:" . $this->defense); + //var_dump("Def:" . $this->_defense); // armors are missing a full byte here // if we insert all 0s, it evens out, but is wrong @@ -791,16 +354,16 @@ class D2Item_NEW { $this->maxdurability = "" . $this->b->read(8); $this->_maxdurability = bindec(strrev($this->maxdurability)); - var_dump("MaxDur:" . $this->maxdurability); - var_dump("MaxDur:" . $this->_maxdurability); + //var_dump("MaxDur:" . $this->maxdurability); + //var_dump("MaxDur:" . $this->_maxdurability); //Only exists if the item's max durability is greater than zero //The first 8 bits are the item's current durability. The last bit is unknown. if ($this->maxdurability !== 0) { $this->currentdurability = $this->b->read(9); $this->_currentdurability = bindec(strrev($this->currentdurability)); - var_dump("CurDur:" . $this->currentdurability); - var_dump("CurDur:" . $this->_currentdurability); + //var_dump("CurDur:" . $this->currentdurability); + //var_dump("CurDur:" . $this->_currentdurability); //$this->currentdurability_unknown_bit = $this->b->read(1); } } @@ -812,16 +375,16 @@ class D2Item_NEW { $this->maxdurability = $this->b->read(8); $this->_maxdurability = bindec(strrev($this->maxdurability)); - var_dump("MaxDur:" . $this->maxdurability); - var_dump("MaxDur:" . $this->_maxdurability); + //var_dump("MaxDur:" . $this->maxdurability); + //var_dump("MaxDur:" . $this->_maxdurability); //Only exists if the item's max durability is greater than zero //The first 8 bits are the item's current durability. The last bit is unknown. if ($this->maxdurability !== 0) { $this->currentdurability = $this->b->read(9); $this->_currentdurability = bindec(strrev($this->currentdurability)); - var_dump("CurDur:" . $this->currentdurability); - var_dump("CurDur:" . $this->_currentdurability); + //var_dump("CurDur:" . $this->currentdurability); + //var_dump("CurDur:" . $this->_currentdurability); //$this->currentdurability_unknown_bit = $this->b->read(1); } } @@ -830,7 +393,7 @@ class D2Item_NEW { if ($this->txt['stackable'] == 1) { $this->stackable = $this->b->read(9); - var_dump("stackable: " . bindec(strrev($this->stackable))); + //var_dump("stackable: " . bindec(strrev($this->stackable))); } @@ -839,68 +402,64 @@ class D2Item_NEW { $this->numsockets = $this->b->read(4); $this->_numsockets = bindec(strrev($this->numsockets)); //$this->numsockets_extra_bit = $this->b->read(1); - var_dump("numsock: " . $this->numsockets); - var_dump("numsock: " . $this->_numsockets); + //var_dump("numsock: " . $this->numsockets); + //var_dump("numsock: " . $this->_numsockets); } // Set properties bit field, used later for reading the set property lists of the item // if item is set //somewhere here in the set thing, we're missing 9 bits. - // if we skip those 9 bits for testing, we should get corect setprops? + // if we skip those 9 bits for testing, we should get corect setprops? if (($quality == D2ItemQuality::SET)) { $this->setfield = $this->b->read(5); // mismatch, need to debug - //d($this->setProps); } - + if (($quality == D2ItemQuality::MAGIC)) { + if ($this->itemcode == 'aqv' || + $this->itemcode == 'cqv' || + $this->itemcode == 'eaq' || + $this->itemcode == 'ebq') { + $this->magicammo = $this->b->read(5); + } + } + + if (($quality == D2ItemQuality::NORMAL)) { + if ($this->txt['type'] == 'tpot') { + $this->tpot = $this->b->read(5); + } + } + + $sql = "SELECT * FROM itemstatcost"; $isc = PDO_FetchAll($sql); - - - - - $readbits = []; + // read properties here $property = $this->b->read(9); $_property = bindec(strrev($property)); - - $val = bindec(strrev($this->b->read((($isc[$_property]['Save Bits']))))); - - var_dump($_property); - var_dump($val); - var_dump($isc[$_property]); - - - -// foreach ($props[$_property]['Bits'] as $k => $v) { -// $readbits[$k] = $v; -// } -// -// //var_dump($readbits); -// -// $propval = []; -// if (!empty($readbits)) { -// foreach ($readbits as $k => $v) { -// $x = $this->b->read($v); -// $propval[$_property][] = $x; -// $_propval[$_property][] = bindec(strrev($x)); -// $_propval[$_property][] = $props[$_property]; -// -// } -// } -// -// var_dump("prop: " . (($property))); -// var_dump("prop: " . (($_property))); -// var_dump((($propval))); - - - if($this->simple) { + for ($i = 0; $i < 10; $i++) { + if ($isc[$_property]['Save Bits']) { + $val = $this->b->read($isc[$_property]['Save Bits']); + $_val = bindec(strrev($val)); + $property = $this->b->read(9); + $_property = bindec(strrev($property)); + //var_dump($isc[$_property]['Stat']); + //var_dump($property); + //var_dump($_property); + //var_dump($val); + //var_dump($_val); + + } + } + + ////var_dump($isc[$_property]); + + if ($this->simple) { echo "Simple"; } - - + + // dump out data $oldbitsSplit = str_split($this->oldbits, 8); $dumpValuesSplit = str_split($this->dumpValues(), 8); @@ -982,8 +541,8 @@ class D2Item_NEW { $values[] = $this->numsockets; $values[] = $this->numsockets_extra_bit; $values[] = $this->setfield; - $values[] = ""; - $values[] = ""; + $values[] = $this->magicammo; + $values[] = $this->tpot; $values[] = ""; $values[] = ""; $values[] = "";