Repository navigation
Expand file tree
/
Copy pathCommandScript.cpp
More file actions
122 lines (96 loc) · 3.64 KB
/
Copy pathCommandScript.cpp
File metadata and controls
122 lines (96 loc) · 3.64 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
#include "StringHelper.h"
#include "PythonEngine.h"
#include "ScriptMgr.h"
#include "Chat.h"
using namespace Acore::ChatCommands;
using namespace PyEng::Utilities;
class PythonCommandScript : public CommandScript
{
public:
PythonCommandScript() : CommandScript("PythonCommandScript") {}
ChatCommandTable GetCommands() const override
{
static ChatCommandTable pythonCommandTable = {
{ "status", HandlePythonStatusCommand, SEC_ADMINISTRATOR, Console::Yes },
{ "reload", HandlePythonReloadCommand, SEC_ADMINISTRATOR, Console::Yes },
{ "exec", HandlePythonExecCommand, SEC_ADMINISTRATOR, Console::Yes }
};
static ChatCommandTable commandTable = {
{ "python", pythonCommandTable }
};
return commandTable;
}
static bool HandlePythonStatusCommand(ChatHandler* handler, char const* /*args*/)
{
bool enabled = sPythonEngine->IsEnabled();
if (!enabled)
{
handler->SendErrorMessage("Python Engine is disabled");
return false;
}
handler->PSendSysMessage("Python Engine: {}", sPythonEngine->IsBusy() ? "Busy" : "Ready");
handler->PSendSysMessage("Reloading: {}", sPythonEngine->IsReloading() ? "Yes" : "No");
handler->PSendSysMessage("Scripts Path: {}", sConfigMgr->GetOption<std::string>("Python.ScriptsPath", "python_scripts"));
return true;
}
static bool HandlePythonReloadCommand(ChatHandler* handler, char const* /*args*/)
{
if (!sPythonEngine->IsEnabled())
{
handler->SendErrorMessage("Error: Python Engine is disabled");
return false;
}
// Begin reload (atomically set reloading flag)
if (!sPythonEngine->BeginReload())
{
handler->SendErrorMessage("Error: Reload already in progress");
return false;
}
handler->SendSysMessage("Reloading Python scripts...");
sPythonEngine->ClearHooks();
sPythonEngine->LoadScripts();
// End reload (atomically clear reloading flag)
sPythonEngine->EndReload();
handler->SendSysMessage("Python scripts reloaded successfully");
return true;
}
static bool HandlePythonExecCommand(ChatHandler* handler, char const* args)
{
if (!sPythonEngine->IsEnabled())
{
handler->SendErrorMessage("Error: Python Engine is disabled");
return false;
}
if (!*args)
{
handler->SendErrorMessage(LANG_CMD_SYNTAX);
return false;
}
std::string code = std::string(StringHelper::Unquote(StringHelper::Trim(args)));
if (code.empty())
{
handler->SendErrorMessage(LANG_CMD_SYNTAX);
return false;
}
auto result = sPythonEngine->ExecuteCode(code);
handler->SendSysMessage(result ? "Output:" : "Error:");
if (result.HasMessage())
{
auto lines = StringHelper::SplitLines(result.GetMessage(), 250);
constexpr size_t MAX_OUTPUT_LINES = 10;
size_t linesToShow = std::min(lines.size(), MAX_OUTPUT_LINES);
for (size_t i = 0; i < linesToShow; ++i)
handler->SendSysMessage(lines[i]);
if (lines.size() > MAX_OUTPUT_LINES)
handler->PSendSysMessage("... ({} lines truncated)", lines.size() - MAX_OUTPUT_LINES);
}
else
handler->SendSysMessage("No output...");
handler->SetSentErrorMessage(!result);
return static_cast<bool>(result);
}
};
void AddSC_PythonCommandScripts()
{
new PythonCommandScript();
}