From 7d70a064613e98ea25c1445c8fe68e16c94b6813 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 9 Aug 2013 15:15:56 +0200 Subject: Renamed Plugin_NewLua to PluginLua. --- source/AllToLua.pkg | 2 +- source/Bindings.cpp | 2 +- source/LuaWindow.cpp | 2 +- source/ManualBindings.cpp | 3 +- source/PluginLua.cpp | 998 ++++++++++++++++++++++++++++++++++++++++++++++ source/PluginLua.h | 143 +++++++ source/PluginManager.cpp | 2 +- source/Plugin_NewLua.cpp | 994 --------------------------------------------- source/Plugin_NewLua.h | 135 ------- 9 files changed, 1146 insertions(+), 1135 deletions(-) create mode 100644 source/PluginLua.cpp create mode 100644 source/PluginLua.h delete mode 100644 source/Plugin_NewLua.cpp delete mode 100644 source/Plugin_NewLua.h (limited to 'source') diff --git a/source/AllToLua.pkg b/source/AllToLua.pkg index 360901ecb..41cbab0f7 100644 --- a/source/AllToLua.pkg +++ b/source/AllToLua.pkg @@ -29,7 +29,7 @@ $cfile "Pawn.h" $cfile "Player.h" $cfile "PluginManager.h" $cfile "Plugin.h" -$cfile "Plugin_NewLua.h" +$cfile "PluginLua.h" $cfile "Server.h" $cfile "World.h" $cfile "Inventory.h" diff --git a/source/Bindings.cpp b/source/Bindings.cpp index ca159ab2b..4271b5541 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -29,7 +29,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S); #include "Player.h" #include "PluginManager.h" #include "Plugin.h" -#include "Plugin_NewLua.h" +#include "PluginLua.h" #include "Server.h" #include "World.h" #include "Inventory.h" diff --git a/source/LuaWindow.cpp b/source/LuaWindow.cpp index c36fabd3c..18cc7650e 100644 --- a/source/LuaWindow.cpp +++ b/source/LuaWindow.cpp @@ -6,7 +6,7 @@ #include "Globals.h" #include "LuaWindow.h" #include "UI/SlotArea.h" -#include "Plugin_NewLua.h" +#include "PluginLua.h" #include "Player.h" #include "lauxlib.h" // Needed for LUA_REFNIL diff --git a/source/ManualBindings.cpp b/source/ManualBindings.cpp index 3c52d35f7..e4d94d2b8 100644 --- a/source/ManualBindings.cpp +++ b/source/ManualBindings.cpp @@ -7,7 +7,7 @@ #include "Root.h" #include "World.h" #include "Plugin.h" -#include "Plugin_NewLua.h" +#include "PluginLua.h" #include "PluginManager.h" #include "Player.h" #include "WebAdmin.h" @@ -19,7 +19,6 @@ #include "BlockEntities/FurnaceEntity.h" #include "md5/md5.h" #include "LuaWindow.h" -#include "LuaState.h" #include "LineBlockTracer.h" diff --git a/source/PluginLua.cpp b/source/PluginLua.cpp new file mode 100644 index 000000000..8adfdbd81 --- /dev/null +++ b/source/PluginLua.cpp @@ -0,0 +1,998 @@ + +// PluginLua.cpp + +// Implements the cPluginLua class representing a plugin written in Lua + +#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules + +#define LUA_USE_POSIX +#include "PluginLua.h" +#include "CommandOutput.h" + +extern "C" +{ + #include "lualib.h" +} + +#include "tolua++.h" + + + + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cPlugin_NewLua: + +cPlugin_NewLua::cPlugin_NewLua(const AString & a_PluginDirectory) : + cPlugin(a_PluginDirectory), + m_LuaState(Printf("plugin %s", a_PluginDirectory.c_str())) +{ +} + + + + + +cPlugin_NewLua::~cPlugin_NewLua() +{ + cCSLock Lock(m_CriticalSection); + m_LuaState.Close(); +} + + + + + +bool cPlugin_NewLua::Initialize(void) +{ + cCSLock Lock(m_CriticalSection); + if (!m_LuaState.IsValid()) + { + m_LuaState.Create(); + + // Inject the identification global variables into the state: + lua_pushlightuserdata(m_LuaState, this); + lua_setglobal(m_LuaState, LUA_PLUGIN_INSTANCE_VAR_NAME); + lua_pushstring(m_LuaState, GetName().c_str()); + lua_setglobal(m_LuaState, LUA_PLUGIN_NAME_VAR_NAME); + } + + std::string PluginPath = FILE_IO_PREFIX + GetLocalDirectory() + "/"; + + // Load all files for this plugin, and execute them + AStringList Files = GetDirectoryContents(PluginPath.c_str()); + for (AStringList::const_iterator itr = Files.begin(); itr != Files.end(); ++itr) + { + if (itr->rfind(".lua") == AString::npos) + { + continue; + } + AString Path = PluginPath + *itr; + if (!m_LuaState.LoadFile(Path)) + { + m_LuaState.Close(); + return false; + } + } // for itr - Files[] + + // Call intialize function + bool res = false; + if (!m_LuaState.Call("Initialize", this, cLuaState::Return, res)) + { + LOGWARNING("Error in plugin %s: Cannot call the Initialize() function. Plugin is temporarily disabled.", GetName().c_str()); + m_LuaState.Close(); + return false; + } + + if (!res) + { + LOGINFO("Plugin %s: Initialize() call failed, plugin is temporarily disabled.", GetName().c_str()); + m_LuaState.Close(); + return false; + } + + return true; +} + + + + + +void cPlugin_NewLua::OnDisable(void) +{ + cCSLock Lock(m_CriticalSection); + if (!m_LuaState.HasFunction("OnDisable")) + { + return; + } + m_LuaState.Call("OnDisable"); +} + + + + + +void cPlugin_NewLua::Tick(float a_Dt) +{ + cCSLock Lock(m_CriticalSection); + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_TICK), a_Dt); +} + + + + + +bool cPlugin_NewLua::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); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_BLOCK_TO_PICKUPS), a_World, a_Digger, a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta, &a_Pickups, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnChat(cPlayer * a_Player, AString & a_Message) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHAT), a_Player, a_Message, cLuaState::Return, res, a_Message); + return res; +} + + + + + +bool cPlugin_NewLua::OnChunkAvailable(cWorld * a_World, int a_ChunkX, int a_ChunkZ) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_AVAILABLE), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnChunkGenerated(cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATED), a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnChunkGenerating(cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATING), a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnChunkUnloaded(cWorld * a_World, int a_ChunkX, int a_ChunkZ) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADED), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnChunkUnloading(cWorld * a_World, int a_ChunkX, int a_ChunkZ) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADING), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnCollectingPickup(cPlayer * a_Player, cPickup * a_Pickup) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_COLLECTING_PICKUP), a_Player, a_Pickup, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnCraftingNoRecipe(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CRAFTING_NO_RECIPE), (cPlayer *)a_Player, a_Grid, a_Recipe, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnDisconnect(cPlayer * a_Player, const AString & a_Reason) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_DISCONNECT), a_Player, a_Reason, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnExecuteCommand(cPlayer * a_Player, const AStringVector & a_Split) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_EXECUTE_COMMAND), a_Player, a_Split, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnExploded(cWorld & a_World, double a_ExplosionSize, bool a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + const char * FnName = GetHookFnName(cPluginManager::HOOK_EXPLODED); + switch (a_Source) + { + case esOther: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; + case esPrimedTNT: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (cTNTEntity *)a_SourceData, cLuaState::Return, res); break; + case esCreeper: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (cCreeper *)a_SourceData, cLuaState::Return, res); break; + case esBed: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (Vector3i *)a_SourceData, cLuaState::Return, res); break; + case esEnderCrystal: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (Vector3i *)a_SourceData, cLuaState::Return, res); break; + case esGhastFireball: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; + case esWitherSkullBlack: + case esWitherSkullBlue: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; + case esWitherBirth: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; + case esPlugin: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; + default: + { + ASSERT(!"Unhandled ExplosionSource"); + return false; + } + } + return res; +} + + + + + +bool cPlugin_NewLua::OnExploding(cWorld & a_World, double & a_ExplosionSize, bool & a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + const char * FnName = GetHookFnName(cPluginManager::HOOK_EXPLODING); + switch (a_Source) + { + case esOther: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; + case esPrimedTNT: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (cTNTEntity *)a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; + case esCreeper: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (cCreeper *)a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; + case esBed: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (Vector3i *)a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; + case esEnderCrystal: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (Vector3i *)a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; + case esGhastFireball: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; + case esWitherSkullBlack: + case esWitherSkullBlue: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; + case esWitherBirth: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; + case esPlugin: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; + default: + { + ASSERT(!"Unhandled ExplosionSource"); + return false; + } + } + return res; +} + + + + + +bool cPlugin_NewLua::OnHandshake(cClientHandle * a_Client, const AString & a_Username) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_HANDSHAKE), a_Client, a_Username, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnKilling(cEntity & a_Victim, cEntity * a_Killer) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_KILLING), &a_Victim, a_Killer, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnLogin(cClientHandle * a_Client, int a_ProtocolVersion, const AString & a_Username) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_LOGIN), a_Client, a_ProtocolVersion, a_Username, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerBreakingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_BREAKING_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_BlockType, a_BlockMeta, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerBrokenBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_BROKEN_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_BlockType, a_BlockMeta, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerEating(cPlayer & a_Player) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_EATING), &a_Player, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerJoined(cPlayer & a_Player) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_JOINED), &a_Player, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerLeftClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_LEFT_CLICK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerMoved(cPlayer & a_Player) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_MOVING), &a_Player, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerPlacedBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_PLACED_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerPlacingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_PLACING_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerRightClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_RIGHT_CLICK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerRightClickingEntity(cPlayer & a_Player, cEntity & a_Entity) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_RIGHT_CLICKING_ENTITY), &a_Player, &a_Entity, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerShooting(cPlayer & a_Player) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_SHOOTING), &a_Player, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerSpawned(cPlayer & a_Player) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_SPAWNED), &a_Player, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerTossingItem(cPlayer & a_Player) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_TOSSING_ITEM), &a_Player, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerUsedBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_USED_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerUsedItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_USED_ITEM), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerUsingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_USING_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPlayerUsingItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_USING_ITEM), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPostCrafting(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_POST_CRAFTING), a_Player, a_Grid, a_Recipe, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnPreCrafting(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PRE_CRAFTING), a_Player, a_Grid, a_Recipe, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnSpawnedEntity(cWorld & a_World, cEntity & a_Entity) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_SPAWNED_ENTITY), &a_World, &a_Entity, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnSpawnedMonster(cWorld & a_World, cMonster & a_Monster) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_SPAWNED_MONSTER), &a_World, &a_Monster, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnSpawningEntity(cWorld & a_World, cEntity & a_Entity) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_SPAWNING_ENTITY), &a_World, &a_Entity, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnSpawningMonster(cWorld & a_World, cMonster & a_Monster) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_SPAWNING_MONSTER), &a_World, &a_Monster, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnTakeDamage(cEntity & a_Receiver, TakeDamageInfo & a_TDI) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_TAKE_DAMAGE), &a_Receiver, &a_TDI, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnUpdatedSign( + cWorld * a_World, + int a_BlockX, int a_BlockY, int a_BlockZ, + const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4, + cPlayer * a_Player +) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_UPDATED_SIGN), a_World, a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, a_Player, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnUpdatingSign( + cWorld * a_World, + int a_BlockX, int a_BlockY, int a_BlockZ, + AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, + cPlayer * a_Player +) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_UPDATING_SIGN), a_World, a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, a_Player, cLuaState::Return, res, a_Line1, a_Line2, a_Line3, a_Line4); + return res; +} + + + + + +bool cPlugin_NewLua::OnWeatherChanged(cWorld & a_World) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_WEATHER_CHANGED), &a_World, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::OnWeatherChanging(cWorld & a_World, eWeather & a_NewWeather) +{ + cCSLock Lock(m_CriticalSection); + bool res = false; + int NewWeather = a_NewWeather; + m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_WEATHER_CHANGING), &a_World, a_NewWeather, cLuaState::Return, res, NewWeather); + a_NewWeather = (eWeather)NewWeather; + return res; +} + + + + + +bool cPlugin_NewLua::HandleCommand(const AStringVector & a_Split, cPlayer * a_Player) +{ + ASSERT(!a_Split.empty()); + CommandMap::iterator cmd = m_Commands.find(a_Split[0]); + if (cmd == m_Commands.end()) + { + LOGWARNING("Command handler is registered in cPluginManager but not in cPlugin, wtf? Command \"%s\".", a_Split[0].c_str()); + return false; + } + + cCSLock Lock(m_CriticalSection); + bool res = false; + m_LuaState.Call(cmd->second, a_Split, a_Player, cLuaState::Return, res); + return res; +} + + + + + +bool cPlugin_NewLua::HandleConsoleCommand(const AStringVector & a_Split, cCommandOutputCallback & a_Output) +{ + ASSERT(!a_Split.empty()); + CommandMap::iterator cmd = m_ConsoleCommands.find(a_Split[0]); + if (cmd == m_ConsoleCommands.end()) + { + LOGWARNING("Console command handler is registered in cPluginManager but not in cPlugin, wtf? Console command \"%s\", plugin \"%s\".", + a_Split[0].c_str(), GetName().c_str() + ); + return false; + } + + cCSLock Lock(m_CriticalSection); + bool res = false; + AString str; + m_LuaState.Call(cmd->second, a_Split, cLuaState::Return, res, str); + if (res && !str.empty()) + { + a_Output.Out(str); + } + return res; +} + + + + + +void cPlugin_NewLua::ClearCommands(void) +{ + cCSLock Lock(m_CriticalSection); + + // Unreference the bound functions so that Lua can GC them + if (m_LuaState != NULL) + { + for (CommandMap::iterator itr = m_Commands.begin(), end = m_Commands.end(); itr != end; ++itr) + { + luaL_unref(m_LuaState, LUA_REGISTRYINDEX, itr->second); + } + } + m_Commands.clear(); +} + + + + + +void cPlugin_NewLua::ClearConsoleCommands(void) +{ + cCSLock Lock(m_CriticalSection); + + // Unreference the bound functions so that Lua can GC them + if (m_LuaState != NULL) + { + for (CommandMap::iterator itr = m_ConsoleCommands.begin(), end = m_ConsoleCommands.end(); itr != end; ++itr) + { + luaL_unref(m_LuaState, LUA_REGISTRYINDEX, itr->second); + } + } + m_ConsoleCommands.clear(); +} + + + + + +bool cPlugin_NewLua::CanAddHook(cPluginManager::PluginHook a_Hook) +{ + const char * FnName = GetHookFnName(a_Hook); + if (FnName == NULL) + { + // Unknown hook ID + LOGWARNING("Plugin %s wants to add an unknown hook ID (%d). The plugin need not work properly.", GetName().c_str(), a_Hook); + return false; + } + + // Check if the function is available + if (m_LuaState.HasFunction(FnName)) + { + return true; + } + + LOGWARNING("Plugin %s wants to add a hook (%d), but it doesn't provide the callback function \"%s\" for it. The plugin need not work properly.", + GetName().c_str(), a_Hook, FnName + ); + + // Lua stacktrace: + LOGWARNING("Stack trace:"); + lua_Debug entry; + int depth = 0; + while (lua_getstack(m_LuaState, depth, &entry)) + { + int status = lua_getinfo(m_LuaState, "Sln", &entry); + assert(status); + + LOGWARNING(" %s(%d): %s", entry.short_src, entry.currentline, entry.name ? entry.name : "?"); + depth++; + } + LOGWARNING("Stack trace end"); + + return false; +} + + + + + +const char * cPlugin_NewLua::GetHookFnName(cPluginManager::PluginHook a_Hook) +{ + switch (a_Hook) + { + case cPluginManager::HOOK_BLOCK_TO_PICKUPS: return "OnBlockToPickups"; + case cPluginManager::HOOK_CHAT: return "OnChat"; + case cPluginManager::HOOK_CHUNK_AVAILABLE: return "OnChunkAvailable"; + case cPluginManager::HOOK_CHUNK_GENERATED: return "OnChunkGenerated"; + case cPluginManager::HOOK_CHUNK_GENERATING: return "OnChunkGenerating"; + case cPluginManager::HOOK_CHUNK_UNLOADED: return "OnChunkUnloaded"; + case cPluginManager::HOOK_CHUNK_UNLOADING: return "OnChunkUnloading"; + case cPluginManager::HOOK_COLLECTING_PICKUP: return "OnCollectingPickup"; + case cPluginManager::HOOK_CRAFTING_NO_RECIPE: return "OnCraftingNoRecipe"; + case cPluginManager::HOOK_DISCONNECT: return "OnDisconnect"; + case cPluginManager::HOOK_EXECUTE_COMMAND: return "OnExecuteCommand"; + case cPluginManager::HOOK_HANDSHAKE: return "OnHandshake"; + case cPluginManager::HOOK_KILLING: return "OnKilling"; + case cPluginManager::HOOK_LOGIN: return "OnLogin"; + case cPluginManager::HOOK_PLAYER_BREAKING_BLOCK: return "OnPlayerBreakingBlock"; + case cPluginManager::HOOK_PLAYER_BROKEN_BLOCK: return "OnPlayerBrokenBlock"; + case cPluginManager::HOOK_PLAYER_EATING: return "OnPlayerEating"; + case cPluginManager::HOOK_PLAYER_JOINED: return "OnPlayerJoined"; + case cPluginManager::HOOK_PLAYER_LEFT_CLICK: return "OnPlayerLeftClick"; + case cPluginManager::HOOK_PLAYER_MOVING: return "OnPlayerMoving"; + case cPluginManager::HOOK_PLAYER_PLACED_BLOCK: return "OnPlayerPlacedBlock"; + case cPluginManager::HOOK_PLAYER_PLACING_BLOCK: return "OnPlayerPlacingBlock"; + case cPluginManager::HOOK_PLAYER_RIGHT_CLICK: return "OnPlayerRightClick"; + case cPluginManager::HOOK_PLAYER_RIGHT_CLICKING_ENTITY: return "OnPlayerRightClickingEntity"; + case cPluginManager::HOOK_PLAYER_SHOOTING: return "OnPlayerShooting"; + case cPluginManager::HOOK_PLAYER_SPAWNED: return "OnPlayerSpawned"; + case cPluginManager::HOOK_PLAYER_TOSSING_ITEM: return "OnPlayerTossingItem"; + case cPluginManager::HOOK_PLAYER_USED_BLOCK: return "OnPlayerUsedBlock"; + case cPluginManager::HOOK_PLAYER_USED_ITEM: return "OnPlayerUsedItem"; + case cPluginManager::HOOK_PLAYER_USING_BLOCK: return "OnPlayerUsingBlock"; + case cPluginManager::HOOK_PLAYER_USING_ITEM: return "OnPlayerUsingItem"; + case cPluginManager::HOOK_POST_CRAFTING: return "OnPostCrafting"; + case cPluginManager::HOOK_PRE_CRAFTING: return "OnPreCrafting"; + case cPluginManager::HOOK_SPAWNED_ENTITY: return "OnSpawnedEntity"; + case cPluginManager::HOOK_SPAWNED_MONSTER: return "OnSpawnedMonster"; + case cPluginManager::HOOK_SPAWNING_ENTITY: return "OnSpawningEntity"; + case cPluginManager::HOOK_SPAWNING_MONSTER: return "OnSpawningMonster"; + case cPluginManager::HOOK_TAKE_DAMAGE: return "OnTakeDamage"; + case cPluginManager::HOOK_TICK: return "OnTick"; + case cPluginManager::HOOK_UPDATED_SIGN: return "OnUpdatedSign"; + case cPluginManager::HOOK_UPDATING_SIGN: return "OnUpdatingSign"; + case cPluginManager::HOOK_WEATHER_CHANGED: return "OnWeatherChanged"; + case cPluginManager::HOOK_WEATHER_CHANGING: return "OnWeatherChanging"; + default: return NULL; + } // switch (a_Hook) +} + + + + + +AString cPlugin_NewLua::HandleWebRequest(const HTTPRequest * a_Request ) +{ + cCSLock Lock(m_CriticalSection); + std::string RetVal = ""; + + std::pair< std::string, std::string > TabName = GetTabNameForRequest(a_Request); + std::string SafeTabName = TabName.second; + if (SafeTabName.empty()) + { + return ""; + } + + sWebPluginTab * Tab = 0; + for (TabList::iterator itr = GetTabs().begin(); itr != GetTabs().end(); ++itr) + { + if ((*itr)->SafeTitle.compare(SafeTabName) == 0) // This is the one! Rawr + { + Tab = *itr; + break; + } + } + + if (Tab != NULL) + { + AString Contents = Printf("WARNING: WebPlugin tab '%s' did not return a string!", Tab->Title.c_str()); + if (!m_LuaState.Call(Tab->UserData, a_Request, cLuaState::Return, Contents)) + { + return "Lua encountered error while processing the page request"; + } + + RetVal += Contents; + } + + return RetVal; +} + + + + + +bool cPlugin_NewLua::AddWebTab(const AString & a_Title, lua_State * a_LuaState, int a_FunctionReference) +{ + cCSLock Lock(m_CriticalSection); + if (a_LuaState != m_LuaState) + { + LOGERROR("Only allowed to add a tab to a WebPlugin of your own Plugin!"); + return false; + } + sWebPluginTab * Tab = new sWebPluginTab(); + Tab->Title = a_Title; + Tab->SafeTitle = SafeString(a_Title); + + Tab->UserData = a_FunctionReference; + + GetTabs().push_back(Tab); + return true; +} + + + + + +void cPlugin_NewLua::BindCommand(const AString & a_Command, int a_FnRef) +{ + ASSERT(m_Commands.find(a_Command) == m_Commands.end()); + m_Commands[a_Command] = a_FnRef; +} + + + + + +void cPlugin_NewLua::BindConsoleCommand(const AString & a_Command, int a_FnRef) +{ + ASSERT(m_ConsoleCommands.find(a_Command) == m_ConsoleCommands.end()); + m_ConsoleCommands[a_Command] = a_FnRef; +} + + + + + +void cPlugin_NewLua::Unreference(int a_LuaRef) +{ + cCSLock Lock(m_CriticalSection); + luaL_unref(m_LuaState, LUA_REGISTRYINDEX, a_LuaRef); +} + + + + + +bool cPlugin_NewLua::CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse) +{ + ASSERT(a_FnRef != LUA_REFNIL); + + cCSLock Lock(m_CriticalSection); + bool res; + m_LuaState.Call(a_FnRef, &a_Window, &a_Player, a_CanRefuse, cLuaState::Return, res); + return res; +} + + + + + +void cPlugin_NewLua::CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window, int a_SlotNum) +{ + ASSERT(a_FnRef != LUA_REFNIL); + + cCSLock Lock(m_CriticalSection); + m_LuaState.Call(a_FnRef, &a_Window, a_SlotNum); +} + + + + diff --git a/source/PluginLua.h b/source/PluginLua.h new file mode 100644 index 000000000..5c0ff3f0d --- /dev/null +++ b/source/PluginLua.h @@ -0,0 +1,143 @@ + +// PluginLua.h + +// Declares the cPluginLua class representing a plugin written in Lua + + + + + +#pragma once + +#include "Plugin.h" +#include "WebPlugin.h" +#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" + + + + +// fwd: UI/Window.h +class cWindow; + + + + + +// tolua_begin +class cPlugin_NewLua : + public cPlugin, + public cWebPlugin +{ +public: + // tolua_end + + cPlugin_NewLua( const AString & a_PluginDirectory ); + ~cPlugin_NewLua(); + + virtual void OnDisable(void) override; + virtual bool Initialize(void) override; + + virtual void Tick(float a_Dt) override; + + virtual bool 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) override; + virtual bool OnChat (cPlayer * a_Player, AString & a_Message) override; + virtual bool OnChunkAvailable (cWorld * a_World, int a_ChunkX, int a_ChunkZ) override; + virtual bool OnChunkGenerated (cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc) override; + virtual bool OnChunkGenerating (cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc) override; + virtual bool OnChunkUnloaded (cWorld * a_World, int a_ChunkX, int a_ChunkZ) override; + virtual bool OnChunkUnloading (cWorld * a_World, int a_ChunkX, int a_ChunkZ) override; + virtual bool OnCollectingPickup (cPlayer * a_Player, cPickup * a_Pickup) override; + virtual bool OnCraftingNoRecipe (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) override; + virtual bool OnDisconnect (cPlayer * a_Player, const AString & a_Reason) override; + virtual bool OnExecuteCommand (cPlayer * a_Player, const AStringVector & a_Split) override; + virtual bool OnExploded (cWorld & a_World, double a_ExplosionSize, bool a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) override; + virtual bool OnExploding (cWorld & a_World, double & a_ExplosionSize, bool & a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) override; + virtual bool OnHandshake (cClientHandle * a_Client, const AString & a_Username) override; + virtual bool OnKilling (cEntity & a_Victim, cEntity * a_Killer) override; + virtual bool OnLogin (cClientHandle * a_Client, int a_ProtocolVersion, const AString & a_Username) override; + virtual bool OnPlayerBreakingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; + virtual bool OnPlayerBrokenBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; + virtual bool OnPlayerEating (cPlayer & a_Player) override; + virtual bool OnPlayerJoined (cPlayer & a_Player) override; + virtual bool OnPlayerMoved (cPlayer & a_Player) override; + virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) override; + virtual bool OnPlayerPlacedBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; + virtual bool OnPlayerPlacingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; + virtual bool OnPlayerRightClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override; + virtual bool OnPlayerRightClickingEntity(cPlayer & a_Player, cEntity & a_Entity) override; + virtual bool OnPlayerShooting (cPlayer & a_Player) override; + virtual bool OnPlayerSpawned (cPlayer & a_Player) override; + virtual bool OnPlayerTossingItem (cPlayer & a_Player) override; + virtual bool OnPlayerUsedBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; + virtual bool OnPlayerUsedItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override; + virtual bool OnPlayerUsingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; + virtual bool OnPlayerUsingItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override; + virtual bool OnPostCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) override; + virtual bool OnPreCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) override; + virtual bool OnSpawnedEntity (cWorld & a_World, cEntity & a_Entity) override; + virtual bool OnSpawnedMonster (cWorld & a_World, cMonster & a_Monster) override; + virtual bool OnSpawningEntity (cWorld & a_World, cEntity & a_Entity) override; + virtual bool OnSpawningMonster (cWorld & a_World, cMonster & a_Monster) override; + virtual bool OnTakeDamage (cEntity & a_Receiver, TakeDamageInfo & a_TakeDamageInfo) override; + virtual bool OnUpdatedSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4, cPlayer * a_Player) override; + virtual bool OnUpdatingSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, cPlayer * a_Player) override; + virtual bool OnWeatherChanged (cWorld & a_World) override; + virtual bool OnWeatherChanging (cWorld & a_World, eWeather & a_NewWeather) override; + + virtual bool HandleCommand(const AStringVector & a_Split, cPlayer * a_Player) override; + + virtual bool HandleConsoleCommand(const AStringVector & a_Split, cCommandOutputCallback & a_Output) override; + + virtual void ClearCommands(void) override; + + virtual void ClearConsoleCommands(void) override; + + virtual bool CanAddHook(cPluginManager::PluginHook a_Hook) override; + + // cWebPlugin override + virtual const AString GetWebTitle(void) const {return GetName(); } + + // cWebPlugin and WebAdmin stuff + virtual AString HandleWebRequest(const HTTPRequest * a_Request ) override; + bool AddWebTab(const AString & a_Title, lua_State * a_LuaState, int a_FunctionReference); // >> EXPORTED IN MANUALBINDINGS << + + /// Binds the command to call the function specified by a Lua function reference. Simply adds to CommandMap. + void BindCommand(const AString & a_Command, int a_FnRef); + + /// Binds the console command to call the function specified by a Lua function reference. Simply adds to CommandMap. + void BindConsoleCommand(const AString & a_Command, int a_FnRef); + + cLuaState & GetLuaState(void) { return m_LuaState; } + + cCriticalSection & GetCriticalSection(void) { return m_CriticalSection; } + + /// Removes a previously referenced object (luaL_unref()) + void Unreference(int a_LuaRef); + + /// Calls the plugin-specified "cLuaWindow closing" callback. Returns true only if the callback returned true + bool CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse); + + /// Calls the plugin-specified "cLuaWindow slot changed" callback. + void CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window, int a_SlotNum); + +protected: + cCriticalSection m_CriticalSection; + cLuaState m_LuaState; + + /// Maps command name into Lua function reference + typedef std::map CommandMap; + + CommandMap m_Commands; + CommandMap m_ConsoleCommands; + + /// Returns the name of Lua function that should handle the specified hook + const char * GetHookFnName(cPluginManager::PluginHook a_Hook); +} ; // tolua_export + + + + diff --git a/source/PluginManager.cpp b/source/PluginManager.cpp index 44e47733c..27ace86b9 100644 --- a/source/PluginManager.cpp +++ b/source/PluginManager.cpp @@ -3,7 +3,7 @@ #include "PluginManager.h" #include "Plugin.h" -#include "Plugin_NewLua.h" +#include "PluginLua.h" #include "WebAdmin.h" #include "Item.h" #include "Root.h" diff --git a/source/Plugin_NewLua.cpp b/source/Plugin_NewLua.cpp deleted file mode 100644 index 051e868cf..000000000 --- a/source/Plugin_NewLua.cpp +++ /dev/null @@ -1,994 +0,0 @@ - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - -#define LUA_USE_POSIX -#include "Plugin_NewLua.h" -#include "CommandOutput.h" - -extern "C" -{ - #include "lualib.h" -} - -#include "tolua++.h" - - - - - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// cPlugin_NewLua: - -cPlugin_NewLua::cPlugin_NewLua(const AString & a_PluginDirectory) : - cPlugin(a_PluginDirectory), - m_LuaState(Printf("plugin %s", a_PluginDirectory.c_str())) -{ -} - - - - - -cPlugin_NewLua::~cPlugin_NewLua() -{ - cCSLock Lock(m_CriticalSection); - m_LuaState.Close(); -} - - - - - -bool cPlugin_NewLua::Initialize(void) -{ - cCSLock Lock(m_CriticalSection); - if (!m_LuaState.IsValid()) - { - m_LuaState.Create(); - - // Inject the identification global variables into the state: - lua_pushlightuserdata(m_LuaState, this); - lua_setglobal(m_LuaState, LUA_PLUGIN_INSTANCE_VAR_NAME); - lua_pushstring(m_LuaState, GetName().c_str()); - lua_setglobal(m_LuaState, LUA_PLUGIN_NAME_VAR_NAME); - } - - std::string PluginPath = FILE_IO_PREFIX + GetLocalDirectory() + "/"; - - // Load all files for this plugin, and execute them - AStringList Files = GetDirectoryContents(PluginPath.c_str()); - for (AStringList::const_iterator itr = Files.begin(); itr != Files.end(); ++itr) - { - if (itr->rfind(".lua") == AString::npos) - { - continue; - } - AString Path = PluginPath + *itr; - if (!m_LuaState.LoadFile(Path)) - { - m_LuaState.Close(); - return false; - } - } // for itr - Files[] - - // Call intialize function - bool res = false; - if (!m_LuaState.Call("Initialize", this, cLuaState::Return, res)) - { - LOGWARNING("Error in plugin %s: Cannot call the Initialize() function. Plugin is temporarily disabled.", GetName().c_str()); - m_LuaState.Close(); - return false; - } - - if (!res) - { - LOGINFO("Plugin %s: Initialize() call failed, plugin is temporarily disabled.", GetName().c_str()); - m_LuaState.Close(); - return false; - } - - return true; -} - - - - - -void cPlugin_NewLua::OnDisable(void) -{ - cCSLock Lock(m_CriticalSection); - if (!m_LuaState.HasFunction("OnDisable")) - { - return; - } - m_LuaState.Call("OnDisable"); -} - - - - - -void cPlugin_NewLua::Tick(float a_Dt) -{ - cCSLock Lock(m_CriticalSection); - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_TICK), a_Dt); -} - - - - - -bool cPlugin_NewLua::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); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_BLOCK_TO_PICKUPS), a_World, a_Digger, a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta, &a_Pickups, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnChat(cPlayer * a_Player, AString & a_Message) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHAT), a_Player, a_Message, cLuaState::Return, res, a_Message); - return res; -} - - - - - -bool cPlugin_NewLua::OnChunkAvailable(cWorld * a_World, int a_ChunkX, int a_ChunkZ) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_AVAILABLE), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnChunkGenerated(cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATED), a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnChunkGenerating(cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATING), a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnChunkUnloaded(cWorld * a_World, int a_ChunkX, int a_ChunkZ) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADED), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnChunkUnloading(cWorld * a_World, int a_ChunkX, int a_ChunkZ) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADING), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnCollectingPickup(cPlayer * a_Player, cPickup * a_Pickup) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_COLLECTING_PICKUP), a_Player, a_Pickup, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnCraftingNoRecipe(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CRAFTING_NO_RECIPE), (cPlayer *)a_Player, a_Grid, a_Recipe, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnDisconnect(cPlayer * a_Player, const AString & a_Reason) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_DISCONNECT), a_Player, a_Reason, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnExecuteCommand(cPlayer * a_Player, const AStringVector & a_Split) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_EXECUTE_COMMAND), a_Player, a_Split, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnExploded(cWorld & a_World, double a_ExplosionSize, bool a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - const char * FnName = GetHookFnName(cPluginManager::HOOK_EXPLODED); - switch (a_Source) - { - case esOther: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; - case esPrimedTNT: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (cTNTEntity *)a_SourceData, cLuaState::Return, res); break; - case esCreeper: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (cCreeper *)a_SourceData, cLuaState::Return, res); break; - case esBed: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (Vector3i *)a_SourceData, cLuaState::Return, res); break; - case esEnderCrystal: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (Vector3i *)a_SourceData, cLuaState::Return, res); break; - case esGhastFireball: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; - case esWitherSkullBlack: - case esWitherSkullBlue: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; - case esWitherBirth: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; - case esPlugin: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res); break; - default: - { - ASSERT(!"Unhandled ExplosionSource"); - return false; - } - } - return res; -} - - - - - -bool cPlugin_NewLua::OnExploding(cWorld & a_World, double & a_ExplosionSize, bool & a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - const char * FnName = GetHookFnName(cPluginManager::HOOK_EXPLODING); - switch (a_Source) - { - case esOther: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; - case esPrimedTNT: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (cTNTEntity *)a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; - case esCreeper: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (cCreeper *)a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; - case esBed: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (Vector3i *)a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; - case esEnderCrystal: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, (Vector3i *)a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; - case esGhastFireball: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; - case esWitherSkullBlack: - case esWitherSkullBlue: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; - case esWitherBirth: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; - case esPlugin: m_LuaState.Call(FnName, &a_World, a_ExplosionSize, a_CanCauseFire, a_X, a_Y, a_Z, a_Source, a_SourceData, cLuaState::Return, res, a_CanCauseFire, a_ExplosionSize); break; - default: - { - ASSERT(!"Unhandled ExplosionSource"); - return false; - } - } - return res; -} - - - - - -bool cPlugin_NewLua::OnHandshake(cClientHandle * a_Client, const AString & a_Username) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_HANDSHAKE), a_Client, a_Username, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnKilling(cEntity & a_Victim, cEntity * a_Killer) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_KILLING), &a_Victim, a_Killer, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnLogin(cClientHandle * a_Client, int a_ProtocolVersion, const AString & a_Username) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_LOGIN), a_Client, a_ProtocolVersion, a_Username, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerBreakingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_BREAKING_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_BlockType, a_BlockMeta, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerBrokenBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_BROKEN_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_BlockType, a_BlockMeta, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerEating(cPlayer & a_Player) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_EATING), &a_Player, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerJoined(cPlayer & a_Player) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_JOINED), &a_Player, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerLeftClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_LEFT_CLICK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerMoved(cPlayer & a_Player) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_MOVING), &a_Player, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerPlacedBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_PLACED_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerPlacingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_PLACING_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerRightClick(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_RIGHT_CLICK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerRightClickingEntity(cPlayer & a_Player, cEntity & a_Entity) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_RIGHT_CLICKING_ENTITY), &a_Player, &a_Entity, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerShooting(cPlayer & a_Player) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_SHOOTING), &a_Player, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerSpawned(cPlayer & a_Player) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_SPAWNED), &a_Player, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerTossingItem(cPlayer & a_Player) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_TOSSING_ITEM), &a_Player, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerUsedBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_USED_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerUsedItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_USED_ITEM), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerUsingBlock(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_USING_BLOCK), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, a_BlockType, a_BlockMeta, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPlayerUsingItem(cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PLAYER_USING_ITEM), &a_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPostCrafting(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_POST_CRAFTING), a_Player, a_Grid, a_Recipe, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnPreCrafting(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_PRE_CRAFTING), a_Player, a_Grid, a_Recipe, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnSpawnedEntity(cWorld & a_World, cEntity & a_Entity) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_SPAWNED_ENTITY), &a_World, &a_Entity, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnSpawnedMonster(cWorld & a_World, cMonster & a_Monster) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_SPAWNED_MONSTER), &a_World, &a_Monster, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnSpawningEntity(cWorld & a_World, cEntity & a_Entity) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_SPAWNING_ENTITY), &a_World, &a_Entity, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnSpawningMonster(cWorld & a_World, cMonster & a_Monster) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_SPAWNING_MONSTER), &a_World, &a_Monster, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnTakeDamage(cEntity & a_Receiver, TakeDamageInfo & a_TDI) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_TAKE_DAMAGE), &a_Receiver, &a_TDI, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnUpdatedSign( - cWorld * a_World, - int a_BlockX, int a_BlockY, int a_BlockZ, - const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4, - cPlayer * a_Player -) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_UPDATED_SIGN), a_World, a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, a_Player, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnUpdatingSign( - cWorld * a_World, - int a_BlockX, int a_BlockY, int a_BlockZ, - AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, - cPlayer * a_Player -) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_UPDATING_SIGN), a_World, a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, a_Player, cLuaState::Return, res, a_Line1, a_Line2, a_Line3, a_Line4); - return res; -} - - - - - -bool cPlugin_NewLua::OnWeatherChanged(cWorld & a_World) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_WEATHER_CHANGED), &a_World, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::OnWeatherChanging(cWorld & a_World, eWeather & a_NewWeather) -{ - cCSLock Lock(m_CriticalSection); - bool res = false; - int NewWeather = a_NewWeather; - m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_WEATHER_CHANGING), &a_World, a_NewWeather, cLuaState::Return, res, NewWeather); - a_NewWeather = (eWeather)NewWeather; - return res; -} - - - - - -bool cPlugin_NewLua::HandleCommand(const AStringVector & a_Split, cPlayer * a_Player) -{ - ASSERT(!a_Split.empty()); - CommandMap::iterator cmd = m_Commands.find(a_Split[0]); - if (cmd == m_Commands.end()) - { - LOGWARNING("Command handler is registered in cPluginManager but not in cPlugin, wtf? Command \"%s\".", a_Split[0].c_str()); - return false; - } - - cCSLock Lock(m_CriticalSection); - bool res = false; - m_LuaState.Call(cmd->second, a_Split, a_Player, cLuaState::Return, res); - return res; -} - - - - - -bool cPlugin_NewLua::HandleConsoleCommand(const AStringVector & a_Split, cCommandOutputCallback & a_Output) -{ - ASSERT(!a_Split.empty()); - CommandMap::iterator cmd = m_ConsoleCommands.find(a_Split[0]); - if (cmd == m_ConsoleCommands.end()) - { - LOGWARNING("Console command handler is registered in cPluginManager but not in cPlugin, wtf? Console command \"%s\", plugin \"%s\".", - a_Split[0].c_str(), GetName().c_str() - ); - return false; - } - - cCSLock Lock(m_CriticalSection); - bool res = false; - AString str; - m_LuaState.Call(cmd->second, a_Split, cLuaState::Return, res, str); - if (res && !str.empty()) - { - a_Output.Out(str); - } - return res; -} - - - - - -void cPlugin_NewLua::ClearCommands(void) -{ - cCSLock Lock(m_CriticalSection); - - // Unreference the bound functions so that Lua can GC them - if (m_LuaState != NULL) - { - for (CommandMap::iterator itr = m_Commands.begin(), end = m_Commands.end(); itr != end; ++itr) - { - luaL_unref(m_LuaState, LUA_REGISTRYINDEX, itr->second); - } - } - m_Commands.clear(); -} - - - - - -void cPlugin_NewLua::ClearConsoleCommands(void) -{ - cCSLock Lock(m_CriticalSection); - - // Unreference the bound functions so that Lua can GC them - if (m_LuaState != NULL) - { - for (CommandMap::iterator itr = m_ConsoleCommands.begin(), end = m_ConsoleCommands.end(); itr != end; ++itr) - { - luaL_unref(m_LuaState, LUA_REGISTRYINDEX, itr->second); - } - } - m_ConsoleCommands.clear(); -} - - - - - -bool cPlugin_NewLua::CanAddHook(cPluginManager::PluginHook a_Hook) -{ - const char * FnName = GetHookFnName(a_Hook); - if (FnName == NULL) - { - // Unknown hook ID - LOGWARNING("Plugin %s wants to add an unknown hook ID (%d). The plugin need not work properly.", GetName().c_str(), a_Hook); - return false; - } - - // Check if the function is available - if (m_LuaState.HasFunction(FnName)) - { - return true; - } - - LOGWARNING("Plugin %s wants to add a hook (%d), but it doesn't provide the callback function \"%s\" for it. The plugin need not work properly.", - GetName().c_str(), a_Hook, FnName - ); - - // Lua stacktrace: - LOGWARNING("Stack trace:"); - lua_Debug entry; - int depth = 0; - while (lua_getstack(m_LuaState, depth, &entry)) - { - int status = lua_getinfo(m_LuaState, "Sln", &entry); - assert(status); - - LOGWARNING(" %s(%d): %s", entry.short_src, entry.currentline, entry.name ? entry.name : "?"); - depth++; - } - LOGWARNING("Stack trace end"); - - return false; -} - - - - - -const char * cPlugin_NewLua::GetHookFnName(cPluginManager::PluginHook a_Hook) -{ - switch (a_Hook) - { - case cPluginManager::HOOK_BLOCK_TO_PICKUPS: return "OnBlockToPickups"; - case cPluginManager::HOOK_CHAT: return "OnChat"; - case cPluginManager::HOOK_CHUNK_AVAILABLE: return "OnChunkAvailable"; - case cPluginManager::HOOK_CHUNK_GENERATED: return "OnChunkGenerated"; - case cPluginManager::HOOK_CHUNK_GENERATING: return "OnChunkGenerating"; - case cPluginManager::HOOK_CHUNK_UNLOADED: return "OnChunkUnloaded"; - case cPluginManager::HOOK_CHUNK_UNLOADING: return "OnChunkUnloading"; - case cPluginManager::HOOK_COLLECTING_PICKUP: return "OnCollectingPickup"; - case cPluginManager::HOOK_CRAFTING_NO_RECIPE: return "OnCraftingNoRecipe"; - case cPluginManager::HOOK_DISCONNECT: return "OnDisconnect"; - case cPluginManager::HOOK_EXECUTE_COMMAND: return "OnExecuteCommand"; - case cPluginManager::HOOK_HANDSHAKE: return "OnHandshake"; - case cPluginManager::HOOK_KILLING: return "OnKilling"; - case cPluginManager::HOOK_LOGIN: return "OnLogin"; - case cPluginManager::HOOK_PLAYER_BREAKING_BLOCK: return "OnPlayerBreakingBlock"; - case cPluginManager::HOOK_PLAYER_BROKEN_BLOCK: return "OnPlayerBrokenBlock"; - case cPluginManager::HOOK_PLAYER_EATING: return "OnPlayerEating"; - case cPluginManager::HOOK_PLAYER_JOINED: return "OnPlayerJoined"; - case cPluginManager::HOOK_PLAYER_LEFT_CLICK: return "OnPlayerLeftClick"; - case cPluginManager::HOOK_PLAYER_MOVING: return "OnPlayerMoving"; - case cPluginManager::HOOK_PLAYER_PLACED_BLOCK: return "OnPlayerPlacedBlock"; - case cPluginManager::HOOK_PLAYER_PLACING_BLOCK: return "OnPlayerPlacingBlock"; - case cPluginManager::HOOK_PLAYER_RIGHT_CLICK: return "OnPlayerRightClick"; - case cPluginManager::HOOK_PLAYER_RIGHT_CLICKING_ENTITY: return "OnPlayerRightClickingEntity"; - case cPluginManager::HOOK_PLAYER_SHOOTING: return "OnPlayerShooting"; - case cPluginManager::HOOK_PLAYER_SPAWNED: return "OnPlayerSpawned"; - case cPluginManager::HOOK_PLAYER_TOSSING_ITEM: return "OnPlayerTossingItem"; - case cPluginManager::HOOK_PLAYER_USED_BLOCK: return "OnPlayerUsedBlock"; - case cPluginManager::HOOK_PLAYER_USED_ITEM: return "OnPlayerUsedItem"; - case cPluginManager::HOOK_PLAYER_USING_BLOCK: return "OnPlayerUsingBlock"; - case cPluginManager::HOOK_PLAYER_USING_ITEM: return "OnPlayerUsingItem"; - case cPluginManager::HOOK_POST_CRAFTING: return "OnPostCrafting"; - case cPluginManager::HOOK_PRE_CRAFTING: return "OnPreCrafting"; - case cPluginManager::HOOK_SPAWNED_ENTITY: return "OnSpawnedEntity"; - case cPluginManager::HOOK_SPAWNED_MONSTER: return "OnSpawnedMonster"; - case cPluginManager::HOOK_SPAWNING_ENTITY: return "OnSpawningEntity"; - case cPluginManager::HOOK_SPAWNING_MONSTER: return "OnSpawningMonster"; - case cPluginManager::HOOK_TAKE_DAMAGE: return "OnTakeDamage"; - case cPluginManager::HOOK_TICK: return "OnTick"; - case cPluginManager::HOOK_UPDATED_SIGN: return "OnUpdatedSign"; - case cPluginManager::HOOK_UPDATING_SIGN: return "OnUpdatingSign"; - case cPluginManager::HOOK_WEATHER_CHANGED: return "OnWeatherChanged"; - case cPluginManager::HOOK_WEATHER_CHANGING: return "OnWeatherChanging"; - default: return NULL; - } // switch (a_Hook) -} - - - - - -AString cPlugin_NewLua::HandleWebRequest(const HTTPRequest * a_Request ) -{ - cCSLock Lock(m_CriticalSection); - std::string RetVal = ""; - - std::pair< std::string, std::string > TabName = GetTabNameForRequest(a_Request); - std::string SafeTabName = TabName.second; - if (SafeTabName.empty()) - { - return ""; - } - - sWebPluginTab * Tab = 0; - for (TabList::iterator itr = GetTabs().begin(); itr != GetTabs().end(); ++itr) - { - if ((*itr)->SafeTitle.compare(SafeTabName) == 0) // This is the one! Rawr - { - Tab = *itr; - break; - } - } - - if (Tab != NULL) - { - AString Contents = Printf("WARNING: WebPlugin tab '%s' did not return a string!", Tab->Title.c_str()); - if (!m_LuaState.Call(Tab->UserData, a_Request, cLuaState::Return, Contents)) - { - return "Lua encountered error while processing the page request"; - } - - RetVal += Contents; - } - - return RetVal; -} - - - - - -bool cPlugin_NewLua::AddWebTab(const AString & a_Title, lua_State * a_LuaState, int a_FunctionReference) -{ - cCSLock Lock(m_CriticalSection); - if (a_LuaState != m_LuaState) - { - LOGERROR("Only allowed to add a tab to a WebPlugin of your own Plugin!"); - return false; - } - sWebPluginTab * Tab = new sWebPluginTab(); - Tab->Title = a_Title; - Tab->SafeTitle = SafeString(a_Title); - - Tab->UserData = a_FunctionReference; - - GetTabs().push_back(Tab); - return true; -} - - - - - -void cPlugin_NewLua::BindCommand(const AString & a_Command, int a_FnRef) -{ - ASSERT(m_Commands.find(a_Command) == m_Commands.end()); - m_Commands[a_Command] = a_FnRef; -} - - - - - -void cPlugin_NewLua::BindConsoleCommand(const AString & a_Command, int a_FnRef) -{ - ASSERT(m_ConsoleCommands.find(a_Command) == m_ConsoleCommands.end()); - m_ConsoleCommands[a_Command] = a_FnRef; -} - - - - - -void cPlugin_NewLua::Unreference(int a_LuaRef) -{ - cCSLock Lock(m_CriticalSection); - luaL_unref(m_LuaState, LUA_REGISTRYINDEX, a_LuaRef); -} - - - - - -bool cPlugin_NewLua::CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse) -{ - ASSERT(a_FnRef != LUA_REFNIL); - - cCSLock Lock(m_CriticalSection); - bool res; - m_LuaState.Call(a_FnRef, &a_Window, &a_Player, a_CanRefuse, cLuaState::Return, res); - return res; -} - - - - - -void cPlugin_NewLua::CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window, int a_SlotNum) -{ - ASSERT(a_FnRef != LUA_REFNIL); - - cCSLock Lock(m_CriticalSection); - m_LuaState.Call(a_FnRef, &a_Window, a_SlotNum); -} - - - - diff --git a/source/Plugin_NewLua.h b/source/Plugin_NewLua.h deleted file mode 100644 index dc9546c8b..000000000 --- a/source/Plugin_NewLua.h +++ /dev/null @@ -1,135 +0,0 @@ - -#pragma once - -#include "Plugin.h" -#include "WebPlugin.h" -#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" - - - - -// fwd: UI/Window.h -class cWindow; - - - - - -// tolua_begin -class cPlugin_NewLua : - public cPlugin, - public cWebPlugin -{ -public: - // tolua_end - - cPlugin_NewLua( const AString & a_PluginDirectory ); - ~cPlugin_NewLua(); - - virtual void OnDisable(void) override; - virtual bool Initialize(void) override; - - virtual void Tick(float a_Dt) override; - - virtual bool 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) override; - virtual bool OnChat (cPlayer * a_Player, AString & a_Message) override; - virtual bool OnChunkAvailable (cWorld * a_World, int a_ChunkX, int a_ChunkZ) override; - virtual bool OnChunkGenerated (cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc) override; - virtual bool OnChunkGenerating (cWorld * a_World, int a_ChunkX, int a_ChunkZ, cChunkDesc * a_ChunkDesc) override; - virtual bool OnChunkUnloaded (cWorld * a_World, int a_ChunkX, int a_ChunkZ) override; - virtual bool OnChunkUnloading (cWorld * a_World, int a_ChunkX, int a_ChunkZ) override; - virtual bool OnCollectingPickup (cPlayer * a_Player, cPickup * a_Pickup) override; - virtual bool OnCraftingNoRecipe (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) override; - virtual bool OnDisconnect (cPlayer * a_Player, const AString & a_Reason) override; - virtual bool OnExecuteCommand (cPlayer * a_Player, const AStringVector & a_Split) override; - virtual bool OnExploded (cWorld & a_World, double a_ExplosionSize, bool a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) override; - virtual bool OnExploding (cWorld & a_World, double & a_ExplosionSize, bool & a_CanCauseFire, double a_X, double a_Y, double a_Z, eExplosionSource a_Source, void * a_SourceData) override; - virtual bool OnHandshake (cClientHandle * a_Client, const AString & a_Username) override; - virtual bool OnKilling (cEntity & a_Victim, cEntity * a_Killer) override; - virtual bool OnLogin (cClientHandle * a_Client, int a_ProtocolVersion, const AString & a_Username) override; - virtual bool OnPlayerBreakingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual bool OnPlayerBrokenBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual bool OnPlayerEating (cPlayer & a_Player) override; - virtual bool OnPlayerJoined (cPlayer & a_Player) override; - virtual bool OnPlayerMoved (cPlayer & a_Player) override; - virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) override; - virtual bool OnPlayerPlacedBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual bool OnPlayerPlacingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual bool OnPlayerRightClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override; - virtual bool OnPlayerRightClickingEntity(cPlayer & a_Player, cEntity & a_Entity) override; - virtual bool OnPlayerShooting (cPlayer & a_Player) override; - virtual bool OnPlayerSpawned (cPlayer & a_Player) override; - virtual bool OnPlayerTossingItem (cPlayer & a_Player) override; - virtual bool OnPlayerUsedBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual bool OnPlayerUsedItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override; - virtual bool OnPlayerUsingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; - virtual bool OnPlayerUsingItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override; - virtual bool OnPostCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) override; - virtual bool OnPreCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) override; - virtual bool OnSpawnedEntity (cWorld & a_World, cEntity & a_Entity) override; - virtual bool OnSpawnedMonster (cWorld & a_World, cMonster & a_Monster) override; - virtual bool OnSpawningEntity (cWorld & a_World, cEntity & a_Entity) override; - virtual bool OnSpawningMonster (cWorld & a_World, cMonster & a_Monster) override; - virtual bool OnTakeDamage (cEntity & a_Receiver, TakeDamageInfo & a_TakeDamageInfo) override; - virtual bool OnUpdatedSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4, cPlayer * a_Player) override; - virtual bool OnUpdatingSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, cPlayer * a_Player) override; - virtual bool OnWeatherChanged (cWorld & a_World) override; - virtual bool OnWeatherChanging (cWorld & a_World, eWeather & a_NewWeather) override; - - virtual bool HandleCommand(const AStringVector & a_Split, cPlayer * a_Player) override; - - virtual bool HandleConsoleCommand(const AStringVector & a_Split, cCommandOutputCallback & a_Output) override; - - virtual void ClearCommands(void) override; - - virtual void ClearConsoleCommands(void) override; - - virtual bool CanAddHook(cPluginManager::PluginHook a_Hook) override; - - // cWebPlugin override - virtual const AString GetWebTitle(void) const {return GetName(); } - - // cWebPlugin and WebAdmin stuff - virtual AString HandleWebRequest(const HTTPRequest * a_Request ) override; - bool AddWebTab(const AString & a_Title, lua_State * a_LuaState, int a_FunctionReference); // >> EXPORTED IN MANUALBINDINGS << - - /// Binds the command to call the function specified by a Lua function reference. Simply adds to CommandMap. - void BindCommand(const AString & a_Command, int a_FnRef); - - /// Binds the console command to call the function specified by a Lua function reference. Simply adds to CommandMap. - void BindConsoleCommand(const AString & a_Command, int a_FnRef); - - cLuaState & GetLuaState(void) { return m_LuaState; } - - cCriticalSection & GetCriticalSection(void) { return m_CriticalSection; } - - /// Removes a previously referenced object (luaL_unref()) - void Unreference(int a_LuaRef); - - /// Calls the plugin-specified "cLuaWindow closing" callback. Returns true only if the callback returned true - bool CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse); - - /// Calls the plugin-specified "cLuaWindow slot changed" callback. - void CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window, int a_SlotNum); - -protected: - cCriticalSection m_CriticalSection; - cLuaState m_LuaState; - - /// Maps command name into Lua function reference - typedef std::map CommandMap; - - CommandMap m_Commands; - CommandMap m_ConsoleCommands; - - /// Returns the name of Lua function that should handle the specified hook - const char * GetHookFnName(cPluginManager::PluginHook a_Hook); -} ; // tolua_export - - - - -- cgit v1.2.3 From 7c9c51425b465a0a7ad19438609ef4cdd832a839 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 9 Aug 2013 15:53:43 +0200 Subject: Fixed squid crashing the server when above or below the world --- source/Mobs/Squid.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/Mobs/Squid.cpp b/source/Mobs/Squid.cpp index 76b1ed94f..cb796f5ec 100644 --- a/source/Mobs/Squid.cpp +++ b/source/Mobs/Squid.cpp @@ -36,9 +36,14 @@ void cSquid::Tick(float a_Dt, cChunk & a_Chunk) Vector3d Pos = GetPosition(); // TODO: Not a real behavior, but cool :D + int RelY = (int)floor(Pos.y); + if ((RelY < 0) || (RelY >= cChunkDef::Height)) + { + return; + } int RelX = (int)floor(Pos.x) - a_Chunk.GetPosX() * cChunkDef::Width; int RelZ = (int)floor(Pos.z) - a_Chunk.GetPosZ() * cChunkDef::Width; - if (!IsBlockWater(a_Chunk.GetBlock(RelX, (int)floor(Pos.y), RelZ)) && !IsOnFire()) + if (!IsBlockWater(a_Chunk.GetBlock(RelX, RelY, RelZ)) && !IsOnFire()) { // Burn for 10 ticks, then decide again StartBurning(10); -- cgit v1.2.3 From f89d6cc9afdafe0092cf837084fffefe69e1b19a Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 9 Aug 2013 16:20:12 +0200 Subject: Piston head, when removed, removes the associated piston body, too. Fixes FS 388 --- source/Blocks/BlockHandler.cpp | 1 + source/Blocks/BlockPiston.cpp | 35 ++++++++++++++++++++++++++++++++++- source/Blocks/BlockPiston.h | 15 +++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/Blocks/BlockHandler.cpp b/source/Blocks/BlockHandler.cpp index 8978f4d46..b4fc5ab2d 100644 --- a/source/Blocks/BlockHandler.cpp +++ b/source/Blocks/BlockHandler.cpp @@ -149,6 +149,7 @@ cBlockHandler * cBlockHandler::CreateBlockHandler(BLOCKTYPE a_BlockType) case E_BLOCK_NETHER_BRICK_STAIRS: return new cBlockStairsHandler (a_BlockType); case E_BLOCK_NOTE_BLOCK: return new cBlockNoteHandler (a_BlockType); case E_BLOCK_PISTON: return new cBlockPistonHandler (a_BlockType); + case E_BLOCK_PISTON_EXTENSION: return new cBlockPistonHeadHandler (); case E_BLOCK_PLANKS: return new cBlockWoodHandler (a_BlockType); case E_BLOCK_PUMPKIN_STEM: return new cBlockStemsHandler (a_BlockType); case E_BLOCK_RAIL: return new cBlockRailHandler (a_BlockType); diff --git a/source/Blocks/BlockPiston.cpp b/source/Blocks/BlockPiston.cpp index d0f90559e..1bfac3ca1 100644 --- a/source/Blocks/BlockPiston.cpp +++ b/source/Blocks/BlockPiston.cpp @@ -35,7 +35,7 @@ cBlockPistonHandler::cBlockPistonHandler(BLOCKTYPE a_BlockType) void cBlockPistonHandler::OnDestroyed(cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ) { - char OldMeta = a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + NIBBLETYPE OldMeta = a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); int newX = a_BlockX; int newY = a_BlockY; @@ -67,3 +67,36 @@ bool cBlockPistonHandler::GetPlacementBlockTypeMeta( + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cBlockPistonHeadHandler: + +cBlockPistonHeadHandler::cBlockPistonHeadHandler(void) : + super(E_BLOCK_PISTON_EXTENSION) +{ +} + + + + + +void cBlockPistonHeadHandler::OnDestroyedByPlayer(cWorld * a_World, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ) +{ + NIBBLETYPE OldMeta = a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + + int newX = a_BlockX; + int newY = a_BlockY; + int newZ = a_BlockZ; + AddPistonDir(newX, newY, newZ, OldMeta & ~(8), -1); + + BLOCKTYPE Block = a_World->GetBlock(newX, newY, newZ); + if ((Block == E_BLOCK_STICKY_PISTON) || (Block == E_BLOCK_PISTON)) + { + a_World->DigBlock(newX, newY, newZ); + } +} + + + + + diff --git a/source/Blocks/BlockPiston.h b/source/Blocks/BlockPiston.h index ef6a4fa97..109f5ea8b 100644 --- a/source/Blocks/BlockPiston.h +++ b/source/Blocks/BlockPiston.h @@ -26,3 +26,18 @@ public: + +class cBlockPistonHeadHandler : + public cBlockHandler +{ + typedef cBlockHandler super; + +public: + cBlockPistonHeadHandler(void); + + virtual void OnDestroyedByPlayer(cWorld * a_World, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ) override; +} ; + + + + -- cgit v1.2.3 From 8fd67cf12a584835e74c1f7a292c30c793e843e1 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 9 Aug 2013 22:40:12 +0200 Subject: First attempt at Linux console colors Ref.: issue #41 --- source/MCLogger.cpp | 66 +++++++++++++++++++++++++++-------------------------- source/MCLogger.h | 17 ++++++++++---- 2 files changed, 47 insertions(+), 36 deletions(-) (limited to 'source') diff --git a/source/MCLogger.cpp b/source/MCLogger.cpp index b0ec4efd5..dd15051c2 100644 --- a/source/MCLogger.cpp +++ b/source/MCLogger.cpp @@ -87,71 +87,73 @@ void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ ) -void cMCLogger::Log(const char* a_Format, va_list a_ArgList) +void cMCLogger::Log(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor( 0x7 ); // 0x7 is default grey color - m_Log->Log( a_Format, a_ArgList ); - SetColor(0x07); // revert color back + SetColor(csGrayOnBlack); + m_Log->Log(a_Format, a_ArgList); } -void cMCLogger::Info(const char* a_Format, va_list a_ArgList) +void cMCLogger::Info(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); -// for( int i = 0; i < 16; i++) -// { -// for( int j = 0; j < 16; j++ ) -// { -// SetConsoleTextAttribute( hConsole, i | (j<<4) ); -// printf("0x%x", (i|j<<4)); -// } -// printf("\n"); -// } - - SetColor( 0xe ); // 0xe is yellow - m_Log->Log( a_Format, a_ArgList ); - SetColor(0x07); // revert color back + SetColor(csYellowOnBlack); + m_Log->Log(a_Format, a_ArgList); } -void cMCLogger::Warn(const char* a_Format, va_list a_ArgList) +void cMCLogger::Warn(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor( 0xc ); // 0xc is red - m_Log->Log( a_Format, a_ArgList ); - SetColor(0x07); // revert color back + SetColor(csRedOnBlack); + m_Log->Log(a_Format, a_ArgList); } -void cMCLogger::Error(const char* a_Format, va_list a_ArgList) +void cMCLogger::Error(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor( 0xc0 ); // 0xc0 is red bg and black text - m_Log->Log( a_Format, a_ArgList ); - SetColor(0x07); // revert color back + SetColor(csBlackOnRed); + m_Log->Log(a_Format, a_ArgList); } -void cMCLogger::SetColor( unsigned char a_Color ) +void cMCLogger::SetColor(eColorScheme a_Scheme) { -#ifdef _WIN32 - SetConsoleTextAttribute(g_Console, a_Color); -#else - (void)a_Color; -#endif + #ifdef _WIN32 + WORD Attrib = 0x07; // by default, gray on black + switch (a_Scheme) + { + case csGrayOnBlack: Attrib = 0x07; break; + case csYellowOnBlack: Attrib = 0x0e; break; + case csRedOnBlack: Attrib = 0x0c; break; + case csBlackOnRed: Attrib = 0xc0; break; + default: ASSERT(!"Unhandled color scheme"); + } + SetConsoleTextAttribute(g_Console, Attrib); + #elif defined(LINUX) && !defined(ANDROID_NDK) + switch (a_Scheme) + { + case csGrayOnBlack: puts("\x1b[0m"); break; + case csYellowOnBlack: puts("\x1b[33;1m"); break; + case csRedOnBlack: puts("\x1b[31;1m"); break; + case csBlackOnRed: puts("\x1b[7;31;1m"); break; + default: ASSERT(!"Unhandled color scheme"); + } + #endif } diff --git a/source/MCLogger.h b/source/MCLogger.h index 0c4566400..158ccc6b6 100644 --- a/source/MCLogger.h +++ b/source/MCLogger.h @@ -26,11 +26,20 @@ public: // tolua_export static cMCLogger* GetInstance(); private: - void SetColor( unsigned char a_Color ); - + enum eColorScheme + { + csGrayOnBlack, + csYellowOnBlack, + csRedOnBlack, + csBlackOnRed, + } ; + + /// Sets the specified color scheme in the terminal (TODO: if coloring available) + void SetColor(eColorScheme a_Scheme); + cCriticalSection m_CriticalSection; - cLog* m_Log; - static cMCLogger* s_MCLogger; + cLog * m_Log; + static cMCLogger * s_MCLogger; }; // tolua_export extern void LOG(const char* a_Format, ...); -- cgit v1.2.3 From 2a97bed3b4831d1fe1000d1c75c068a942020b14 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 10 Aug 2013 09:42:20 +0200 Subject: Bad preprocesor used for discovering Linux --- source/MCLogger.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source') diff --git a/source/MCLogger.cpp b/source/MCLogger.cpp index dd15051c2..e6fc86775 100644 --- a/source/MCLogger.cpp +++ b/source/MCLogger.cpp @@ -144,7 +144,7 @@ void cMCLogger::SetColor(eColorScheme a_Scheme) default: ASSERT(!"Unhandled color scheme"); } SetConsoleTextAttribute(g_Console, Attrib); - #elif defined(LINUX) && !defined(ANDROID_NDK) + #elif defined(__linux) && !defined(ANDROID_NDK) switch (a_Scheme) { case csGrayOnBlack: puts("\x1b[0m"); break; -- cgit v1.2.3 From 528feaba2c4df71912cd6734a0beec569b7a7d26 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 10 Aug 2013 09:45:05 +0200 Subject: Use printf() instead of puts(). puts() outputs an extra newline, making it unsuitable --- source/MCLogger.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source') diff --git a/source/MCLogger.cpp b/source/MCLogger.cpp index e6fc86775..78665f9d2 100644 --- a/source/MCLogger.cpp +++ b/source/MCLogger.cpp @@ -147,10 +147,10 @@ void cMCLogger::SetColor(eColorScheme a_Scheme) #elif defined(__linux) && !defined(ANDROID_NDK) switch (a_Scheme) { - case csGrayOnBlack: puts("\x1b[0m"); break; - case csYellowOnBlack: puts("\x1b[33;1m"); break; - case csRedOnBlack: puts("\x1b[31;1m"); break; - case csBlackOnRed: puts("\x1b[7;31;1m"); break; + case csGrayOnBlack: printf("\x1b[0m"); break; + case csYellowOnBlack: printf("\x1b[33;1m"); break; + case csRedOnBlack: printf("\x1b[31;1m"); break; + case csBlackOnRed: printf("\x1b[7;31;1m"); break; default: ASSERT(!"Unhandled color scheme"); } #endif -- cgit v1.2.3 From 63b2fa21e8c7891da5f232e79f41b845d17c6e3d Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 10 Aug 2013 12:44:39 +0200 Subject: Disabled coloring for redirected output. --- source/MCLogger.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++---------- source/MCLogger.h | 11 +++++--- 2 files changed, 66 insertions(+), 17 deletions(-) (limited to 'source') diff --git a/source/MCLogger.cpp b/source/MCLogger.cpp index 78665f9d2..ae411302f 100644 --- a/source/MCLogger.cpp +++ b/source/MCLogger.cpp @@ -2,6 +2,7 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include +#include #include "Log.h" @@ -9,15 +10,20 @@ cMCLogger * cMCLogger::s_MCLogger = NULL; +bool g_ShouldColorOutput = false; #ifdef _WIN32 HANDLE g_Console = GetStdHandle(STD_OUTPUT_HANDLE); + WORD g_DefaultConsoleAttrib = 0x07; +#elif defined (__linux) && !defined(ANDROID_NDK) + bool g_ShouldColorOutput; #endif -cMCLogger* cMCLogger::GetInstance() + +cMCLogger * cMCLogger::GetInstance(void) { return s_MCLogger; } @@ -34,6 +40,20 @@ cMCLogger::cMCLogger(void) m_Log->Log("--- Started Log ---"); s_MCLogger = this; + + #ifdef _WIN32 + // See whether we are writing to a console the default console attrib: + g_ShouldColorOutput = (_isatty(_fileno(stdin)) != 0); + if (g_ShouldColorOutput) + { + CONSOLE_SCREEN_BUFFER_INFO sbi; + GetConsoleScreenBufferInfo(g_Console, &sbi); + g_DefaultConsoleAttrib = sbi.wAttributes; + } + #elif defined (__linux) && !defined(ANDROID_NDK) + g_ShouldColorOutput = isatty(fileno(stdin)); + // TODO: Check if the terminal supports colors, somehow? + #endif } @@ -90,8 +110,9 @@ void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ ) void cMCLogger::Log(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor(csGrayOnBlack); + SetColor(csRegular); m_Log->Log(a_Format, a_ArgList); + ResetColor(); } @@ -101,8 +122,9 @@ void cMCLogger::Log(const char * a_Format, va_list a_ArgList) void cMCLogger::Info(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor(csYellowOnBlack); + SetColor(csInfo); m_Log->Log(a_Format, a_ArgList); + ResetColor(); } @@ -112,8 +134,9 @@ void cMCLogger::Info(const char * a_Format, va_list a_ArgList) void cMCLogger::Warn(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor(csRedOnBlack); + SetColor(csWarning); m_Log->Log(a_Format, a_ArgList); + ResetColor(); } @@ -123,8 +146,9 @@ void cMCLogger::Warn(const char * a_Format, va_list a_ArgList) void cMCLogger::Error(const char * a_Format, va_list a_ArgList) { cCSLock Lock(m_CriticalSection); - SetColor(csBlackOnRed); + SetColor(csError); m_Log->Log(a_Format, a_ArgList); + ResetColor(); } @@ -133,24 +157,28 @@ void cMCLogger::Error(const char * a_Format, va_list a_ArgList) void cMCLogger::SetColor(eColorScheme a_Scheme) { + if (!g_ShouldColorOutput) + { + return; + } #ifdef _WIN32 WORD Attrib = 0x07; // by default, gray on black switch (a_Scheme) { - case csGrayOnBlack: Attrib = 0x07; break; - case csYellowOnBlack: Attrib = 0x0e; break; - case csRedOnBlack: Attrib = 0x0c; break; - case csBlackOnRed: Attrib = 0xc0; break; + case csRegular: Attrib = 0x07; break; // Gray on black + case csInfo: Attrib = 0x0e; break; // Yellow on black + case csWarning: Attrib = 0x0c; break; // Read on black + case csError: Attrib = 0xc0; break; // Black on red default: ASSERT(!"Unhandled color scheme"); } SetConsoleTextAttribute(g_Console, Attrib); #elif defined(__linux) && !defined(ANDROID_NDK) switch (a_Scheme) { - case csGrayOnBlack: printf("\x1b[0m"); break; - case csYellowOnBlack: printf("\x1b[33;1m"); break; - case csRedOnBlack: printf("\x1b[31;1m"); break; - case csBlackOnRed: printf("\x1b[7;31;1m"); break; + case csRegular: printf("\x1b[0m"); break; // Whatever the console default is + case csInfo: printf("\x1b[33;1m"); break; // Yellow on black + case csWarning: printf("\x1b[31;1m"); break; // Red on black + case csError: printf("\x1b[1;33;41;1m"); break; // Yellow on red default: ASSERT(!"Unhandled color scheme"); } #endif @@ -160,8 +188,26 @@ void cMCLogger::SetColor(eColorScheme a_Scheme) +void cMCLogger::ResetColor(void) +{ + if (!g_ShouldColorOutput) + { + return; + } + #ifdef _WIN32 + SetConsoleTextAttribute(g_Console, g_DefaultConsoleAttrib); + #elif defined(__linux) && !defined(ANDROID_NDK) + printf("\x1b[0m"); + #endif +} + + + + + ////////////////////////////////////////////////////////////////////////// // Global functions + void LOG(const char* a_Format, ...) { va_list argList; diff --git a/source/MCLogger.h b/source/MCLogger.h index 158ccc6b6..6f7d34e66 100644 --- a/source/MCLogger.h +++ b/source/MCLogger.h @@ -28,15 +28,18 @@ public: // tolua_export private: enum eColorScheme { - csGrayOnBlack, - csYellowOnBlack, - csRedOnBlack, - csBlackOnRed, + csRegular, + csInfo, + csWarning, + csError, } ; /// Sets the specified color scheme in the terminal (TODO: if coloring available) void SetColor(eColorScheme a_Scheme); + /// Resets the color back to whatever is the default in the terminal + void ResetColor(void); + cCriticalSection m_CriticalSection; cLog * m_Log; static cMCLogger * s_MCLogger; -- cgit v1.2.3 From 211a8395d4e8f572ac98b753530a1e6e060bf983 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 10 Aug 2013 12:50:23 +0200 Subject: Fixed Linux build. --- source/MCLogger.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/MCLogger.cpp b/source/MCLogger.cpp index ae411302f..99c1533ac 100644 --- a/source/MCLogger.cpp +++ b/source/MCLogger.cpp @@ -2,7 +2,6 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include -#include #include "Log.h" @@ -13,6 +12,8 @@ cMCLogger * cMCLogger::s_MCLogger = NULL; bool g_ShouldColorOutput = false; #ifdef _WIN32 + #include // Needed for _isatty(), not available on Linux + HANDLE g_Console = GetStdHandle(STD_OUTPUT_HANDLE); WORD g_DefaultConsoleAttrib = 0x07; #elif defined (__linux) && !defined(ANDROID_NDK) -- cgit v1.2.3 From 9794496571b1bd008205049a3abdb9143dc645f5 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 10 Aug 2013 12:51:57 +0200 Subject: Another Linux compilation fix. --- source/MCLogger.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source') diff --git a/source/MCLogger.cpp b/source/MCLogger.cpp index 99c1533ac..80cbf7406 100644 --- a/source/MCLogger.cpp +++ b/source/MCLogger.cpp @@ -17,6 +17,8 @@ bool g_ShouldColorOutput = false; HANDLE g_Console = GetStdHandle(STD_OUTPUT_HANDLE); WORD g_DefaultConsoleAttrib = 0x07; #elif defined (__linux) && !defined(ANDROID_NDK) + #include // Needed for isatty() on Linux + bool g_ShouldColorOutput; #endif -- cgit v1.2.3 From 19781ecad53c37adc99c4f9b3044b2fb956071c5 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 10 Aug 2013 13:03:05 +0200 Subject: Coloring is reset before the LF. --- source/Log.cpp | 6 +++--- source/MCLogger.cpp | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'source') diff --git a/source/Log.cpp b/source/Log.cpp index 5f78bba1b..2b505de5d 100644 --- a/source/Log.cpp +++ b/source/Log.cpp @@ -117,13 +117,13 @@ void cLog::Log(const char * a_Format, va_list argList) AString Line; #ifdef _DEBUG - Printf(Line, "[%04x|%02d:%02d:%02d] %s\n", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str()); + Printf(Line, "[%04x|%02d:%02d:%02d] %s", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str()); #else - Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str()); + Printf(Line, "[%02d:%02d:%02d] %s", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str()); #endif if (m_File) { - fputs(Line.c_str(), m_File); + fprintf(m_File, "%s\n", Line.c_str(), m_File); fflush(m_File); } diff --git a/source/MCLogger.cpp b/source/MCLogger.cpp index 80cbf7406..4442fc5b0 100644 --- a/source/MCLogger.cpp +++ b/source/MCLogger.cpp @@ -18,8 +18,6 @@ bool g_ShouldColorOutput = false; WORD g_DefaultConsoleAttrib = 0x07; #elif defined (__linux) && !defined(ANDROID_NDK) #include // Needed for isatty() on Linux - - bool g_ShouldColorOutput; #endif @@ -40,7 +38,7 @@ cMCLogger::cMCLogger(void) AString FileName; Printf(FileName, "LOG_%d.txt", (int)time(NULL)); m_Log = new cLog(FileName); - m_Log->Log("--- Started Log ---"); + m_Log->Log("--- Started Log ---\n"); s_MCLogger = this; @@ -74,7 +72,7 @@ cMCLogger::cMCLogger(const AString & a_FileName) cMCLogger::~cMCLogger() { - m_Log->Log("--- Stopped Log ---"); + m_Log->Log("--- Stopped Log ---\n"); delete m_Log; if (this == s_MCLogger) s_MCLogger = NULL; @@ -116,6 +114,7 @@ void cMCLogger::Log(const char * a_Format, va_list a_ArgList) SetColor(csRegular); m_Log->Log(a_Format, a_ArgList); ResetColor(); + puts(""); } @@ -128,6 +127,7 @@ void cMCLogger::Info(const char * a_Format, va_list a_ArgList) SetColor(csInfo); m_Log->Log(a_Format, a_ArgList); ResetColor(); + puts(""); } @@ -140,6 +140,7 @@ void cMCLogger::Warn(const char * a_Format, va_list a_ArgList) SetColor(csWarning); m_Log->Log(a_Format, a_ArgList); ResetColor(); + puts(""); } @@ -152,6 +153,7 @@ void cMCLogger::Error(const char * a_Format, va_list a_ArgList) SetColor(csError); m_Log->Log(a_Format, a_ArgList); ResetColor(); + puts(""); } -- cgit v1.2.3 From 15f80e0811fbc3a842eb29a2f203e107102935c8 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 10 Aug 2013 13:07:49 +0200 Subject: Linux color redirection fixed. Outputting to stdin, yeah, right, that's gonna work :P --- source/MCLogger.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source') diff --git a/source/MCLogger.cpp b/source/MCLogger.cpp index 4442fc5b0..de0fcae2e 100644 --- a/source/MCLogger.cpp +++ b/source/MCLogger.cpp @@ -52,7 +52,7 @@ cMCLogger::cMCLogger(void) g_DefaultConsoleAttrib = sbi.wAttributes; } #elif defined (__linux) && !defined(ANDROID_NDK) - g_ShouldColorOutput = isatty(fileno(stdin)); + g_ShouldColorOutput = isatty(fileno(stdout)); // TODO: Check if the terminal supports colors, somehow? #endif } -- cgit v1.2.3