Crossfire Server, Trunk
cfpython.h
Go to the documentation of this file.
1 /*****************************************************************************/
2 /* CFPython - A Python module for Crossfire RPG. */
3 /* Version: 2.0beta8 (also known as "Alexander") */
4 /* Contact: yann.chachkoff@myrealbox.com */
5 /*****************************************************************************/
6 /* That code is placed under the GNU General Public Licence (GPL) */
7 /* (C)2001-2005 by Chachkoff Yann (Feel free to deliver your complaints) */
8 /*****************************************************************************/
9 /* CrossFire, A Multiplayer game for X-windows */
10 /* */
11 /* Copyright (C) 2000 Mark Wedel */
12 /* Copyright (C) 1992 Frank Tore Johansen */
13 /* */
14 /* This program is free software; you can redistribute it and/or modify */
15 /* it under the terms of the GNU General Public License as published by */
16 /* the Free Software Foundation; either version 2 of the License, or */
17 /* (at your option) any later version. */
18 /* */
19 /* This program is distributed in the hope that it will be useful, */
20 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
21 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
22 /* GNU General Public License for more details. */
23 /* */
24 /* You should have received a copy of the GNU General Public License */
25 /* along with this program; if not, write to the Free Software */
26 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 /* */
28 /*****************************************************************************/
29 #ifndef PLUGIN_PYTHON_H
30 #define PLUGIN_PYTHON_H
31 
32 /* First the required header files - only the CF module interface and Python */
33 #ifdef WIN32
34  #ifdef _DEBUG
35  #undef _DEBUG
36  #include <Python.h>
37  #define _DEBUG
38  #else
39  #include <Python.h>
40  #endif
41 #else /* WIN32 */
42  #include <Python.h>
43 #endif
44 
45 /* This is for allowing specific features of Python 3
46  * For example, Python 3.8 changes the typecasting on some stuff we use,
47  * so make a check for that.
48  */
49 #if PY_MAJOR_VERSION >= 3
50 # if PY_MINOR_VERSION >= 3
51 # define IS_PY3K3
52 # endif
53 # if PY_MINOR_VERSION >= 8
54 # define IS_PY3K8
55 # endif
56 # if PY_MINOR_VERSION >= 9
57 # define IS_PY3K9
58 # endif
59 # if PY_MINOR_VERSION >= 10
60 # define IS_PY3K10
61 # endif
62 #endif
63 
64 /* Handle Bytes vs. String */
65 #define CF_IS_PYSTR(cfpy_obj) (PyUnicode_Check(cfpy_obj))
66 
67 #include <plugin.h>
68 
69 #undef MODULEAPI
70 #ifdef WIN32
71 # ifdef PYTHON_PLUGIN_EXPORTS
72 # define MODULEAPI __declspec(dllexport)
73 # else
74 # define MODULEAPI __declspec(dllimport)
75 # endif
76 #else
77 #ifdef HAVE_VISIBILITY
78 # define MODULEAPI __attribute__((visibility("default")))
79 #else
80 # define MODULEAPI
81 #endif
82 #endif
83 
84 #define PLUGIN_NAME "Python"
85 #define PLUGIN_VERSION "CFPython Plugin 2.0a13 (Fido)"
86 
87 #include <plugin_common.h>
88 #include <cfpython_object.h>
89 #include <cfpython_map.h>
90 #include <cfpython_archetype.h>
91 #include <cfpython_party.h>
92 #include <cfpython_region.h>
93 
94 struct CFPContext {
96  PyObject *who;
97  PyObject *activator;
98  PyObject *third;
99  PyObject *event;
100  char message[1024];
102  char script[1024];
103  char options[1024];
105  struct talk_info *talk;
106 };
107 
108 typedef struct {
109  const char *name;
110  const int value;
111 } CFConstant;
112 
113 extern f_plug_api gethook;
114 
115 extern CFPContext *context_stack;
116 
118 
119 extern PyMethodDef CFPythonMethods[];
120 extern const CFConstant cstDirection[];
121 extern const CFConstant cstType[];
122 extern const CFConstant cstMove[];
123 extern const CFConstant cstMessageFlag[];
124 extern const CFConstant cstAttackType[];
125 extern const CFConstant cstAttackTypeNumber[];
126 extern const CFConstant cstEventType[];
127 extern const CFConstant cstTime[];
128 extern const CFConstant cstReplyTypes[];
129 extern const CFConstant cstAttackMovement[];
130 
131 #include <cfpython_proto.h>
132 
149 #if PY_VERSION_HEX == 0x030503F0
150 #define CF_PYTHON_OBJECT(NAME, DEALLOC, CONVERT, HASH, FLAGS, DOC, CMP, METHODS, GETSET, BASE, OBNEW) \
151 PyTypeObject Crossfire_ ## NAME ## Type = { \
152  /* See http://bugs.python.org/issue4385 */ \
153  PyVarObject_HEAD_INIT(NULL, 0) \
154  "Crossfire." #NAME, /* tp_name*/ \
155  sizeof(Crossfire_ ## NAME), /* tp_basicsize*/ \
156  0, /* tp_itemsize*/ \
157  DEALLOC, /* tp_dealloc*/ \
158  (printfunc)NULL, /* tp_print*/ \
159  NULL, /* tp_getattr*/ \
160  NULL, /* tp_setattr*/ \
161  NULL, /* tp_reserved */ \
162  NULL, /* tp_repr*/ \
163  CONVERT, /* tp_as_number*/ \
164  NULL, /* tp_as_sequence*/ \
165  NULL, /* tp_as_mapping*/ \
166  HASH, /* tp_hash */ \
167  NULL, /* tp_call*/ \
168  NULL, /* tp_str*/ \
169  PyObject_GenericGetAttr, /* tp_getattro*/ \
170  PyObject_GenericSetAttr, /* tp_setattro*/ \
171  NULL, /* tp_as_buffer*/ \
172  FLAGS, /* tp_flags*/ \
173  DOC, /* tp_doc */ \
174  NULL, /* tp_traverse */ \
175  NULL, /* tp_clear */ \
176  CMP, /* tp_richcompare */ \
177  0, /* tp_weaklistoffset */ \
178  NULL, /* tp_iter */ \
179  NULL, /* tp_iternext */ \
180  METHODS, /* tp_methods */ \
181  NULL, /* tp_members */ \
182  GETSET, /* tp_getset */ \
183  BASE, /* tp_base */ \
184  NULL, /* tp_dict */ \
185  NULL, /* tp_descr_get */ \
186  NULL, /* tp_descr_set */ \
187  0, /* tp_dictoffset */ \
188  NULL, /* tp_init */ \
189  NULL, /* tp_alloc */ \
190  OBNEW, /* tp_new */ \
191  NULL, /* tp_free */ \
192  NULL, /* tp_is_gc */ \
193  NULL, /* tp_bases */ \
194  NULL, /* tp_mro */ \
195  NULL, /* tp_cache */ \
196  NULL, /* tp_subclasses */ \
197  NULL, /* tp_weaklist */ \
198  NULL, /* tp_del */ \
199  0, /* tp_version_tag */ \
200  NULL /* tp_finalize */ \
201 }
202 #elif PY_VERSION_HEX == 0x030902F0
203 #define CF_PYTHON_OBJECT(NAME, DEALLOC, CONVERT, HASH, FLAGS, DOC, CMP, METHODS, GETSET, BASE, OBNEW) \
204 PyTypeObject Crossfire_ ## NAME ## Type = { \
205  /* See http://bugs.python.org/issue4385 */ \
206  PyVarObject_HEAD_INIT(NULL, 0) \
207  "Crossfire." #NAME, /* tp_name*/ \
208  sizeof(Crossfire_ ## NAME), /* tp_basicsize*/ \
209  0, /* tp_itemsize*/ \
210  DEALLOC, /* tp_dealloc*/ \
211  (printfunc)NULL, /* tp_print*/ \
212  NULL, /* tp_getattr*/ \
213  NULL, /* tp_setattr*/ \
214  NULL, /* tp_reserved */ \
215  NULL, /* tp_repr*/ \
216  CONVERT, /* tp_as_number*/ \
217  NULL, /* tp_as_sequence*/ \
218  NULL, /* tp_as_mapping*/ \
219  HASH, /* tp_hash */ \
220  NULL, /* tp_call*/ \
221  NULL, /* tp_str*/ \
222  PyObject_GenericGetAttr, /* tp_getattro*/ \
223  PyObject_GenericSetAttr, /* tp_setattro*/ \
224  NULL, /* tp_as_buffer*/ \
225  FLAGS, /* tp_flags*/ \
226  DOC, /* tp_doc */ \
227  NULL, /* tp_traverse */ \
228  NULL, /* tp_clear */ \
229  CMP, /* tp_richcompare */ \
230  0, /* tp_weaklistoffset */ \
231  NULL, /* tp_iter */ \
232  NULL, /* tp_iternext */ \
233  METHODS, /* tp_methods */ \
234  NULL, /* tp_members */ \
235  GETSET, /* tp_getset */ \
236  BASE, /* tp_base */ \
237  NULL, /* tp_dict */ \
238  NULL, /* tp_descr_get */ \
239  NULL, /* tp_descr_set */ \
240  0, /* tp_dictoffset */ \
241  NULL, /* tp_init */ \
242  NULL, /* tp_alloc */ \
243  OBNEW, /* tp_new */ \
244  NULL, /* tp_free */ \
245  NULL, /* tp_is_gc */ \
246  NULL, /* tp_bases */ \
247  NULL, /* tp_mro */ \
248  NULL, /* tp_cache */ \
249  NULL, /* tp_subclasses */ \
250  NULL, /* tp_weaklist */ \
251  NULL, /* tp_del */ \
252  0, /* tp_version_tag */ \
253  NULL, /* tp_finalize */ \
254  NULL /* tp_vectorcall */ \
255 }
256 #else
257 #define CF_PYTHON_OBJECT(NAME, DEALLOC, CONVERT, HASH, FLAGS, DOC, CMP, METHODS, GETSET, BASE, OBNEW) \
258 PyTypeObject Crossfire_ ## NAME ## Type = { \
259  /* See http://bugs.python.org/issue4385 */ \
260  PyVarObject_HEAD_INIT(NULL, 0) \
261  "Crossfire." #NAME, /* tp_name*/ \
262  sizeof(Crossfire_ ## NAME), /* tp_basicsize*/ \
263  0, /* tp_itemsize*/ \
264  DEALLOC, /* tp_dealloc*/ \
265  (printfunc)NULL, /* tp_print*/ \
266  NULL, /* tp_getattr*/ \
267  NULL, /* tp_setattr*/ \
268  NULL, /* tp_reserved */ \
269  NULL, /* tp_repr*/ \
270  CONVERT, /* tp_as_number*/ \
271  NULL, /* tp_as_sequence*/ \
272  NULL, /* tp_as_mapping*/ \
273  HASH, /* tp_hash */ \
274  NULL, /* tp_call*/ \
275  NULL, /* tp_str*/ \
276  PyObject_GenericGetAttr, /* tp_getattro*/ \
277  PyObject_GenericSetAttr, /* tp_setattro*/ \
278  NULL, /* tp_as_buffer*/ \
279  FLAGS, /* tp_flags*/ \
280  DOC, /* tp_doc */ \
281  NULL, /* tp_traverse */ \
282  NULL, /* tp_clear */ \
283  CMP, /* tp_richcompare */ \
284  0, /* tp_weaklistoffset */ \
285  NULL, /* tp_iter */ \
286  NULL, /* tp_iternext */ \
287  METHODS, /* tp_methods */ \
288  NULL, /* tp_members */ \
289  GETSET, /* tp_getset */ \
290  BASE, /* tp_base */ \
291  NULL, /* tp_dict */ \
292  NULL, /* tp_descr_get */ \
293  NULL, /* tp_descr_set */ \
294  0, /* tp_dictoffset */ \
295  NULL, /* tp_init */ \
296  NULL, /* tp_alloc */ \
297  OBNEW, /* tp_new */ \
298  NULL, /* tp_free */ \
299  NULL, /* tp_is_gc */ \
300  NULL, /* tp_bases */ \
301  NULL, /* tp_mro */ \
302  NULL, /* tp_cache */ \
303  NULL, /* tp_subclasses */ \
304  NULL, /* tp_weaklist */ \
305  NULL, /* tp_del */ \
306 }
307 #endif
308 
315 #if PY_VERSION_HEX == 0x030503F0 || PY_VERSION_HEX == 0x030902F0
316 #define CF_PYTHON_NUMBER_METHODS(NAME, LONG) \
317 static PyNumberMethods NAME ## Convert = { \
318  NULL, /* binaryfunc nb_add; */ /* __add__ */ \
319  NULL, /* binaryfunc nb_subtract; */ /* __sub__ */ \
320  NULL, /* binaryfunc nb_multiply; */ /* __mul__ */ \
321  NULL, /* binaryfunc nb_remainder; */ /* __mod__ */ \
322  NULL, /* binaryfunc nb_divmod; */ /* __divmod__ */ \
323  NULL, /* ternaryfunc nb_power; */ /* __pow__ */ \
324  NULL, /* unaryfunc nb_negative; */ /* __neg__ */ \
325  NULL, /* unaryfunc nb_positive; */ /* __pos__ */ \
326  NULL, /* unaryfunc nb_absolute; */ /* __abs__ */ \
327  NULL, /* inquiry nb_bool; */ /* __bool__ */ \
328  NULL, /* unaryfunc nb_invert; */ /* __invert__ */ \
329  NULL, /* binaryfunc nb_lshift; */ /* __lshift__ */ \
330  NULL, /* binaryfunc nb_rshift; */ /* __rshift__ */ \
331  NULL, /* binaryfunc nb_and; */ /* __and__ */ \
332  NULL, /* binaryfunc nb_xor; */ /* __xor__ */ \
333  NULL, /* binaryfunc nb_or; */ /* __or__ */ \
334  /* This is not a typo. For Py3k it should be Crossfire_Map_Long \
335  * and NOT Crossfire_Map_Int. \
336  */ \
337  LONG, /* unaryfunc nb_int; */ /* __int__ */ \
338  NULL, /* void *nb_reserved; */ \
339  NULL, /* unaryfunc nb_float; */ /* __float__ */ \
340  NULL, /* binaryfunc nb_inplace_add; */ \
341  NULL, /* binaryfunc nb_inplace_subtract; */ \
342  NULL, /* binaryfunc nb_inplace_multiply; */ \
343  NULL, /* binaryfunc nb_inplace_remainder; */ \
344  NULL, /* ternaryfunc nb_inplace_power; */ \
345  NULL, /* binaryfunc nb_inplace_lshift; */ \
346  NULL, /* binaryfunc nb_inplace_rshift; */ \
347  NULL, /* binaryfunc nb_inplace_and; */ \
348  NULL, /* binaryfunc nb_inplace_xor; */ \
349  NULL, /* binaryfunc nb_inplace_or; */ \
350  \
351  NULL, /* binaryfunc nb_floor_divide; */ \
352  NULL, /* binaryfunc nb_true_divide; */ \
353  NULL, /* binaryfunc nb_inplace_floor_divide; */ \
354  NULL, /* binaryfunc nb_inplace_true_divide; */ \
355  NULL, /* unaryfunc nb_index; */ \
356  NULL, /* binaryfunc nb_matrix_multiply; */ \
357  NULL /* binaryfunc nb_inplace_matrix_multiply; */ \
358 }
359 #else
360 #define CF_PYTHON_NUMBER_METHODS(NAME, LONG) \
361 static PyNumberMethods NAME ## Convert = { \
362  NULL, /* binaryfunc nb_add; */ /* __add__ */ \
363  NULL, /* binaryfunc nb_subtract; */ /* __sub__ */ \
364  NULL, /* binaryfunc nb_multiply; */ /* __mul__ */ \
365  NULL, /* binaryfunc nb_remainder; */ /* __mod__ */ \
366  NULL, /* binaryfunc nb_divmod; */ /* __divmod__ */ \
367  NULL, /* ternaryfunc nb_power; */ /* __pow__ */ \
368  NULL, /* unaryfunc nb_negative; */ /* __neg__ */ \
369  NULL, /* unaryfunc nb_positive; */ /* __pos__ */ \
370  NULL, /* unaryfunc nb_absolute; */ /* __abs__ */ \
371  NULL, /* inquiry nb_bool; */ /* __bool__ */ \
372  NULL, /* unaryfunc nb_invert; */ /* __invert__ */ \
373  NULL, /* binaryfunc nb_lshift; */ /* __lshift__ */ \
374  NULL, /* binaryfunc nb_rshift; */ /* __rshift__ */ \
375  NULL, /* binaryfunc nb_and; */ /* __and__ */ \
376  NULL, /* binaryfunc nb_xor; */ /* __xor__ */ \
377  NULL, /* binaryfunc nb_or; */ /* __or__ */ \
378  /* This is not a typo. For Py3k it should be Crossfire_Map_Long \
379  * and NOT Crossfire_Map_Int. \
380  */ \
381  LONG, /* unaryfunc nb_int; */ /* __int__ */ \
382  NULL, /* void *nb_reserved; */ \
383  NULL, /* unaryfunc nb_float; */ /* __float__ */ \
384  NULL, /* binaryfunc nb_inplace_add; */ \
385  NULL, /* binaryfunc nb_inplace_subtract; */ \
386  NULL, /* binaryfunc nb_inplace_multiply; */ \
387  NULL, /* binaryfunc nb_inplace_remainder; */ \
388  NULL, /* ternaryfunc nb_inplace_power; */ \
389  NULL, /* binaryfunc nb_inplace_lshift; */ \
390  NULL, /* binaryfunc nb_inplace_rshift; */ \
391  NULL, /* binaryfunc nb_inplace_and; */ \
392  NULL, /* binaryfunc nb_inplace_xor; */ \
393  NULL, /* binaryfunc nb_inplace_or; */ \
394  \
395  NULL, /* binaryfunc nb_floor_divide; */ \
396  NULL, /* binaryfunc nb_true_divide; */ \
397  NULL, /* binaryfunc nb_inplace_floor_divide; */ \
398  NULL, /* binaryfunc nb_inplace_true_divide; */ \
399  NULL /* unaryfunc nb_index; */ \
400 }
401 #endif
402 
403 #endif /* PLUGIN_PYTHON_H */
CFPContext::event_code
int event_code
Definition: cfpython.h:101
cstReplyTypes
const CFConstant cstReplyTypes[]
Definition: cfpython.cpp:1383
CFPContext::third
PyObject * third
Definition: cfpython.h:98
CFPContext::activator
PyObject * activator
Definition: cfpython.h:97
cfpython_party.h
CFConstant::value
const int value
Definition: cfpython.h:110
cfpython_region.h
cstDirection
const CFConstant cstDirection[]
Definition: cfpython.cpp:1112
plugin.h
cstEventType
const CFConstant cstEventType[]
Definition: cfpython.cpp:1331
CFPContext::down
CFPContext * down
Definition: cfpython.h:95
CFConstant::name
const char * name
Definition: cfpython.h:109
CFPContext::who
PyObject * who
Definition: cfpython.h:96
cstMove
const CFConstant cstMove[]
Definition: cfpython.cpp:1240
f_plug_api
void(* f_plug_api)(int *type,...)
Definition: plugin.h:79
current_context
CFPContext * current_context
Definition: cfpython.cpp:106
CFPContext
Definition: cfpython.h:94
cstType
const CFConstant cstType[]
Definition: cfpython.cpp:1124
cstTime
const CFConstant cstTime[]
Definition: cfpython.cpp:1373
context_stack
CFPContext * context_stack
Definition: cfpython.cpp:104
CFPContext::message
char message[1024]
Definition: cfpython.h:100
cfpython_object.h
CFPContext::talk
struct talk_info * talk
Definition: cfpython.h:105
cstAttackType
const CFConstant cstAttackType[]
Definition: cfpython.cpp:1271
cstAttackTypeNumber
const CFConstant cstAttackTypeNumber[]
Definition: cfpython.cpp:1301
CFConstant
Definition: cfpython.h:108
plugin_common.h
CFPythonMethods
PyMethodDef CFPythonMethods[]
Definition: cfpython.cpp:765
cstMessageFlag
const CFConstant cstMessageFlag[]
Definition: cfpython.cpp:1251
talk_info
Definition: dialog.h:50
CFPContext::event
PyObject * event
Definition: cfpython.h:99
cfpython_archetype.h
gethook
f_plug_api gethook
Definition: cfnewspaper.cpp:41
CFPContext::options
char options[1024]
Definition: cfpython.h:103
cstAttackMovement
const CFConstant cstAttackMovement[]
Definition: cfpython.cpp:1390
CFPContext::script
char script[1024]
Definition: cfpython.h:102
cfpython_proto.h
CFPContext::returnvalue
int returnvalue
Definition: cfpython.h:104
cfpython_map.h