Crossfire Server, Trunk
pyi-generate.cpp
Go to the documentation of this file.
1 #include <fstream>
2 
3 extern "C" {
4 #include "include/cfpython.h"
5 }
6 
8  const char *method;
9  const char *parameters;
10  const char *type;
11  const char *description;
12 };
13 
15  const char *property;
16  const char *type;
17  const char *description;
18 };
19 
20 static methodSignature unknown = { nullptr, nullptr, "any", nullptr };
21 static propertySignature unknownProperty = { nullptr, "any", nullptr };
22 
23 static const methodSignature moduleMethods[] = {
24  { "FindPlayer", "name: str", "Player", "Find the specified player from its name.\n:param name Player's name, case-sensitive.\n:return Player, None if no player matches." },
25  { nullptr, nullptr, nullptr, nullptr },
26 };
27 
28 static const methodSignature objectMethods[] = {
29  { "WriteKey", "key: str, value: str, add_key: int=0", "bool", "Add a key with the specified value to the object.\n:param key Key name.\n:param value Value to give the key.\n:param add_key If 1 then the key is set, if 0 then the key is only updated if it exists.\n:return True if the key was added or updated, False else." },
30  { nullptr, nullptr, nullptr, nullptr },
31 };
33  { nullptr, nullptr, nullptr },
34 };
35 
36 static const methodSignature playerMethods[] = {
37  { nullptr, nullptr, nullptr, nullptr },
38 };
40  { "MarkedItem", "Object", "Marked item, used by some actions." },
41  { nullptr, nullptr, nullptr },
42 };
43 
44 static const methodSignature regionMethods[] = {
45  { nullptr, nullptr, nullptr, nullptr },
46 };
48  { nullptr, nullptr, nullptr },
49 };
50 
51 static const methodSignature mapMethods[] = {
52  { nullptr, nullptr, nullptr, nullptr },
53 };
54 static const propertySignature mapProperties[] = {
55  { nullptr, nullptr, nullptr },
56 };
57 
58 static const methodSignature partyMethods[] = {
59  { nullptr, nullptr, nullptr, nullptr },
60 };
62  { nullptr, nullptr, nullptr },
63 };
64 
65 static const methodSignature archMethods[] = {
66  { nullptr, nullptr, nullptr, nullptr },
67 };
69  { nullptr, nullptr, nullptr },
70 };
71 
72 const methodSignature *getSignature(const methodSignature *signatures, const char *method) {
73  for (size_t s = 0; signatures[s].method; s++) {
74  if (strcmp(method, signatures[s].method) == 0) {
75  return &signatures[s];
76  }
77  }
78  return &unknown;
79 }
80 
81 void writeDescriptionAndBody(std::ofstream &pyi, const char *indent, const char *description) {
82  if (description) {
83  pyi << std::endl << indent << "\t\"\"\"" << std::endl << description << std::endl << indent << "\t\"\"\"" << std::endl << indent << "..." << std::endl;
84  } else {
85  pyi << " ..." << std::endl;
86  }
87 }
88 
89 void writeMethods(std::ofstream &pyi, const PyMethodDef method[], const methodSignature *signatures, bool inClass) {
90  const char *indent = inClass ? "\t" : "";
91  for (size_t m = 0; method[m].ml_name != nullptr; ++m) {
92  auto signature = getSignature(signatures, method[m].ml_name);
93  pyi << indent << "def " << method[m].ml_name << "(";
94  if (inClass) {
95  pyi << "self" << (signature->parameters ? ", " : "");
96  };
97  if (signature->parameters) {
98  pyi << signature->parameters;
99  }
100  pyi << ") -> " << signature->type << ":";
101  writeDescriptionAndBody(pyi, indent, signature->description);
102  pyi << std::endl;
103  }
104 }
105 
106 const propertySignature *getSignature(const propertySignature *properties, const char *property) {
107  for (size_t s = 0; properties[s].property; s++) {
108  if (strcmp(property, properties[s].property) == 0) {
109  return &properties[s];
110  }
111  }
112  return &unknownProperty;
113 }
114 
115 void writeClass(std::ofstream &pyi, const char *name, const PyTypeObject *obj, const methodSignature *methods, const propertySignature *properties, const char *base = nullptr) {
116  pyi << "class " << name;
117  if (base) {
118  pyi << "(" << base << ")";
119  }
120  pyi << ":" << std::endl << std::endl;
121  auto property = obj->tp_getset;
122  while (property->name) {
123  auto signature = getSignature(properties, property->name);
124  pyi << "\t@property" << std::endl;
125  pyi << "\tdef " << property->name << "(self) -> " << signature->type << ":";
126  writeDescriptionAndBody(pyi, "\t", signature->description);
127  pyi << std::endl;
128  if (property->set) {
129  pyi << "\t@" << property->name << ".setter" << std::endl;
130  pyi << "\tdef " << property->name << "(self, value: " << signature->type << ") -> None:";
131  writeDescriptionAndBody(pyi, "\t", signature->description);
132  pyi << std::endl;
133  }
134  ++property;
135  }
136 
137  writeMethods(pyi, obj->tp_methods, methods, true);
138 }
139 
140 void writeEnum(std::ofstream &pyi, const char *name, const CFConstant *constants) {
141  pyi << "class " << name << "(enum):" << std::endl;
142  for (size_t c = 0; constants[c].name != nullptr; c++) {
143  pyi << "\t" << constants[c].name << " = " << constants[c].value << std::endl;
144  }
145  pyi << std::endl;
146 }
147 
148 void writeConstants(std::ofstream &pyi, const CFConstant *constants) {
149  for (size_t c = 0; constants[c].name != nullptr; c++) {
150  pyi << constants[c].name << " = " << constants[c].value << std::endl;
151  }
152  pyi << std::endl;
153 }
154 
155 int main(int, char **) {
156  std::ofstream pyi("Crossfire.pyi");
157 
158  pyi << "\"\"\"" << std::endl << "Crossfire stubs. Auto-generated file, don't edit." << std::endl << "\"\"\"" << std::endl << std::endl;
159 
162  writeClass(pyi, "Player", &Crossfire_PlayerType, playerMethods, playerProperties, "Object");
167 
168  writeEnum(pyi, "Direction", cstDirection);
169  writeEnum(pyi, "Type", cstType);
170  writeEnum(pyi, "Move", cstMove);
171  writeEnum(pyi, "MessageFlag", cstMessageFlag);
172  writeEnum(pyi, "AttackType", cstAttackType);
173  writeEnum(pyi, "AttackTypeNumber", cstAttackTypeNumber);
174  writeEnum(pyi, "EventType", cstEventType);
175  writeConstants(pyi, cstTime);
178 
179  pyi << "LogError = 1\nLogInfo = 2\nLogDebug = 3\nLogMonster = 4" << std::endl;
180 
181  return 0;
182 }
cstAttackType
const CFConstant cstAttackType[]
Definition: cfpython.c:1273
playerMethods
static const methodSignature playerMethods[]
Definition: pyi-generate.cpp:36
objectProperties
static const propertySignature objectProperties[]
Definition: pyi-generate.cpp:32
archProperties
static const propertySignature archProperties[]
Definition: pyi-generate.cpp:68
mapProperties
static const propertySignature mapProperties[]
Definition: pyi-generate.cpp:54
c
static event_registration c
Definition: citylife.cpp:427
unknown
static methodSignature unknown
Definition: pyi-generate.cpp:20
propertySignature
Definition: pyi-generate.cpp:14
CFConstant::value
const int value
Definition: cfpython.h:107
regionProperties
static const propertySignature regionProperties[]
Definition: pyi-generate.cpp:47
mapMethods
static const methodSignature mapMethods[]
Definition: pyi-generate.cpp:51
obj
Definition: object.h:277
CFConstant::name
const char * name
Definition: cfpython.h:106
Crossfire_PlayerType
PyTypeObject Crossfire_PlayerType
m
static event_registration m
Definition: citylife.cpp:427
propertySignature::property
const char * property
Definition: pyi-generate.cpp:15
cstEventType
const CFConstant cstEventType[]
Definition: cfpython.c:1333
cstTime
const CFConstant cstTime[]
Definition: cfpython.c:1375
cfpython.h
writeMethods
void writeMethods(std::ofstream &pyi, const PyMethodDef method[], const methodSignature *signatures, bool inClass)
Definition: pyi-generate.cpp:89
methodSignature::type
const char * type
Definition: pyi-generate.cpp:10
propertySignature::description
const char * description
Definition: pyi-generate.cpp:17
cstMove
const CFConstant cstMove[]
Definition: cfpython.c:1242
main
int main(int, char **)
Definition: pyi-generate.cpp:155
archMethods
static const methodSignature archMethods[]
Definition: pyi-generate.cpp:65
unknownProperty
static propertySignature unknownProperty
Definition: pyi-generate.cpp:21
cstType
const CFConstant cstType[]
Definition: cfpython.c:1126
cstMessageFlag
const CFConstant cstMessageFlag[]
Definition: cfpython.c:1253
methodSignature
Definition: pyi-generate.cpp:7
regionMethods
static const methodSignature regionMethods[]
Definition: pyi-generate.cpp:44
partyProperties
static const propertySignature partyProperties[]
Definition: pyi-generate.cpp:61
Crossfire_PartyType
PyTypeObject Crossfire_PartyType
writeClass
void writeClass(std::ofstream &pyi, const char *name, const PyTypeObject *obj, const methodSignature *methods, const propertySignature *properties, const char *base=nullptr)
Definition: pyi-generate.cpp:115
CFConstant
Definition: cfpython.h:105
Crossfire_ArchetypeType
PyTypeObject Crossfire_ArchetypeType
cstAttackTypeNumber
const CFConstant cstAttackTypeNumber[]
Definition: cfpython.c:1303
cstAttackMovement
const CFConstant cstAttackMovement[]
Definition: cfpython.c:1392
writeEnum
void writeEnum(std::ofstream &pyi, const char *name, const CFConstant *constants)
Definition: pyi-generate.cpp:140
Crossfire_RegionType
PyTypeObject Crossfire_RegionType
methodSignature::parameters
const char * parameters
Definition: pyi-generate.cpp:9
cstDirection
const CFConstant cstDirection[]
Definition: cfpython.c:1114
methodSignature::method
const char * method
Definition: pyi-generate.cpp:8
objectMethods
static const methodSignature objectMethods[]
Definition: pyi-generate.cpp:28
writeDescriptionAndBody
void writeDescriptionAndBody(std::ofstream &pyi, const char *indent, const char *description)
Definition: pyi-generate.cpp:81
CFPythonMethods
PyMethodDef CFPythonMethods[]
Definition: cfpython.c:769
playerProperties
static const propertySignature playerProperties[]
Definition: pyi-generate.cpp:39
cstReplyTypes
const CFConstant cstReplyTypes[]
Definition: cfpython.c:1385
getSignature
const methodSignature * getSignature(const methodSignature *signatures, const char *method)
Definition: pyi-generate.cpp:72
partyMethods
static const methodSignature partyMethods[]
Definition: pyi-generate.cpp:58
propertySignature::type
const char * type
Definition: pyi-generate.cpp:16
moduleMethods
static const methodSignature moduleMethods[]
Definition: pyi-generate.cpp:23
Crossfire_ObjectType
PyTypeObject Crossfire_ObjectType
writeConstants
void writeConstants(std::ofstream &pyi, const CFConstant *constants)
Definition: pyi-generate.cpp:148
methodSignature::description
const char * description
Definition: pyi-generate.cpp:11
properties
const char * properties[PROPERTY_COUNT]
Definition: ArchetypesModel.cpp:21
give.name
name
Definition: give.py:27
Crossfire_MapType
PyTypeObject Crossfire_MapType