#include "Libraries/Async/Async.h"
#include "Libraries/FileSystemWatcher/FileSystemWatcher.h"
namespace SC
{
struct IPluginContract
{
static constexpr auto InterfaceHash =
PluginHash(
"IPluginContract");
Function<void(void)> onDraw;
};
struct PluginClient : public IPluginContract
{
PluginClient()
{
IPluginContract::onDraw = []()
{
};
}
bool init() { return true; }
bool close() { return true; }
};
SC_PLUGIN_DEFINE(PluginClient);
SC_PLUGIN_EXPORT_INTERFACES(PluginClient, IPluginContract);
struct PluginHost
{
StringPath executablePath;
StringPath libraryRootDirectory;
StringPath someLibraryDirectory;
StringPath pluginsPath;
{
eventLoop = &loop;
SC_TRY(PluginCompiler::findBestCompiler(compiler));
SC_TRY(PluginSysroot::findBestSysroot(compiler.
type, sysroot));
SC_TRY(compiler.
includePaths.push_back(libraryRootDirectory));
SC_TRY(compiler.
includePaths.push_back(someLibraryDirectory));
fileSystemWatcherRunner.init(*eventLoop);
SC_TRY(fileSystemWatcher.init(fileSystemWatcherRunner));
watcher.notifyCallback.bind<PluginHost, &PluginHost::onFileChanged>(*this);
SC_TRY(fileSystemWatcher.watch(watcher, pluginsPath.view()));
return Result(true);
}
Result close()
{
SC_TRY(fileSystemWatcher.close());
eventLoop = nullptr;
return Result(true);
}
Result syncRegistry()
{
Span<PluginDefinition> definitionsSpan;
SC_TRY(PluginScanner::scanDirectory(pluginsPath.view(), definitions, fileStorage, definitionsSpan))
return Result(true);
}
{
SC_TRY(registry.
loadPlugin(identifier, compiler, sysroot, executablePath.view(),
PluginRegistry::LoadMode::Reload));
return Result(true);
}
void draw()
{
if (contract)
{
contract->onDraw();
}
}
private:
IPluginContract* contract = nullptr;
FileSystemWatcherAsync fileSystemWatcherRunner;
{
auto reload = [this](const PluginIdentifier& plugin) { (void)load(plugin.view()); };
}
};
}
constexpr unsigned int PluginHash(const char(&str)[N])
Compute compile time FNV hash for a char array.
Definition PluginHash.h:33
StringSpan relativePath
Relative path of the file being notified from basePath
Definition FileSystemWatcher.h:130
Reads and holds CFLAGS and LDFLAGS environment variables, mainly to pass down sysroot location.
Definition Plugin.h:253
Compiles a plugin to a dynamic library.
Definition Plugin.h:186
FixedVector< StringPath, 8 > includePaths
Path to include directories used to compile plugin.
Definition Plugin.h:217
Type type
Compile Type.
Definition Plugin.h:213
Result compile(const PluginDefinition &definition, const PluginSysroot &sysroot, const PluginCompilerEnvironment &environment, Span< char > &compilerLog) const
Compiles a Definition to an object file.
Plugin description, category, dependencies, files and directory location.
Definition Plugin.h:114
PluginIdentity identity
Uniquely identifier a plugin.
Definition Plugin.h:115
FixedString< 256 > description
Long description of plugin.
Definition Plugin.h:117
FixedVector< PluginIdentifier, 8 > dependencies
Dependencies necessary to load this plugin.
Definition Plugin.h:121
FixedString< 64 > category
Category where plugin belongs to.
Definition Plugin.h:118
FixedVector< PluginBuildOption, 8 > build
Build options.
Definition Plugin.h:122
A plugin dynamic library loaded from a SC::PluginRegistry.
Definition Plugin.h:263
SystemDynamicLibrary dynamicLibrary
System handle of plugin's dynamic library.
Definition Plugin.h:265
bool queryInterface(T *&outInterface) const
Try to obtain a given interface as exported by a plugin through SC_PLUGIN_EXPORT_INTERFACES macro.
Definition Plugin.h:276
PluginIdentifier identifier
Unique string identifying the plugin.
Definition Plugin.h:102
FixedString< 64 > name
Plugin name.
Definition Plugin.h:103
FixedString< 16 > version
Plugin version (x.y.z)
Definition Plugin.h:104
Holds a registry of plugins, loading and compiling them on the fly.
Definition Plugin.h:301
void getPluginsToReloadBecauseOf(StringView relativePath, TimeMs tolerance, Function< void(const PluginIdentifier &)> onPlugin)
Enumerates all plugins that must be reloaded when relativePath is modified.
void init(Span< PluginDynamicLibrary > librariesStorage)
Init a PluginRegistry with some given storage.
Result unloadPlugin(const StringView identifier)
Unloads an already loaded plugin by its identifier.
Result loadPlugin(const StringView identifier, const PluginCompiler &compiler, const PluginSysroot &sysroot, StringView executablePath, LoadMode loadMode=LoadMode::Load)
Loads a plugin with given identifier, compiling it with given PluginCompiler.
Result removeAllBuildProducts(const StringView identifier)
Removes all temporary build products of the Plugin with given identifier.
Result replaceDefinitions(Span< PluginDefinition > &&definitions)
Appends the definitions to registry.
PluginDynamicLibrary * findPlugin(const StringView identifier)
Find a PluginDynamicLibrary in the registry with a given identifier.
Holds include and library paths for a system toolchain, used to let plugins link to libc and libc++.
Definition Plugin.h:238
StringPath executableFile
Path to current executable.
Definition Testing.h:66
StringPath libraryRootDirectory
Path to sources directory for library.
Definition Testing.h:65
Type-safe wrapper of uint64 used to represent milliseconds.
Definition Time.h:50