Skip to content
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

Plugin event|library script registration via funtion decorator #47

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tinygiant98
Copy link
Collaborator

@tinygiant98 tinygiant98 commented Oct 21, 2022

Yet another extension to reduce workload for creating a plugin. This function takes a CSV list of patterns, pulls the matching .nss files, scans the contents for specified decorators @EVENT[...] and, optionally, @LIBRARY[], and registers the event and library scripts scripts for that plugin.

  1. Tested to work as intended as long as the decorator is in a comment immediately before the function definition. Can theoretically be used in function prototypes, but hasn't been tested there.

  2. Had to revert to calling AddLibraryScript directly (vs RegisterLibraryScript) because of issues with SetScriptParam not passing the correct library for the current VM session, so the library script overwrite notifications are bypassed. Let me know if you have a better way to implement this.

  3. The lines are bit a long due to the nature of regex and JsonPointer addressing. I shortened them quite a bit with SubstituteString, but they're still a bit abnormally lengthy.

  4. Could possibly add another regex capture group to prevent a second scan of the document for the different decorators, but I'll leave that for commenting. I left them separate for now given how different the regex constructs are between the two decorators.

  5. Using this function, all RegisterEventScript and RegisterLibraryScript calls, and the entire OnLibraryScript function, were completely done away with, so there was no library numbering to be managed, greatly reducing the maintenance requirements for library loading.

This is my current OnLibraryLoad for my testing plugin, which registers 25 event scripts and 32 library scripts with a single call and no library management:

void OnLibraryLoad()
{
    if (!GetIfPluginExists("test"))
    {
        object oPlugin = CreatePlugin("test");
        SetName(oPlugin, "[Plugin] System :: Module Testing System");
        SetDebugPrefix(HexColorString("[Testing]", COLOR_ORANGE_DARK), oPlugin);
        SetDescription(oPlugin,
            "This plugin provides functionality for testing various module systems.");
        RegisterPluginScripts(oPlugin, "test_l_plugin,test_i_events");
    }
}

@tinygiant98 tinygiant98 added patch .35 Content that depends on NWN:EE .35 preview enhancement New feature or request labels Oct 21, 2022
@squattingmonk
Copy link
Owner

I love this idea. Decorators provide so many interesting possibilities. We could streamline libraries in the same way. Looking forward to merging when .35 is stable.

@tinygiant98
Copy link
Collaborator Author

Agreed! I have a very similar function for util_i_libraries that I haven't quite finished yet, but when it's there, I'll PR it with the .35 tag. We can also likely extend this idea for additional decorators that serve functions other than events and libraries, such as functions for NUI/form actions, etc. One way to do this is to scan for all decorators, then call a registered function for that decorator, but I'll have to think through that design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request patch .35 Content that depends on NWN:EE .35 preview
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants