65 #define PYTHON_CACHE_SIZE 16
83 static PyObject *
matchString(PyObject *
self, PyObject *args);
84 static PyObject *
findPlayer(PyObject *
self, PyObject *args);
85 static PyObject *
readyMap(PyObject *
self, PyObject *args);
86 static PyObject *
createMap(PyObject *
self, PyObject *args);
94 static PyObject *
getWhoAmI(PyObject *
self, PyObject *args);
96 static PyObject *
getWhoIsThird(PyObject *
self, PyObject *args);
98 static PyObject *
getScriptName(PyObject *
self, PyObject *args);
100 static PyObject *
getEvent(PyObject *
self, PyObject *args);
103 static PyObject *
getArchetypes(PyObject *
self, PyObject *args);
104 static PyObject *
getPlayers(PyObject *
self, PyObject *args);
105 static PyObject *
getMaps(PyObject *
self, PyObject *args);
106 static PyObject *
getParties(PyObject *
self, PyObject *args);
107 static PyObject *
getRegions(PyObject *
self, PyObject *args);
113 static PyObject *
getTime(PyObject *
self, PyObject *args);
114 static PyObject *
destroyTimer(PyObject *
self, PyObject *args);
116 static PyObject *
findAnimation(PyObject *
self, PyObject *args);
117 static PyObject *
log_message(PyObject *
self, PyObject *args);
118 static PyObject *
findFace(PyObject *
self, PyObject *args);
119 static PyObject *
getSeasonName(PyObject *
self, PyObject *args);
120 static PyObject *
getMonthName(PyObject *
self, PyObject *args);
133 PyErr_SetString(PyExc_ValueError, buf);
137 {
"WhoAmI",
getWhoAmI, METH_NOARGS, NULL },
143 {
"WhatIsEvent",
getEvent, METH_NOARGS, NULL },
151 {
"ReadyMap",
readyMap, METH_VARARGS, NULL },
152 {
"CreateMap",
createMap, METH_VARARGS, NULL },
153 {
"FindPlayer",
findPlayer, METH_VARARGS, NULL },
154 {
"MatchString",
matchString, METH_VARARGS, NULL },
162 {
"GetPlayers",
getPlayers, METH_NOARGS, NULL },
164 {
"GetMaps",
getMaps, METH_NOARGS, NULL },
165 {
"GetParties",
getParties, METH_NOARGS, NULL },
166 {
"GetRegions",
getRegions, METH_NOARGS, NULL },
171 {
"GetTime",
getTime, METH_NOARGS, NULL },
175 {
"FindFace",
findFace, METH_VARARGS, NULL },
181 { NULL, NULL, 0, NULL }
197 if (!PyArg_ParseTuple(args,
"i", &eventcode))
209 if (!PyArg_ParseTuple(args,
"i", &eventcode))
230 if (!PyArg_ParseTuple(args,
"s", &obname))
241 return Py_BuildValue(
"i", i);
245 return Py_BuildValue(
"i", current_context->
returnvalue);
251 if (!PyArg_ParseTuple(args,
"i", &i))
263 if (!PyArg_ParseTuple(args,
"ss", &premiere, &seconde))
268 return Py_BuildValue(
"i", 1);
270 return Py_BuildValue(
"i", 0);
273 static PyObject *
findPlayer(PyObject *
self, PyObject *args) {
277 if (!PyArg_ParseTuple(args,
"s", &txt))
290 static PyObject *
readyMap(PyObject *
self, PyObject *args) {
295 if (!PyArg_ParseTuple(args,
"s|i", &mapname, &flags))
303 static PyObject *
createMap(PyObject *
self, PyObject *args) {
307 if (!PyArg_ParseTuple(args,
"ii", &sizex, &sizey))
343 static PyObject *
getWhoAmI(PyObject *
self, PyObject *args) {
344 if (!current_context->
who) {
348 Py_INCREF(current_context->
who);
349 return current_context->
who;
362 if (!current_context->
third) {
366 Py_INCREF(current_context->
third);
367 return current_context->
third;
371 if (current_context->
message == NULL)
372 return Py_BuildValue(
"");
374 return Py_BuildValue(
"s", current_context->
message);
378 return Py_BuildValue(
"s", current_context->
script);
382 if (!current_context->
options) {
386 return Py_BuildValue(
"s", current_context->
options);
389 static PyObject *
getEvent(PyObject *
self, PyObject *args) {
390 if (!current_context->
event) {
394 Py_INCREF(current_context->
event);
395 return current_context->
event;
420 list = PyList_New(0);
429 static PyObject *
getPlayers(PyObject *
self, PyObject *args) {
433 list = PyList_New(0);
442 static PyObject *
getMaps(PyObject *
self, PyObject *args) {
446 list = PyList_New(0);
455 static PyObject *
getParties(PyObject *
self, PyObject *args) {
459 list = PyList_New(0);
468 static PyObject *
getRegions(PyObject *
self, PyObject *args) {
472 list = PyList_New(0);
485 list = PyList_New(0);
500 if (!PyArg_ParseTuple(args,
"ssd", &cmdname, &scriptname, &cmdspeed))
509 if (CustomCommand[i].name != NULL) {
510 if (!strcmp(CustomCommand[i].name, cmdname)) {
511 set_exception(
"command '%s' is already registered", cmdname);
517 if (CustomCommand[i].name == NULL) {
520 CustomCommand[i].
speed = cmdspeed;
529 static PyObject *
getTime(PyObject *
self, PyObject *args) {
535 list = PyList_New(0);
536 PyList_Append(list, Py_BuildValue(
"i", tod.
year));
537 PyList_Append(list, Py_BuildValue(
"i", tod.
month));
538 PyList_Append(list, Py_BuildValue(
"i", tod.
day));
539 PyList_Append(list, Py_BuildValue(
"i", tod.
hour));
540 PyList_Append(list, Py_BuildValue(
"i", tod.
minute));
541 PyList_Append(list, Py_BuildValue(
"i", tod.
dayofweek));
542 PyList_Append(list, Py_BuildValue(
"i", tod.
weekofmonth));
543 PyList_Append(list, Py_BuildValue(
"i", tod.
season));
544 PyList_Append(list, Py_BuildValue(
"i", tod.
periodofday));
552 if (!PyArg_ParseTuple(args,
"i", &
id))
560 if (!PyArg_ParseTuple(args,
"s", &name))
565 static PyObject *
findFace(PyObject *
self, PyObject *args) {
568 if (!PyArg_ParseTuple(args,
"s", &name))
578 if (!PyArg_ParseTuple(args,
"is", &intLevel, &message))
601 if ((message != NULL) && (message[strlen(message)] ==
'\n'))
602 cf_log(level,
"CFPython: %s", message);
604 cf_log(level,
"CFPython: %s\n", message);
612 if (!PyArg_ParseTuple(args,
"s", &name))
620 if (!PyArg_ParseTuple(args,
"i", &i))
628 if (!PyArg_ParseTuple(args,
"i", &i))
636 if (!PyArg_ParseTuple(args,
"i", &i))
644 if (!PyArg_ParseTuple(args,
"i", &i))
650 current_context = NULL;
651 context_stack = NULL;
655 if (current_context == NULL) {
656 context_stack = context;
657 context->
down = NULL;
661 current_context = context;
667 if (current_context != NULL) {
669 current_context = current_context->
down;
677 Py_XDECREF(context->
event);
678 Py_XDECREF(context->
third);
679 Py_XDECREF(context->
who);
686 PyObject *scriptfile;
688 struct stat stat_buf;
693 if (!(scriptfile = PyFile_FromString(filename,
"r"))) {
694 cf_log(
llevDebug,
"cfpython - The Script file %s can't be opened\n", filename);
697 if (stat(filename, &stat_buf)) {
698 cf_log(
llevDebug,
"cfpython - The Script file %s can't be stat:ed\n", filename);
700 Py_DECREF(scriptfile);
714 if (pycode_cache[i].file == NULL) {
715 replace = &pycode_cache[i];
717 }
else if (pycode_cache[i].file == sh_path) {
719 if (pycode_cache[i].code == NULL || (pycode_cache[i].cached_time < stat_buf.st_mtime)) {
721 replace = &pycode_cache[i];
725 run = &pycode_cache[i];
728 }
else if (replace == NULL || pycode_cache[i].used_time < replace->used_time)
730 replace = &pycode_cache[i];
735 Py_XDECREF(replace->
code);
736 replace->
code = NULL;
739 if (replace->
file != sh_path) {
747 if (!scriptfile && !(scriptfile = PyFile_FromString(filename,
"r"))) {
748 cf_log(
llevDebug,
"cfpython - The Script file %s can't be opened\n", filename);
749 replace->
code = NULL;
752 if ((n = PyParser_SimpleParseFile(PyFile_AsFile(scriptfile), filename, Py_file_input))) {
753 replace->
code = PyNode_Compile(n, filename);
757 if (PyErr_Occurred())
768 Py_DECREF(scriptfile);
778 PyCodeObject *pycode;
790 PyDict_SetItemString(dict,
"__builtins__", PyEval_GetBuiltins());
791 ret = PyEval_EvalCode(pycode, dict, NULL);
792 if (PyErr_Occurred()) {
797 printf(
"cfpython - %d items in heap\n", PyDict_Size(dict));
798 list = PyDict_Values(dict);
799 for (item = PyList_Size(list)-1; item >= 0; item--) {
800 dict = PyList_GET_ITEM(list, item);
801 ret = PyObject_Str(dict);
802 printf(
" ref %s = %d\n", PyString_AsString(ret), dict->ob_refcnt);
824 strncpy(tmp,
"Crossfire_",
sizeof(tmp));
825 strncat(tmp, name,
sizeof(tmp)-strlen(tmp));
827 new = Py_InitModule(tmp, NULL);
830 while (constants[i].name != NULL) {
831 PyModule_AddIntConstant(
new, (
char *)constants[i].name, constants[i].value);
832 PyDict_SetItem(dict, PyInt_FromLong(constants[i].value), PyString_FromString(constants[i].name));
835 PyDict_SetItemString(PyModule_GetDict(module), name,
new);
841 strncpy(tmp, name,
sizeof(tmp));
842 strncat(tmp,
"Name",
sizeof(tmp)-strlen(tmp));
843 PyDict_SetItemString(PyModule_GetDict(module), tmp, dict);
857 strncpy(tmp,
"Crossfire_",
sizeof(tmp));
858 strncat(tmp, name,
sizeof(tmp)-strlen(tmp));
860 new = Py_InitModule(tmp, NULL);
862 while (constants[i].name != NULL) {
863 PyModule_AddIntConstant(
new, (
char *)constants[i].name, constants[i].value);
866 PyDict_SetItemString(PyModule_GetDict(module), name,
new);
1071 static const CFConstant cstAttackTypeNumber[] = {
1152 addConstants(module,
"AttackTypeNumber", cstAttackTypeNumber);
1170 Py_Py3kWarningFlag++;
1188 d = PyModule_GetDict(m);
1203 PyModule_AddObject(m,
"LogError", Py_BuildValue(
"i",
llevError));
1204 PyModule_AddObject(m,
"LogInfo", Py_BuildValue(
"i",
llevInfo));
1205 PyModule_AddObject(m,
"LogDebug", Py_BuildValue(
"i",
llevDebug));
1206 PyModule_AddObject(m,
"LogMonster", Py_BuildValue(
"i",
llevMonster));
1208 CFPythonError = PyErr_NewException(
"Crossfire.error", NULL, NULL);
1211 CustomCommand[i].
name = NULL;
1212 CustomCommand[i].
script = NULL;
1213 CustomCommand[i].
speed = 0.0;
1226 const char *propname;
1231 va_start(args, type);
1232 propname = va_arg(args,
const char *);
1234 if (!strcmp(propname,
"command?")) {
1235 const char *cmdname;
1236 cmdname = va_arg(args,
const char *);
1241 if (CustomCommand[i].name != NULL) {
1242 if (!strcmp(CustomCommand[i].name, cmdname)) {
1243 rtn_cmd->
name = CustomCommand[i].
name;
1244 rtn_cmd->
time = (float)CustomCommand[i].speed;
1252 }
else if (!strcmp(propname,
"Identification")) {
1253 buf = va_arg(args,
char *);
1254 size = va_arg(args,
int);
1258 }
else if (!strcmp(propname,
"FullName")) {
1259 buf = va_arg(args,
char *);
1260 size = va_arg(args,
int);
1270 char buf[1024], path[1024];
1277 cf_log(
llevError,
"Illegal call of cfpython_runPluginCommand, call find_plugin_command first.\n");
1287 context->
third = NULL;
1310 PyObject *scriptfile;
1333 scriptfile = PyFile_FromString(
cf_get_maps_directory(
"python/events/python_init.py", path,
sizeof(path)),
"r");
1334 if (scriptfile != NULL) {
1335 PyRun_SimpleFile(PyFile_AsFile(scriptfile),
cf_get_maps_directory(
"python/events/python_init.py", path,
sizeof(path)));
1336 Py_DECREF(scriptfile);
1353 va_start(args, type);
1358 context->
who = NULL;
1360 context->
third = NULL;
1361 context->
event = NULL;
1371 op = va_arg(args,
object *);
1377 op = va_arg(args,
object *);
1383 op = va_arg(args,
object *);
1390 pl = va_arg(args,
player *);
1392 buf = va_arg(args,
char *);
1399 pl = va_arg(args,
player *);
1401 buf = va_arg(args,
char *);
1408 op = va_arg(args,
object *);
1414 op = va_arg(args,
object *);
1416 buf = va_arg(args,
char *);
1423 op = va_arg(args,
object *);
1425 buf = va_arg(args,
char *);
1432 op = va_arg(args,
object *);
1434 buf = va_arg(args,
char *);
1441 op = va_arg(args,
object *);
1448 op = va_arg(args,
object *);
1464 op = va_arg(args,
object *);
1465 buf = va_arg(args,
char *);
1469 op = va_arg(args,
object *);
1517 va_start(args, type);
1522 buf = va_arg(args,
char *);
1525 context->
fix = va_arg(args,
int);
1526 event = va_arg(args,
object *);
static PyObject * log_message(PyObject *self, PyObject *args)
CF_PLUGIN int cfpython_runPluginCommand(object *op, char *params)
static PyObject * getSeasonName(PyObject *self, PyObject *args)
CFPContext * current_context
static PyObject * private_data
PyObject * Crossfire_Party_wrap(partylist *what)
object * cf_create_object(void)
void cf_get_time(timeofday_t *tod)
static PyObject * findAnimation(PyObject *self, PyObject *args)
void cf_free_string(sstring str)
const char * cf_get_month_name(int index)
PyObject * Crossfire_Region_wrap(region *what)
PyObject * Crossfire_Map_wrap(mapstruct *what)
static void addConstants(PyObject *module, const char *name, const CFConstant *constants)
static void initContextStack(void)
static PyObject * getPlayers(PyObject *self, PyObject *args)
static void initConstants(PyObject *module)
mapstruct * cf_get_empty_map(int sizex, int sizey)
static PyObject * findPlayer(PyObject *self, PyObject *args)
static PyObject * createCFObjectByName(PyObject *self, PyObject *args)
static PyObject * registerGEvent(PyObject *self, PyObject *args)
PyTypeObject Crossfire_MapType
static PyObject * getSharedDictionary(PyObject *self, PyObject *args)
CF_PLUGIN int postInitPlugin(void)
static PyObject * getPeriodofdayName(PyObject *self, PyObject *args)
partylist * cf_party_get_first(void)
region * cf_region_get_first(void)
static PyObject * readyMap(PyObject *self, PyObject *args)
void *(* f_plug_api)(int *type,...)
static PyObject * CFPythonError
mapstruct * cf_map_get_map_property(mapstruct *map, int propcode)
void init_map_assoc_table(void)
archetype * cf_archetype_get_next(archetype *arch)
int cf_timer_destroy(int id)
static PyObject * getMaps(PyObject *self, PyObject *args)
region * cf_region_get_next(region *reg)
static PyObject * getWhoAmI(PyObject *self, PyObject *args)
static PyObject * createCFObject(PyObject *self, PyObject *args)
PyTypeObject Crossfire_PlayerType
player * cf_player_find(const char *plname)
void cf_system_register_global_event(int event, const char *name, f_plug_api hook)
static int do_script(CFPContext *context, int silent)
#define CFAPI_MAP_PROP_NEXT
sstring cf_add_string(const char *str)
static PyMethodDef CFPythonMethods[]
static pycode_cache_entry pycode_cache[PYTHON_CACHE_SIZE]
static PyObject * getPrivateDictionary(PyObject *self, PyObject *args)
const char * cf_get_weekday_name(int index)
int cf_find_animation(const char *txt)
partylist * cf_party_get_next(partylist *party)
void cf_log(LogLevel logLevel, const char *format,...)
static PyObject * findFace(PyObject *self, PyObject *args)
const char * cf_get_season_name(int index)
static PyObject * getDataDirectory(PyObject *self, PyObject *args)
char * cf_strdup_local(const char *str)
object * cf_friendlylist_get_first(void)
static void set_exception(const char *fmt,...)
static PyObject * destroyTimer(PyObject *self, PyObject *args)
static PyObject * getScriptParameters(PyObject *self, PyObject *args)
#define ATNR_CANCELLATION
int cf_init_plugin(f_plug_api getHooks)
CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr)
static void addSimpleConstants(PyObject *module, const char *name, const CFConstant *constants)
static PyObject * getCFPythonVersion(PyObject *self, PyObject *args)
static PyObject * getLocalDirectory(PyObject *self, PyObject *args)
static PyObject * getConfigDirectory(PyObject *self, PyObject *args)
void Handle_Map_Unload_Hook(Crossfire_Map *map)
PyMODINIT_FUNC initcjson(void)
#define ATNR_LIFE_STEALING
static PyObject * getEvent(PyObject *self, PyObject *args)
static PyObject * getReturnValue(PyObject *self, PyObject *args)
mapstruct * cf_map_get_map(const char *name, int flags)
static PyObject * createMap(PyObject *self, PyObject *args)
static PyObject * getArchetypes(PyObject *self, PyObject *args)
static PythonCmd CustomCommand[NR_CUSTOM_CMD]
CF_PLUGIN void * getPluginProperty(int *type,...)
static int current_command
PyObject * Crossfire_Object_wrap(object *what)
static PyObject * getTime(PyObject *self, PyObject *args)
CF_PLUGIN int closePlugin(void)
void cf_system_unregister_global_event(int event, const char *name)
static PyObject * getParties(PyObject *self, PyObject *args)
char * cf_get_maps_directory(const char *name, char *buf, int size)
static PyObject * getWeekdayName(PyObject *self, PyObject *args)
CF_PLUGIN void * eventListener(int *type,...)
object * cf_friendlylist_get_next(object *ob)
PyTypeObject Crossfire_PartyType
static PyObject * getRegions(PyObject *self, PyObject *args)
int cf_find_face(const char *name, int error)
static PyObject * matchString(PyObject *self, PyObject *args)
static PyObject * shared_data
#define ATNR_COUNTERSPELL
static const flag_definition flags[]
const char * cf_re_cmp(const char *str, const char *regexp)
object * cf_object_get_object_property(object *op, int propcode)
#define CFAPI_PLAYER_PROP_NEXT
archetype * cf_archetype_get_first(void)
int snprintf(char *dest, int max, const char *format,...)
void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize)
static PyObject * getWhoIsThird(PyObject *self, PyObject *args)
#define PYTHON_CACHE_SIZE
static PyCodeObject * compilePython(char *filename)
static PyObject * getMapHasBeenLoaded(PyObject *self, PyObject *args)
static PyObject * registerCommand(PyObject *self, PyObject *args)
PyTypeObject Crossfire_ArchetypeType
mapstruct * cf_map_has_been_loaded(const char *name)
static CFPContext * popContext(void)
const char * cf_get_directory(int id)
mapstruct * cf_map_get_first(void)
static PyObject * getTempDirectory(PyObject *self, PyObject *args)
PyTypeObject Crossfire_ObjectType
void init_object_assoc_table(void)
static void pushContext(CFPContext *context)
struct _cfpcontext * down
static PyObject * getWhatIsMessage(PyObject *self, PyObject *args)
static PyObject * getWhoIsActivator(PyObject *self, PyObject *args)
#define EVENT_PLAYER_DEATH
PyObject * Crossfire_Archetype_wrap(archetype *what)
static PyObject * getMonthName(PyObject *self, PyObject *args)
static PyObject * getPlayerDirectory(PyObject *self, PyObject *args)
static PyObject * setReturnValue(PyObject *self, PyObject *args)
const char * cf_get_periodofday_name(int index)
static void freeContext(CFPContext *context)
PyTypeObject Crossfire_RegionType
static PyObject * unregisterGEvent(PyObject *self, PyObject *args)
object * cf_create_object_by_name(const char *name)
CFPContext * context_stack
static PyObject * getFriendlyList(PyObject *self, PyObject *args)
static PyObject * getMapDirectory(PyObject *self, PyObject *args)
static PyObject * getUniqueDirectory(PyObject *self, PyObject *args)
CF_PLUGIN void * cfpython_globalEventListener(int *type,...)
static PyObject * getScriptName(PyObject *self, PyObject *args)