This is a read only copy without any forum functionality of the old Modcraft forum.
If there is anything that you would like to have removed, message me on Discord via Kaev#5208.
Big thanks to Alastor for making this copy!

Menu

Author Topic: [SOLVED] [C++] Command crashes on usage  (Read 1738 times)

bizzlesnaff

  • Registred Member
  • Wiki Incarnate
  • *****
  • Posts: 124
    • View Profile
[SOLVED] [C++] Command crashes on usage
« on: June 27, 2015, 08:44:21 pm »
Hey guys ;)

Atm I'm trying to create my own command (trinitycore btw). I just try to say ingame .showaccount (while target any player) and the result should be a text which is in a custom database and linked with the id of the player..
Well, here is my code:
Code: [Select]
RBAC.h
RBAC_PERM_COMMAND_CUSTOMINFO = 799,

cs_misc.cpp
{ "showaccount",  rbac::RBAC_PERM_COMMAND_CUSTOMINFO, false, &HandleNewCommand,   "", NULL },


static bool HandleNewCommand(ChatHandler * handler, char const* args)
{

Player* target;
ObjectGuid targetGuid;
std::string targetName;

QueryResult resulta = CharacterDatabase.PQuery("SELECT Info FROM characterinfo WHERE acc='%s'", targetGuid);


handler->PSendSysMessage("Info: %s", resulta);
return true;
}

I copied this code from some tutorials, and other scripts together, so i dont wonder that it doesn't work. But it compile fine, just if I type ingame ".showaccount" the server crashed up, but with no error log...just...shutting down or something

someone an idea what I've made wrong?

sorry for my english...thats such a dump language :)
« Last Edit: July 03, 2015, 12:42:32 am by Admin »

Kaev

  • Contributors
  • Creator of Worlds
  • *****
  • Posts: 308
    • View Profile
Re: [C++] RBAC Command
« Reply #1 on: June 28, 2015, 11:04:51 am »
Did you add the command to the rbac_permissions and rbac_linked_permissions table?
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

bizzlesnaff

  • Registred Member
  • Wiki Incarnate
  • *****
  • Posts: 124
    • View Profile
Re: [C++] RBAC Command
« Reply #2 on: June 28, 2015, 04:36:56 pm »
Ah yeah of course, forgot to write it. I did every step of this tutorial..
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

bizzlesnaff

  • Registred Member
  • Wiki Incarnate
  • *****
  • Posts: 124
    • View Profile
Re: [C++] RBAC Command
« Reply #3 on: June 30, 2015, 12:42:45 am »
little push ;)?
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Kaev

  • Contributors
  • Creator of Worlds
  • *****
  • Posts: 308
    • View Profile
Re: [C++] RBAC Command
« Reply #4 on: June 30, 2015, 08:01:48 am »
QueryResult resulta = CharacterDatabase.PQuery("SELECT Info FROM characterinfo WHERE acc='%s'", targetGuid);

%s means null terminated string, but a guid is a integer value. Try %u instead of %s. %u means unsigned integer.

EDIT:
Lol, didn't saw that.
You can't just print a query as a string, like you did here:

handler->PSendSysMessage("Info: %s", resulta);

Here's a short snippet of a script, which i wrote:
Code: [Select]
QueryResult result = WorldDatabase.Query("SELECT * FROM hm_waves");
if (result)
{
do
{
Field *fields = result->Fetch();
int waveid = fields[0].GetInt32();
creature.id = fields[1].GetInt32();
} while (result->NextRow());
}
If the field info is a string, you have to use .GetString(); instead of GetInt32();. The array index is the number of the field in the query. You only request one field with your query, so it should be std::string infotext = fields[0].GetString();
And you don't have to use the do-while-loop, because you probably only expect one result.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Ascathos

  • Moderators
  • Creator of Worlds
  • *****
  • Posts: 1129
    • View Profile
Re: [C++] Command crashes on usage
« Reply #5 on: July 01, 2015, 08:59:52 pm »
Sorry for my long abstinence. I wasn't home, thus no response yet.
You query with targetGuid being NULL. This is impossible to work.

Code: [Select]
RBAC.h
RBAC_PERM_COMMAND_CUSTOMINFO                      = 799,

cs_misc.cpp
{ "showaccount",     rbac::RBAC_PERM_COMMAND_CUSTOMINFO,      false, &HandleNewCommand,            "", NULL },


static bool HandleNewCommand(ChatHandler* handler, char const* args)
   {
      Player* target;
      ObjectGuid parseGUID = ObjectGuid::Create<HighGuid::Player>(strtoull(args, nullptr, 10));
      std::string targetName;
     
        if (ObjectMgr::GetPlayerNameByGUID(parseGUID, targetName))
        {
            target = ObjectAccessor::FindPlayer(parseGUID);
            targetGuid = parseGUID;
        }
        else if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
            return false;

      QueryResult resulta = CharacterDatabase.PQuery("SELECT Info FROM characterinfo WHERE acc=%u", targetGuid);
     
      if (resulta)
      {
         handler->PSendSysMessage("Info: %s", (*resulta)[0].GetString());
         return true;
      }      
     
      return false;
   }

This uses a guid, a target or handler (Command user). I based the way to get a target by copying what I used (and how it later was modified) from .pinfo. Please read further into other commands to see what you need. If you have questions, there is a TrinityCore IRC. I am on it as well.

I also took the liberty to change the topic name to be more approperiate.

Edit: Just notice that Info is a string, thus edited the fetched result.




EDIT 2:
Repost, accidently deleted this post (...).
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

bizzlesnaff

  • Registred Member
  • Wiki Incarnate
  • *****
  • Posts: 124
    • View Profile
Re: [C++] Command crashes on usage
« Reply #6 on: July 02, 2015, 12:43:25 am »
Thank you both ;)
I'll try it tomorrow. Never thought, that a simple command could be so difficult ;)
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

bizzlesnaff

  • Registred Member
  • Wiki Incarnate
  • *****
  • Posts: 124
    • View Profile
Re: [C++] Command crashes on usage
« Reply #7 on: July 02, 2015, 10:55:38 pm »
Well i tried it today again, but it doesn't work for me ;(

I'll wait some weeks and learn more, maybe then I know how to fix it ;)

Again, thank you for your support ;)
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Rochet2

  • Contributors
  • Polygonshifter
  • *****
  • Posts: 59
    • View Profile
    • http://rochet2.github.io/
Re: [C++] Command crashes on usage
« Reply #8 on: July 02, 2015, 11:48:45 pm »
Maybe this helps?

Code: [Select]
static bool HandleNewCommand(ChatHandler* handler, char const* args)
{
    // get world session
    WorldSession* session = handler->GetSession();
    // make sure it exists. It doesnt if this command is called from the console
    if (!session)
        return false;

    // get the player that invoked this command
    Player* player = session->GetPlayer();
    if (!player)
        return false;

    // get the player's target and make sure its a player
    Player* target = player->GetSelectedPlayer();
    if (!target)
        return false;

    // make a database query with the account ID
    // dont use strings to identify things :) And you can get the name from auth database by the account id anyways if needed
    QueryResult result = CharacterDatabase.PQuery("SELECT `info` FROM `characterinfo` WHERE `accountid` = %u", target->GetSession()->GetAccountId());

    if (!result) // always make sure a row was returned before actually using it
    {
        handler->SendSysMessage("Info: no info");
    }
    else
    {
        // get the info string from the query. the selected values are indexed starting from 0, so info is at position 0
        std::string info = result->Fetch()[0].GetString();
        handler->PSendSysMessage("Info: %s", info.c_str()); // using c_str() to convert the std::string into const char* which is what PSendSysMessage, printf and other need.
    }

    // command succeeded, return true to not show syntax info or error message
    return true;
}


Usually wrong database queries call abort and close the server. Better get those database types and columns right.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

bizzlesnaff

  • Registred Member
  • Wiki Incarnate
  • *****
  • Posts: 124
    • View Profile
Re: [C++] Command crashes on usage
« Reply #9 on: July 03, 2015, 12:42:05 am »
Wow that works aboslut perfect...thank you very much!
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Ascathos

  • Moderators
  • Creator of Worlds
  • *****
  • Posts: 1129
    • View Profile
Re: [SOLVED] [C++] Command crashes on usage
« Reply #10 on: July 03, 2015, 01:21:55 pm »
ah, forgot the fetch->result[0]. Oh well, as long as it works
« Last Edit: January 01, 1970, 01:00:00 am by Admin »