3#define SOL_NO_EXCEPTIONS 1
8inline void SanitizeEnvironment(sol::state& lua, sol::environment& env)
10 env[
"collectgarbage"] = sol::nil;
11 env[
"dofile"] = sol::nil;
12 env[
"loadfile"] = sol::nil;
13 env[
"module"] = sol::nil;
14 env[
"load"] = sol::nil;
17 env[
"getfenv"] = sol::nil;
18 env[
"setfenv"] = sol::nil;
19 env[
"newproxy"] = sol::nil;
20 env[
"rawset"] = sol::nil;
21 env[
"rawget"] = sol::nil;
23 sol::table metatable = lua.create_table();
24 metatable[
"__index"] = lua.globals();
25 metatable[
"__newindex"] = [](sol::table self, sol::object key, sol::object value)
27 self.raw_set(key, value);
29 metatable[sol::metatable_key] = sol::nil;
30 metatable[
"__metatable"] =
"locked";
32 env[sol::metatable_key] = metatable;
39 inline sol::environment CreateEnvironment(sol::state& lua,
const bool globals)
43 sol::environment env = sol::environment(lua, sol::create, lua.globals());
45 SanitizeEnvironment(lua, env);
50 sol::environment env(lua, sol::create);
52 SanitizeEnvironment(lua, env);
57 inline bool LoadFile(sol::state& lua,
const char* name)
59 sol::protected_function_result result = lua.safe_script_file(name);
66 sol::error e = result;
67 LogColor(LOG_YELLOW,
"Failed to load lua file ", name,
" with error: ", e.what());
72 inline bool LoadFile(sol::state& lua, sol::environment& env,
const char* name)
74 sol::protected_function_result result = lua.safe_script_file(name, env);
81 sol::error e = result;
82 LogColor(LOG_YELLOW,
"Failed to load lua file ", name,
" with error: ", e.what());
88 std::optional<T> GetValue(sol::state& lua,
const char* key)
90 sol::object
object = lua[key];
93 return object.as<T>();
96 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected ", Demangle<T>());
101 template <
typename T>
102 std::optional<T> GetValue(sol::environment& env,
const char* key)
104 sol::object
object = env[key];
107 return object.as<T>();
110 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected ", Demangle<T>());
115 template <
typename O,
typename T>
116 std::optional<T> GetValueObjectValue(sol::state& lua,
const char* objectKey,
const char* key)
118 sol::object
object = lua[objectKey];
121 sol::object member =
object.as<sol::table>()[key];
124 return member.as<T>();
127 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected ", Demangle<T>());
132 LogColor(LOG_YELLOW,
"Type mismatch for: ", objectKey,
" expected ", Demangle<O>());
137 template <
typename O,
typename T>
138 std::optional<T> GetValueObjectValue(sol::environment& env,
const char* objectKey,
const char* key)
140 sol::object
object = env[objectKey];
143 sol::object member =
object.as<sol::table>()[key];
146 return member.as<T>();
149 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected ", Demangle<T>());
154 LogColor(LOG_YELLOW,
"Type mismatch for: ", objectKey,
" expected ", Demangle<O>());
159 template <
typename T>
160 bool TypeExists(sol::state& lua)
162 std::string typeName = DemangleWithoutNamespace<T>();
164 sol::object obj = lua[typeName];
168 template <
typename T>
169 bool TypeExists(sol::environment& env)
171 std::string typeName = DemangleWithoutNamespace<T>();
173 sol::object obj = env[typeName];
177 inline bool ObjectExists(sol::state& lua,
const char* key)
179 sol::object obj = lua[key];
183 inline bool ObjectExists(sol::environment& env,
const char* key)
185 sol::object obj = env[key];
189 inline bool FunctionExists(sol::state& lua,
const char* key)
191 sol::object obj = lua[key];
192 return obj.valid() && obj.is<sol::function>();
195 inline bool FunctionExists(sol::environment& env,
const char* key)
197 sol::object obj = env[key];
198 return obj.valid() && obj.is<sol::function>();
201 template <
bool log =
true,
typename... Args>
202 bool CallFunction(sol::state& lua,
const char* key, Args&&... args)
204 sol::protected_function function = lua[key];
207 sol::protected_function_result result = function(std::forward<Args>(args)...);
213 sol::error e = result;
215 LogColor(LOG_YELLOW,
"Invalid call of function ", key,
" with error: ", e.what());
222 LogColor(LOG_YELLOW,
"Function ", key,
" does not exists");
228 template <
bool log =
true,
typename... Args>
229 bool CallFunction(sol::environment& env,
const char* key, Args&&... args)
231 sol::protected_function function = env[key];
234 sol::protected_function_result result = function(std::forward<Args>(args)...);
240 sol::error e = result;
242 LogColor(LOG_YELLOW,
"Invalid call of function ", key,
" with error: ", e.what());
249 LogColor(LOG_YELLOW,
"Function ", key,
" does not exists");
255 template <
typename T,
typename... Args>
256 std::optional<T> CallFunctionWithReturn(sol::state& lua,
const char* key, Args&&... args)
258 sol::protected_function function = lua[key];
261 sol::protected_function_result result = function(std::forward<Args>(args)...);
264 sol::object
object = result.get<sol::object>();
267 return object.as<T>();
270 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected: ", Demangle<T>());
275 sol::error e = result;
276 LogColor(LOG_YELLOW,
"Invalid call of function: ", key,
" with error: ", e.what());
281 LogColor(LOG_YELLOW,
"Function ", key,
" does not exists");
286 template <
typename T,
typename... Args>
287 std::optional<T> CallFunctionWithReturn(sol::environment& env,
const char* key, Args&&... args)
289 sol::protected_function function = env[key];
292 sol::protected_function_result result = function(std::forward<Args>(args)...);
295 sol::object
object = result.get<sol::object>();
298 return object.as<T>();
301 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected: ", Demangle<T>());
306 sol::error e = result;
307 LogColor(LOG_YELLOW,
"Invalid call of function: ", key,
" with error: ", e.what());
312 LogColor(LOG_YELLOW,
"Function ", key,
" does not exists");
317 template <
typename T>
318 void RegisterFunction(sol::state& lua,
const char* key, T function)
323 template <
typename T>
324 void RegisterFunction(sol::environment& env,
const char* key, T function)
329 template <
typename T,
typename I,
typename... Args>
330 void RegisterMethod(sol::state& lua,
const char* key, I& instance, T (I::*method)(Args...))
332 lua.set_function(key, method, &instance);
335 template <
typename T,
typename I,
typename... Args>
336 void RegisterMethod(sol::environment& env,
const char* key, I& instance, T (I::*method)(Args...))
338 env.set_function(key, method, &instance);
341 template <
typename T,
typename... Args>
342 void RegisterType(sol::state& lua,
const char* name, Args&&... args)
344 lua.new_usertype<T>(name, std::forward<Args>(args)...);
347 template <
typename T,
typename... Args>
348 void RegisterType(sol::environment& env,
const char* name, Args&&... args)
350 env.new_usertype<T>(name, std::forward<Args>(args)...);
353 template <
typename T>
354 void BindObject(sol::state& lua,
const char* name, T
object)
359 template <
typename T>
360 void BindObject(sol::environment& env,
const char* name, T
object)