Allow wasm fetch patchstorage thigns, set system factory template

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2022-07-09 00:17:07 +01:00
parent e389ca7469
commit 3af971534c
No known key found for this signature in database
GPG key ID: CDBAA37ABC74FBA0
7 changed files with 248 additions and 38 deletions

View file

@ -34,24 +34,38 @@
# undef DEBUG
#endif
#ifdef HAVE_LIBLO
# ifdef HEADLESS
# include <lo/lo.h>
# include "extra/Thread.hpp"
# endif
# include "CardinalCommon.hpp"
#if defined(HAVE_LIBLO) && defined(HEADLESS)
# include <lo/lo.h>
# include "extra/Thread.hpp"
#endif
#include <list>
#include "CardinalCommon.hpp"
#include "DistrhoPluginUtils.hpp"
#include "PluginContext.hpp"
#include "extra/Base64.hpp"
#ifndef DISTRHO_OS_WASM
#ifdef DISTRHO_OS_WASM
# include <emscripten/emscripten.h>
#else
# include "extra/SharedResourcePointer.hpp"
#endif
#if CARDINAL_VARIANT_FX
# define CARDINAL_FACTORY_TEMPLATE_NAME "template-fx.vcv"
#elif CARDINAL_VARIANT_SYNTH
# define CARDINAL_FACTORY_TEMPLATE_NAME "template-synth.vcv"
#else
# define CARDINAL_FACTORY_TEMPLATE_NAME "template.vcv"
#endif
#ifdef DISTRHO_OS_WASM
# define CARDINAL_TEMPLATE_NAME "template-wasm.vcv"
#else
# define CARDINAL_TEMPLATE_NAME CARDINAL_FACTORY_TEMPLATE_NAME
#endif
static const constexpr uint kCardinalStateBaseCount = 3; // patch, screenshot, comment
#ifndef HEADLESS
@ -63,14 +77,6 @@ static const constexpr uint kCardinalStateCount = kCardinalStateBaseCount + 2; /
static const constexpr uint kCardinalStateCount = kCardinalStateBaseCount;
#endif
#if CARDINAL_VARIANT_FX
# define CARDINAL_TEMPLATE_NAME "template-fx.vcv"
#elif CARDINAL_VARIANT_SYNTH
# define CARDINAL_TEMPLATE_NAME "template-synth.vcv"
#else
# define CARDINAL_TEMPLATE_NAME "template.vcv"
#endif
namespace rack {
namespace engine {
void Engine_setAboutToClose(Engine*);
@ -97,6 +103,21 @@ bool d_isDiffHigherThanLimit(const T& v1, const T& v2, const T& limit)
// -----------------------------------------------------------------------------------------------------------
#ifdef DISTRHO_OS_WASM
EM_JS(char*, getPatchStorageSlug, (), {
var searchParams = new URLSearchParams(window.location.search);
var patch = searchParams.get('patchstorage');
if (!patch)
return null;
var length = lengthBytesUTF8(patch) + 1;
var str = _malloc(length);
stringToUTF8(patch, str, length);
return str;
});
#endif
// -----------------------------------------------------------------------------------------------------------
struct Initializer
#if defined(HAVE_LIBLO) && defined(HEADLESS)
: public Thread
@ -107,6 +128,7 @@ struct Initializer
CardinalBasePlugin* oscPlugin = nullptr;
#endif
std::string templatePath;
std::string factoryTemplatePath;
Initializer(const CardinalBasePlugin* const plugin)
{
@ -158,6 +180,7 @@ struct Initializer
asset::bundlePath = system::join(resourcePath, "PluginManifests");
asset::systemDir = resourcePath;
templatePath = system::join(asset::systemDir, CARDINAL_TEMPLATE_NAME);
factoryTemplatePath = system::join(asset::systemDir, CARDINAL_FACTORY_TEMPLATE_NAME);
}
}
@ -170,6 +193,7 @@ struct Initializer
if (system::exists(system::join(asset::systemDir, "res")))
{
templatePath = CARDINAL_PLUGIN_SOURCE_DIR DISTRHO_OS_SEP_STR CARDINAL_TEMPLATE_NAME;
factoryTemplatePath = CARDINAL_PLUGIN_SOURCE_DIR DISTRHO_OS_SEP_STR CARDINAL_FACTORY_TEMPLATE_NAME;
}
// If source code dir does not exist use install target prefix as system dir
else
@ -187,17 +211,20 @@ struct Initializer
asset::systemDir = CARDINAL_PLUGIN_PREFIX "/share/cardinal";
#endif
if (! asset::systemDir.empty())
{
asset::bundlePath = system::join(asset::systemDir, "PluginManifests");
templatePath = system::join(asset::systemDir, CARDINAL_TEMPLATE_NAME);
}
asset::bundlePath = system::join(asset::systemDir, "PluginManifests");
templatePath = system::join(asset::systemDir, CARDINAL_TEMPLATE_NAME);
factoryTemplatePath = system::join(asset::systemDir, CARDINAL_FACTORY_TEMPLATE_NAME);
}
}
asset::userDir = asset::systemDir;
}
#ifdef DISTRHO_OS_WASM
if ((patchStorageSlug = getPatchStorageSlug()) != nullptr)
templatePath = CARDINAL_IMPORTED_TEMPLATE_FILENAME;
#endif
// Log environment
INFO("%s %s v%s", APP_NAME.c_str(), APP_EDITION.c_str(), APP_VERSION.c_str());
INFO("%s", system::getOperatingSystemInfo().c_str());
@ -206,6 +233,7 @@ struct Initializer
INFO("System directory: %s", asset::systemDir.c_str());
INFO("User directory: %s", asset::userDir.c_str());
INFO("Template patch: %s", templatePath.c_str());
INFO("System template patch: %s", factoryTemplatePath.c_str());
// Report to user if something is wrong with the installation
if (asset::systemDir.empty())
@ -564,6 +592,7 @@ public:
context->patch = new rack::patch::Manager;
context->patch->autosavePath = fAutosavePath;
context->patch->templatePath = fInitializer->templatePath;
context->patch->factoryTemplatePath = fInitializer->factoryTemplatePath;
context->event = new rack::widget::EventState;
context->scene = new rack::app::Scene;
@ -572,28 +601,33 @@ public:
if (! isDummyInstance())
context->window = new rack::window::Window;
context->patch->loadTemplate();
context->scene->rackScroll->reset();
#ifdef DISTRHO_OS_WASM
if (rack::patchStorageSlug == nullptr)
#endif
{
context->patch->loadTemplate();
context->scene->rackScroll->reset();
}
#if defined(HAVE_LIBLO) && defined(HEADLESS)
#if defined(HAVE_LIBLO) && defined(HEADLESS)
fInitializer->oscPlugin = this;
#endif
#endif
}
~CardinalPlugin() override
{
#if defined(HAVE_LIBLO) && defined(HEADLESS)
#if defined(HAVE_LIBLO) && defined(HEADLESS)
fInitializer->oscPlugin = nullptr;
#endif
#endif
{
const ScopedContext sc(this);
context->patch->clear();
// do a little dance to prevent context scene deletion from saving to temp dir
#ifndef HEADLESS
#ifndef HEADLESS
const ScopedValueSetter<bool> svs(rack::settings::headless, true);
#endif
#endif
Engine_setAboutToClose(context->engine);
delete context;
}