c2ephp

agents/COB/AgentBlock.php

Go to the documentation of this file.
00001 <?php
00002 
00003 require_once(dirname(__FILE__).'/COBBlock.php');
00004 require_once(dirname(__FILE__).'/../../sprites/SpriteFrame.php');
00005 require_once(dirname(__FILE__).'/../../sprites/S16Frame.php');
00006 require_once(dirname(__FILE__).'/../../sprites/SPRFrame.php');
00007 
00015 define('DEPENDENCY_SPRITE','sprite');
00017 define('DEPENDENCY_SOUND','sound');
00019 
00020 
00022 
00026 class COBAgentBlock extends COBBlock {
00028 
00029     private $agentName;
00030     private $agentDescription;
00031 
00032     private $lastUsageDate; //unix timestamp
00033     private $reuseInterval; // seconds
00034     private $quantityAvailable;
00035     private $expiryDate; //unix timestamp
00036 
00037     //'reserved' - never officially used by CL
00038     private $reserved1;
00039     private $reserved2;
00040     private $reserved3;
00041 
00042     private $dependencies = array();
00043     private $thumbnail; // SpriteFrame
00044 
00045     private $installScript;
00046     private $removeScript;
00047     private $eventScripts;
00049 
00051 
00057     public function COBAgentBlock($agentName,$agentDescription) {
00058         parent::COBBlock(COB_BLOCK_AGENT);
00059         $this->agentName = $agentName;
00060         $this->agentDescription = $agentDescription;
00061     }
00063 
00065     public function GetAgentName() {
00066         return $this->agentName;
00067     }
00069 
00071     public function GetAgentDescription() {
00072         return $this->agentDescription;
00073     }
00075 
00077     public function GetInstallScript() {
00078         return $this->installScript;
00079     }
00081 
00083     public function GetRemoveScript() {
00084         return $this->removeScript;
00085     }
00087 
00089     public function GetEventScriptCount() {
00090         return sizeof($this->eventScripts);
00091     }
00093 
00095     public function GetEventScripts() {
00096         return $this->eventScripts;
00097     }
00099 
00103     public function GetEventScript($whichScript) {
00104         return $this->eventScripts[$whichScript];
00105     }
00107 
00109     public function GetThumbnail() {
00110         return $this->thumbnail;
00111     }
00113 
00118     public function GetDependencies($type=null) {
00119         $dependenciesToReturn = array();
00120         foreach($this->dependencies as $dependency) {
00121             if($type == null || $type == $dependency->GetType()) {
00122                 $dependenciesToReturn[] = $dependency;
00123             }
00124         }
00125         return $dependenciesToReturn;
00126     }
00128 
00132     public function GetReserved1() {
00133         return $this->reserved1;
00134     }
00136 
00140     public function GetReserved2() {
00141         return $this->reserved2;
00142     }
00144 
00148     public function GetReserved3() {
00149         return $this->reserved3;
00150     }
00152 
00155     public function AddDependency(COBDependency $dependency) { 
00156         if(!in_array($dependency->GetDependencyName(),$this->dependencies)) {
00157             $this->dependencies[] = $dependency;
00158         }
00159     }
00161 
00164     public function SetInstallScript($installScript) {
00165         $this->installScript = $installScript;
00166     }
00168 
00171     public function SetRemoveScript($removeScript) {
00172         $this->removeScript = $removeScript;
00173     }
00175 
00178     public function AddEventScript($eventScript) {
00179         $this->eventScripts[] = $eventScript;
00180     }
00182 
00185     public function SetLastUsageDate($time) {
00186         if($time > time()) {
00187             return false;
00188         } else {
00189             $this->lastUsageDate = $time;
00190         }
00191     }
00193 
00196     public function SetExpiryDate($time) {
00197         $this->expiryDate = $time;
00198     }
00200 
00203     public function SetQuantityAvailable($quantity) {
00204         $this->quantityAvailable = $quantity;
00205     }
00207 
00209     public function SetReuseInterval($interval) {
00210         $this->reuseInterval = $interval;
00211     }
00213 
00220     public function SetReserved($reserved1,$reserved2,$reserved3) {
00221         $this->reserved1 = $reserved1;
00222         $this->reserved2 = $reserved2;
00223         $this->reserved3 = $reserved3;
00224     }
00226 
00228     public function SetThumbnail(SpriteFrame $frame) {
00229         if($this->thumbnail != null) {
00230             throw new Exception('Thumbnail already added');
00231         }
00232         $this->thumbnail = $frame;
00233     }
00235 
00239     public function AddC1RemoveScriptFromRCB(IReader $reader) {
00240         if($this->removeScript != '') {
00241             throw new Exception('Script already added!');
00242         }
00243         $rcb = new COB($reader);
00244         $ablocks = $rcb->GetBlocks(COB_BLOCK_AGENT);
00245         $this->removeScript = $ablocks[0]->GetInstallScript();
00246     }
00247 
00249 
00254     public static function CreateFromReaderC2(IReader $reader) {
00255         $quantityAvailable = $reader->ReadInt(2);
00256         if($quantityAvailable == 0xffff) {
00257             $quantityAvailable = -1;
00258         }       
00259         $lastUsageDate = $reader->ReadInt(4);
00260         $reuseInterval = $reader->ReadInt(4);
00261 
00262         $expiryDay = $reader->ReadInt(1);
00263         $expiryMonth = $reader->ReadInt(1);
00264         $expiryYear = $reader->ReadInt(2);
00265         $expiryDate = mktime(0,0,0,$expiryMonth,$expiryDay,$expiryYear);
00266 
00267         $reserved = array($reader->ReadInt(4),$reader->ReadInt(4),$reader->ReadInt(4));
00268 
00269         $agentName = $reader->ReadCString();
00270         $agentDescription = $reader->ReadCString();
00271 
00272         $installScript = str_replace(',',"\n",$reader->ReadCString());
00273         $removeScript = str_replace(',',"\n",$reader->ReadCString());
00274 
00275         $numEventScripts = $reader->ReadInt(2);
00276 
00277         $eventScripts = array();
00278 
00279         for($i=0;$i<$numEventScripts;$i++) {
00280             $eventScripts[] = str_replace(',',"\n",$reader->ReadCString());
00281         }
00282         $numDependencies = $reader->ReadInt(2);
00283         $dependencies = array();
00284 
00285         for($i=0;$i<$numDependencies;$i++) {
00286             $type = ($reader->ReadInt(2) == 0)?DEPENDENCY_SPRITE:DEPENDENCY_SOUND;
00287             $name = $reader->ReadCString();
00288             $dependencies[] = new COBDependency($type,$name);
00289         }
00290         $thumbWidth = $reader->ReadInt(2);
00291         $thumbHeight = $reader->ReadInt(2);
00292 
00293         $thumbnail = new S16Frame($reader,'565',$thumbWidth,$thumbHeight,$reader->GetPosition());
00294         $reader->Skip($thumbHeight*$thumbWidth*2);
00295 
00296         //parsing finished, onto making an AgentBlock.
00297         $agentBlock = new COBAgentBlock($agentName,$agentDescription);
00298         $agentBlock->AddQuantityAvailable($quantityAvailable);
00299         $agentBlock->AddReuseInterval($reuseInterval);
00300         $agentBlock->AddExpiryDate($expiryDate);
00301         $agentBlock->AddLastUsageDate($lastUsageDate);
00302         $agentBlock->AddReserved($reserved[0],$reserved[1],$reserved[2]);
00303         $agentBlock->AddInstallScript($installScript);
00304         $agentBlock->AddRemoveScript($removeScript);
00305         foreach($eventScripts as $eventScript) {
00306             $agentBlock->AddEventScript($eventScript);
00307         }
00308         foreach($dependencies as $dependency) {
00309             $agentBlock->AddDependency($dependency);
00310         }
00311         $agentBlock->AddThumbnail($thumbnail);
00312         return $agentBlock;
00313 
00314     }
00316 
00321     public static function CreateFromReaderC1(IReader $reader) {
00322         $quantityAvailable  = $reader->ReadInt(2);
00323         $expires_month = $reader->ReadInt(4);
00324         $expires_day = $reader->ReadInt(4);
00325         $expires_year = $reader->ReadInt(4);
00326         $expiryDate = mktime(0,0,0,$expires_month,$expires_day,$expires_year);
00327 
00328         $numObjectScripts = $reader->ReadInt(2);
00329         $numInstallScripts = $reader->ReadInt(2);
00330         $quantityUsed = $reader->ReadInt(4);
00331         $objectScripts = array();
00332         for($i=0;$i<$numObjectScripts;$i++) {
00333             $scriptsize = $reader->ReadInt(1);
00334             if($scriptsize == 255) {
00335                 $scriptsize = $reader->ReadInt(2);
00336             }
00337             $objectScripts[$i] = $reader->Read($scriptsize);
00338         }
00339         $installScripts = array();
00340         for($i=0;$i<$numInstallScripts;$i++) {
00341             $scriptsize = $reader->ReadInt(1);
00342             if($scriptsize == 255) {
00343                 $scriptsize = $reader->ReadInt(2);
00344             }
00345             $installScripts[$i] = $reader->Read($scriptsize);
00346         }
00347         $pictureWidth = $reader->ReadInt(4);
00348         $pictureHeight = $reader->ReadInt(4);
00349         $unknown = $reader->ReadInt(2);
00350         $sprframe = null;
00351         if($pictureWidth > 0 || $pictureHeight > 0) {
00352             $sprframe = new SPRFrame($reader,$pictureWidth,$pictureHeight);
00353             $sprframe->Flip(); 
00354         }
00355 
00356         $agentName = $reader->Read($reader->ReadInt(1));
00357 
00358         $agentBlock = new COBAgentBlock($agentName,'');
00359         $agentBlock->AddQuantityAvailable($quantityAvailable);
00360         $agentBlock->AddExpiryDate($expiryDate);
00361         if($sprframe != null) {
00362             $agentBlock->AddThumbnail($sprframe);
00363         }
00364         foreach($objectScripts as $objectScript) {
00365             $agentBlock->AddEventScript($objectScript);
00366         }
00367         $agentBlock->AddInstallScript(implode("\n*c2ephp Install script seperator\n",$installScripts));
00368         return $agentBlock;
00369     }
00371 }
00372 
00374 class COBDependency {
00376 
00377     private $type;
00378     private $name;
00379 
00381 
00383 
00386     public function COBDependency($type,$name) {
00387         $this->type = $type;
00388         $this->name = $name;
00389     }
00391 
00393     public function GetDependencyType() {
00394         return $this->type;
00395     }
00397 
00399     public function GetDependencyName() {
00400         return $this->name;
00401     }
00402 }
00403 ?>
 All Classes Files Functions Variables Enumerations