continued BotLoader, added ModManager.class.php & timers
[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->writeDB("php_p10.db");
68         }
69         
70         public function autosave() {
71                 foreach($this->loadedBots as $name => $bot) {
72                         if(array_key_exists(strtolower($name), $this->botDatabases)) {
73                                 $db = $bot->writeDB();
74                                 $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
75                         }
76                 }
77                 $this->save();
78                 timer(60*10, array($this, "autosave"), array());
79         }
80         
81         public function loop() {
82                 foreach($this->loadedBots as $name => $bot) {
83                         $bot->loop();
84                 }
85         }
86         
87         private function loadBot($name, $botfile) {
88                 if(array_key_exists(strtolower($name), $this->loadedBots)) return false;
89                 //load bot
90                 $bot = $this->loadClass($botfile, $name);
91                 if(!$bot) return false;
92                 $bot->load($this->uplink);
93                 $this->loadedBots[strtolower($name)] = $bot;
94                 if(!array_key_exists($name, $this->botdb["bots"])) {
95                         $this->botdb["bots"][$name] = $botfile;
96                 }
97         }
98         
99         private function unloadBot($name, $delete = true) {
100                 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
101                 //unload bot
102                 $bot = $this->loadedBots[strtolower($name)];
103                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
104                         $db = $bot->writeDB();
105                         $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
106                         unset($this->botDatabases[strtolower($name)]);
107                 }
108                 ModCMD::unbindBot($bot);
109                 $bot->unload(false);
110                 unset($this->loadedBots[strtolower($name)]);
111                 if(array_key_exists($name, $this->botdb["bots"]) && $delete) {
112                         unset($this->botdb["bots"][$name]);
113                 }
114         }
115         
116         private function rehashBot($name) {
117                 if(!(array_key_exists(strtolower($name), $this->loadedBots))) return false;
118                 //rehash bot
119                 $bot = $this->loadedBots[strtolower($name)];
120                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
121                         $db = $bot->writeDB();
122                         $this->saxdb->setSection($this->botDatabases[strtolower($name)],$db);
123                         unset($this->botDatabases[strtolower($name)]);
124                 }
125                 ModCMD::unbindBot($bot);
126                 $data = $bot->unload(true);
127                 unset($this->loadedBots[strtolower($name)]);
128                 //ok  bot is unloaded... load it again...
129                 $bot = $this->loadClass($botfile, $name);
130                 if(!$bot) return false;
131                 $bot->load($this->uplink, $data);
132                 $this->loadedBots[strtolower($name)] = $bot;
133         }
134         
135         private function listLoadedBots() {
136                 return $this->loadedBots;
137         }
138         
139         private function addDBsection($bot, $section) {
140                 if(!is_a($bot, "Bot")) return false;
141                 $name = null;
142                 foreach($this->loadedBots as $botname => $cbot) {
143                         if($cbot === $bot) {
144                                 $name = $botname;
145                                 break;
146                         }
147                 }
148                 if($name == null) return false;
149                 if(array_key_exists(strtolower($name), $this->botDatabases)) {
150                         return false;
151                 }
152                 $this->botDatabases[strtolower($name)] = $section;
153                 $bot->readDB($this->saxdb->getSection($section));
154         }
155         
156         private function loadClass($file, $classprefix) {
157                 $dir = self::BOT_DIR;
158                 $tmp = self::TMP_DIR;
159                 $maincode=file_get_contents($dir."/".$file);
160                 if(!$maincode) return;
161                 $class = rand(10000,99999);
162                 while(class_exists("bot_".$classprefix."_".$class)) {
163                         $class = rand(10000,99999);
164                 }
165                 $maincode = str_replace('{$_NAME}', "module_".$classprefix."_".$class, $maincode);
166                 $fp = fopen($tmp."/modules_".$classprefix."_".$class.".tmp.php", 'w');
167                 fwrite($fp, $maincode);
168                 fclose($fp);
169                 include($tmp."/modules_".$classprefix."_".$class.".tmp.php");
170                 $classname = "module_".$classprefix."_".$class;
171                 $newclass = new $classname();
172                 unlink($tmp."/modules_".$classprefix."_".$class.".tmp.php");
173                 return $newclass;
174         }
175         
176         public static function load($name, $botfile) {
177                 return self::$botloader->loadBot($name, $botfile);
178         }
179         
180         public static function unload($name) {
181                 return self::$botloader->unloadBot($name);
182         }
183         
184         public static function rehash($name) {
185                 return self::$botloader->rehashBot($name);
186         }
187         
188         public static function listBots() {
189                 return self::$botloader->listLoadedBots();
190         }
191         
192         public static function registerDB($bot, $name) {
193                 return self::$botloader->addDBsection($bot, $name);
194         }
195 }
196
197 ?>