diff --git a/CMakeLists.txt b/CMakeLists.txt index bf367d3..b8ac43d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,8 @@ file(GLOB_RECURSE SOURCE "src/NoNut/core/*.h" "src/NoNut/core/*.cpp" + "src/classes/*.h" + "src/classes/*.cpp" "src/classes/sq/*.h" "src/classes/sq/*.cpp" "src/classes/py/*.h" diff --git a/g2o/__init__.py b/g2o/__init__.py index 4fbe286..4d1a4ac 100644 --- a/g2o/__init__.py +++ b/g2o/__init__.py @@ -9,6 +9,7 @@ from g2o.events import removeEvent from g2o.classes.packets import Packet from g2o.classes.damage import DamageDescription from g2o.classes.items import ItemGround +from g2o.classes.items import ItemsGround from g2o.classes.daedalus import Daedalus from g2o.classes.sky import Sky diff --git a/g2o/classes/items.py b/g2o/classes/items.py index 2736252..e92dbdb 100644 --- a/g2o/classes/items.py +++ b/g2o/classes/items.py @@ -1,5 +1,19 @@ import sqg2o +class ItemsGround: + + @staticmethod + def getById(id : int): + return sqg2o.ItemsGround.getById(id) + + @staticmethod + def create(data : dict) -> int: + return sqg2o.ItemsGround.create(data) + + @staticmethod + def destroy(id : int): + sqg2o.ItemsGround.destroy(id) + class ItemGround(sqg2o.ItemGround): """ This class represents item on the ground. diff --git a/src/NoNut/core/Class.cpp b/src/NoNut/core/Class.cpp index c0bb583..b631bcb 100644 --- a/src/NoNut/core/Class.cpp +++ b/src/NoNut/core/Class.cpp @@ -8,6 +8,7 @@ namespace nonut HSQUIRRELVM vm = Sqrat::DefaultVM::Get(); if (classObjectInstance._type == OT_NULL) { + bIsNull = true; const auto top = sq_gettop(vm); sq_pushroottable(vm); //push root table @@ -60,4 +61,9 @@ namespace nonut { return classObjectInstance; } + + bool Class::isNull() const + { + return bIsNull; + } } diff --git a/src/NoNut/core/Class.h b/src/NoNut/core/Class.h index d5cdde1..5a41b28 100644 --- a/src/NoNut/core/Class.h +++ b/src/NoNut/core/Class.h @@ -24,6 +24,7 @@ namespace nonut virtual ~Class(); [[nodiscard]] SQObject getInstance() const override; + bool isNull() const; protected: // Object holding information about class @@ -37,6 +38,8 @@ namespace nonut Function ctor(CONSTRUCTOR_NAME, classObjectInstance, classObject); ctor(std::forward(args)...); } + + bool bIsNull = false; }; } #endif // NONUT_CORE_CLASS_H diff --git a/src/NoNut/core/CustomTypes.cpp b/src/NoNut/core/CustomTypes.cpp index b7392ce..13e6e68 100644 --- a/src/NoNut/core/CustomTypes.cpp +++ b/src/NoNut/core/CustomTypes.cpp @@ -132,7 +132,7 @@ namespace nonut else if (value._type == OT_TABLE) { SqDict result = SqDict(); - result.convert(object); + result.convert(value); data[sq_objtostring(&key)] = result.data; } } diff --git a/src/bind.cpp b/src/bind.cpp index d4e5c27..8365620 100644 --- a/src/bind.cpp +++ b/src/bind.cpp @@ -4,6 +4,7 @@ #include "classes/py/ItemGround.h" #include "classes/py/Daedalus.h" #include "classes/py/Sky.h" +#include "classes/py/ItemsGround.h" #include namespace py = pybind11; @@ -86,4 +87,12 @@ PYBIND11_EMBEDDED_MODULE(sqg2o, m) { .def_static("setRainStopTime", [](int hour, int min){ return PySky::setRainStopTime(hour, min); }) .def_static("getRainStartTime", [](){ return PySky::getRainStartTime(); }) .def_static("getRainStopTime", [](){ return PySky::getRainStopTime(); }); + +// ------------------------------------------------------------------------- + + py::class_(m, "ItemsGround") + + .def_static("getById", [](int value){ return PyItemsGround::getById(value); }) + .def_static("create", [](py::dict value){ return PyItemsGround::create(value); }) + .def_static("destroy", [](int value){ return PyItemsGround::destroy(value); }); } \ No newline at end of file diff --git a/src/classes/Dictionary.cpp b/src/classes/Dictionary.cpp new file mode 100644 index 0000000..30f0e30 --- /dev/null +++ b/src/classes/Dictionary.cpp @@ -0,0 +1,41 @@ + +#include +#include +#include "Dictionary.h" +#include +namespace py = pybind11; + + +Sqrat::Table* PyDictionary::toSqObject(py::dict value) +{ + Sqrat::Table* result = new Sqrat::Table(Sqrat::DefaultVM::Get()); + HSQUIRRELVM vm = Sqrat::DefaultVM::Get(); + + for(auto item : value) + { + std::string key = item.first.cast(); + + if (py::isinstance(item.second)) + result->SetValue(key.c_str(), item.second.cast().c_str()); + else if (py::isinstance(item.second)) + result->SetValue(key.c_str(), item.second.cast()); + else if (py::isinstance(item.second)) + result->SetValue(key.c_str(), item.second.cast()); + else if (py::isinstance(item.second)) + result->SetValue(key.c_str(), item.second.cast()); + else if (py::isinstance(item.second)) + { + Sqrat::Table *pTable = PyDictionary::toSqObject(item.second.cast()); + + sq_pushobject(vm, result->GetObject()); + sq_pushstring(vm, key.c_str(), -1); + sq_pushobject(vm, pTable->GetObject()); + sq_newslot(vm, -3, false); + sq_pop(vm,1); + + delete pTable; + } + } + + return result; +}; \ No newline at end of file diff --git a/src/classes/Dictionary.h b/src/classes/Dictionary.h new file mode 100644 index 0000000..7ed5c18 --- /dev/null +++ b/src/classes/Dictionary.h @@ -0,0 +1,15 @@ +#ifndef _PYDICTIONARY_H_ +#define _PYDICTIONARY_H_ + +#include +#include +namespace py = pybind11; + +class PyDictionary +{ +public: + + static Sqrat::Table* toSqObject(py::dict value); +}; + +#endif \ No newline at end of file diff --git a/src/classes/py/DamageDescription.h b/src/classes/py/DamageDescription.h index 7ead0d6..c9b9512 100644 --- a/src/classes/py/DamageDescription.h +++ b/src/classes/py/DamageDescription.h @@ -9,7 +9,7 @@ private: nonut::DamageDescription *sqobj; public: - PyDamageDescription(SQObject obj) { sqobj = new nonut::DamageDescription(obj); } + PyDamageDescription(SQObject obj) { if (obj._type == OT_NULL) throw py::type_error("Presented Squirrel Object doesn't exist (type: null)"); sqobj = new nonut::DamageDescription(obj); } nonut::Int getFlags() { return sqobj->flags; } nonut::Int getDamage() { return sqobj->damage; } diff --git a/src/classes/py/ItemGround.h b/src/classes/py/ItemGround.h index 0423619..249a309 100644 --- a/src/classes/py/ItemGround.h +++ b/src/classes/py/ItemGround.h @@ -12,7 +12,8 @@ private: nonut::ItemGround *sqobj; public: - PyItemGround(SQObject obj) { sqobj = new nonut::ItemGround(obj); } + PyItemGround(SQObject obj) { if (obj._type == OT_NULL) throw py::type_error("Presented Squirrel Object doesn't exist (type: null)"); sqobj = new nonut::ItemGround(obj); } + PyItemGround(nonut::ItemGround obj) { if (obj.isNull()) throw py::type_error("Presented ItemGround doesn't exist (type: null)"); sqobj = &obj; } py::tuple getPosition() { return py::make_tuple(sqobj->getPosition().toTuple()); } py::tuple getRotation() { return py::make_tuple(sqobj->getRotation().toTuple()); } diff --git a/src/classes/py/ItemsGround.h b/src/classes/py/ItemsGround.h new file mode 100644 index 0000000..55f6fc3 --- /dev/null +++ b/src/classes/py/ItemsGround.h @@ -0,0 +1,24 @@ +#ifndef _PYITEMSGROUND_H_ +#define _PYITEMSGROUND_H_ + +#include +#include +#include "../Dictionary.h" + +class PyItemsGround +{ + +public: + + static PyItemGround getById(nonut::Int value) { return PyItemGround(nonut::ItemsGround::get()->getById(value)); } + static nonut::Int create(py::dict value) + { + Sqrat::Table* pTable = PyDictionary::toSqObject(value); + nonut::Int result = nonut::ItemsGround::get()->create(pTable->GetObject()); + delete pTable; + return result; + } + static void destroy(nonut::Int value) { nonut::ItemsGround::get()->destroy(value); } +}; + +#endif \ No newline at end of file diff --git a/src/classes/py/Packet.h b/src/classes/py/Packet.h index ab2ad03..209e5de 100644 --- a/src/classes/py/Packet.h +++ b/src/classes/py/Packet.h @@ -10,7 +10,7 @@ private: public: PyPacket() { sqpacket = new nonut::Packet(); }; - PyPacket(SQObject obj) { sqpacket = new nonut::Packet(obj); } + PyPacket(SQObject obj) { if (obj._type == OT_NULL) throw py::type_error("Presented Squirrel Object doesn't exist (type: null)"); sqpacket = new nonut::Packet(obj); } void reset() { sqpacket->reset(); } void send(nonut::Int id, nonut::Int value) { sqpacket->send(id, value); } diff --git a/src/classes/sq/ItemsGround.cpp b/src/classes/sq/ItemsGround.cpp new file mode 100644 index 0000000..c214a69 --- /dev/null +++ b/src/classes/sq/ItemsGround.cpp @@ -0,0 +1,23 @@ +#include +#include "ItemsGround.h" + +namespace nonut +{ + ItemsGround* ItemsGround::get() + { + if (inst == nullptr) + { + inst = new ItemsGround(); + } + return inst; + } + + ItemsGround::ItemsGround() : + StaticClass("ItemsGround"), + + METHOD_CTOR(getById), + METHOD_CTOR(create), + METHOD_CTOR(destroy) + { + } +} diff --git a/src/classes/sq/ItemsGround.h b/src/classes/sq/ItemsGround.h new file mode 100644 index 0000000..7ce3729 --- /dev/null +++ b/src/classes/sq/ItemsGround.h @@ -0,0 +1,30 @@ +#ifndef _ITEMSGROUND_H_ +#define _ITEMSGROUND_H_ +#include + +#include +#include +#include "ItemGround.h" +#include +namespace py = pybind11; + +namespace nonut +{ + class ItemsGround : public StaticClass + { + public: + static ItemsGround* get(); + + Function getById; + Function create; + Function destroy; + + private: + + static inline ItemsGround* inst = nullptr; + + ItemsGround(); + }; +} +#endif + diff --git a/src/constants/sqconstants.cpp b/src/constants/sqconstants.cpp index 0cc5fa0..0c9e221 100644 --- a/src/constants/sqconstants.cpp +++ b/src/constants/sqconstants.cpp @@ -1,7 +1,6 @@ #include #include #include "events/sqevents.h" -#include #include "sqconstants.h" namespace py = pybind11; diff --git a/src/main.cpp b/src/main.cpp index 9aa9c5e..3e30e7a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ extern "C" SQRESULT SQRAT_API sqmodule_load(HSQUIRRELVM vm, HSQAPI api) { registerSquirrelConstants(); registerSquirrelEvents(); - + g2o = py::module_::import("g2o"); scripts = py::module_::import("scripts");