feat: Added Packet class and onPacket event

This commit is contained in:
AURUMVORXX
2024-11-05 02:44:38 +03:00
parent 558e607c5f
commit 2657b2c613
15 changed files with 585 additions and 7 deletions

View File

@@ -0,0 +1,111 @@
#include "CustomTypes.h"
#include "Array.h"
#include "Property.h"
namespace nonut::g2o
{
#define GET_SLOT(slot, type) slot = arrayWrapper.get<type>(#slot)
void GameTime::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(day, Int);
GET_SLOT(hour, Int);
GET_SLOT(min, Int);
}
void Position2d::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(x, Int);
GET_SLOT(y, Int);
}
void Position3d::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(x, Float);
GET_SLOT(y, Float);
GET_SLOT(z, Float);
}
void Size2d::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(width, Int);
GET_SLOT(height, Int);
}
void Rect::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(x, Int);
GET_SLOT(y, Int);
GET_SLOT(width, Int);
GET_SLOT(height, Int);
}
void UV::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(x, Float);
GET_SLOT(y, Float);
GET_SLOT(width, Float);
GET_SLOT(height, Float);
}
void Resolution::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(x, Int);
GET_SLOT(y, Int);
GET_SLOT(bpp, Int);
}
void Item::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(instance, Int);
GET_SLOT(amount, Int);
GET_SLOT(name, String);
}
void Color::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(r, Int);
GET_SLOT(g, Int);
GET_SLOT(b, Int);
}
void BodyVisual::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(bodyModel, String);
GET_SLOT(bodyTxt, Int);
GET_SLOT(headModel, String);
GET_SLOT(headTxt, Int);
}
void NetworkStats::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(packetReceived, Int);
GET_SLOT(packetlossTotal, Int);
GET_SLOT(packetlossLastSecond, Int);
GET_SLOT(messagesInResendBuffer, Int);
GET_SLOT(messageInSendBuffer, Int);
GET_SLOT(bytesInResendBuffer, Int);
GET_SLOT(bytesInSendBuffer, Int);
}
void Position3dWithName::convert(SQObject object)
{
Array arrayWrapper(object);
GET_SLOT(name, String);
GET_SLOT(x, Float);
GET_SLOT(y, Float);
GET_SLOT(z, Float);
}
}

View File

@@ -0,0 +1,168 @@
#ifndef NONUT_G2O_SHARED_CUSTOM_TYPES_H
#define NONUT_G2O_SHARED_CUSTOM_TYPES_H
#include <string>
#include "Utils.h"
namespace nonut::g2o
{
struct GameTime : CustomType
{
void convert(SQObject object) override;
Int day{};
Int hour{};
Int min{};
auto toTuple()
{
return std::make_tuple(day, hour, min);
}
};
struct Position2d : CustomType
{
void convert(SQObject object) override;
Int x{};
Int y{};
auto toTuple()
{
return std::make_tuple(x, y);
}
};
struct Position3d : CustomType
{
void convert(SQObject object) override;
Float x{};
Float y{};
Float z{};
auto toTuple()
{
return std::make_tuple(x, y, z);
}
};
struct Size2d : CustomType
{
void convert(SQObject object) override;
Int width{};
Int height{};
auto toTuple()
{
return std::make_tuple(width, height);
}
};
struct Rect : CustomType
{
void convert(SQObject object) override;
Int x;
Int y;
Int width;
Int height;
auto toTuple()
{
return std::make_tuple(x, y, width, height);
}
};
struct UV : CustomType
{
void convert(SQObject object) override;
Float x;
Float y;
Float width;
Float height;
auto toTuple()
{
return std::make_tuple(x, y, width, height);
}
};
struct Resolution : CustomType
{
void convert(SQObject object) override;
Int x{};
Int y{};
Int bpp{};
auto toTuple()
{
return std::make_tuple(x, y, bpp);
}
};
struct Item : CustomType
{
void convert(SQObject object) override;
Int instance{};
Int amount{};
String name{};
auto toTuple()
{
return std::make_tuple(instance, amount, name);
}
};
struct Color : CustomType
{
void convert(SQObject object) override;
Int r{};
Int g{};
Int b{};
auto toTuple()
{
return std::make_tuple(r, g, b);
}
};
struct BodyVisual : CustomType
{
void convert(SQObject object) override;
String bodyModel{};
Int bodyTxt{};
String headModel{};
Int headTxt{};
auto toTuple()
{
return std::make_tuple(bodyModel, bodyTxt, headModel, headTxt);
}
};
struct NetworkStats : CustomType
{
void convert(SQObject object) override;
Int packetReceived{};
Int packetlossTotal{};
Int packetlossLastSecond{};
Int messagesInResendBuffer{};
Int messageInSendBuffer{};
Int bytesInResendBuffer{};
Int bytesInSendBuffer{};
auto toTuple()
{
return std::make_tuple(
packetReceived,
packetlossTotal,
packetlossLastSecond,
messagesInResendBuffer,
messageInSendBuffer,
bytesInResendBuffer,
bytesInSendBuffer);
}
};
struct Position3dWithName : CustomType
{
void convert(SQObject object) override;
String name{};
Float x{};
Float y{};
Float z{};
auto toTuple()
{
return std::make_tuple(name, x, y, z);
}
};
}
#endif // NONUT_G2O_SHARED_CUSTOM_TYPES_H

View File

@@ -1,5 +1,6 @@
#ifndef NONUT_CORE_INSTANCE_H
#define NONUT_CORE_INSTANCE_H
#include <sqapi.h>
namespace nonut
{

34
src/bind.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <pybind11/embed.h>
#include "classes/py/Packet.h"
namespace py = pybind11;
PYBIND11_EMBEDDED_MODULE(sqg2o, m) {
py::class_<PyPacket>(m, "Packet")
.def(py::init<>())
.def("reset", &PyPacket::reset)
.def("send", &PyPacket::send)
.def("sendToAll", &PyPacket::sendToAll)
.def("writeInt8", &PyPacket::writeInt8)
.def("writeUInt8", &PyPacket::writeUInt8)
.def("writeInt16", &PyPacket::writeInt16)
.def("writeUInt16", &PyPacket::writeUInt16)
.def("writeInt32", &PyPacket::writeInt32)
.def("writeUInt32", &PyPacket::writeUInt32)
.def("writeFloat", &PyPacket::writeFloat)
.def("writeBool", &PyPacket::writeBool)
.def("writeString", &PyPacket::writeString)
.def("readInt8", &PyPacket::readInt8)
.def("readUInt8", &PyPacket::readUInt8)
.def("readInt16", &PyPacket::readInt16)
.def("readUInt16", &PyPacket::readUInt16)
.def("readInt32", &PyPacket::readInt32)
.def("readUInt32", &PyPacket::readUInt32)
.def("readFloat", &PyPacket::readFloat)
.def("readBool", &PyPacket::readBool)
.def("readString", &PyPacket::readString)
.def("__del__", &PyPacket::del)
.def_property_readonly("bitsUsed", &PyPacket::getBitsUsed)
.def_property_readonly("bytesUsed", &PyPacket::getBytesUsed);
}

47
src/classes/py/Packet.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef _PYPACKET_H_
#define _PYPACKET_
#include <classes/sq/Packet.h>
class PyPacket
{
private:
nonut::Packet *sqpacket;
public:
PyPacket() { sqpacket = new nonut::Packet(); };
PyPacket(SQObject obj) { sqpacket = new nonut::Packet(obj); }
void reset() { sqpacket->reset(); }
void send(nonut::Int id, nonut::Int value) { sqpacket->send(id, value); }
void sendToAll(nonut::Int value) { sqpacket->sendToAll(value); };
void writeInt8(nonut::Int value) { sqpacket->writeInt8(value); }
void writeUInt8(nonut::Int value) { sqpacket->writeUInt8(value); }
void writeInt16(nonut::Int value) { sqpacket->writeInt16(value); }
void writeUInt16(nonut::Int value) { sqpacket->writeUInt16(value); }
void writeInt32(nonut::Int value) { sqpacket->writeInt32(value); }
void writeUInt32(nonut::Int value) { sqpacket->writeUInt32(value); }
void writeBool(nonut::Bool value) { sqpacket->writeBool(value); }
void writeFloat(nonut::Float value) { sqpacket->writeFloat(value); }
void writeString(nonut::String value) { sqpacket->writeString(value); }
nonut::Int readInt8() { return sqpacket->readInt8(); }
nonut::Int readUInt8() { return sqpacket->readUInt8(); }
nonut::Int readInt16() { return sqpacket->readInt16(); }
nonut::Int readUInt16() { return sqpacket->readUInt16(); }
nonut::Int readInt32() { return sqpacket->readInt32(); }
nonut::Int readUInt32() { return sqpacket->readUInt32(); }
nonut::Bool readBool() { return sqpacket->readBool(); }
nonut::Float readFloat() { return sqpacket->readFloat(); }
nonut::String readString() { return sqpacket->readString(); }
nonut::Int getBitsUsed() { return sqpacket->bitsUsed; }
nonut::Int getBytesUsed() { return sqpacket->bytesUsed; }
void del() { delete sqpacket; }
};
#endif

64
src/classes/sq/Packet.cpp Normal file
View File

@@ -0,0 +1,64 @@
#include <NoNut/core/CommonHeader.h>
#include "Packet.h"
namespace nonut
{
Packet::Packet() :
Class("Packet"),
METHOD_CTOR(reset),
METHOD_CTOR(send),
METHOD_CTOR(sendToAll),
METHOD_CTOR(writeBool),
METHOD_CTOR(writeInt8),
METHOD_CTOR(writeUInt8),
METHOD_CTOR(writeInt16),
METHOD_CTOR(writeUInt16),
METHOD_CTOR(writeInt32),
METHOD_CTOR(writeUInt32),
METHOD_CTOR(writeFloat),
METHOD_CTOR(writeString),
METHOD_CTOR(readBool),
METHOD_CTOR(readInt8),
METHOD_CTOR(readUInt8),
METHOD_CTOR(readInt16),
METHOD_CTOR(readUInt16),
METHOD_CTOR(readInt32),
METHOD_CTOR(readUInt32),
METHOD_CTOR(readFloat),
METHOD_CTOR(readString),
PROPERTY_CTOR(bitsUsed),
PROPERTY_CTOR(bytesUsed)
{
classCtor();
}
Packet::Packet(SQObject object) :
Class("Packet", object),
METHOD_CTOR(reset),
METHOD_CTOR(send),
METHOD_CTOR(sendToAll),
METHOD_CTOR(writeBool),
METHOD_CTOR(writeInt8),
METHOD_CTOR(writeUInt8),
METHOD_CTOR(writeInt16),
METHOD_CTOR(writeUInt16),
METHOD_CTOR(writeInt32),
METHOD_CTOR(writeUInt32),
METHOD_CTOR(writeFloat),
METHOD_CTOR(writeString),
METHOD_CTOR(readBool),
METHOD_CTOR(readInt8),
METHOD_CTOR(readUInt8),
METHOD_CTOR(readInt16),
METHOD_CTOR(readUInt16),
METHOD_CTOR(readInt32),
METHOD_CTOR(readUInt32),
METHOD_CTOR(readFloat),
METHOD_CTOR(readString),
PROPERTY_CTOR(bitsUsed),
PROPERTY_CTOR(bytesUsed)
{
}
}

45
src/classes/sq/Packet.h Normal file
View File

@@ -0,0 +1,45 @@
#ifndef NONUT_G2O_SERVER_CLASS_PACKET
#define NONUT_G2O_SERVER_CLASS_PACKET
#include <string>
#include <NoNut/core/Class.h>
#include <NoNut/core/CustomTypes.h>
namespace nonut
{
class Packet : public Class
{
public:
Packet();
explicit Packet(SQObject object);
// Methods
Function<void> reset;
Function<void, Int, Int> send;
Function<void, Int> sendToAll;
Function<void, Bool> writeBool;
Function<void, Int> writeInt8;
Function<void, Int> writeUInt8;
Function<void, Int> writeInt16;
Function<void, Int> writeUInt16;
Function<void, Int> writeInt32;
Function<void, Int> writeUInt32;
Function<void, Float> writeFloat;
Function<void, String&> writeString;
Function<Bool> readBool;
Function<Int> readInt8;
Function<Int> readUInt8;
Function<Int> readInt16;
Function<Int> readUInt16;
Function<Int> readInt32;
Function<Int> readUInt32;
Function<Float> readFloat;
Function<String> readString;
// Properties
Property<Int> bitsUsed;
Property<Int> bytesUsed;
};
}
#endif // NONUT_G2O_SERVER_CLASS_PACKET

View File

@@ -84,4 +84,6 @@ void registerSquirrelEvents()
addEventHandler("onPlayerEquipRing", sq_onPlayerEquipRing, 0);
addEventHandler("onPlayerEquipShield", sq_onPlayerEquipShield, 0);
addEventHandler("onPlayerEquipSpell", sq_onPlayerEquipSpell, 0);
addEventHandler("onPacket", sq_onPacket, 0);
}

View File

@@ -45,6 +45,8 @@ SQInteger sq_onPlayerSpellSetup(HSQUIRRELVM);
SQInteger sq_onPlayerTeleport(HSQUIRRELVM);
SQInteger sq_onPlayerToggleFaceAni(HSQUIRRELVM);
SQInteger sq_onPacket(HSQUIRRELVM);
void registerSquirrelEvents();
#endif

View File

@@ -0,0 +1,24 @@
#include <sqapi.h>
#include <pybind11/embed.h>
#include "NoNut/core/Utils.h"
#include <classes/py/Packet.h>
#include "sqevents.h"
namespace py = pybind11;
using namespace pybind11::literals;
extern py::module_ g2o;
SQInteger sq_onPacket(HSQUIRRELVM vm)
{
SQInteger playerid;
HSQOBJECT data;
nonut::sqGetValue(vm, 2, &playerid);
nonut::sqGetValue(vm, 3, &data);
py::dict kwargs = py::dict("playerid"_a=playerid, "data"_a=PyPacket(data));
callEvent("onPacket", kwargs);
return 0;
}

View File

@@ -1,7 +1,5 @@
#include <sqapi.h>
#include <pybind11/embed.h>
#include <iostream>
#include <pybind11/embed.h>
#include "events/sqevents.h"
namespace py = pybind11;
@@ -11,6 +9,7 @@ py::module_ pysys = py::module_::import("sys");
py::module_ g2o;
py::module_ scripts;
extern "C" SQRESULT SQRAT_API sqmodule_load(HSQUIRRELVM vm, HSQAPI api)
{
try