summaryrefslogtreecommitdiffstats
path: root/src/Bindings/PluginLua.cpp
diff options
context:
space:
mode:
authordaniel0916 <theschokolps@gmail.com>2014-04-07 20:12:17 +0200
committerdaniel0916 <theschokolps@gmail.com>2014-04-07 20:12:17 +0200
commit2e9754ac1cf0537c12ab7974cf55c451c0724540 (patch)
tree713c5b8c8f22f77893b30b9c8cefca4a7c491483 /src/Bindings/PluginLua.cpp
parentFixed merge conflict (diff)
parentFixed some more minor issues with the redstone simulator. (diff)
downloadcuberite-2e9754ac1cf0537c12ab7974cf55c451c0724540.tar
cuberite-2e9754ac1cf0537c12ab7974cf55c451c0724540.tar.gz
cuberite-2e9754ac1cf0537c12ab7974cf55c451c0724540.tar.bz2
cuberite-2e9754ac1cf0537c12ab7974cf55c451c0724540.tar.lz
cuberite-2e9754ac1cf0537c12ab7974cf55c451c0724540.tar.xz
cuberite-2e9754ac1cf0537c12ab7974cf55c451c0724540.tar.zst
cuberite-2e9754ac1cf0537c12ab7974cf55c451c0724540.zip
Diffstat (limited to 'src/Bindings/PluginLua.cpp')
-rw-r--r--src/Bindings/PluginLua.cpp181
1 files changed, 164 insertions, 17 deletions
diff --git a/src/Bindings/PluginLua.cpp b/src/Bindings/PluginLua.cpp
index 4c4664815..dcc816839 100644
--- a/src/Bindings/PluginLua.cpp
+++ b/src/Bindings/PluginLua.cpp
@@ -5,7 +5,11 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
+#ifdef __APPLE__
+#define LUA_USE_MACOSX
+#else
#define LUA_USE_POSIX
+#endif
#include "PluginLua.h"
#include "../CommandOutput.h"
@@ -14,6 +18,7 @@ extern "C"
#include "lua/src/lualib.h"
}
+#undef TOLUA_TEMPLATE_BIND
#include "tolua++/include/tolua++.h"
@@ -75,6 +80,7 @@ bool cPluginLua::Initialize(void)
if (!m_LuaState.IsValid())
{
m_LuaState.Create();
+ m_LuaState.RegisterAPILibs();
// Inject the identification global variables into the state:
lua_pushlightuserdata(m_LuaState, this);
@@ -82,42 +88,69 @@ bool cPluginLua::Initialize(void)
lua_pushstring(m_LuaState, GetName().c_str());
lua_setglobal(m_LuaState, LUA_PLUGIN_NAME_VAR_NAME);
+ // Add the plugin's folder to the package.path and package.cpath variables (#693):
+ m_LuaState.AddPackagePath("path", FILE_IO_PREFIX + GetLocalFolder() + "/?.lua");
+ #ifdef _WIN32
+ m_LuaState.AddPackagePath("cpath", GetLocalFolder() + "\\?.dll");
+ #else
+ m_LuaState.AddPackagePath("cpath", FILE_IO_PREFIX + GetLocalFolder() + "/?.so");
+ #endif
+
tolua_pushusertype(m_LuaState, this, "cPluginLua");
lua_setglobal(m_LuaState, "g_Plugin");
}
std::string PluginPath = FILE_IO_PREFIX + GetLocalFolder() + "/";
- // Load all files for this plugin, and execute them
+ // List all Lua files for this plugin. Info.lua has a special handling - make it the last to load:
AStringVector Files = cFile::GetFolderContents(PluginPath.c_str());
-
- int numFiles = 0;
- for (AStringVector::const_iterator itr = Files.begin(); itr != Files.end(); ++itr)
+ AStringVector LuaFiles;
+ bool HasInfoLua = false;
+ for (AStringVector::const_iterator itr = Files.begin(), end = Files.end(); itr != end; ++itr)
{
- if (itr->rfind(".lua") == AString::npos)
+ if (itr->rfind(".lua") != AString::npos)
{
- continue;
+ if (*itr == "Info.lua")
+ {
+ HasInfoLua = true;
+ }
+ else
+ {
+ LuaFiles.push_back(*itr);
+ }
}
+ }
+ std::sort(LuaFiles.begin(), LuaFiles.end());
+
+ // Warn if there are no Lua files in the plugin folder:
+ if (LuaFiles.empty())
+ {
+ LOGWARNING("No lua files found: plugin %s is missing.", GetName().c_str());
+ Close();
+ return false;
+ }
+
+ // Load all files in the list, including the Info.lua as last, if it exists:
+ for (AStringVector::const_iterator itr = LuaFiles.begin(), end = LuaFiles.end(); itr != end; ++itr)
+ {
AString Path = PluginPath + *itr;
if (!m_LuaState.LoadFile(Path))
{
Close();
return false;
- }
- else
- {
- numFiles++;
}
} // for itr - Files[]
-
- if (numFiles == 0)
+ if (HasInfoLua)
{
- LOGWARNING("No lua files found: plugin %s is missing.", GetName().c_str());
- Close();
- return false;
+ AString Path = PluginPath + "Info.lua";
+ if (!m_LuaState.LoadFile(Path))
+ {
+ Close();
+ return false;
+ }
}
- // Call intialize function
+ // Call the Initialize function:
bool res = false;
if (!m_LuaState.Call("Initialize", this, cLuaState::Return, res))
{
@@ -125,7 +158,6 @@ bool cPluginLua::Initialize(void)
Close();
return false;
}
-
if (!res)
{
LOGINFO("Plugin %s: Initialize() call failed, plugin is temporarily disabled.", GetName().c_str());
@@ -168,6 +200,26 @@ void cPluginLua::Tick(float a_Dt)
+bool cPluginLua::OnBlockSpread(cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, eSpreadSource a_Source)
+{
+ cCSLock Lock(m_CriticalSection);
+ bool res = false;
+ cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_BLOCK_SPREAD];
+ for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
+ {
+ m_LuaState.Call((int)(**itr), a_World, a_BlockX, a_BlockY, a_BlockZ, a_Source, cLuaState::Return, res);
+ if (res)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
bool cPluginLua::OnBlockToPickups(cWorld * a_World, cEntity * a_Digger, int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, cItems & a_Pickups)
{
cCSLock Lock(m_CriticalSection);
@@ -623,6 +675,26 @@ bool cPluginLua::OnPlayerBrokenBlock(cPlayer & a_Player, int a_BlockX, int a_Blo
+bool cPluginLua::OnPlayerDestroyed(cPlayer & a_Player)
+{
+ cCSLock Lock(m_CriticalSection);
+ bool res = false;
+ cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_PLAYER_DESTROYED];
+ for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
+ {
+ m_LuaState.Call((int)(**itr), &a_Player, cLuaState::Return, res);
+ if (res)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
bool cPluginLua::OnPlayerEating(cPlayer & a_Player)
{
cCSLock Lock(m_CriticalSection);
@@ -1041,6 +1113,46 @@ bool cPluginLua::OnPreCrafting(const cPlayer * a_Player, const cCraftingGrid * a
+bool cPluginLua::OnProjectileHitBlock(cProjectileEntity & a_Projectile)
+{
+ cCSLock Lock(m_CriticalSection);
+ bool res = false;
+ cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_PROJECTILE_HIT_BLOCK];
+ for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
+ {
+ m_LuaState.Call((int)(**itr), &a_Projectile, cLuaState::Return, res);
+ if (res)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
+bool cPluginLua::OnProjectileHitEntity(cProjectileEntity & a_Projectile, cEntity & a_HitEntity)
+{
+ cCSLock Lock(m_CriticalSection);
+ bool res = false;
+ cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_PROJECTILE_HIT_ENTITY];
+ for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
+ {
+ m_LuaState.Call((int)(**itr), &a_Projectile, &a_HitEntity, cLuaState::Return, res);
+ if (res)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
bool cPluginLua::OnSpawnedEntity(cWorld & a_World, cEntity & a_Entity)
{
cCSLock Lock(m_CriticalSection);
@@ -1383,6 +1495,7 @@ const char * cPluginLua::GetHookFnName(int a_HookType)
{
switch (a_HookType)
{
+ case cPluginManager::HOOK_BLOCK_SPREAD: return "OnBlockSpread";
case cPluginManager::HOOK_BLOCK_TO_PICKUPS: return "OnBlockToPickups";
case cPluginManager::HOOK_CHAT: return "OnChat";
case cPluginManager::HOOK_CHUNK_AVAILABLE: return "OnChunkAvailable";
@@ -1469,6 +1582,40 @@ bool cPluginLua::AddHookRef(int a_HookType, int a_FnRefIdx)
+int cPluginLua::CallFunctionFromForeignState(
+ const AString & a_FunctionName,
+ cLuaState & a_ForeignState,
+ int a_ParamStart,
+ int a_ParamEnd
+)
+{
+ cCSLock Lock(m_CriticalSection);
+
+ // Call the function:
+ int NumReturns = m_LuaState.CallFunctionWithForeignParams(a_FunctionName, a_ForeignState, a_ParamStart, a_ParamEnd);
+ if (NumReturns < 0)
+ {
+ // The call has failed, an error has already been output to the log, so just silently bail out with the same error
+ return NumReturns;
+ }
+
+ // Copy all the return values:
+ int Top = lua_gettop(m_LuaState);
+ int res = a_ForeignState.CopyStackFrom(m_LuaState, Top - NumReturns + 1, Top);
+
+ // Remove the return values off this stack:
+ if (NumReturns > 0)
+ {
+ lua_pop(m_LuaState, NumReturns);
+ }
+
+ return res;
+}
+
+
+
+
+
AString cPluginLua::HandleWebRequest(const HTTPRequest * a_Request )
{
cCSLock Lock(m_CriticalSection);