Files
fs2open.github.com/code/scripting/ade.h
T
asarium 1c12349990 Add extended scripting hook system
This moves towards defining the hooks in the code module that executes
them. Also provides the API necessary for better documentation of hooks.
This only covers a few hooks as a test and demonstration of the new API.
If no issues are found with this it can be extended in the future.
2020-04-13 14:45:19 +02:00

307 lines
6.7 KiB
C++

//
//
#ifndef FS2_OPEN_ADE_H
#define FS2_OPEN_ADE_H
#include "globalincs/pstypes.h"
#include "globalincs/version.h"
#include "object/object.h"
extern "C" {
#include <lauxlib.h>
#include <lualib.h>
}
#include <memory>
/**
* @defgroup ade_api ADE API functions
*
* @brief Functions and macros used in the ADE scripting API
*
* These functions enable the code to communicate with external scripts and expose an API for them to use
*/
// Forward definition
struct DocumentationElement;
namespace scripting {
/**
*
* @param L
* @param stackdump
*
* @ingroup ade_api
*/
void ade_stackdump(lua_State* L, char* stackdump);
/**
*
* @param L
* @return
*
* @ingroup ade_api
*/
int ade_friendly_error(lua_State* L);
//*************************Lua types*************************
// Value fo ade_odata::size for when buf contains a pointer
const size_t ODATA_PTR_SIZE = (size_t) -1;
const int ADE_FUNCNAME_UPVALUE_INDEX = 1;
const int ADE_SETTING_UPVALUE_INDEX = 2;
const int ADE_DESTRUCTOR_OBJ_UPVALUE_INDEX = 3; // Upvalue which stores the reference to the ade_obj of a destructor
#define ADE_SETTING_VAR lua_toboolean(L,lua_upvalueindex(ADE_SETTING_UPVALUE_INDEX))
template <typename T>
struct ade_odata_getter {
size_t idx;
T* value_ptr;
ade_odata_getter(size_t idx_in, T* ptr_in) : idx(idx_in), value_ptr(ptr_in) {}
};
template <typename T>
struct ade_odata_ptr_getter {
size_t idx;
T** value_ptr;
ade_odata_ptr_getter(size_t idx_in, T** ptr_in) : idx(idx_in), value_ptr(ptr_in) {}
};
template <typename T>
struct ade_odata_setter {
size_t idx;
T value;
ade_odata_setter(size_t idx_in, T value_in) : idx(idx_in), value(std::move(value_in)) {}
};
//WMC - 'Type' is the same as ade_set_args,
//plus some extra
//b - boolean
//d - double
//f - float
//i - integer
//s - string
//x - fix
//o - object
//EXTRA:
//l - library //WMC - no longer exists
//u - function
//v - virtual variable
//
//u - oh wait...
enum class ade_type_info_type {
Empty,
Simple,
Tuple,
Array
};
class ade_type_array;
/**
* @brief A definition of a type used in the ADE system
*/
class ade_type_info {
ade_type_info_type _type = ade_type_info_type::Empty;
const char* _simple_name = nullptr;
SCP_vector<ade_type_info> _elements;
public:
ade_type_info() = default;
/*implicit*/ ade_type_info(const char* single_type); // NOLINT(hicpp-explicit-conversions)
/*implicit*/ ade_type_info(std::initializer_list<ade_type_info> tuple_types);
/*implicit*/ ade_type_info(const ade_type_array& listType);
ade_type_info_type getType() const;
bool isEmpty() const;
bool isSimple() const;
bool isTuple() const;
bool isArray() const;
const char* getSimpleName() const;
const ade_type_info& arrayType() const;
const SCP_vector<ade_type_info>& elements() const;
};
class ade_type_array {
ade_type_info _element_type;
public:
explicit ade_type_array(ade_type_info elementType);
const ade_type_info& getElementType() const;
};
/**
* @ingroup ade_api
*/
class ade_table_entry {
public:
const char* Name = nullptr;
const char* ShortName = nullptr;
//Important stuff
size_t Idx = UINT_MAX;
size_t ParentIdx = UINT_MAX;
size_t DerivatorIdx = UINT_MAX;
//ade_id AdeID;
//ade_id DerivatorID; //Who do we derive from
//Type-specific
bool Instanced = false; //Is this a single instance?
char Type = '\0';
//Functions/virtfuncs
lua_CFunction Function = nullptr;
// For Objects, the destructor of the object
void* Destructor_upvalue = nullptr;
lua_CFunction Destructor = nullptr;
size_t Size = 0;
//Metadata
const char* Arguments = nullptr;
const char* Description = nullptr;
ade_type_info ReturnType;
const char* ReturnDescription = nullptr;
gameversion::version DeprecationVersion;
const char* DeprecationMessage = nullptr;
//Subentries, of course
//WMC - I have HAD it with these motherfriendly vectors
//on this motherfriendly class.
size_t Num_subentries = 0;
size_t Subentries[256];
private:
//*****Internal functions
//int IndexHandler(lua_State *L);
public:
//*****Constructors
ade_table_entry();
//*****Operators
//ade_table_entry &operator = (const ade_table_entry &ate);
//*****Functions
size_t AddSubentry(ade_table_entry& n_ate);
int SetTable(lua_State* L, int p_amt_ldx, int p_mtb_ldx);
void OutputMeta(FILE* fp);
std::unique_ptr<DocumentationElement> ToDocumentationElement();
//*****Get
const char* GetName() { if (Name != NULL) { return Name; } else { return ShortName; }}
};
/**
* @ingroup ade_api
*/
class ade_manager {
SCP_vector<ade_table_entry> _table_entries;
ade_manager() {}
public:
static ade_manager* getInstance();
// Disallow copying
ade_manager(const ade_manager&) = delete;
ade_manager& operator=(const ade_manager&) = delete;
// Disallow moving
ade_manager(ade_manager&&) = delete;
ade_manager& operator=(ade_manager&&) = delete;
size_t addTableEntry(const ade_table_entry& entry);
ade_table_entry& getEntry(size_t idx);
const ade_table_entry& getEntry(size_t idx) const;
size_t getNumEntries() const { return _table_entries.size(); }
};
/**
* @ingroup ade_api
*/
void ade_stackdump(lua_State *L, char *stackdump);
/**
* @ingroup ade_api
*/
int ade_friendly_error(lua_State* L);
/**
* @ingroup ade_api
*/
const char* ade_get_type_string(lua_State* L, int argnum);
/**
* @brief Converts an object index to something that can be used with ade_set_args.
*
* This respects the actual type of the object so all appropriate functions are available in Lua.
*
* @warning This is only used internally and should not be used by API code. Use ade_set_object_with_breed instead.
*
* @param obj_idx The object index
* @return The ade odata
*/
ade_odata_setter<object_h> ade_object_to_odata(int obj_idx);
/**
* @brief Sets an object parameter with the right type
*
* This should be used everywhere where an object value is returned to make sure that the scripter has access to
* all API functions.
*
* @param L The lua state
* @param obj_idx The object number
* @return The return value of ade_set_args
*
* @author WMC
* @ingroup ade_api
*/
int ade_set_object_with_breed(lua_State* L, int obj_idx);
/**
* @brief Loads and executes a default lua script
*
* This uses the specified file name and either retrieves it from the default files or uses it as a file name if the mod
* option is enabled.
*
* @param L The lua state
* @param name The name of the script file
*
* @ingroup ade_api
*/
void load_default_script(lua_State* L, const char* name);
/**
* @brief Writes HTML to fp which produces a link to the specified type info
* @param fp The stream to write the HTML to
* @param type_info The type to generate the link for
*/
void ade_output_type_link(FILE* fp, const ade_type_info& type_info);
namespace internal {
ade_table_entry& getTableEntry(size_t idx);
}
} // namespace scripting
#endif // FS2_OPEN_ADE_H