Improvements to Hooks

Arantor

  • With a Box
  • As long as the planets are turning, as long as the stars are burning, as long as your dreams are coming true...
  • Posts: 13,612
Re: Improvements to Hooks
« Reply #15 on May 22nd, 2011, 06:25 PM »
Yup. integrate_pre_include just contains a list of files to load on SMF startup, you set it in the installer and no file edit is required to have that file loaded later.
  When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest.


Farjo

  • "genuinely interested"
  • We-Gen
  • Posts: 248
Re: Improvements to Hooks
« Reply #16 on May 22nd, 2011, 06:32 PM »
Well that's quite clever then :) So the other bits:- integrate_bbc_codes and integrate_bbc_buttons... they're hooks are they? And they call / load (again sorry for my mis-usage of coding terms) the functions in her Subs-SimpleSpoiler file?
We've moved on - will you come with us?

Arantor

  • With a Box
  • As long as the planets are turning, as long as the stars are burning, as long as your dreams are coming true...
  • Posts: 13,612
Re: Improvements to Hooks
« Reply #17 on May 22nd, 2011, 06:36 PM »
Yes, basically.

Really, all a hook physically is, is just a setting in $modSettings that contains a 'to do' list. For the include hooks, it's a list of files to load, for the rest, it's a list of functions to call.

You'll see call_integration_hook() mentions throughout the main SMF source (we renamed the function and the hooks, more on this later), which say, "Get this list of functions, and run them" - and often passing information to the hooked functions, so you can manipulate them right there.
  When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest.


Farjo

  • "genuinely interested"
  • We-Gen
  • Posts: 248
Re: Improvements to Hooks
« Reply #18 on May 22nd, 2011, 06:51 PM »
Cor look at this old dog learning new coding tricks :P  I'm having afternoon tea to celebrate :eheh:

Thanks again Arantor for your time, patience and explanations.
We've moved on - will you come with us?

Nao

  • With a Box
  • If you say so.....
  • Posts: 12,901
Re: Improvements to Hooks
« Reply #19 on May 22nd, 2011, 07:17 PM »
Here's what Miss All Sunday's code would look like in Wedge:

Code: [Select]
add_hook('bbc_codes', 'spoiler_bbc_add_code', 'Subs-SimpleSpoiler');
add_hook('bbc_buttons', 'spoiler_bbc_add_button', 'Subs-SimpleSpoiler');

A bit simpler, eh ;)
...« I say wedge wedge (in the butt) »
 « Everyone knows rock attained perfection in 1974. It's a scientific fact. » (Homer Simpson)

Farjo

  • "genuinely interested"
  • We-Gen
  • Posts: 248
Re: Improvements to Hooks
« Reply #20 on May 22nd, 2011, 08:15 PM »
Yes, even I can see that's much neater. The original post now makes more sense and I can see why it's got an entire thread dedicated to it.


What about the additional menu items (buttons) that I have on my SMF installation - this is just a code hack. Would I be able to make my own hook function? This would be cool as at present I must re-edit the code with each upgrade. Perhaps:
Code: [Select]
add_hook('menu_items', 'farjo_add_menu_items', 'farjo_code_changes');

where menu_items would be substituted for the correct name of the hook that controls the buttons, farjo_code_changes.php contains the code that I currently paste into Subs.php but wrapped within the function farjo_add_menu_items.
And if so will there be a idiot-readable list of hooks so that someone like me would be able to make  this type of minor changes to their set-up? I know that this change is aimed at proper modders, but it would also encourage others with my limited coding skill to make changes (changing the actual source code is something I wouldn't have the confidence to do, but making a separate file.... well what's the harm?).
We've moved on - will you come with us?

Arantor

  • With a Box
  • As long as the planets are turning, as long as the stars are burning, as long as your dreams are coming true...
  • Posts: 13,612
Re: Improvements to Hooks
« Reply #21 on May 22nd, 2011, 08:21 PM »
Well, not *that* code - that's Wedge code.

In your case, you'll create your file, farjo_code_changes.php, and put it in Sources/

Then your package installer would contain:
Code: [Select]
$hooks = array(
'integrate_menu_buttons' => 'farjo_add_menu_items',
'integrate_pre_include' => '$boarddir/Sources/farjo_code_changes.php',
);

foreach ($hooks as $hook => $function)
add_integration_function($hook, $function);

But the principle's the same. Nao just streamlined what the add function did to avoid the complexity and hassle.
  When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest.


Farjo

  • "genuinely interested"
  • We-Gen
  • Posts: 248
Re: Improvements to Hooks
« Reply #22 on May 22nd, 2011, 09:19 PM »
Sorry, I meant if my club switches to wedge how it could be done. From experience, the SMF way that you show would take me a while as there's plenty for the inexperienced fool to get wrong - a missing single-quote, a misspelt word... But the wedge way would be simple - even I could only make a maximum 1 mistake ;)

Anyway thanks for your time. Another reason to look forward to its release - best you get on with it instead of answering my stupid questions :)
We've moved on - will you come with us?

Nao

  • With a Box
  • If you say so.....
  • Posts: 12,901
Re: Improvements to Hooks
« Reply #23 on May 22nd, 2011, 10:43 PM »
Quote from Farjo on May 22nd, 2011, 08:15 PM
Yes, even I can see that's much neater. The original post now makes more sense and I can see why it's got an entire thread dedicated to it.
There are more features that deserve their own topic, but I was too lazy in the end so they're just in the Minor Features topic ;)
Quote
What about the additional menu items (buttons) that I have on my SMF installation - this is just a code hack. Would I be able to make my own hook function?
Yes.
Quote
This would be cool as at present I must re-edit the code with each upgrade. Perhaps:
Code: [Select]
add_hook('menu_items', 'farjo_add_menu_items', 'farjo_code_changes');
No, the first param is the original hook name, the second parameter is the function to add to the hook (whatever you wrote), and the third parameter (which is optional) is the name of a source file to load where the function can be found.
i.e. if you load a hook that's in a file that also contains a hook function called right before that hook in the code path, then you don't need to bother loading the file.
Quote
where menu_items would be substituted for the correct name of the hook that controls the buttons,
That's 'menu_buttons', AFAIK.
Quote
And if so will there be a idiot-readable list of hooks so that someone like me would be able to make  this type of minor changes to their set-up?
I'll try to document the hooks in the future. Although a list can easily be built from simply searching for "add_hook(" in the Wedge codebase. Right now it's pretty similar to the SMF hook list -- a few were removed, and I probably added a dozen or so. Thing is, I'm not really excited with the idea of adding hooks that never get used, so I'd rather wait for modders to actually ask me for a hook at this or that position. Then I'll figure out the ideal parameter list and the ideal position for the hook, and add it.
Considering that if your mod only uses hooks, it doesn't need any reinstalling when you upgrade your forum, it'll definitely be worth upgrading said forum just to add support for these hooks.
Quote
I know that this change is aimed at proper modders, but it would also encourage others with my limited coding skill to make changes (changing the actual source code is something I wouldn't have the confidence to do, but making a separate file.... well what's the harm?).
Sure!
...« I say wedge wedge (in the butt) »
 « Everyone knows rock attained perfection in 1974. It's a scientific fact. » (Homer Simpson)

Farjo

  • "genuinely interested"
  • We-Gen
  • Posts: 248
Re: Improvements to Hooks
« Reply #24 on May 23rd, 2011, 05:22 PM »
Thanks for the reply.
The hook is as you say integrate_menu_buttons and the list of hooks is here: http://wiki.simplemachines.org/smf/index.php?title=Integration_hooks&oldid=5972 So there's another mini-project for me - to write a mod  :wow:
Quote
There are more features that deserve their own topic, but I was too lazy in the end so they're just in the Minor Features topic ;)
Yes that's a massive topic which I'm still only halfway through reading :)
We've moved on - will you come with us?

Arantor

  • With a Box
  • As long as the planets are turning, as long as the stars are burning, as long as your dreams are coming true...
  • Posts: 13,612
Re: Improvements to Hooks
« Reply #25 on May 23rd, 2011, 05:34 PM »
They really need to update that, all the hooks are described with the original limitations they had when they were in RC3... (back when I originally wrote it)
  When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest.


DoctorMalboro

  • We-Gen
  • I like rounded borders.
  • Posts: 315
Re: Improvements to Hooks
« Reply #26 on May 29th, 2011, 11:54 PM »
Now that mods would be "edit-free" wouldn't be better to be saved in a folder apart from Sources to have things more nice and clean?

Arantor

  • With a Box
  • As long as the planets are turning, as long as the stars are burning, as long as your dreams are coming true...
  • Posts: 13,612
Re: Improvements to Hooks
« Reply #27 on May 30th, 2011, 12:04 AM »
Two things.

Firstly, both SMF and Wedge have limitations as far as the hooks go. Wedge does it better, much better, but there's still a limitation as to where files can be.

Secondly, some people do crazy shit like moving their Sources dir out of the web tree, so you kind of need to put it in there - but I guess we could do it as having a folder for add-ons where each add-on gets its own folder therein?
  When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest.


DoctorMalboro

  • We-Gen
  • I like rounded borders.
  • Posts: 315
Re: Improvements to Hooks
« Reply #28 on May 30th, 2011, 03:23 AM »
I think that could make the understanding of WHAT IS A MOD and WHAT IS CORE STUFF IMO :P

Nao

  • With a Box
  • If you say so.....
  • Posts: 12,901
Re: Improvements to Hooks
« Reply #29 on May 30th, 2011, 09:50 AM »
I'm still pushing for Wedge to completely disable the ability to hack into source files... And I'm ready to add as many hook points as requested to ensure as many mods as possible can be done for Wedge.
It really doesn't kill performance at all, in my tests at least. It's just an extra function call and an empty() test on each new hook...
...« I say wedge wedge (in the butt) »
 « Everyone knows rock attained perfection in 1974. It's a scientific fact. » (Homer Simpson)