Added asynchronous file writing

This commit is contained in:
Shoghi Cervantes 2015-05-22 16:32:08 +02:00
parent cfe5ca91b2
commit 149234f125
No known key found for this signature in database
GPG Key ID: 78464DB0A7837F89
4 changed files with 61 additions and 8 deletions

View File

@ -351,7 +351,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
* @return bool * @return bool
*/ */
public function canSee(Player $player){ public function canSee(Player $player){
return !isset($this->hiddenPlayers[$player->getName()]); return !isset($this->hiddenPlayers[$player->getUniqueId()]);
} }
/** /**
@ -361,7 +361,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
if($player === $this){ if($player === $this){
return; return;
} }
$this->hiddenPlayers[$player->getName()] = $player; $this->hiddenPlayers[$player->getUniqueId()] = $player;
$player->despawnFrom($this); $player->despawnFrom($this);
} }
@ -372,7 +372,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
if($player === $this){ if($player === $this){
return; return;
} }
unset($this->hiddenPlayers[$player->getName()]); unset($this->hiddenPlayers[$player->getUniqueId()]);
if($player->isOnline()){ if($player->isOnline()){
$player->spawnTo($this); $player->spawnTo($this);
} }
@ -2735,14 +2735,14 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
public function close($message = "", $reason = "generic reason", $notify = true){ public function close($message = "", $reason = "generic reason", $notify = true){
if($this->connected and !$this->closed){ if($this->connected and !$this->closed){
if($notify and $reason != ""){ if($notify and strlen((string) $reason) > 0){
$pk = new DisconnectPacket; $pk = new DisconnectPacket;
$pk->message = $reason; $pk->message = $reason;
$this->directDataPacket($pk->setChannel(Network::CHANNEL_PRIORITY)); $this->directDataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
} }
$this->connected = false; $this->connected = false;
if($this->username != ""){ if(strlen($this->getName()) > 0){
$this->server->getPluginManager()->callEvent($ev = new PlayerQuitEvent($this, $message)); $this->server->getPluginManager()->callEvent($ev = new PlayerQuitEvent($this, $message));
if($this->server->getAutoSave() and $this->loggedIn === true){ if($this->server->getAutoSave() and $this->loggedIn === true){
$this->save(); $this->save();

View File

@ -91,6 +91,7 @@ use pocketmine\plugin\PharPluginLoader;
use pocketmine\plugin\Plugin; use pocketmine\plugin\Plugin;
use pocketmine\plugin\PluginLoadOrder; use pocketmine\plugin\PluginLoadOrder;
use pocketmine\plugin\PluginManager; use pocketmine\plugin\PluginManager;
use pocketmine\scheduler\FileWriteTask;
use pocketmine\scheduler\SendUsageTask; use pocketmine\scheduler\SendUsageTask;
use pocketmine\scheduler\ServerScheduler; use pocketmine\scheduler\ServerScheduler;
use pocketmine\tile\Chest; use pocketmine\tile\Chest;
@ -843,7 +844,8 @@ class Server{
$nbt = new NBT(NBT::BIG_ENDIAN); $nbt = new NBT(NBT::BIG_ENDIAN);
try{ try{
$nbt->setData($nbtTag); $nbt->setData($nbtTag);
file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed());
$this->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed()));
}catch(\Exception $e){ }catch(\Exception $e){
$this->logger->critical($this->getLanguage()->translateString("pocketmine.data.saveError", [$name, $e->getMessage()])); $this->logger->critical($this->getLanguage()->translateString("pocketmine.data.saveError", [$name, $e->getMessage()]));
if(\pocketmine\DEBUG > 1 and $this->logger instanceof MainLogger){ if(\pocketmine\DEBUG > 1 and $this->logger instanceof MainLogger){

View File

@ -0,0 +1,43 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* 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.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\scheduler;
class FileWriteTask extends AsyncTask{
private $path;
private $contents;
private $flags;
public function __construct($path, $contents, $flags = 0){
$this->path = $path;
$this->contents = $contents;
$this->flags = (int) $flags;
}
public function onRun(){
try{
file_put_contents($this->path, $this->contents, (int) $this->flags);
}catch (\Exception $e){
}
}
}

View File

@ -20,6 +20,7 @@
*/ */
namespace pocketmine\utils; namespace pocketmine\utils;
use pocketmine\scheduler\FileWriteTask;
use pocketmine\Server; use pocketmine\Server;
@ -170,9 +171,11 @@ class Config{
} }
/** /**
* @param bool $async
*
* @return boolean * @return boolean
*/ */
public function save(){ public function save($async = false){
if($this->correct === true){ if($this->correct === true){
try{ try{
$content = null; $content = null;
@ -194,7 +197,12 @@ class Config{
$content = implode("\r\n", array_keys($this->config)); $content = implode("\r\n", array_keys($this->config));
break; break;
} }
if($async){
Server::getInstance()->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->file, $content));
}else{
file_put_contents($this->file, $content); file_put_contents($this->file, $content);
}
}catch(\Exception $e){ }catch(\Exception $e){
$logger = Server::getInstance()->getLogger(); $logger = Server::getInstance()->getLogger();
$logger->critical("Could not save Config " . $this->file . ": " . $e->getMessage()); $logger->critical("Could not save Config " . $this->file . ": " . $e->getMessage());