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

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

Author Topic: Reloading custom modules  (Read 4383 times)

0 Members and 1 Guest are viewing this topic.

Offline Riccarr

  • BeBot User
  • **
  • Posts: 35
  • Karma: +0/-0
Reloading custom modules
« on: January 17, 2010, 02:42:47 am »
Hello,

While working on a new module I find it a pain in the a$$ to have to stop/restart the bot each time I need to make adjustments to the custom module.  I'm not very php savvy, but I see that the main.php loops the custom dir and does a require_once() to load each module.

I thought perhaps making a call to a new function (in main.php) that would check the timestamp of the modules there and call require() again on any module whose timestamp was newer than the last time checked.

I got this all to work ... except that when it calls to reload the modified php file, it does not actually effect a change to the version that is already loaded.

What I mean is if a function in the custom mod returns "test1" when called via in-game command; and then I update the module so the function returns "test2" and save it, this causes the timestamp to update and it gets reloaded with a "require()" call. Then while still in game (and not restarting the bot) the same command still returns "test1". So the call to require() loaded the module however it didn't overwrite or replace the existing module in memory.

I hope your following me.  Maybe you all are already doing something like this and I'm missing the boat?

Here is the reload function I use; I place a call to it in the WHILE loop near the bottom of the main.php module, where it "listens for incoming events"

Code: [Select]

/*
Listen for incoming events...
*/
while(true)
{
if ($aoc -> wait_for_packet() == "disconnected")
$bot -> reconnect();

$bot -> cron();
   
    reloadCustoms();  //call our reload function
}


function reloadCustoms()
{
    global $bot;
    global $lastCustomLoad;   //this set in after custom modules loaded
    global $lastCustomCheck;  //this set in after custom modules loaded
   
    if (time() - $lastCustomCheck > 10)   //just to keep overhead low, this loop only runs every 10 seconds
    {
        // $bot -> log("MAIN", "LOG", "Checking reload list ...");
        $filesLoaded = false;
        $folder = dir("./custom/modules/");
        while ($mod = $folder->read())
        {
            $value = $bot -> core("ini") -> get($mod,"Custom_Modules");

            $fileTime = filemtime("./custom/modules/" . $mod);
            if (!is_dir($mod) && !preg_match("/^_/", $mod) && preg_match("/\.php$/i", $mod)
                && $value != "FALSE")
                if ($fileTime > $lastCustomLoad)
                {
                    require "custom/modules/" . $mod;
                    $bot -> log("MAIN","MOD LOAD", "RELOADING: ".$mod);
                    $filesLoaded = true;
                }
        }
       
        if ($filesLoaded)
            $lastCustomLoad = time();
           
        $lastCustomCheck = time();
    }
}

Offline Khalem

  • BeBot Founder
  • Administrator
  • ********
  • Posts: 1169
  • Karma: +0/-0
    • http://www.ancarim.com
Re: Reloading custom modules
« Reply #1 on: January 17, 2010, 02:48:04 pm »
Loading a module is not an issue while the bot is running. However PHP provides no way for us to unload current functions and classes afaik.

This has been discussed internally in the past, and the verdict was that it's simply not possible to do within the constraints PHP gives us.
BeBot Founder and Fixer Kingpin

Offline Riccarr

  • BeBot User
  • **
  • Posts: 35
  • Karma: +0/-0
Re: Reloading custom modules
« Reply #2 on: January 18, 2010, 07:20:52 am »
Pity ... was hoping it was workable as it would safe a lot of bot stops and restarting.

Thanks for letting me know.

Offline DJKRose

  • Contributor
  • *******
  • Posts: 38
  • Karma: +0/-0
Re: Reloading custom modules
« Reply #3 on: January 30, 2010, 06:02:43 pm »
You could try runkit_import(). It reads in a PHP file replacing all the existing functions and classes with the new definitions.
Note that everything outside functions/classes is ignored, so the new instantiation of modules (normally the first line) has to be done manually somehow.

 

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


0.8.x updates for AoC by bitnykk
[January 30, 2024, 11:16:08 pm]


0.8.x updates for AO by bitnykk
[January 30, 2024, 11:15:37 pm]


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


Bebot and Rasberry by bitnykk
[November 29, 2023, 11:04:14 pm]

* Who's Online
  • Dot Guests: 576
  • 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