-
Notifications
You must be signed in to change notification settings - Fork 39
/
command_binding.cc
213 lines (190 loc) · 7.15 KB
/
command_binding.cc
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
// This file is part of SWGANH which is released under the MIT license.
// See file LICENSE or go to http://swganh.com/LICENSE
#include "command_binding.h"
#include "swganh/python_shared_ptr.h"
#include <boost/python.hpp>
#include "swganh/app/swganh_kernel.h"
#include "swganh_core/object/creature/creature.h"
#include "swganh_core/object/tangible/tangible.h"
#include "swganh/scripting/utilities.h"
#include "base_swg_command.h"
#include "command_interface.h"
namespace bp = boost::python;
using swganh::command::BaseSwgCommand;
using swganh::command::CommandCallback;
using swganh::command::CommandInterface;
using swganh::command::CommandProperties;
using swganh::scripting::ScopedGilLock;
struct BaseSwgCommandWrapper : BaseSwgCommand, bp::wrapper<BaseSwgCommand>
{
BaseSwgCommandWrapper(
PyObject* obj,
swganh::app::SwganhKernel* kernel,
const CommandProperties& properties)
: BaseSwgCommand(kernel, properties)
{
ScopedGilLock lock;
bp::detail::initialize_wrapper(obj, this);
}
bool Validate()
{
bool validated = false;
ScopedGilLock lock;
try
{
auto validate = this->get_override("validate");
if (validate)
{
validated = validate();
}
else
{
validated = this->BaseSwgCommand::Validate();
}
}
catch(bp::error_already_set& /*e*/)
{
PyErr_Print();
}
return validated;
}
boost::optional<std::shared_ptr<CommandCallback>> Run()
{
boost::optional<std::shared_ptr<CommandCallback>> callback;
ScopedGilLock lock;
try
{
bp::object result = this->get_override("run")();
if (!result.is_none())
{
CommandCallback* obj_pointer = bp::extract<CommandCallback*>(result);
callback.reset(std::shared_ptr<CommandCallback>(obj_pointer, [result] (CommandCallback*) {}));
}
}
catch(bp::error_already_set& /*e*/)
{
PyErr_Print();
}
return callback;
}
void SetCommandProperties(const CommandProperties& properties)
{
ScopedGilLock lock;
try
{
auto setup = this->get_override("setup");
if (setup)
{
setup();
}
else
{
this->BaseSwgCommand::SetCommandProperties(properties);
}
}
catch(bp::error_already_set& /*e*/)
{
PyErr_Print();
}
}
void PostRun(bool success)
{
ScopedGilLock lock;
try
{
auto postRun = this->get_override("postRun");
if (postRun)
{
postRun(success);
}
else
{
this->BaseSwgCommand::PostRun(success);
}
}
catch(bp::error_already_set& /*e*/)
{
PyErr_Print();
}
}
};
class CommandCallbackWrapper : public CommandCallback, bp::wrapper<CommandCallback>
{
public:
CommandCallbackWrapper(
PyObject* obj,
boost::python::object function,
uint64_t delay_timer)
: CommandCallback([function] () -> boost::optional<std::shared_ptr<CommandCallback>>
{
boost::optional<std::shared_ptr<CommandCallback>> callback;
{
ScopedGilLock lock;
try
{
bp::object result = function();
if (!result.is_none())
{
CommandCallback* obj_pointer = bp::extract<CommandCallback*>(result);
callback.reset(std::shared_ptr<CommandCallback>(obj_pointer, [result] (CommandCallback*) {}));
}
}
catch(bp::error_already_set& /*e*/)
{
PyErr_Print();
}
}
return callback;
}, delay_timer)
{
ScopedGilLock lock;
bp::detail::initialize_wrapper(obj, this);
}
};
void swganh::command::ExportCommand()
{
bp::class_<CommandCallback, std::shared_ptr<CommandCallbackWrapper>, boost::noncopyable>
("Callback", bp::init<bp::object, uint64_t>())
.def("getDelayTimeInMs", &CommandCallbackWrapper::GetDelayTimeInMs)
.def("execute", &CommandCallbackWrapper::operator())
;
bp::class_<CommandInterface, boost::noncopyable>("CommandInterface", bp::no_init)
.def("validate", bp::pure_virtual(&CommandInterface::Validate))
.def("run", bp::pure_virtual(&CommandInterface::Run))
.def("postRun", bp::pure_virtual(&CommandInterface::PostRun))
;
bp::class_<CommandProperties>("CommandProperties", bp::no_init)
.def_readwrite("default_time", &CommandProperties::default_time)
.def_readwrite("ability", &CommandProperties::character_ability)
.def_readwrite("client_effect_self", &CommandProperties::client_effect_self)
.def_readwrite("client_effect_target", &CommandProperties::client_effect_target)
.def_readwrite("allow_in_locomotion", &CommandProperties::allow_in_locomotion)
.def_readwrite("allow_in_state", &CommandProperties::allow_in_state)
.def_readwrite("target_type", &CommandProperties::target_type)
.def_readwrite("call_on_target", &CommandProperties::call_on_target)
.def_readwrite("command_group", &CommandProperties::command_group)
.def_readwrite("max_range_to_target", &CommandProperties::max_range_to_target)
.def_readwrite("god_level", &CommandProperties::god_level)
.def_readwrite("add_to_combat_queue", &CommandProperties::add_to_combat_queue)
;
bp::class_<BaseSwgCommand, BaseSwgCommandWrapper, bp::bases<CommandInterface>, boost::noncopyable>
("BaseSwgCommand", bp::init<swganh::app::SwganhKernel*, const CommandProperties&>())
.def("validate", &BaseSwgCommandWrapper::Validate)
.def("setup", &BaseSwgCommand::SetCommandProperties)
.def("postRun", &BaseSwgCommand::PostRun)
.def("getKernel", &BaseSwgCommandWrapper::GetKernel, bp::return_internal_reference<>())
.def("getCommandName", &BaseSwgCommandWrapper::GetCommandName)
.def("getActionCounter", &BaseSwgCommandWrapper::GetActionCounter)
.def("getPriority", &BaseSwgCommandWrapper::GetPriority)
.def("getCommandGroup", &BaseSwgCommandWrapper::GetCommandGroup)
.def("getCommandString", &BaseSwgCommandWrapper::GetCommandString, bp::return_value_policy<bp::copy_const_reference>())
.def("getTargetRequiredType", &BaseSwgCommandWrapper::GetTargetRequiredType)
.def("getMaxRangeToTarget", &BaseSwgCommandWrapper::GetMaxRangeToTarget)
.def("getDefaultTime", &BaseSwgCommandWrapper::GetDefaultTime)
.def("getRequiredAbility", &BaseSwgCommandWrapper::GetRequiredAbility)
.def("isQueuedCommand", &BaseSwgCommandWrapper::IsQueuedCommand)
.def("getActor", &BaseSwgCommandWrapper::GetActor, bp::return_value_policy<bp::copy_const_reference>())
.def("getTarget", &BaseSwgCommandWrapper::GetTarget, bp::return_value_policy<bp::copy_const_reference>())
.def("getTargetCreature", &BaseSwgCommandWrapper::GetTargetCreature)
;
}