collapse collapse
* User Info
 
 
Welcome, Guest. Please login or register.
* Search

* Board Stats
  • stats Total Members: 989
  • stats Total Posts: 18366
  • stats Total Topics: 2501
  • stats Total Categories: 7
  • stats Total Boards: 35
  • stats Most Online: 1144

Author Topic: Core/API Registration  (Read 4599 times)

0 Members and 1 Guest are viewing this topic.

Offline Glarawyn

  • BeBot Hero
  • ******
  • Posts: 521
  • Karma: +0/-0
Core/API Registration
« on: November 02, 2006, 05:51:57 am »
Before this disappears from my IRC scroll back...

Quote
<Glara> Instead of each core module having it's own variable, couldn't they be packed into a single array?
<Glara> So we'd do $bot -> core['whois'] instead of $bot -> whois = &$whoiscache;
<Glara> $this -> bot -> core['whois'] -> lookup($name);
<Glara> anyone could create a "core" module to share functions without editing Bot.php...
<Glara> Just thinking out loud. ;)
01:28PM <Khalem> A valid suggestion
01:29PM <Naturalistic> Aye
01:29PM <Khalem> Slightly more typing, a slight inconvenience for a gain
<Glara> Bigger inconvenience to edit Bot.php for any custom core modules
<Glara> Upgrading to a new bugfix release replaces anything custom in Bot.php.
01:32PM <Naturalistic> yes, like Vhab's bot... you can't edit the core :p
<Glara> useful stuff has come out of core customizations though
<Glara> but there seem to be workarounds to do the same thing without changing the actual core files.
01:39PM <Khalem> Actually, it could be made more robust by using a function to  do the actual registering
01:39PM <Khalem> But again, if a developer screws up the bot core with his core hooks, it's fairly obvious
<Glara> Khalem: that would probably be a good idea to keep people from clobbering another module.
<Glara> and keeping registered module names unique.

So I went ahead and fiddled a bit and came up with a simple api registration function. By default it won't reset an existing function, but you can pass TRUE as the third function parameter if you're really really sure you want to clobber any existing class reference, though there is no protection from someone else doing the same to you in their module. I added log functions so that if something funky goes on, logs can be reviewed to see registrations.

Add to Bot.php around line 71:
Code: [Select]
    var $api;

Add to Bot.php around line 805:
Code: [Select]
    /*
    Registers APIs
    */
    function register_api($apiname, &$apiref, $replace=FALSE)
    { // Start function register_api()
        if (isset($this -> api[$apiname]))
        {
            if ($override) // Register the api, replace existing registration.
            {
                $this -> api[$apiname] = $apiref;
                $this -> log("API", "REG", "Reregistered API ".$apiname);
                return TRUE;
            }
            else // Don't register the api over the existing registered api.
            {
                $this -> log("API", "REG", "Did not register API ".$apiname." because it is already registered.");
                return FALSE;
            }
        }
        else // API is not set, so register it.
        {
            $this -> api[$apiname] = $apiref;
            $this -> log("API", "REG", "Register new API ".$apiname);
            return TRUE;
        }
    } // End function register_api()

Test modules attached. APITest1.php registers itself, APITest2.php uses APITest1 functions.

Offline Nesi

  • BeBot User
  • **
  • Posts: 55
  • Karma: +0/-0
Re: Core/API Registration
« Reply #1 on: November 02, 2006, 10:56:53 am »
It is already possible to add a reference to $this -> bot -> whateverclass from the module file itself -  If that was all you wanted to do?
« Last Edit: November 02, 2006, 11:16:15 am by Nesi »

Offline Alreadythere

  • BeBot Maintainer
  • BeBot Hero
  • ******
  • Posts: 1288
  • Karma: +0/-0
Re: Core/API Registration
« Reply #2 on: November 02, 2006, 11:47:38 am »
Like Nesi said, it's not needed to edit the Bot.php just to add another Core class. All you need to do is $bot -> myclass = &$myclass; in the myclass.php. Then you can call $this -> bot -> myclass everywhere.

Still a default way to add new core modules can never harm, especially with your collision check. You just got to rename the variable $overwrite to $replace in your api function (or the other way around). Right now that check can never be true ;)

Offline Khalem

  • BeBot Founder
  • Administrator
  • ********
  • Posts: 1169
  • Karma: +0/-0
    • http://www.ancarim.com
Re: Core/API Registration
« Reply #3 on: November 02, 2006, 04:46:47 pm »
Yes you can do it without modifying Bot.php, but there are no error checking or handling, nor do you have any way to easilly detect conflicts and this could save you from pointless hours of debugging too :)
BeBot Founder and Fixer Kingpin

 

* Recent Posts
Com bot module by bitnykk
[November 25, 2024, 05:36:11 pm ]


0.8.x updates for AO by bitnykk
[June 23, 2024, 03:19:47 pm ]


0.8.x updates for AoC by bitnykk
[June 23, 2024, 03:19:44 pm ]


[AoC] special char for items module by bitnykk
[February 09, 2024, 09:41:18 pm ]


BeBot still alive & kicking ! by bitnykk
[December 17, 2023, 12:58:44 am ]

* Who's Online
  • Dot Guests: 304
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.
* Forum Staff
bitnykk admin bitnykk
Administrator
Khalem admin Khalem
Administrator
WeZoN gmod WeZoN
Global Moderator
SimplePortal 2.3.7 © 2008-2024, SimplePortal