Skip to content

Commit

Permalink
Bugfixes and code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgammon committed Sep 17, 2010
1 parent 54797dc commit 13411c1
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 39 deletions.
36 changes: 35 additions & 1 deletion OtherTypes.h
Expand Up @@ -905,6 +905,40 @@ typedef CTypedPtrList <CPtrList, CActiveTag*> CActiveTagList;
/////////////////////////////////////////////////////////////////////////////
// CPlugin - these are world plugins

// for storing map directions, and inverses of them

This comment has been minimized.

Copy link
@Twisol

Twisol Sep 17, 2010

Contributor

...What is this comment doing here?

This comment has been minimized.

Copy link
@nickgammon

nickgammon Sep 18, 2010

Author Owner

Copy and paste job. Fixed now.

class CScriptDispatchID
{
public:
// default constructor
CScriptDispatchID () :
_dispid (DISPID_UNKNOWN),
_count (0) {};

// copy constructor
CScriptDispatchID (const CScriptDispatchID & d)
: _dispid (d._dispid),
_count (d._count)
{};

// operator =
const CScriptDispatchID & operator= (const CScriptDispatchID & rhs)
{
_dispid = rhs._dispid;
_count = rhs._count;
return *this;
};

// returns true if DISPID is valid
inline bool isvalid () const { return _dispid != DISPID_UNKNOWN; };

DISPID _dispid;
__int64 _count;
}; // end of class CScriptDispatchID

typedef map<const char *, CScriptDispatchID> CScriptDispatchIDsMap;
typedef map<const char *, CScriptDispatchID>::const_iterator CScriptDispatchIDIterator;


class CScriptEngine;

class CPlugin :public CObject
Expand Down Expand Up @@ -954,7 +988,7 @@ class CPlugin :public CObject

// WARNING! PHP currently uses a DISPID of zero, so that can't be used as a "not found" flag.

map<const char *, DISPID> m_PluginCallbacks; // maps plugin callback names to their DISPIDs
CScriptDispatchIDsMap m_PluginCallbacks; // maps plugin callback names to their DISPIDs

// methods

Expand Down
12 changes: 6 additions & 6 deletions doc.cpp
Expand Up @@ -8086,7 +8086,7 @@ bool CMUSHclientDoc::SendToFirstPluginCallbacks (const char * sName, const char
// see what the plugin makes of this,
pPlugin->ExecutePluginScript (sName, sText);

if (pPlugin->m_PluginCallbacks [sName] != DISPID_UNKNOWN)
if (pPlugin->m_PluginCallbacks [sName].isvalid ())
{
m_CurrentPlugin = pSavedPlugin;
return true; // indicate we found it
Expand Down Expand Up @@ -8119,7 +8119,7 @@ bool CMUSHclientDoc::SendToAllPluginCallbacks (const char * sName,
if (!pPlugin->ExecutePluginScript (sName, sText))
bResult = false;

if (bStopOnFalse && !bResult && pPlugin->m_PluginCallbacks [sName] != DISPID_UNKNOWN)
if (bStopOnFalse && !bResult && pPlugin->m_PluginCallbacks [sName].isvalid ())
return false;

} // end of doing each plugin
Expand Down Expand Up @@ -8171,10 +8171,10 @@ bool CMUSHclientDoc::SendToAllPluginCallbacks (const char * sName,

bool bResult = pPlugin->ExecutePluginScript (sName, arg1, sText);

if (bStopOnTrue && bResult && pPlugin->m_PluginCallbacks [sName] != DISPID_UNKNOWN)
if (bStopOnTrue && bResult && pPlugin->m_PluginCallbacks [sName].isvalid ())
return true;

if (bStopOnFalse && !bResult && pPlugin->m_PluginCallbacks [sName] != DISPID_UNKNOWN)
if (bStopOnFalse && !bResult && pPlugin->m_PluginCallbacks [sName].isvalid ())
return false;

} // end of doing each plugin
Expand Down Expand Up @@ -8209,10 +8209,10 @@ bool CMUSHclientDoc::SendToAllPluginCallbacks (const char * sName,

bool bResult = pPlugin->ExecutePluginScript (sName, arg1, arg2, sText);

if (bStopOnTrue && bResult && pPlugin->m_PluginCallbacks [sName] != DISPID_UNKNOWN)
if (bStopOnTrue && bResult && pPlugin->m_PluginCallbacks [sName].isvalid ())
return true;

if (bStopOnFalse && !bResult && pPlugin->m_PluginCallbacks [sName] != DISPID_UNKNOWN)
if (bStopOnFalse && !bResult && pPlugin->m_PluginCallbacks [sName].isvalid ())
return false;

} // end of doing each plugin
Expand Down
10 changes: 5 additions & 5 deletions doc.h
Expand Up @@ -1181,11 +1181,11 @@ class CMUSHclientDoc : public CDocument
DISPID m_dispidOnMXP_SetVariable; // handler on MXP set variable, eg. set hp
DISPID m_dispidOnMXP_Error; // handler on MXP error (parse etc.)

bool m_bPluginProcessesOpenTag; // does a plugin handle the open tag?
bool m_bPluginProcessesCloseTag; // does a plugin handle the close tag?
bool m_bPluginProcessesSetVariable; // does a plugin handle set variable?
bool m_bPluginProcessesSetEntity; // does a plugin handle set entity?
bool m_bPluginProcessesError; // does a plugin handle the error?
bool m_bPluginProcessesOpenTag; // does any plugin handle the open tag?
bool m_bPluginProcessesCloseTag; // does any plugin handle the close tag?
bool m_bPluginProcessesSetVariable; // does any plugin handle set variable?
bool m_bPluginProcessesSetEntity; // does any plugin handle set entity?
bool m_bPluginProcessesError; // does any plugin handle the error?

// listening for UDP packets

Expand Down
9 changes: 5 additions & 4 deletions mxp/mxpClose.cpp
Expand Up @@ -177,10 +177,11 @@ bool bHaveVariable = false;
} // not Lua
} // end of script callback wanted

SendToAllPluginCallbacks (ON_PLUGIN_MXP_CLOSETAG,
CFormat ("%s,%s",
(LPCTSTR) strTag,
(LPCTSTR) strText));
if (m_bPluginProcessesCloseTag)
SendToAllPluginCallbacks (ON_PLUGIN_MXP_CLOSETAG,
CFormat ("%s,%s",
(LPCTSTR) strTag,
(LPCTSTR) strText));

m_CurrentPlugin = NULL;

Expand Down
2 changes: 1 addition & 1 deletion mxp/mxpOpenAtomic.cpp
Expand Up @@ -44,7 +44,7 @@ COLORREF iForeColour = pStyle->iForeColour;
COLORREF iBackColour = pStyle->iBackColour;

// call script if required
if (m_dispidOnMXP_OpenTag != DISPID_UNKNOWN || m_bPluginProcessesOpenTag)
if ((m_dispidOnMXP_OpenTag != DISPID_UNKNOWN) || m_bPluginProcessesOpenTag)
{
// dummy-up an argument list
CString strArgument;
Expand Down
27 changes: 20 additions & 7 deletions plugins.cpp
Expand Up @@ -68,11 +68,13 @@ DISPID CPlugin::GetPluginDispid (const char * sName)

void CPlugin::ExecutePluginScript (const char * sName)
{

DISPID & iRoutine = m_PluginCallbacks [sName];
CScriptDispatchID & dispid_info = m_PluginCallbacks [sName];
DISPID & iRoutine = dispid_info._dispid;

if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
dispid_info._count++;

// do this so plugin can find its own state (eg. with GetPluginID)
CPlugin * pSavedPlugin = m_pDoc->m_CurrentPlugin;
m_pDoc->m_CurrentPlugin = this;
Expand Down Expand Up @@ -119,10 +121,13 @@ void CPlugin::ExecutePluginScript (const char * sName)
bool CPlugin::ExecutePluginScript (const char * sName,
const char * sText)
{
DISPID & iRoutine = m_PluginCallbacks [sName];
CScriptDispatchID & dispid_info = m_PluginCallbacks [sName];
DISPID & iRoutine = dispid_info._dispid;

if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
dispid_info._count++;

// do this so plugin can find its own state (eg. with GetPluginID)
CPlugin * pSavedPlugin = m_pDoc->m_CurrentPlugin;
m_pDoc->m_CurrentPlugin = this;
Expand Down Expand Up @@ -206,10 +211,12 @@ bool CPlugin::ExecutePluginScript (const char * sName,
const long arg1,
const string sText)
{
DISPID & iRoutine = m_PluginCallbacks [sName];
CScriptDispatchID & dispid_info = m_PluginCallbacks [sName];
DISPID & iRoutine = dispid_info._dispid;

if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
dispid_info._count++;
// do this so plugin can find its own state (eg. with GetPluginID)
CPlugin * pSavedPlugin = m_pDoc->m_CurrentPlugin;
m_pDoc->m_CurrentPlugin = this;
Expand Down Expand Up @@ -303,10 +310,12 @@ bool CPlugin::ExecutePluginScript (const char * sName,
const long arg2,
const string sText)
{
DISPID & iRoutine = m_PluginCallbacks [sName];
CScriptDispatchID & dispid_info = m_PluginCallbacks [sName];
DISPID & iRoutine = dispid_info._dispid;

if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
dispid_info._count++;
long nInvocationCount = 0;

// do this so plugin can find its own state (eg. with GetPluginID)
Expand Down Expand Up @@ -398,10 +407,12 @@ bool CPlugin::ExecutePluginScript (const char * sName,
const char * arg3,
const char * arg4)
{
DISPID & iRoutine = m_PluginCallbacks [sName];
CScriptDispatchID & dispid_info = m_PluginCallbacks [sName];
DISPID & iRoutine = dispid_info._dispid;

if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
dispid_info._count++;
long nInvocationCount = 0;

// do this so plugin can find its own state (eg. with GetPluginID)
Expand Down Expand Up @@ -492,10 +503,12 @@ bool CPlugin::ExecutePluginScript (const char * sName,
void CPlugin::ExecutePluginScriptRtn (const char * sName,
CString & strText)
{
DISPID & iRoutine = m_PluginCallbacks [sName];
CScriptDispatchID & dispid_info = m_PluginCallbacks [sName];
DISPID & iRoutine = dispid_info._dispid;

if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
dispid_info._count++;
// do this so plugin can find its own state (eg. with GetPluginID)
CPlugin * pSavedPlugin = m_pDoc->m_CurrentPlugin;
m_pDoc->m_CurrentPlugin = this;
Expand Down
2 changes: 1 addition & 1 deletion scripting/methods.cpp
Expand Up @@ -11466,7 +11466,7 @@ long CMUSHclientDoc::BroadcastPlugin(long Message, LPCTSTR Text)
(LPCTSTR) strCurrentName,
Text);

if (pPlugin->m_PluginCallbacks [ON_PLUGIN_BROADCAST] != DISPID_UNKNOWN)
if (pPlugin->m_PluginCallbacks [ON_PLUGIN_BROADCAST].isvalid ())
iCount++;

} // end of doing each plugin
Expand Down
75 changes: 73 additions & 2 deletions world_debug.cpp
Expand Up @@ -1297,7 +1297,25 @@ VARIANT CMUSHclientDoc::Debug(LPCTSTR Command)
CString strVariableName;
CVariable * variable_item;
pPlugin->m_VariableMap.GetNextAssoc (varpos, strVariableName, variable_item);
}
} // end of for loop

// count callbacks that are either valid (and not fired) or fired at some point
int nTotalCallbacks = 0;
for (CScriptDispatchIDIterator i = pPlugin->m_PluginCallbacks.begin ();
i != pPlugin->m_PluginCallbacks.end ();
i++)
if (i->second.isvalid () || i->second._count > 0)
nTotalCallbacks++;

// no quick way of finding timers count
int nTotalTimers = 0;
for (POSITION timerpos = pPlugin->m_TimerMap.GetStartPosition(); timerpos; nTotalTimers++)
{
CTimer * pTimer;
CString strName;
GetTimerMap ().GetNextAssoc (timerpos, strName, pTimer);
} // end of for loop


// first time we find a non-zero count, we draw the left bracket
// afterwards, we put a space between the previous item and this one
Expand Down Expand Up @@ -1325,7 +1343,7 @@ VARIANT CMUSHclientDoc::Debug(LPCTSTR Command)
}

// hyperlink for timers
if (pPlugin->m_TimerRevMap.size () > 0)
if (nTotalTimers > 0)
{
if (bDoneOne)
Tell (" ");
Expand All @@ -1348,6 +1366,18 @@ VARIANT CMUSHclientDoc::Debug(LPCTSTR Command)
bDoneOne = true;
}

// hyperlink for callbacks
if (nTotalCallbacks > 0)
{
if (bDoneOne)
Tell (" ");
else
ColourTell (strColour, "", " [");
Hyperlink (CFormat ("!!" DEBUG_PLUGIN_ID ":callbacklist(_%s_)",(LPCTSTR) pPlugin->m_strID),
"Cb", "Click to list callbacks", "cyan", "", 0);
bDoneOne = true;
}

// wrap up line
if (bDoneOne)
ColourNote (strColour, "", "]");
Expand Down Expand Up @@ -2016,6 +2046,47 @@ void CMUSHclientDoc::DebugHelper (const CString strAction, CString strArgument)
} // end of variablelist


//-----------------------------------------------------------------------
// callbacklist
//-----------------------------------------------------------------------

else if (strAction == "callbacklist")
{

ColourNote (SCRIPTERRORCONTEXTFORECOLOUR, "", "------ Callback List (alphabetic order) ------");
Note ("");

int iCount = 0;
map<string,CScriptDispatchID> sortedCallbacks;

for (CScriptDispatchIDIterator it = m_CurrentPlugin->m_PluginCallbacks.begin ();
it != m_CurrentPlugin->m_PluginCallbacks.end ();
it++)

if (it->second.isvalid () || it->second._count > 0)
sortedCallbacks [it->first] = it->second;

// this will get them into alphabetic order (the original map is by pointers, not sorted by name)

for (map<string,CScriptDispatchID>::const_iterator it2 = sortedCallbacks.begin ();
it2 != sortedCallbacks.end ();
it2++)

{
iCount++;
Note ( CFormat ("%-30s : Valid: %s. Called %I64i time%s.",
it2->first.c_str (),
SHOW_TRUE (it2->second.isvalid ()),
PLURAL (it2->second._count)
));

}

ColourNote (SCRIPTERRORCONTEXTFORECOLOUR, "", TFormat ("%i callback%s.", PLURAL (iCount)));

} // end of callbacklist


//-----------------------------------------------------------------------
// acceleratorlist
//-----------------------------------------------------------------------
Expand Down
29 changes: 17 additions & 12 deletions xml/xml_load_world.cpp
Expand Up @@ -478,23 +478,28 @@ LONGLONG iCounterFrequency = large_int_frequency.QuadPart;
// find all plugin callbacks by looping through table - add to map

for (int i = 0; PluginCallbacksNames [i]; i++)
m_CurrentPlugin->m_PluginCallbacks [PluginCallbacksNames [i]] =
m_CurrentPlugin->m_PluginCallbacks [PluginCallbacksNames [i]]._dispid =
m_CurrentPlugin->GetPluginDispid (PluginCallbacksNames [i]);

// note if we need to call these routines
// note if we need to call these routines (that is, if *any* plugin supports them)

if (m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_OPENTAG].isvalid ())
m_bPluginProcessesOpenTag = true;

if (m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_CLOSETAG].isvalid ())
m_bPluginProcessesCloseTag = true;

if (m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_SETVARIABLE].isvalid ())
m_bPluginProcessesSetVariable = true;

if (m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_SETENTITY].isvalid ())
m_bPluginProcessesSetEntity = true;

if (m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_ERROR].isvalid ())
m_bPluginProcessesError = true;

m_bPluginProcessesOpenTag = m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_OPENTAG] != DISPID_UNKNOWN;
m_bPluginProcessesCloseTag = m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_CLOSETAG] != DISPID_UNKNOWN;
m_bPluginProcessesSetVariable = m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_SETVARIABLE] != DISPID_UNKNOWN;
m_bPluginProcessesSetEntity = m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_SETENTITY] != DISPID_UNKNOWN;
m_bPluginProcessesError = m_CurrentPlugin->m_PluginCallbacks [ON_PLUGIN_MXP_ERROR] != DISPID_UNKNOWN;

} // end of having a script
else
{ // no script, make sure all callbacks are marked as not available
for (int i = 0; PluginCallbacksNames [i]; i++)
m_CurrentPlugin->m_PluginCallbacks [PluginCallbacksNames [i]] = DISPID_UNKNOWN;
}

// add to world plugins
m_PluginList.AddTail (m_CurrentPlugin);
Expand Down

0 comments on commit 13411c1

Please sign in to comment.