PMF Plugin Reading

This commit is contained in:
Shoghi Cervantes Pueyo
2013-03-16 12:27:23 +01:00
parent 6dfe348767
commit 1bfef261ab
4 changed files with 73 additions and 5 deletions

128
src/pmf/PMF.php Normal file
View File

@@ -0,0 +1,128 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
define("PMF_CURRENT_VERSION", 0x01);
class PMF{
protected $fp;
private $file;
private $version;
private $type;
public function __construct($file, $new = false, $type = 0, $version = PMF_CURRENT_VERSION){
if($new === true){
$this->create($file, $type, $version);
}else{
if($this->load($file) !== true){
$this->parseInfo();
}
}
}
public function getVersion(){
return $this->version;
}
public function getType(){
return $this->type;
}
public function load($file){
$this->close();
$this->file = realpath($file);
if(($this->fp = @fopen($file, "c+b")) !== false){
$stat = fstat($this->fp);
if($stat["size"] >= 5){ //Header + 2 Bytes
return true;
}
fclose($this->fp);
}
return false;
}
public function parseInfo(){
if(fread($this->fp, 3) !== "PMF"){
return false;
}
$this->seek(0);
$this->version = ord($this->read(1));
switch($this->version){
case 0x01:
$this->type = ord($this->read(1));
break;
default:
console("[ERROR] Tried loading non-supported PMF version ".$this->version." on file ".$this->file);
return false;
}
return true;
}
public function getFile(){
return $this->file;
}
public function close(){
unset($this->version, $this->type, $this->file);
fclose($this->fp);
}
public function create($file, $type, $version = PMF_CURRENT_VERSION){
$this->file = realpath($file);
if(!is_resource($this->fp)){
if(($this->fp = @fopen($file, "c+b")) === false){
return false;
}
}
$this->seek(0);
$this->write("PMF" . chr((int) $type) . chr((int) $version));
}
public function read($length){
if($length <= 0){
return "";
}
if(is_resource($this->fp)){
return fread($this->fp, (int) $length);
}
return false;
}
public function write($string, $length = false){
if(is_resource($this->fp)){
return ($length === false ? fwrite($this->fp, $string) : fwrite($this->fp, $string, $length));
}
return false;
}
public function seek($offset, $whence = SEEK_SET){
if(is_resource($this->fp)){
return fseek($this->fp, (int) $offset, (int) $whence);
}
return false;
}
}

61
src/pmf/Plugin.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
/***REM_START***/
require_once(FILE_PATH."/src/pmf/PMF.php");
/***REM_END***/
define("PMF_CURRENT_PLUGIN_VERSION", 0x00);
class PMFPlugin extends PMF{
private $pluginData = array();
public function __construct($file){
$this->load($file);
$this->parseInfo();
$this->parsePlugin();
}
protected function parsePlugin(){
if($this->type !== 0x01){
return false;
}
$this->pluginData["fversion"] = ord($this->read(1));
$this->pluginData["name"] = $this->read(Utils::readShort($this->read(2), false));
$this->pluginData["version"] = $this->read(Utils::readShort($this->read(2), false));
$this->pluginData["author"] = $this->read(Utils::readShort($this->read(2), false));
$this->pluginData["class"] = $this->read(Utils::readShort($this->read(2), false));
$this->pluginData["identifier"] = $this->read(Utils::readShort($this->read(2), false)); //Will be used to check for updates
$this->pluginData["extra"] = $this->read(Utils::readShort($this->read(2), false)); //Additional custom plugin data
$this->pluginData["code"] = "";
while(!feof($this->fp)){
$this->pluginData["code"] .= fread($this->fp, 4096);
}
$this->pluginData["code"] = gzinflate($this->pluginData["code"]);
}
}