File name independency for Parameters

Discussion in 'Javascript/Plugin Support' started by Mellye, Oct 29, 2015.

  1. Mellye

    Mellye Veteran Veteran

    Messages:
    347
    Likes Received:
    277
    First Language:
    Portuguese
    As we know, to get our parameters from the PluginManager, we need to call

    PluginManager.parameters(plugin_name)With the plugin_name being defined by the name of the file (sans extension).

    For the sake of ease of sharing plugins, it would be fantastic if users didn't need to worry about saving plugins with the exact correct name.

    So a good course of action would be if we just used some method inside our plugin to detect its own filename. There are a bunch of ways to do it in JavaScript, but they are all a bit "workaround-y".

    I'm away from my computer for a while, so I can't test it right now. Is there a way to get our plugin own filename that would works properly, and wouldn't cause issues, in RMMV?
     
    Last edited by a moderator: Oct 29, 2015
    #1
  2. Zalerinian

    Zalerinian Jack of all Errors Veteran

    Messages:
    4,695
    Likes Received:
    922
    Location:
    The Internet.
    First Language:
    English
    Primarily Uses:
    N/A
    you can do it, yes. lavra made a separate plugin to do this, but I'm working on getting a nice, automatic solution into the MVCommons. 
     
    #2
  3. GaryCXJk

    GaryCXJk Veteran Veteran

    Messages:
    88
    Likes Received:
    46
    Location:
    Zaandam, the Netherlands
    First Language:
    Dutch
    Here's the post I made in another topic.

    Okay, I've got an atrocity of a code right here that should do the ultimate trick or treat.

    Essentially, what it does is, it will go through each method until it either finds the plugin parameters or it just gives up and uses the fallback.

    _pluginName is the name of the plugin, or the standard plugin name. _propNames is an array containing all essential parameters (properties of the parameters object), and _defaultParams is an object containing the default values.

    First, it just uses PluginManager. Next, it uses document.currentScript, only available on Webkit and Gecko based browsers, as well as Safari and Opera. Then, it uses the description of the plugins. If during that same loop there's nothing in the description for some reason, it checks all properties if they exist. That step could possibly be skipped, though, but still. Added the possibility. Finally, if all fails, it just says, meh, screw that, and uses _defaultParams.

    It's not fool proof, but it's as close as it can get without having to resort to using AJAX to get the contents of the script.
     

    var _pluginName = 'CXJ_Exit'; var _propNames = ['Text - Exit', 'Text - To Desktop', 'Add to title', 'Add to Game End']; var _defaultParams = { 'Text - Exit' : 'Exit', 'Text - To Desktop' : 'To Desktop', 'Add to title' : 'true', 'Add to Game End' : 'true' }; var _getParameters = function(pluginName, propNames, defaultParams) { /* Private function that checks plugin content */ var _checkPluginContent = function(parameters) { for(var prop in parameters) { if(parameters.hasOwnProperty(prop)) { return true; } } return false; } var parameters = PluginManager.parameters(pluginName); if(_checkPluginContent(parameters)) { return parameters; } var currentScript = document.currentScript; if(currentScript) { var scriptName = document.currentScript.src; scriptName = scriptName.substr(scriptName.indexOf('js/plugins/') + 11); scriptName = scriptName.substr(0, scriptName.lastIndexOf('.js')); parameters = PluginManager.parameters(scriptName); } if(_checkPluginContent(parameters)) { return parameters; } for(var idx = 0; idx < $plugins.length; idx++) { var plugin = $plugins[idx]; var params = plugin.parameters; if(plugin.description.indexOf('<' + pluginName + '>') > -1) { return params; } var hasFound = true; for(var idx = 0; idx < _propNames.length; idx++) { if(!params.hasOwnProperty(propNames[idx])) { hasFound = false; break; } } if(hasFound) { return params; } } return _defaultParams; } var parameters = _getParameters(_pluginName, _propNames, _defaultParams);By the way, code's CC0.
     
    #3
  4. Tsukihime

    Tsukihime Veteran Veteran

    Messages:
    8,230
    Likes Received:
    3,062
    Location:
    Toronto
    First Language:
    English
    Why specify default parameters if it can't find the correct plugin?


    As far as I'm concerned, if it can't find the plugin, it should not be considered installed and should either throw an error or make sure it isn't executed.


    If you're falling back to defaults that are hardcoded in the code, the user is going to be confused why the plugin works, but their settings aren't being loaded.


    I'd rather just throw an error if they don't name it properly.
     
    Last edited by a moderator: Oct 29, 2015
    #4
    Galv likes this.
  5. Iavra

    Iavra Veteran Veteran

    Messages:
    1,797
    Likes Received:
    856
    First Language:
    German
    Take a look at the topic linked in my signature. No need to include another plugin, anymore. The general idea is to include an id value inside the plugin description and iterate over the plugin array.


    Since the array contains the plugin name (filename), description and parameters, we can use the description id to get our parameters.
     
    Last edited by a moderator: Oct 29, 2015
    #5
    Mellye likes this.
  6. GaryCXJk

    GaryCXJk Veteran Veteran

    Messages:
    88
    Likes Received:
    46
    Location:
    Zaandam, the Netherlands
    First Language:
    Dutch
    It's up to the plugin scripter to decide. Leaving out the parameter will just default to JavaScript returning undefined, after which you can throw an error.
     
    #6

Share This Page