D2S/Item parser in progress. Still need to debug cubemain.txt doc generator output colulmns. Saving code for now

This commit is contained in:
Hash Borgir
2022-06-20 19:27:08 -06:00
parent 521dfe12c6
commit 822e3dcbd8
19 changed files with 482 additions and 1242 deletions

View File

@@ -75,11 +75,75 @@ function strtobits(string $str): string {
return $ret;
}
function swapEndianness($hex) {
function swapEndianness(string $hex) {
return implode('', array_reverse(str_split($hex, 2)));
}
function modifyBit(int $n, int $p, bool $b){
function setBit(int $n, int $p, bool $b){
return ($b ? ($n | (1 << $p)) : ($n & ~(1 << $p)) );
}
function getBit(int $b, int $p){
return intval(($b & (1 << $p)) != 0);
}
function checksum($fileData) {
$nSignature = 0;
foreach($fileData as $byte){
$nSignature = ((($nSignature << 1) | ($nSignature >> 31)) + $byte) & 0xFFFFFFFF;
}
return swapEndianness(dechex($nSignature));
}
/**
* Find the position of the Xth occurrence of a substring in a string
* @param $haystack
* @param $needle
* @param $number integer > 0
* @return int
*/
function strposX($haystack, $needle, $number) {
if ($number == 1) {
return strpos($haystack, $needle);
} elseif ($number > 1) {
return strpos($haystack, $needle, strposX($haystack, $needle, $number - 1) + strlen($needle));
} else {
return error_log('Error: Value for parameter $number is out of range');
}
}
function isBit(string $bit): int {
return ((int) $bit ? 1 : 0 );
}
function toBits($input): string {
$output = '';
if (is_string($input)){
foreach(str_split($input) as $i){
$output .= str_pad(decbin(ord($i)), 8, 0, STR_PAD_LEFT);
}
return $output;
} else if (is_int($input)){
return str_pad(decbin($input), 8, 0, STR_PAD_LEFT);
} else if (is_array($input)){
foreach ($input as $i) {
$output .= tobits($i);
}
return $output;
}
}
function print_mem()
{
/* Currently used memory */
$mem_usage = memory_get_usage();
/* Peak memory usage */
$mem_peak = memory_get_peak_usage();
echo 'The script is now using: <strong>' . round($mem_usage / 1024) . 'KB</strong> of memory.<br>';
echo 'Peak usage: <strong>' . round($mem_peak / 1024) . 'KB</strong> of memory.<br><br>';
}