Go to the documentation of this file.
72 #define PYTHON_CACHE_SIZE 256
84 #define MAX_COMMANDS 1024
98 vsnprintf(
buf,
sizeof(
buf), fmt, arg);
101 PyErr_SetString(PyExc_ValueError,
buf);
120 if (!PyArg_ParseTuple(
args,
"i", &eventcode))
133 if (!PyArg_ParseTuple(
args,
"i", &eventcode))
157 if (!PyArg_ParseTuple(
args,
"s", &obname))
170 return Py_BuildValue(
"i", i);
183 if (!PyArg_ParseTuple(
args,
"i", &i))
196 if (!PyArg_ParseTuple(
args,
"ss", &premiere, &seconde))
201 return Py_BuildValue(
"i", 1);
203 return Py_BuildValue(
"i", 0);
211 if (!PyArg_ParseTuple(
args,
"s", &txt))
243 if (!PyArg_ParseTuple(
args,
"ii", &sizex, &sizey))
330 return Py_BuildValue(
"");
390 list = PyList_New(0);
405 list = PyList_New(0);
420 list = PyList_New(0);
435 list = PyList_New(0);
450 list = PyList_New(0);
465 list = PyList_New(0);
485 context->
third = NULL;
487 context->
event = NULL;
511 if (!PyArg_ParseTuple(
args,
"ssd|i", &cmdname, &scriptname, &cmdspeed, &
type))
536 set_exception(
"failed to register command (overriding an existing one with a different type?)");
552 list = PyList_New(0);
553 PyList_Append(
list, Py_BuildValue(
"i", tod.
year));
554 PyList_Append(
list, Py_BuildValue(
"i", tod.
month));
555 PyList_Append(
list, Py_BuildValue(
"i", tod.
day));
556 PyList_Append(
list, Py_BuildValue(
"i", tod.
hour));
557 PyList_Append(
list, Py_BuildValue(
"i", tod.
minute));
560 PyList_Append(
list, Py_BuildValue(
"i", tod.
season));
570 if (!PyArg_ParseTuple(
args,
"i", &
id))
579 if (!PyArg_ParseTuple(
args,
"s", &
name))
588 if (!PyArg_ParseTuple(
args,
"s", &
name))
599 if (!PyArg_ParseTuple(
args,
"is", &intLevel, &
message))
634 if (!PyArg_ParseTuple(
args,
"s", &
name))
643 if (!PyArg_ParseTuple(
args,
"i", &i))
652 if (!PyArg_ParseTuple(
args,
"i", &i))
661 if (!PyArg_ParseTuple(
args,
"i", &i))
670 if (!PyArg_ParseTuple(
args,
"i", &i))
686 if (!PyArg_ParseTuple(
args,
"ss", &word, &
reply)) {
745 cf_log(
llevError,
"CFPython: warning, too long message in npcSay, will be truncated");
759 int largest_coin = 0;
762 if (!PyArg_ParseTuple(
args,
"L|i", &
value, &largest_coin))
766 return Py_BuildValue(
"s",
buf);
770 {
"WhoAmI",
getWhoAmI, METH_NOARGS, NULL },
776 {
"WhatIsEvent",
getEvent, METH_NOARGS, NULL },
784 {
"ReadyMap",
readyMap, METH_VARARGS, NULL },
785 {
"CreateMap",
createMap, METH_VARARGS, NULL },
786 {
"FindPlayer",
findPlayer, METH_VARARGS, NULL },
787 {
"MatchString",
matchString, METH_VARARGS, NULL },
795 {
"GetPlayers",
getPlayers, METH_NOARGS, NULL },
797 {
"GetMaps",
getMaps, METH_NOARGS, NULL },
798 {
"GetParties",
getParties, METH_NOARGS, NULL },
799 {
"GetRegions",
getRegions, METH_NOARGS, NULL },
804 {
"GetTime",
getTime, METH_NOARGS, NULL },
808 {
"FindFace",
findFace, METH_VARARGS, NULL },
814 {
"AddReply",
addReply, METH_VARARGS, NULL },
816 {
"NPCSay",
npcSay, METH_VARARGS, NULL },
818 { NULL, NULL, 0, NULL }
829 context->
down = NULL;
849 Py_XDECREF(context->
event);
850 Py_XDECREF(context->
third);
851 Py_XDECREF(context->
who);
860 PyObject *scriptfile;
865 scriptfile = PyFile_FromFd(fd,
filename,
"r", -1, NULL, NULL, NULL, 1);
874 return fdopen(PyObject_AsFileDescriptor(
obj),
"r");
888 static PyObject *io_module = NULL;
902 PyObject *output = PyObject_GetAttrString(
catcher,
"value");
903 PyObject* empty = PyUnicode_FromString(
"");
908 PyObject_SetAttrString(
catcher,
"value", empty);
918 PyObject *scriptfile = NULL;
920 struct stat stat_buf;
964 if (
replace->file != sh_path) {
982 io_module = PyImport_ImportModule(
"io");
983 scriptfile = PyObject_CallMethod(io_module,
"open",
"ss",
filename,
"rb");
989 PyObject *source_bytes = PyObject_CallMethod(scriptfile,
"read",
"");
990 (
void)PyObject_CallMethod(scriptfile,
"close",
"");
991 PyObject *code = Py_CompileString(PyBytes_AsString(source_bytes),
filename, Py_file_input);
993 replace->code = (PyCodeObject *)code;
995 if (PyErr_Occurred())
998 replace->cached_time = stat_buf.st_mtime;
1012 if ((n = PyParser_SimpleParseFile(pyfile,
filename, Py_file_input))) {
1016 if (PyErr_Occurred())
1019 replace->cached_time = stat_buf.st_mtime;
1028 Py_DECREF(scriptfile);
1031 assert(run != NULL);
1032 run->used_time = time(NULL);
1037 PyCodeObject *pycode;
1046 dict = PyDict_New();
1047 PyDict_SetItemString(
dict,
"__builtins__", PyEval_GetBuiltins());
1048 ret = PyEval_EvalCode((PyObject *)pycode,
dict, NULL);
1049 if (PyErr_Occurred()) {
1072 snprintf(
tmp,
sizeof(
tmp),
"Crossfire_%s",
name);
1074 new = PyModule_New(
tmp);
1075 dict = PyDict_New();
1077 while (constants[i].
name != NULL) {
1078 PyModule_AddIntConstant(
new, (
char *)constants[i].
name, constants[i].
value);
1079 PyDict_SetItem(
dict, PyLong_FromLong(constants[i].
value), PyUnicode_FromString(constants[i].
name));
1082 PyDict_SetItemString(PyModule_GetDict(module),
name,
new);
1085 PyDict_SetItemString(PyModule_GetDict(module),
tmp,
dict);
1103 snprintf(
tmp,
sizeof(
tmp),
"Crossfire_%s",
name);
1105 new = PyModule_New(
tmp);
1107 while (constants[i].
name != NULL) {
1108 PyModule_AddIntConstant(
new, (
char *)constants[i].
name, constants[i].
value);
1111 PyDict_SetItemString(PyModule_GetDict(module),
name,
new);
1435 PyObject *d = PyModule_GetDict(
m);
1464 PyModule_AddObject(
m,
"LogError", Py_BuildValue(
"i",
llevError));
1465 PyModule_AddObject(
m,
"LogInfo", Py_BuildValue(
"i",
llevInfo));
1466 PyModule_AddObject(
m,
"LogDebug", Py_BuildValue(
"i",
llevDebug));
1467 PyModule_AddObject(
m,
"LogMonster", Py_BuildValue(
"i",
llevMonster));
1469 CFPythonError = PyErr_NewException(
"Crossfire.error", NULL, NULL);
1476 PyModuleDef_HEAD_INIT,
1497 const char *stdOutErr =
1499 class CatchOutErr:\n\
1500 def __init__(self):\n\
1502 def write(self, txt):\n\
1503 self.value += txt\n\
1504 catchOutErr = CatchOutErr()\n\
1505 sys.stdout = catchOutErr\n\
1506 sys.stderr = catchOutErr\n\
1525 m = PyImport_ImportModule(
"Crossfire");
1535 m = PyImport_AddModule(
"__main__");
1536 PyRun_SimpleString(stdOutErr);
1537 catcher = PyObject_GetAttrString(
m,
"catchOutErr");
1543 const char *propname;
1548 propname = va_arg(
args,
const char *);
1549 if (!strcmp(propname,
"Identification")) {
1551 size = va_arg(
args,
int);
1555 }
else if (!strcmp(propname,
"FullName")) {
1557 size = va_arg(
args,
int);
1615 for (
int e = 0; eventFiles[e] != NULL; e++) {
1616 free(eventFiles[e]);
1627 char **eventFiles = NULL;
1630 int allocated = 0,
current = 0;
1641 eventFiles = calloc(1,
sizeof(eventFiles[0]));
1642 eventFiles[0] = NULL;
1646 while ((d =
readdir(dp)) != NULL) {
1649 if (S_ISDIR(sb.st_mode)) {
1652 if (strcmp(d->d_name + strlen(d->d_name) - 3,
".py")) {
1658 eventFiles = realloc(eventFiles,
sizeof(
char *) * (allocated + 1));
1659 for (
int i =
current; i < allocated + 1; i++) {
1660 eventFiles[i] = NULL;
1671 PyObject *scriptfile;
1677 for (i = 0;
GECodes[i] != 0; i++)
1681 if (scriptfile != NULL) {
1684 Py_DECREF(scriptfile);
1698 for (
int i = 0;
GECodes[i] != 0; i++) {
1720 context->
who = NULL;
1722 context->
third = NULL;
1723 context->
event = NULL;
1724 context->
talk = NULL;
1732 op = va_arg(
args,
object *);
1737 op = va_arg(
args,
object *);
1739 op = va_arg(
args,
object *);
1744 op = va_arg(
args,
object *);
1767 op = va_arg(
args,
object *);
1772 op = va_arg(
args,
object *);
1780 op = va_arg(
args,
object *);
1788 op = va_arg(
args,
object *);
1796 op = va_arg(
args,
object *);
1802 op = va_arg(
args,
object *);
1815 op = va_arg(
args,
object *);
1820 op = va_arg(
args,
object *);
1847 (*copy) = (*context);
1849 Py_XINCREF(copy->
event);
1850 Py_XINCREF(copy->
third);
1851 Py_XINCREF(copy->
who);
1896 event = va_arg(
args,
object *);
void cf_cost_string_from_value(uint64_t cost, int largest_coin, char *buffer, int length)
static PyCodeObject * compilePython(char *filename)
static PyObject * getPlayerDirectory(PyObject *self, PyObject *args)
static PyModuleDef CrossfireModule
static PyObject * findAnimation(PyObject *self, PyObject *args)
static PyObject * getWhatIsMessage(PyObject *self, PyObject *args)
void cf_log(LogLevel logLevel, const char *format,...)
#define ATNR_CANCELLATION
const char * cf_get_weekday_name(int index)
sstring cf_add_string(const char *str)
sstring replies[MAX_REPLIES]
mapstruct * cf_map_get_first(void)
static PyObject * getFriendlyList(PyObject *self, PyObject *args)
PyObject * Crossfire_Map_wrap(mapstruct *what)
const char * cf_get_directory(int id)
const CFConstant cstAttackType[]
CF_PLUGIN int eventListener(int *type,...)
static int do_script(CFPContext *context)
const char * cf_get_season_name(int index)
static PyObject * getMaps(PyObject *self, PyObject *args)
sstring replies_words[MAX_REPLIES]
CF_PLUGIN int closePlugin(void)
static PyObject * findFace(PyObject *self, PyObject *args)
archetype * cf_archetype_get_first(void)
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
PyObject * Crossfire_Region_wrap(region *what)
object * cf_object_get_object_property(object *op, int propcode)
#define CFAPI_MAP_PROP_NEXT
static PyObject * getWhoAmI(PyObject *self, PyObject *args)
static event_registration c
static PyObject * getMonthName(PyObject *self, PyObject *args)
static PyObject * getWeekdayName(PyObject *self, PyObject *args)
object * cf_create_object_by_name(const char *name)
static PyObject * addReply(PyObject *self, PyObject *args)
PyObject * Crossfire_Object_wrap(object *what)
CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr)
int cf_timer_destroy(int id)
CFPContext * current_context
static PyObject * getCFPythonVersion(PyObject *self, PyObject *args)
static PyObject * getParties(PyObject *self, PyObject *args)
static PyObject * getScriptName(PyObject *self, PyObject *args)
region * cf_region_get_next(region *reg)
static command_registration registered_commands[MAX_COMMANDS]
static void freeEventFiles(char **eventFiles)
#define COMMAND_TYPE_NORMAL
static PyObject * setPlayerMessage(PyObject *self, PyObject *args)
static const flag_definition flags[]
partylist * cf_party_get_next(partylist *party)
void Handle_Map_Unload_Hook(Crossfire_Map *map)
static PyObject * PyInit_Crossfire(void)
static PyObject * createCFObject(PyObject *self, PyObject *args)
PyTypeObject Crossfire_PlayerType
static PyObject * getMapHasBeenLoaded(PyObject *self, PyObject *args)
static event_registration m
static PyObject * costStringFromValue(PyObject *self, PyObject *args)
static PyObject * getEvent(PyObject *self, PyObject *args)
static PyObject * unregisterGEvent(PyObject *self, PyObject *args)
DIR * opendir(const char *)
static PyObject * getConfigDirectory(PyObject *self, PyObject *args)
CF_PLUGIN void * getPluginProperty(int *type,...)
static void initContextStack(void)
void cf_log_plain(LogLevel logLevel, const char *message)
partylist * cf_party_get_first(void)
static PyObject * shared_data
const CFConstant cstEventType[]
static PyObject * matchString(PyObject *self, PyObject *args)
void init_map_assoc_table(void)
static void pushContext(CFPContext *context)
struct _cfpcontext * down
int cf_find_face(const char *name, int error)
void(* f_plug_api)(int *type,...)
static PyObject * createCFObjectByName(PyObject *self, PyObject *args)
static void freeContext(CFPContext *context)
void cf_system_unregister_global_event(int event, const char *name)
const char * cf_get_month_name(int index)
int cf_init_plugin(f_plug_api getHooks)
struct dirent * readdir(DIR *)
static void python_command_function(object *op, const char *params, const char *script)
mapstruct * cf_get_empty_map(int sizex, int sizey)
region * cf_region_get_first(void)
const CFConstant cstTime[]
static PyObject * getWhoIsThird(PyObject *self, PyObject *args)
static void cfpython_init_types(PyObject *m)
static PyObject * getRegions(PyObject *self, PyObject *args)
static PyObject * readyMap(PyObject *self, PyObject *args)
const CFConstant cstMove[]
static PyObject * cfpython_openpyfile(char *filename)
const typedef char * sstring
#define ATNR_COUNTERSPELL
static void set_exception(const char *fmt,...)
CF_PLUGIN int postInitPlugin(void)
CF_PLUGIN char SvnRevPlugin[]
mapstruct * cf_map_get_map_property(mapstruct *map, int propcode)
static PyObject * getLocalDirectory(PyObject *self, PyObject *args)
const CFConstant cstType[]
void cf_free_string(sstring str)
static void log_python_error(void)
#define COMMAND_TYPE_WIZARD
const CFConstant cstMessageFlag[]
static PyObject * getReturnValue(PyObject *self, PyObject *args)
#define EVENT_PLAYER_DEATH
command_registration cf_system_register_command_extra(const char *name, const char *extra, command_function_extra func, uint8_t command_type, float time)
CF_PLUGIN int cfpython_globalEventListener(int *type,...)
static PyObject * createMap(PyObject *self, PyObject *args)
void cf_system_register_global_event(int event, const char *name, f_plug_event hook)
static PyObject * log_message(PyObject *self, PyObject *args)
static const char * GEPaths[]
static void addSimpleConstants(PyObject *module, const char *name, const CFConstant *constants)
int cf_find_animation(const char *txt)
static PyObject * registerGEvent(PyObject *self, PyObject *args)
static PyObject * CFPythonError
static PyObject * getSharedDictionary(PyObject *self, PyObject *args)
PyTypeObject Crossfire_PartyType
static PyObject * getSeasonName(PyObject *self, PyObject *args)
archetype * cf_archetype_get_next(archetype *arch)
const char * cf_get_periodofday_name(int index)
static FILE * cfpython_pyfile_asfile(PyObject *obj)
static CFPContext * popContext(void)
static PyObject * getArchetypes(PyObject *self, PyObject *args)
PyObject * Crossfire_Archetype_wrap(archetype *what)
static PyObject * npcSay(PyObject *self, PyObject *args)
static PyObject * getWhoIsActivator(PyObject *self, PyObject *args)
PyTypeObject Crossfire_ArchetypeType
mapstruct * cf_map_has_been_loaded(const char *name)
const CFConstant cstAttackTypeNumber[]
static PyObject * getPlayers(PyObject *self, PyObject *args)
const char * cf_re_cmp(const char *str, const char *regexp)
static PyObject * destroyTimer(PyObject *self, PyObject *args)
static void initConstants(PyObject *module)
PyObject * Crossfire_Party_wrap(partylist *what)
static PyObject * catcher
CFPContext * context_stack
object * cf_create_object(void)
char * cf_get_maps_directory(const char *name, char *buf, int size)
const CFConstant cstAttackMovement[]
static PyObject * registerCommand(PyObject *self, PyObject *args)
static const char * getGlobalEventPath(int code)
sstring npc_msgs[MAX_NPC]
PyTypeObject Crossfire_RegionType
player * cf_player_find(const char *plname)
const CFConstant cstDirection[]
void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize)
object * cf_friendlylist_get_next(object *ob)
static PyObject * getScriptParameters(PyObject *self, PyObject *args)
static char ** getEventFiles(CFPContext *context)
void cf_system_unregister_command(command_registration command)
static PyObject * findPlayer(PyObject *self, PyObject *args)
static pycode_cache_entry pycode_cache[PYTHON_CACHE_SIZE]
#define PYTHON_CACHE_SIZE
event
DIALOGCHECK MINARGS 1 MAXARGS 2
static PyObject * getPrivateDictionary(PyObject *self, PyObject *args)
uint64_t command_registration
mapstruct * cf_map_get_map(const char *name, int flags)
PyObject * PyInit_cjson(void)
static PyObject * setReturnValue(PyObject *self, PyObject *args)
static PyObject * getTempDirectory(PyObject *self, PyObject *args)
PyMethodDef CFPythonMethods[]
static PyObject * getMapDirectory(PyObject *self, PyObject *args)
object * cf_friendlylist_get_first(void)
static void addConstants(PyObject *module, const char *name, const CFConstant *constants)
const CFConstant cstReplyTypes[]
static PyObject * getDataDirectory(PyObject *self, PyObject *args)
#define ATNR_LIFE_STEALING
static PyObject * private_data
PyTypeObject Crossfire_ObjectType
static PyObject * getPeriodofdayName(PyObject *self, PyObject *args)
void init_object_assoc_table(void)
static PyObject * getTime(PyObject *self, PyObject *args)
static PyObject * getUniqueDirectory(PyObject *self, PyObject *args)
#define CFAPI_PLAYER_PROP_NEXT
void cf_get_time(timeofday_t *tod)
PyTypeObject Crossfire_MapType