infernotorrent

Mod_class // ihm

Jan 22nd, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.95 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5.     /*
  6.  
  7.     *   Extension class for Contra. By photofroggy.
  8.  
  9.     *  
  10.  
  11.     *   Released under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License, which allows you to copy, distribute, transmit, alter, transform,
  12.  
  13.     *   and build upon this work but not use it for commercial purposes, providing that you attribute me, photofroggy (froggywillneverdie@msn.com) for its creation.
  14.  
  15.     *  
  16.  
  17.     *   This is essentially the module API
  18.  
  19.     *   for Contra. It is made in an effort
  20.  
  21.     *   to make developing modules as simple
  22.  
  23.     *   as possible while providing useful
  24.  
  25.     *   features for command and event
  26.  
  27.     *   management. Alot of the stuff is
  28.  
  29.     *   is just an interface between this class
  30.  
  31.     *   and the events class though.
  32.  
  33.     */
  34.  
  35.  
  36.  
  37. define('EXT_CUSTOM',0);
  38.  
  39. define('EXT_SYSTEM',1);
  40.  
  41. define('EXT_LIBRARY',2);
  42.  
  43.  
  44.  
  45. class extension {
  46.  
  47.  
  48.  
  49.     // These are variables to store parts of the core.
  50.  
  51.     public $Bot = Null;
  52.  
  53.     public $Console = Null;
  54.  
  55.     public $dAmn = Null;
  56.  
  57.     public $Timer = Null;
  58.  
  59.     public $user = Null;
  60.  
  61.    
  62.  
  63.     // Module properties!
  64.  
  65.     public $name = Null;
  66.  
  67.     public $type = EXT_CUSTOM; // Loldefault.
  68.  
  69.     public $status = false;
  70.  
  71.     public $about = Null;
  72.  
  73.     public $author = Null;
  74.  
  75.     public $version = Null;
  76.  
  77.     public $loading = Null;
  78.  
  79.     public $evts = array();
  80.  
  81.     public $cmds = array();
  82.  
  83.    
  84.  
  85.     final function __construct($Bot) {
  86.  
  87.         // The core itself.
  88.  
  89.         $this->Bot = $Bot;
  90.  
  91.         // The Bot's console!
  92.  
  93.         $this->Console = $Bot->Console;
  94.  
  95.         // Our dAmn API!
  96.  
  97.         $this->dAmn = $Bot->dAmn;
  98.  
  99.         // Contra's very own timer class!
  100.  
  101.         $this->Timer = $Bot->Timer;
  102.  
  103.         // And we also have a user class for user access management.
  104.  
  105.         $this->user = $Bot->user;
  106.  
  107.         // Now we're loading the module!
  108.  
  109.         $this->loading = true;
  110.  
  111.         // Call the init method so the module can be configured properly.
  112.  
  113.         $this->init();
  114.  
  115.         // Call the load method to verify the load.
  116.  
  117.         $this->load();
  118.  
  119.         // Unset the loading because we're not loading now...
  120.  
  121.         unset($this->loading);
  122.  
  123.     }
  124.  
  125.    
  126.  
  127.     function init() {}
  128.  
  129.     final function hook($meth, $event) {
  130.  
  131.         if(!method_exists($this, $meth)) return;
  132.  
  133.         $hook = $this->Bot->Events->hook($this->name, $meth, $event);
  134.  
  135.         if(isset($this->loading)) $this->evts[$event][] = $meth;
  136.  
  137.         return $hook;
  138.  
  139.     }
  140.  
  141.    
  142.  
  143.     final function unhook($meth, $event) { return $this->Bot->Events->unhook($this->name, $meth, $event); }
  144.  
  145.     final function addCmd($cmd, $meth, $p = 25, $s = true) {
  146.  
  147.         if(is_array($cmd)) {
  148.  
  149.             foreach($cmd as $id => $scmd) { $this->addCmd($scmd, $meth, $p, $s); }
  150.  
  151.             return;
  152.  
  153.         }
  154.  
  155.         if(!method_exists($this, $meth)) return;
  156.  
  157.         $this->Bot->Events->add_command($this->name, $cmd, $meth, $p, $s);
  158.  
  159.         if(isset($this->loading)) $this->cmds[] = $cmd;
  160.  
  161.     }
  162.  
  163.    
  164.  
  165.     final function cmdHelp($cmd, $str) {
  166.  
  167.         if(is_array($cmd)) {
  168.  
  169.             foreach($cmd as $id => $scmd) { $this->cmdHelp($scmd, $str); }
  170.  
  171.             return;
  172.  
  173.         }
  174.  
  175.         $this->Bot->Events->cmdHelp($this->name, $cmd, $str);
  176.  
  177.     }
  178.  
  179.    
  180.  
  181.     final function switchCmd($cmd, $s = true) { return $this->Bot->Events->switchCmd($cmd, $s); }
  182.  
  183.     final function load() {
  184.  
  185.    
  186.  
  187.         global $INC_FILE, $INC_DIR;
  188.  
  189.         $file = $INC_DIR.'/'.$INC_FILE;
  190.  
  191.         if($this->name !== Null) {
  192.  
  193.             if($this->author !== Null) {
  194.  
  195.                 if($this->type != EXT_CUSTOM
  196.  
  197.                 && $this->type != EXT_SYSTEM
  198.  
  199.                 && $this->type != EXT_LIBRARY) $this->type = EXT_CUSTOM;
  200.  
  201.                 if(!array_key_exists($this->name, $this->Bot->mod)) {
  202.  
  203.                     $this->Bot->mod[$this->name] = $this;
  204.  
  205.                     unset($this->evts); unset($this->cmds);
  206.  
  207.                     if(DEBUG) $this->Console->Notice('Loaded Module '.$this->name.'.');
  208.  
  209.                     return;
  210.  
  211.                     // eh... Houston, we have a problem!
  212.  
  213.                 } else { $this->Console->Warning($file.' tried loading '.$this->name.', which is already loaded.'); }
  214.  
  215.             } else { $this->Console->Warning($file.' tried loading a module without an author.'); }
  216.  
  217.         } else { $this->Console->Warning('No name was provided for a loading module in '.$file.', ignoring.'); }
  218.  
  219.         if(!empty($this->evts)) foreach($this->evts as $event => $meths)
  220.  
  221.                 foreach($meths as $meth) $this->unhook($event);
  222.  
  223.         if(!empty($this->cmds)) foreach($this->cmds as $cmd)
  224.  
  225.                 $this->Bot->Events->delCmd($this->name, $cmd);
  226.  
  227.    
  228.  
  229.     }
  230.  
  231.     // The methods below are ugly fuckers :D
  232.  
  233.     final protected function Read($file, $format = 0) {
  234.  
  235.    
  236.  
  237.         if(!is_dir('./storage')) mkdir('./storage', 0755);
  238.  
  239.         if(!is_dir('./storage/mod')) mkdir('./storage/mod', 0755);
  240.  
  241.         if(!is_dir('./storage/mod/'.$this->name)) mkdir('./storage/mod/'.$this->name, 0755);
  242.  
  243.         if(!file_exists('./storage/mod/'.$this->name.'/'.$file.'.bsv')) return false;
  244.  
  245.         switch($format) {
  246.  
  247.             case 2: return include './storage/mod/'.$this->name.'/'.$file.'.bsv';
  248.  
  249.                 break;
  250.  
  251.             case 1: return file_get_contents('./storage/mod/'.$this->name.'/'.$file.'.bsv');
  252.  
  253.                 break;
  254.  
  255.             case 0:
  256.  
  257.             default: return unserialize(file_get_contents('./storage/mod/'.$this->name.'/'.$file.'.bsv'));
  258.  
  259.                 break;
  260.  
  261.         }
  262.  
  263.    
  264.  
  265.     }
  266.  
  267.    
  268.  
  269.     final protected function Write($file, $data, $format = 0) {
  270.  
  271.    
  272.  
  273.         if(!is_dir('./storage')) mkdir('./storage', 0755);
  274.  
  275.         if(!is_dir('./storage/mod')) mkdir('./storage/mod', 0755);
  276.  
  277.         if(!is_dir('./storage/mod/'.$this->name)) mkdir('./storage/mod/'.$this->name, 0755);
  278.  
  279.         switch($format) {
  280.  
  281.             case 2: save_config('./storage/mod/'.$this->name.'/'.$file.'.bsv', $data);
  282.  
  283.                 break;
  284.  
  285.             case 1: file_put_contents('./storage/mod/'.$this->name.'/'.$file.'.bsv', $data);
  286.  
  287.                 break;
  288.  
  289.             case 0: default:
  290.  
  291.                 file_put_contents('./storage/mod/'.$this->name.'/'.$file.'.bsv', serialize($data));
  292.  
  293.                 break;
  294.  
  295.         }
  296.  
  297.    
  298.  
  299.     }
  300.  
  301.    
  302.  
  303.     final protected function Unlink($file) {
  304.  
  305.    
  306.  
  307.         if(!is_dir('./storage')) mkdir('./storage', 0755);
  308.  
  309.         if(!is_dir('./storage/mod')) mkdir('./storage/mod', 0755);
  310.  
  311.         if(!is_dir('./storage/mod/'.$this->name)) mkdir('./storage/mod/'.$this->name, 0755);
  312.  
  313.         if(!file_exists('./storage/mod/'.$this->name.'/'.$file.'.bsv')) return;
  314.  
  315.         unlink('./storage/mod/'.$this->name.'/'.$file.'.bsv');
  316.  
  317.    
  318.  
  319.     }
  320.  
  321.  
  322.  
  323. }
  324.  
  325.  
  326.  
  327. ?>
Add Comment
Please, Sign In to add comment