diff --git a/test.php b/test.php index b3bd31b..1b53b47 100644 --- a/test.php +++ b/test.php @@ -17,12 +17,44 @@ require_once './src/D2DocGenerator.php'; define('DB_FILE', "ironman-dev.db"); PDO_Connect("sqlite:" . DB_FILE); -$fp = fopen("x", "rb+"); +$filename = "D:\Diablo II\MODS\ironman-dev\save\Test.d2s"; +$fp = fopen($filename, "rb+"); +$filesize = filesize($filename); +$pucData = unpack("C*",file_get_contents($filename)); + + // ddump($pucData); -fseek($fp, 0); -$y = fread($fp, 1); +// ddump(fseek($fp, 1)); + +// pucData - pointer to the byte stream of the .d2s file +// iSize - number of bytes in the stream ( filesize ) +function checksum($fp, $pucData, $iSize){ + // delete old checksum at offset 0x0C - byte 12 + fseek($fp, 12); + // (I) unsigned integer (machine dependent size and byte order) + fwrite($fp, pack('I', 0)); // produces 4 bytes + + $uiCS = 0b00000000000000000000000000000000; + + // this is the whole checksum calculation + for ($j = 1; $j <= $iSize; ++$j ){ + $uiCS = (($uiCS << 1)|( $uiCS >> 31)) + decbin($pucData[$j]); + // dump(decbin($pucData[$j])); + } + + + dump(decbin($uiCS)); + + //fseek($fp, 12); + //fwrite($fp, pack('I', $uiCS)); +} + +fseek($fp, 347); +fwrite($fp, pack('C', 0xFD)); +fseek($fp, 348); +fwrite($fp, pack('C', 0x9F)); + +checksum($fp, $pucData, $filesize); -fseek($fp, 4); -fwrite($fp, "__"); fclose($fp);