hex='';
if (is_file($filename)) {
$handle=fopen($filename,"r");
while (!feof($handle)) {
$this->hex.=fgets($handle,filesize($filename));
}
fclose($handle);
} else {
die ("$filename not exists!");
}
}
public function save($filename,$charinfo=false)
{
if (!$handle = fopen($filename, 'w')) {
die("Can't open file $filename");
}
if (!$charinfo) $this->setChecksum(); //вычислить CRC
fwrite($handle, $this->hex);
fclose($handle);
}
//выбрать срез строки
public function getValue($start,$length)
{
return substr($this->hex, $start, $length);
}
//заменить срез строки значением $hexstr
public function setValue($hexstr,$start,$length)
{
$this->hex=substr($this->hex, 0, $start) . $hexstr . substr($this->hex, $start+$length);
}
public function setName ($charname, $charinfo = false)
{
$start = (!$charinfo) ? 20 : 48; // charsave = 20, charinfo = 48
$this->checkName($charname);
$charname=str_pad($charname, 16,chr(0), STR_PAD_RIGHT); //заполнить все пустые справа
$this->setValue($charname,$start,16);
}
public function checkName($charname)
{
if(!preg_match("/^[a-zA-Z-_]+$/", $charname) or strlen($charname)<3 or strlen($charname)>15) {
die ("Имя должно быть 3-15 символов и содержать только буквы и _-
Name must contain 3-15 characters and allowed only letters, numeral and _-");
}
}
public function getName ($start=20)
{
//для charinfo $start = 48
$charname=$this->getValue($start,16);
$charname=str_replace("?","",$charname);
return $charname;
}
public function getCharinfo()
{
$charname=str_replace('?','',$charname);
return $charname;
}
#public function setUndead() { //снять dead
# $this->setValue("d",36,1);
#}
//отредактирован в php моем редакторе :-)
public function setLock()
{
$this->setValue("lock",52,4);
}
private function setChecksum()
{
$this->setValue(chr(0).chr(0).chr(0).chr(0), 12,4); //обнулить CRC
$sum = 0;
for($i=0;$i < strlen($this->hex);$i++)
{
$sum = sprintf('%032s',decbin($sum));
$sum = substr($sum,1) . substr($sum, 0, 1);
$sum = bindec($sum);
$sum += ord(substr($this->hex,$i,1));
}
$this->setValue($this->dec2str($sum,true), 12,4); //записать CRC
}
// set ladder and dead flags
// $softcore (softcore = true | hardcore = false)
// $ladder (ladder = true | nonladder = false)
// $dead (dead = true | live = false)
// $charinfo (charinfo = true | charsave = false)
public function setLadderDeadFlag($softcore = true, $ladder = true, $dead = false, $charinfo = false)
{
$type[0][0][0] = 37; // hardcore - nonladder - live 0010 0101
$type[0][0][1] = 44; // hardcore - nonladder - dead 0010 1100
$type[0][1][0] = 101; // hardcore - ladder - live 0110 0101
$type[0][1][1] = 108; // hardcore - ladder - dead 0110 1100
$type[1][0][0] = 33; // softcore - nonladder - live 0001 1111
$type[1][0][1] = 40; // softcore - nonladder - dead 0010 1000
$type[1][1][0] = 97; // softcore - ladder - live 0110 0001
$type[1][1][1] = 104; // softcore - ladder - dead 0110 1000
// position
$pos = (!$charinfo) ? $pos = 36 : $pos = 180;
$value = chr( $type[$softcore][$ladder][$dead] );
$this->setValue($value, $pos, 1);
}
// получить флаг ladder / dead
public function GetLadderDeadFlag($charinfo = false)
{
// position
$pos = (!$charinfo) ? $pos = 36 : $pos = 180;
$flag = $this->getValue($pos, 1);
return $flag;
}
// bool (dead = true | live = false)
public function SetDead($bool = false, $charinfo = false)
{
switch ( ord( $this->GetLadderDeadFlag($charinfo) ) )
{
case 37:
$this->setLadderDeadFlag(0,0,$bool, $charinfo);
break;
case 44:
$this->setLadderDeadFlag(0,0,$bool, $charinfo);
break;
case 101:
$this->setLadderDeadFlag(0,1,$bool, $charinfo);
break;
case 108:
$this->setLadderDeadFlag(0,1,$bool, $charinfo);
break;
case 33:
$this->setLadderDeadFlag(1,0,$bool, $charinfo);
break;
case 40:
$this->setLadderDeadFlag(1,0,$bool, $charinfo);
break;
case 97:
$this->setLadderDeadFlag(1,1,$bool, $charinfo);
break;
case 104:
$this->setLadderDeadFlag(1,1,$bool, $charinfo);
break;
}
echo ord( $this->GetLadderDeadFlag($charinfo) );
return true;
}
// bool (ladder = true | nonladder = false)
public function SetLadder($bool, $charinfo = false)
{
switch ( ord( $this->GetLadderDeadFlag($charinfo) ) )
{
case 37:
$this->setLadderDeadFlag(0,$bool,0, $charinfo);
break;
case 44:
$this->setLadderDeadFlag(0,$bool,1, $charinfo);
break;
case 101:
$this->setLadderDeadFlag(0,$bool,0, $charinfo);
break;
case 108:
$this->setLadderDeadFlag(0,$bool,1, $charinfo);
break;
case 33:
$this->setLadderDeadFlag(1,$bool,0, $charinfo);
break;
case 40:
$this->setLadderDeadFlag(1,$bool,1, $charinfo);
break;
case 97:
$this->setLadderDeadFlag(1,$bool,0, $charinfo);
break;
case 104:
$this->setLadderDeadFlag(1,$bool,1, $charinfo);
break;
}
return true;
}
//число в строку(символы)
private function dec2str($int,$reverse=false)
{
$hex=dechex($int);
$arr=str_split($hex,2);
if ($reverse==true) $arr=array_reverse($arr);
$str="";
foreach ($arr as $key=>$value) {
$str.=chr(hexdec($value));
}
return $str;
}
//строку в hex
private function str2hex($str)
{
$hexStr='';
for ($i=0; $i