From 4727ed20846bb3d1a9eabb27aaaa9c5524129556 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Mon, 24 Sep 2018 21:33:39 +0100 Subject: Add a formatting function for Vector3 (#4282) * Vector3: Add custom fmt compatible formatter. * cLuaState: Add fmt version of ApiParamError * Use vector formatting in manual bindings * Always log vectors with FLOG --- src/Bindings/LuaState.cpp | 5 +-- src/Bindings/LuaState.h | 24 ++++++++++-- src/Bindings/ManualBindings.h | 10 ++--- src/Bindings/ManualBindings_BlockArea.cpp | 62 +++++++++++++------------------ 4 files changed, 53 insertions(+), 48 deletions(-) (limited to 'src/Bindings') diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 7ec099982..c1e71bd5c 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -2004,7 +2004,7 @@ void cLuaState::LogStackTrace(lua_State * a_LuaState, int a_StartingDepth) -int cLuaState::ApiParamError(const char * a_MsgFormat, fmt::ArgList argp) +int cLuaState::ApiParamError(fmt::StringRef a_Msg) { // Retrieve current function name lua_Debug entry; @@ -2012,8 +2012,7 @@ int cLuaState::ApiParamError(const char * a_MsgFormat, fmt::ArgList argp) VERIFY(lua_getinfo(m_LuaState, "n", &entry)); // Compose the error message: - AString msg = Printf(a_MsgFormat, argp); - AString errorMsg = fmt::format("{0}: {1}", (entry.name != nullptr) ? entry.name : "", msg); + AString errorMsg = fmt::format("{0}: {1}", (entry.name != nullptr) ? entry.name : "", a_Msg); // Log everything into the console: LOGWARNING("%s", errorMsg.c_str()); diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 2510c6f0b..362f16e21 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -820,11 +820,29 @@ public: /** Logs all items in the current stack trace to the server console */ static void LogStackTrace(lua_State * a_LuaState, int a_StartingDepth = 0); - /** Formats and prints the message, prefixed with the current function name, then logs the stack contents and raises a Lua error. + /** Prints the message, prefixed with the current function name, then logs the stack contents and raises a Lua error. To be used for bindings when they detect bad parameters. Doesn't return, but a dummy return type is provided so that Lua API functions may do "return ApiParamError(...)". */ - int ApiParamError(const char * a_MsgFormat, fmt::ArgList); - FMT_VARIADIC(int, ApiParamError, const char *) + int ApiParamError(fmt::StringRef a_Msg); + + /** Formats and prints the message using printf-style format specifiers, but prefixed with the current function name, then logs the stack contents and raises a Lua error. + To be used for bindings when they detect bad parameters. + Doesn't return, but a dummy return type is provided so that Lua API functions may do "return ApiParamError(...)". */ + template + int ApiParamError(const char * a_MsgFormat, const Args & ... a_Args) + { + return ApiParamError(Printf(a_MsgFormat, a_Args...)); + } + + /** Formats and prints the message using python-style format specifiers, but prefixed with the current function name, then logs the stack contents and raises a Lua error. + To be used for bindings when they detect bad parameters. + Doesn't return, but a dummy return type is provided so that Lua API functions may do "return ApiParamError(...)". */ + template + int FApiParamError(const char * a_MsgFormat, const Args & ... a_Args) + { + return ApiParamError(fmt::format(a_MsgFormat, a_Args...)); + } + /** Returns the type of the item on the specified position in the stack */ AString GetTypeText(int a_StackPos); diff --git a/src/Bindings/ManualBindings.h b/src/Bindings/ManualBindings.h index fb19d5a61..42e34eda2 100644 --- a/src/Bindings/ManualBindings.h +++ b/src/Bindings/ManualBindings.h @@ -288,17 +288,17 @@ public: L.GetStackValues(1, Self, BlockX, BlockY, BlockZ, FnRef); if (Self == nullptr) { - return lua_do_error(tolua_S, "Error in function call '#funcname#': Invalid 'self'"); + return L.ApiParamError("Invalid 'self'"); } if (!FnRef.IsValid()) { - return lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a valid callback function for parameter #5"); + return L.ApiParamError("Expected a valid callback function for parameter #5"); } if (!(Self->*CoordCheckFn)(BlockX, BlockY, BlockZ)) { - return lua_do_error(tolua_S, Printf("Error in function call '#funcname#': The provided coordinates ({%d, %d, %d}) are not valid", - BlockX, BlockY, BlockZ - ).c_str()); + return L.FApiParamError("The provided coordinates ({0}) are not valid", + Vector3i{BlockX, BlockY, BlockZ} + ); } // Call the DoWith function: diff --git a/src/Bindings/ManualBindings_BlockArea.cpp b/src/Bindings/ManualBindings_BlockArea.cpp index 72d9f7497..b2c634b33 100644 --- a/src/Bindings/ManualBindings_BlockArea.cpp +++ b/src/Bindings/ManualBindings_BlockArea.cpp @@ -129,7 +129,7 @@ static int tolua_cBlockArea_Create(lua_State * a_LuaState) // Create the area: if ((size.x <= 0) || (size.y <= 0) || (size.z <= 0)) { - return L.ApiParamError("Invalid sizes, must be greater than zero, got {%d, %d, %d}", size.x, size.y, size.z); + return L.FApiParamError("Invalid sizes, must be greater than zero, got {0}", size); } ASSERT(self != nullptr); self->Create(size, dataTypes); @@ -216,10 +216,8 @@ static int tolua_cBlockArea_GetBlockTypeMeta(lua_State * a_LuaState) readVector3iOverloadParams(L, 2, coords, "coords"); if (!self->IsValidCoords(coords)) { - return L.ApiParamError("Coords ({%d, %d, %d}) out of range ({%d, %d, %d} - {%d, %d, %d}).", - coords.x, coords.y, coords.z, - self->GetOriginX(), self->GetOriginY(), self->GetOriginZ(), - self->GetOriginX() + self->GetSizeX() - 1, self->GetOriginY() + self->GetSizeY() - 1, self->GetOriginZ() + self->GetSizeZ() - 1 + return L.FApiParamError("Coords ({0}) out of range ({1} - {2).", + coords, self->GetOrigin(), self->GetOrigin() + self->GetSize() - Vector3i{1, 1, 1} ); } BLOCKTYPE blockType; @@ -368,9 +366,8 @@ static int tolua_cBlockArea_GetRelBlockTypeMeta(lua_State * a_LuaState) readVector3iOverloadParams(L, 2, coords, "coords"); if (!self->IsValidRelCoords(coords)) { - return L.ApiParamError("The coords ({%d, %d, %d}) are out of range (max {%d, %d, %d}).", - coords.x, coords.y, coords.z, - self->GetSizeX() - 1, self->GetSizeY() - 1, self->GetSizeZ() - 1 + return L.FApiParamError("The coords ({0}) are out of range (max {1}).", + coords, self->GetSize() - Vector3i{1, 1, 1} ); } BLOCKTYPE blockType; @@ -518,32 +515,32 @@ static int tolua_cBlockArea_Read(lua_State * a_LuaState) bounds.Sort(); if (bounds.p1.y < 0) { - LOGWARNING("cBlockArea:Read(): MinBlockY less than zero, adjusting to zero. Coords: {%d, %d, %d} - {%d, %d, %d}", - bounds.p1.x, bounds.p1.y, bounds.p1.z, bounds.p2.x, bounds.p2.y, bounds.p2.z + FLOGWARNING("cBlockArea:Read(): MinBlockY less than zero, adjusting to zero. Coords: {0} - {1}", + bounds.p1, bounds.p2 ); L.LogStackTrace(); bounds.p1.y = 0; } else if (bounds.p1.y >= cChunkDef::Height) { - LOGWARNING("cBlockArea:Read(): MinBlockY more than chunk height, adjusting to chunk height. Coords: {%d, %d, %d} - {%d, %d, %d}", - bounds.p1.x, bounds.p1.y, bounds.p1.z, bounds.p2.x, bounds.p2.y, bounds.p2.z + FLOGWARNING("cBlockArea:Read(): MinBlockY more than chunk height, adjusting to chunk height. Coords: {0} - {1}", + bounds.p1, bounds.p2 ); L.LogStackTrace(); bounds.p1.y = cChunkDef::Height - 1; } if (bounds.p2.y < 0) { - LOGWARNING("cBlockArea:Read(): MaxBlockY less than zero, adjusting to zero. Coords: {%d, %d, %d} - {%d, %d, %d}", - bounds.p1.x, bounds.p1.y, bounds.p1.z, bounds.p2.x, bounds.p2.y, bounds.p2.z + FLOGWARNING("cBlockArea:Read(): MaxBlockY less than zero, adjusting to zero. Coords: {0} - {1}", + bounds.p1, bounds.p2 ); L.LogStackTrace(); bounds.p2.y = 0; } else if (bounds.p2.y > cChunkDef::Height) { - LOGWARNING("cBlockArea:Read(): MaxBlockY more than chunk height, adjusting to chunk height. Coords: {%d, %d, %d} - {%d, %d, %d}", - bounds.p1.x, bounds.p1.y, bounds.p1.z, bounds.p2.x, bounds.p2.y, bounds.p2.z + FLOGWARNING("cBlockArea:Read(): MaxBlockY more than chunk height, adjusting to chunk height. Coords: {0} - {1}", + bounds.p1, bounds.p2 ); L.LogStackTrace(); bounds.p2.y = cChunkDef::Height; @@ -789,10 +786,8 @@ static int GetBlock(lua_State * a_LuaState) readVector3iOverloadParams(L, 2, coords, "coords"); if (!self->IsValidCoords(coords)) { - return L.ApiParamError("The coords ({%d, %d, %d}) are out of range ({%d, %d, %d} - {%d, %d, %d}).", - coords.x, coords.y, coords.z, - self->GetOriginX(), self->GetOriginY(), self->GetOriginZ(), - self->GetOriginX() + self->GetSizeX() - 1, self->GetOriginY() + self->GetSizeY() - 1, self->GetOriginZ() + self->GetSizeZ() - 1 + return L.FApiParamError("The coords ({0}) are out of range ({1} - {2}).", + coords, self->GetOrigin(), self->GetOrigin() + self->GetSize() - Vector3i{1, 1, 1} ); } @@ -842,9 +837,8 @@ static int GetRelBlock(lua_State * a_LuaState) readVector3iOverloadParams(L, 2, coords, "coords"); if (!self->IsValidRelCoords(coords)) { - return L.ApiParamError("The coords ({%d, %d, %d}) are out of range ({%d, %d, %d}).", - coords.x, coords.y, coords.z, - self->GetSizeX(), self->GetSizeY(), self->GetSizeZ() + return L.ApiParamError("The coords ({0}) are out of range ({1}).", + coords, self->GetSize() ); } @@ -894,10 +888,8 @@ static int SetBlock(lua_State * a_LuaState) auto idx = readVector3iOverloadParams(L, 2, coords, "coords"); if (!self->IsValidCoords(coords)) { - return L.ApiParamError("The coords ({%d, %d, %d}) are out of range ({%d, %d, %d} - {%d, %d, %d}).", - coords.x, coords.y, coords.z, - self->GetOriginX(), self->GetOriginY(), self->GetOriginZ(), - self->GetOriginX() + self->GetSizeX() - 1, self->GetOriginY() + self->GetSizeY() - 1, self->GetOriginZ() + self->GetSizeZ() - 1 + return L.FApiParamError("The coords ({0}) are out of range ({1} - {2}).", + coords, self->GetOrigin(), self->GetOrigin() + self->GetSize() - Vector3i{1, 1, 1} ); } DataType data; @@ -949,9 +941,8 @@ static int SetRelBlock(lua_State * a_LuaState) auto idx = readVector3iOverloadParams(L, 2, coords, "coords"); if (!self->IsValidRelCoords(coords)) { - return L.ApiParamError("The coords ({%d, %d, %d}) are out of range ({%d, %d, %d}).", - coords.x, coords.y, coords.z, - self->GetSizeX(), self->GetSizeY(), self->GetSizeZ() + return L.FApiParamError("The coords ({0}) are out of range ({1}).", + coords, self->GetSize() ); } DataType data; @@ -993,10 +984,8 @@ static int tolua_cBlockArea_SetBlockTypeMeta(lua_State * a_LuaState) auto idx = readVector3iOverloadParams(L, 2, coords, "coords"); if (!self->IsValidCoords(coords)) { - return L.ApiParamError("The coords ({%d, %d, %d}) are out of range ({%d, %d, %d} - {%d, %d, %d}).", - coords.x, coords.y, coords.z, - self->GetOriginX(), self->GetOriginY(), self->GetOriginZ(), - self->GetOriginX() + self->GetSizeX() - 1, self->GetOriginY() + self->GetSizeY() - 1, self->GetOriginZ() + self->GetSizeZ() - 1 + return L.FApiParamError("The coords ({0}) are out of range ({1} - {2}).", + coords, self->GetOrigin(), self->GetOrigin() + self->GetSize() - Vector3i{1, 1, 1} ); } @@ -1043,9 +1032,8 @@ static int tolua_cBlockArea_SetRelBlockTypeMeta(lua_State * a_LuaState) auto idx = readVector3iOverloadParams(L, 2, coords, "coords"); if (!self->IsValidRelCoords(coords)) { - return L.ApiParamError("The coords ({%d, %d, %d}) are out of range ({%d, %d, %d}).", - coords.x, coords.y, coords.z, - self->GetSizeX(), self->GetSizeY(), self->GetSizeZ() + return L.FApiParamError("The coords ({0}) are out of range ({1}).", + coords, self->GetSize() ); } -- cgit v1.2.3