2011-06-24 21:35:29 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
|
|
|
|
|
|
|
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"
|
2011-06-24 21:35:29 -06:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
struct DFLibrary;
|
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;
|
2011-12-30 12:25:50 -07:00
|
|
|
|
2011-06-24 21:35:29 -06:00
|
|
|
enum command_result
|
|
|
|
{
|
2011-08-05 20:37:29 -06:00
|
|
|
CR_WOULD_BREAK = -2,
|
2011-06-24 21:35:29 -06:00
|
|
|
CR_NOT_IMPLEMENTED = -1,
|
|
|
|
CR_FAILURE = 0,
|
2011-12-31 02:25:46 -07:00
|
|
|
CR_OK = 1,
|
|
|
|
CR_WRONG_USAGE = 2
|
2011-06-24 21:35:29 -06:00
|
|
|
};
|
2011-12-30 07:12:15 -07:00
|
|
|
enum state_change_event
|
|
|
|
{
|
|
|
|
SC_GAME_LOADED,
|
2011-12-30 12:25:50 -07:00
|
|
|
SC_GAME_UNLOADED,
|
|
|
|
SC_VIEWSCREEN_CHANGED
|
2011-12-30 07:12:15 -07:00
|
|
|
};
|
2011-12-30 12:25:50 -07:00
|
|
|
struct DFHACK_EXPORT PluginCommand
|
2011-06-24 21:35:29 -06:00
|
|
|
{
|
2011-12-30 12:25:50 -07:00
|
|
|
typedef command_result (*command_function)(Core *, std::vector <std::string> &);
|
|
|
|
typedef bool (*command_hotkey_guard)(Core *, 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!
|
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
|
|
|
{
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
}
|
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;
|
|
|
|
enum plugin_state
|
|
|
|
{
|
|
|
|
PS_UNLOADED,
|
|
|
|
PS_LOADED,
|
|
|
|
PS_BROKEN
|
|
|
|
};
|
2011-06-24 21:35:29 -06:00
|
|
|
friend class PluginManager;
|
2011-07-18 08:22:49 -06:00
|
|
|
Plugin(DFHack::Core* core, const std::string& filepath, const std::string& filename, PluginManager * pm);
|
2011-06-24 21:35:29 -06:00
|
|
|
~Plugin();
|
2011-07-18 08:22:49 -06:00
|
|
|
command_result on_update();
|
2011-12-30 07:12:15 -07:00
|
|
|
command_result on_state_change(state_change_event event);
|
2011-07-18 08:22:49 -06:00
|
|
|
public:
|
|
|
|
bool load();
|
|
|
|
bool unload();
|
|
|
|
bool reload();
|
2011-08-05 20:37:29 -06:00
|
|
|
command_result invoke( std::string & command, std::vector <std::string> & parameters, bool interactive );
|
2011-12-30 12:25:50 -07:00
|
|
|
bool can_invoke_hotkey( std::string & command, df::viewscreen *top );
|
2011-07-18 08:22:49 -06:00
|
|
|
plugin_state getState () const;
|
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;
|
|
|
|
}
|
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;
|
|
|
|
std::string filename;
|
|
|
|
std::string name;
|
|
|
|
DFLibrary * plugin_lib;
|
2011-07-18 08:22:49 -06:00
|
|
|
PluginManager * parent;
|
|
|
|
plugin_state state;
|
2011-06-24 21:35:29 -06:00
|
|
|
command_result (*plugin_init)(Core *, std::vector <PluginCommand> &);
|
|
|
|
command_result (*plugin_status)(Core *, std::string &);
|
|
|
|
command_result (*plugin_shutdown)(Core *);
|
2011-06-26 18:13:01 -06:00
|
|
|
command_result (*plugin_onupdate)(Core *);
|
2011-12-30 07:12:15 -07:00
|
|
|
command_result (*plugin_onstatechange)(Core *, state_change_event);
|
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();
|
2011-06-26 18:13:01 -06:00
|
|
|
void OnUpdate( void );
|
2011-12-30 07:12:15 -07:00
|
|
|
void OnStateChange( state_change_event event );
|
2011-07-18 08:22:49 -06:00
|
|
|
void registerCommands( Plugin * p );
|
|
|
|
void unregisterCommands( Plugin * p );
|
2011-07-09 03:33:58 -06:00
|
|
|
// PUBLIC METHODS
|
|
|
|
public:
|
2011-07-18 08:22:49 -06:00
|
|
|
Plugin *getPluginByName (const std::string & name);
|
2011-12-30 12:25:50 -07:00
|
|
|
Plugin *getPluginByCommand (const std::string &command);
|
2011-08-05 20:37:29 -06:00
|
|
|
command_result InvokeCommand( std::string & command, std::vector <std::string> & parameters, bool interactive = true );
|
2011-12-30 12:25:50 -07:00
|
|
|
bool CanInvokeHotkey(std::string &command, df::viewscreen *top);
|
2011-07-18 08:22:49 -06:00
|
|
|
Plugin* operator[] (std::size_t index)
|
2011-06-25 00:05:17 -06:00
|
|
|
{
|
|
|
|
if(index >= all_plugins.size())
|
|
|
|
return 0;
|
|
|
|
return all_plugins[index];
|
|
|
|
};
|
|
|
|
std::size_t size()
|
|
|
|
{
|
|
|
|
return all_plugins.size();
|
|
|
|
}
|
2011-07-09 03:33:58 -06:00
|
|
|
// DATA
|
2011-06-24 21:35:29 -06:00
|
|
|
private:
|
2011-07-26 21:59:09 -06:00
|
|
|
tthread::mutex * cmdlist_mutex;
|
2011-07-18 08:22:49 -06:00
|
|
|
std::map <std::string, Plugin *> belongs;
|
2011-06-24 21:35:29 -06:00
|
|
|
std::vector <Plugin *> all_plugins;
|
|
|
|
std::string plugin_path;
|
|
|
|
};
|
2011-12-30 12:25:50 -07:00
|
|
|
|
2011-12-31 02:25:46 -07:00
|
|
|
// Predefined hotkey guards
|
2011-12-30 12:25:50 -07:00
|
|
|
DFHACK_EXPORT bool default_hotkey(Core *, df::viewscreen *);
|
2011-12-31 02:25:46 -07:00
|
|
|
DFHACK_EXPORT bool dwarfmode_hotkey(Core *, df::viewscreen *);
|
|
|
|
DFHACK_EXPORT bool cursor_hotkey(Core *, df::viewscreen *);
|
2011-06-24 21:35:29 -06:00
|
|
|
}
|
|
|
|
|