Set currentModelPath in restore() - even if it is null or empty.

Set it to empty and clear the current model if a model fails to load.
This commit is contained in:
Mike Oliphant
2023-07-04 13:03:46 -07:00
parent cde3df4e84
commit 33a0c08327
+36 -27
View File
@@ -85,11 +85,14 @@ namespace NAM {
auto msg = static_cast<const LV2LoadModelMsg*>(data); auto msg = static_cast<const LV2LoadModelMsg*>(data);
auto nam = static_cast<NAM::Plugin*>(instance); auto nam = static_cast<NAM::Plugin*>(instance);
::DSP* model = nullptr;
LV2SwitchModelMsg response = { kWorkTypeSwitch, {}, {} };
LV2_Worker_Status result = LV2_WORKER_SUCCESS;
try try
{ {
// load model from path // load model from path
const size_t pathlen = strlen(msg->path); const size_t pathlen = strlen(msg->path);
::DSP* model;
if (pathlen == 0 || pathlen >= MAX_FILE_NAME) if (pathlen == 0 || pathlen >= MAX_FILE_NAME)
{ {
@@ -119,18 +122,22 @@ namespace NAM {
} }
} }
LV2SwitchModelMsg response = { kWorkTypeSwitch, {}, model }; response.model = model;
memcpy(response.path, msg->path, pathlen);
respond(handle, sizeof(response), &response);
return LV2_WORKER_SUCCESS; memcpy(response.path, msg->path, pathlen);
} }
catch (std::exception& e) catch (std::exception& e)
{ {
response.path[0] = '\0';
lv2_log_error(&nam->logger, "Unable to load model from: '%s'\n", msg->path); lv2_log_error(&nam->logger, "Unable to load model from: '%s'\n", msg->path);
result = LV2_WORKER_ERR_UNKNOWN;
} }
break; respond(handle, sizeof(response), &response);
return result;
} }
case kWorkTypeFree: case kWorkTypeFree:
@@ -331,8 +338,6 @@ namespace NAM {
LV2_State_Status Plugin::restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle, LV2_State_Status Plugin::restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle,
uint32_t flags, const LV2_Feature* const* features) uint32_t flags, const LV2_Feature* const* features)
{ {
//if (!haveLog) return LV2_STATE_SUCCESS;
auto nam = static_cast<NAM::Plugin*>(instance); auto nam = static_cast<NAM::Plugin*>(instance);
// Get model_Path from state // Get model_Path from state
@@ -343,16 +348,17 @@ namespace NAM {
lv2_log_trace(&nam->logger, "Restoring model '%s'\n", (const char*)value); lv2_log_trace(&nam->logger, "Restoring model '%s'\n", (const char*)value);
if (!value) { NAM::LV2LoadModelMsg msg = { NAM::kWorkTypeLoad, {} };
lv2_log_error(&nam->logger, "Missing model_Path\n");
return LV2_STATE_ERR_NO_PROPERTY;
}
if (type != nam->uris.atom_Path) { LV2_State_Status result = LV2_STATE_SUCCESS;
lv2_log_error(&nam->logger, "Non-path model_Path\n");
return LV2_STATE_ERR_BAD_TYPE;
}
// Check if a path is set
if (!value || (type != nam->uris.atom_Path))
{
msg.path[0] = '\0';
}
else
{
LV2_State_Map_Path* map_path = (LV2_State_Map_Path*)lv2_features_data(features, LV2_STATE__mapPath); LV2_State_Map_Path* map_path = (LV2_State_Map_Path*)lv2_features_data(features, LV2_STATE__mapPath);
if (map_path == nullptr) if (map_path == nullptr)
@@ -367,22 +373,16 @@ namespace NAM {
size_t pathLen = strlen(path); size_t pathLen = strlen(path);
LV2_State_Status result = LV2_STATE_SUCCESS; if (pathLen >= MAX_FILE_NAME)
if (pathLen < MAX_FILE_NAME)
{
// Schedule model to be loaded by the provided worker
NAM::LV2LoadModelMsg msg = { NAM::kWorkTypeLoad, {} };
memcpy(msg.path, path, pathLen);
nam->schedule->schedule_work(nam->schedule->handle, sizeof(msg), &msg);
}
else
{ {
lv2_log_error(&nam->logger, "Model path is too long (max %u chars)\n", MAX_FILE_NAME); lv2_log_error(&nam->logger, "Model path is too long (max %u chars)\n", MAX_FILE_NAME);
result = LV2_STATE_ERR_UNKNOWN; result = LV2_STATE_ERR_UNKNOWN;
} }
else
{
memcpy(msg.path, path, pathLen);
}
LV2_State_Free_Path* free_path = (LV2_State_Free_Path*)lv2_features_data(features, LV2_STATE__freePath); LV2_State_Free_Path* free_path = (LV2_State_Free_Path*)lv2_features_data(features, LV2_STATE__freePath);
@@ -396,6 +396,15 @@ namespace NAM {
free(path); free(path);
#endif #endif
} }
}
if (result == LV2_STATE_SUCCESS)
{
// Schedule model to be loaded by the provided worker
nam->schedule->schedule_work(nam->schedule->handle, sizeof(msg), &msg);
nam->currentModelPath = msg.path;
}
return result; return result;
} }