📄 notifications.cpp
字号:
/************************************************************************************
Copyright (c) 2000 Aaron O'Neil
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1) Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2) Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3) Redistributions in binary form must reproduce the above copyright notice on
program startup. Additional credits for program modification are acceptable
but original copyright and credits must be visible at startup.
4) You may charge a reasonable copying fee for any distribution of Mud Master.
You may charge any fee you choose for support of Mud Master. You may not
charge a fee for Mud Master itself.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************************/
#include "StdAfx.h"
#include "Extern.h"
#include "Notifications.h"
#include "MudWindow.h"
#include "Dll.h"
CNotifications::CNotifications(const char *pszTypeName)
{
m_ptrList.SetSize(DEFAULT_NOT_ARRAY_SIZE,DEFAULT_NOT_GROW_SIZE);
m_nCount = 0;
m_nGetIndex = 0;
m_strTypeName = pszTypeName;
}
CNotifications::~CNotifications()
{
RemoveAll();
}
void CNotifications::RemoveAll()
{
NOTIFICATION *pNot;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
delete pNot;
}
m_ptrList.RemoveAll();
m_nCount = 0;
m_nGetIndex = 0;
}
BOOL CNotifications::Add(HINSTANCE hDll,const char *pszFunction)
{
NOTIFICATION *pNot;
// If this dll is already in the list, just change the name of
// the function to call.
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (pNot->hDll == hDll)
{
pNot->strFunction = pszFunction;
return(TRUE);
}
}
NOTIFICATION *pNew = new NOTIFICATION;
pNew->strFunction = pszFunction;
pNew->hDll = hDll;
m_ptrList.SetAtGrow(m_nCount,pNew);
m_nCount++;
return(TRUE);
}
BOOL CNotifications::Remove(HINSTANCE hDll)
{
NOTIFICATION *pNot;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (pNot->hDll == hDll)
{
m_ptrList.RemoveAt(i);
m_nCount--;
delete pNot;
return(TRUE);
}
}
return(FALSE);
}
BOOL CNotifications::Remove(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(FALSE);
NOTIFICATION *pNot = (NOTIFICATION *)m_ptrList.GetAt(nIndex);
if (pNot == NULL)
return(FALSE);
m_ptrList.RemoveAt(nIndex);
m_nCount--;
delete pNot;
return(TRUE);
}
NOTIFICATION* CNotifications::GetFirst()
{
if (!m_nCount)
return(NULL);
m_nGetIndex = 0;
return((NOTIFICATION *)m_ptrList.GetAt(m_nGetIndex));
}
NOTIFICATION* CNotifications::GetNext()
{
if (m_nGetIndex+1 == m_nCount)
return(NULL);
m_nGetIndex++;
return((NOTIFICATION *)m_ptrList.GetAt(m_nGetIndex));
}
NOTIFICATION* CNotifications::FindExact(HINSTANCE hDll)
{
NOTIFICATION *pNot;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (pNot != NULL && pNot->hDll == hDll)
return(pNot);
}
return(NULL);
}
void CNotifications::Print()
{
CString strText;
DLL *pDll;
NOTIFICATION *pNot;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (!pNot)
continue;
pDll = _dlls.FindExact(pNot->hDll);
if (!pDll)
continue;
strText.Format("%-15s %-20s %s\n",
(const char *)m_strTypeName,
(const char *)pDll->strName,
(const char *)pNot->strFunction);
_terminal.Print(strText);
}
}
///////////////////////////////////////////////////////////////////
// Connection notifications
//
CNotifyConnect::CNotifyConnect() : CNotifications("Connect")
{
}
void CNotifyConnect::Notify(SOCKET hSocket)
{
NOTIFICATION *pNot;
CONNECTPROC pProc;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (!pNot)
continue;
pProc = (CONNECTPROC)GetProcAddress(pNot->hDll,pNot->strFunction);
if (pProc)
pProc(hSocket);
}
}
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Connection notifications
//
CNotifyDisconnect::CNotifyDisconnect() : CNotifications("Disconnect")
{
}
void CNotifyDisconnect::Notify()
{
NOTIFICATION *pNot;
DISCONNECTPROC pProc;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (!pNot)
continue;
pProc = (DISCONNECTPROC)GetProcAddress(pNot->hDll,pNot->strFunction);
if (pProc)
pProc();
}
}
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Chat Connection notifications
//
CNotifyChatConnect::CNotifyChatConnect() : CNotifications("ChatConnect")
{
}
void CNotifyChatConnect::Notify(SOCKET hSocket, const char *pszChatName)
{
NOTIFICATION *pNot;
CHATCONNECTPROC pProc;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (!pNot)
continue;
pProc = (CHATCONNECTPROC)GetProcAddress(pNot->hDll,pNot->strFunction);
if (pProc)
pProc(hSocket,pszChatName);
}
}
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Chat Disconnection notifications
//
CNotifyChatDisconnect::CNotifyChatDisconnect() : CNotifications("ChatDisconnect")
{
}
void CNotifyChatDisconnect::Notify(SOCKET hSocket, const char *pszChatName)
{
NOTIFICATION *pNot;
CHATDISCONNECTPROC pProc;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (!pNot)
continue;
pProc = (CHATDISCONNECTPROC)GetProcAddress(pNot->hDll,pNot->strFunction);
if (pProc)
pProc(hSocket,pszChatName);
}
}
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Input Notifications
//
CNotifyInput::CNotifyInput() : CNotifications("Input")
{
}
// Returns TRUE if any one of the dlls in the list says it processed it.
BOOL CNotifyInput::Notify(const char *pszInput)
{
BOOL bProcessed = FALSE;
NOTIFICATION *pNot;
INPUTPROC pProc;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (!pNot)
continue;
pProc = (INPUTPROC)GetProcAddress(pNot->hDll,pNot->strFunction);
if (pProc && pProc(pszInput))
bProcessed = TRUE;
}
return(bProcessed);
}
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Raw input (from socket) Notifications
//
CNotifyRaw::CNotifyRaw() : CNotifications("Raw")
{
}
// Returns TRUE if any one of the dlls in the list says it processed it.
BOOL CNotifyRaw::Notify(char *pszBuf)
{
BOOL bProcessed = FALSE;
NOTIFICATION *pNot;
RAWPROC pProc;
for (int i=0;i<m_nCount;i++)
{
pNot = (NOTIFICATION *)m_ptrList.GetAt(i);
if (!pNot)
continue;
pProc = (RAWPROC)GetProcAddress(pNot->hDll,pNot->strFunction);
if (pProc && pProc(pszBuf))
bProcessed = TRUE;
}
return(bProcessed);
}
//
///////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -