-
Notifications
You must be signed in to change notification settings - Fork 172
/
httpScriptAccess.cpp
158 lines (135 loc) · 5.13 KB
/
httpScriptAccess.cpp
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
#include "httpScriptAccess.h"
#include "gameGlobalInfo.h"
#define sOBJECT "_OBJECT_"
EEHttpServer::EEHttpServer(int port, string static_file_path)
: server(port)
{
server.setStaticFilePath(static_file_path);
server.addURLHandler("/exec.lua", [](const sp::io::http::Server::Request& request) -> string
{
if (!gameGlobalInfo)
{
return "{\"ERROR\": \"No game\"}";
}
P<ScriptObject> script = new ScriptObject();
script->setMaxRunCycles(100000);
string output;
if (!script->runCode(request.post_data, output))
output = "{\"ERROR\": \"Script error: " + script->getError().replace("\"", "'") + "\"}";
script->destroy();
return output;
});
server.addURLHandler("/get.lua", [](const sp::io::http::Server::Request& request) -> string
{
/*
Call LUA-exposed functions and return their result in a dictionary.
Use _OBJECT_=someObjectGetter() to get the object of which to call functions
Defaults to getPlayerShip(-1)
Syntax: /get.lua?dictionaryKey=functionName("arguments)
Example Getter: /get.lua?hull=getHull()&nukes=getWeaponStorage("nuke")
Creates the following LUA-code:
object = getPlayerShip(-1)
if object == nil then return {error = "No valid object"} end
return {hull = object:getHull(), nukes = object:getWeaponStorage("nuke"), }
Returns: {hull = 100, nukes = 42}
*/
if (!gameGlobalInfo)
{
return "{\"ERROR\": \"No game\"}";
}
string luaCode;
string objectId = "getPlayerShip(-1)";
if (my_spaceship) {
int index = gameGlobalInfo->findPlayerShip(my_spaceship);
objectId = "getPlayerShip("+std::to_string(index+1)+")";
}
std::unordered_map<string, string>::const_iterator i;
P<ScriptObject> script;
string output;
// Look for _OBJECT_ in parameters. If not found, use default
i = request.query.find(sOBJECT);
if (i != request.query.end())
{
objectId = i->second;
}
luaCode = "object = " + objectId + "\n" +
"if object == nil then return {error = \"No valid object\"} end\n" +
"return {";
// Loop through URL parameters
for (i = request.query.begin(); i != request.query.end(); i++)
{
if (i->first == sOBJECT)
continue;
// Fail if trying to set stuff. We only do get.
if (i->second.substr(0, 3) == "set")
{
return "{\"ERROR\": \"Cannot set values through get.lua\", \"COMMAND\": \"" + i->second + "\"}";
}
// Build LUA-code
luaCode += i->first + " = object:" + i->second + ", ";
} luaCode += "}";
// Run script
script = new ScriptObject();
script->setMaxRunCycles(100000);
// Return dictionary with error, else output
if (!script->runCode(luaCode, output))
{
output = "{\"ERROR\": \"Script error\"}";
}
script->destroy();
return output;
});
server.addURLHandler("/set.lua", [](const sp::io::http::Server::Request& request) -> string
{
/*
Call LUA-exposed functions with arguments.
Use _OBJECT_=someObjectGetter() to get the object of which to call functions
Defaults to getPlayerShip(-1)
Syntax: /set.lua?someFunction('arg')&otherFunction(1,2,3)
Example Setter: /set.lua?setShieldsActive(true)&setSpeed(200, 3)
Creates the following LUA-code:
object = getPlayerShip(-1)
if object == nil then return {error = "No valid object"} end
object:setShieldsActive(true);
object:setSpeed(200, 3);
Returns nothing, or ERROR on failure.
*/
if (!gameGlobalInfo)
{
return "{\"ERROR\": \"No game\"}";
}
string luaCode;
string objectId = "getPlayerShip(-1)";
if (my_spaceship) {
int index = gameGlobalInfo->findPlayerShip(my_spaceship);
objectId = "getPlayerShip("+std::to_string(index+1)+")";
}
std::unordered_map<string, string>::const_iterator i;
P<ScriptObject> script;
string output;
i = request.query.find(sOBJECT);
if (i != request.query.end())
{
objectId = i->first;
}
luaCode = "object = " + objectId + "\n" +
"if object == nil then return {error = \"No valid object\"} end\n";
for (i = request.query.begin(); i != request.query.end(); i++)
{
if (i->first == sOBJECT)
continue;
if (i->second == "")
luaCode += "object:" + i->first + ";\n";
else
luaCode += i->first + ":" + i->second + ";\n";
}
script = new ScriptObject();
script->setMaxRunCycles(100000);
if (!script->runCode(luaCode, output))
output = "{\"ERROR\": \"Script error\"}";
else
output = "{}";
script->destroy();
return output;
});
}