Use the new pstruct access API.

This commit is contained in:
David Anderson
2026-09-01 18:58:42 -07:00
parent 02c56a6a42
commit 35d2a648cf
3 changed files with 162 additions and 51 deletions
+158 -47
View File
@@ -259,14 +259,54 @@ void CPlugin::EvictWithError(PluginStatus status, const char *error_fmt, ...)
}
}
// Pstruct fields are stored in the .pstruct_glb table, not in .pubvars.
// v1 images have no pstruct table, so callers fall back to legacy reads.
static bool GetPstructStringField(sp::SmxImage* image, const sp::smx_pstruct_global* glb,
const char* field, std::string* out)
{
std::variant<std::string, cell_t> v;
if (image->GetPstructValue(glb, field, &v) != SP_ERROR_NONE ||
!std::holds_alternative<std::string>(v))
{
return false;
}
*out = std::get<std::string>(std::move(v));
return true;
}
static bool GetPstructBoolField(sp::SmxImage* image, const sp::smx_pstruct_global* glb,
const char* field, bool* out)
{
std::variant<std::string, cell_t> v;
if (image->GetPstructValue(glb, field, &v) != SP_ERROR_NONE ||
!std::holds_alternative<cell_t>(v))
{
return false;
}
*out = std::get<cell_t>(v) != 0;
return true;
}
bool CPlugin::ReadInfo()
{
/* Now grab the info */
uint32_t idx;
IPluginContext *base = GetBaseContext();
int err = base->FindPubvarByName("myinfo", &idx);
int err;
if (err == SP_ERROR_NONE) {
sp::SmxImage* image = base->GetBaseRuntime()->image();
if (const sp::smx_pstruct_global* myinfo = image->FindPstructGlobal("myinfo")) {
auto update_field = [&](const char* field, std::string *dest) {
if (!GetPstructStringField(image, myinfo, field, dest))
*dest = "";
};
update_field("name", &info_name_);
update_field("description", &info_description_);
update_field("author", &info_author_);
update_field("version", &info_version_);
update_field("url", &info_url_);
} else if ((err = base->FindPubvarByName("myinfo", &idx)) == SP_ERROR_NONE) {
struct sm_plugininfo_c_t
{
cell_t name;
@@ -1088,50 +1128,93 @@ bool CPluginManager::FindOrRequirePluginDeps(CPlugin *pPlugin)
uint32_t num = pBase->GetPubVarsNum();
sp_pubvar_t *pubvar;
char *name, *file;
char pathfile[PLATFORM_MAX_PATH];
for (uint32_t i=0; i<num; i++) {
if (pBase->GetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE)
continue;
if (strncmp(pubvar->name, "__pl_", 5) == 0) {
char pathfile[PLATFORM_MAX_PATH];
libsys->GetFileFromPath(pathfile, sizeof(pathfile), pPlugin->GetFilename());
struct PluginDep
{
std::string pubvar_name;
std::string name;
std::string file;
bool required;
};
std::vector<PluginDep> deps;
sp::SmxImage* image = pBase->GetBaseRuntime()->image();
if (image->pstruct_global_count()) {
for (uint32_t i = 0; i < image->pstruct_global_count(); i++) {
const sp::smx_pstruct_global* glb = image->pstruct_globals() + i;
const char* pubvar_name = image->names() + glb->name;
if (strncmp(pubvar_name, "__pl_", 5) != 0)
continue;
PluginDep dep;
dep.pubvar_name = pubvar_name;
dep.required = false;
if (!GetPstructStringField(image, glb, "file", &dep.file))
continue;
if (!GetPstructStringField(image, glb, "name", &dep.name))
continue;
GetPstructBoolField(image, glb, "required", &dep.required);
deps.push_back(std::move(dep));
}
} else {
for (uint32_t i=0; i<num; i++) {
if (pBase->GetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE)
continue;
if (strncmp(pubvar->name, "__pl_", 5) != 0)
continue;
pl = (_pl *)pubvar->offs;
if (pBase->LocalToString(pl->file, &file) != SP_ERROR_NONE)
continue;
if (pBase->LocalToString(pl->name, &name) != SP_ERROR_NONE)
continue;
libsys->GetFileFromPath(pathfile, sizeof(pathfile), pPlugin->GetFilename());
if (strcmp(pathfile, file) == 0)
continue;
if (pl->required == false) {
IPluginFunction *pFunc;
char buffer[64];
ke::SafeSprintf(buffer, sizeof(buffer), "__pl_%s_SetNTVOptional", &pubvar->name[5]);
if ((pFunc=pBase->GetFunctionByName(buffer))) {
cell_t res;
if (pFunc->Execute(&res) != SP_ERROR_NONE) {
pPlugin->EvictWithError(Plugin_Failed, "Fatal error during initializing plugin load");
return false;
}
}
} else {
/* Check that we aren't registering the same library twice */
pPlugin->AddRequiredLib(name);
CPlugin *found = nullptr;
for (PluginIter iter(m_plugins); !iter.done(); iter.next()) {
CPlugin *pl = (*iter);
if (pl->HasLibrary(name)) {
found = pl;
break;
}
}
if (!found) {
pPlugin->EvictWithError(Plugin_Failed, "Could not find required plugin \"%s\"", name);
PluginDep dep;
dep.pubvar_name = pubvar->name;
dep.name = name;
dep.file = file;
dep.required = !!pl->required;
deps.push_back(std::move(dep));
}
}
for (const PluginDep& dep : deps) {
if (strcmp(pathfile, dep.file.c_str()) == 0)
continue;
if (!dep.required) {
IPluginFunction *pFunc;
char buffer[64];
ke::SafeSprintf(buffer, sizeof(buffer), "__pl_%s_SetNTVOptional", &dep.pubvar_name[5]);
if ((pFunc=pBase->GetFunctionByName(buffer))) {
cell_t res;
if (pFunc->Execute(&res) != SP_ERROR_NONE) {
pPlugin->EvictWithError(Plugin_Failed, "Fatal error during initializing plugin load");
return false;
}
found->AddDependent(pPlugin);
}
} else {
/* Check that we aren't registering the same library twice */
pPlugin->AddRequiredLib(dep.name.c_str());
CPlugin *found = nullptr;
for (PluginIter iter(m_plugins); !iter.done(); iter.next()) {
CPlugin *other = (*iter);
if (other->HasLibrary(dep.name.c_str())) {
found = other;
break;
}
}
if (!found) {
pPlugin->EvictWithError(Plugin_Failed, "Could not find required plugin \"%s\"", dep.name.c_str());
return false;
}
found->AddDependent(pPlugin);
}
}
@@ -1149,6 +1232,31 @@ bool CPlugin::ForEachExtVar(const ExtVarCallback& callback)
} *ext;
IPluginContext *pBase = GetBaseContext();
sp::SmxImage* image = pBase->GetBaseRuntime()->image();
for (uint32_t i = 0; i < image->pstruct_global_count(); i++) {
const sp::smx_pstruct_global* glb = image->pstruct_globals() + i;
const char* pubvar_name = image->names() + glb->name;
if (strncmp(pubvar_name, "__ext_", 6) != 0)
continue;
ExtVar var;
var.autoload = false;
var.required = false;
if (!GetPstructStringField(image, glb, "file", &var.file))
continue;
if (!GetPstructStringField(image, glb, "name", &var.name))
continue;
GetPstructBoolField(image, glb, "autoload", &var.autoload);
GetPstructBoolField(image, glb, "required", &var.required);
if (!callback(pubvar_name, var))
return false;
}
if (image->pstruct_global_count())
return true;
for (uint32_t i = 0; i < pBase->GetPubVarsNum(); i++)
{
sp_pubvar_t *pubvar;
@@ -1161,14 +1269,17 @@ bool CPlugin::ForEachExtVar(const ExtVarCallback& callback)
ext = (_ext *)pubvar->offs;
ExtVar var;
if (pBase->LocalToString(ext->file, &var.file) != SP_ERROR_NONE)
char *str;
if (pBase->LocalToString(ext->file, &str) != SP_ERROR_NONE)
continue;
if (pBase->LocalToString(ext->name, &var.name) != SP_ERROR_NONE)
var.file = str;
if (pBase->LocalToString(ext->name, &str) != SP_ERROR_NONE)
continue;
var.name = str;
var.autoload = !!ext->autoload;
var.required = !!ext->required;
if (!callback(pubvar, var))
if (!callback(pubvar->name, var))
return false;
}
return true;
@@ -1210,12 +1321,12 @@ void CPlugin::SetWaitingToUnload(bool andReload)
void CPluginManager::LoadExtensions(CPlugin *pPlugin)
{
auto callback = [pPlugin] (const sp_pubvar_t *pubvar, const CPlugin::ExtVar& ext) -> bool
auto callback = [pPlugin] (const char *, const CPlugin::ExtVar& ext) -> bool
{
char path[PLATFORM_MAX_PATH];
/* Attempt to auto-load if necessary */
if (ext.autoload) {
libsys->PathFormat(path, PLATFORM_MAX_PATH, "%s", ext.file);
libsys->PathFormat(path, PLATFORM_MAX_PATH, "%s", ext.file.c_str());
g_Extensions.LoadAutoExtension(path, ext.required);
}
return true;
@@ -1226,24 +1337,24 @@ void CPluginManager::LoadExtensions(CPlugin *pPlugin)
bool CPluginManager::RequireExtensions(CPlugin *pPlugin)
{
auto callback = [pPlugin]
(const sp_pubvar_t *pubvar, const CPlugin::ExtVar& ext) -> bool
(const char *pubvar, const CPlugin::ExtVar& ext) -> bool
{
/* Is this required? */
if (ext.required) {
char path[PLATFORM_MAX_PATH];
libsys->PathFormat(path, PLATFORM_MAX_PATH, "%s", ext.file);
libsys->PathFormat(path, PLATFORM_MAX_PATH, "%s", ext.file.c_str());
IExtension *pExt = g_Extensions.FindExtensionByFile(path);
if (!pExt)
pExt = g_Extensions.FindExtensionByName(ext.name);
pExt = g_Extensions.FindExtensionByName(ext.name.c_str());
if (!pExt || !pExt->IsRunning(nullptr, 0)) {
pPlugin->EvictWithError(Plugin_Failed, "Required extension \"%s\" file(\"%s\") not running", ext.name, ext.file);
pPlugin->EvictWithError(Plugin_Failed, "Required extension \"%s\" file(\"%s\") not running", ext.name.c_str(), ext.file.c_str());
return false;
}
g_Extensions.BindChildPlugin(pExt, pPlugin);
} else {
char buffer[64];
ke::SafeSprintf(buffer, sizeof(buffer), "__ext_%s_SetNTVOptional", &pubvar->name[6]);
ke::SafeSprintf(buffer, sizeof(buffer), "__ext_%s_SetNTVOptional", &pubvar[6]);
if (IPluginFunction *pFunc = pPlugin->GetBaseContext()->GetFunctionByName(buffer)) {
cell_t res;
@@ -2404,4 +2515,4 @@ IPluginManager *CPluginManager::GetOldAPI()
{
return &sOldPluginAPI;
}
+3 -3
View File
@@ -125,13 +125,13 @@ public:
}
struct ExtVar {
char *name;
char *file;
std::string name;
std::string file;
bool autoload;
bool required;
};
typedef ke::Function<bool(const sp_pubvar_t *, const ExtVar& ext)> ExtVarCallback;
typedef ke::Function<bool(const char *pubvar_name, const ExtVar& ext)> ExtVarCallback;
bool ForEachExtVar(const ExtVarCallback& callback);
void ForEachLibrary(ke::Function<void(const char *)> callback);