Skip to content

Commit

Permalink
[python] fix: Object.from_dict to use the object system
Browse files Browse the repository at this point in the history
  • Loading branch information
jd28 committed Nov 28, 2024
1 parent 68e7434 commit 94850e9
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions rollnw-py/wrapper_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ nlohmann::json to_json_helper_player(const nw::Player* self)
template <typename T>
T* create_object_from_json_helper(const nlohmann::json& j)
{
auto obj = new T;
auto obj = nw::kernel::objects().make<T>();
if constexpr (!std::is_same_v<T, nw::Player>) {
T::deserialize(obj, j, nw::SerializationProfile::blueprint);
} else {
Expand Down Expand Up @@ -239,8 +239,8 @@ void init_objects_creature(py::module& nw)
py::class_<nw::Creature, nw::ObjectBase>(nw, "Creature")
.def(py::init<>())

.def_static("from_dict", &create_object_from_json_helper<nw::Creature>)
.def_static("from_file", &create_object_from_file_helper<nw::Creature>, py::return_value_policy::reference_internal)
.def_static("from_dict", &create_object_from_json_helper<nw::Creature>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Creature>, py::return_value_policy::reference)

.def("to_dict", &to_json_helper<nw::Creature>)
.def("handle", &nw::Creature::handle)
Expand Down Expand Up @@ -324,8 +324,8 @@ void init_objects_door(py::module& nw)
.def_readonly_static("json_archive_version", &nw::Door::json_archive_version)
.def_readonly_static("object_type", &nw::Door::object_type)

.def_static("from_dict", &create_object_from_json_helper<nw::Door>)
.def_static("from_file", &create_object_from_file_helper<nw::Door>, py::return_value_policy::reference_internal)
.def_static("from_dict", &create_object_from_json_helper<nw::Door>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Door>, py::return_value_policy::reference)

.def_readwrite("common", &nw::Door::common)
.def_readwrite("conversation", &nw::Door::conversation)
Expand Down Expand Up @@ -378,8 +378,8 @@ void init_objects_encounter(py::module& nw)
.def_readonly_static("json_archive_version", &nw::Encounter::json_archive_version)
.def_readonly_static("object_type", &nw::Encounter::object_type)

.def_static("from_dict", &create_object_from_json_helper<nw::Encounter>)
.def_static("from_file", &create_object_from_file_helper<nw::Encounter>, py::return_value_policy::reference_internal)
.def_static("from_dict", &create_object_from_json_helper<nw::Encounter>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Encounter>, py::return_value_policy::reference)

.def_readwrite("common", &nw::Encounter::common)
.def_readwrite("creatures", &nw::Encounter::creatures)
Expand Down Expand Up @@ -460,8 +460,8 @@ void init_objects_item(py::module& nw)
.def_readonly_static("json_archive_version", &nw::Item::json_archive_version)
.def_readonly_static("object_type", &nw::Item::object_type)

.def_static("from_dict", &create_object_from_json_helper<nw::Item>)
.def_static("from_file", &create_object_from_file_helper<nw::Item>, py::return_value_policy::reference_internal)
.def_static("from_dict", &create_object_from_json_helper<nw::Item>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Item>, py::return_value_policy::reference)

.def_readwrite("common", &nw::Item::common)

Expand Down Expand Up @@ -594,8 +594,8 @@ void init_objects_placeable(py::module& nw)
.def_readonly_static("json_archive_version", &nw::Placeable::json_archive_version)
.def_readonly_static("object_type", &nw::Placeable::object_type)

.def_static("from_dict", &create_object_from_json_helper<nw::Placeable>)
.def_static("from_file", &create_object_from_file_helper<nw::Placeable>, py::return_value_policy::reference_internal)
.def_static("from_dict", &create_object_from_json_helper<nw::Placeable>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Placeable>, py::return_value_policy::reference)

.def_readwrite("common", &nw::Placeable::common)
.def_readwrite("conversation", &nw::Placeable::conversation)
Expand Down Expand Up @@ -632,8 +632,8 @@ void init_objects_player(py::module& nw)

.def("to_dict", &to_json_helper_player)

.def_static("from_dict", &create_object_from_json_helper<nw::Player>)
.def_static("from_file", &create_object_from_file_helper<nw::Player>, py::return_value_policy::reference_internal);
.def_static("from_dict", &create_object_from_json_helper<nw::Player>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Player>, py::return_value_policy::reference);
}

void init_objects_sound(py::module& nw)
Expand All @@ -646,8 +646,8 @@ void init_objects_sound(py::module& nw)
.def_readonly_static("json_archive_version", &nw::Sound::json_archive_version)
.def_readonly_static("object_type", &nw::Sound::object_type)

.def_static("from_dict", &create_object_from_json_helper<nw::Sound>)
.def_static("from_file", &create_object_from_file_helper<nw::Sound>, py::return_value_policy::reference_internal)
.def_static("from_dict", &create_object_from_json_helper<nw::Sound>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Sound>, py::return_value_policy::reference)

.def_readwrite("common", &nw::Sound::common)
.def_readwrite("sounds", &nw::Sound::sounds)
Expand Down Expand Up @@ -688,8 +688,8 @@ void init_objects_store(py::module& nw)
.def_readonly_static("json_archive_version", &nw::Store::json_archive_version)
.def_readonly_static("object_type", &nw::Store::object_type)

.def_static("from_dict", &create_object_from_json_helper<nw::Store>)
.def_static("from_file", &create_object_from_file_helper<nw::Store>, py::return_value_policy::reference_internal)
.def_static("from_dict", &create_object_from_json_helper<nw::Store>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Store>, py::return_value_policy::reference)

.def_readwrite("common", &nw::Store::common)

Expand Down Expand Up @@ -735,8 +735,8 @@ void init_object_trigger(pybind11::module& nw)
.def_readonly_static("json_archive_version", &nw::Trigger::json_archive_version)
.def_readonly_static("object_type", &nw::Trigger::object_type)

.def_static("from_dict", &create_object_from_json_helper<nw::Trigger>)
.def_static("from_file", &create_object_from_file_helper<nw::Trigger>, py::return_value_policy::reference_internal)
.def_static("from_dict", &create_object_from_json_helper<nw::Trigger>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Trigger>, py::return_value_policy::reference)

.def_readwrite("common", &nw::Trigger::common)
.def_readwrite("geometry", &nw::Trigger::geometry)
Expand All @@ -763,8 +763,8 @@ void init_object_waypoint(pybind11::module& nw)
.def_readonly_static("json_archive_version", &nw::Waypoint::json_archive_version)
.def_readonly_static("object_type", &nw::Waypoint::object_type)

.def_static("from_dict", &create_object_from_json_helper<nw::Waypoint>)
.def_static("from_file", &create_object_from_file_helper<nw::Waypoint>, py::return_value_policy::reference_internal)
.def_static("from_dict", &create_object_from_json_helper<nw::Waypoint>, py::return_value_policy::reference)
.def_static("from_file", &create_object_from_file_helper<nw::Waypoint>, py::return_value_policy::reference)

.def("to_dict", &to_json_helper<nw::Waypoint>)

Expand Down

0 comments on commit 94850e9

Please sign in to comment.