diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 43a1722..b7b6493 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -1,12 +1,13 @@ #pragma once -#include #include "TLuaEngine.h" +#include namespace LuaAPI { void Print(sol::variadic_args); namespace MP { static inline TLuaEngine* Engine { nullptr }; + void GetOSName(); std::tuple GetServerVersion(); } diff --git a/src/LuaAPI.cpp b/src/LuaAPI.cpp index f50a04a..084df24 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -1,6 +1,37 @@ #include "LuaAPI.h" #include "TLuaEngine.h" +static std::string LuaToString(const sol::object& Value) { + switch (Value.get_type()) { + case sol::type::string: + return Value.as(); + case sol::type::number: + return std::to_string(Value.as()); + case sol::type::boolean: + return Value.as() ? "true" : "false"; + case sol::type::table: { + std::stringstream Result; + auto Table = Value.as(); + Result << "[[table: " << Table.pointer() << "]]: {"; + if (!Table.empty()) { + for (const auto& Entry : Table) { + Result << "\n\t" << LuaToString(Entry.first) << ": " << LuaToString(Entry.second) << ","; + } + Result << "\n"; + } + Result << "}"; + return Result.str(); + } + case sol::type::function: { + std::stringstream ss; + ss << "[[function: " << Value.as().pointer() << "]]"; + return ss.str(); + } + default: + return "((unprintable type))"; + } +} + void LuaAPI::MP::GetOSName() { } @@ -11,12 +42,8 @@ std::tuple LuaAPI::MP::GetServerVersion() { void LuaAPI::Print(sol::variadic_args Args) { std::string ToPrint = ""; for (const auto& Arg : Args) { - if (Arg.get_type() == sol::type::string) { - ToPrint += Arg.as(); - } else { - ToPrint += "((unprintable type))"; - } - ToPrint += " "; + ToPrint += LuaToString(Arg); + ToPrint += "\t"; } luaprint(ToPrint); }