LuaAPI: Print: dump tables properly and recursively

This commit is contained in:
Lion Kortlepel 2021-09-16 12:21:11 +02:00
parent 9ef6c32864
commit d7f7a81cb0
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
2 changed files with 35 additions and 7 deletions

View File

@ -1,12 +1,13 @@
#pragma once #pragma once
#include <tuple>
#include "TLuaEngine.h" #include "TLuaEngine.h"
#include <tuple>
namespace LuaAPI { namespace LuaAPI {
void Print(sol::variadic_args); void Print(sol::variadic_args);
namespace MP { namespace MP {
static inline TLuaEngine* Engine { nullptr }; static inline TLuaEngine* Engine { nullptr };
void GetOSName(); void GetOSName();
std::tuple<int, int, int> GetServerVersion(); std::tuple<int, int, int> GetServerVersion();
} }

View File

@ -1,6 +1,37 @@
#include "LuaAPI.h" #include "LuaAPI.h"
#include "TLuaEngine.h" #include "TLuaEngine.h"
static std::string LuaToString(const sol::object& Value) {
switch (Value.get_type()) {
case sol::type::string:
return Value.as<std::string>();
case sol::type::number:
return std::to_string(Value.as<float>());
case sol::type::boolean:
return Value.as<bool>() ? "true" : "false";
case sol::type::table: {
std::stringstream Result;
auto Table = Value.as<sol::table>();
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<sol::function>().pointer() << "]]";
return ss.str();
}
default:
return "((unprintable type))";
}
}
void LuaAPI::MP::GetOSName() { void LuaAPI::MP::GetOSName() {
} }
@ -11,12 +42,8 @@ std::tuple<int, int, int> LuaAPI::MP::GetServerVersion() {
void LuaAPI::Print(sol::variadic_args Args) { void LuaAPI::Print(sol::variadic_args Args) {
std::string ToPrint = ""; std::string ToPrint = "";
for (const auto& Arg : Args) { for (const auto& Arg : Args) {
if (Arg.get_type() == sol::type::string) { ToPrint += LuaToString(Arg);
ToPrint += Arg.as<std::string>(); ToPrint += "\t";
} else {
ToPrint += "((unprintable type))";
}
ToPrint += " ";
} }
luaprint(ToPrint); luaprint(ToPrint);
} }