fix: Crash on adding name key into kwargs dictionary

+ Changed file structure for events
This commit is contained in:
AURUMVORXX
2024-11-04 05:10:54 +03:00
parent 6a20d96bb5
commit bb51677592
6 changed files with 49 additions and 39 deletions

39
src/events/sqevents.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include <sqapi.h>
#include <pybind11/embed.h>
#include "sqevents.h"
#include "sqcontainers.h"
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);
}
void registerSquirrelEvents()
{
addEventHandler("onInit", sq_onInit, 0);
addEventHandler("onExit", sq_onExit, 0);
addEventHandler("onTick", sq_onTick, 0);
addEventHandler("onTime", sq_onTime, 0);
addEventHandler("onBan", sq_onBan, 0);
addEventHandler("onUnban", sq_onUnban, 0);
}

View File

@@ -1,9 +1,13 @@
#pragma once
#ifndef _SQEVENTS_H_
#define _SQEVENTS_H
SQInteger sq_onInit(HSQUIRRELVM);
SQInteger sq_onExit(HSQUIRRELVM);
SQInteger sq_onTick(HSQUIRRELVM);
SQInteger sq_onTime(HSQUIRRELVM);
SQInteger sq_onBan(HSQUIRRELVM);
SQInteger sq_onUnban(HSQUIRRELVM);
void registerSquirrelEvents();
void registerSquirrelEvents();
#endif

View File

@@ -1,32 +1,13 @@
#include <sqapi.h>
#include <pybind11/embed.h>
#include "sqcontainers.h"
#include "sqevents.h"
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");
@@ -81,14 +62,4 @@ SQInteger sq_onUnban(HSQUIRRELVM vm)
g2o.attr("callEvent")("onUnban", **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);
addEventHandler("onBan", sq_onBan, 0);
addEventHandler("onUnban", sq_onUnban, 0);
}

View File

@@ -2,7 +2,7 @@
#include <pybind11/embed.h>
#include <iostream>
#include <pybind11/embed.h>
#include "sqevents.h"
#include "events/sqevents.h"
namespace py = pybind11;
py::scoped_interpreter guard{};

View File

@@ -18,15 +18,11 @@ py::dict sqParseTable(Sqrat::Table tab)
if (key._type != OT_STRING)
continue;
py::str pkey = "{0}_";
switch(value._type)
{
case OT_STRING:
// TODO: for some reason, key names 'name' and 'reason' crashing the server (maybe some more)
// Adding _ to key names is fixing this, but need to find a better solution
result[pkey.format(sq_objtostring(&key))] = sq_objtostring(&value);
result[sq_objtostring(&key)] = sq_objtostring(&value);
break;
case OT_INTEGER:
result[sq_objtostring(&key)] = sq_objtointeger(&value);