D2S Editing WIP

This commit is contained in:
Hash Borgir
2023-06-01 23:42:12 -06:00
parent 3357f757ea
commit b9f4054028
4 changed files with 450 additions and 436 deletions

View File

@@ -1,138 +1,200 @@
<?php
// Set error reporting to only show errors and parse errors
error_reporting(E_ERROR | E_PARSE);
// Set the time limit for script execution to unlimited
set_time_limit(-1);
// Set the maximum input time to unlimited
ini_set('max_input_time', '-1');
// Set the maximum execution time to unlimited
ini_set('max_execution_time', '0');
// Start the session
session_start();
// Start output buffering
ob_start();
// Define the constant 'DB_FILE' with the value of 'modname' session variable followed by ".db"
define('DB_FILE', $_SESSION['modname'] . ".db");
// Include the configuration file
require_once './config.php';
// Include the PDO wrapper file
require_once './_pdo.php';
// Include the required class files
require_once "./src/D2Functions.php";
require_once "./src/D2Database.php";
require_once './src/D2Files.php';
require_once './src/D2TxtParser.php';
require_once './src/D2ItemDesc.php';
require_once './src/D2Char.php';
require_once './src/D2Char.php';
require_once './src/D2CharStructureData.php';
require_once './src/D2ByteReader.php';
$dStruct = new D2CharStructureData();
// Create an instance of D2CharStructureData class
$csData = new D2CharStructureData();
$g = $_GET;
$cmd = $g['cmd'];
// Get the file path from the POST data and replace backslashes /
$filePath = $g['filePath'];
$filePath = str_replace("\\", "\\\\", $filePath);
// Handle the WP check/uncheck
if ($cmd == "wp") {
$diff = $g['diff'];
if ($diff == "Norm") {
$offset = 643;
}
if ($diff == "NM") {
$offset = 667;
}
if ($diff == "Hell") {
$offset = 691;
}
/*
array (size=5)
'cmd' => string 'wp' (length=2)
'name' => string '1' (length=1)
'value' => string '1' (length=1)
'filePath' => string 'D:\\Diablo II\\MODS\\ironman-dev\\save\\Sorc.d2s' (length=48)
'diff' => string 'Norm' (length=4)
*/
$p = $_POST;
// ddump($p);
$fileContents = file_get_contents($filePath); // Read the contents of the file
$ByteReader = new D2ByteReader($fileContents); // Create a new instance of D2ByteReader with the file data
$fileData = $ByteReader->getData(); // Get the data from the ByteReader instance
foreach($p['quest'] as $k => $v) {
$q[$k] = ($dStruct->_qNorm[$k]);
}
$wpBytes = $ByteReader->read($offset, 5, 1);
$wpBytesToBits = $ByteReader->toBits($wpBytes);
foreach($p['wp'] as $k => $v) {
$w[$k] = ($dStruct->_qNorm[$k]);
$BitReader = new D2BitReader($wpBytesToBits);
$BitReader->setBit($g['name'], $g['value']);
$newBits = $BitReader->getBits();
$newBitsToBytes = $ByteReader->bitsToHexString($newBits);
$ByteReader->writeBytes($offset, $newBitsToBytes);
$newFileData = $ByteReader->getData();
$fileSaved = file_put_contents($filePath, $newFileData);
$checksum = (shell_exec("bin\d2scs.exe \"$filePath\""));
if ($fileSaved) {
echo "Success";
} else {
echo "Fail";
}
}
die();
// Get the POST data
$p = $_GET;
// Loop through each 'quest' element in the POST data and assign the corresponding value from 'dStruct->_qNorm' to 'q'
foreach ($p['quest'] as $k => $v) {
$q[$k] = $dStruct->_qNorm[$k];
}
$wpNames_flipped = array_values($dStruct->wpNames_flipped);
// Loop through each 'wp' element in the POST data and assign the corresponding value from 'dStruct->_qNorm' to 'w'
foreach ($p['wp'] as $k => $v) {
$w[$k] = $dStruct->_qNorm[$k];
}
// Get the file path from the POST data and replace backslashes /
$filePath = $p['filePath'];
$filePath = str_replace("\\", "\\\\", $filePath);
// Open the file in binary mode for both reading and writing
$fp = fopen($filePath, "rb+");
// 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
// edit quests which are checked, to Just finished FE FF
// Edit quests which are checked to set them as 'Just finished' (FE FF)
foreach ($q as $k => $v) {
// Set the file pointer position to the quest offset
fseek($fp, $v);
// Write the byte value 0xFE at the current position
fwrite($fp, pack('C', 0xFE));
fseek($fp, $v+1);
// Move the file pointer to the next position
fseek($fp, $v + 1);
// Write the byte value 0xFF at the current position
fwrite($fp, pack('C', 0xFF));
}
if ($p['wp_all'] == "1") {
fseek($fp, 643);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 644);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 645);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 646);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 647);
fwrite($fp, pack('C', 0x7F));
fseek($fp, 667);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 668);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 669);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 670);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 671);
fwrite($fp, pack('C', 0x7F));
fseek($fp, 691);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 692);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 693);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 694);
fwrite($fp, pack('C', 0xFF));
fseek($fp, 695);
fwrite($fp, pack('C', 0x7F));
}
else if ($p['wp_all'] == "0")
{
fseek($fp, 643);
fwrite($fp, pack('C', 0x00));
fseek($fp, 644);
fwrite($fp, pack('C', 0x00));
fseek($fp, 645);
fwrite($fp, pack('C', 0x00));
fseek($fp, 646);
fwrite($fp, pack('C', 0x00));
fseek($fp, 647);
fwrite($fp, pack('C', 0x00));
fseek($fp, 667);
fwrite($fp, pack('C', 0x00));
fseek($fp, 668);
fwrite($fp, pack('C', 0x00));
fseek($fp, 669);
fwrite($fp, pack('C', 0x00));
fseek($fp, 670);
fwrite($fp, pack('C', 0x00));
fseek($fp, 671);
fwrite($fp, pack('C', 0x00));
fseek($fp, 691);
fwrite($fp, pack('C', 0x00));
fseek($fp, 692);
fwrite($fp, pack('C', 0x00));
fseek($fp, 693);
fwrite($fp, pack('C', 0x00));
fseek($fp, 694);
fwrite($fp, pack('C', 0x00));
fseek($fp, 695);
fwrite($fp, pack('C', 0x00));
// Array containing the fseek wp offsets
// 5 acts per difficulty
// starts at 641, but we skip two bytes, go to 643, then read next 5 bytes, each byte is a bitfield per act.
$offsets = [
643, 644, 645, 646, 647, // First set of offsets Norm
667, 668, 669, 670, 671, // Second set of offsets NM
691, 692, 693, 694, 695 // Third set of offsets Hell
];
// Determine the value to write based on the condition $p['wp_all'] == "1"
$valueToWrite = ($p['wp_all'] == "1") ? 0xFF : 0x00;
// Loop over each offset in the $offsets array
foreach ($offsets as $offset) {
// Set the file pointer position to the current offset
fseek($fp, $offset);
// Write the value specified by pack('C', $valueToWrite)
fwrite($fp, pack('C', $valueToWrite));
}
$checksum = (shell_exec("bin\d2scs.exe \"$filePath\""));
// write NEW checksum at offset 0x0C - byte 12
//fseek($fp, 12);
// (I) unsigned integer (machine dependent size and byte order)
//fwrite($fp, pack('H*', $checksum)); // produces 4 bytes
fclose($fp);
header('Location: /');
header('Location: /');