diff --git a/CMakeLists.txt b/CMakeLists.txt index c3608cc..63bb325 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,10 @@ file(GLOB_RECURSE SOURCE "src/NoNut/core/*.h" "src/NoNut/core/*.cpp" + "src/classes/sq/*.h" + "src/classes/sq/*.cpp" + "src/classes/py/*.h" + "src/classes/py/*.cpp" "src/*.h" "src/*.cpp" "src/events/*.h" diff --git a/g2o/__init__.py b/g2o/__init__.py index 201129a..a1fb6a6 100644 --- a/g2o/__init__.py +++ b/g2o/__init__.py @@ -1,5 +1,7 @@ -from g2o.events import addEvent -from g2o.events import callEvent -from g2o.events import event -from g2o.events import removeEventHandler \ No newline at end of file +from g2o.events import addEvent +from g2o.events import callEvent +from g2o.events import event +from g2o.events import removeEventHandler + +from g2o.packets import Packet \ No newline at end of file diff --git a/g2o/events.py b/g2o/events.py index 5c961aa..2d83c54 100644 --- a/g2o/events.py +++ b/g2o/events.py @@ -160,4 +160,6 @@ addEvent('onPlayerEquipMeleeWeapon') addEvent('onPlayerEquipRangedWeapon') addEvent('onPlayerEquipRing') addEvent('onPlayerEquipShield') -addEvent('onPlayerEquipSpell') \ No newline at end of file +addEvent('onPlayerEquipSpell') + +addEvent('onPacket') \ No newline at end of file diff --git a/g2o/packets.py b/g2o/packets.py new file mode 100644 index 0000000..d4cd6d5 --- /dev/null +++ b/g2o/packets.py @@ -0,0 +1,73 @@ +import sqg2o + +class Packet(sqg2o.Packet): + def __init__(self): + return super().__init__() + + def reset(self): + return super().reset() + + def send(self, playerid : int, reliability : int): + return super().send(playerid, reliability) + + def writeInt8(self, value : int): + return super().writeInt8(value) + + def writeUInt8(self, value : int): + return super().writeInt8(value) + + def writeInt16(self, value : int): + return super().writeInt16(value) + + def writeUInt16(self, value : int): + return super().writeInt16(value) + + def writeInt32(self, value : int): + return super().writeInt32(value) + + def writeUInt32(self, value : int): + return super().writeInt32(value) + + def writeBool(self, value : bool): + return super().writeBool(value) + + def writeFloat(self, value : float): + return super().writeFloat(value) + + def writeString(self, value : str): + return super().writeString(value) + + def readInt8(self,) -> int: + return super().readInt8() + + def readUInt8(self,) -> int: + return super().readInt8() + + def readInt16(self) -> int: + return super().readInt16() + + def readUInt16(self) -> int: + return super().readInt16() + + def readInt32(self) -> int: + return super().readInt32() + + def readUInt32(self) -> int: + return super().readInt32() + + def readBool(self) -> bool: + return super().readBool() + + def readFloat(self) -> float: + return super().readFloat() + + def readString(self) -> str: + return super().readString() + + @property + def bitsUsed(self): + return super().bitsUsed + + @property + def bytesUsed(self): + return super().bytesUsed \ No newline at end of file diff --git a/src/NoNut/core/CustomTypes.cpp b/src/NoNut/core/CustomTypes.cpp new file mode 100644 index 0000000..ed6e692 --- /dev/null +++ b/src/NoNut/core/CustomTypes.cpp @@ -0,0 +1,111 @@ +#include "CustomTypes.h" + +#include "Array.h" +#include "Property.h" + +namespace nonut::g2o +{ +#define GET_SLOT(slot, type) slot = arrayWrapper.get(#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); + } +} diff --git a/src/NoNut/core/CustomTypes.h b/src/NoNut/core/CustomTypes.h new file mode 100644 index 0000000..83a81c0 --- /dev/null +++ b/src/NoNut/core/CustomTypes.h @@ -0,0 +1,168 @@ +#ifndef NONUT_G2O_SHARED_CUSTOM_TYPES_H +#define NONUT_G2O_SHARED_CUSTOM_TYPES_H + +#include + +#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 diff --git a/src/NoNut/core/Instance.h b/src/NoNut/core/Instance.h index 8123793..fd68a80 100644 --- a/src/NoNut/core/Instance.h +++ b/src/NoNut/core/Instance.h @@ -1,5 +1,6 @@ #ifndef NONUT_CORE_INSTANCE_H #define NONUT_CORE_INSTANCE_H +#include namespace nonut { diff --git a/src/bind.cpp b/src/bind.cpp new file mode 100644 index 0000000..6e4b9ef --- /dev/null +++ b/src/bind.cpp @@ -0,0 +1,34 @@ +#include +#include "classes/py/Packet.h" + +namespace py = pybind11; + +PYBIND11_EMBEDDED_MODULE(sqg2o, m) { + py::class_(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); +} \ No newline at end of file diff --git a/src/classes/py/Packet.h b/src/classes/py/Packet.h new file mode 100644 index 0000000..e44bda6 --- /dev/null +++ b/src/classes/py/Packet.h @@ -0,0 +1,47 @@ +#ifndef _PYPACKET_H_ +#define _PYPACKET_ + +#include + +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 \ No newline at end of file diff --git a/src/classes/sq/Packet.cpp b/src/classes/sq/Packet.cpp new file mode 100644 index 0000000..65d743a --- /dev/null +++ b/src/classes/sq/Packet.cpp @@ -0,0 +1,64 @@ +#include +#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) + { + } +} diff --git a/src/classes/sq/Packet.h b/src/classes/sq/Packet.h new file mode 100644 index 0000000..db74606 --- /dev/null +++ b/src/classes/sq/Packet.h @@ -0,0 +1,45 @@ +#ifndef NONUT_G2O_SERVER_CLASS_PACKET +#define NONUT_G2O_SERVER_CLASS_PACKET +#include + +#include +#include + +namespace nonut +{ + class Packet : public Class + { + public: + Packet(); + explicit Packet(SQObject object); + + // Methods + Function reset; + Function send; + Function sendToAll; + Function writeBool; + Function writeInt8; + Function writeUInt8; + Function writeInt16; + Function writeUInt16; + Function writeInt32; + Function writeUInt32; + Function writeFloat; + Function writeString; + Function readBool; + Function readInt8; + Function readUInt8; + Function readInt16; + Function readUInt16; + Function readInt32; + Function readUInt32; + Function readFloat; + Function readString; + + // Properties + Property bitsUsed; + Property bytesUsed; + }; +} +#endif // NONUT_G2O_SERVER_CLASS_PACKET + diff --git a/src/events/sqevents.cpp b/src/events/sqevents.cpp index 3ee174c..416326e 100644 --- a/src/events/sqevents.cpp +++ b/src/events/sqevents.cpp @@ -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); } \ No newline at end of file diff --git a/src/events/sqevents.h b/src/events/sqevents.h index 0f54b9b..fb98d8d 100644 --- a/src/events/sqevents.h +++ b/src/events/sqevents.h @@ -45,6 +45,8 @@ SQInteger sq_onPlayerSpellSetup(HSQUIRRELVM); SQInteger sq_onPlayerTeleport(HSQUIRRELVM); SQInteger sq_onPlayerToggleFaceAni(HSQUIRRELVM); +SQInteger sq_onPacket(HSQUIRRELVM); + void registerSquirrelEvents(); #endif \ No newline at end of file diff --git a/src/events/sqevents_network.cpp b/src/events/sqevents_network.cpp new file mode 100644 index 0000000..0540306 --- /dev/null +++ b/src/events/sqevents_network.cpp @@ -0,0 +1,24 @@ +#include +#include +#include "NoNut/core/Utils.h" +#include +#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; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9be3115..d276e3b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,5 @@ #include #include -#include -#include #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