Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Plugins common code, initial implementation #469

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ option(ENABLE_THEORA "Support video capture to OGG Theora? (Default: on)" ON)
option(ENABLE_TOOLS "Build different tools? (Default: off)" OFF)
option(NATIVE_OSX_APP "Support native OSX paths read data from (Default: off)" OFF)
option(FAST_MATH "Build with unsafe fast-math compiller option (Default: off)" OFF)
option(ENABLE_PLUGINS "Build with experimental plugins support (Default: off)" OFF)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type." FORCE)
Expand Down Expand Up @@ -285,6 +286,10 @@ if(OCTREE_DEBUG)
add_definitions(-DOCTREE_DEBUG)
endif()

if(ENABLE_PLUGINS)
add_definitions(-DENABLE_PLUGINS)
endif()

include_directories("${CMAKE_SOURCE_DIR}/src" ${CMAKE_BINARY_DIR})

# configure a header file to pass some of the CMake settings
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_subdirectory(celscript)
# These compiled objects are merged with the celengine library
add_subdirectory(celephem)
add_subdirectory(celcompat)
add_subdirectory(celplugin)

if (ENABLE_TOOLS)
add_subdirectory(tools)
Expand Down
69 changes: 63 additions & 6 deletions src/celengine/parseobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@
#include <celephem/spiceorbit.h>
#include <celephem/spicerotation.h>
#endif
#include <celephem/scriptorbit.h>
#include <celephem/scriptrotation.h>
#if defined(ENABLE_PLUGINS)
#include <celplugin/plugin.h>
#include <celplugin/pluginmanager.h>
#else
#include <celscript/lua/scriptorbit.h>
#include <celscript/lua/scriptrotation.h>
#endif
#include <celmath/geomutil.h>
#include <celutil/debug.h>
#include <cassert>
Expand Down Expand Up @@ -630,12 +635,15 @@ CreateSpiceRotation(Hash* rotationData,
}
#endif


#if defined(ENABLE_PLUGINS)
static CachingOrbit*
#else
static ScriptedOrbit*
#endif
CreateScriptedOrbit(Hash* orbitData,
const fs::path& path)
{
#if !defined(CELX)
#if !defined(CELX) && !defined(ENABLE_PLUGINS)
clog << "ScriptedOrbit not usable without scripting support.\n";
return nullptr;
#else
Expand All @@ -648,19 +656,42 @@ CreateScriptedOrbit(Hash* orbitData,
return nullptr;
}

#if defined(ENABLE_PLUGINS)
// If language name is missing assume Lua to be backward compatible
string language("LUA");
orbitData->getString("Language", language);
#endif

// Module name is optional
string moduleName;
orbitData->getString("Module", moduleName);

Value* pathValue = new Value(path.string());
orbitData->addValue("AddonPath", *pathValue);

#if defined(ENABLE_PLUGINS)
auto pluginManager = celestia::plugin::GetPluginManager();
if (pluginManager == nullptr)
{
fmt::print(cerr, "Error: PluginManager is not initialized!\n");
return nullptr;
}
auto plugin = pluginManager->getScriptPlugin(language);
if (plugin == nullptr)
{
fmt::print(cerr, "Support for language {} is missing\n", language);
return nullptr;
}

auto *scriptedOrbit = plugin->createScriptedOrbit(moduleName, funcName, orbitData);
#else
ScriptedOrbit* scriptedOrbit = new ScriptedOrbit();
if (!scriptedOrbit->initialize(moduleName, funcName, orbitData))
{
delete scriptedOrbit;
scriptedOrbit = nullptr;
}
#endif

return scriptedOrbit;
#endif
Expand Down Expand Up @@ -1001,12 +1032,15 @@ CreatePrecessingRotationModel(Hash* rotationData,
}
}


#if defined(ENABLE_PLUGINS)
static RotationModel*
#else
static ScriptedRotation*
#endif
CreateScriptedRotation(Hash* rotationData,
const fs::path& path)
{
#if !defined(CELX)
#if !defined(CELX) && !defined(ENABLE_PLUGINS)
clog << "ScriptedRotation not usable without scripting support.\n";
return nullptr;
#else
Expand All @@ -1019,19 +1053,42 @@ CreateScriptedRotation(Hash* rotationData,
return nullptr;
}

#if defined(ENABLE_PLUGINS)
// If language name is missing assume Lua to be backward compatible
string language("LUA");
rotationData->getString("LUA", language);
#endif

// Module name is optional
string moduleName;
rotationData->getString("Module", moduleName);

Value* pathValue = new Value(path.string());
rotationData->addValue("AddonPath", *pathValue);

#if defined(ENABLE_PLUGINS)
auto pluginManager = celestia::plugin::GetPluginManager();
if (pluginManager == nullptr)
{
fmt::print(cerr, "Error: PluginManager is not initialized!\n");
return nullptr;
}
auto plugin = pluginManager->getScriptPlugin(language);
if (plugin == nullptr)
{
fmt::print(cerr, "Support for language {} is missing\n", language);
return nullptr;
}

auto *scriptedRotation = plugin->createScriptedRotation(moduleName, funcName, rotationData);
#else
ScriptedRotation* scriptedRotation = new ScriptedRotation();
if (!scriptedRotation->initialize(moduleName, funcName, rotationData))
{
delete scriptedRotation;
scriptedRotation = nullptr;
}
#endif

return scriptedRotation;
#endif
Expand Down
12 changes: 0 additions & 12 deletions src/celephem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ if(ENABLE_SPICE)
)
endif()


if(ENABLE_CELX)
list(APPEND CELEPHEM_SOURCES
scriptobject.cpp
scriptobject.h
scriptorbit.cpp
scriptorbit.h
scriptrotation.cpp
scriptrotation.h
)
endif()

# These object files are merged in the celegine library
add_library(celephem OBJECT ${CELEPHEM_SOURCES})

Expand Down
2 changes: 1 addition & 1 deletion src/celestia/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ endif()
add_library(celestia STATIC ${CELESTIA_SOURCES}
$<TARGET_OBJECTS:celcommonscript>
$<TARGET_OBJECTS:cellegacyscript>
$<TARGET_OBJECTS:celluascript>)
$<TARGET_OBJECTS:celplugin>)

#[[
add_library(celestia SHARED ${CELESTIA_SOURCES})
Expand Down
24 changes: 14 additions & 10 deletions src/celestia/celestiacore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// keyboard events. CelestiaCore then turns those events into calls
// to Renderer and Simulation.
//
// Copyright (C) 2001-2009, the Celestia Development Team
// Copyright (C) 2001-2019, the Celestia Development Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -54,11 +54,8 @@
#include <celutil/color.h>
#include <celengine/vecgl.h>
#include <celengine/rectangle.h>

#ifdef CELX
#include <celephem/scriptobject.h>
#endif

#include <celplugin/plugin.h>
#include <celplugin/pluginmanager.h>
#include "imagecapture.h"

// TODO: proper gettext
Expand All @@ -69,6 +66,7 @@ using namespace Eigen;
using namespace std;
using namespace celmath;
using namespace celestia::scripts;
using namespace celestia::plugin;

static const int DragThreshold = 3;

Expand Down Expand Up @@ -146,9 +144,13 @@ CelestiaCore::CelestiaCore() :
renderer(new Renderer()),
timer(new Timer()),
m_legacyPlugin(make_unique<LegacyScriptPlugin>(this)),
#if defined(CELX) && !defined(ENABLE_PLUGINS)
m_luaPlugin(make_unique<LuaScriptPlugin>(this)),
m_scriptMaps(make_shared<ScriptMaps>())
#endif
m_scriptMaps(make_shared<ScriptMaps>()),
m_pluginManager(make_unique<PluginManager>(this))
{
SetPluginManager(m_pluginManager.get());

for (int i = 0; i < KeyCount; i++)
{
Expand All @@ -165,6 +167,8 @@ CelestiaCore::CelestiaCore() :

CelestiaCore::~CelestiaCore()
{
SetPluginManager(nullptr);

if (movieCapture != nullptr)
recordEnd();

Expand Down Expand Up @@ -300,7 +304,7 @@ void CelestiaCore::runScript(const fs::path& filename)
if (m_script != nullptr)
scriptState = sim->getPauseState() ? ScriptPaused : ScriptRunning;
}
#ifdef CELX
#if defined(CELX) && !defined(ENABLE_PLUGINS)
else if (m_luaPlugin->isOurFile(localeFilename))
{
m_script = m_luaPlugin->loadScript(localeFilename);
Expand Down Expand Up @@ -3601,7 +3605,7 @@ bool CelestiaCore::initSimulation(const fs::path& configFileName,
}
}

#ifdef CELX
#if defined(CELX) && !defined(ENABLE_PLUGINS)
initLuaHook(progressNotifier);
#endif

Expand Down Expand Up @@ -4397,7 +4401,7 @@ bool CelestiaCore::referenceMarkEnabled(const string& refMark, Selection sel) co
}


#ifdef CELX
#if defined(CELX) && !defined(ENABLE_PLUGINS)
bool CelestiaCore::initLuaHook(ProgressNotifier* progressNotifier)
{
return CreateLuaEnvironment(this, config, progressNotifier);
Expand Down
15 changes: 13 additions & 2 deletions src/celestia/celestiacore.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ class Url;

// class CelestiaWatcher;
class CelestiaCore;

namespace celestia
{
namespace plugin
{
class PluginManager;
class Plugin;
}
}
// class astro::Date;

typedef Watcher<CelestiaCore> CelestiaWatcher;
Expand Down Expand Up @@ -343,7 +350,7 @@ class CelestiaCore // : public Watchable<CelestiaCore>
protected:
bool readStars(const CelestiaConfig&, ProgressNotifier*);
void renderOverlay();
#ifdef CELX
#if defined(CELX) && !defined(ENABLE_PLUGINS)
bool initLuaHook(ProgressNotifier*);
#endif // CELX

Expand Down Expand Up @@ -399,9 +406,13 @@ class CelestiaCore // : public Watchable<CelestiaCore>
std::unique_ptr<celestia::scripts::IScript> m_script;
std::unique_ptr<celestia::scripts::IScriptHook> m_scriptHook;
std::unique_ptr<celestia::scripts::LegacyScriptPlugin> m_legacyPlugin;
#if defined(CELX) && !defined(ENABLE_PLUGINS)
std::unique_ptr<celestia::scripts::LuaScriptPlugin> m_luaPlugin;
#endif
std::shared_ptr<celestia::scripts::ScriptMaps> m_scriptMaps;

std::unique_ptr<celestia::plugin::PluginManager> m_pluginManager;

enum ScriptState
{
ScriptCompleted,
Expand Down
10 changes: 10 additions & 0 deletions src/celplugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# These object files are merged in the celegine library
set(CELPLUGIN_SOURCES
plugin-common.h
plugin.cpp
plugin.h
pluginmanager.cpp
pluginmanager.h
)
add_library(celplugin OBJECT ${CELPLUGIN_SOURCES})

48 changes: 48 additions & 0 deletions src/celplugin/host.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#define PUBLIC_GET_INFO
#include <fmt/printf.h>
#include "plugin.h"
#include "pluginmanager.h"
#include <iostream>
#ifdef _WIN32
#include <direct.h> // _getcwd
#else
#include <unistd.h> // getcwd
#endif

using namespace celestia::plugin;

int main()
{
#ifdef _WIN32
wchar_t *cwd = _wgetcwd(nullptr, 0);
if (cwd == nullptr) cwd = L"";
#else
char *cwd = getcwd(nullptr, 0);
if (cwd == nullptr) cwd = "";
#endif

PluginManager pm;
pm.setSearchDirectory(cwd);
const Plugin* p = pm.loadByName("myplug");
if (p == nullptr)
{
std::cout << "load failed\n";
return 1;
}

const PluginInfo *pi = p->getPluginInfo();
fmt::printf("APIVersion = %hx, Type = %hu, ID = %p\n", pi->APIVersion, pi->Type, fmt::ptr(pi->ID));

if (p->getType() == Scripting)
{
fmt::printf("%s\n", p->getScriptLanguage());
fmt::printf("%p %p\n", fmt::ptr(p), fmt::ptr(pm.getScriptPlugin("lUa")));
}
else
{
void (*myfn)() = reinterpret_cast<void(*)()>(pi->ID);
(*myfn)();
}

return 0;
}
15 changes: 15 additions & 0 deletions src/celplugin/myplug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include "plugin-common.h"

using namespace celestia::plugin;

static void myfn()
{
std::cout << "hi from plugin!\n";
}

CELESTIA_PLUGIN_ENTRYPOINT()
{
static PluginInfo pinf(Scripting, "LUA");
return &pinf;
}
Loading