Fixed plugins inside plugins (Repost) #478
Merged
+1
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Accidentally closed this PR.
The change would set the global variable "PLUGIN" to the local variable oldPlugin after a plugin is loaded to allow plugins inside plugins. This change does not result in the PLUGIN global variable staying even after all plugins have been defined. See below for a detailed step by step analysis as to why.
Firstly a new plugin is loaded using ipL. For this explanation, let's say that this is the first plugin that is loaded overall and that it has one or multiple plugins in its plugins folder. So this is what the directory looks like plugin/plugins/nested_plugin.
At this line:
helix/gamemode/core/libs/sh_plugin.lua
Line 17 in 8cab35e
the local variable "oldPlugin" is set to the gv (global variable) "PLUGIN", however because this is the first plugin to be loaded, the gv "PLUGIN" is nil, as such oldPlugin is nil. This is important for later.
Next, the function continues as normal until it reaches this line:
helix/gamemode/core/libs/sh_plugin.lua
Line 48 in 8cab35e
Right here, it loads the nested plugin (nested_plugin), which calls ipL again.
This time when we reach line 17:
helix/gamemode/core/libs/sh_plugin.lua
Line 17 in 8cab35e
the gv "PLUGIN" is not nil, it is the plugin table from the original plugin, as such oldPlugin will not be nil.
We continue towards the end of ipL, still loading the nested plugin:
helix/gamemode/core/libs/sh_plugin.lua
Line 86 in 8cab35e
Right here the gv "PLUGIN" is set to oldPlugin. Why? Because the actual gv "PLUGIN" variable was replaced by the nested plugin's table. In order to allow the original plugin to continue loading, it has to be set to oldPlugin, which for the nested plugin is equal to the original plugin's table.
Now we return to loading the original plugin. We continue to the same again:
helix/gamemode/core/libs/sh_plugin.lua
Line 86 in 8cab35e
This time though because oldPlugin is nil (because gv "PLUGIN" was undefined), the gv "PLUGIN" is also set to nil, thus once the plugin is loaded, we won't have left behind a gv.
TLDR:
Plugin 1 loads, calls ipL to load a nested plugin, plugin 2. It sets a local variable "oldPlugin" to plugin 1's table. It finishes loading setting PLUGIN to plugin 2's local "oldPlugin" variable (so to plugin 1's table). Plugin 1 then sets PLUGIN to it's local variable "oldPlugin" which is nil, so PLUGIN is nil after loading.