Skip to content

Instantly share code, notes, and snippets.

@SOF3
Last active April 18, 2017 23:42
Show Gist options
  • Save SOF3/cff6aaa6dbbd3784c86fc9a77e747c7d to your computer and use it in GitHub Desktop.
Save SOF3/cff6aaa6dbbd3784c86fc9a77e747c7d to your computer and use it in GitHub Desktop.
<?php
class ReadOnlyFileStreamWrapper{
const SCHEME_RAW = "pmmpreadonly";
const SCHEME = self::SCHEME_RAW . "://";
private $realPointer;
public function stream_open($path, $mode){
assert(substr($path, 0, strlen(self::SCHEME)) === self::SCHEME);
$this->realPointer = fopen(substr($path, strlen(self::SCHEME)), $mode);
stream_set_read_buffer($this->realPointer, 1024 * 16); //16KB
}
public function stream_read($count){
return fread($this->realPointer, $count);
}
public function stream_close(){
fclose($this->realPointer);
}
public function stream_seek($offset, $whence = SEEK_SET){
return fseek($this->realPointer, $offset, $whence);
}
public function stream_tell(){
return ftell($this->realPointer);
}
public function stream_eof(){
return feof($this->realPointer);
}
public function stream_truncate($size){
return ftruncate($this->realPointer, $size);
}
}
<?php
class ReadOnlyMcRegion extends \pocketmine\level\format\io\region\McRegion{
protected function loadRegion(int $x, int $z){
if(!isset($this->regions[$index = Level::chunkHash($x, $z)])){
$this->regions[$index] = new ReadOnlyRegionLoader($this, $x, $z, static::REGION_FILE_EXTENSION);
}
}
}
<?php
use pocketmine\level\format\io\region\{RegionLoader, McRegion};
stream_wrapper_register(ReadOnlyFileStreamWrapper::SCHEME_RAW, ReadOnlyFileStreamWrapper::class);
class ReadOnlyRegionLoader extends RegionLoader{
public function __construct(McRegion $level, int $regionX, int $regionZ, string $fileExtension = McRegion::REGION_FILE_EXTENSION){
parent::__construct($level, $regionX, $regionZ, $fileExtension);
fclose($this->filePointer);
$this->filePointer = fopen(ReadOnlyFileStreamWrapper::SCHEME . $this->filePath, "rb");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment