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