summaryrefslogtreecommitdiffstats
path: root/src/Bindings
diff options
context:
space:
mode:
authorx12xx12x <44411062+12xx12@users.noreply.github.com>2021-06-23 00:09:06 +0200
committerGitHub <noreply@github.com>2021-06-23 00:09:06 +0200
commitcc9f7c06b39ce840d0dcde36e816cf025bd7ce81 (patch)
treea2adc4d33fbba4915e245133ff3896aecd2a798d /src/Bindings
parentDeath messages for tamed pets and ocelots are now tamable. (#5243) (diff)
downloadcuberite-cc9f7c06b39ce840d0dcde36e816cf025bd7ce81.tar
cuberite-cc9f7c06b39ce840d0dcde36e816cf025bd7ce81.tar.gz
cuberite-cc9f7c06b39ce840d0dcde36e816cf025bd7ce81.tar.bz2
cuberite-cc9f7c06b39ce840d0dcde36e816cf025bd7ce81.tar.lz
cuberite-cc9f7c06b39ce840d0dcde36e816cf025bd7ce81.tar.xz
cuberite-cc9f7c06b39ce840d0dcde36e816cf025bd7ce81.tar.zst
cuberite-cc9f7c06b39ce840d0dcde36e816cf025bd7ce81.zip
Diffstat (limited to '')
-rw-r--r--src/Bindings/LuaState.cpp16
-rw-r--r--src/Bindings/LuaState.h1
-rw-r--r--src/Bindings/ManualBindings.cpp81
3 files changed, 50 insertions, 48 deletions
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp
index ccd7244fa..3a1379df1 100644
--- a/src/Bindings/LuaState.cpp
+++ b/src/Bindings/LuaState.cpp
@@ -1489,6 +1489,22 @@ bool cLuaState::GetStackValue(int a_StackPos, cUUID & a_Value)
+bool cLuaState::GetStackValue(int a_StackPos, std::string_view & a_Value)
+{
+ size_t Length = 0;
+ const char * const Value = lua_tolstring(m_LuaState, a_StackPos, &Length);
+ if (Value != nullptr)
+ {
+ a_Value = { Value, Length };
+ return true;
+ }
+ return false;
+}
+
+
+
+
+
template <typename T>
bool cLuaState::GetStackValue(int a_StackPos, Vector3<T> & a_ReturnedVal)
{
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h
index 6f7a334b6..420fdad69 100644
--- a/src/Bindings/LuaState.h
+++ b/src/Bindings/LuaState.h
@@ -664,6 +664,7 @@ public:
bool GetStackValue(int a_StackPos, eWeather & a_Value);
bool GetStackValue(int a_StackPos, float & a_ReturnedVal);
bool GetStackValue(int a_StackPos, cUUID & a_Value);
+ bool GetStackValue(int a_StackPos, std::string_view & a_Value);
// template to catch all of the various c++ integral types without overload conflicts
template <class T, typename = std::enable_if_t<std::is_integral_v<T>>>
diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp
index 1d0519708..37fc20d39 100644
--- a/src/Bindings/ManualBindings.cpp
+++ b/src/Bindings/ManualBindings.cpp
@@ -347,50 +347,59 @@ static int tolua_StringSplitAndTrim(lua_State * tolua_S)
-/** Retrieves the log message from the first param on the Lua stack.
-Can take either a string or a cCompositeChat.
-*/
-static void LogFromLuaStack(lua_State * tolua_S, eLogLevel a_LogLevel)
+/** Prints the message to the console, optionally formatting it with a plugin name prefix if the second param on the Lua stack is true. */
+static void LogFromLuaStack(lua_State * tolua_S, const std::string_view a_Message, const eLogLevel a_LogLevel)
{
- tolua_Error err;
- if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err))
+ if (lua_isboolean(tolua_S, 2) && (lua_toboolean(tolua_S, 2) == 1))
{
- auto Msg = static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr))->ExtractText();
- Logger::LogSimple(Msg, a_LogLevel);
- return;
+ Logger::LogSimple(a_Message, a_LogLevel);
+ }
+ else
+ {
+ Logger::LogSimple(fmt::format("[{}] {}", cManualBindings::GetLuaPlugin(tolua_S)->GetName(), a_Message), a_LogLevel);
}
-
- size_t len = 0;
- const char * str = lua_tolstring(tolua_S, 1, &len);
- Logger::LogSimple(fmt::format("[{}] {}", cManualBindings::GetLuaPlugin(tolua_S)->GetName(), std::string_view(str, len)), a_LogLevel);
}
-static int tolua_LOG(lua_State * tolua_S)
+/** Retrieves a string log message from the first param on the Lua stack, optionally prefixes it with plugin name, and prints it to the console. */
+static void LogFromLuaStack(lua_State * tolua_S, const eLogLevel a_LogLevel)
{
+ cLuaState L(tolua_S);
+
// If there's no param, spit out an error message instead of crashing:
- if (lua_isnil(tolua_S, 1))
+ if (!L.CheckParamString(1))
{
- LOGWARNING("Attempting to LOG a nil value!");
- cLuaState::LogStackTrace(tolua_S);
- return 0;
+ return;
}
- // If the param is a cCompositeChat, read the log level from it:
- eLogLevel LogLevel = eLogLevel::Regular;
+ std::string_view Message;
+ L.GetStackValue(1, Message);
+ LogFromLuaStack(tolua_S, Message, a_LogLevel);
+}
+
+
+
+
+
+static int tolua_LOG(lua_State * tolua_S)
+{
+ // If the param is a cCompositeChat, read the data from it:
tolua_Error err;
if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err))
{
- LogLevel = cCompositeChat::MessageTypeToLogLevel(
- static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr))->GetMessageType()
- );
+ const auto CompositeChat = static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr));
+ if (CompositeChat != nullptr) // isusertype returns true for nil values
+ {
+ LogFromLuaStack(tolua_S, CompositeChat->ExtractText(), cCompositeChat::MessageTypeToLogLevel(CompositeChat->GetMessageType()));
+ return 0;
+ }
}
// Log the message:
- LogFromLuaStack(tolua_S, LogLevel);
+ LogFromLuaStack(tolua_S, eLogLevel::Regular);
return 0;
}
@@ -400,14 +409,6 @@ static int tolua_LOG(lua_State * tolua_S)
static int tolua_LOGINFO(lua_State * tolua_S)
{
- // If there's no param, spit out an error message instead of crashing:
- if (lua_isnil(tolua_S, 1))
- {
- LOGWARNING("Attempting to LOGINFO a nil value!");
- cLuaState::LogStackTrace(tolua_S);
- return 0;
- }
-
LogFromLuaStack(tolua_S, eLogLevel::Info);
return 0;
}
@@ -418,14 +419,6 @@ static int tolua_LOGINFO(lua_State * tolua_S)
static int tolua_LOGWARN(lua_State * tolua_S)
{
- // If there's no param, spit out an error message instead of crashing:
- if (lua_isnil(tolua_S, 1))
- {
- LOGWARNING("Attempting to LOGWARN a nil value!");
- cLuaState::LogStackTrace(tolua_S);
- return 0;
- }
-
LogFromLuaStack(tolua_S, eLogLevel::Warning);
return 0;
}
@@ -436,14 +429,6 @@ static int tolua_LOGWARN(lua_State * tolua_S)
static int tolua_LOGERROR(lua_State * tolua_S)
{
- // If there's no param, spit out an error message instead of crashing:
- if (lua_isnil(tolua_S, 1))
- {
- LOGWARNING("Attempting to LOGERROR a nil value!");
- cLuaState::LogStackTrace(tolua_S);
- return 0;
- }
-
LogFromLuaStack(tolua_S, eLogLevel::Error);
return 0;
}