iw5-mod/src/module/scripting.cpp

132 lines
3.0 KiB
C++
Raw Normal View History

2019-01-13 05:16:19 -05:00
#include <std_include.hpp>
#include "utils/hook.hpp"
2019-01-13 15:28:05 -05:00
#include "utils/io.hpp"
2019-01-16 10:19:21 -05:00
#include "scheduler.hpp"
#include "scripting.hpp"
2019-01-13 05:16:19 -05:00
2019-01-13 13:03:46 -05:00
utils::hook scripting::start_hook_;
utils::hook scripting::stop_hook_;
void scripting::post_load()
{
start_hook_.initialize(SELECT_VALUE(0x50C575, 0x50D4F2, 0x48A026), &start_execution_stub, HOOK_CALL) //
->install() //
->quick();
stop_hook_.initialize(SELECT_VALUE(0x528B04, 0x569E46, 0x4F03FA), &stop_execution_stub, HOOK_CALL) //
->install() //
->quick();
2019-01-13 13:03:46 -05:00
utils::hook(SELECT_VALUE(0x6109F3, 0x56B637, 0x4EDFF7), &vm_notify_stub, HOOK_CALL).install()->quick();
utils::hook(SELECT_VALUE(0x6128BE, 0x56D541, 0x4EFAF9), &vm_notify_stub, HOOK_CALL).install()->quick();
if (game::is_sp())
2019-01-13 13:03:46 -05:00
{
utils::hook(0x610970, &vm_notify_stub, HOOK_JUMP).install()->quick();
}
2019-01-13 15:28:05 -05:00
}
void scripting::pre_destroy()
{
2019-01-20 07:21:43 -05:00
this->scripts_.clear();
2019-01-13 15:28:05 -05:00
}
2019-01-13 13:03:46 -05:00
2019-01-20 07:21:43 -05:00
void scripting::load_scripts()
2019-01-13 13:03:46 -05:00
{
2019-01-13 15:28:05 -05:00
const auto scripts = utils::io::list_files("open-iw5/scripts/");
for (const auto& script : scripts)
{
if (script.substr(script.find_last_of('.') + 1) == "chai")
{
2019-01-13 15:53:52 -05:00
try
{
2019-01-20 07:21:43 -05:00
auto context = std::make_unique<game::scripting::context>();
context->get_chai()->eval_file(script);
this->scripts_.push_back(std::move(context));
2019-01-13 15:53:52 -05:00
}
2019-01-16 10:19:21 -05:00
catch (chaiscript::exception::eval_error& e)
2019-01-13 16:59:01 -05:00
{
2019-01-16 10:19:21 -05:00
throw std::runtime_error(e.pretty_print());
2019-01-13 15:53:52 -05:00
}
2019-01-13 15:28:05 -05:00
}
}
2019-01-13 13:03:46 -05:00
}
void scripting::start_execution()
{
try
{
this->load_scripts();
}
catch (std::exception& e)
{
propagate_error(e);
}
}
void scripting::stop_execution()
2019-01-13 13:03:46 -05:00
{
this->scripts_.clear();
2019-01-13 13:03:46 -05:00
}
void scripting::dispatch(game::scripting::event* event)
2019-01-13 13:03:46 -05:00
{
for (const auto& script : this->scripts_)
{
script->get_event_handler()->dispatch(event);
}
2019-01-13 13:03:46 -05:00
}
2019-01-17 15:05:48 -05:00
void scripting::propagate_error(const std::exception& e)
2019-01-16 16:44:50 -05:00
{
printf("\n******* Script execution error *******\n");
printf("%s\n", e.what());
printf("**************************************\n\n");
scheduler::error("Script execution error\n(see console for actual details)\n", 5);
}
void scripting::start_execution_stub()
2019-01-13 13:03:46 -05:00
{
module_loader::get<scripting>()->start_execution();
reinterpret_cast<void(*)()>(start_hook_.get_original())();
}
2019-01-13 13:03:46 -05:00
void scripting::stop_execution_stub()
{
module_loader::get<scripting>()->stop_execution();
reinterpret_cast<void(*)()>(stop_hook_.get_original())();
2019-01-13 13:03:46 -05:00
}
void scripting::vm_notify_stub(const unsigned int notify_id, const unsigned short type,
game::native::VariableValue* stack)
2019-01-13 13:03:46 -05:00
{
try
2019-01-13 13:03:46 -05:00
{
game::scripting::event e;
e.name = game::native::SL_ConvertToString(type);
e.entity_id = notify_id;
if (e.name == "touch") return; // Skip that for now
//printf("%X: %s\n", e.entity_id, e.name.data());
2019-01-13 06:07:19 -05:00
for (auto value = stack; value->type != game::native::SCRIPT_END; --value)
{
e.arguments.emplace_back(*value);
}
module_loader::get<scripting>()->dispatch(&e);
}
catch (std::exception& e)
2019-01-13 13:03:46 -05:00
{
propagate_error(e);
2019-01-13 13:03:46 -05:00
}
game::native::VM_Notify(notify_id, type, stack);
2019-01-13 13:03:46 -05:00
}
2019-01-13 06:07:19 -05:00
2019-01-13 05:16:19 -05:00
REGISTER_MODULE(scripting)