d2tools/src/D2Functions.php

192 lines
5.4 KiB
PHP
Raw Normal View History

2021-03-23 08:13:54 +00:00
<?php
2021-03-28 08:12:25 +00:00
/*
2021-03-23 08:13:54 +00:00
2021-03-28 08:12:25 +00:00
Copyright (C) 2021 Hash Borgir
This file is part of D2Modder
Redistribution and use in source and binary forms, with
or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* This software must not be used for commercial purposes
* without my consent. Any sales or commercial use are prohibited
* without my express knowledge and consent.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY!
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2021-03-24 10:26:51 +00:00
function ddump($var) {
2022-06-21 13:06:41 +00:00
//echo "<pre>";
var_dump($var);
2022-06-21 13:06:41 +00:00
//echo "</pre>";
2022-06-21 13:06:41 +00:00
// 'Content-Type: application/json');
// echo json_encode($var, JSON_INVALID_UTF8_IGNORE | JSON_PRETTY_PRINT);
2021-03-23 08:13:54 +00:00
die();
}
2021-03-23 18:35:28 +00:00
2022-04-27 04:23:04 +00:00
function dump($var) {
2022-06-21 13:06:41 +00:00
//echo "<pre>";
var_dump($var);
2022-06-21 13:06:41 +00:00
//echo "</pre>";
2022-06-21 13:06:41 +00:00
//'Content-Type: application/json');
//echo json_encode($var, JSON_INVALID_UTF8_IGNORE | JSON_PRETTY_PRINT);
2022-04-27 04:23:04 +00:00
}
function strtobits(string $str): string {
$ret = "";
for ($i = 0; $i < strlen($str); ++$i) {
$ord = ord($str[$i]);
for ($bitnum = 7; $bitnum >= 0; --$bitnum) {
if ($ord & (1 << $bitnum)) {
$ret .= "1";
} else {
$ret .= "0";
}
}
}
return $ret;
}
function swapEndianness(string $hex) {
return implode('', array_reverse(str_split($hex, 2)));
}
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);
}
/**
* Calculate D2S Checksum
* @param $data
* @return string
*/
function checksum($data) : string {
$nSignature = 0;
foreach($data 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>';
}
function rcopy($src, $dst) {
// open the source directory
$dir = opendir($src);
// Make the destination directory if not exist
@mkdir($dst);
// Loop through the files in source directory
while( $file = readdir($dir) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) )
{
rcopy($src . '/' . $file, $dst . '/' . $file);
}
else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
function rrmdir($src) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $src . '/' . $file;
if ( is_dir($full) ) {
rrmdir($full);
}
else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
}