Updated Copyright year to 2013
[getmangos.git] / src / game / debugcmds.cpp
blob712f552d28739368bfcc1dcf50e4f01fc4f6e2f9
1 /*
2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Database/DatabaseEnv.h"
21 #include "WorldPacket.h"
22 #include "Player.h"
23 #include "Opcodes.h"
24 #include "Chat.h"
25 #include "Log.h"
26 #include "Unit.h"
27 #include "GossipDef.h"
28 #include "Language.h"
29 #include "BattleGround/BattleGroundMgr.h"
30 #include <fstream>
31 #include "ObjectMgr.h"
32 #include "ObjectGuid.h"
33 #include "SpellMgr.h"
35 bool ChatHandler::HandleDebugSendSpellFailCommand(char* args)
37 if (!*args)
38 return false;
40 uint32 failnum;
41 if (!ExtractUInt32(&args, failnum) || failnum > 255)
42 return false;
44 uint32 failarg1;
45 if (!ExtractOptUInt32(&args, failarg1, 0))
46 return false;
48 uint32 failarg2;
49 if (!ExtractOptUInt32(&args, failarg2, 0))
50 return false;
52 WorldPacket data(SMSG_CAST_FAILED, 5);
53 data << uint8(0);
54 data << uint32(133);
55 data << uint8(failnum);
56 if (failarg1 || failarg2)
57 data << uint32(failarg1);
58 if (failarg2)
59 data << uint32(failarg2);
61 m_session->SendPacket(&data);
63 return true;
66 bool ChatHandler::HandleDebugSendPoiCommand(char* args)
68 Player* pPlayer = m_session->GetPlayer();
69 Unit* target = getSelectedUnit();
70 if (!target)
72 SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
73 return true;
76 uint32 icon;
77 if (!ExtractUInt32(&args, icon))
78 return false;
80 uint32 flags;
81 if (!ExtractUInt32(&args, flags))
82 return false;
84 DETAIL_LOG("Command : POI, NPC = %u, icon = %u flags = %u", target->GetGUIDLow(), icon, flags);
85 pPlayer->PlayerTalkClass->SendPointOfInterest(target->GetPositionX(), target->GetPositionY(), Poi_Icon(icon), flags, 30, "Test POI");
86 return true;
89 bool ChatHandler::HandleDebugSendEquipErrorCommand(char* args)
91 if (!*args)
92 return false;
94 uint8 msg = atoi(args);
95 m_session->GetPlayer()->SendEquipError(InventoryResult(msg), NULL, NULL);
96 return true;
99 bool ChatHandler::HandleDebugSendSellErrorCommand(char* args)
101 if (!*args)
102 return false;
104 uint8 msg = atoi(args);
105 m_session->GetPlayer()->SendSellError(SellResult(msg), 0, ObjectGuid(), 0);
106 return true;
109 bool ChatHandler::HandleDebugSendBuyErrorCommand(char* args)
111 if (!*args)
112 return false;
114 uint8 msg = atoi(args);
115 m_session->GetPlayer()->SendBuyError(BuyResult(msg), 0, 0, 0);
116 return true;
119 bool ChatHandler::HandleDebugSendOpcodeCommand(char* /*args*/)
121 Unit* unit = getSelectedUnit();
122 if (!unit || (unit->GetTypeId() != TYPEID_PLAYER))
123 unit = m_session->GetPlayer();
125 std::ifstream ifs("opcode.txt");
126 if (ifs.bad())
127 return false;
129 uint32 opcode;
130 ifs >> opcode;
132 WorldPacket data(opcode, 0);
134 while (!ifs.eof())
136 std::string type;
137 ifs >> type;
139 if (type == "")
140 break;
142 if (type == "uint8")
144 uint16 val1;
145 ifs >> val1;
146 data << uint8(val1);
148 else if (type == "uint16")
150 uint16 val2;
151 ifs >> val2;
152 data << val2;
154 else if (type == "uint32")
156 uint32 val3;
157 ifs >> val3;
158 data << val3;
160 else if (type == "uint64")
162 uint64 val4;
163 ifs >> val4;
164 data << val4;
166 else if (type == "float")
168 float val5;
169 ifs >> val5;
170 data << val5;
172 else if (type == "string")
174 std::string val6;
175 ifs >> val6;
176 data << val6;
178 else if (type == "pguid")
180 data << unit->GetPackGUID();
182 else
184 DEBUG_LOG("Sending opcode: unknown type '%s'", type.c_str());
185 break;
188 ifs.close();
189 DEBUG_LOG("Sending opcode %u", data.GetOpcode());
190 data.hexlike();
191 ((Player*)unit)->GetSession()->SendPacket(&data);
192 PSendSysMessage(LANG_COMMAND_OPCODESENT, data.GetOpcode(), unit->GetName());
193 return true;
196 bool ChatHandler::HandleDebugUpdateWorldStateCommand(char* args)
198 uint32 world;
199 if (!ExtractUInt32(&args, world))
200 return false;
202 uint32 state;
203 if (!ExtractUInt32(&args, state))
204 return false;
206 m_session->GetPlayer()->SendUpdateWorldState(world, state);
207 return true;
210 bool ChatHandler::HandleDebugPlayCinematicCommand(char* args)
212 // USAGE: .debug play cinematic #cinematicid
213 // #cinematicid - ID decimal number from CinemaicSequences.dbc (1st column)
214 uint32 dwId;
215 if (!ExtractUInt32(&args, dwId))
216 return false;
218 if (!sCinematicSequencesStore.LookupEntry(dwId))
220 PSendSysMessage(LANG_CINEMATIC_NOT_EXIST, dwId);
221 SetSentErrorMessage(true);
222 return false;
225 m_session->GetPlayer()->SendCinematicStart(dwId);
226 return true;
229 bool ChatHandler::HandleDebugPlayMovieCommand(char* args)
231 // USAGE: .debug play movie #movieid
232 // #movieid - ID decimal number from Movie.dbc (1st column)
233 uint32 dwId;
234 if (!ExtractUInt32(&args, dwId))
235 return false;
237 if (!sMovieStore.LookupEntry(dwId))
239 PSendSysMessage(LANG_MOVIE_NOT_EXIST, dwId);
240 SetSentErrorMessage(true);
241 return false;
244 m_session->GetPlayer()->SendMovieStart(dwId);
245 return true;
248 // Play sound
249 bool ChatHandler::HandleDebugPlaySoundCommand(char* args)
251 // USAGE: .debug playsound #soundid
252 // #soundid - ID decimal number from SoundEntries.dbc (1st column)
253 uint32 dwSoundId;
254 if (!ExtractUInt32(&args, dwSoundId))
255 return false;
257 if (!sSoundEntriesStore.LookupEntry(dwSoundId))
259 PSendSysMessage(LANG_SOUND_NOT_EXIST, dwSoundId);
260 SetSentErrorMessage(true);
261 return false;
264 Unit* unit = getSelectedUnit();
265 if (!unit)
267 SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
268 SetSentErrorMessage(true);
269 return false;
272 if (m_session->GetPlayer()->GetSelectionGuid())
273 unit->PlayDistanceSound(dwSoundId, m_session->GetPlayer());
274 else
275 unit->PlayDirectSound(dwSoundId, m_session->GetPlayer());
277 PSendSysMessage(LANG_YOU_HEAR_SOUND, dwSoundId);
278 return true;
281 // Send notification in channel
282 bool ChatHandler::HandleDebugSendChannelNotifyCommand(char* args)
284 const char* name = "test";
286 uint32 code;
287 if (!ExtractUInt32(&args, code) || code > 255)
288 return false;
290 WorldPacket data(SMSG_CHANNEL_NOTIFY, (1 + 10));
291 data << uint8(code); // notify type
292 data << name; // channel name
293 data << uint32(0);
294 data << uint32(0);
295 m_session->SendPacket(&data);
296 return true;
299 // Send notification in chat
300 bool ChatHandler::HandleDebugSendChatMsgCommand(char* args)
302 const char* msg = "testtest";
304 uint32 type;
305 if (!ExtractUInt32(&args, type) || type > 255)
306 return false;
308 WorldPacket data;
309 ChatHandler::FillMessageData(&data, m_session, type, 0, "chan", m_session->GetPlayer()->GetObjectGuid(), msg, m_session->GetPlayer());
310 m_session->SendPacket(&data);
311 return true;
314 bool ChatHandler::HandleDebugSendQuestPartyMsgCommand(char* args)
316 uint32 msg;
317 if (!ExtractUInt32(&args, msg))
318 return false;
320 m_session->GetPlayer()->SendPushToPartyResponse(m_session->GetPlayer(), msg);
321 return true;
324 bool ChatHandler::HandleDebugGetLootRecipientCommand(char* /*args*/)
326 Creature* target = getSelectedCreature();
327 if (!target)
328 return false;
330 if (!target->HasLootRecipient())
331 SendSysMessage("loot recipient: no loot recipient");
332 else if (Player* recipient = target->GetLootRecipient())
333 PSendSysMessage("loot recipient: %s with raw data %s from group %u",
334 recipient->GetGuidStr().c_str(),
335 target->GetLootRecipientGuid().GetString().c_str(),
336 target->GetLootGroupRecipientId());
337 else
338 SendSysMessage("loot recipient: offline ");
340 return true;
343 bool ChatHandler::HandleDebugSendQuestInvalidMsgCommand(char* args)
345 uint32 msg = atol(args);
346 m_session->GetPlayer()->SendCanTakeQuestResponse(msg);
347 return true;
350 bool ChatHandler::HandleDebugGetItemStateCommand(char* args)
352 if (!*args)
353 return false;
355 ItemUpdateState state = ITEM_UNCHANGED;
356 bool list_queue = false, check_all = false;
358 std::string state_str;
360 if (strncmp(args, "unchanged", strlen(args)) == 0)
362 state = ITEM_UNCHANGED;
363 state_str = "unchanged";
365 else if (strncmp(args, "changed", strlen(args)) == 0)
367 state = ITEM_CHANGED;
368 state_str = "changed";
370 else if (strncmp(args, "new", strlen(args)) == 0)
372 state = ITEM_NEW;
373 state_str = "new";
375 else if (strncmp(args, "removed", strlen(args)) == 0)
377 state = ITEM_REMOVED;
378 state_str = "removed";
380 else if (strncmp(args, "queue", strlen(args)) == 0)
381 list_queue = true;
382 else if (strncmp(args, "all", strlen(args)) == 0)
383 check_all = true;
384 else
385 return false;
387 Player* player = getSelectedPlayer();
388 if (!player) player = m_session->GetPlayer();
390 if (!list_queue && !check_all)
392 state_str = "The player has the following " + state_str + " items: ";
393 SendSysMessage(state_str.c_str());
394 for (uint8 i = PLAYER_SLOT_START; i < PLAYER_SLOT_END; ++i)
396 if (i >= BUYBACK_SLOT_START && i < BUYBACK_SLOT_END)
397 continue;
399 Item* item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, i);
400 if (!item) continue;
401 if (!item->IsBag())
403 if (item->GetState() == state)
404 PSendSysMessage("%s bag: 255 slot: %u owner: %s",
405 item->GetGuidStr().c_str(), item->GetSlot(), item->GetOwnerGuid().GetString().c_str());
407 else
409 Bag* bag = (Bag*)item;
410 for (uint8 j = 0; j < bag->GetBagSize(); ++j)
412 Item* item2 = bag->GetItemByPos(j);
413 if (item2 && item2->GetState() == state)
414 PSendSysMessage("%s bag: %u slot: %u owner: %s",
415 item2->GetGuidStr().c_str(), item2->GetBagSlot(), item2->GetSlot(),
416 item2->GetOwnerGuid().GetString().c_str());
422 if (list_queue)
424 std::vector<Item*>& updateQueue = player->GetItemUpdateQueue();
425 for (size_t i = 0; i < updateQueue.size(); ++i)
427 Item* item = updateQueue[i];
428 if (!item) continue;
430 Bag* container = item->GetContainer();
431 uint8 bag_slot = container ? container->GetSlot() : uint8(INVENTORY_SLOT_BAG_0);
433 std::string st;
434 switch (item->GetState())
436 case ITEM_UNCHANGED: st = "unchanged"; break;
437 case ITEM_CHANGED: st = "changed"; break;
438 case ITEM_NEW: st = "new"; break;
439 case ITEM_REMOVED: st = "removed"; break;
442 PSendSysMessage("%s bag: %u slot: %u - state: %s",
443 item->GetGuidStr().c_str(), bag_slot, item->GetSlot(), st.c_str());
445 if (updateQueue.empty())
446 PSendSysMessage("updatequeue empty");
449 if (check_all)
451 bool error = false;
452 std::vector<Item*>& updateQueue = player->GetItemUpdateQueue();
453 for (uint8 i = PLAYER_SLOT_START; i < PLAYER_SLOT_END; ++i)
455 if (i >= BUYBACK_SLOT_START && i < BUYBACK_SLOT_END)
456 continue;
458 Item* item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, i);
459 if (!item) continue;
461 if (item->GetSlot() != i)
463 PSendSysMessage("%s at slot %u has an incorrect slot value: %d",
464 item->GetGuidStr().c_str(), i, item->GetSlot());
465 error = true; continue;
468 if (item->GetOwnerGuid() != player->GetObjectGuid())
470 PSendSysMessage("%s at slot %u owner (%s) and inventory owner (%s) don't match!",
471 item->GetGuidStr().c_str(), item->GetSlot(),
472 item->GetOwnerGuid().GetString().c_str(), player->GetGuidStr().c_str());
473 error = true; continue;
476 if (Bag* container = item->GetContainer())
478 PSendSysMessage("%s at slot %u has a container %s from slot %u but shouldnt!",
479 item->GetGuidStr().c_str(), item->GetSlot(),
480 container->GetGuidStr().c_str(), container->GetSlot());
481 error = true; continue;
484 if (item->IsInUpdateQueue())
486 uint16 qp = item->GetQueuePos();
487 if (qp > updateQueue.size())
489 PSendSysMessage("%s at slot %u has a queuepos (%d) larger than the update queue size! ",
490 item->GetGuidStr().c_str(), item->GetSlot(), qp);
491 error = true; continue;
494 if (updateQueue[qp] == NULL)
496 PSendSysMessage("%s at slot %u has a queuepos (%d) that points to NULL in the queue!",
497 item->GetGuidStr().c_str(), item->GetSlot(), qp);
498 error = true; continue;
501 if (updateQueue[qp] != item)
503 PSendSysMessage("%s at slot %u has a queuepos (%d) that points to %s in the queue (bag %u, slot %u)",
504 item->GetGuidStr().c_str(), item->GetSlot(), qp,
505 updateQueue[qp]->GetGuidStr().c_str(), updateQueue[qp]->GetBagSlot(), updateQueue[qp]->GetSlot());
506 error = true; continue;
509 else if (item->GetState() != ITEM_UNCHANGED)
511 PSendSysMessage("%s at slot %u is not in queue but should be (state: %d)!",
512 item->GetGuidStr().c_str(), item->GetSlot(), item->GetState());
513 error = true; continue;
516 if (item->IsBag())
518 Bag* bag = (Bag*)item;
519 for (uint8 j = 0; j < bag->GetBagSize(); ++j)
521 Item* item2 = bag->GetItemByPos(j);
522 if (!item2) continue;
524 if (item2->GetSlot() != j)
526 PSendSysMessage("%s in bag %u at slot %u has an incorrect slot value: %u",
527 item2->GetGuidStr().c_str(), bag->GetSlot(), j, item2->GetSlot());
528 error = true; continue;
531 if (item2->GetOwnerGuid() != player->GetObjectGuid())
533 PSendSysMessage("%s in bag %u at slot %u owner (%s) and inventory owner (%s) don't match!",
534 item2->GetGuidStr().c_str(), bag->GetSlot(), item2->GetSlot(),
535 item2->GetOwnerGuid().GetString().c_str(), player->GetGuidStr().c_str());
536 error = true; continue;
539 Bag* container = item2->GetContainer();
540 if (!container)
542 PSendSysMessage("%s in bag %u at slot %u has no container!",
543 item2->GetGuidStr().c_str(), bag->GetSlot(), item2->GetSlot());
544 error = true; continue;
547 if (container != bag)
549 PSendSysMessage("%s in bag %u at slot %u has a different container %s from slot %u!",
550 item2->GetGuidStr().c_str(), bag->GetSlot(), item2->GetSlot(),
551 container->GetGuidStr().c_str(), container->GetSlot());
552 error = true; continue;
555 if (item2->IsInUpdateQueue())
557 uint16 qp = item2->GetQueuePos();
558 if (qp > updateQueue.size())
560 PSendSysMessage("%s in bag %u at slot %u has a queuepos (%d) larger than the update queue size! ",
561 item2->GetGuidStr().c_str(), bag->GetSlot(), item2->GetSlot(), qp);
562 error = true; continue;
565 if (updateQueue[qp] == NULL)
567 PSendSysMessage("%s in bag %u at slot %u has a queuepos (%d) that points to NULL in the queue!",
568 item2->GetGuidStr().c_str(), bag->GetSlot(), item2->GetSlot(), qp);
569 error = true; continue;
572 if (updateQueue[qp] != item2)
574 PSendSysMessage("%s in bag %u at slot %u has a queuepos (%d) that points to %s in the queue (bag %u slot %u)",
575 item2->GetGuidStr().c_str(), bag->GetSlot(), item2->GetSlot(), qp,
576 updateQueue[qp]->GetGuidStr().c_str(), updateQueue[qp]->GetBagSlot(), updateQueue[qp]->GetSlot());
577 error = true; continue;
580 else if (item2->GetState() != ITEM_UNCHANGED)
582 PSendSysMessage("%s in bag %u at slot %u is not in queue but should be (state: %d)!",
583 item2->GetGuidStr().c_str(), bag->GetSlot(), item2->GetSlot(), item2->GetState());
584 error = true; continue;
590 for (size_t i = 0; i < updateQueue.size(); ++i)
592 Item* item = updateQueue[i];
593 if (!item) continue;
595 if (item->GetOwnerGuid() != player->GetObjectGuid())
597 PSendSysMessage("queue(" SIZEFMTD "): %s has the owner (%s) and inventory owner (%s) don't match!",
598 i, item->GetGuidStr().c_str(),
599 item->GetOwnerGuid().GetString().c_str(), player->GetGuidStr().c_str());
600 error = true; continue;
603 if (item->GetQueuePos() != i)
605 PSendSysMessage("queue(" SIZEFMTD "): %s has queuepos doesn't match it's position in the queue!",
606 i, item->GetGuidStr().c_str());
607 error = true; continue;
610 if (item->GetState() == ITEM_REMOVED) continue;
611 Item* test = player->GetItemByPos(item->GetBagSlot(), item->GetSlot());
613 if (test == NULL)
615 PSendSysMessage("queue(" SIZEFMTD "): %s has incorrect (bag %u slot %u) values, the player doesn't have an item at that position!",
616 i, item->GetGuidStr().c_str(), item->GetBagSlot(), item->GetSlot());
617 error = true; continue;
620 if (test != item)
622 PSendSysMessage("queue(" SIZEFMTD "): %s has incorrect (bag %u slot %u) values, the %s is there instead!",
623 i, item->GetGuidStr().c_str(), item->GetBagSlot(), item->GetSlot(),
624 test->GetGuidStr().c_str());
625 error = true; continue;
628 if (!error)
629 SendSysMessage("All OK!");
632 return true;
635 bool ChatHandler::HandleDebugBattlegroundCommand(char* /*args*/)
637 sBattleGroundMgr.ToggleTesting();
638 return true;
641 bool ChatHandler::HandleDebugArenaCommand(char* /*args*/)
643 sBattleGroundMgr.ToggleArenaTesting();
644 return true;
647 bool ChatHandler::HandleDebugSpellCheckCommand(char* /*args*/)
649 sLog.outString("Check expected in code spell properties base at table 'spell_check' content...");
650 sSpellMgr.CheckUsedSpells("spell_check");
651 return true;
654 bool ChatHandler::HandleDebugSendLargePacketCommand(char* /*args*/)
656 const char* stuffingString = "This is a dummy string to push the packet's size beyond 128000 bytes. ";
657 std::ostringstream ss;
658 while (ss.str().size() < 128000)
659 ss << stuffingString;
660 SendSysMessage(ss.str().c_str());
661 return true;
664 bool ChatHandler::HandleDebugSendSetPhaseShiftCommand(char* args)
666 if (!*args)
667 return false;
669 char* m = strtok((char*)args, " ");
670 char* p = strtok(NULL, " ");
672 uint16 MapId = atoi(m);
673 uint32 PhaseShift = atoi(p);
674 m_session->SendSetPhaseShift(PhaseShift, MapId);
675 return true;
678 // show animation
679 bool ChatHandler::HandleDebugAnimCommand(char* args)
681 uint32 emote_id;
682 if (!ExtractUInt32(&args, emote_id))
683 return false;
685 m_session->GetPlayer()->HandleEmoteCommand(emote_id);
686 return true;
689 bool ChatHandler::HandleDebugSetAuraStateCommand(char* args)
691 int32 state;
692 if (!ExtractInt32(&args, state))
693 return false;
695 Unit* unit = getSelectedUnit();
696 if (!unit)
698 SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
699 SetSentErrorMessage(true);
700 return false;
703 if (!state)
705 // reset all states
706 for (int i = 1; i <= 32; ++i)
707 unit->ModifyAuraState(AuraState(i), false);
708 return true;
711 unit->ModifyAuraState(AuraState(abs(state)), state > 0);
712 return true;
715 bool ChatHandler::HandleSetValueHelper(Object* target, uint32 field, char* typeStr, char* valStr)
717 ObjectGuid guid = target->GetObjectGuid();
719 // not allow access to nonexistent or critical for work field
720 if (field >= target->GetValuesCount() || field <= OBJECT_FIELD_ENTRY)
722 PSendSysMessage(LANG_TOO_BIG_INDEX, field, guid.GetString().c_str(), target->GetValuesCount());
723 return false;
726 uint32 base; // 0 -> float
727 if (!typeStr)
728 base = 10;
729 else if (strncmp(typeStr, "int", strlen(typeStr)) == 0)
730 base = 10;
731 else if (strncmp(typeStr, "hex", strlen(typeStr)) == 0)
732 base = 16;
733 else if (strncmp(typeStr, "bit", strlen(typeStr)) == 0)
734 base = 2;
735 else if (strncmp(typeStr, "float", strlen(typeStr)) == 0)
736 base = 0;
737 else
738 return false;
740 if (base)
742 uint32 iValue;
743 if (!ExtractUInt32Base(&valStr, iValue, base))
744 return false;
746 DEBUG_LOG(GetMangosString(LANG_SET_UINT), guid.GetString().c_str(), field, iValue);
747 target->SetUInt32Value(field , iValue);
748 PSendSysMessage(LANG_SET_UINT_FIELD, guid.GetString().c_str(), field, iValue);
750 else
752 float fValue;
753 if (!ExtractFloat(&valStr, fValue))
754 return false;
756 DEBUG_LOG(GetMangosString(LANG_SET_FLOAT), guid.GetString().c_str(), field, fValue);
757 target->SetFloatValue(field , fValue);
758 PSendSysMessage(LANG_SET_FLOAT_FIELD, guid.GetString().c_str(), field, fValue);
761 return true;
764 bool ChatHandler::HandleDebugSetItemValueCommand(char* args)
766 uint32 guid;
767 if (!ExtractUInt32(&args, guid))
768 return false;
770 uint32 field;
771 if (!ExtractUInt32(&args, field))
772 return false;
774 char* typeStr = ExtractOptNotLastArg(&args);
775 if (!typeStr)
776 return false;
778 char* valStr = ExtractLiteralArg(&args);
779 if (!valStr)
780 return false;
782 Item* item = m_session->GetPlayer()->GetItemByGuid(ObjectGuid(HIGHGUID_ITEM, guid));
783 if (!item)
784 return false;
786 return HandleSetValueHelper(item, field, typeStr, valStr);
789 bool ChatHandler::HandleDebugSetValueCommand(char* args)
791 Unit* target = getSelectedUnit();
792 if (!target)
794 SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
795 SetSentErrorMessage(true);
796 return false;
799 uint32 field;
800 if (!ExtractUInt32(&args, field))
801 return false;
803 char* typeStr = ExtractOptNotLastArg(&args);
804 if (!typeStr)
805 return false;
807 char* valStr = ExtractLiteralArg(&args);
808 if (!valStr)
809 return false;
811 return HandleSetValueHelper(target, field, typeStr, valStr);
814 bool ChatHandler::HandleGetValueHelper(Object* target, uint32 field, char* typeStr)
816 ObjectGuid guid = target->GetObjectGuid();
818 if (field >= target->GetValuesCount())
820 PSendSysMessage(LANG_TOO_BIG_INDEX, field, guid.GetString().c_str(), target->GetValuesCount());
821 return false;
824 uint32 base; // 0 -> float
825 if (!typeStr)
826 base = 10;
827 else if (strncmp(typeStr, "int", strlen(typeStr)) == 0)
828 base = 10;
829 else if (strncmp(typeStr, "hex", strlen(typeStr)) == 0)
830 base = 16;
831 else if (strncmp(typeStr, "bit", strlen(typeStr)) == 0)
832 base = 2;
833 else if (strncmp(typeStr, "float", strlen(typeStr)) == 0)
834 base = 0;
835 else
836 return false;
838 if (base)
840 uint32 iValue = target->GetUInt32Value(field);
842 switch (base)
844 case 2:
846 // starting 0 if need as required bitstring format
847 std::string res;
848 res.reserve(1 + 32 + 1);
849 res = iValue & (1 << (32 - 1)) ? "0" : " ";
850 for (int i = 32; i > 0; --i)
851 res += iValue & (1 << (i - 1)) ? "1" : "0";
852 DEBUG_LOG(GetMangosString(LANG_GET_BITSTR), guid.GetString().c_str(), field, res.c_str());
853 PSendSysMessage(LANG_GET_BITSTR_FIELD, guid.GetString().c_str(), field, res.c_str());
854 break;
856 case 16:
857 DEBUG_LOG(GetMangosString(LANG_GET_HEX), guid.GetString().c_str(), field, iValue);
858 PSendSysMessage(LANG_GET_HEX_FIELD, guid.GetString().c_str(), field, iValue);
859 break;
860 case 10:
861 default:
862 DEBUG_LOG(GetMangosString(LANG_GET_UINT), guid.GetString().c_str(), field, iValue);
863 PSendSysMessage(LANG_GET_UINT_FIELD, guid.GetString().c_str(), field, iValue);
866 else
868 float fValue = target->GetFloatValue(field);
869 DEBUG_LOG(GetMangosString(LANG_GET_FLOAT), guid.GetString().c_str(), field, fValue);
870 PSendSysMessage(LANG_GET_FLOAT_FIELD, guid.GetString().c_str(), field, fValue);
873 return true;
876 bool ChatHandler::HandleDebugGetItemValueCommand(char* args)
878 uint32 guid;
879 if (!ExtractUInt32(&args, guid))
880 return false;
882 uint32 field;
883 if (!ExtractUInt32(&args, field))
884 return false;
886 char* typeStr = ExtractLiteralArg(&args);
887 if (!typeStr && *args) // optional arg but check format fail case
888 return false;
890 Item* item = m_session->GetPlayer()->GetItemByGuid(ObjectGuid(HIGHGUID_ITEM, guid));
891 if (!item)
892 return false;
894 return HandleGetValueHelper(item, field, typeStr);
897 bool ChatHandler::HandleDebugGetValueCommand(char* args)
899 Unit* target = getSelectedUnit();
900 if (!target)
902 SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
903 SetSentErrorMessage(true);
904 return false;
907 uint32 field;
908 if (!ExtractUInt32(&args, field))
909 return false;
911 char* typeStr = ExtractLiteralArg(&args);
912 if (!typeStr && *args) // optional arg but check format fail case
913 return false;
915 return HandleGetValueHelper(target, field, typeStr);
918 bool ChatHandler::HandlerDebugModValueHelper(Object* target, uint32 field, char* typeStr, char* valStr)
920 ObjectGuid guid = target->GetObjectGuid();
922 // not allow access to nonexistent or critical for work field
923 if (field >= target->GetValuesCount() || field <= OBJECT_FIELD_ENTRY)
925 PSendSysMessage(LANG_TOO_BIG_INDEX, field, guid.GetString().c_str(), target->GetValuesCount());
926 return false;
929 uint32 type; // 0 -> float 1 -> int add 2-> bit or 3 -> bit and 4 -> bit and not
930 if (strncmp(typeStr, "int", strlen(typeStr)) == 0)
931 type = 1;
932 else if (strncmp(typeStr, "float", strlen(typeStr)) == 0)
933 type = 0;
934 else if (strncmp(typeStr, "|=", strlen("|=") + 1) == 0) // exactly copy
935 type = 2;
936 else if (strncmp(typeStr, "&=", strlen("&=") + 1) == 0) // exactly copy
937 type = 3;
938 else if (strncmp(typeStr, "&=~", strlen("&=~") + 1) == 0) // exactly copy
939 type = 4;
940 else
941 return false;
943 if (type)
945 uint32 iValue;
946 if (!ExtractUInt32Base(&valStr, iValue, type == 1 ? 10 : 16))
947 return false;
949 uint32 value = target->GetUInt32Value(field);
951 switch (type)
953 default:
954 case 1: // int +
955 value = uint32(int32(value) + int32(iValue));
956 DEBUG_LOG(GetMangosString(LANG_CHANGE_INT32), guid.GetString().c_str(), field, iValue, value, value);
957 PSendSysMessage(LANG_CHANGE_INT32_FIELD, guid.GetString().c_str(), field, iValue, value, value);
958 break;
959 case 2: // |= bit or
960 value |= iValue;
961 DEBUG_LOG(GetMangosString(LANG_CHANGE_HEX), guid.GetString().c_str(), field, typeStr, iValue, value);
962 PSendSysMessage(LANG_CHANGE_HEX_FIELD, guid.GetString().c_str(), field, typeStr, iValue, value);
963 break;
964 case 3: // &= bit and
965 value &= iValue;
966 DEBUG_LOG(GetMangosString(LANG_CHANGE_HEX), guid.GetString().c_str(), field, typeStr, iValue, value);
967 PSendSysMessage(LANG_CHANGE_HEX_FIELD, guid.GetString().c_str(), field, typeStr, iValue, value);
968 break;
969 case 4: // &=~ bit and not
970 value &= ~iValue;
971 DEBUG_LOG(GetMangosString(LANG_CHANGE_HEX), guid.GetString().c_str(), field, typeStr, iValue, value);
972 PSendSysMessage(LANG_CHANGE_HEX_FIELD, guid.GetString().c_str(), field, typeStr, iValue, value);
973 break;
976 target->SetUInt32Value(field, value);
978 else
980 float fValue;
981 if (!ExtractFloat(&valStr, fValue))
982 return false;
984 float value = target->GetFloatValue(field);
986 value += fValue;
988 DEBUG_LOG(GetMangosString(LANG_CHANGE_FLOAT), guid.GetString().c_str(), field, fValue, value);
989 PSendSysMessage(LANG_CHANGE_FLOAT_FIELD, guid.GetString().c_str(), field, fValue, value);
991 target->SetFloatValue(field, value);
994 return true;
997 bool ChatHandler::HandleDebugModItemValueCommand(char* args)
999 uint32 guid;
1000 if (!ExtractUInt32(&args, guid))
1001 return false;
1003 uint32 field;
1004 if (!ExtractUInt32(&args, field))
1005 return false;
1007 char* typeStr = ExtractLiteralArg(&args);
1008 if (!typeStr)
1009 return false;
1011 char* valStr = ExtractLiteralArg(&args);
1012 if (!valStr)
1013 return false;
1015 Item* item = m_session->GetPlayer()->GetItemByGuid(ObjectGuid(HIGHGUID_ITEM, guid));
1016 if (!item)
1017 return false;
1019 return HandlerDebugModValueHelper(item, field, typeStr, valStr);
1022 bool ChatHandler::HandleDebugModValueCommand(char* args)
1024 Unit* target = getSelectedUnit();
1025 if (!target)
1027 SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
1028 SetSentErrorMessage(true);
1029 return false;
1032 uint32 field;
1033 if (!ExtractUInt32(&args, field))
1034 return false;
1036 char* typeStr = ExtractLiteralArg(&args);
1037 if (!typeStr && *args) // optional arg but check format fail case
1038 return false;
1040 char* valStr = ExtractLiteralArg(&args);
1041 if (!valStr)
1042 return false;
1044 return HandlerDebugModValueHelper(target, field, typeStr, valStr);
1047 bool ChatHandler::HandleDebugSpellCoefsCommand(char* args)
1049 uint32 spellid = ExtractSpellIdFromLink(&args);
1050 if (!spellid)
1051 return false;
1053 SpellEntry const* spellEntry = sSpellStore.LookupEntry(spellid);
1054 if (!spellEntry)
1055 return false;
1057 SpellBonusEntry const* bonus = sSpellMgr.GetSpellBonusData(spellid);
1059 float direct_calc = CalculateDefaultCoefficient(spellEntry, SPELL_DIRECT_DAMAGE);
1060 float dot_calc = CalculateDefaultCoefficient(spellEntry, DOT);
1062 bool isDirectHeal = false;
1063 for (int i = 0; i < 3; ++i)
1065 SpellEffectEntry const* spellEffect = spellEntry->GetSpellEffect(SpellEffectIndex(i));
1066 if(!spellEffect)
1067 continue;
1068 // Heals (Also count Mana Shield and Absorb effects as heals)
1069 if (spellEffect->Effect == SPELL_EFFECT_HEAL || spellEffect->Effect == SPELL_EFFECT_HEAL_MAX_HEALTH ||
1070 (spellEffect->Effect == SPELL_EFFECT_APPLY_AURA && (spellEffect->EffectApplyAuraName == SPELL_AURA_SCHOOL_ABSORB || spellEffect->EffectApplyAuraName == SPELL_AURA_PERIODIC_HEAL)))
1072 isDirectHeal = true;
1073 break;
1077 bool isDotHeal = false;
1078 for (int i = 0; i < 3; ++i)
1080 SpellEffectEntry const* spellEffect = spellEntry->GetSpellEffect(SpellEffectIndex(i));
1081 if(!spellEffect)
1082 continue;
1083 // Periodic Heals
1084 if (spellEffect->Effect == SPELL_EFFECT_APPLY_AURA && spellEffect->EffectApplyAuraName == SPELL_AURA_PERIODIC_HEAL)
1086 isDotHeal = true;
1087 break;
1091 char const* directHealStr = GetMangosString(LANG_DIRECT_HEAL);
1092 char const* directDamageStr = GetMangosString(LANG_DIRECT_DAMAGE);
1093 char const* dotHealStr = GetMangosString(LANG_DOT_HEAL);
1094 char const* dotDamageStr = GetMangosString(LANG_DOT_DAMAGE);
1096 PSendSysMessage(LANG_SPELLCOEFS, spellid, isDirectHeal ? directHealStr : directDamageStr,
1097 direct_calc, direct_calc * 1.88f, bonus ? bonus->direct_damage : 0.0f, bonus ? bonus->ap_bonus : 0.0f);
1098 PSendSysMessage(LANG_SPELLCOEFS, spellid, isDotHeal ? dotHealStr : dotDamageStr,
1099 dot_calc, dot_calc * 1.88f, bonus ? bonus->dot_damage : 0.0f, bonus ? bonus->ap_dot_bonus : 0.0f);
1101 return true;
1104 bool ChatHandler::HandleDebugSpellModsCommand(char* args)
1106 char* typeStr = ExtractLiteralArg(&args);
1107 if (!typeStr)
1108 return false;
1110 uint16 opcode;
1111 if (strncmp(typeStr, "flat", strlen(typeStr)) == 0)
1112 opcode = SMSG_SET_FLAT_SPELL_MODIFIER;
1113 else if (strncmp(typeStr, "pct", strlen(typeStr)) == 0)
1114 opcode = SMSG_SET_PCT_SPELL_MODIFIER;
1115 else
1116 return false;
1118 uint32 effidx;
1119 if (!ExtractUInt32(&args, effidx) || effidx >= 64 + 32)
1120 return false;
1122 uint32 spellmodop;
1123 if (!ExtractUInt32(&args, spellmodop) || spellmodop >= MAX_SPELLMOD)
1124 return false;
1126 int32 value;
1127 if (!ExtractInt32(&args, value))
1128 return false;
1130 Player* chr = getSelectedPlayer();
1131 if (chr == NULL)
1133 SendSysMessage(LANG_NO_CHAR_SELECTED);
1134 SetSentErrorMessage(true);
1135 return false;
1138 // check online security
1139 if (HasLowerSecurity(chr))
1140 return false;
1142 PSendSysMessage(LANG_YOU_CHANGE_SPELLMODS, opcode == SMSG_SET_FLAT_SPELL_MODIFIER ? "flat" : "pct",
1143 spellmodop, value, effidx, GetNameLink(chr).c_str());
1144 if (needReportToTarget(chr))
1145 ChatHandler(chr).PSendSysMessage(LANG_YOURS_SPELLMODS_CHANGED, GetNameLink().c_str(),
1146 opcode == SMSG_SET_FLAT_SPELL_MODIFIER ? "flat" : "pct", spellmodop, value, effidx);
1148 WorldPacket data(opcode, (1 + 1 + 2 + 2));
1149 data << uint32(1);
1150 data << uint32(1);
1151 data << uint8(spellmodop);
1152 data << uint8(effidx);
1153 data << int32(value);
1154 chr->GetSession()->SendPacket(&data);
1156 return true;