finished first "alpha" version of PHP-P10 v2
[PHP-P10.git] / BotLoader / BotLoader.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  BotLoader/BotLoader.class.php
24  *
25  * This class loades / rehashs or unloads all Bots.
26  *
27  */
28 require_once("Bot.class.php");
29 require_once("saxdb.class.php");
30
31 class BotLoader {
32         const BOT_DIR = "Bots";
33         const TMP_DIR = "tmp";
34         private $uplink;
35         private $saxdb;
36         private $botdb;
37         private $loadedBots = array();
38         private $botDatabases = array();
39         private static $botloader;
40         
41         public function __construct($uplink) {
42                 $this->uplink = $uplink;
43                 $this->saxdb = new saxdb();
44                 $this->saxdb->loadDB("php_p10.db");
45                 $this->botdb = $this->saxdb->getSection("BotLoader");
46                 self::$botloader = $this;
47                 timer(60*10, array($this, "autosave"), array());
48         }
49         
50         public function loadBots() {
51                 if(array_key_exists("bots", $this->botdb) && is_array($this->botdb["bots"])) {
52                         foreach($this->botdb["bots"] as $name => $botfile) {
53                                 $this->loadBot($name, $botfile);
54                         }
55                 } else {
56                         $this->botdb["bots"] = array();
57                 }
58         }
59         
60         public function unloadBots() {
61                 foreach($this->loadedBots as $name => $bot) {
62                         $this->unloadBot($name, false);
63                 }
64         }
65         
66         public function save() {
67                 $this->saxdb->setSection("BotLoader", $this->botdb);
68                 $this->saxdb->writeDB("php_p10.db");
69         }
70         
71         public function autosave() {
72                 foreach($this->loadedBots as $name => $bot) {
73                         if(array_key_exists(strtolower($name), $this->botDatabases)) {
74                                 $db = $bot->writeDB();
75                                 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
76                         }
77                 }
78                 $this->save();
79                 timer(60*10, array($this, "autosave"), array());
80         }
81         
82         public function loop() {
83                 foreach($this->loadedBots as $name => $bot) {
84                         $bot->loop();
85                 }
86         }
87         
88         private function loadBot($name, $botfile) {
89                 if(array_key_exists(strtolower($name), $this->loadedBots)) return false;
90                 //load bot
91                 $bot = $this->loadClass($botfile, $name);
92                 if(!$bot) return false;
93                 $bot->load($this->uplink);
94                 $this->loadedBots[strtolower($name)] = $bot;
95                 if(!array_key_exists($name, $this->botdb["bots"])) {
96                         $this->botdb["bots"][$name] = $botfile;
97                 }
98         }
99         
100         private function unloadBot($name, $delete = true) {
101                 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
102                 //unload bot
103                 $bot = $this->loadedBots[strtolower($name)];
104                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
105                         $db = $bot->writeDB();
106                         $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
107                         unset($this->botDatabases[strtolower($name)]);
108                 }
109                 ModCMD::unbindBot($bot);
110                 $bot->unload(false);
111                 unset($this->loadedBots[strtolower($name)]);
112                 if(array_key_exists($name, $this->botdb["bots"]) && $delete) {
113                         unset($this->botdb["bots"][$name]);
114                 }
115         }
116         
117         private function rehashBot($name) {
118                 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
119                 $botfile = null;
120                 foreach($this->botdb["bots"] as $botname => $file) {
121                         if(strtolower($botname) == strtolower($name)) {
122                                 $botfile = $file;
123                                 break;
124                         }
125                 }
126                 if(!$botfile) break;
127                 //rehash bot
128                 $bot = $this->loadedBots[strtolower($name)];
129                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
130                         $db = $bot->writeDB();
131                         $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
132                         unset($this->botDatabases[strtolower($name)]);
133                 }
134                 ModCMD::unbindBot($bot);
135                 $data = $bot->unload(true);
136                 unset($this->loadedBots[strtolower($name)]);
137                 //ok  bot is unloaded... load it again...
138                 $bot = $this->loadClass($botfile, $name);
139                 if(!$bot) return false;
140                 $bot->load($this->uplink, $data);
141                 $this->loadedBots[strtolower($name)] = $bot;
142         }
143         
144         private function listLoadedBots() {
145                 return $this->loadedBots;
146         }
147         
148         private function addDBsection($bot, $section) {
149                 if(!is_a($bot, "Bot")) return false;
150                 $name = null;
151                 foreach($this->loadedBots as $botname => $cbot) {
152                         if($cbot === $bot) {
153                                 $name = $botname;
154                                 break;
155                         }
156                 }
157                 if($name == null) return false;
158                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
159                         return false;
160                 }
161                 $this->botDatabases[strtolower($name)] = $section;
162                 $bot->readDB($this->saxdb->getSection($section));
163         }
164         
165         private function loadClass($file, $classprefix) {
166                 $dir = self::BOT_DIR;
167                 $tmp = self::TMP_DIR;
168                 $maincode=file_get_contents($dir."/".$file);
169                 if(!$maincode) return;
170                 $class = rand(10000,99999);
171                 while(class_exists("bot_".$classprefix."_".$class)) {
172                         $class = rand(10000,99999);
173                 }
174                 $maincode = str_replace('{$_NAME}', "module_".$classprefix."_".$class, $maincode);
175                 $fp = fopen($tmp."/modules_".$classprefix."_".$class.".tmp.php", 'w');
176                 fwrite($fp, $maincode);
177                 fclose($fp);
178                 include($tmp."/modules_".$classprefix."_".$class.".tmp.php");
179                 $classname = "module_".$classprefix."_".$class;
180                 $newclass = new $classname();
181                 unlink($tmp."/modules_".$classprefix."_".$class.".tmp.php");
182                 return $newclass;
183         }
184         
185         public static function load($name, $botfile) {
186                 return self::$botloader->loadBot($name, $botfile);
187         }
188         
189         public static function unload($name) {
190                 return self::$botloader->unloadBot($name);
191         }
192         
193         public static function rehash($name) {
194                 return self::$botloader->rehashBot($name);
195         }
196         
197         public static function listBots() {
198                 return self::$botloader->listLoadedBots();
199         }
200         
201         public static function registerDB($bot, $name) {
202                 return self::$botloader->addDBsection($bot, $name);
203         }
204 }
205
206 ?>