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

Fixed plugins inside plugins (Repost) #478

Merged

Conversation

DoopieWop
Copy link
Collaborator

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:

local oldPlugin = PLUGIN

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:
ix.plugin.LoadFromDir(path.."/plugins")

Right here, it loads the nested plugin (nested_plugin), which calls ipL again.
This time when we reach line 17:
local oldPlugin = PLUGIN

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:
_G[variable] = oldPlugin

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:

_G[variable] = oldPlugin

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.

@alexgrist alexgrist merged commit f74ba97 into NebulousCloud:master Jan 19, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants