PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
PMF.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
25 namespace pocketmine\level\conversor\pmf;
26 
27 
29 
30 class PMF{
31  const VERSION = 0x01;
32 
33  protected $fp;
34  protected $file;
35  private $version;
36  private $type;
37 
38  public function __construct($file, $new = false, $type = 0, $version = PMF::VERSION){
39  if($new === true){
40  $this->create($file, $type, $version);
41  }else{
42  if($this->load($file) !== true){
43  $this->parseInfo();
44  }
45  }
46  }
47 
48  public function getVersion(){
49  return $this->version;
50  }
51 
52  public function getType(){
53  return $this->type;
54  }
55 
56  public function load($file){
57  $this->close();
58  $this->file = $file;
59  if(($this->fp = @fopen($file, "c+b")) !== false){
60  fseek($this->fp, 0, SEEK_END);
61  if(ftell($this->fp) >= 5){ //Header + 2 Bytes
62  @flock($this->fp, LOCK_EX);
63 
64  return true;
65  }
66  $this->close();
67  }
68 
69  return false;
70  }
71 
72  public function parseInfo(){
73  $this->seek(0);
74  if(fread($this->fp, 3) !== "PMF"){
75  return false;
76  }
77  $this->version = ord($this->read(1));
78  switch($this->version){
79  case 0x01:
80  $this->type = ord($this->read(1));
81  break;
82  default:
83  MainLogger::getLogger()->alert("Tried loading non-supported PMF version " . $this->version . " on file " . $this->file);
84 
85  return false;
86  }
87 
88  return true;
89  }
90 
91  public function getFile(){
92  return $this->file;
93  }
94 
95  public function close(){
96  unset($this->version, $this->type, $this->file);
97  if(is_object($this->fp)){
98  @flock($this->fp, LOCK_UN);
99  fclose($this->fp);
100  }
101  }
102 
103  public function create($file, $type, $version = PMF::VERSION){
104  $this->file = $file;
105  @mkdir(dirname($this->file), 0755, true);
106  if(!is_resource($this->fp)){
107  if(($this->fp = @fopen($file, "c+b")) === false){
108  return false;
109  }
110  }
111  $this->seek(0);
112  $this->write("PMF" . chr((int) $version) . chr((int) $type));
113 
114  return true;
115  }
116 
117  public function read($length){
118  if($length <= 0){
119  return "";
120  }
121  if(is_resource($this->fp)){
122  return fread($this->fp, (int) $length);
123  }
124 
125  return false;
126  }
127 
128  public function write($string, $length = false){
129  if(is_resource($this->fp)){
130  return ($length === false ? fwrite($this->fp, $string) : fwrite($this->fp, $string, $length));
131  }
132 
133  return false;
134  }
135 
136  public function seek($offset, $whence = SEEK_SET){
137  if(is_resource($this->fp)){
138  return fseek($this->fp, (int) $offset, (int) $whence);
139  }
140 
141  return false;
142  }
143 
144 }