Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
fix: Plugin loader
Browse files Browse the repository at this point in the history
tweak: File structure in fxmanifest
tweak: Plugin Loading logic on client side
  • Loading branch information
Jordan2139 committed Oct 28, 2024
1 parent 5f80263 commit 7a24b2a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 46 deletions.
158 changes: 126 additions & 32 deletions sonorancad/core/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,74 @@ Config.GetPluginConfig = function(pluginName)
correctConfig = LoadResourceFile(GetCurrentResourceName(),
'/configuration/' .. pluginName ..
'_config.lua')
if not correctConfig then
infoLog(
('Plugin %s only has the default configurations file (%s_config.dist.lua)... Attempting to use default file'):format(
pluginName, pluginName))
correctConfig = LoadResourceFile(GetCurrentResourceName(),
'/configuration/' .. pluginName ..
'_config.dist.lua')
end
if not correctConfig then
warnLog(
('Plugin %s is missing critical configuration. Please check our plugin install guide at https://info.sonorancad.com/integration-submodules/integration-submodules/plugin-installation for steps to properly install.'):format(
pluginName))
Config.plugins[pluginName] = {
enabled = false,
disableReason = 'Missing configuration file'
}
return {
enabled = false,
disableReason = 'Missing configuration file'
}
else
Config.plugins[pluginName] = correctConfig
if Config.critError then
Config.plugins[pluginName].enabled = false
Config.plugins[pluginName].disableReason = 'startup aborted'
elseif Config.plugins[pluginName].enabled == nil then
Config.plugins[pluginName].enabled = true
elseif Config.plugins[pluginName].enabled == false then
Config.plugins[pluginName].disableReason = 'Disabled'
local configChunk = correctConfig:match("local config = {.-\n}") ..
"\nreturn config"
if not configChunk then
errorLog("No config table found in the string.")
end
local tempEnv = {}
setmetatable(tempEnv, {__index = _G}) -- Allow access to global functions if needed
local loadedPlugin, pluginError =
load(configChunk, 'config', 't', tempEnv)
if loadedPlugin then
-- Execute and capture the returned config table
local success, res = pcall(loadedPlugin)
if not success then
errorLog(
('Plugin %s failed to load due to error: %s'):format(
pluginName, res))
Config.plugins[pluginName] = {
enabled = false,
disableReason = 'Failed to load'
}
return {enabled = false, disableReason = 'Failed to load'}
end
if res and type(res) == "table" then
-- Assign the extracted config to Config.plugins[pluginName]
Config.plugins[pluginName] = res
else
-- Handle case where config is not available
errorLog(
('Plugin %s did not define a valid config table.'):format(
pluginName))
Config.plugins[pluginName] = {
enabled = false,
disableReason = 'Invalid or missing config'
}
return {
enabled = false,
disableReason = 'Invalid or missing config'
}
end
if Config.critError then
Config.plugins[pluginName].enabled = false
Config.plugins[pluginName].disableReason = 'startup aborted'
elseif Config.plugins[pluginName].enabled == nil then
Config.plugins[pluginName].enabled = true
elseif Config.plugins[pluginName].enabled == false then
Config.plugins[pluginName].disableReason = 'Disabled'
end
else
errorLog(('Plugin %s failed to load due to error: %s'):format(
pluginName, pluginError))
Config.plugins[pluginName] = {
enabled = false,
disableReason = 'Failed to load'
}
return {enabled = false, disableReason = 'Failed to load'}
end
return Config.plugins[pluginName]
end
Expand Down Expand Up @@ -82,27 +129,74 @@ Config.LoadPlugin = function(pluginName, cb)
correctConfig = LoadResourceFile(GetCurrentResourceName(),
'/configuration/' .. pluginName ..
'_config.lua')
if not correctConfig then
infoLog(
('Plugin %s only has the default configurations file (%s_config.dist.lua)... Attempting to use default file'):format(
pluginName, pluginName))
correctConfig = LoadResourceFile(GetCurrentResourceName(),
'/configuration/' .. pluginName ..
'_config.dist.lua')
end
if not correctConfig then
warnLog(
('Plugin %s is missing critical configuration. Please check our plugin install guide at https://info.sonorancad.com/integration-submodules/integration-submodules/plugin-installation for steps to properly install.'):format(
pluginName))
Config.plugins[pluginName] = {
enabled = false,
disableReason = 'Missing configuration file'
}
return {
enabled = false,
disableReason = 'Missing configuration file'
}
else
Config.plugins[pluginName] = correctConfig
if Config.critError then
Config.plugins[pluginName].enabled = false
Config.plugins[pluginName].disableReason = 'startup aborted'
elseif Config.plugins[pluginName].enabled == nil then
Config.plugins[pluginName].enabled = true
elseif Config.plugins[pluginName].enabled == false then
Config.plugins[pluginName].disableReason = 'Disabled'
local configChunk = correctConfig:match("local config = {.-\n}") ..
"\nreturn config"
if not configChunk then
errorLog("No config table found in the string.")
end
local tempEnv = {}
setmetatable(tempEnv, {__index = _G}) -- Allow access to global functions if needed
local loadedPlugin, pluginError =
load(configChunk, 'config', 't', tempEnv)
if loadedPlugin then
-- Execute and capture the returned config table
local success, res = pcall(loadedPlugin)
if not success then
errorLog(
('Plugin %s failed to load due to error: %s'):format(
pluginName, res))
Config.plugins[pluginName] = {
enabled = false,
disableReason = 'Failed to load'
}
return {enabled = false, disableReason = 'Failed to load'}
end
if res and type(res) == "table" then
-- Assign the extracted config to Config.plugins[pluginName]
Config.plugins[pluginName] = res
else
-- Handle case where config is not available
errorLog(
('Plugin %s did not define a valid config table.'):format(
pluginName))
Config.plugins[pluginName] = {
enabled = false,
disableReason = 'Invalid or missing config'
}
return {
enabled = false,
disableReason = 'Invalid or missing config'
}
end
if Config.critError then
Config.plugins[pluginName].enabled = false
Config.plugins[pluginName].disableReason = 'startup aborted'
elseif Config.plugins[pluginName].enabled == nil then
Config.plugins[pluginName].enabled = true
elseif Config.plugins[pluginName].enabled == false then
Config.plugins[pluginName].disableReason = 'Disabled'
end
else
errorLog(('Plugin %s failed to load due to error: %s'):format(
pluginName, pluginError))
Config.plugins[pluginName] = {
enabled = false,
disableReason = 'Failed to load'
}
return {enabled = false, disableReason = 'Failed to load'}
end
return Config.plugins[pluginName]
end
Expand Down
26 changes: 14 additions & 12 deletions sonorancad/core/plugin_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ CreateThread(function()
end
if Config.plugins[k].requiresPlugins ~= nil then
for _, plugin in pairs(Config.plugins[k].requiresPlugins) do
local isCritical = plugin.critical
if isCritical then
logError("PLUGIN_DEPENDENCY_ERROR", getErrorText(
"PLUGIN_DEPENDENCY_ERROR"):format(k,
plugin.name))
Config.plugins[k].enabled = false
Config.plugins[k].disableReason =
("Missing dependency %s"):format(plugin.name)
elseif plugin.name ~= "esxsupport" then
warnLog(
("[plugin loader] Plugin %s requires %s, but it is not installed. Some features may not work properly."):format(
k, plugin.name))
if Config.plugins[plugin.name] == nil or not Config.plugins[plugin.name].enabled then
local isCritical = plugin.critical
if isCritical then
logError("PLUGIN_DEPENDENCY_ERROR", getErrorText(
"PLUGIN_DEPENDENCY_ERROR"):format(k,
plugin.name))
Config.plugins[k].enabled = false
Config.plugins[k].disableReason =
("Missing dependency %s"):format(plugin.name)
elseif plugin.name ~= "esxsupport" then
warnLog(
("[plugin loader] Plugin %s requires %s, but it is not installed. Some features may not work properly."):format(
k, plugin.name))
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions sonorancad/fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ server_scripts {
,'core/unittracking.lua'
,'core/updater.lua'
,'core/apicheck.lua'
,'submodules/**/*_config.lua'
,'configuration/**/*_config.lua'
,'core/plugin_loader.lua'
,'submodules/**/sv_*.lua'
,'submodules/**/sv_*.js'
Expand All @@ -30,7 +30,7 @@ client_scripts {
,'core/shared_functions.lua'
,'core/client.lua'
,'core/lighting.lua'
,'submodules/**/*_config.lua'
,'configuration/**/*_config.lua'
,'submodules/**/cl_*.lua'
,'submodules/**/cl_*.js'
}
Expand Down

0 comments on commit 7a24b2a

Please sign in to comment.