feat: Added class ItemsGround

+ added exception handling on returning OT_NULL types
This commit is contained in:
AURUMVORXX
2024-11-07 20:14:20 +03:00
parent 4eaa9e4e2e
commit 0596853161
17 changed files with 174 additions and 6 deletions

View File

@@ -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"

View File

@@ -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

View File

@@ -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.

View File

@@ -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;
}
}

View File

@@ -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<void, Args...> ctor(CONSTRUCTOR_NAME, classObjectInstance, classObject);
ctor(std::forward<Args>(args)...);
}
bool bIsNull = false;
};
}
#endif // NONUT_CORE_CLASS_H

View File

@@ -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;
}
}

View File

@@ -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 <NoNut/core/Constant.h>
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_<PyItemsGround>(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); });
}

View File

@@ -0,0 +1,41 @@
#include <sqapi.h>
#include <pybind11/embed.h>
#include "Dictionary.h"
#include <NoNut/core/CustomTypes.h>
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<std::string>();
if (py::isinstance<py::str>(item.second))
result->SetValue(key.c_str(), item.second.cast<std::string>().c_str());
else if (py::isinstance<py::int_>(item.second))
result->SetValue(key.c_str(), item.second.cast<int>());
else if (py::isinstance<py::float_>(item.second))
result->SetValue(key.c_str(), item.second.cast<float>());
else if (py::isinstance<py::bool_>(item.second))
result->SetValue(key.c_str(), item.second.cast<bool>());
else if (py::isinstance<py::dict>(item.second))
{
Sqrat::Table *pTable = PyDictionary::toSqObject(item.second.cast<py::dict>());
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;
};

15
src/classes/Dictionary.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef _PYDICTIONARY_H_
#define _PYDICTIONARY_H_
#include <pybind11/embed.h>
#include <string>
namespace py = pybind11;
class PyDictionary
{
public:
static Sqrat::Table* toSqObject(py::dict value);
};
#endif

View File

@@ -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; }

View File

@@ -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()); }

View File

@@ -0,0 +1,24 @@
#ifndef _PYITEMSGROUND_H_
#define _PYITEMSGROUND_H_
#include <classes/sq/ItemsGround.h>
#include <classes/py/ItemGround.h>
#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

View File

@@ -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); }

View File

@@ -0,0 +1,23 @@
#include <NoNut/core/CommonHeader.h>
#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)
{
}
}

View File

@@ -0,0 +1,30 @@
#ifndef _ITEMSGROUND_H_
#define _ITEMSGROUND_H_
#include <string>
#include <NoNut/core/StaticClass.h>
#include <NoNut/core/CustomTypes.h>
#include "ItemGround.h"
#include <pybind11/embed.h>
namespace py = pybind11;
namespace nonut
{
class ItemsGround : public StaticClass
{
public:
static ItemsGround* get();
Function<ItemGround, Int> getById;
Function<Int, SQObject> create;
Function<void, Int> destroy;
private:
static inline ItemsGround* inst = nullptr;
ItemsGround();
};
}
#endif

View File

@@ -1,7 +1,6 @@
#include <sqapi.h>
#include <pybind11/embed.h>
#include "events/sqevents.h"
#include <iostream>
#include "sqconstants.h"
namespace py = pybind11;