-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPythonSystem.cc
More file actions
283 lines (237 loc) · 7.92 KB
/
PythonSystem.cc
File metadata and controls
283 lines (237 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (C) 2013 Alec Thomas <alec@swapoff.org>
* All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* Author: Alec Thomas <alec@swapoff.org>
*/
// http://docs.python.org/2/extending/extending.html
#include <boost/python.hpp>
#include <boost/noncopyable.hpp>
#include <cassert>
#include <string>
#include <iostream>
#include <sstream>
#include "entityx/python/PythonSystem.h"
#include "entityx/python/config.h"
namespace py = boost::python;
namespace entityx {
namespace python {
static const py::object None;
class PythonEntityXLogger {
public:
PythonEntityXLogger() {}
explicit PythonEntityXLogger(PythonSystem::LoggerFunction logger) : logger_(logger) {}
~PythonEntityXLogger() { flush(true); }
void write(const std::string &text) {
line_ += text;
flush();
}
private:
void flush(bool force = false) {
size_t offset;
while ( (offset = line_.find('\n')) != std::string::npos ) {
std::string text = line_.substr(0, offset);
logger_(text);
line_ = line_.substr(offset + 1);
}
if ( force && line_.size() ) {
logger_(line_);
line_ = "";
}
}
PythonSystem::LoggerFunction logger_;
std::string line_;
};
/**
* Base class for Python entities.
*/
struct PythonEntity {
explicit PythonEntity(EntityManager* entity_manager, Entity::Id id) : _entity(Entity(entity_manager, id)) {} // NOLINT
virtual ~PythonEntity() {}
void destroy() {
_entity.destroy();
}
operator Entity () const { return _entity; }
virtual void update(float dt) {}
Entity::Id _entity_id() const {
return _entity.id();
}
Entity _entity;
};
static std::string PythonEntity_repr(const PythonEntity &entity) {
std::stringstream repr;
repr << "<Entity " << entity._entity.id().index() << "." << entity._entity.id().version() << ">";
return repr.str();
}
static std::string Entity_Id_repr(Entity::Id id) {
std::stringstream repr;
repr << "<Entity::Id " << id.index() << "." << id.version() << ">";
return repr.str();
}
// A to-Python converter from Entity to PythonEntity.
struct EntityToPythonEntity {
static PyObject *convert(Entity entity) {
auto python = entity.component<PythonScript>();
assert(python && "Entity does not have a PythonComponent");
return py::incref(python->object.ptr());
}
};
Entity::Id EntityManager_configure(EntityManager& entity_manager, py::object self) {
Entity entity = entity_manager.create();
entity.assign<PythonScript>(self);
return entity.id();
}
BOOST_PYTHON_MODULE(_entityx) {
py::to_python_converter<Entity, EntityToPythonEntity>();
py::class_<PythonEntityXLogger>("Logger", py::no_init)
.def("write", &PythonEntityXLogger::write);
py::class_<BaseEvent, boost::noncopyable>("BaseEvent", py::no_init);
py::class_<PythonEntity>("Entity", py::init<EntityManager*, Entity::Id>())
.def_readonly("_entity_id", &PythonEntity::_entity_id)
.def("update", &PythonEntity::update)
.def("destroy", &PythonEntity::destroy)
.def("__repr__", &PythonEntity_repr);
py::class_<Entity::Id>("EntityId", py::no_init)
.def_readonly("id", &Entity::Id::id)
.def_readonly("index", &Entity::Id::index)
.def_readonly("version", &Entity::Id::version)
.def("__repr__", &Entity_Id_repr);
py::class_<PythonScript>("PythonScript", py::init<py::object>())
.def("assign_to", &assign_to<PythonScript>)
.def("get_component", &get_component<PythonScript>,
py::return_value_policy<py::reference_existing_object>())
.staticmethod("get_component");
py::class_<EntityManager, boost::noncopyable>("EntityManager", py::no_init)
.def("configure", &EntityManager_configure);
void (EventManager::*emit)(const BaseEvent &) = &EventManager::emit;
py::class_<EventManager, boost::noncopyable>("EventManager", py::no_init)
.def("emit", emit);
py::implicitly_convertible<PythonEntity, Entity>();
}
static void log_to_stderr(const std::string &text) {
std::cerr << "python stderr: " << text << std::endl;
}
static void log_to_stdout(const std::string &text) {
std::cout << "python stdout: " << text << std::endl;
}
// PythonSystem below here
bool PythonSystem::initialized_ = false;
PythonSystem::PythonSystem(EntityManager& entity_manager)
: em_(entity_manager), stdout_(log_to_stdout), stderr_(log_to_stderr) {
if ( !initialized_ ) {
initialize_python_module();
}
Py_Initialize();
if ( !initialized_ ) {
init_entityx();
initialized_ = true;
}
}
PythonSystem::~PythonSystem() {
try {
py::object entityx = py::import("_entityx");
entityx.attr("_entity_manager").del();
entityx.attr("_event_manager").del();
py::object sys = py::import("sys");
sys.attr("stdout").del();
sys.attr("stderr").del();
py::object gc = py::import("gc");
gc.attr("collect")();
}
catch ( ... ) {
PyErr_Print();
PyErr_Clear();
throw;
}
// FIXME: It would be good to do this, but it is not supported by boost::python:
// http://www.boost.org/doc/libs/1_53_0/libs/python/todo.html#pyfinalize-safety
// Py_Finalize();
}
void PythonSystem::add_installed_library_path() {
add_path(ENTITYX_INSTALLED_PYTHON_PACKAGE_DIR);
}
void PythonSystem::add_path(const std::string &path) {
python_paths_.push_back(path);
}
void PythonSystem::initialize_python_module() {
assert(PyImport_AppendInittab("_entityx", init_entityx) != -1 &&
"Failed to initialize _entityx Python module");
}
void PythonSystem::configure(EventManager& ev) {
ev.subscribe<EntityDestroyedEvent>(*this);
ev.subscribe<ComponentAddedEvent<PythonScript>>(*this);
try {
py::object main_module = py::import("__main__");
py::object main_namespace = main_module.attr("__dict__");
// Initialize logging.
py::object sys = py::import("sys");
sys.attr("stdout") = PythonEntityXLogger(stdout_);
sys.attr("stderr") = PythonEntityXLogger(stderr_);
// Add paths to interpreter sys.path
for ( auto path : python_paths_ ) {
py::str dir = path.c_str();
sys.attr("path").attr("insert")(0, dir);
}
py::object entityx = py::import("_entityx");
entityx.attr("_entity_manager") = boost::ref<EntityManager>(em_);
entityx.attr("_event_manager") = boost::ref<EventManager>(ev);
}
catch ( ... ) {
PyErr_Print();
PyErr_Clear();
throw;
}
}
void PythonSystem::update(EntityManager & em,
EventManager & events, TimeDelta dt) {
em.each<PythonScript>(
[=](Entity entity, PythonScript& python) {
try {
// Access PythonEntity and call Update.
python.object.attr("update")(dt);
}
catch ( const py::error_already_set& ) {
PyErr_Print();
PyErr_Clear();
throw;
}
});
}
void PythonSystem::log_to(LoggerFunction sout, LoggerFunction serr) {
stdout_ = sout;
stderr_ = serr;
}
void PythonSystem::receive(const EntityDestroyedEvent &event) {
for ( auto proxy : event_proxies_ ) {
proxy->delete_receiver(event.entity);
}
}
void PythonSystem::receive(const ComponentAddedEvent<PythonScript> &event) {
// If the component was created in C++ it won't have a Python object
// associated with it. Create one.
if ( !event.component->object ) {
py::object module = py::import(event.component->module.c_str());
py::object cls = module.attr(event.component->cls.c_str());
py::object from_raw_entity = cls.attr("_from_raw_entity");
if ( py::len(event.component->args) == 0 ) {
ComponentHandle<PythonScript> p = event.component;
p->object = from_raw_entity(event.entity.id());
} else {
py::list args;
args.append(event.entity.id());
args.extend(event.component->args);
ComponentHandle<PythonScript> p = event.component;
p->object = from_raw_entity(*py::tuple(args));
}
}
for ( auto proxy : event_proxies_ ) {
if ( proxy->can_send(event.component->object) ) {
proxy->add_receiver(event.entity);
}
}
}
} // namespace python
} // namespace entityx