mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-08-17 16:57:05 +00:00
LuaAPI: Print: dump tables properly and recursively
This commit is contained in:
parent
9ef6c32864
commit
d7f7a81cb0
@ -1,12 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <tuple>
|
||||
#include "TLuaEngine.h"
|
||||
#include <tuple>
|
||||
|
||||
namespace LuaAPI {
|
||||
void Print(sol::variadic_args);
|
||||
namespace MP {
|
||||
static inline TLuaEngine* Engine { nullptr };
|
||||
|
||||
void GetOSName();
|
||||
std::tuple<int, int, int> GetServerVersion();
|
||||
}
|
||||
|
@ -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<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() {
|
||||
}
|
||||
|
||||
@ -11,12 +42,8 @@ std::tuple<int, int, int> 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<std::string>();
|
||||
} else {
|
||||
ToPrint += "((unprintable type))";
|
||||
}
|
||||
ToPrint += " ";
|
||||
ToPrint += LuaToString(Arg);
|
||||
ToPrint += "\t";
|
||||
}
|
||||
luaprint(ToPrint);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user