1 #include <boost/thread/mutex.hpp>
2 #include "d2_functions.hpp"
10 boost::mutex python_mutex
;
13 struct global_interpreter_lock
15 PyGILState_STATE gil_state
;
17 global_interpreter_lock()
19 gil_state
= PyGILState_Ensure();
22 ~global_interpreter_lock()
24 PyGILState_Release(gil_state
);
28 void perform_automap_callback(unit
& current_unit
, int x
, int y
, uchar colour
)
33 boost::mutex::scoped_lock
lock(python_mutex
);
34 //global_interpreter_lock lock;
36 python_monster_data
* monster_data_pointer
= PyObject_New(python_monster_data
, &monster_data_type
);
37 python_monster_data
& current_monster_data
= *monster_data_pointer
;
38 if(!current_monster_data
.initialise(current_unit
))
41 current_monster_data
.x
= x
;
42 current_monster_data
.y
= y
;
43 current_monster_data
.colour
= colour
;
45 PyObject
* return_value
= PyObject_CallFunction(automap_handler
, "O", monster_data_pointer
);
47 Py_XDECREF(current_monster_data
.treasure_class
);
48 Py_XDECREF(current_monster_data
.special_abilities
);
49 Py_XDECREF(current_monster_data
.enchanted
);
50 Py_DECREF(monster_data_pointer
);
58 Py_DECREF(return_value
);
61 bool perform_packet_callback(std::string
const & packet
)
66 boost::mutex::scoped_lock
lock(python_mutex
);
67 //global_interpreter_lock lock;
69 PyObject
* packet_string
= create_string(packet
);
71 PyObject
* return_value
= PyObject_CallFunction(packet_handler
, "O", packet_string
);
78 bool output
= (return_value
== Py_True
);
80 Py_DECREF(packet_string
);
81 Py_DECREF(return_value
);
86 bool perform_command_callback(std::string
const & line
)
91 boost::mutex::scoped_lock
lock(python_mutex
);
93 PyObject
* argument
= create_string(line
);
95 PyObject
* return_value
= PyObject_CallFunction(command_handler
, "O", argument
);
102 bool output
= (return_value
== Py_True
);
105 Py_DECREF(return_value
);
110 bool perform_keyboard_callback(unsigned virtual_key
)
112 if(!keyboard_handler
)
115 boost::mutex::scoped_lock
lock(python_mutex
);
117 PyObject
* argument
= PyLong_FromUnsignedLong(virtual_key
);
118 PyObject
* return_value
= PyObject_CallFunction(keyboard_handler
, "O", argument
);
125 Py_XDECREF(argument
);
126 Py_DECREF(return_value
);
131 bool perform_bncs_callback(std::string
const & packet
)
133 if(!bncs_packet_handler
)
136 boost::mutex::scoped_lock
lock(python_mutex
);
138 PyObject
* argument
= PyString_FromStringAndSize(packet
.c_str(), packet
.size());
139 PyObject
* return_value
= PyObject_CallFunction(bncs_packet_handler
, "O", argument
);
146 Py_XDECREF(argument
);
147 Py_DECREF(return_value
);
152 void perform_item_callback(unit
& current_item
)
157 boost::mutex::scoped_lock
lock(python_mutex
);
159 item_text
* item_text_pointer
= d2_get_item_text(current_item
.table_index
);
160 if(item_text_pointer
== 0)
162 error("Item text pointer is 0 for item " + ail::hex_string_32(reinterpret_cast<unsigned>(¤t_item
)));
167 python_item_data
* item_data_pointer
= PyObject_New(python_item_data
, &item_data_type
);
168 python_item_data
& current_python_item_data
= *item_data_pointer
;
170 current_python_item_data
.id
= current_item
.id
;
172 wchar_t * unicode_name
= get_unit_name(¤t_item
);
173 std::string name
= wchar_to_string(unicode_name
);
174 current_python_item_data
.type
= create_string(name
);
176 std::string code
= item_text_pointer
->get_code();
177 current_python_item_data
.code
= create_string(code
);
179 item_data
& current_item_data
= *current_item
.item_data_pointer
;
181 current_python_item_data
.level
= current_item_data
.item_level
;
182 current_python_item_data
.quality
= current_item_data
.quality
;
184 current_python_item_data
.sockets
= d2_get_unit_stat(¤t_item
, 0xc2, 0);
186 current_python_item_data
.ethereal
= (current_item_data
.flags
& 0x00400000 ? Py_True
: Py_False
);
188 Py_INCREF(current_python_item_data
.ethereal
);
190 PyObject
* return_value
= PyObject_CallFunction(item_handler
, "O", item_data_pointer
);
197 Py_DECREF(current_python_item_data
.ethereal
);
198 Py_XDECREF(current_python_item_data
.type
);
199 Py_XDECREF(current_python_item_data
.code
);
200 Py_XDECREF(item_data_pointer
);
201 Py_DECREF(return_value
);