Files
fs2open.github.com/code/scripting/api/libs/engine.cpp
T
asarium bf521f1c6b Make the campaign room API available to scripting
This makes the necessary campaign room API available to the scripting
API so that it can be implemented using libRocket. Also adds an
engine.sleep function which I needed for making my Lua debugger work
reliably.
2020-06-03 17:49:15 +02:00

95 lines
2.5 KiB
C++

//
//
#include "engine.h"
#include "scripting/scripting.h"
namespace scripting {
namespace api {
//**********LIBRARY: Engine
ADE_LIB(l_Engine, "Engine", "engine", "Basic engine access functions");
ADE_FUNC(addHook, l_Engine,
"string name, function hookFunction, [table conditionals = {}], [function override_func = return false]",
"Adds a function to be called from the specified game hook", "boolean",
"true if hook was installed properly, false otherwise")
{
using namespace luacpp;
const char* hook_name;
LuaFunction hook;
LuaTable conditionals;
LuaFunction override_func;
if (!ade_get_args(L, "su|tu", &hook_name, &hook, &conditionals, &override_func)) {
return ADE_RETURN_FALSE;
}
if (!hook.isValid()) {
LuaError(L, "Hook function is invalid!");
return ADE_RETURN_FALSE;
}
ConditionedHook cond_hook;
script_action action;
action.action_type = scripting_string_to_action(hook_name);
if (action.action_type == CHA_NONE) {
LuaError(L, "Invalid hook name '%s'!", hook_name);
return ADE_RETURN_FALSE;
}
action.hook.hook_function.language = SC_LUA;
action.hook.hook_function.function = hook;
if (override_func.isValid()) {
action.hook.override_function.language = SC_LUA;
action.hook.override_function.function = override_func;
}
cond_hook.AddAction(&action);
if (conditionals.isValid()) {
for (const auto& tableEntry : conditionals) {
const auto& key = tableEntry.first;
const auto& value = tableEntry.second;
if (!key.is(ValueType::STRING)) {
LuaError(L, "Conditional key '%s' is not a string", key.getValue<SCP_string>().c_str());
return ADE_RETURN_FALSE;
}
if (!value.is(ValueType::STRING)) {
LuaError(L, "Conditional value '%s' for key '%s' is not a string", value.getValue<SCP_string>().c_str(),
key.getValue<SCP_string>().c_str());
return ADE_RETURN_FALSE;
}
script_condition cond;
cond.condition_type = scripting_string_to_condition(key.getValue<SCP_string>().c_str());
cond.condition_string = value.getValue<SCP_string>();
cond_hook.AddCondition(&cond);
}
}
Script_system.AddConditionedHook(std::move(cond_hook));
return ADE_RETURN_TRUE;
}
ADE_FUNC(sleep, l_Engine, "number seconds", "Executes a <b>blocking</b> sleep. Usually only necessary for development or testing purposes. Use with care!", nullptr, nullptr)
{
float seconds = 0.0f;
if (!ade_get_args(L, "f", &seconds)) {
return ADE_RETURN_NIL;
}
os_sleep(fl2i(seconds * 1000.0f));
return ADE_RETURN_NIL;
}
} // namespace api
} // namespace scripting