PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
GenerationThread.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 
22 namespace pocketmine\level\generator;
23 
24 
26 
27 class GenerationThread extends Thread{
28 
29  protected $loadPaths;
31  protected $loader;
33  protected $logger;
34 
36  protected $externalQueue;
38  protected $internalQueue;
39 
43  public function getInternalQueue(){
44  return $this->internalQueue;
45  }
46 
50  public function getExternalQueue(){
51  return $this->externalQueue;
52  }
53 
54  public function pushMainToThreadPacket($str){
55  $this->internalQueue[] = $str;
56  $this->synchronized(function (){
57  $this->notify();
58  });
59  }
60 
61  public function readMainToThreadPacket(){
62  return $this->internalQueue->shift();
63  }
64 
65  public function pushThreadToMainPacket($str){
66  $this->externalQueue[] = $str;
67  }
68 
69  public function readThreadToMainPacket(){
70  return $this->externalQueue->shift();
71  }
72 
76  public function getLogger(){
77  return $this->logger;
78  }
79 
80  public function __construct(\Threaded $internalThreaded, \Threaded $externalThreaded, \ThreadedLogger $logger, \ClassLoader $loader){
81  $this->loader = $loader;
82  $this->logger = $logger;
83  $loadPaths = [];
84  $this->addDependency($loadPaths, new \ReflectionClass($this->loader));
85  $this->loadPaths = array_reverse($loadPaths);
86 
87  $this->externalQueue = $internalThreaded;
88  $this->internalQueue = $externalThreaded;
89 
90  $this->start();
91  }
92 
93  protected function addDependency(array &$loadPaths, \ReflectionClass $dep){
94  if($dep->getFileName() !== false){
95  $loadPaths[$dep->getName()] = $dep->getFileName();
96  }
97 
98  if($dep->getParentClass() instanceof \ReflectionClass){
99  $this->addDependency($loadPaths, $dep->getParentClass());
100  }
101 
102  foreach($dep->getInterfaces() as $interface){
103  $this->addDependency($loadPaths, $interface);
104  }
105  }
106 
107  public function run(){
108  error_reporting(-1);
109  gc_enable();
110  //Load removed dependencies, can't use require_once()
111  foreach($this->loadPaths as $name => $path){
112  if(!class_exists($name, false) and !interface_exists($name, false)){
113  require($path);
114  }
115  }
116  $this->loader->register();
117 
118  $generationManager = new GenerationManager($this, $this->getLogger(), $this->loader);
119  }
120 }