3#define SOL_NO_EXCEPTIONS 1
14 std::string name =
typeid(T).name();
18 std::unique_ptr<char, void (*)(
void*)> res{abi::__cxa_demangle(name.c_str(), NULL, NULL, &status), std::free};
20 return (status == 0) ? res.get() : name;
23inline std::string Demangle(
const std::type_index& type)
25 const char* name = type.name();
29 std::unique_ptr<char, void (*)(
void*)> res{abi::__cxa_demangle(name,
nullptr,
nullptr, &status), std::free};
31 return (status == 0) ? res.get() : name;
35std::string DemangleWithoutNamespace()
37 std::string demangled = Demangle<T>();
39 size_t pos = demangled.find_last_of(
"::");
40 if (pos != std::string::npos)
42 return demangled.substr(pos + 1);
48inline void SanitizeEnvironment(sol::state& lua, sol::environment& env)
50 env[
"collectgarbage"] = sol::nil;
51 env[
"dofile"] = sol::nil;
52 env[
"loadfile"] = sol::nil;
53 env[
"module"] = sol::nil;
54 env[
"load"] = sol::nil;
57 env[
"getfenv"] = sol::nil;
58 env[
"setfenv"] = sol::nil;
59 env[
"newproxy"] = sol::nil;
60 env[
"rawset"] = sol::nil;
61 env[
"rawget"] = sol::nil;
63 sol::table metatable = lua.create_table();
64 metatable[
"__index"] = lua.globals();
65 metatable[
"__newindex"] = [](sol::table self, sol::object key, sol::object value)
67 self.raw_set(key, value);
69 metatable[sol::metatable_key] = sol::nil;
70 metatable[
"__metatable"] =
"locked";
72 env[sol::metatable_key] = metatable;
79 inline sol::environment CreateEnvironment(sol::state& lua,
const bool globals)
83 sol::environment env = sol::environment(lua, sol::create, lua.globals());
85 SanitizeEnvironment(lua, env);
90 sol::environment env(lua, sol::create);
92 SanitizeEnvironment(lua, env);
97 inline bool LoadFile(sol::state& lua,
const char* name)
99 sol::protected_function_result result = lua.safe_script_file(name);
106 sol::error e = result;
107 LogColor(LOG_YELLOW,
"Failed to load lua file ", name,
" with error: ", e.what());
112 inline bool LoadFile(sol::state& lua, sol::environment& env,
const char* name)
114 sol::protected_function_result result = lua.safe_script_file(name, env);
121 sol::error e = result;
122 LogColor(LOG_YELLOW,
"Failed to load lua file ", name,
" with error: ", e.what());
127 template <
typename T>
128 std::optional<T> GetValue(sol::state& lua,
const char* key)
130 sol::object
object = lua[key];
133 return object.as<T>();
136 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected ", Demangle<T>());
141 template <
typename T>
142 std::optional<T> GetValue(sol::environment& env,
const char* key)
144 sol::object
object = env[key];
147 return object.as<T>();
150 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected ", Demangle<T>());
155 template <
typename O,
typename T>
156 std::optional<T> GetValueObjectValue(sol::state& lua,
const char* objectKey,
const char* key)
158 sol::object
object = lua[objectKey];
161 sol::object member =
object.as<sol::table>()[key];
164 return member.as<T>();
167 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected ", Demangle<T>());
172 LogColor(LOG_YELLOW,
"Type mismatch for: ", objectKey,
" expected ", Demangle<O>());
177 template <
typename O,
typename T>
178 std::optional<T> GetValueObjectValue(sol::environment& env,
const char* objectKey,
const char* key)
180 sol::object
object = env[objectKey];
183 sol::object member =
object.as<sol::table>()[key];
186 return member.as<T>();
189 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected ", Demangle<T>());
194 LogColor(LOG_YELLOW,
"Type mismatch for: ", objectKey,
" expected ", Demangle<O>());
199 template <
typename T>
200 bool TypeExists(sol::state& lua)
202 std::string typeName = DemangleWithoutNamespace<T>();
204 sol::object obj = lua[typeName];
208 template <
typename T>
209 bool TypeExists(sol::environment& env)
211 std::string typeName = DemangleWithoutNamespace<T>();
213 sol::object obj = env[typeName];
217 inline bool ObjectExists(sol::state& lua,
const char* key)
219 sol::object obj = lua[key];
223 inline bool ObjectExists(sol::environment& env,
const char* key)
225 sol::object obj = env[key];
229 inline bool FunctionExists(sol::state& lua,
const char* key)
231 sol::object obj = lua[key];
232 return obj.valid() && obj.is<sol::function>();
235 inline bool FunctionExists(sol::environment& env,
const char* key)
237 sol::object obj = env[key];
238 return obj.valid() && obj.is<sol::function>();
241 template <
bool log =
true,
typename... Args>
242 bool CallFunction(sol::state& lua,
const char* key, Args&&... args)
244 sol::protected_function function = lua[key];
247 sol::protected_function_result result = function(std::forward<Args>(args)...);
253 sol::error e = result;
255 LogColor(LOG_YELLOW,
"Invalid call of function ", key,
" with error: ", e.what());
262 LogColor(LOG_YELLOW,
"Function ", key,
" does not exists");
268 template <
bool log =
true,
typename... Args>
269 bool CallFunction(sol::environment& env,
const char* key, Args&&... args)
271 sol::protected_function function = env[key];
274 sol::protected_function_result result = function(std::forward<Args>(args)...);
280 sol::error e = result;
282 LogColor(LOG_YELLOW,
"Invalid call of function ", key,
" with error: ", e.what());
289 LogColor(LOG_YELLOW,
"Function ", key,
" does not exists");
295 template <
typename T,
typename... Args>
296 std::optional<T> CallFunctionWithReturn(sol::state& lua,
const char* key, Args&&... args)
298 sol::protected_function function = lua[key];
301 sol::protected_function_result result = function(std::forward<Args>(args)...);
304 sol::object
object = result.get<sol::object>();
307 return object.as<T>();
310 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected: ", Demangle<T>());
315 sol::error e = result;
316 LogColor(LOG_YELLOW,
"Invalid call of function: ", key,
" with error: ", e.what());
321 LogColor(LOG_YELLOW,
"Function ", key,
" does not exists");
326 template <
typename T,
typename... Args>
327 std::optional<T> CallFunctionWithReturn(sol::environment& env,
const char* key, Args&&... args)
329 sol::protected_function function = env[key];
332 sol::protected_function_result result = function(std::forward<Args>(args)...);
335 sol::object
object = result.get<sol::object>();
338 return object.as<T>();
341 LogColor(LOG_YELLOW,
"Type mismatch for: ", key,
" expected: ", Demangle<T>());
346 sol::error e = result;
347 LogColor(LOG_YELLOW,
"Invalid call of function: ", key,
" with error: ", e.what());
352 LogColor(LOG_YELLOW,
"Function ", key,
" does not exists");
357 template <
typename T>
358 void RegisterFunction(sol::state& lua,
const char* key, T function)
363 template <
typename T>
364 void RegisterFunction(sol::environment& env,
const char* key, T function)
369 template <
typename T,
typename I,
typename... Args>
370 void RegisterMethod(sol::state& lua,
const char* key, I& instance, T (I::*method)(Args...))
372 lua.set_function(key, method, &instance);
375 template <
typename T,
typename I,
typename... Args>
376 void RegisterMethod(sol::environment& env,
const char* key, I& instance, T (I::*method)(Args...))
378 env.set_function(key, method, &instance);
381 template <
typename T,
typename... Args>
382 void RegisterType(sol::state& lua,
const char* name, Args&&... args)
384 lua.new_usertype<T>(name, std::forward<Args>(args)...);
387 template <
typename T,
typename... Args>
388 void RegisterType(sol::environment& env,
const char* name, Args&&... args)
390 env.new_usertype<T>(name, std::forward<Args>(args)...);
393 template <
typename T>
394 void BindObject(sol::state& lua,
const char* name, T
object)
399 template <
typename T>
400 void BindObject(sol::environment& env,
const char* name, T
object)