feat: Added event calling into Python

+ Added onInit, onTick, onExit, onTime events
This commit is contained in:
AURUMVORXX
2024-11-02 02:47:38 +03:00
parent d972b08b6f
commit aece83e914
4 changed files with 98 additions and 20 deletions

22
src/main.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <sqapi.h>
#include <pybind11/embed.h>
#include <iostream>
#include <pybind11/embed.h>
#include "sqevents.h"
namespace py = pybind11;
py::scoped_interpreter guard{};
py::module_ g2o = py::module_::import("g2o");
py::module_ scripts = py::module_::import("scripts");
extern "C" SQRESULT SQRAT_API sqmodule_load(HSQUIRRELVM vm, HSQAPI api)
{
SqModule::Initialize(vm, api);
Sqrat::DefaultVM::Set(vm);
registerSquirrelEvents();
return SQ_OK;
}

68
src/sqevents.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include <sqapi.h>
#include <pybind11/embed.h>
#include <iostream>
namespace py = pybind11;
using namespace pybind11::literals;
extern py::module_ g2o;
void addEventHandler(const char* eventName, SQFUNCTION closure, unsigned int priority = 9999)
{
using namespace SqModule;
Sqrat::Function sq_addEventHandler = Sqrat::RootTable().GetFunction("addEventHandler");
if (sq_addEventHandler.IsNull())
return;
HSQOBJECT closureHandle;
sq_newclosure(vm, closure, 0);
sq_getstackobj(vm, -1, &closureHandle);
Sqrat::Function func(vm, Sqrat::RootTable().GetObject(), closureHandle);
sq_addEventHandler(eventName, func, priority);
sq_pop(vm, 1);
}
SQInteger sq_onInit(HSQUIRRELVM vm)
{
py::object result = g2o.attr("callEvent")("onInit");
return 0;
}
SQInteger sq_onExit(HSQUIRRELVM vm)
{
py::object result = g2o.attr("callEvent")("onExit");
return 0;
}
SQInteger sq_onTick(HSQUIRRELVM vm)
{
py::object result = g2o.attr("callEvent")("onTick");
return 0;
}
SQInteger sq_onTime(HSQUIRRELVM vm)
{
SQInteger day, hour, min;
sq_getinteger(vm, 4, &day);
sq_getinteger(vm, 3, &hour);
sq_getinteger(vm, 2, &min);
py::dict kwargs = py::dict("day"_a=day, "hour"_a=hour, "min"_a=min);
g2o.attr("callEvent")("onTime", **kwargs);
return 0;
}
void registerSquirrelEvents()
{
addEventHandler("onInit", sq_onInit, 0);
addEventHandler("onExit", sq_onExit, 0);
addEventHandler("onTick", sq_onTick, 0);
addEventHandler("onTime", sq_onTime, 0);
}

8
src/sqevents.h Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
SQInteger sq_onInit(HSQUIRRELVM);
SQInteger sq_onExit(HSQUIRRELVM);
SQInteger sq_onTick(HSQUIRRELVM);
SQInteger sq_onTime(HSQUIRRELVM);
void registerSquirrelEvents();

View File

@@ -1,20 +0,0 @@
#include <sqapi.h>
/* squirreldoc (func)
*
* This is an entry point for the module.
*
* @version 0.1
* @side shared
* @name sqmodule_load
* @param (HSQUIRRELVM) vm the squirrel vm.
* @param (HSQAPI) api the api ptr containing all of the squirrel functions.
* @return (int) returns a function status.
*
*/
extern "C" SQRESULT SQRAT_API sqmodule_load(HSQUIRRELVM vm, HSQAPI api)
{
SqModule::Initialize(vm, api);
return SQ_OK;
}