diff options
Diffstat (limited to 'src/Bindings')
-rw-r--r-- | src/Bindings/LuaState.cpp | 18 | ||||
-rw-r--r-- | src/Bindings/LuaState.h | 1 | ||||
-rw-r--r-- | src/Bindings/ManualBindings.cpp | 418 | ||||
-rw-r--r-- | src/Bindings/ManualBindings.h | 2 | ||||
-rw-r--r-- | src/Bindings/ManualBindings_World.cpp | 109 | ||||
-rw-r--r-- | src/Bindings/PluginLua.h | 4 | ||||
-rw-r--r-- | src/Bindings/PluginManager.cpp | 2 |
7 files changed, 495 insertions, 59 deletions
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 7b519a798..c9e7815ca 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -128,7 +128,7 @@ void cLuaState::Close(void) { LOGWARNING( "%s: Detected mis-use, calling Close() on an attached state (0x%p). Detaching instead.", - __FUNCTION__, m_LuaState + __FUNCTION__, static_cast<void *>(m_LuaState) ); Detach(); return; @@ -146,7 +146,7 @@ void cLuaState::Attach(lua_State * a_State) { if (m_LuaState != nullptr) { - LOGINFO("%s: Already contains a LuaState (0x%p), will be closed / detached.", __FUNCTION__, m_LuaState); + LOGINFO("%s: Already contains a LuaState (0x%p), will be closed / detached.", __FUNCTION__, static_cast<void *>(m_LuaState)); if (m_IsOwned) { Close(); @@ -174,7 +174,7 @@ void cLuaState::Detach(void) { LOGWARNING( "%s: Detected a mis-use, calling Detach() when the state is owned. Closing the owned state (0x%p).", - __FUNCTION__, m_LuaState + __FUNCTION__, static_cast<void *>(m_LuaState) ); Close(); return; @@ -676,6 +676,18 @@ void cLuaState::Push(int a_Value) +void cLuaState::Push(long a_Value) +{ + ASSERT(IsValid()); + + tolua_pushnumber(m_LuaState, static_cast<lua_Number>(a_Value)); + m_NumCurrentFunctionArgs += 1; +} + + + + + void cLuaState::Push(UInt32 a_Value) { ASSERT(IsValid()); diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 759fdc54f..269a10369 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -248,6 +248,7 @@ public: void Push(cLuaUDPEndpoint * a_UDPEndpoint); void Push(double a_Value); void Push(int a_Value); + void Push(long a_Value); void Push(const UInt32 a_Value); void Push(void * a_Ptr); void Push(std::chrono::milliseconds a_time); diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 876d4e280..7e6839fdf 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -482,8 +482,258 @@ cPluginLua * cManualBindings::GetLuaPlugin(lua_State * L) +static int tolua_cFile_ChangeFileExt(lua_State * tolua_S) +{ + // API signature: + // ChangeFileExt(string, string) -> string + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2, 3) || + !L.CheckParamEnd(4) + ) + { + return 0; + } + + // Execute: + AString FileName, NewExt; + ASSERT(L.GetStackValues(2, FileName, NewExt)); + L.Push(cFile::ChangeFileExt(FileName, NewExt)); + return 1; +} + + + + + +static int tolua_cFile_Copy(lua_State * tolua_S) +{ + // API signature: + // cFile:Copy(string, string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2, 3) || + !L.CheckParamEnd(4) + ) + { + return 0; + } + + // Execute: + AString SrcFile, DstFile; + ASSERT(L.GetStackValues(2, SrcFile, DstFile)); + L.Push(cFile::Copy(SrcFile, DstFile)); + return 1; +} + + + + + +static int tolua_cFile_CreateFolder(lua_State * tolua_S) +{ + // API signature: + // cFile:CreateFolder(string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString FolderPath; + ASSERT(L.GetStackValues(2, FolderPath)); + L.Push(cFile::CreateFolder(FolderPath)); + return 1; +} + + + + + +static int tolua_cFile_CreateFolderRecursive(lua_State * tolua_S) +{ + // API signature: + // cFile:CreateFolderRecursive(string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString FolderPath; + ASSERT(L.GetStackValues(2, FolderPath)); + L.Push(cFile::CreateFolderRecursive(FolderPath)); + return 1; +} + + + + + +static int tolua_cFile_Delete(lua_State * tolua_S) +{ + // API signature: + // cFile:Delete(string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString Path; + ASSERT(L.GetStackValues(2, Path)); + L.Push(cFile::Delete(Path)); + return 1; +} + + + + + +static int tolua_cFile_DeleteFile(lua_State * tolua_S) +{ + // API signature: + // cFile:DeleteFile(string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString Path; + ASSERT(L.GetStackValues(2, Path)); + L.Push(cFile::DeleteFile(Path)); + return 1; +} + + + + + +static int tolua_cFile_DeleteFolder(lua_State * tolua_S) +{ + // API signature: + // cFile:DeleteFolder(string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString Path; + ASSERT(L.GetStackValues(2, Path)); + L.Push(cFile::DeleteFolder(Path)); + return 1; +} + + + + + +static int tolua_cFile_DeleteFolderContents(lua_State * tolua_S) +{ + // API signature: + // cFile:DeleteFolderContents(string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString Path; + ASSERT(L.GetStackValues(2, Path)); + L.Push(cFile::DeleteFolderContents(Path)); + return 1; +} + + + + + +static int tolua_cFile_Exists(lua_State * tolua_S) +{ + // API signature: + // cFile:Exists(string) -> bool + + // Obsolete, use IsFile() or IsFolder() instead + cLuaState L(tolua_S); + LOGWARNING("cFile:Exists() is obsolete, use cFile:IsFolder() or cFile:IsFile() instead!"); + L.LogStackTrace(); + + // Check params: + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString Path; + ASSERT(L.GetStackValues(2, Path)); + L.Push(cFile::Exists(Path)); + return 1; +} + + + + + static int tolua_cFile_GetFolderContents(lua_State * tolua_S) { + // API signature: + // cFile:GetFolderContents(string) -> {string, string, ...} + // Check params: cLuaState LuaState(tolua_S); if ( @@ -497,7 +747,7 @@ static int tolua_cFile_GetFolderContents(lua_State * tolua_S) // Get params: AString Folder; - LuaState.GetStackValues(2, Folder); + ASSERT(LuaState.GetStackValues(2, Folder)); // Execute and push result: LuaState.Push(cFile::GetFolderContents(Folder)); @@ -508,8 +758,119 @@ static int tolua_cFile_GetFolderContents(lua_State * tolua_S) +static int tolua_cFile_GetLastModificationTime(lua_State * tolua_S) +{ + // API signature: + // cFile:GetLastModificationTime(string) -> number + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString Path; + ASSERT(L.GetStackValues(2, Path)); + L.Push(cFile::GetLastModificationTime(Path)); + return 1; +} + + + + + +static int tolua_cFile_GetSize(lua_State * tolua_S) +{ + // API signature: + // cFile:GetSize(string) -> number + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString Path; + ASSERT(L.GetStackValues(2, Path)); + L.Push(cFile::GetSize(Path)); + return 1; +} + + + + + +static int tolua_cFile_IsFile(lua_State * tolua_S) +{ + // API signature: + // cFile:IsFile(string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString Path; + ASSERT(L.GetStackValues(2, Path)); + L.Push(cFile::IsFile(Path)); + return 1; +} + + + + + +static int tolua_cFile_IsFolder(lua_State * tolua_S) +{ + // API signature: + // cFile:IsFolder(string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2) || + !L.CheckParamEnd(3) + ) + { + return 0; + } + + // Execute: + AString Path; + ASSERT(L.GetStackValues(2, Path)); + L.Push(cFile::IsFolder(Path)); + return 1; +} + + + + + static int tolua_cFile_ReadWholeFile(lua_State * tolua_S) { + // API signature: + // cFile:ReadWholeFile(string) -> string + // Check params: cLuaState LuaState(tolua_S); if ( @@ -523,7 +884,7 @@ static int tolua_cFile_ReadWholeFile(lua_State * tolua_S) // Get params: AString FileName; - LuaState.GetStackValues(2, FileName); + ASSERT(LuaState.GetStackValues(2, FileName)); // Execute and push result: LuaState.Push(cFile::ReadWholeFile(FileName)); @@ -534,6 +895,33 @@ static int tolua_cFile_ReadWholeFile(lua_State * tolua_S) +static int tolua_cFile_Rename(lua_State * tolua_S) +{ + // API signature: + // cFile:Rename(string, string) -> bool + + // Check params: + cLuaState L(tolua_S); + if ( + !L.CheckParamUserTable(1, "cFile") || + !L.CheckParamString(2, 3) || + !L.CheckParamEnd(4) + ) + { + return 0; + } + + // Execute: + AString SrcPath, DstPath; + ASSERT(L.GetStackValues(2, SrcPath, DstPath)); + L.Push(cFile::Rename(SrcPath, DstPath)); + return 1; +} + + + + + static int tolua_cPluginManager_GetAllPlugins(lua_State * tolua_S) { // API function no longer available: @@ -1151,7 +1539,7 @@ static int tolua_cPlayer_GetPermissions(lua_State * tolua_S) cPlayer * self = reinterpret_cast<cPlayer *>(tolua_tousertype(tolua_S, 1, nullptr)); if (self == nullptr) { - LOGWARNING("%s: invalid self (%p)", __FUNCTION__, self); + LOGWARNING("%s: invalid self (%p)", __FUNCTION__, static_cast<void *>(self)); return 0; } @@ -1182,7 +1570,7 @@ static int tolua_cPlayer_GetRestrictions(lua_State * tolua_S) cPlayer * self = reinterpret_cast<cPlayer *>(tolua_tousertype(tolua_S, 1, nullptr)); if (self == nullptr) { - LOGWARNING("%s: invalid self (%p)", __FUNCTION__, self); + LOGWARNING("%s: invalid self (%p)", __FUNCTION__, static_cast<void *>(self)); return 0; } @@ -1211,7 +1599,7 @@ static int tolua_cPlayer_OpenWindow(lua_State * tolua_S) cWindow * wnd = reinterpret_cast<cWindow *>(tolua_tousertype(tolua_S, 2, nullptr)); if ((self == nullptr) || (wnd == nullptr)) { - LOGWARNING("%s: invalid self (%p) or wnd (%p)", __FUNCTION__, self, wnd); + LOGWARNING("%s: invalid self (%p) or wnd (%p)", __FUNCTION__, static_cast<void *>(self), static_cast<void *>(wnd)); return 0; } @@ -1292,7 +1680,7 @@ static int tolua_SetObjectCallback(lua_State * tolua_S) OBJTYPE * self = reinterpret_cast<OBJTYPE *>(tolua_tousertype(tolua_S, 1, nullptr)); if (self == nullptr) { - LOGWARNING("%s: invalid self (%p)", __FUNCTION__, self); + LOGWARNING("%s: invalid self (%p)", __FUNCTION__, static_cast<void *>(self)); return 0; } int FnRef = luaL_ref(tolua_S, LUA_REGISTRYINDEX); // Store function reference for later retrieval @@ -2846,8 +3234,22 @@ void cManualBindings::Bind(lua_State * tolua_S) tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S, "cFile"); - tolua_function(tolua_S, "GetFolderContents", tolua_cFile_GetFolderContents); - tolua_function(tolua_S, "ReadWholeFile", tolua_cFile_ReadWholeFile); + tolua_function(tolua_S, "ChangeFileExt", tolua_cFile_ChangeFileExt); + tolua_function(tolua_S, "Copy", tolua_cFile_Copy); + tolua_function(tolua_S, "CreateFolder", tolua_cFile_CreateFolder); + tolua_function(tolua_S, "CreateFolderRecursive", tolua_cFile_CreateFolderRecursive); + tolua_function(tolua_S, "Delete", tolua_cFile_Delete); + tolua_function(tolua_S, "DeleteFile", tolua_cFile_DeleteFile); + tolua_function(tolua_S, "DeleteFolder", tolua_cFile_DeleteFolder); + tolua_function(tolua_S, "DeleteFolderContents", tolua_cFile_DeleteFolderContents); + tolua_function(tolua_S, "Exists", tolua_cFile_Exists); + tolua_function(tolua_S, "GetFolderContents", tolua_cFile_GetFolderContents); + tolua_function(tolua_S, "GetLastModificationTime", tolua_cFile_GetLastModificationTime); + tolua_function(tolua_S, "GetSize", tolua_cFile_GetSize); + tolua_function(tolua_S, "IsFile", tolua_cFile_IsFile); + tolua_function(tolua_S, "IsFolder", tolua_cFile_IsFolder); + tolua_function(tolua_S, "ReadWholeFile", tolua_cFile_ReadWholeFile); + tolua_function(tolua_S, "Rename", tolua_cFile_Rename); tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S, "cHopperEntity"); diff --git a/src/Bindings/ManualBindings.h b/src/Bindings/ManualBindings.h index 38f7ac5f1..e2556bafd 100644 --- a/src/Bindings/ManualBindings.h +++ b/src/Bindings/ManualBindings.h @@ -400,7 +400,7 @@ public: L.GetStackValues(1, Self, Box, FnRef); if ((Self == nullptr) || (Box == nullptr)) { - LOGWARNING("Invalid world (%p) or boundingbox (%p)", Self, Box); + LOGWARNING("Invalid world (%p) or boundingbox (%p)", static_cast<void *>(Self), static_cast<void *>(Box)); L.LogStackTrace(); return 0; } diff --git a/src/Bindings/ManualBindings_World.cpp b/src/Bindings/ManualBindings_World.cpp index e2902b81a..ba80d7130 100644 --- a/src/Bindings/ManualBindings_World.cpp +++ b/src/Bindings/ManualBindings_World.cpp @@ -110,6 +110,59 @@ static int tolua_cWorld_ChunkStay(lua_State * tolua_S) +static int tolua_cWorld_ForEachLoadedChunk(lua_State * tolua_S) +{ + // Exported manually, because tolua doesn't support converting functions to functor types. + // Function signature: ForEachLoadedChunk(callback) -> bool + + cLuaState L(tolua_S); + if ( + !L.CheckParamUserType(1, "cWorld") || + !L.CheckParamFunction(2) + ) + { + return 0; + } + + cPluginLua * Plugin = cManualBindings::GetLuaPlugin(tolua_S); + if (Plugin == nullptr) + { + return 0; + } + + // Read the params: + cWorld * World = reinterpret_cast<cWorld *>(tolua_tousertype(tolua_S, 1, nullptr)); + if (World == nullptr) + { + LOGWARNING("World:ForEachLoadedChunk(): invalid world parameter"); + L.LogStackTrace(); + return 0; + } + cLuaState::cRef FnRef; + L.GetStackValues(2, FnRef); + if (!FnRef.IsValid()) + { + return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #2"); + } + + // Call the enumeration: + bool ret = World->ForEachLoadedChunk( + [&L, &FnRef](int a_ChunkX, int a_ChunkZ) -> bool + { + bool res = false; // By default continue the enumeration + L.Call(FnRef, a_ChunkX, a_ChunkZ, cLuaState::Return, res); + return res; + } + ); + + // Push the return value: + L.Push(ret); + return 1; +} + + + + static int tolua_cWorld_GetBlockInfo(lua_State * tolua_S) { @@ -321,7 +374,6 @@ static int tolua_cWorld_PrepareChunk(lua_State * tolua_S) class cLuaWorldTask : - public cWorld::cTask, public cPluginLua::cResettable { public: @@ -331,11 +383,7 @@ public: { } -protected: - int m_FnRef; - - // cWorld::cTask overrides: - virtual void Run(cWorld & a_World) override + void Run(cWorld & a_World) { cCSLock Lock(m_CSPlugin); if (m_Plugin != nullptr) @@ -343,7 +391,10 @@ protected: m_Plugin->Call(m_FnRef, &a_World); } } -} ; + +protected: + int m_FnRef; +}; @@ -380,9 +431,9 @@ static int tolua_cWorld_QueueTask(lua_State * tolua_S) return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #1"); } - auto task = std::make_shared<cLuaWorldTask>(*Plugin, FnRef); - Plugin->AddResettable(task); - self->QueueTask(task); + auto ResettableTask = std::make_shared<cLuaWorldTask>(*Plugin, FnRef); + Plugin->AddResettable(ResettableTask); + self->QueueTask(std::bind(&cLuaWorldTask::Run, ResettableTask, std::placeholders::_1)); return 0; } @@ -430,35 +481,6 @@ static int tolua_cWorld_SetSignLines(lua_State * tolua_S) -class cLuaScheduledWorldTask : - public cWorld::cTask, - public cPluginLua::cResettable -{ -public: - cLuaScheduledWorldTask(cPluginLua & a_Plugin, int a_FnRef) : - cPluginLua::cResettable(a_Plugin), - m_FnRef(a_FnRef) - { - } - -protected: - int m_FnRef; - - // cWorld::cTask overrides: - virtual void Run(cWorld & a_World) override - { - cCSLock Lock(m_CSPlugin); - if (m_Plugin != nullptr) - { - m_Plugin->Call(m_FnRef, &a_World); - } - } -}; - - - - - static int tolua_cWorld_ScheduleTask(lua_State * tolua_S) { // Binding for cWorld::ScheduleTask @@ -495,11 +517,9 @@ static int tolua_cWorld_ScheduleTask(lua_State * tolua_S) return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #1"); } - int DelayTicks = static_cast<int>(tolua_tonumber(tolua_S, 2, 0)); - - auto task = std::make_shared<cLuaScheduledWorldTask>(*Plugin, FnRef); - Plugin->AddResettable(task); - World->ScheduleTask(DelayTicks, static_cast<cWorld::cTaskPtr>(task)); + auto ResettableTask = std::make_shared<cLuaWorldTask>(*Plugin, FnRef); + Plugin->AddResettable(ResettableTask); + World->ScheduleTask(static_cast<int>(tolua_tonumber(tolua_S, 2, 0)), std::bind(&cLuaWorldTask::Run, ResettableTask, std::placeholders::_1)); return 0; } @@ -580,6 +600,7 @@ void cManualBindings::BindWorld(lua_State * tolua_S) tolua_function(tolua_S, "ForEachEntityInChunk", ForEachInChunk<cWorld, cEntity, &cWorld::ForEachEntityInChunk>); tolua_function(tolua_S, "ForEachFurnaceInChunk", ForEachInChunk<cWorld, cFurnaceEntity, &cWorld::ForEachFurnaceInChunk>); tolua_function(tolua_S, "ForEachPlayer", ForEach< cWorld, cPlayer, &cWorld::ForEachPlayer>); + tolua_function(tolua_S, "ForEachLoadedChunk", tolua_cWorld_ForEachLoadedChunk); tolua_function(tolua_S, "GetBlockInfo", tolua_cWorld_GetBlockInfo); tolua_function(tolua_S, "GetBlockTypeMeta", tolua_cWorld_GetBlockTypeMeta); tolua_function(tolua_S, "GetSignLines", tolua_cWorld_GetSignLines); diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index 4344b36ac..1312103b8 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -14,8 +14,8 @@ #include "LuaState.h" // Names for the global variables through which the plugin is identified in its LuaState -#define LUA_PLUGIN_NAME_VAR_NAME "_MCServerInternal_PluginName" -#define LUA_PLUGIN_INSTANCE_VAR_NAME "_MCServerInternal_PluginInstance" +#define LUA_PLUGIN_NAME_VAR_NAME "_CuberiteInternal_PluginName" +#define LUA_PLUGIN_INSTANCE_VAR_NAME "_CuberiteInternal_PluginInstance" diff --git a/src/Bindings/PluginManager.cpp b/src/Bindings/PluginManager.cpp index 026865f5b..72e031b33 100644 --- a/src/Bindings/PluginManager.cpp +++ b/src/Bindings/PluginManager.cpp @@ -1761,7 +1761,7 @@ bool cPluginManager::BindConsoleCommand(const AString & a_Command, cPlugin * a_P { if (cmd->second.m_Plugin == nullptr) { - LOGWARNING("Console command \"%s\" is already bound internally by MCServer, cannot bind in plugin \"%s\".", a_Command.c_str(), a_Plugin->GetName().c_str()); + LOGWARNING("Console command \"%s\" is already bound internally by Cuberite, cannot bind in plugin \"%s\".", a_Command.c_str(), a_Plugin->GetName().c_str()); } else { |