-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScripting.cpp
More file actions
48 lines (41 loc) · 1020 Bytes
/
Copy pathScripting.cpp
File metadata and controls
48 lines (41 loc) · 1020 Bytes
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
#include <sol.hpp>
#include "engine/Types.h"
#include "engine/Console.h"
#include "engine/Scripting.h"
//Example implementation that uses Sol.
namespace Scripting
{
sol::state Sol;
static void CCmdLua(const jsonArray& args);
void Setup()
{
Sol["print"] = [&](sol::variadic_args va) { console->Print(0, va[0]); };
console->RegisterCCmd("lua", CCmdLua, true);
}
bool Conditional(const std::string& expression)
{
return Sol.script(fmt::format("return ({})", expression));
}
std::string BJTS(const std::string& code, const std::vector<std::string>& args)
{
Sol["bjts"] = args;
auto ret = Sol.script(code).get<std::string>();
Sol["bjts"] = nullptr;
return ret;
}
static void CCmdLua(const jsonArray& args)
{
try
{
Sol.script(args[0].as_string());
}
catch (sol::error& e)
{
std::string what = e.what();
if (what.find("attempt to yield from outside a coroutine") != std::string::npos)
return; //Accept this silently.
else
conprint(1, "Error: {}", what);
}
}
}