latest, mostly working, having trouble reading item props

This commit is contained in:
Hash Borgir 2023-06-18 21:02:51 -06:00
parent a678635c90
commit 410376d2d8
4 changed files with 2729 additions and 286 deletions

View File

@ -43,7 +43,7 @@ foreach ($ISCData as $k => $v) {
//$filePath = "D:\Diablo II\MODS\MedianXL2012\save\Test.d2s";
$filePath = "testa.d2s";
$filePath = "Test110.d2s";
$char = new D2Char($filePath);
//$char->setChar("CharacterStatus", "Died", 0);

View File

@ -67,6 +67,17 @@ function dump($var) {
//echo json_encode($var, JSON_INVALID_UTF8_IGNORE | JSON_PRETTY_PRINT);
}
function d($variable) {
echo '<pre>';
if (is_string($variable)) {
echo 'String (' . strlen($variable) . ') "' . htmlspecialchars($variable) . '"';
} else {
print_r($variable);
}
echo '</pre>';
}
/**
* @param string $str
* @return string

View File

@ -4,12 +4,448 @@ require_once 'D2BitReader.php';
require_once 'D2ItemStructureData.php';
require_once 'D2Strings.php';
require_once 'D2Functions.php';
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
@ -53,6 +489,12 @@ class D2Item {
$this->b = new D2BitReader($bits); // D2BitReader object
$this->oldbits = $bits;
$this->charName = $charName;
$this->ISC = new D2ItemData();
//ddump($this->ISC->getIscStrings('mana'));
$this->parseItem();
}
@ -155,6 +597,7 @@ class D2Item {
switch ($quality) {
case D2ItemQuality::LOW_QUALITY:
$this->quality = "Low Quality";
echo $this->quality;
$this->low_quality_item_data = $this->b->read(3);
$low_quality_item_data = bindec(strrev($this->low_quality_item_data));
switch ($low_quality_item_data) {
@ -178,19 +621,21 @@ class D2Item {
break;
case D2ItemQuality::NORMAL:
$this->quality = "Normal";
echo $this->quality;
break;
case D2ItemQuality::HIGH_QUALITY:
$this->quality = "High Quality";
echo $this->quality;
$this->high_quality_item_data = $this->b->read(3);
break;
case D2ItemQuality::MAGIC:
$this->quality = "Magic";
echo $this->quality;
// read 11 bits, prefix.
// if no prefix, then next 11 bits will be suffix
$this->magic_prefix = $this->b->read(11);
if (!bindec($this->magic_prefix)) {
$this->magic_suffix = $this->b->read(11);
}
$this->magic_suffix = $this->b->read(11);
$prefix = bindec(strrev($this->magic_prefix));
$suffix = bindec(strrev($this->magic_suffix));
@ -258,10 +703,34 @@ class D2Item {
break;
case D2ItemQuality::UNIQUE:
$this->quality = "Unique";
echo $this->quality;
$this->unique = $this->b->read(12);
break;
case D2ItemQuality::CRAFTED:
$this->quality = "Crafted";
echo $this->quality;
// this is from rare suffix/prefix.txt
$this->rareID1 = $this->b->read(8);
$this->rareID2 = $this->b->read(8);
// these are from magix suffix/perfi
$prefixes = [];
$suffixes = [];
for ($i = 0; $i < 3; $i++) {
$this->prefixBit = $this->b->read(1);
if ($this->prefixBit) {
$this->_prefixes[$i] = $this->b->read(11);
$prefixes[$i] = bindec(strrev($this->_prefixes[$i]));
}
$this->suffixBit = $this->b->read(1);
if ($this->suffixBit) {
$this->_suffixes[$i] = $this->b->read(11);
$suffixes[$i] = bindec(strrev($this->_sufffixes[$i]));
}
}
break;
default:
$this->quality = "Unknown";
@ -305,7 +774,7 @@ class D2Item {
// if it is an Armor, then get defense
if (!empty($res)) {
$this->defense = $this->b->read($SaveAdd);
$this->defense = $this->b->read(11);
$this->_defense = bindec(strrev($this->defense)) - 10;
var_dump("Def:" . $this->defense);
@ -328,11 +797,11 @@ class D2Item {
//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(8);
$this->currentdurability = $this->b->read(9);
$this->_currentdurability = bindec(strrev($this->currentdurability));
var_dump("CurDur:" . $this->currentdurability);
var_dump("CurDur:" . $this->_currentdurability);
$this->currentdurability_unknown_bit = $this->b->read(1);
//$this->currentdurability_unknown_bit = $this->b->read(1);
}
}
@ -349,11 +818,11 @@ class D2Item {
//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(8);
$this->currentdurability = $this->b->read(9);
$this->_currentdurability = bindec(strrev($this->currentdurability));
var_dump("CurDur:" . $this->currentdurability);
var_dump("CurDur:" . $this->_currentdurability);
$this->currentdurability_unknown_bit = $this->b->read(1);
//$this->currentdurability_unknown_bit = $this->b->read(1);
}
}
@ -381,30 +850,57 @@ class D2Item {
if (($quality == D2ItemQuality::SET)) {
$this->setfield = $this->b->read(5); // mismatch, need to debug
//var_dump($this->setProps);
//d($this->setProps);
}
$magicprops = new D2ItemProperties();
$props = $magicprops->magicprops;
$sql = "SELECT * FROM itemstatcost";
$isc = PDO_FetchAll($sql);
$readbits = [];
// read properties here
$property = $this->b->read(9);
$_property = bindec(strrev($property));
var_dump($property);
$val = bindec(strrev($this->b->read((($isc[$_property]['Save Bits'])))));
var_dump($_property);
//var_dump($props);
//// $properties = [];
// while ($property !== "111111111") { // property is not 0x1FF (9 1s)
//
// ddump($props[bindec(strrev($property))]['Bits'][0]);
//
// //$properties[bindec(strrev($property))] = $this->b->read($props[bindec(strrev($property))]['Bits'][0]);
// $property = $this->b->read(9);
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];
//
// }
// }
//ddump($properties);
//
// var_dump("prop: " . (($property)));
// var_dump("prop: " . (($_property)));
// var_dump((($propval)));
if($this->simple) {
echo "Simple";
}
// dump out data
$oldbitsSplit = str_split($this->oldbits, 8);
$dumpValuesSplit = str_split($this->dumpValues(), 8);
@ -484,7 +980,7 @@ class D2Item {
$values[] = $this->currentdurability_unknown_bit;
$values[] = $this->stackable;
$values[] = $this->numsockets;
//$values[] = $this->numsockets_extra_bit;
$values[] = $this->numsockets_extra_bit;
$values[] = $this->setfield;
$values[] = "";
$values[] = "";

File diff suppressed because it is too large Load Diff