-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New hooks in files loading #34
Conversation
This is a request to add 3 hooks to Settings::loadByHandle, the purpose of this request is to make is possible for plugins change the filepath and parsing method of any file just before it's loading. Be aware that hook names depend on the loading file.
Do you have an example plugin using these hooks that we can use for testing? |
Sure, here is my current use of this Pull request code: http://pastebin.com/84X3rjny |
|
||
#hooks of type 'pre_load_' make it possible to change the filename before openkore searches for it (this filename must contain file name and file path) | ||
my %pre_load = (internalFilename => $internalFilename, filename => \$filename); | ||
Plugins::callHook('pre_load_'.$internalFilename, \%pre_load); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on a crusade against deeply nested logic. :)
How about:
my $pre_load = {internalFilename => $internalFilename, filename => $filename};
Plugins::callHook('pre_load_'.$internalFilename, $pre_load);
if ($pre_load->{return}) {
$filename = $pre_load->{filename};
} elsif ($object->{autoSearch} && $object->{type} == CONTROL_FILE_TYPE) {
$filename = _findFileFromFolders($object->{name}, \@controlFolders);
} elsif ($object->{autoSearch}) {
$filename = _findFileFromFolders($object->{name}, \@tablesFolders);
} else {
$filename = $object->{name};
}
Also, should we pass the $object->{type}
as well, so plugins can make the same sort of decisions that this code does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right, the hook is on a per-file basis so presumably the receiver already knows the object type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$filename = $pre_load->{filename}; is not necessary since in my code $pre_load{'filename'} holds a reference to '$filename' so it can be changed inside the plugin itself without any extra source code. About the nested logic, i guess since we are at it there could be no bad at doing some cleaning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it's a good time to add the proper verification for 'TABLE_FILE_TYPE' instead of assuming everything that isn't a 'CONTROL_FILE_TYPE' is a 'TABLE_FILE_TYPE', this adds support for new file types in the future.
my $internalFilename = $object->{internalName} || $object->{name}; | ||
|
||
#hooks of type 'pre_load_' make it possible to change the filename before openkore searches for it (this filename must contain file name and file path) | ||
my %pre_load = (internalFilename => $internalFilename, filename => \$filename); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of passing scalar refs around as in/out variables. Perl doesn't make them easy to work with; when using them, one has to use the clunky double sigil ${$pre_load->{filename}}
to read and write them. While it takes an extra line here to extract the modified $filename
, it becomes easier for any hook handlers to work with the data.
OpenKore does use this idiom elsewhere though. Whatever you prefer is fine. :)
Plugin example updated to work without the scalar refs: http://pastebin.com/84X3rjny |
Perl script to convert current portals.txt into JSON format to test with the plugin I gave: http://pastebin.com/Vb3f3Nqa |
Please don't merge without checking all the boxes. I'm assuming you tested it first, so QA should have been checked? |
This is a request to add 3 hooks to Settings::loadByHandle, the purpose
of this request is to make it possible for plugins to change the filepath
and parsing method of any file just before it's loading. Be aware that
hook names depend on the loading file.