/** Send the names of the commands. */ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdNames() { Packet *p = new Packet(ADMIN_PACKET_SERVER_CMD_NAMES); for (uint i = 0; i < CMD_END; i++) { const char *cmdname = GetCommandName(i); /* Should SEND_MTU be exceeded, start a new packet * (magic 5: 1 bool "more data" and one uint16 "command id", one * byte for string '\0' termination and 1 bool "no more data" */ if (p->size + strlen(cmdname) + 5 >= SEND_MTU) { p->Send_bool(false); this->SendPacket(p); p = new Packet(ADMIN_PACKET_SERVER_CMD_NAMES); } p->Send_bool(true); p->Send_uint16(i); p->Send_string(cmdname); } /* Marker to notify the end of the packet has been reached. */ p->Send_bool(false); this->SendPacket(p); return NETWORK_RECV_STATUS_OKAY; }
void plResponderProc::AddCommand() { RECT rect; GetWindowRect(GetDlgItem(fhDlg, IDC_ADD_CMD), &rect); // Create the popup menu and get the option the user selects SetForegroundWindow(fhDlg); int type = TrackPopupMenu(fhMenu, TPM_RIGHTALIGN | TPM_NONOTIFY | TPM_RETURNCMD, rect.left, rect.top, 0, fhDlg, NULL); PostMessage(fhDlg, WM_USER, 0, 0); if (type == 0) return; CmdID& cmdID = fMenuCmds[type]; plResponderCmd *cmd = cmdID.first; int cmdIdx = cmdID.second; IParamBlock2 *cmdPB = cmd->CreatePB(cmdIdx); fStatePB->Append(kStateCmdParams, 1, (ReferenceTarget**)&cmdPB); IParamBlock2 *waitPB = ResponderWait::CreatePB(); fStatePB->Append(kStateCmdWait, 1, (ReferenceTarget**)&waitPB); BOOL enabled = TRUE; fStatePB->Append(kStateCmdEnabled, 1, &enabled); const char* name = GetCommandName(fStatePB->Count(kStateCmdParams)-1); int idx = ListBox_AddString(fhList, name); ListBox_SetCurSel(fhList, idx); ICreateCmdRollups(); }
void llWorker::Print(void) { _llLogger()->WriteNextLine(LOG_COMMAND,"%s ", GetCommandName()); for (unsigned int i=0; i<name.size(); i++) { if (used[i]) { if (i_value[i]) { _llLogger()->AddToLine(' '); _llLogger()->AddToLine(name[i]); _llLogger()->AddToLine("=", *(i_value[i])); } else if (f_value[i]) { _llLogger()->AddToLine(' '); _llLogger()->AddToLine(name[i]); _llLogger()->AddToLine("=", *(f_value[i]), 0); } else if (d_value[i]) { _llLogger()->AddToLine(' '); _llLogger()->AddToLine(name[i]); _llLogger()->AddToLine("=", *(d_value[i]), 0); } else if (s_value[i] && *s_value[i]) { _llLogger()->AddToLine(' '); _llLogger()->AddToLine(name[i]); _llLogger()->AddToLine("=\""); _llLogger()->AddToLine(*(s_value[i])); _llLogger()->AddToLine('\"'); } else if (flag[i]) { _llLogger()->AddToLine(' '); _llLogger()->AddToLine(name[i]); } } } _llLogger()->Dump(); }
char *CommandLine :: GetCommand ( ) { if ( !cmd_name ) GetCommandName(); return( cmd_name && *cmd_name ? cmd_name : NULL); }
static void CheckForExtraTokens(cmdScannerADT cs) { string token; token = ReadCommandToken(cs); if (!StringEqual(token, "")) { Error("%s: Found %s when expecting end of line", GetCommandName(cs), token); } }
bool CCommandLineArgs::Exists ( const char* szKey ) { std::string key = szKey; GetCommandName((char*)key.c_str()); tmCommandMap::iterator itr = commands.find(key); return (itr != commands.end()); }
void plResponderProc::LoadList() { ListBox_ResetContent(fhList); for (int i = 0; i < fStatePB->Count(kStateCmdParams); i++) { const char* name = GetCommandName(i); ListBox_AddString(fhList, name); } ListBox_SetCurSel(fhList, -1); }
//从脚本文件的label处开始读取脚本命令,结果存入scripts bool CScript::Run(char* strFilePath, std::queue<std::string>* scripts, char* strLabel) { char* strCmd; std::string strCmdName; //打开脚本文件 if(OpenFile(strFilePath)) { m_nFileOffset = 0; //若传入指定label,计算指定label的文件偏移量 if(strcmp(strLabel,"") != 0) { if (FindLabel(strLabel) == 0) return false; } while(1) { strCmd = ReadCommand(); //读取一条命令 TrimSpace(strCmd); //去除首尾空字符 strCmdName.assign(GetCommandName(strCmd)); //命令为return,结束当前读取操作 if(strlen(strCmd) == 0 || stricmp(strCmd, "return") == 0) { SAFE_DELETE(strCmd); strCmdName.clear(); return true; } //读取的为标号,跳过该标号 else if (strCmd[strlen(strCmd)-1] == ':') { SAFE_DELETE(strCmd); strCmdName.clear(); continue; } // RunCommand(strCmd); //执行 scripts->push(std::string(strCmd)); //交命令加入结果列表中 SAFE_DELETE(strCmd); strCmdName.clear(); } } SAFE_DELETE(strCmd); strCmdName.clear(); return false; }
int CCommandLineArgs::GetDataI ( const char* szKey ) { std::string key = szKey; GetCommandName((char*)key.c_str()); tmCommandMap::iterator itr = commands.find(key); if(itr == commands.end()) return false; return atoi(itr->second.c_str()); }
void CCommandLineArgs::Set ( COSFile &file ) { // read in a file or something std::string key; std::string data; unsigned char c; file.Open("rt"); while (file.ScanChar(&c) != EOF) { if ( (c != ' ') && (c != '\n')) { if (data.size()>0) data+=c; else key += c; } else if (c == '\n') { data.erase(0,1); GetCommandName((char*)key.c_str()); commands[key] = data; data.erase(0,data.size()); key.erase(0,key.size()); } else data = c; } if ((key.size() > 0) ) { data.erase(0,1); GetCommandName((char*)key.c_str()); commands[key] = data; } file.Close(); }
void EventMapView::DumpKey(const char *aPrefix, EKey *Key) { char KeyName[128] = ""; char Entry[2048] = ""; char *p; int id; if (aPrefix) { strcpy(KeyName, aPrefix); strcat(KeyName, "_"); } GetKeyName(KeyName + strlen(KeyName), sizeof(KeyName)-strlen(KeyName), Key->fKey); sprintf(Entry, "%13s ", KeyName); id = Key->Cmd; for (int i = 0; i < Macros[id].Count; i++) { p = Entry + strlen(Entry); if (Macros[id].cmds[i].type == CT_COMMAND) { if (Macros[id].cmds[i].repeat > 1) sprintf(p, "%d:%s ", Macros[id].cmds[i].repeat, GetCommandName(Macros[id].cmds[i].u.num)); else sprintf(p, "%s ", GetCommandName(Macros[id].cmds[i].u.num)); } else if (Macros[id].cmds[i].type == CT_NUMBER) { sprintf(p, "%ld ", Macros[id].cmds[i].u.num); } else if (Macros[id].cmds[i].type == CT_STRING) { sprintf(p, "'%s' ", Macros[id].cmds[i].u.string); } else if (Macros[id].cmds[i].type == CT_CONCAT) { strcat(p, ". "); } else if (Macros[id].cmds[i].type == CT_VARIABLE) { sprintf(p, "$(%ld) ", Macros[id].cmds[i].u.num); } if (strlen(Entry) > 70) { if (i != Macros[id].Count - 1) { // not the last entry AddLine(Entry); sprintf(Entry, "%13s ", ""); } } } AddLine(Entry); }
LOCAL void CommandError(ITEMID idCommand) /***********************************************************************/ { TCHAR szCommand[MAX_CMD_LEN]; STRING szFormat; STRING szName; if (AstralStrEx(idCommand, szCommand, sizeof(szCommand))) GetCommandName(szCommand, szName); else wsprintf(szName, "%d", idCommand); AstralStrEx(IDS_COMMANDERROR, szFormat, sizeof(szFormat)); wsprintf(szCommand, szFormat, (LPTSTR)szName); Print(szCommand); }
const char* GetCommandMenuName(int commandID,XString &name) { name=""; const char* cstr = GetCommandName(commandID); if (!cstr) return NULL; name = cstr; KeyboardShortcutManager* ksm = s_Plugininterface->GetKeyboardShortcutManager(); if (ksm) { ksm->GetKSName( STR_CATEGORY, commandID, name, FALSE/*do not clear string*/, TRUE/*add tab to string if string not empty*/); } return name.CStr(); }
void plResponderProc::ICmdRightClick(HWND hCmdList) { // Get the position of the cursor in screen and tree client coords POINT point, localPoint; GetCursorPos(&point); localPoint = point; ScreenToClient(hCmdList, &localPoint); LRESULT res = SendMessage(hCmdList, LB_ITEMFROMPOINT, 0, MAKELPARAM(localPoint.x, localPoint.y)); WORD index = LOWORD(res); if (index == WORD(LB_ERR)) return; RECT rect; SendMessage(hCmdList, LB_GETITEMRECT, index, (LPARAM)&rect); // Make sure we're actually ON an item, LB_ITEMFROMPOINT get the closest instead of exact if (localPoint.y >= rect.top && localPoint.y <= rect.bottom) { BOOL enabled = fStatePB->GetInt(kStateCmdEnabled, 0, index); HMENU hMenu = CreatePopupMenu(); AppendMenu(hMenu, MF_STRING, 1, enabled ? "Disable" : "Enable"); SetForegroundWindow(fhDlg); int sel = TrackPopupMenu(hMenu, TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, 0, fhDlg, NULL); if (sel == 1) { fStatePB->SetValue(kStateCmdEnabled, 0, !enabled, index); ListBox_DeleteString(hCmdList, index); ListBox_InsertString(hCmdList, index, GetCommandName(index)); } DestroyMenu(hMenu); } }
bool CScript::RunCommand(char* strCmd) { std::string strCmdName = GetCommandName(strCmd); int i; for(i=0; i<m_nCommandNum; i++) { if (stricmp(strCmdName.c_str(),m_Commands[i].m_strCmdName.c_str()) == 0) break; } //若未在命令名称列表中找到对应的命令,直接返回false if(i == m_nCommandNum) return false; switch(m_Commands[i].m_nId) { case DEMO::CMD_RETURN: { } break; //移动玩家角色和NPC case DEMO::CMD_MOVE_PLAYER_TO: //MovePlayerTo("PlayerName",x,y) case DEMO::CMD_MOVE_NPC_TO: //MoveNpcTo("NpcName",x,y) { std::string name = GetStringParam(strCmd, 0); int p1 = GetIntParam(strCmd, 1); int p2 = GetIntParam(strCmd, 2); CMessage msg(DEMO_MSG::MSG_SET_OBJECT_MOVE, 0, Int2To1(p1,p2), &name); _SendMsg(msg); } break; //设置玩家角色和NPC朝向 case DEMO::CMD_SET_PLAYER_DIR: //SetPlayerDir("name","left/right/up/down/left_up/left_down/right_up/right_down") case DEMO::CMD_SET_NPC_DIR: //SetNpcDir("name","left...") { std::string name = GetStringParam(strCmd, 0); std::string dir = GetStringParam(strCmd, 1); size_t nDir = StrToDirection(dir.c_str()); CMessage msg(DEMO_MSG::MSG_SET_OBJECT_DIR, nDir, 0, &name); _SendMsg(msg); } break; //设置是否显示玩家角色和NPC,0不显示,1显示 case DEMO::CMD_SET_PLAYER_SHOW: //SetPlayerShow("name",0/1) case DEMO::CMD_SET_NPC_SHOW: //SetNpcShow("name",0/1) { std::string name = GetStringParam(strCmd, 0); int p1 = GetIntParam(strCmd, 1); CMessage msg(DEMO_MSG::MSG_SET_OBJECT_SHOW, p1, 0, &name); _SendMsg(msg); } break; //设置指定玩家和NPC的坐标(x,y) case DEMO::CMD_SET_NPC_POS: //SetNpcPos("NpcName",x,y) case DEMO::CMD_SET_PLAYER_POS: { std::string name = GetStringParam(strCmd, 0); int p1 = GetIntParam(strCmd, 1); int p2 = GetIntParam(strCmd, 2); CMessage msg(DEMO_MSG::MSG_SET_OBJECT_POS, Int2To1(p1,p2), 0, &name); _SendMsg(msg); } break; //根据名称设置当前玩家角色 case DEMO::CMD_SET_CURRENT_PLAYER: { std::string name = GetStringParam(strCmd, 0); CMessage msg(DEMO_MSG::MSG_SET_CURRENT_PLAYER, 0, 0, &name); _SendMsg(msg); } break; //设置当前镜头左上角的世界坐标 case DEMO::CMD_SET_CAMERA_TO: //SetCameraTo(x,y) { int p1 = GetIntParam(strCmd, 0); int p2 = GetIntParam(strCmd, 1); _SendMsg(CMessage(DEMO_MSG::MSG_SET_CAMERA_TO, p1, p2)); // cout << "(" << p1 << ", " << p2 << ")"; } break; //对话命令 case DEMO::CMD_SAY: //Say("content","PicPath") { std::string strContent = GetStringParam(strCmd, 0); std::string strName = GetStringParam(strCmd, 1); CMessage msg(DEMO_MSG::MSG_SAY, 0, 0, &strContent, &strName); _SendMsg(msg); } break; case DEMO::CMD_DELAY: //Delay(time) { int p1 = GetIntParam(strCmd, 0); // cout << "(" << p1 << ")"; } break; //将地图镜头从当前移动到目的坐标(x,y) case DEMO::CMD_SCROLL_MAP_TO: //ScrollMapTo(x,y) { int p1 = GetIntParam(strCmd, 0); int p2 = GetIntParam(strCmd, 1); _SendMsg(CMessage(DEMO_MSG::MSG_SCROLL_MAP, Int2To1(p1, p2))); // cout << "(" << p1 << ", " << p2 << ")"; } break; //载入并切换到对应地图 case DEMO::CMD_LOAD_MAP: //LoadMap("MapPath") { std::string strMapPath = GetStringParam(strCmd, 0); _SendMsg(CMessage(DEMO_MSG::MSG_LOAD_MAP, 0, 0, &strMapPath)); // cout << "(\"" << strMapPath << "\")"; } break; case DEMO::CMD_SAVE_MAP: //SaveMap("MapPath") { std::string strMapPath = GetStringParam(strCmd, 0); // cout << "(\"" << strMapPath << "\")"; } break; //输出系统消息 case DEMO::CMD_GAME_MESSAGE: //GameMessage("content") { std::string strContent = GetStringParam(strCmd, 0); CMessage msg(DEMO_MSG::MSG_SHOW_GAME_MSG, 0, 0, &strContent); _SendMsg(msg); // cout << "(\"" << strContent << "\")"; } break; //播放游戏音乐 case DEMO::CMD_PLAY_MUSIC: //PlaySound("MusicPath",0/1) case DEMO::CMD_PLAY_EFFECT: { std::string strMusicName = GetStringParam(strCmd, 0); int p1 = GetIntParam(strCmd, 1); CMessage msg(DEMO_MSG::MSG_PLAY_SOUND, p1, 0, &strMusicName); _SendMsg(msg); // cout << "(\"" << strMusicName << "\")"; } break; //暂停游戏音乐 case DEMO::CMD_STOP_MUSIC: //StopSound() { std::string strMusicName = GetStringParam(strCmd, 0); CMessage msg(DEMO_MSG::MSG_STOP_MUSIC, 0, 0, &strMusicName); _SendMsg(msg); } //暂停游戏音乐 case DEMO::CMD_STOP_EFFECT: //StopEffect() { std::string strMusicName = GetStringParam(strCmd, 0); CMessage msg(DEMO_MSG::MSG_STOP_EFFECT, 0, 0, &strMusicName); _SendMsg(msg); } break; //设置游戏砖块对应属性的值 case DEMO::CMD_SET_MAP_TILE: //SetMapTile(x,y,"iTrap/tTrap/level/isBlock",value) { int x = GetIntParam(strCmd, 0); int y = GetIntParam(strCmd, 1); std::string strType = GetStringParam(strCmd, 2); int value = GetIntParam(strCmd, 3); _SendMsg(CMessage(DEMO_MSG::MSG_SET_MAP_TILE, Int2To1(x,y), value, &strType)); } break; //锁定当前屏幕,1锁定,0解除锁定 case DEMO::CMD_LOCK_SCREEN: //LockScreen(0/1) { int p1 = GetIntParam(strCmd, 0); _SendMsg(CMessage(DEMO_MSG::MSG_LOCK_SCREEN, p1)); } break; //开启选择框 case DEMO::CMD_OPEN_CHOOSE_PANEL: { std::string strTitle = GetStringParam(strCmd, 0); int p1 = GetIntParam(strCmd, 1); _SendMsg(CMessage(DEMO_MSG::MSG_OPEN_CHOOSE_PANEL, p1, 0, &strTitle)); } break; //设置选择框中的选项 case DEMO::CMD_SET_CHOOSE_ITEM: { std::string strTitle = GetStringParam(strCmd, 0); int p1 = GetIntParam(strCmd, 1); _SendMsg(CMessage(DEMO_MSG::MSG_SET_CHOOSE_ITEM, p1, 0, &strTitle)); } break; //暂停音乐 case DEMO::CMD_PAUSE_MUSIC: { std::string strMusicName = GetStringParam(strCmd, 0); _SendMsg(CMessage(DEMO_MSG::MSG_PAUSE_MUSIC, 0, 0, &strMusicName)); } break; //从暂停点,恢复播放音乐 case DEMO::CMD_RESUME_MUSIC: { std::string strMusicName = GetStringParam(strCmd, 0); _SendMsg(CMessage(DEMO_MSG::MSG_RESUME_MUSIC, 0, 0, &strMusicName)); } break; //设置音乐播放音量 case DEMO::CMD_SET_MUSIC_VOLUME: { std::string strMusicName = GetStringParam(strCmd, 0); int p1 = GetIntParam(strCmd, 1); _SendMsg(CMessage(DEMO_MSG::MSG_SET_MUSIC_VOLUME, p1, 0, &strMusicName)); } break; //将NPC恢复到其站立的上一个位置 case DEMO::CMD_RECOVER_OBJ_POS: { std::string strObjName = GetStringParam(strCmd, 0); _SendMsg(CMessage(DEMO_MSG::MSG_RECOVER_OBJ_POS, 0, 0, &strObjName)); } break; default: break; } // cout << endl; return true; }
void CCommandLineArgs::Set ( const char* szCommandLine ) { if (!szCommandLine || !szCommandLine[0]) return; std::string key; std::string data; char szTemp[512]; char *pTemp; int pos = 0; int len = (int)strlen(szCommandLine); while (pos < len) { pTemp = strchr(&szCommandLine[pos],'-'); if (!pTemp) pos = len; else { pos = (int)(pTemp-szCommandLine); pos += 1; pTemp=strchr(pTemp+1,' '); if (pTemp) { strncpy(szTemp,&szCommandLine[pos],pTemp-&szCommandLine[pos]); szTemp[pTemp-&szCommandLine[pos]]= '\0'; GetCommandName(szTemp); key=szTemp; pos = (int)(pTemp-szCommandLine+1); pTemp=strchr(pTemp+1,'-'); if (!pTemp) { // we are done pTemp = (char*)&szCommandLine[len]; strncpy(szTemp,&szCommandLine[pos],pTemp-&szCommandLine[pos]); szTemp[pTemp-&szCommandLine[pos]]= '\0'; pos = len; } else { // we are not done if (pTemp-&szCommandLine[pos] != 0) { strncpy(szTemp,&szCommandLine[pos],pTemp-&szCommandLine[pos]-1); szTemp[pTemp-&szCommandLine[pos]-1]= '\0'; pos = (int)(pTemp-szCommandLine); } else szTemp[0] = '\0'; } data = szTemp; commands[key]=data; } } } }
void TrafficDump(CDemoReader& reader, bool trafficStats) { InitCommandNames(); std::vector<unsigned> trafficCounter(55, 0); int frame = 0; int cmdId = 0; while (!reader.ReachedEnd()) { netcode::RawPacket* packet; packet = reader.GetData(3.40282347e+38f); if (packet == 0) continue; trafficCounter[packet->data[0]] += packet->length; const unsigned char* buffer = packet->data; char buf[16]; // FIXME: cba to look up how to format numbers with iostreams sprintf(buf, "%06d ", frame); std::cout << buf; switch ((unsigned char)buffer[0]) { case NETMSG_AICOMMAND: std::cout << "AICOMMAND: Playernum: " << (unsigned)buffer[3]; std::cout << " Length: " << (unsigned)packet->length; std::cout << " UnitId: " << *((short*)(buffer + 4)); cmdId = *((int*)(buffer + 6)); std::cout << " CommandId: " << GetCommandName(cmdId) << "(" << cmdId << ")"; std::cout << " Options: " << (unsigned)buffer[10]; std::cout << " Parameters:"; for (unsigned short i = 11; i < packet->length; i += 4) { std::cout << " " << *((float*)(buffer + i)); } std::cout << std::endl; break; case NETMSG_PLAYERNAME: std::cout << "PLAYERNAME: Playernum: " << (unsigned)buffer[2] << " Name: " << buffer+3 << std::endl; break; case NETMSG_SETPLAYERNUM: std::cout << "SETPLAYERNUM: Playernum: " << (unsigned)buffer[1] << std::endl; break; case NETMSG_QUIT: std::cout << "QUIT" << std::endl; break; case NETMSG_STARTPLAYING: std::cout << "STARTPLAYING" << std::endl; break; case NETMSG_STARTPOS: std::cout << "STARTPOS: Playernum: " << (unsigned)buffer[1] << " Team: " << (unsigned)buffer[2] << " Readyness: " << (unsigned)buffer[3] << std::endl; break; case NETMSG_SYSTEMMSG: std::cout << "SYSTEMMSG: Player: " << (unsigned)buffer[3] << " Msg: " << (char*)(buffer+4) << std::endl; break; case NETMSG_CHAT: std::cout << "CHAT: Player: " << (unsigned)buffer[2] << " Msg: " << (char*)(buffer+4) << std::endl; break; case NETMSG_KEYFRAME: std::cout << "KEYFRAME: " << *(int*)(buffer+1) << std::endl; ++frame; if (*(int*)(buffer+1) != frame) { std::cout << "keyframe mismatch!" << std::endl; } break; case NETMSG_NEWFRAME: std::cout << "NEWFRAME" << std::endl; ++frame; break; case NETMSG_PLAYERINFO: std::cout << "NETMSG_PLAYERINFO: Player:" << (int)buffer[1] << " Ping: " << *(uint16_t*)&buffer[6] << std::endl; break; case NETMSG_LUAMSG: std::cout << "LUAMSG length:" << packet->length << std::endl; break; case NETMSG_TEAM: std::cout << "TEAM Playernum:" << (int)buffer[1] << " Action:"; switch (buffer[2]) { case TEAMMSG_GIVEAWAY: std::cout << "GIVEAWAY"; break; case TEAMMSG_RESIGN: std::cout << "RESIGN"; break; case TEAMMSG_TEAM_DIED: std::cout << "TEAM_DIED"; break; case TEAMMSG_JOIN_TEAM: std::cout << "JOIN_TEAM"; break; default: std::cout << (int)buffer[2]; } std::cout << " Parameter:" << (int)buffer[3] << std::endl; break; case NETMSG_COMMAND: std::cout << "COMMAND Playernum:" << (int)buffer[3] << " Size: " << *(unsigned short*)(buffer+1) << std::endl; if (*(unsigned short*)(buffer+1) != packet->length) std::cout << " packet length error: expected: " << *(unsigned short*)(buffer+1) << " got: " << packet->length << std::endl; break; default: std::cout << "MSG: " << (unsigned)buffer[0] << std::endl; } delete packet; } // how many times did each message appear for (unsigned i = 0; i != trafficCounter.size(); ++i) { if (trafficStats && trafficCounter[i] > 0) std::cout << "Msg " << i << ": " << trafficCounter[i] << std::endl; } }
void TrafficDump(CDemoReader& reader, bool trafficStats) { InitCommandNames(); std::vector<unsigned> trafficCounter(NETMSG_LAST, 0); int frame = 0; int cmdId = 0; while (!reader.ReachedEnd()) { netcode::RawPacket* packet; packet = reader.GetData(3.402823466e+38f); if (packet == NULL) continue; assert(packet->data[0]<NETMSG_LAST); trafficCounter[packet->data[0]] += packet->length; const unsigned char* buffer = packet->data; char buf[16]; // FIXME: cba to look up how to format numbers with iostreams sprintf(buf, "%06d ", frame); std::cout << buf; const int cmd = (unsigned char)buffer[0]; switch (cmd) { case NETMSG_AICOMMAND: std::cout << "AICOMMAND: Playernum: " << (unsigned)buffer[3]; std::cout << " Length: " << (unsigned)packet->length; std::cout << " AI id: " << (unsigned)buffer[4]; std::cout << " UnitId: " << *((short*)(buffer + 5)); cmdId = *((int*)(buffer + 7)); std::cout << " CommandId: " << GetCommandName(cmdId) << "(" << cmdId << ")"; std::cout << " Options: " << (unsigned)buffer[11]; std::cout << " Parameters:"; for (unsigned short i = 12; i < packet->length; i += 4) { std::cout << " " << *((float*)(buffer + i)); } std::cout << std::endl; break; case NETMSG_AICOMMANDS: { std::cout << "AICOMMANDS: Playernum: " << (unsigned)buffer[3]; std::cout << " Length: " << (unsigned)packet->length; std::cout << " AI id: " << (unsigned)buffer[4]; std::cout << " Pair: " << (unsigned)buffer[5]; unsigned int sameid = *((unsigned int*)(buffer + 6)); std::cout << " SameID: " << sameid; unsigned int sameopt = (unsigned)buffer[10]; std::cout << " SameOpt: " << sameopt; unsigned short samesize = *((unsigned short*)(buffer + 11)); std::cout << " SameSize: " << samesize; short uidc = *((short*)(buffer + 13)); std::cout << " UnitIDCount: " << uidc; for (unsigned int i = 0; i < uidc; ++i) { std::cout << " " << *((short*)(buffer + 15 + i * 2)); } short cidc = *((short*)(buffer + 15 + uidc * 2)); int startp = 15 + uidc * 2 + 2; std::cout << " CmdIDCount: " << cidc; for (unsigned int i = 0; i < cidc; ++i) { if (sameid == 0) { std::cout << " " << *((unsigned int*)(buffer + startp)); startp += 4; } if (sameopt == 0xFF) { std::cout << " " << (unsigned)buffer[startp]; startp += 1; } if (sameopt == 0xFFFF) { std::cout << " " << *((unsigned short*)(buffer + startp)); startp += 2; } } std::cout << std::endl; break; } case NETMSG_PLAYERNAME: std::cout << "PLAYERNAME: Playernum: " << (unsigned)buffer[2] << " Name: " << buffer+3 << std::endl; break; case NETMSG_SETPLAYERNUM: std::cout << "SETPLAYERNUM: Playernum: " << (unsigned)buffer[1] << std::endl; break; case NETMSG_QUIT: std::cout << "QUIT" << std::endl; break; case NETMSG_STARTPLAYING: std::cout << "STARTPLAYING" << std::endl; break; case NETMSG_STARTPOS: std::cout << "STARTPOS: Playernum: " << (unsigned)buffer[1] << " Team: " << (unsigned)buffer[2] << " Readyness: " << (unsigned)buffer[3] << std::endl; break; case NETMSG_SYSTEMMSG: std::cout << "SYSTEMMSG: Player: " << (unsigned)buffer[3] << " Msg: " << (char*)(buffer+4) << std::endl; break; case NETMSG_CHAT: std::cout << "CHAT: Player: " << (unsigned)buffer[2] << " Msg: " << (char*)(buffer+4) << std::endl; break; case NETMSG_KEYFRAME: std::cout << "KEYFRAME: " << *(int*)(buffer+1) << std::endl; ++frame; if (*(int*)(buffer+1) != frame) { std::cout << "keyframe mismatch!" << std::endl; } break; case NETMSG_NEWFRAME: std::cout << "NEWFRAME" << std::endl; ++frame; break; case NETMSG_PLAYERINFO: std::cout << "NETMSG_PLAYERINFO: Player:" << (int)buffer[1] << " Ping: " << *(uint16_t*)&buffer[6] << std::endl; break; case NETMSG_LUAMSG: { std::cout << "LUAMSG length:" << packet->length << " Player:" << (unsigned)buffer[3] << " Script: " << *(uint16_t*)&buffer[4] << " Mode: " << (unsigned)buffer[6] << " Msg: "; PrintBinary(&packet->data[7], packet->length); std::cout << std::endl; break; } case NETMSG_TEAM: std::cout << "TEAM Playernum:" << (int)buffer[1] << " Action:"; switch (buffer[2]) { case TEAMMSG_GIVEAWAY: std::cout << "GIVEAWAY"; break; case TEAMMSG_RESIGN: std::cout << "RESIGN"; break; case TEAMMSG_TEAM_DIED: std::cout << "TEAM_DIED"; break; case TEAMMSG_JOIN_TEAM: std::cout << "JOIN_TEAM"; break; default: std::cout << (int)buffer[2]; } std::cout << " Parameter:" << (int)buffer[3] << std::endl; break; case NETMSG_COMMAND: std::cout << "COMMAND Playernum:" << (int)buffer[3] << " Size: " << *(unsigned short*)(buffer+1) << std::endl; if (*(unsigned short*)(buffer+1) != packet->length) std::cout << " packet length error: expected: " << *(unsigned short*)(buffer+1) << " got: " << packet->length << std::endl; break; case NETMSG_SELECT: std::cout << "NETMGS_SELECT: Playernum: " << (unsigned)buffer[3]; std::cout << " Length: " << (unsigned)packet->length; std::cout << " Unit IDs:"; for (unsigned short i = 4; i < packet->length; i += 2) { std::cout << " " << *((short*)(buffer + i)); } std::cout << std::endl; break; case NETMSG_GAMEOVER: std::cout << "NETMSG_GAMEOVER" << std::endl; break; case NETMSG_MAPDRAW: std::cout << "NETMSG_MAPDRAW" << std::endl; break; case NETMSG_PATH_CHECKSUM: std::cout << "NETMSG_PATH_CHECKSUM" << std::endl; break; case NETMSG_INTERNAL_SPEED: std::cout << "NETMSG_INTERNAL_SPEED" << std::endl; break; case NETMSG_PLAYERLEFT: std::cout << "NETMSG_PLAYERLEFT" << std::endl; break; case NETMSG_GAMEDATA: std::cout << "NETMSG_GAMEDATA" << std::endl; break; case NETMSG_CREATE_NEWPLAYER: std::cout << "NETMSG_CREATE_NEWPLAYER" << std::endl; break; case NETMSG_GAMEID: std::cout << "NETMSG_GAMEID" << std::endl; break; case NETMSG_RANDSEED: std::cout << "NETMSG_RANDSEED" << std::endl; break; case NETMSG_SHARE: std::cout << "NETMSG_SHARE: Playernum: " << (unsigned)buffer[1]; std::cout << " Team: " << (unsigned)buffer[2]; std::cout << " ShareUnits: " << (unsigned)buffer[3]; std::cout << " Metal: " << *(float*)(buffer + 4); std::cout << " Energy: " << *(float*)(buffer + 8); std::cout << std::endl; break; case NETMSG_CCOMMAND: std::cout << "NETMSG_CCOMMAND: " << std::endl; break; case NETMSG_PAUSE: std::cout << "NETMSG_PAUSE: Player " << (unsigned)buffer[1] << " paused: " << (unsigned)buffer[2] << std::endl; break; case NETMSG_SYNCRESPONSE: std::cout << "NETMSG_SYNCRESPONSE: " << std::endl; break; case NETMSG_DIRECT_CONTROL: std::cout << "NETMSG_DIRECT_CONTROL: " << std::endl; break; case NETMSG_SETSHARE: std::cout << "NETMSG_SETSHARE: " << std::endl; break; default: std::cout << "MSG: " << cmd << std::endl; } delete packet; } // how many times did each message appear for (unsigned i = 0; i != trafficCounter.size(); ++i) { if (trafficStats && trafficCounter[i] > 0) std::cout << "Msg " << i << ": " << trafficCounter[i] << std::endl; } }
/*! * Helper function for the toplevel network safe docommand function for the current company. * * @param tile The tile to perform a command on (see #CommandProc) * @param p1 Additional data for the command (see #CommandProc) * @param p2 Additional data for the command (see #CommandProc) * @param cmd The command to execute (a CMD_* value) * @param callback A callback function to call after the command is finished * @param text The text to pass * @param my_cmd indicator if the command is from a company or server (to display error messages for a user) * @param estimate_only whether to give only the estimate or also execute the command * @return the command cost of this function. */ CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd, bool estimate_only) { /* Prevent recursion; it gives a mess over the network */ assert(_docommand_recursive == 0); _docommand_recursive = 1; /* Reset the state. */ _additional_cash_required = 0; /* Get pointer to command handler */ byte cmd_id = cmd & CMD_ID_MASK; assert(cmd_id < lengthof(_command_proc_table)); CommandProc *proc = _command_proc_table[cmd_id].proc; /* Shouldn't happen, but you never know when someone adds * NULLs to the _command_proc_table. */ assert(proc != NULL); /* Command flags are used internally */ CommandFlags cmd_flags = GetCommandFlags(cmd); /* Flags get send to the DoCommand */ DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags); #ifdef ENABLE_NETWORK /* Make sure p2 is properly set to a ClientID. */ assert(!(cmd_flags & CMD_CLIENT_ID) || p2 != 0); #endif /* Do not even think about executing out-of-bounds tile-commands */ if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (cmd_flags & CMD_ALL_TILES) == 0))) return_dcpi(CMD_ERROR, false); /* Always execute server and spectator commands as spectator */ bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0; /* If the company isn't valid it may only do server command or start a new company! * The server will ditch any server commands a client sends to it, so effectively * this guards the server from executing functions for an invalid company. */ if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) { return_dcpi(CMD_ERROR, false); } Backup<CompanyByte> cur_company(_current_company, FILE_LINE); if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR); bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0; /* Test the command. */ _cleared_object_areas.Clear(); SetTownRatingTestMode(true); ClearStorageChanges(false); CommandCost res = proc(tile, flags, p1, p2, text); SetTownRatingTestMode(false); /* Make sure we're not messing things up here. */ assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify()); /* If the command fails, we're doing an estimate * or the player does not have enough money * (unless it's a command where the test and * execution phase might return different costs) * we bail out here. */ if (res.Failed() || estimate_only || (!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) { if (!_networking || _generating_world || (cmd & CMD_NETWORK_COMMAND) != 0) { /* Log the failed command as well. Just to be able to be find * causes of desyncs due to bad command test implementations. */ DEBUG(desync, 1, "cmdf: %08x; %02x; %02x; %06x; %08x; %08x; %08x; \"%s\" (%s)", _date, _date_fract, (int)_current_company, tile, p1, p2, cmd & ~CMD_NETWORK_COMMAND, text, GetCommandName(cmd)); } cur_company.Restore(); return_dcpi(res, false); } #ifdef ENABLE_NETWORK /* * If we are in network, and the command is not from the network * send it to the command-queue and abort execution */ if (_networking && !_generating_world && !(cmd & CMD_NETWORK_COMMAND)) { NetworkSendCommand(tile, p1, p2, cmd & ~CMD_FLAGS_MASK, callback, text, _current_company); cur_company.Restore(); /* Don't return anything special here; no error, no costs. * This way it's not handled by DoCommand and only the * actual execution of the command causes messages. Also * reset the storages as we've not executed the command. */ return_dcpi(CommandCost(), false); } #endif /* ENABLE_NETWORK */ DEBUG(desync, 1, "cmd: %08x; %02x; %02x; %06x; %08x; %08x; %08x; \"%s\" (%s)", _date, _date_fract, (int)_current_company, tile, p1, p2, cmd & ~CMD_NETWORK_COMMAND, text, GetCommandName(cmd)); /* Actually try and execute the command. If no cost-type is given * use the construction one */ _cleared_object_areas.Clear(); ClearStorageChanges(false); CommandCost res2 = proc(tile, flags | DC_EXEC, p1, p2, text); if (cmd_id == CMD_COMPANY_CTRL) { cur_company.Trash(); /* We are a new company -> Switch to new local company. * We were closed down -> Switch to spectator * Some other company opened/closed down -> The outside function will switch back */ _current_company = _local_company; } else { /* Make sure nothing bad happened, like changing the current company. */ assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify()); cur_company.Restore(); } /* If the test and execution can differ we have to check the * return of the command. Otherwise we can check whether the * test and execution have yielded the same result, * i.e. cost and error state are the same. */ if (!test_and_exec_can_differ) { assert(res.GetCost() == res2.GetCost() && res.Failed() == res2.Failed()); // sanity check } else if (res2.Failed()) { return_dcpi(res2, false); } /* If we're needing more money and we haven't done * anything yet, ask for the money! */ if (_additional_cash_required != 0 && res2.GetCost() == 0) { /* It could happen we removed rail, thus gained money, and deleted something else. * So make sure the signal buffer is empty even in this case */ UpdateSignalsInBuffer(); SetDParam(0, _additional_cash_required); return_dcpi(CommandCost(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY), false); } /* update last build coordinate of company. */ if (tile != 0) { Company *c = Company::GetIfValid(_current_company); if (c != NULL) c->last_build_coordinate = tile; } SubtractMoneyFromCompany(res2); /* update signals if needed */ UpdateSignalsInBuffer(); return_dcpi(res2, true); }