2011-06-24 21:35:29 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
2012-09-29 20:03:37 -06:00
|
|
|
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
|
2011-06-24 21:35:29 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Export.h"
|
|
|
|
#include "Hooks.h"
|
2012-03-10 04:55:42 -07:00
|
|
|
#include "ColorText.h"
|
2014-12-02 19:44:20 -07:00
|
|
|
#include "MiscUtils.h"
|
2011-06-24 21:35:29 -06:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-03-14 09:57:29 -06:00
|
|
|
|
2012-05-18 07:54:05 -06:00
|
|
|
#include "Core.h"
|
2021-06-02 15:49:19 -06:00
|
|
|
#include "DataFuncs.h"
|
2012-05-18 07:54:05 -06:00
|
|
|
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
typedef struct lua_State lua_State;
|
|
|
|
|
2011-07-26 21:59:09 -06:00
|
|
|
namespace tthread
|
|
|
|
{
|
|
|
|
class mutex;
|
|
|
|
class condition_variable;
|
|
|
|
}
|
2011-12-30 12:25:50 -07:00
|
|
|
namespace df
|
|
|
|
{
|
|
|
|
struct viewscreen;
|
|
|
|
}
|
2011-06-24 21:35:29 -06:00
|
|
|
namespace DFHack
|
|
|
|
{
|
|
|
|
class Core;
|
|
|
|
class PluginManager;
|
2012-01-11 07:54:54 -07:00
|
|
|
class virtual_identity;
|
2012-03-15 03:01:23 -06:00
|
|
|
class RPCService;
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
class function_identity_base;
|
2012-04-15 09:09:25 -06:00
|
|
|
namespace Lua {
|
|
|
|
class Notification;
|
|
|
|
}
|
2011-12-30 12:25:50 -07:00
|
|
|
|
2015-04-03 12:02:14 -06:00
|
|
|
namespace Version {
|
|
|
|
const char *dfhack_version();
|
2015-06-24 17:32:45 -06:00
|
|
|
const char *git_description();
|
2018-03-10 14:55:00 -07:00
|
|
|
int dfhack_abi_version();
|
2015-04-03 12:02:14 -06:00
|
|
|
}
|
|
|
|
|
2012-04-14 17:15:15 -06:00
|
|
|
// anon type, pretty much
|
|
|
|
struct DFLibrary;
|
|
|
|
|
2018-06-28 15:23:55 -06:00
|
|
|
// DFLibrary* that can be used to resolve global names
|
|
|
|
extern DFLibrary* GLOBAL_NAMES;
|
2012-04-14 17:15:15 -06:00
|
|
|
// Open a plugin library
|
2012-06-02 15:35:05 -06:00
|
|
|
DFHACK_EXPORT DFLibrary * OpenPlugin (const char * filename);
|
2012-04-14 17:15:15 -06:00
|
|
|
// find a symbol inside plugin
|
2012-06-02 15:35:05 -06:00
|
|
|
DFHACK_EXPORT void * LookupPlugin (DFLibrary * plugin ,const char * function);
|
2012-04-14 17:15:15 -06:00
|
|
|
// Close a plugin library
|
2012-06-02 15:35:05 -06:00
|
|
|
DFHACK_EXPORT void ClosePlugin (DFLibrary * plugin);
|
2012-04-14 17:15:15 -06:00
|
|
|
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
struct DFHACK_EXPORT CommandReg {
|
|
|
|
const char *name;
|
|
|
|
int (*command)(lua_State*);
|
|
|
|
};
|
|
|
|
struct DFHACK_EXPORT FunctionReg {
|
|
|
|
const char *name;
|
|
|
|
function_identity_base *identity;
|
2011-12-30 07:12:15 -07:00
|
|
|
};
|
2012-04-15 09:09:25 -06:00
|
|
|
struct DFHACK_EXPORT EventReg {
|
|
|
|
const char *name;
|
|
|
|
Lua::Notification *event;
|
|
|
|
};
|
2011-12-30 12:25:50 -07:00
|
|
|
struct DFHACK_EXPORT PluginCommand
|
2011-06-24 21:35:29 -06:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
typedef command_result (*command_function)(color_ostream &out, std::vector <std::string> &);
|
|
|
|
typedef bool (*command_hotkey_guard)(df::viewscreen *);
|
2011-12-31 02:25:46 -07:00
|
|
|
|
2011-08-05 20:37:29 -06:00
|
|
|
/// create a command with a name, description, function pointer to its code
|
|
|
|
/// and saying if it needs an interactive terminal
|
|
|
|
/// Most commands shouldn't require an interactive terminal!
|
2022-07-17 18:05:29 -06:00
|
|
|
/// Note that the description and usage fields are only used for
|
|
|
|
/// out-of-tree plugins that do not have rendered help installed in
|
|
|
|
/// the hack/docs directory. Help for all internal plugins comes from
|
|
|
|
/// the rendered .rst files.
|
2011-06-24 21:35:29 -06:00
|
|
|
PluginCommand(const char * _name,
|
|
|
|
const char * _description,
|
2011-12-30 12:25:50 -07:00
|
|
|
command_function function_,
|
2011-12-31 02:25:46 -07:00
|
|
|
bool interactive_ = false,
|
|
|
|
const char * usage_ = ""
|
2011-06-24 21:35:29 -06:00
|
|
|
)
|
2011-12-30 12:25:50 -07:00
|
|
|
: name(_name), description(_description),
|
|
|
|
function(function_), interactive(interactive_),
|
2011-12-31 02:25:46 -07:00
|
|
|
guard(NULL), usage(usage_)
|
2011-06-24 21:35:29 -06:00
|
|
|
{
|
2015-02-16 18:17:47 -07:00
|
|
|
fix_usage();
|
2011-06-24 21:35:29 -06:00
|
|
|
}
|
2011-12-30 12:25:50 -07:00
|
|
|
|
|
|
|
PluginCommand(const char * _name,
|
|
|
|
const char * _description,
|
|
|
|
command_function function_,
|
|
|
|
command_hotkey_guard guard_,
|
2011-12-31 02:25:46 -07:00
|
|
|
const char * usage_ = "")
|
2011-12-30 12:25:50 -07:00
|
|
|
: name(_name), description(_description),
|
|
|
|
function(function_), interactive(false),
|
2011-12-31 02:25:46 -07:00
|
|
|
guard(guard_), usage(usage_)
|
2011-06-24 21:35:29 -06:00
|
|
|
{
|
2015-02-16 18:17:47 -07:00
|
|
|
fix_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void fix_usage()
|
|
|
|
{
|
|
|
|
if (usage.size() && usage[usage.size() - 1] != '\n')
|
|
|
|
usage.push_back('\n');
|
2011-06-24 21:35:29 -06:00
|
|
|
}
|
2011-12-30 12:25:50 -07:00
|
|
|
|
2011-12-31 02:25:46 -07:00
|
|
|
bool isHotkeyCommand() const { return guard != NULL; }
|
|
|
|
|
2011-06-24 21:35:29 -06:00
|
|
|
std::string name;
|
|
|
|
std::string description;
|
2011-12-30 12:25:50 -07:00
|
|
|
command_function function;
|
2011-08-05 20:37:29 -06:00
|
|
|
bool interactive;
|
2011-12-30 12:25:50 -07:00
|
|
|
command_hotkey_guard guard;
|
2011-12-31 02:25:46 -07:00
|
|
|
std::string usage;
|
2011-06-24 21:35:29 -06:00
|
|
|
};
|
|
|
|
class Plugin
|
|
|
|
{
|
2011-07-18 08:22:49 -06:00
|
|
|
struct RefLock;
|
2012-02-21 10:19:17 -07:00
|
|
|
struct RefAutolock;
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
struct RefAutoinc;
|
2011-06-24 21:35:29 -06:00
|
|
|
friend class PluginManager;
|
2012-03-15 03:01:23 -06:00
|
|
|
friend class RPCService;
|
2015-08-14 14:11:23 -06:00
|
|
|
Plugin(DFHack::Core* core, const std::string& filepath,
|
|
|
|
const std::string &plug_name, PluginManager * pm);
|
2011-06-24 21:35:29 -06:00
|
|
|
~Plugin();
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result on_update(color_ostream &out);
|
|
|
|
command_result on_state_change(color_ostream &out, state_change_event event);
|
2018-08-26 17:26:09 -06:00
|
|
|
command_result save_data(color_ostream &out);
|
|
|
|
command_result load_data(color_ostream &out);
|
2012-03-15 03:01:23 -06:00
|
|
|
void detach_connection(RPCService *svc);
|
2011-07-18 08:22:49 -06:00
|
|
|
public:
|
2015-08-14 14:11:23 -06:00
|
|
|
enum plugin_state
|
|
|
|
{
|
|
|
|
PS_UNLOADED,
|
|
|
|
PS_LOADED,
|
|
|
|
PS_BROKEN,
|
|
|
|
PS_LOADING,
|
|
|
|
PS_UNLOADING,
|
|
|
|
PS_DELETED
|
|
|
|
};
|
|
|
|
static const char *getStateDescription (plugin_state state);
|
2012-03-15 03:01:23 -06:00
|
|
|
bool load(color_ostream &out);
|
|
|
|
bool unload(color_ostream &out);
|
|
|
|
bool reload(color_ostream &out);
|
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
bool can_be_enabled() { return plugin_is_enabled != 0; }
|
|
|
|
bool is_enabled() { return plugin_is_enabled && *plugin_is_enabled; }
|
|
|
|
bool can_set_enabled() { return plugin_is_enabled != 0 && plugin_enable; }
|
|
|
|
command_result set_enabled(color_ostream &out, bool enable);
|
|
|
|
|
2012-03-15 03:01:23 -06:00
|
|
|
command_result invoke(color_ostream &out, const std::string & command, std::vector <std::string> & parameters);
|
|
|
|
bool can_invoke_hotkey(const std::string & command, df::viewscreen *top );
|
2011-07-18 08:22:49 -06:00
|
|
|
plugin_state getState () const;
|
2012-03-15 03:01:23 -06:00
|
|
|
|
|
|
|
RPCService *rpc_connect(color_ostream &out);
|
|
|
|
|
2011-06-26 20:49:56 -06:00
|
|
|
const PluginCommand& operator[] (std::size_t index) const
|
2011-06-25 00:05:17 -06:00
|
|
|
{
|
|
|
|
return commands[index];
|
|
|
|
};
|
2011-06-26 20:49:56 -06:00
|
|
|
std::size_t size() const
|
2011-06-25 00:05:17 -06:00
|
|
|
{
|
|
|
|
return commands.size();
|
|
|
|
}
|
2011-06-26 20:49:56 -06:00
|
|
|
const std::string & getName() const
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
2015-08-14 14:11:23 -06:00
|
|
|
plugin_state getState()
|
|
|
|
{
|
|
|
|
return state;
|
|
|
|
}
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
|
|
|
void open_lua(lua_State *state, int table);
|
|
|
|
|
2011-06-24 21:35:29 -06:00
|
|
|
private:
|
2011-07-18 08:22:49 -06:00
|
|
|
RefLock * access;
|
2011-06-24 21:35:29 -06:00
|
|
|
std::vector <PluginCommand> commands;
|
2012-03-15 03:01:23 -06:00
|
|
|
std::vector <RPCService*> services;
|
2015-08-14 14:11:23 -06:00
|
|
|
std::string path;
|
2011-06-24 21:35:29 -06:00
|
|
|
std::string name;
|
|
|
|
DFLibrary * plugin_lib;
|
2011-07-18 08:22:49 -06:00
|
|
|
PluginManager * parent;
|
|
|
|
plugin_state state;
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
2012-08-23 09:27:12 -06:00
|
|
|
struct LuaCommand;
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
std::map<std::string, LuaCommand*> lua_commands;
|
|
|
|
static int lua_cmd_wrapper(lua_State *state);
|
|
|
|
|
2012-08-23 09:27:12 -06:00
|
|
|
struct LuaFunction;
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
std::map<std::string, LuaFunction*> lua_functions;
|
|
|
|
static int lua_fun_wrapper(lua_State *state);
|
2012-04-15 09:09:25 -06:00
|
|
|
void push_function(lua_State *state, LuaFunction *fn);
|
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
static int lua_is_enabled(lua_State *state);
|
|
|
|
static int lua_set_enabled(lua_State *state);
|
|
|
|
|
2012-08-23 09:27:12 -06:00
|
|
|
struct LuaEvent;
|
2012-04-15 09:09:25 -06:00
|
|
|
std::map<std::string, LuaEvent*> lua_events;
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
|
|
|
void index_lua(DFLibrary *lib);
|
|
|
|
void reset_lua();
|
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
bool *plugin_is_enabled;
|
2014-12-02 19:29:13 -07:00
|
|
|
std::vector<std::string>* plugin_globals;
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result (*plugin_init)(color_ostream &, std::vector <PluginCommand> &);
|
|
|
|
command_result (*plugin_status)(color_ostream &, std::string &);
|
|
|
|
command_result (*plugin_shutdown)(color_ostream &);
|
|
|
|
command_result (*plugin_onupdate)(color_ostream &);
|
|
|
|
command_result (*plugin_onstatechange)(color_ostream &, state_change_event);
|
2013-09-30 03:19:51 -06:00
|
|
|
command_result (*plugin_enable)(color_ostream &, bool);
|
2012-03-15 03:01:23 -06:00
|
|
|
RPCService* (*plugin_rpcconnect)(color_ostream &);
|
2019-05-29 17:52:03 -06:00
|
|
|
command_result (*plugin_save_data)(color_ostream &);
|
|
|
|
command_result (*plugin_load_data)(color_ostream &);
|
2011-06-24 21:35:29 -06:00
|
|
|
};
|
|
|
|
class DFHACK_EXPORT PluginManager
|
|
|
|
{
|
2011-07-09 03:33:58 -06:00
|
|
|
// PRIVATE METHODS
|
|
|
|
friend class Core;
|
2011-07-18 08:22:49 -06:00
|
|
|
friend class Plugin;
|
2011-06-24 21:35:29 -06:00
|
|
|
PluginManager(Core * core);
|
|
|
|
~PluginManager();
|
2015-08-14 14:11:23 -06:00
|
|
|
void init();
|
2012-03-10 04:55:42 -07:00
|
|
|
void OnUpdate(color_ostream &out);
|
|
|
|
void OnStateChange(color_ostream &out, state_change_event event);
|
2011-07-18 08:22:49 -06:00
|
|
|
void registerCommands( Plugin * p );
|
|
|
|
void unregisterCommands( Plugin * p );
|
2019-05-29 17:52:03 -06:00
|
|
|
void doSaveData(color_ostream &out);
|
|
|
|
void doLoadData(color_ostream &out);
|
2011-07-09 03:33:58 -06:00
|
|
|
// PUBLIC METHODS
|
|
|
|
public:
|
2015-08-14 17:32:41 -06:00
|
|
|
// list names of all plugins present in hack/plugins
|
2015-08-14 14:11:23 -06:00
|
|
|
std::vector<std::string> listPlugins();
|
2015-08-14 17:32:41 -06:00
|
|
|
// create Plugin instances for any plugins in hack/plugins that aren't present in all_plugins
|
|
|
|
void refresh();
|
2015-08-14 14:11:23 -06:00
|
|
|
|
|
|
|
bool load (const std::string &name);
|
|
|
|
bool loadAll();
|
|
|
|
bool unload (const std::string &name);
|
|
|
|
bool unloadAll();
|
|
|
|
bool reload (const std::string &name);
|
|
|
|
bool reloadAll();
|
|
|
|
|
|
|
|
Plugin *getPluginByName (const std::string &name) { return (*this)[name]; }
|
2011-12-30 12:25:50 -07:00
|
|
|
Plugin *getPluginByCommand (const std::string &command);
|
2012-03-15 03:01:23 -06:00
|
|
|
command_result InvokeCommand(color_ostream &out, const std::string & command, std::vector <std::string> & parameters);
|
|
|
|
bool CanInvokeHotkey(const std::string &command, df::viewscreen *top);
|
2015-08-14 14:11:23 -06:00
|
|
|
Plugin* operator[] (const std::string name);
|
|
|
|
std::size_t size();
|
|
|
|
|
|
|
|
std::map<std::string, Plugin*>::iterator begin();
|
|
|
|
std::map<std::string, Plugin*>::iterator end();
|
2011-07-09 03:33:58 -06:00
|
|
|
// DATA
|
2011-06-24 21:35:29 -06:00
|
|
|
private:
|
2015-08-14 14:11:23 -06:00
|
|
|
Core *core;
|
|
|
|
bool addPlugin(std::string name);
|
|
|
|
tthread::recursive_mutex * plugin_mutex;
|
2011-07-26 21:59:09 -06:00
|
|
|
tthread::mutex * cmdlist_mutex;
|
2015-08-14 14:11:23 -06:00
|
|
|
std::map <std::string, Plugin*> command_map;
|
|
|
|
std::map <std::string, Plugin*> all_plugins;
|
2011-06-24 21:35:29 -06:00
|
|
|
std::string plugin_path;
|
|
|
|
};
|
2011-12-30 12:25:50 -07:00
|
|
|
|
2012-03-03 06:38:24 -07:00
|
|
|
namespace Gui
|
|
|
|
{
|
|
|
|
// Predefined hotkey guards
|
2012-03-10 04:55:42 -07:00
|
|
|
DFHACK_EXPORT bool default_hotkey(df::viewscreen *);
|
2022-11-09 15:32:09 -07:00
|
|
|
DFHACK_EXPORT bool anywhere_hotkey(df::viewscreen *);
|
2012-03-10 04:55:42 -07:00
|
|
|
DFHACK_EXPORT bool dwarfmode_hotkey(df::viewscreen *);
|
|
|
|
DFHACK_EXPORT bool cursor_hotkey(df::viewscreen *);
|
2012-03-03 06:38:24 -07:00
|
|
|
}
|
2012-02-21 10:19:17 -07:00
|
|
|
};
|
2011-06-24 21:35:29 -06:00
|
|
|
|
2015-08-14 14:11:23 -06:00
|
|
|
#define DFHACK_PLUGIN_AUX(m_plugin_name, is_dev) \
|
2023-07-31 22:08:30 -06:00
|
|
|
extern "C" { \
|
2015-08-14 14:11:23 -06:00
|
|
|
DFhackDataExport const char * plugin_name = m_plugin_name;\
|
|
|
|
DFhackDataExport const char * plugin_version = DFHack::Version::dfhack_version();\
|
|
|
|
DFhackDataExport const char * plugin_git_description = DFHack::Version::git_description();\
|
2018-03-10 14:55:00 -07:00
|
|
|
DFhackDataExport int plugin_abi_version = DFHack::Version::dfhack_abi_version();\
|
2018-02-16 00:17:58 -07:00
|
|
|
DFhackDataExport DFHack::Plugin *plugin_self = NULL;\
|
2023-07-31 22:08:30 -06:00
|
|
|
std::vector<std::string> plugin_globals_noptr;\
|
|
|
|
DFhackDataExport std::vector<std::string>* plugin_globals = &plugin_globals_noptr; \
|
|
|
|
DFhackDataExport bool plugin_dev = is_dev; \
|
|
|
|
}
|
2015-04-01 15:50:16 -06:00
|
|
|
|
|
|
|
/// You have to include DFHACK_PLUGIN("plugin_name") in every plugin you write - just once. Ideally at the top of the main file.
|
|
|
|
#ifdef DEV_PLUGIN
|
2015-08-14 14:11:23 -06:00
|
|
|
#define DFHACK_PLUGIN(m_plugin_name) DFHACK_PLUGIN_AUX(m_plugin_name, true)
|
2015-04-01 15:50:16 -06:00
|
|
|
#else
|
2021-06-17 23:58:03 -06:00
|
|
|
// Expose the plugin name to the DFHack core, as well as metadata like the DFHack version.
|
2015-08-14 14:11:23 -06:00
|
|
|
#define DFHACK_PLUGIN(m_plugin_name) DFHACK_PLUGIN_AUX(m_plugin_name, false)
|
2015-04-01 15:50:16 -06:00
|
|
|
#endif
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
#define DFHACK_PLUGIN_IS_ENABLED(varname) \
|
|
|
|
DFhackDataExport bool plugin_is_enabled = false; \
|
|
|
|
bool &varname = plugin_is_enabled;
|
|
|
|
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
#define DFHACK_PLUGIN_LUA_COMMANDS \
|
|
|
|
DFhackCExport const DFHack::CommandReg plugin_lua_commands[] =
|
|
|
|
#define DFHACK_PLUGIN_LUA_FUNCTIONS \
|
|
|
|
DFhackCExport const DFHack::FunctionReg plugin_lua_functions[] =
|
2012-04-15 09:09:25 -06:00
|
|
|
#define DFHACK_PLUGIN_LUA_EVENTS \
|
|
|
|
DFhackCExport const DFHack::EventReg plugin_lua_events[] =
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
|
|
|
#define DFHACK_LUA_COMMAND(name) { #name, name }
|
2012-04-15 09:09:25 -06:00
|
|
|
#define DFHACK_LUA_FUNCTION(name) { #name, df::wrap_function(name,true) }
|
|
|
|
#define DFHACK_LUA_EVENT(name) { #name, &name##_event }
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
#define DFHACK_LUA_END { NULL, NULL }
|
2014-12-02 19:29:13 -07:00
|
|
|
|
2015-08-14 14:11:23 -06:00
|
|
|
|
|
|
|
#define REQUIRE_GLOBAL_NO_USE(global_name) \
|
2014-12-02 19:29:13 -07:00
|
|
|
static int VARIABLE_IS_NOT_USED CONCAT_TOKENS(required_globals_, __LINE__) = \
|
|
|
|
(plugin_globals->push_back(#global_name), 0);
|
2015-08-14 14:11:23 -06:00
|
|
|
|
|
|
|
#define REQUIRE_GLOBAL(global_name) \
|
|
|
|
using df::global::global_name; \
|
|
|
|
REQUIRE_GLOBAL_NO_USE(global_name)
|