@@ -1,24 +1,24 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | function DeArchive($data) { |
| 3 | - if(is_string($data)) { |
|
| 4 | - if(substr($data,0,55) == "Creatures Evolution Engine - Archived information file.") { |
|
| 5 | - $data = substr($data,strpos($data,chr(0x1A).chr(0x04))+2); |
|
| 6 | - $data = gzuncompress($data); |
|
| 7 | - return $data; |
|
| 8 | - } |
|
| 9 | - die('Couldn\'t dearchive -- Probably invalid file'); |
|
| 10 | - return false; |
|
| 11 | - } else if(is_resource($data)) { |
|
| 12 | - return false; //coming soon |
|
| 13 | - } |
|
| 3 | + if(is_string($data)) { |
|
| 4 | + if(substr($data,0,55) == "Creatures Evolution Engine - Archived information file.") { |
|
| 5 | + $data = substr($data,strpos($data,chr(0x1A).chr(0x04))+2); |
|
| 6 | + $data = gzuncompress($data); |
|
| 7 | + return $data; |
|
| 8 | + } |
|
| 9 | + die('Couldn\'t dearchive -- Probably invalid file'); |
|
| 10 | + return false; |
|
| 11 | + } else if(is_resource($data)) { |
|
| 12 | + return false; //coming soon |
|
| 13 | + } |
|
| 14 | 14 | } |
| 15 | 15 | function Archive($data,$filehandle=null) { |
| 16 | - if(is_resource($filehandle)) { |
|
| 17 | - return false; |
|
| 18 | - } |
|
| 19 | - $data = gzcompress($data); |
|
| 20 | - $data = "Creatures Evolution Engine - Archived information file. zLib 1.13 compressed.".chr(0x1A).chr(0x04).$data; |
|
| 21 | - return $data; |
|
| 16 | + if(is_resource($filehandle)) { |
|
| 17 | + return false; |
|
| 18 | + } |
|
| 19 | + $data = gzcompress($data); |
|
| 20 | + $data = "Creatures Evolution Engine - Archived information file. zLib 1.13 compressed.".chr(0x1A).chr(0x04).$data; |
|
| 21 | + return $data; |
|
| 22 | 22 | |
| 23 | 23 | } |
| 24 | 24 | ?> |
| 25 | 25 | \ No newline at end of file |
@@ -21,12 +21,15 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public function FileReader($filename) |
| 23 | 23 | { |
| 24 | - if(!file_exists($filename)) |
|
| 25 | - throw new Exception("File does not exist: ".$filename); |
|
| 26 | - if(!is_file($filename)) |
|
| 27 | - throw new Exception("Target is not a file."); |
|
| 28 | - if(!is_readable($filename)) |
|
| 29 | - throw new Exception("File exists, but is not readable."); |
|
| 24 | + if(!file_exists($filename)) { |
|
| 25 | + throw new Exception("File does not exist: ".$filename); |
|
| 26 | + } |
|
| 27 | + if(!is_file($filename)) { |
|
| 28 | + throw new Exception("Target is not a file."); |
|
| 29 | + } |
|
| 30 | + if(!is_readable($filename)) { |
|
| 31 | + throw new Exception("File exists, but is not readable."); |
|
| 32 | + } |
|
| 30 | 33 | |
| 31 | 34 | $this->fp = fopen($filename, 'rb'); |
| 32 | 35 | } |
@@ -45,8 +48,9 @@ discard block |
||
| 45 | 48 | for($x = 0; $x < $count; $x++) |
| 46 | 49 | { |
| 47 | 50 | $buffer = (ord(fgetc($this->fp)) << ($x * 8)); |
| 48 | - if($buffer === false) |
|
| 49 | - throw new Exception("Read failure"); |
|
| 51 | + if($buffer === false) { |
|
| 52 | + throw new Exception("Read failure"); |
|
| 53 | + } |
|
| 50 | 54 | $int += $buffer; |
| 51 | 55 | } |
| 52 | 56 | return $int; |
@@ -4,16 +4,16 @@ |
||
| 4 | 4 | class PRAYFileTest extends c2ephpAbstractTestCase { |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | - * @dataProvider createAgentFiles |
|
| 8 | - */ |
|
| 7 | + * @dataProvider createAgentFiles |
|
| 8 | + */ |
|
| 9 | 9 | public function testCreatePRAYFile(PRAYFile $prayfile) { |
| 10 | 10 | $this->assertInstanceOf('PRAYFile',$prayfile); |
| 11 | 11 | $this->assertNotNull($prayfile); |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | - * @dataProvider createAgentFiles |
|
| 16 | - */ |
|
| 15 | + * @dataProvider createAgentFiles |
|
| 16 | + */ |
|
| 17 | 17 | public function testNumberOfBlocks(PRAYFile $prayfile,$fixtureInfo) { |
| 18 | 18 | $this->assertNotNull($prayfile); |
| 19 | 19 | $this->assertEquals($fixtureInfo['block count'],sizeof($prayfile->GetBlocks())); |
@@ -34,8 +34,9 @@ |
||
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | $buffer = $reader->ReadInt(2); |
| 37 | - if($buffer < 1) |
|
| 38 | - throw new Exception('Sprite file appears to contain less than 1 frame.'); |
|
| 37 | + if($buffer < 1) { |
|
| 38 | + throw new Exception('Sprite file appears to contain less than 1 frame.'); |
|
| 39 | + } |
|
| 39 | 40 | $frameCount = $buffer; |
| 40 | 41 | for($x=0; $x < $frameCount; $x++) |
| 41 | 42 | { |
@@ -89,8 +89,7 @@ |
||
| 89 | 89 | $red = ($pixel & 0xF800) >> 8; |
| 90 | 90 | $green = ($pixel & 0x07E0) >> 3; |
| 91 | 91 | $blue = ($pixel & 0x001F) << 3; |
| 92 | - } |
|
| 93 | - else if($this->encoding == "555") |
|
| 92 | + } else if($this->encoding == "555") |
|
| 94 | 93 | { |
| 95 | 94 | $red = ($pixel & 0x7C00) >> 7; |
| 96 | 95 | $green = ($pixel & 0x03E0) >> 2; |
@@ -75,10 +75,11 @@ discard block |
||
| 75 | 75 | for($x = 0; $x < $this->GetWidth();) |
| 76 | 76 | { |
| 77 | 77 | $run = $this->reader->ReadInt(2); |
| 78 | - if(($run & 0x0001) > 0) |
|
| 79 | - $run_type = "colour"; |
|
| 80 | - else |
|
| 81 | - $run_type = "black"; |
|
| 78 | + if(($run & 0x0001) > 0) { |
|
| 79 | + $run_type = "colour"; |
|
| 80 | + } else { |
|
| 81 | + $run_type = "black"; |
|
| 82 | + } |
|
| 82 | 83 | $run_length = ($run & 0x7FFF) >> 1; |
| 83 | 84 | if($run_type == "black") |
| 84 | 85 | { |
@@ -87,8 +88,7 @@ discard block |
||
| 87 | 88 | { |
| 88 | 89 | imagesetpixel($image, $x, $y, imagecolorallocate($image, 0, 0, 0)); |
| 89 | 90 | } |
| 90 | - } |
|
| 91 | - else //colour run |
|
| 91 | + } else //colour run |
|
| 92 | 92 | { |
| 93 | 93 | $z = $x + $run_length; |
| 94 | 94 | for(;$x < $z; $x++) |
@@ -99,8 +99,7 @@ discard block |
||
| 99 | 99 | $red = ($pixel & 0xF800) >> 8; |
| 100 | 100 | $green = ($pixel & 0x07E0) >> 3; |
| 101 | 101 | $blue = ($pixel & 0x001F) << 3; |
| 102 | - } |
|
| 103 | - else if($this->encoding == "555") |
|
| 102 | + } else if($this->encoding == "555") |
|
| 104 | 103 | { |
| 105 | 104 | $red = ($pixel & 0x7C00) >> 7; |
| 106 | 105 | $green = ($pixel & 0x03E0) >> 2; |
@@ -110,8 +109,9 @@ discard block |
||
| 110 | 109 | imagesetpixel($image, $x, $y, $colour); |
| 111 | 110 | } |
| 112 | 111 | } |
| 113 | - if($x == $this->GetWidth()) |
|
| 114 | - $this->reader->Skip(2); |
|
| 112 | + if($x == $this->GetWidth()) { |
|
| 113 | + $this->reader->Skip(2); |
|
| 114 | + } |
|
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | 117 | $this->gdImage = $image; |
@@ -15,31 +15,31 @@ |
||
| 15 | 15 | * Reads in the IReader and creates SPRFrames as required. |
| 16 | 16 | * @param $reader An IReader to read from. |
| 17 | 17 | */ |
| 18 | - public function SPRFile(IReader $reader) { |
|
| 19 | - parent::SpriteFile('SPR'); |
|
| 20 | - $frameCount = $reader->ReadInt(2); |
|
| 18 | + public function SPRFile(IReader $reader) { |
|
| 19 | + parent::SpriteFile('SPR'); |
|
| 20 | + $frameCount = $reader->ReadInt(2); |
|
| 21 | 21 | |
| 22 | - for($i=0;$i<$frameCount;$i++) { |
|
| 23 | - $offset = $reader->ReadInt(4); |
|
| 24 | - $width = $reader->ReadInt(2); |
|
| 25 | - $height = $reader->ReadInt(2); |
|
| 26 | - $this->AddFrame(new SPRFrame($reader,$width,$height,$offset)); |
|
| 27 | - } |
|
| 28 | - } |
|
| 22 | + for($i=0;$i<$frameCount;$i++) { |
|
| 23 | + $offset = $reader->ReadInt(4); |
|
| 24 | + $width = $reader->ReadInt(2); |
|
| 25 | + $height = $reader->ReadInt(2); |
|
| 26 | + $this->AddFrame(new SPRFrame($reader,$width,$height,$offset)); |
|
| 27 | + } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | /// @brief Compiles the SPR file into a binary string |
| 31 | - public function Compile() { |
|
| 32 | - $data = pack('v',$this->GetFrameCount()); |
|
| 33 | - $offset = 2+(8*$this->GetFrameCount()); |
|
| 34 | - foreach($this->GetFrames() as $frame) { |
|
| 35 | - $data .= pack('V',$offset); |
|
| 36 | - $data .= pack('vv',$frame->GetWidth(),$frame->GetHeight()); |
|
| 37 | - $offset += $frame->GetWidth()*$frame->GetHeight(); |
|
| 38 | - } |
|
| 39 | - foreach($this->GetFrames() as $frame) { |
|
| 40 | - $data .= $frame->Encode(); |
|
| 41 | - } |
|
| 42 | - return $data; |
|
| 43 | - } |
|
| 31 | + public function Compile() { |
|
| 32 | + $data = pack('v',$this->GetFrameCount()); |
|
| 33 | + $offset = 2+(8*$this->GetFrameCount()); |
|
| 34 | + foreach($this->GetFrames() as $frame) { |
|
| 35 | + $data .= pack('V',$offset); |
|
| 36 | + $data .= pack('vv',$frame->GetWidth(),$frame->GetHeight()); |
|
| 37 | + $offset += $frame->GetWidth()*$frame->GetHeight(); |
|
| 38 | + } |
|
| 39 | + foreach($this->GetFrames() as $frame) { |
|
| 40 | + $data .= $frame->Encode(); |
|
| 41 | + } |
|
| 42 | + return $data; |
|
| 43 | + } |
|
| 44 | 44 | } |
| 45 | 45 | ?> |
@@ -3,110 +3,110 @@ |
||
| 3 | 3 | /// @brief Superclass for all SpriteFile types |
| 4 | 4 | abstract class SpriteFile { |
| 5 | 5 | |
| 6 | - /// @cond INTERNAL_DOCS |
|
| 6 | + /// @cond INTERNAL_DOCS |
|
| 7 | 7 | |
| 8 | - private $frames = array(); |
|
| 9 | - private $spritefiletype; |
|
| 10 | - /// @endcond |
|
| 8 | + private $frames = array(); |
|
| 9 | + private $spritefiletype; |
|
| 10 | + /// @endcond |
|
| 11 | 11 | |
| 12 | - /// @cond INTERNAL_DOCS |
|
| 12 | + /// @cond INTERNAL_DOCS |
|
| 13 | 13 | |
| 14 | - public function SpriteFile($filetype) { |
|
| 14 | + public function SpriteFile($filetype) { |
|
| 15 | 15 | $this->spritefiletype = $filetype; |
| 16 | - } |
|
| 17 | - /// @endcond |
|
| 16 | + } |
|
| 17 | + /// @endcond |
|
| 18 | 18 | |
| 19 | - /// @brief Gets a SpriteFrame from the SpriteFile. |
|
| 20 | - /** |
|
| 21 | - * @param $frame The 0-based index of the frame to get. |
|
| 22 | - * @return A SpriteFrame |
|
| 23 | - */ |
|
| 24 | - public function GetFrame($frame) { |
|
| 25 | - return $this->frames[$frame]; |
|
| 26 | - } |
|
| 27 | - /// @brief Gets the entire frame array. |
|
| 28 | - /** |
|
| 29 | - * @return An array of SpriteFrames |
|
| 30 | - */ |
|
| 31 | - public function GetFrames() { |
|
| 32 | - return $this->frames; |
|
| 33 | - } |
|
| 19 | + /// @brief Gets a SpriteFrame from the SpriteFile. |
|
| 20 | + /** |
|
| 21 | + * @param $frame The 0-based index of the frame to get. |
|
| 22 | + * @return A SpriteFrame |
|
| 23 | + */ |
|
| 24 | + public function GetFrame($frame) { |
|
| 25 | + return $this->frames[$frame]; |
|
| 26 | + } |
|
| 27 | + /// @brief Gets the entire frame array. |
|
| 28 | + /** |
|
| 29 | + * @return An array of SpriteFrames |
|
| 30 | + */ |
|
| 31 | + public function GetFrames() { |
|
| 32 | + return $this->frames; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /// @brief Compiles the SpriteFile into a binary string |
|
| 36 | - /** |
|
| 37 | - * @return A binary string containing the SpriteFile's data and frames. |
|
| 38 | - */ |
|
| 39 | - public abstract function Compile(); |
|
| 35 | + /// @brief Compiles the SpriteFile into a binary string |
|
| 36 | + /** |
|
| 37 | + * @return A binary string containing the SpriteFile's data and frames. |
|
| 38 | + */ |
|
| 39 | + public abstract function Compile(); |
|
| 40 | 40 | |
| 41 | - /// @brief Adds a SpriteFrame to the SpriteFile |
|
| 42 | - /** |
|
| 43 | - * If necessary, this function converts the SpriteFrame to the |
|
| 44 | - * correct format. |
|
| 45 | - * At the moment, this can only add a SpriteFrame to the end of the |
|
| 46 | - * SpriteFile. TODO: I aim to fix this by the CCSF 2011. |
|
| 47 | - * @internal |
|
| 48 | - * This process uses some magic which require all types of |
|
| 49 | - * SpriteFile and SpriteFrame to use 3-character identifiers. |
|
| 50 | - * This means that if you want to make your own sprite formats |
|
| 51 | - * you'll need to override this function and provide all our magic |
|
| 52 | - * plus your own. |
|
| 53 | - * @endinternal |
|
| 54 | - * @param $frame A SpriteFrame |
|
| 55 | - * @param $position Where to put the frame. Currently un-used. |
|
| 56 | - */ |
|
| 57 | - public function AddFrame(SpriteFrame $frame, $position=false) { |
|
| 58 | - /* |
|
| 41 | + /// @brief Adds a SpriteFrame to the SpriteFile |
|
| 42 | + /** |
|
| 43 | + * If necessary, this function converts the SpriteFrame to the |
|
| 44 | + * correct format. |
|
| 45 | + * At the moment, this can only add a SpriteFrame to the end of the |
|
| 46 | + * SpriteFile. TODO: I aim to fix this by the CCSF 2011. |
|
| 47 | + * @internal |
|
| 48 | + * This process uses some magic which require all types of |
|
| 49 | + * SpriteFile and SpriteFrame to use 3-character identifiers. |
|
| 50 | + * This means that if you want to make your own sprite formats |
|
| 51 | + * you'll need to override this function and provide all our magic |
|
| 52 | + * plus your own. |
|
| 53 | + * @endinternal |
|
| 54 | + * @param $frame A SpriteFrame |
|
| 55 | + * @param $position Where to put the frame. Currently un-used. |
|
| 56 | + */ |
|
| 57 | + public function AddFrame(SpriteFrame $frame, $position=false) { |
|
| 58 | + /* |
|
| 59 | 59 | if($position === false) { |
| 60 | 60 | $position = sizeof($this->frames); |
| 61 | 61 | } else if($position < 0) { |
| 62 | 62 | $position = sizeof($this->frames) - $position; |
| 63 | 63 | } |
| 64 | 64 | */ |
| 65 | - if($this->spritefiletype == substr(get_class($frame),0,3)) { |
|
| 66 | - //$this->frames[$position] = $frame; |
|
| 67 | - $this->frames[] = $frame; |
|
| 68 | - } else { |
|
| 69 | - //$this->frames[$position] = $frame->ToSpriteFrame($this->spritefiletype); |
|
| 70 | - $this->frames[] = $frame->ToSpriteFrame($this->spritefiletype); |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - /// @brief Replaces a frame in the SpriteFile |
|
| 74 | - /** |
|
| 75 | - * Replaces the frame in the given position |
|
| 76 | - * Uses the same magic as AddFrame |
|
| 77 | - * @param $frame A SpriteFrame of any type. |
|
| 78 | - * @param $position Which frame to replace. If negative, counts |
|
| 79 | - * backwards from the end of the frames array. |
|
| 80 | - */ |
|
| 81 | - public function ReplaceFrame(SpriteFrame $frame, $position) { |
|
| 82 | - if($position < 0) { |
|
| 83 | - $position = sizeof($this->frames) - $position; |
|
| 84 | - } |
|
| 85 | - $this->frames[$position] = $frame->ToSpriteFrame($this->spritefiletype); |
|
| 86 | - } |
|
| 87 | - /// @brief Gets the number of frames currently stored in this SpriteFile. |
|
| 88 | - /** |
|
| 89 | - * @return The number of frames |
|
| 90 | - */ |
|
| 91 | - public function GetFrameCount() { |
|
| 92 | - return sizeof($this->frames); |
|
| 93 | - } |
|
| 94 | - /// @brief Deletes the frame in the given position. |
|
| 95 | - /** |
|
| 96 | - * @param $frame The 0-based index of the frame to delete. |
|
| 97 | - */ |
|
| 98 | - public function DeleteFrame($frame) { |
|
| 99 | - unset($this->frames[$frame]); |
|
| 100 | - } |
|
| 101 | - /// @brief Converts the given frame to PNG. <strong>Deprecated.</strong> |
|
| 102 | - /** |
|
| 103 | - * May be removed in a future release. |
|
| 104 | - * Use GetFrame($frame)->ToPNG() instead. |
|
| 105 | - * @param $frame The 0-based index of the frame to delete. |
|
| 106 | - * @return A binary string containing a PNG. |
|
| 107 | - */ |
|
| 108 | - public function ToPNG($frame) { |
|
| 109 | - return $this->frames[$frame]->ToPNG(); |
|
| 110 | - } |
|
| 65 | + if($this->spritefiletype == substr(get_class($frame),0,3)) { |
|
| 66 | + //$this->frames[$position] = $frame; |
|
| 67 | + $this->frames[] = $frame; |
|
| 68 | + } else { |
|
| 69 | + //$this->frames[$position] = $frame->ToSpriteFrame($this->spritefiletype); |
|
| 70 | + $this->frames[] = $frame->ToSpriteFrame($this->spritefiletype); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + /// @brief Replaces a frame in the SpriteFile |
|
| 74 | + /** |
|
| 75 | + * Replaces the frame in the given position |
|
| 76 | + * Uses the same magic as AddFrame |
|
| 77 | + * @param $frame A SpriteFrame of any type. |
|
| 78 | + * @param $position Which frame to replace. If negative, counts |
|
| 79 | + * backwards from the end of the frames array. |
|
| 80 | + */ |
|
| 81 | + public function ReplaceFrame(SpriteFrame $frame, $position) { |
|
| 82 | + if($position < 0) { |
|
| 83 | + $position = sizeof($this->frames) - $position; |
|
| 84 | + } |
|
| 85 | + $this->frames[$position] = $frame->ToSpriteFrame($this->spritefiletype); |
|
| 86 | + } |
|
| 87 | + /// @brief Gets the number of frames currently stored in this SpriteFile. |
|
| 88 | + /** |
|
| 89 | + * @return The number of frames |
|
| 90 | + */ |
|
| 91 | + public function GetFrameCount() { |
|
| 92 | + return sizeof($this->frames); |
|
| 93 | + } |
|
| 94 | + /// @brief Deletes the frame in the given position. |
|
| 95 | + /** |
|
| 96 | + * @param $frame The 0-based index of the frame to delete. |
|
| 97 | + */ |
|
| 98 | + public function DeleteFrame($frame) { |
|
| 99 | + unset($this->frames[$frame]); |
|
| 100 | + } |
|
| 101 | + /// @brief Converts the given frame to PNG. <strong>Deprecated.</strong> |
|
| 102 | + /** |
|
| 103 | + * May be removed in a future release. |
|
| 104 | + * Use GetFrame($frame)->ToPNG() instead. |
|
| 105 | + * @param $frame The 0-based index of the frame to delete. |
|
| 106 | + * @return A binary string containing a PNG. |
|
| 107 | + */ |
|
| 108 | + public function ToPNG($frame) { |
|
| 109 | + return $this->frames[$frame]->ToPNG(); |
|
| 110 | + } |
|
| 111 | 111 | } |
| 112 | 112 | ?> |
@@ -101,8 +101,9 @@ |
||
| 101 | 101 | * if it does't already. |
| 102 | 102 | */ |
| 103 | 103 | protected function EnsureDecoded() { |
| 104 | - if(!$this->decoded) |
|
| 105 | - $this->Decode(); |
|
| 104 | + if(!$this->decoded) { |
|
| 105 | + $this->Decode(); |
|
| 106 | + } |
|
| 106 | 107 | |
| 107 | 108 | $this->decoded = true; |
| 108 | 109 | } |