c2ephp

agents/PRAY/PrayBlock.php

Go to the documentation of this file.
00001 <?php
00005 require_once(dirname(__FILE__).'/AGNTBlock.php');
00006 require_once(dirname(__FILE__).'/CREABlock.php');
00007 require_once(dirname(__FILE__).'/DFAMBlock.php');
00008 require_once(dirname(__FILE__).'/DSAGBlock.php');
00009 require_once(dirname(__FILE__).'/DSEXBlock.php');
00010 require_once(dirname(__FILE__).'/EGGSBlock.php');
00011 require_once(dirname(__FILE__).'/EXPCBlock.php');
00012 require_once(dirname(__FILE__).'/FILEBlock.php');
00013 require_once(dirname(__FILE__).'/GENEBlock.php');
00014 require_once(dirname(__FILE__).'/GLSTBlock.php');
00015 require_once(dirname(__FILE__).'/LIVEBlock.php');
00016 require_once(dirname(__FILE__).'/PHOTBlock.php');
00017 require_once(dirname(__FILE__).'/SFAMBlock.php');
00018 
00019 
00027 define('PRAY_BLOCK_AGNT','AGNT'); 
00029 define('PRAY_BLOCK_CREA','CREA');
00031 define('PRAY_BLOCK_DFAM','DFAM');
00033 define('PRAY_BLOCK_DSAG','DSAG');
00035 define('PRAY_BLOCK_DSEX','DSEX');
00037 define('PRAY_BLOCK_EGGS' ,'EGGS' );
00039 define('PRAY_BLOCK_EXPC','EXPC');
00041 define('PRAY_BLOCK_FILE','FILE');
00043 define('PRAY_BLOCK_GENE','GENE');
00045 define('PRAY_BLOCK_GLST','GLST');
00047 define('PRAY_BLOCK_LIVE','LIVE');
00049 define('PRAY_BLOCK_PHOT','PHOT');
00051 define('PRAY_BLOCK_SFAM','SFAM');
00053 
00057 
00058 
00059 
00060 define('PRAY_FLAG_ZLIB_COMPRESSED',1);
00062 
00064 abstract class PrayBlock {
00066 
00067     private $prayfile;
00068     private $content;
00069     private $name;
00070     private $type;
00071     private $decompiled;
00072 
00074     //
00076 
00078 
00086     public function PrayBlock($prayfile,$name,$content,$flags,$type) {
00087         if(strlen($type) != 4) {
00088             throw new Exception('Invalid PRAY block type: '.$type);
00089         }
00090         if($prayfile instanceof PRAYFile) {
00091             $this->prayfile = $prayfile;
00092             $this->decompiled = false;
00093         } else {
00094             $this->prayfile = null;
00095             $this->decompiled = true;
00096         }
00097         $this->name = $name;
00098         $this->content = $content;
00099         $this->flags = $flags;
00100         $this->type = $type;
00101     }
00102 
00104 
00108     protected function EncodeBlockHeader($length,$uncompressedlength=false) {
00109         $compiled = $this->GetType();
00110         $compiled .= substr($this->GetName(),0,128);
00111         $len = 128 - strlen($this->GetName());
00112         
00113         for($i=0;$i<$len;$i++) {
00114             $compiled .= pack('x');
00115         }
00116         if($uncompressedlength === false) {
00117             $uncompressedlength = $length;
00118         }
00119         $compiled .= pack('VVV',$length,$uncompressedlength,$this->flags);
00120         return $compiled;
00121     }
00123 
00125 
00127 
00132     protected function PerformFlagOperations($data) {
00133         if($this->IsFlagSet(PRAY_FLAG_ZLIB_COMPRESSED)) {
00134             $data = gzcompress($data);
00135         }
00136         return $data;
00137     }
00138 
00140 
00142 
00144     public function GetName() {
00145         return $this->name;
00146     }
00148 
00153     public function GetData() {
00154         if($this->decompiled) {
00155             throw new Exception('Can\'t get data on a decompiled PRAYBlock. It must be compiled first');
00156             return;
00157         }
00158         if($this->IsFlagSet(PRAY_FLAG_ZLIB_COMPRESSED)) {
00159             $this->content = gzuncompress($this->content);
00160             $this->SetFlagsOff(PRAY_FLAG_ZLIB_COMPRESSED);
00161         }
00162         return $this->content;
00163     }
00165 
00170     public function GetType() {
00171         return $this->type;
00172     }
00174 
00179     public function GetFlags() {
00180         return $this->flags;
00181     }
00183 
00187     public function IsFlagSet($flag) {
00188         return (($this->flags & $flag) === $flag);
00189     }
00190 
00192 
00194 
00198     protected function GetLength() {
00199         return strlen($this->content);
00200     }
00202 
00206     protected function SetData($data) {
00207         $this->content = $data;
00208     }
00210 
00213     protected function GetPrayFile() {
00214         return $this->prayfile;
00215     }
00217 
00220     protected function SetFlagsOn($flags) {
00221         $this->flags = $this->flags | $flags;
00222     }
00224 
00227     protected function SetFlagsOff($flags) {
00228         $this->flags = $this->flags & ~$flags;
00229 
00230     }
00232 
00235     protected function EnsureDecompiled() {
00236         if($this->decompiled) {
00237             return;
00238         } else {
00239             $this->DecompileBlockData();
00240             $this->decompiled = true;
00241         }
00242     }
00243 
00245     // Started above GetLength.
00246 
00248 
00253     public function Compile() {
00254         if($this->decompiled) { 
00255             $data = $this->CompileBlockData();
00256             $uclength = strlen($data);
00257             $data = $this->PerformFlagOperations($data);
00258             $compiled = $this->EncodeBlockHeader(strlen($data),$uclength);
00259             $compiled .= $data;
00260             $this->content = $data;
00261             $this->decompiled = false;
00262             return $compiled; 
00263         } else {
00264             $data = $this->PerformFlagOperations($this->content);
00265             $compiled  = $this->EncodeBlockHeader(strlen($this->content),strlen($data));
00266             $compiled .= $this->content;
00267             return $compiled;
00268         }
00269     }
00270 
00272 
00274 
00277     protected abstract function CompileBlockData();
00279 
00285     protected abstract function DecompileBlockData();
00286 
00288 
00296     public static function MakePrayBlock($blocktype,PRAYFile $prayfile,$name,$content,$flags) {
00297         switch($blocktype) {
00298             //agents
00299         case PRAY_BLOCK_AGNT:
00300             return new AGNTBlock($prayfile,$name,$content,$flags);
00301         case PRAY_BLOCK_DSAG:
00302             return new DSAGBlock($prayfile,$name,$content,$flags);
00303         case PRAY_BLOCK_LIVE:
00304             return new LIVEBlock($prayfile,$name,$content,$flags); //sea monkeys agent files.
00305 
00306             //egg
00307         case PRAY_BLOCK_EGGS:
00308             return new EGGSBlock($prayfile,$name,$content,$flags);
00309 
00310             //starter families
00311         case PRAY_BLOCK_DFAM:
00312             return new DFAMBlock($prayfile,$name,$content,$flags);
00313         case PRAY_BLOCK_SFAM:
00314             return new SFAMBlock($prayfile,$name,$content,$flags);
00315 
00316             //exported creatures
00317         case PRAY_BLOCK_EXPC:
00318             return new EXPCBlock($prayfile,$name,$content,$flags);
00319         case PRAY_BLOCK_DSEX:
00320             return new DSEXBlock($prayfile,$name,$content,$flags);
00321 
00322         case PRAY_BLOCK_CREA:
00323             return new CREABlock($prayfile,$name,$content,$flags);
00324 
00325             //creature photos
00326         case PRAY_BLOCK_PHOT:
00327             return new PHOTBlock($prayfile,$name,$content,$flags);
00328 
00329             //creature history
00330         case PRAY_BLOCK_GLST:
00331             return new GLSTBlock($prayfile,$name,$content,$flags);
00332 
00333             //creature genetics
00334         case PRAY_BLOCK_GENE:
00335             return new GENEBlock($prayfile,$name,$content,$flags);
00336 
00337             //files
00338         case PRAY_BLOCK_FILE:
00339             return new FILEBlock($prayfile,$name,$content,$flags);
00340 
00341         default:
00342             return null;
00343         }
00344     }
00346 }
00347 ?>
 All Classes Files Functions Variables Enumerations