Factor the iterator out of LoadOrRequireExtensions.
This commit is contained in:
parent
e559e6ffa8
commit
15f4a05122
@ -1122,9 +1122,8 @@ bool CPluginManager::FindOrRequirePluginDeps(CPlugin *pPlugin, char *error, size
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass, char *error, size_t maxlength)
|
bool CPlugin::ForEachExtVar(const ExtVarCallback& callback)
|
||||||
{
|
{
|
||||||
/* Find any extensions this plugin needs */
|
|
||||||
struct _ext
|
struct _ext
|
||||||
{
|
{
|
||||||
cell_t name;
|
cell_t name;
|
||||||
@ -1133,7 +1132,7 @@ bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass
|
|||||||
cell_t required;
|
cell_t required;
|
||||||
} *ext;
|
} *ext;
|
||||||
|
|
||||||
IPluginContext *pBase = pPlugin->GetBaseContext();
|
IPluginContext *pBase = GetBaseContext();
|
||||||
for (uint32_t i = 0; i < pBase->GetPubVarsNum(); i++)
|
for (uint32_t i = 0; i < pBase->GetPubVarsNum(); i++)
|
||||||
{
|
{
|
||||||
sp_pubvar_t *pubvar;
|
sp_pubvar_t *pubvar;
|
||||||
@ -1145,29 +1144,42 @@ bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass
|
|||||||
|
|
||||||
ext = (_ext *)pubvar->offs;
|
ext = (_ext *)pubvar->offs;
|
||||||
|
|
||||||
char *file, *name;
|
ExtVar var;
|
||||||
if (pBase->LocalToString(ext->file, &file) != SP_ERROR_NONE)
|
if (pBase->LocalToString(ext->file, &var.file) != SP_ERROR_NONE)
|
||||||
continue;
|
continue;
|
||||||
if (pBase->LocalToString(ext->name, &name) != SP_ERROR_NONE)
|
if (pBase->LocalToString(ext->name, &var.name) != SP_ERROR_NONE)
|
||||||
continue;
|
continue;
|
||||||
|
var.autoload = !!ext->autoload;
|
||||||
|
var.required = !!ext->required;
|
||||||
|
|
||||||
|
if (!callback(pubvar, var))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass, char *error, size_t maxlength)
|
||||||
|
{
|
||||||
|
auto callback = [pPlugin, pass, error, maxlength]
|
||||||
|
(const sp_pubvar_t *pubvar, const CPlugin::ExtVar& ext) -> bool
|
||||||
|
{
|
||||||
char path[PLATFORM_MAX_PATH];
|
char path[PLATFORM_MAX_PATH];
|
||||||
if (pass == 1) {
|
if (pass == 1) {
|
||||||
/* Attempt to auto-load if necessary */
|
/* Attempt to auto-load if necessary */
|
||||||
if (ext->autoload) {
|
if (ext.autoload) {
|
||||||
libsys->PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
|
libsys->PathFormat(path, PLATFORM_MAX_PATH, "%s", ext.file);
|
||||||
g_Extensions.LoadAutoExtension(path, !!ext->required);
|
g_Extensions.LoadAutoExtension(path, ext.required);
|
||||||
}
|
}
|
||||||
} else if (pass == 2) {
|
} else if (pass == 2) {
|
||||||
/* Is this required? */
|
/* Is this required? */
|
||||||
if (ext->required) {
|
if (ext.required) {
|
||||||
libsys->PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
|
libsys->PathFormat(path, PLATFORM_MAX_PATH, "%s", ext.file);
|
||||||
IExtension *pExt = g_Extensions.FindExtensionByFile(path);
|
IExtension *pExt = g_Extensions.FindExtensionByFile(path);
|
||||||
if (!pExt)
|
if (!pExt)
|
||||||
pExt = g_Extensions.FindExtensionByName(name);
|
pExt = g_Extensions.FindExtensionByName(ext.name);
|
||||||
|
|
||||||
if (!pExt || !pExt->IsRunning(nullptr, 0)) {
|
if (!pExt || !pExt->IsRunning(nullptr, 0)) {
|
||||||
ke::SafeSprintf(error, maxlength, "Required extension \"%s\" file(\"%s\") not running", name, file);
|
ke::SafeSprintf(error, maxlength, "Required extension \"%s\" file(\"%s\") not running", ext.name, ext.file);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
g_Extensions.BindChildPlugin(pExt, pPlugin);
|
g_Extensions.BindChildPlugin(pExt, pPlugin);
|
||||||
@ -1175,7 +1187,7 @@ bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass
|
|||||||
char buffer[64];
|
char buffer[64];
|
||||||
ke::SafeSprintf(buffer, sizeof(buffer), "__ext_%s_SetNTVOptional", &pubvar->name[6]);
|
ke::SafeSprintf(buffer, sizeof(buffer), "__ext_%s_SetNTVOptional", &pubvar->name[6]);
|
||||||
|
|
||||||
if (IPluginFunction *pFunc = pBase->GetFunctionByName(buffer)) {
|
if (IPluginFunction *pFunc = pPlugin->GetBaseContext()->GetFunctionByName(buffer)) {
|
||||||
cell_t res;
|
cell_t res;
|
||||||
if (pFunc->Execute(&res) != SP_ERROR_NONE) {
|
if (pFunc->Execute(&res) != SP_ERROR_NONE) {
|
||||||
ke::SafeSprintf(error, maxlength, "Fatal error during plugin initialization (ext req)");
|
ke::SafeSprintf(error, maxlength, "Fatal error during plugin initialization (ext req)");
|
||||||
@ -1184,9 +1196,9 @@ bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
return true;
|
return pPlugin->ForEachExtVar(ke::Move(callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
CPlugin *CPluginManager::CompileAndPrep(const char *path, char *error, size_t maxlength)
|
CPlugin *CPluginManager::CompileAndPrep(const char *path, char *error, size_t maxlength)
|
||||||
|
@ -53,6 +53,7 @@
|
|||||||
#include "PhraseCollection.h"
|
#include "PhraseCollection.h"
|
||||||
#include <am-string.h>
|
#include <am-string.h>
|
||||||
#include <bridge/include/IScriptManager.h>
|
#include <bridge/include/IScriptManager.h>
|
||||||
|
#include <am-function.h>
|
||||||
|
|
||||||
class CPlayer;
|
class CPlayer;
|
||||||
|
|
||||||
@ -155,6 +156,16 @@ public:
|
|||||||
CNativeOwner *ToNativeOwner() {
|
CNativeOwner *ToNativeOwner() {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ExtVar {
|
||||||
|
char *name;
|
||||||
|
char *file;
|
||||||
|
bool autoload;
|
||||||
|
bool required;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef ke::Lambda<bool(const sp_pubvar_t *, const ExtVar& ext)> ExtVarCallback;
|
||||||
|
bool ForEachExtVar(const ExtVarCallback& callback);
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Creates a plugin object with default values.
|
* Creates a plugin object with default values.
|
||||||
|
Loading…
Reference in New Issue
Block a user