📄 procedures.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 "Procedures.h"
#include "extern.h"
#include "UserLists.h"
#include "Events.h"
#include "Actions.h"
#include "AliasMap.h"
#include "Macros.h"
#include "TabList.h"
#include "StatusBar.h"
#include "Commands.h"
#include "MudArray.h"
#include "Gags.h"
#include "Highlights.h"
#include "Substitute.h"
#include "Chat.h"
#include "DebugStack.h"
CHashTable _htProcedures;
// It is very important to put procedures into the list in
// alphabetical order. The hash table will mess up badly
// if they aren't (by first letter only).
PROCEDURELIST _procedureList[] =
{
{"Abs",Abs,"p_abs"},
{"AnsiBold",AnsiBold,"p_color"},
{"AnsiReset",AnsiReset,"p_color"},
{"Asc",Asc,"p_asc"},
{"BackBlack",BackBlack,"p_color"},
{"BackBlue",BackBlue,"p_color"},
{"BackCyan",BackCyan,"p_color"},
{"BackGreen",BackGreen,"p_color"},
{"BackMagenta",BackMagenta,"p_color"},
{"BackRed",BackRed,"p_color"},
{"BackYellow",BackYellow,"p_color"},
{"BackWhite",BackWhite,"p_color"},
{"CharColor",CharColor,"p_chrclr"},
{"ChatConnects",ChatConnects,"p_ccon"},
{"ChatName",ChatName,"p_cname"},
{"Chr",Chr,"p_chr"},
{"ConCat",ConCat,"p_concat"},
{"Connected",Connected,"p_connec"},
{"Day",Day,"p_day"},
{"EventTime",EventTime,"p_evtime"},
{"Exists",Exists,"p_exists"},
{"FileExists",FileExists,"p_fileex"},
{"ForeBlack",ForeBlack,"p_color"},
{"ForeBlue",ForeBlue,"p_color"},
{"ForeCyan",ForeCyan,"p_color"},
{"ForeGreen",ForeGreen,"p_color"},
{"ForeMagenta",ForeMagenta,"p_color"},
{"ForeRed",ForeRed,"p_color"},
{"ForeYellow",ForeYellow,"p_color"},
{"ForeWhite",ForeWhite,"p_color"},
{"GetArray",GetArray,"p_getary"},
{"GetCount",GetCount,"p_gcount"},
{"GetItem",GetItem,"p_gitem"},
{"GetKey",GetKey,"p_gkey"},
{"Hour",Hour,"p_hour"},
{"InList",InList,"p_inlist"},
{"IP",IP,"p_ip"},
{"IPP",IPP,"p_ip"},
{"IsEmpty",IsEmpty,"p_isemp"},
{"IsNumber",IsNumber,"p_isnum"},
{"KeyWaiting",KeyWaiting,"p_kwait"},
{"Left",Left,"p_left"},
{"Len",Len,"p_len"},
{"Lower",Lower,"p_lower"},
{"LTrim",LTrim,"p_ltrim"},
{"Math",Math,"p_math"},
{"Mid",Mid,"p_mid"},
{"Minute",Minute,"p_minute"},
{"Month",Month,"p_month"},
{"NumActions",NumActions,"p_numact"},
{"NumAliases",NumAliases,"p_numali"},
{"NumArrays",NumArrays,"p_numary"},
{"NumBarItems",NumBarItems,"p_numbar"},
{"NumEvents",NumEvents,"p_numev"},
{"NumGags",NumGags,"p_numgag"},
{"NumHighlights",NumHighlights,"p_numhig"},
{"NumLists",NumLists,"p_numlst"},
{"NumMacros",NumMacros,"p_nummac"},
{"NumSubstitutes",NumSubstitutes,"p_numsub"},
{"NumTabList",NumTabList,"p_numtab"},
{"NumVariables",NumVariables,"p_numvar"},
{"PadLeft",PadLeft,"p_padlef"},
{"PadRight",PadRight,"p_padrig"},
{"PreTrans",PreTrans,"p_pretra"},
{"Random",Random,"p_random"},
{"Right",Right,"p_right"},
{"RTrim",RTrim,"p_rtrim"},
{"Second",Second,"p_second"},
{"StripAnsi",StripAnsi,"p_sansi"},
{"StrStr",StrStr,"p_strstr"},
{"TextColor",TextColor,"p_tcolor"},
{"Time",Time,"p_time"},
{"TimeToDay",TimeToDay,"p_time"},
{"TimeToDayOfWeek",TimeToDayOfWeek,"p_time"},
{"TimeToHour",TimeToHour,"p_time"},
{"TimeToMinute",TimeToMinute,"p_time"},
{"TimeToMonth",TimeToMonth,"p_time"},
{"TimeToSecond",TimeToSecond,"p_time"},
{"TimeToYear",TimeToYear,"p_time"},
{"Upper",Upper,"p_upper"},
{"Val",Val,"p_val"},
{"Var",Var,"p_var"},
{"Version",VersionProc,"p_ver"},
{"Word",Word,"p_word"},
{"WordColor",WordColor,"p_wclr"},
{"WordCount",WordCount,"p_wcount"},
{"Year",Year,"p_year"},
{"",NULL},
};
// Creates the hash table entries for the procedures.
void HashProcedures()
{
UINT nCurEntry; // Current hash table entry.
UINT nEntry; // Entry of the item being checked.
int i;
int nStart;
int nEnd;
_htProcedures.Clear();
nStart = HASHITEM_EMPTY;
nEnd = HASHITEM_EMPTY;
for (i=0;_procedureList[i].proc;i++)
{
nEntry = _procedureList[i].szProcedure[0];
// Will only be this value first time thru.
if (!i)
{
nCurEntry = nEntry;
nStart = i;
}
// If the entry we just found is not equal the to the entry we are working
// with, need to finish off the last entry and start a new one.
if (nEntry != nCurEntry)
{
_htProcedures.SetAt(nCurEntry,nStart,nEnd);
nStart = i;
nCurEntry = nEntry;
}
nEnd = i;
}
// Last time thru loop need to add last hash entry.
_htProcedures.SetAt(nCurEntry,nStart,nEnd);
}
void FindParam(CString &strText, CString &strResult)
{
strResult.Empty();
// strText.TrimRight();
// strText.TrimLeft();
if (strText.IsEmpty())
return;
int nIndex = 0;
int nNestedProcs = 0;
int nLen = strText.GetLength();
while(nIndex < nLen)
{
// Might be using the escape char to force an @.
if (strText[nIndex] == _config.chEscape && nIndex+1 < nLen && strText[nIndex+1] == '@')
{
strResult += "@";
nIndex += 2;
continue;
}
// Might be using the escape char to force a comma.
if (strText[nIndex] == _config.chEscape && nIndex+1 < nLen && strText[nIndex+1] == ',')
{
strResult += ',';
nIndex += 2;
continue;
}
if (strText[nIndex] == _config.chEscape && nIndex+1 < nLen && strText[nIndex+1] == ')')
{
strResult += ')';
nIndex += 2;
continue;
}
if (nNestedProcs && strText[nIndex] == ')')
nNestedProcs--;
if (strText[nIndex] == '@')
nNestedProcs++;
if (!nNestedProcs && strText[nIndex] == ',')
break;
strResult += strText[nIndex];
nIndex++;
}
if (nIndex+1 < nLen)
strText = strText.Right(nLen-nIndex-1);
else
strText.Empty();
}
BOOL ProcedureVar(const char *pszProc, CString &strResult)
{
// Pull the procedure name off.
CString strProc;
while(*pszProc)
{
if (*pszProc != '(')
strProc += *pszProc;
else
break;
pszProc++;
}
// Skip over the paren.
pszProc++;
// Collect everything between the parens.
int nParenCount = 1;
CString strParams;
while(*pszProc)
{
if (*pszProc == '(')
nParenCount++;
if (*pszProc == _config.chEscape && *(pszProc+1) && *(pszProc+1) == ')')
{
strParams += _config.chEscape;
strParams += ")";
pszProc += 2;
continue;
}
if (*pszProc == ')')
{
nParenCount--;
if (!nParenCount)
break;
}
strParams += *pszProc;
pszProc++;
}
HASHITEM *phi = _htProcedures[strProc[0]];
if (!phi || phi->nStart == HASHITEM_EMPTY || phi->nEnd == HASHITEM_EMPTY)
{
PrintMessage("Invalid procedure.",TRUE,TRUE);
_debugStack.DumpStack(_config.nDebugDepth);
return(FALSE);
}
for (int i=phi->nStart;i<=phi->nEnd && _procedureList[i].proc;i++)
if (strProc == _procedureList[i].szProcedure)
return((_procedureList[i].proc)(strParams,strResult));
return(FALSE);
}
BOOL DoProcedure(const char *pszProc)
{
// Get past the '@'.
pszProc++;
// Pull the procedure name off.
CString strProc;
while(*pszProc)
{
if (*pszProc != '(')
strProc += *pszProc;
else
break;
pszProc++;
}
// Skip over the paren.
pszProc++;
// Collect everything between the parens.
int nParenCount = 1;
CString strParams;
while(*pszProc)
{
if (*pszProc == '(')
nParenCount++;
if (*pszProc == ')')
{
nParenCount--;
if (!nParenCount)
break;
}
strParams += *pszProc;
pszProc++;
}
if (strProc == "CommandToList")
{
CommandToList(strParams);
return(TRUE);
}
return(FALSE);
}
//////////////////////////////////////////////////////////////
// General Purpose Procedures
//
BOOL Abs(CString &strParams, CString &strResult)
{
CString strNum;
FindParam(strParams,strNum);
ReplaceVariables(strNum);
strResult.Format("%d",abs(atoi(strNum)));
return(TRUE);
}
BOOL AnsiBold(CString &strParams, CString &strResult)
{
strResult = ANSI_BOLD;
return(TRUE);
}
BOOL AnsiReset(CString &strParams, CString &strResult)
{
strResult = ANSI_RESET;
return(TRUE);
}
BOOL BackBlack(CString &strParams, CString &strResult)
{
strResult = ANSI_B_BLACK;
return(TRUE);
}
BOOL BackBlue(CString &strParams, CString &strResult)
{
strResult = ANSI_B_BLUE;
return(TRUE);
}
BOOL BackCyan(CString &strParams, CString &strResult)
{
strResult = ANSI_B_CYAN;
return(TRUE);
}
BOOL BackGreen(CString &strParams, CString &strResult)
{
strResult = ANSI_B_GREEN;
return(TRUE);
}
BOOL BackMagenta(CString &strParams, CString &strResult)
{
strResult = ANSI_B_MAGENTA;
return(TRUE);
}
BOOL BackRed(CString &strParams, CString &strResult)
{
strResult = ANSI_B_RED;
return(TRUE);
}
BOOL BackYellow(CString &strParams, CString &strResult)
{
strResult = ANSI_B_YELLOW;
return(TRUE);
}
BOOL BackWhite(CString &strParams, CString &strResult)
{
strResult = ANSI_B_WHITE;
return(TRUE);
}
BOOL CharColor(CString &strParams, CString &strResult)
{
if (!_ptlLastText)
{
strResult = "-1";
return(TRUE);
}
CString strNum;
FindParam(strParams, strNum);
ReplaceVariables(strNum);
int nIndex = atoi(strNum) - 1;
if (nIndex < 0 || nIndex >= _ptlLastText->m_nLen)
{
strResult = "-1";
return(TRUE);
}
strResult.Format("%d",_ptlLastText->m_ptrAttr[nIndex]);
return(TRUE);
}
BOOL ChatConnects(CString &strParams, CString &strResult)
{
strResult.Format("%d",_chat.GetCount());
return(TRUE);
}
BOOL ChatName(CString &strParams, CString &strResult)
{
CString strNum;
int nNum;
FindParam(strParams,strNum);
ReplaceVariables(strNum);
nNum = atoi(strNum);
if (!nNum)
strResult = _chat.GetChatName();
else
if (nNum < 1 || nNum > _chat.GetCount())
{
if (_config.bDebugMessages)
{
CString strTemp;
strTemp.Format("@ChatName(%s): Chat index out of range.",strNum);
PrintMessage(strTemp,TRUE,TRUE);
_debugStack.DumpStack(_config.nDebugDepth);
}
return(FALSE);
}
else
{
CHAT *pChat = _chat.GetAt(nNum-1);
if (!pChat)
return(FALSE);
strResult = pChat->strName;
}
return(TRUE);
}
BOOL Chr(CString &strParams, CString &strResult)
{
CString strNum;
FindParam(strParams,strNum);
ReplaceVariables(strNum);
int nNum = atoi(strNum);
if (nNum < 0 || nNum > 254)
{
if (_config.bDebugMessages)
{
CString strTemp;
strTemp.Format("@Chr(%d): Character values must be between 0 and 254.",nNum);
PrintMessage(strTemp,TRUE,TRUE);
_debugStack.DumpStack(_config.nDebugDepth);
}
return(FALSE);
}
strResult.Format("%c",nNum);
return(TRUE);
}
BOOL Asc(CString &strParams, CString &strResult)
{
if (strParams.IsEmpty())
{
if (_config.bDebugMessages)
{
PrintMessage("Procedure @Asc() called with an empty value.",TRUE,TRUE);
_debugStack.DumpStack(_config.nDebugDepth);
}
return(FALSE);
}
strResult.Format("%d",strParams.GetAt(0));
return(TRUE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -