Add scripting compatibility for plutonium
This commit is contained in:
parent
64a90a5573
commit
2d5821d0bb
@ -1,11 +1,13 @@
|
|||||||
#include "std_include.hpp"
|
#include "std_include.hpp"
|
||||||
#include "context_initializer.hpp"
|
#include "context_initializer.hpp"
|
||||||
|
|
||||||
|
#include "utils/string.hpp"
|
||||||
|
|
||||||
namespace game::scripting::context_initializer
|
namespace game::scripting::context_initializer
|
||||||
{
|
{
|
||||||
void initialize_entity(context* context)
|
void initialize_entity(context* context)
|
||||||
{
|
{
|
||||||
const auto chai = context->get_chai();
|
auto* const chai = context->get_chai();
|
||||||
|
|
||||||
chai->add(chaiscript::user_type<entity>(), "_entity");
|
chai->add(chaiscript::user_type<entity>(), "_entity");
|
||||||
chai->add(chaiscript::constructor<entity()>(), "_entity");
|
chai->add(chaiscript::constructor<entity()>(), "_entity");
|
||||||
@ -54,173 +56,48 @@ namespace game::scripting::context_initializer
|
|||||||
return context->get_event_handler()->add_event_listener(listener);
|
return context->get_event_handler()->add_event_listener(listener);
|
||||||
}), "onNotify");
|
}), "onNotify");
|
||||||
|
|
||||||
// Notification
|
chai->add_global(chaiscript::Boxed_Value(0), "gsc");
|
||||||
chai->add(chaiscript::fun(&entity::notify), "vectorNotify");
|
|
||||||
chai->add(chaiscript::fun([](const entity& ent, const std::string& event)
|
chai->add(chaiscript::fun([context](const entity& entity, const std::string& function,
|
||||||
|
const std::vector<chaiscript::Boxed_Value>& arguments)
|
||||||
{
|
{
|
||||||
return ent.notify(event, {});
|
const auto function_lower = utils::string::to_lower(function);
|
||||||
}), "notify");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
if (function_lower == "notify" && !arguments.empty())
|
||||||
[](const entity& ent, const std::string& event,
|
{
|
||||||
const chaiscript::Boxed_Value& a1)
|
const auto real_arguments = std::vector<chaiscript::Boxed_Value>(
|
||||||
{
|
arguments.begin() + 1, arguments.end());
|
||||||
return ent.notify(event, {a1});
|
|
||||||
}), "notify");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
entity.notify(chaiscript::boxed_cast<std::string>(arguments[0]), real_arguments);
|
||||||
[](const entity& ent, const std::string& event,
|
}
|
||||||
const chaiscript::Boxed_Value& a1,
|
else if (context->get_executer()->function_exists(function_lower, false))
|
||||||
const chaiscript::Boxed_Value& a2)
|
{
|
||||||
{
|
return entity.call(function_lower, arguments);
|
||||||
return ent.notify(event, {a1, a2});
|
}
|
||||||
}), "notify");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
return chaiscript::Boxed_Value(0);
|
||||||
[](const entity& ent, const std::string& event,
|
}), "method_missing");
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2,
|
|
||||||
const chaiscript::Boxed_Value& a3)
|
|
||||||
{
|
|
||||||
return ent.notify(event, {a1, a2, a3});
|
|
||||||
}), "notify");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
chai->add(chaiscript::fun([context](const chaiscript::Boxed_Value&/*object*/,
|
||||||
[](const entity& ent, const std::string& event,
|
const std::string& function,
|
||||||
const chaiscript::Boxed_Value& a1,
|
const std::vector<chaiscript::Boxed_Value>& arguments)
|
||||||
const chaiscript::Boxed_Value& a2,
|
|
||||||
const chaiscript::Boxed_Value& a3,
|
|
||||||
const chaiscript::Boxed_Value& a4)
|
|
||||||
{
|
|
||||||
return ent.notify(event, {a1, a2, a3, a4});
|
|
||||||
}), "notify");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[](const entity& ent, const std::string& event,
|
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2,
|
|
||||||
const chaiscript::Boxed_Value& a3,
|
|
||||||
const chaiscript::Boxed_Value& a4,
|
|
||||||
const chaiscript::Boxed_Value& a5)
|
|
||||||
{
|
|
||||||
return ent.notify(event, {a1, a2, a3, a4, a5});
|
|
||||||
}), "notify");
|
|
||||||
|
|
||||||
// Instance call
|
|
||||||
chai->add(chaiscript::fun(&entity::call), "vectorCall");
|
|
||||||
chai->add(chaiscript::fun([](const entity& ent, const std::string& function)
|
|
||||||
{
|
{
|
||||||
return ent.call(function, {});
|
const auto function_lower = utils::string::to_lower(function);
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
if (context->get_executer()->function_exists(function_lower, true))
|
||||||
[](const entity& ent, const std::string& function,
|
{
|
||||||
const chaiscript::Boxed_Value& a1)
|
return context->get_executer()->call(function_lower, 0, arguments);
|
||||||
{
|
}
|
||||||
return ent.call(function, {a1});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
return chaiscript::Boxed_Value(0);
|
||||||
[](const entity& ent, const std::string& function,
|
}), "method_missing");
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2)
|
|
||||||
{
|
|
||||||
return ent.call(function, {a1, a2});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[](const entity& ent, const std::string& function,
|
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2,
|
|
||||||
const chaiscript::Boxed_Value& a3)
|
|
||||||
{
|
|
||||||
return ent.call(function, {a1, a2, a3});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[](const entity& ent, const std::string& function,
|
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2,
|
|
||||||
const chaiscript::Boxed_Value& a3,
|
|
||||||
const chaiscript::Boxed_Value& a4)
|
|
||||||
{
|
|
||||||
return ent.call(function, {a1, a2, a3, a4});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[](const entity& ent, const std::string& function,
|
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2,
|
|
||||||
const chaiscript::Boxed_Value& a3,
|
|
||||||
const chaiscript::Boxed_Value& a4,
|
|
||||||
const chaiscript::Boxed_Value& a5)
|
|
||||||
{
|
|
||||||
return ent.call(function, {a1, a2, a3, a4, a5});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
// Global call
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[context](const std::string& function,
|
|
||||||
const std::vector<chaiscript::Boxed_Value>& arguments)
|
|
||||||
{
|
|
||||||
return context->get_executer()->call(function, 0, arguments);
|
|
||||||
}), "vectorCall");
|
|
||||||
chai->add(chaiscript::fun([context](const std::string& function)
|
|
||||||
{
|
|
||||||
return context->get_executer()->call(function, 0, {});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[context](const std::string& function,
|
|
||||||
const chaiscript::Boxed_Value& a1)
|
|
||||||
{
|
|
||||||
return context->get_executer()->call(function, 0, {a1});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[context](const std::string& function,
|
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2)
|
|
||||||
{
|
|
||||||
return context->get_executer()->call(function, 0, {a1, a2});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[context](const std::string& function,
|
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2,
|
|
||||||
const chaiscript::Boxed_Value& a3)
|
|
||||||
{
|
|
||||||
return context->get_executer()->call(function, 0, {a1, a2, a3});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[context](const std::string& function,
|
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2,
|
|
||||||
const chaiscript::Boxed_Value& a3,
|
|
||||||
const chaiscript::Boxed_Value& a4)
|
|
||||||
{
|
|
||||||
return context->get_executer()->call(function, 0, {a1, a2, a3, a4});
|
|
||||||
}), "call");
|
|
||||||
|
|
||||||
chai->add(chaiscript::fun(
|
|
||||||
[context](const std::string& function,
|
|
||||||
const chaiscript::Boxed_Value& a1,
|
|
||||||
const chaiscript::Boxed_Value& a2,
|
|
||||||
const chaiscript::Boxed_Value& a3,
|
|
||||||
const chaiscript::Boxed_Value& a4,
|
|
||||||
const chaiscript::Boxed_Value& a5)
|
|
||||||
{
|
|
||||||
return context->get_executer()->call(function, 0, {a1, a2, a3, a4, a5});
|
|
||||||
}), "call");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize(context* context)
|
void initialize(context* context)
|
||||||
{
|
{
|
||||||
initialize_entity(context);
|
initialize_entity(context);
|
||||||
|
|
||||||
const auto chai = context->get_chai();
|
auto* const chai = context->get_chai();
|
||||||
|
|
||||||
chai->add(chaiscript::fun([](const std::string& string)
|
chai->add(chaiscript::fun([](const std::string& string)
|
||||||
{
|
{
|
||||||
|
@ -166,4 +166,9 @@ namespace game::scripting
|
|||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool executer::function_exists(const std::string& function, const bool prefer_global)
|
||||||
|
{
|
||||||
|
return find_function_index(function, prefer_global) >= 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@ namespace game::scripting
|
|||||||
chaiscript::Boxed_Value call(const std::string& function, unsigned int entity_id,
|
chaiscript::Boxed_Value call(const std::string& function, unsigned int entity_id,
|
||||||
std::vector<chaiscript::Boxed_Value> arguments) const;
|
std::vector<chaiscript::Boxed_Value> arguments) const;
|
||||||
|
|
||||||
|
static bool function_exists(const std::string& function, bool prefer_global);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
context* context_;
|
context* context_;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user