1
0

LuaWindow: Fixed memory leaks with unclosed windows

A player disconnecting will now force all windows to close, not respecting any plugin's overrides.
http://forum.mc-server.org/showthread.php?tid=1146&pid=8350#pid8350

git-svn-id: http://mc-server.googlecode.com/svn/trunk@1543 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2013-06-02 21:59:25 +00:00
parent d2b3011c05
commit 8ff22344af
9 changed files with 22 additions and 21 deletions

View File

@ -582,10 +582,10 @@ function HandleTestWndCmd(a_Split, a_Player)
-- Test out the OnClosing callback's ability to refuse to close the window -- Test out the OnClosing callback's ability to refuse to close the window
local attempt = 1; local attempt = 1;
local OnClosing = function(Window, Player) local OnClosing = function(Window, Player, CanRefuse)
Player:SendMessage("Window closing attempt #" .. attempt); Player:SendMessage("Window closing attempt #" .. attempt .. "; CanRefuse = " .. tostring(CanRefuse));
attempt = attempt + 1; attempt = attempt + 1;
return (attempt <= 3); -- refuse twice, then allow return CanRefuse and (attempt <= 3); -- refuse twice, then allow, unless CanRefuse is set to true
end end
-- Log the slot changes -- Log the slot changes

View File

@ -117,20 +117,20 @@ void cLuaWindow::SetOnSlotChanged(cPlugin_NewLua * a_Plugin, int a_FnRef)
bool cLuaWindow::ClosedByPlayer(cPlayer & a_Player) bool cLuaWindow::ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse)
{ {
// First notify the plugin through the registered callback: // First notify the plugin through the registered callback:
if (m_OnClosingFnRef != LUA_REFNIL) if (m_OnClosingFnRef != LUA_REFNIL)
{ {
ASSERT(m_Plugin != NULL); ASSERT(m_Plugin != NULL);
if (m_Plugin->CallbackWindowClosing(m_OnClosingFnRef, *this, a_Player)) if (m_Plugin->CallbackWindowClosing(m_OnClosingFnRef, *this, a_Player, a_CanRefuse))
{ {
// The callback disagrees // The callback disagrees (the higher levels check the CanRefuse flag compliance)
return false; return false;
} }
} }
return super::ClosedByPlayer(a_Player); return super::ClosedByPlayer(a_Player, a_CanRefuse);
} }

View File

@ -83,7 +83,7 @@ protected:
int m_OnSlotChangedFnRef; int m_OnSlotChangedFnRef;
// cWindow overrides: // cWindow overrides:
virtual bool ClosedByPlayer(cPlayer & a_Player) override; virtual bool ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse) override;
virtual void Destroy(void) override; virtual void Destroy(void) override;
// cItemGrid::cListener overrides: // cItemGrid::cListener overrides:

View File

@ -128,7 +128,7 @@ void cPlayer::Initialize( cWorld* a_World )
void cPlayer::Destroyed() void cPlayer::Destroyed()
{ {
CloseWindow(); CloseWindow(false);
m_ClientHandle = NULL; m_ClientHandle = NULL;
} }
@ -437,7 +437,7 @@ void cPlayer::OpenWindow(cWindow * a_Window)
{ {
if (a_Window != m_CurrentWindow) if (a_Window != m_CurrentWindow)
{ {
CloseWindow(); CloseWindow(false);
} }
a_Window->OpenedByPlayer(*this); a_Window->OpenedByPlayer(*this);
m_CurrentWindow = a_Window; m_CurrentWindow = a_Window;
@ -448,7 +448,7 @@ void cPlayer::OpenWindow(cWindow * a_Window)
void cPlayer::CloseWindow(void) void cPlayer::CloseWindow(bool a_CanRefuse)
{ {
if (m_CurrentWindow == NULL) if (m_CurrentWindow == NULL)
{ {
@ -456,7 +456,7 @@ void cPlayer::CloseWindow(void)
return; return;
} }
if (m_CurrentWindow->ClosedByPlayer(*this)) if (m_CurrentWindow->ClosedByPlayer(*this, a_CanRefuse) || !a_CanRefuse)
{ {
// Close accepted, go back to inventory window (the default): // Close accepted, go back to inventory window (the default):
m_CurrentWindow = m_InventoryWindow; m_CurrentWindow = m_InventoryWindow;
@ -473,7 +473,7 @@ void cPlayer::CloseWindow(void)
void cPlayer::CloseWindowIfID(char a_WindowID) void cPlayer::CloseWindowIfID(char a_WindowID, bool a_CanRefuse)
{ {
if ((m_CurrentWindow == NULL) || (m_CurrentWindow->GetWindowID() != a_WindowID)) if ((m_CurrentWindow == NULL) || (m_CurrentWindow->GetWindowID() != a_WindowID))
{ {

View File

@ -93,11 +93,11 @@ public:
// tolua_begin // tolua_begin
/// Closes the current window, resets current window to m_InventoryWindow /// Closes the current window, resets current window to m_InventoryWindow. A plugin may refuse the closing if a_CanRefuse is true
void CloseWindow(void); void CloseWindow(bool a_CanRefuse = true);
/// Closes the current window if it matches the specified ID, resets current window to m_InventoryWindow /// Closes the current window if it matches the specified ID, resets current window to m_InventoryWindow
void CloseWindowIfID(char a_WindowID); void CloseWindowIfID(char a_WindowID, bool a_CanRefuse = true);
cClientHandle * GetClientHandle(void) const { return m_ClientHandle; } cClientHandle * GetClientHandle(void) const { return m_ClientHandle; }

View File

@ -1665,7 +1665,7 @@ void cPlugin_NewLua::Unreference(int a_LuaRef)
bool cPlugin_NewLua::CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player) bool cPlugin_NewLua::CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse)
{ {
ASSERT(a_FnRef != LUA_REFNIL); ASSERT(a_FnRef != LUA_REFNIL);
@ -1673,9 +1673,10 @@ bool cPlugin_NewLua::CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPla
lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_FnRef); // Push the function to be called lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_FnRef); // Push the function to be called
tolua_pushusertype(m_LuaState, &a_Window, "cWindow"); tolua_pushusertype(m_LuaState, &a_Window, "cWindow");
tolua_pushusertype(m_LuaState, &a_Player, "cPlayer"); tolua_pushusertype(m_LuaState, &a_Player, "cPlayer");
tolua_pushboolean (m_LuaState, a_CanRefuse ? 1 : 0);
// Call function: // Call function:
int s = lua_pcall(m_LuaState, 2, 1, 0); int s = lua_pcall(m_LuaState, 3, 1, 0);
if (report_errors(m_LuaState, s)) if (report_errors(m_LuaState, s))
{ {
LOGERROR("LUA error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState)); LOGERROR("LUA error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState));

View File

@ -106,7 +106,7 @@ public:
void Unreference(int a_LuaRef); void Unreference(int a_LuaRef);
/// Calls the plugin-specified "cLuaWindow closing" callback. Returns true only if the callback returned true /// 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 CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse);
/// Calls the plugin-specified "cLuaWindow slot changed" callback. /// Calls the plugin-specified "cLuaWindow slot changed" callback.
void CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window, int a_SlotNum); void CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window, int a_SlotNum);

View File

@ -251,7 +251,7 @@ void cWindow::OpenedByPlayer(cPlayer & a_Player)
bool cWindow::ClosedByPlayer(cPlayer & a_Player) bool cWindow::ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse)
{ {
// Checks whether the player is still holding an item // Checks whether the player is still holding an item
if (a_Player.IsDraggingItem()) if (a_Player.IsDraggingItem())

View File

@ -108,7 +108,7 @@ public:
void OpenedByPlayer(cPlayer & a_Player); void OpenedByPlayer(cPlayer & a_Player);
/// Called when a player closes this window; notifies all slot areas. Returns true if close accepted /// Called when a player closes this window; notifies all slot areas. Returns true if close accepted
virtual bool ClosedByPlayer(cPlayer & a_Player); virtual bool ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse);
void SendWholeWindow(cClientHandle & a_Client); void SendWholeWindow(cClientHandle & a_Client);
void BroadcastWholeWindow(void); void BroadcastWholeWindow(void);