Filthy newline drama?
[craw.git] / craw / python_callback.cpp
blob91d5b7ae0400d2f21222ca1595ff5129cea18cd8
1 #include <boost/thread/mutex.hpp>
2 #include "d2_functions.hpp"
3 #include "python.hpp"
4 #include "utility.hpp"
6 namespace python
8 namespace
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)
30 if(!automap_handler)
31 return;
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))
39 exit_process();
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);
52 if(!return_value)
54 PyErr_Print();
55 return;
58 Py_DECREF(return_value);
61 bool perform_packet_callback(std::string const & packet)
63 if(!packet_handler)
64 return true;
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);
72 if(!return_value)
74 PyErr_Print();
75 return true;
78 bool output = (return_value == Py_True);
80 Py_DECREF(packet_string);
81 Py_DECREF(return_value);
83 return output;
86 bool perform_command_callback(std::string const & line)
88 if(!command_handler)
89 return false;
91 boost::mutex::scoped_lock lock(python_mutex);
93 PyObject * argument = create_string(line);
95 PyObject * return_value = PyObject_CallFunction(command_handler, "O", argument);
96 if(!return_value)
98 PyErr_Print();
99 return false;
102 bool output = (return_value == Py_True);
104 Py_DECREF(argument);
105 Py_DECREF(return_value);
107 return output;
110 bool perform_keyboard_callback(unsigned virtual_key)
112 if(!keyboard_handler)
113 return false;
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);
119 if(!return_value)
121 PyErr_Print();
122 return false;
125 Py_XDECREF(argument);
126 Py_DECREF(return_value);
128 return true;
131 bool perform_bncs_callback(std::string const & packet)
133 if(!bncs_packet_handler)
134 return false;
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);
140 if(!return_value)
142 PyErr_Print();
143 return false;
146 Py_XDECREF(argument);
147 Py_DECREF(return_value);
149 return true;
152 void perform_item_callback(unit & current_item)
154 if(!item_handler)
155 return;
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>(&current_item)));
163 exit_process();
164 return;
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(&current_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(&current_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);
191 if(!return_value)
193 PyErr_Print();
194 return;
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);