Package net.bnubot.util

Examples of net.bnubot.util.BNetInputStream


      task.complete();

      BNLSPacketReader bpr = new BNLSPacketReader(bnlsInputStream);
      if(bpr.packetId != BNLSPacketId.BNLS_VERSIONCHECKEX2)
        throw new IOException("Recieved the wrong packet (" + bpr.packetId.name() + ", but expected BNLS_VERSIONCHECKEX2)");
      BNetInputStream bnlsIn = bpr.getInputStream();
      int success = bnlsIn.readDWord();
      if (success != 1) {
        Out.error(getClass(), "BNLS_VERSIONCHECKEX2 Failed\n"
            + HexDump.hexDump(bpr.getData()));
        setBNLSConnected(false);
        return null;
      }

      VersionCheckResult vcr = new VersionCheckResult();
      vcr.exeVersion = bnlsIn.readDWord();
      vcr.exeHash = bnlsIn.readDWord();
      vcr.exeInfo = bnlsIn.readNTBytes();
      bnlsIn.readDWord(); // cookie
      bnlsIn.readDWord(); // verbyte
      assert (bnlsIn.available() == 0);

      Out.info(getClass(), "Recieved version check from BNLS.");

      if((vcr.exeVersion == 0)
      || (vcr.exeHash == 0)
View Full Code Here


    sendUserInfo();

    while(isConnected() && !socket.isClosed() && !disposed) {
      if(bnInputStream.available() > 0) {
        BotNetPacketReader pr = new BotNetPacketReader(bnInputStream);
        BNetInputStream is = pr.getData();

        eventHandlers.clear();
        eventHandlers.addAll(master.getEventHandlers());

        switch(pr.packetId) {
        case PACKET_IDLE: {
          sendIdle();
          break;
        }
        case PACKET_STATSUPDATE: {
          int result = is.readDWord();
          switch(result) {
          case 0:
            dispatchRecieveError("Status update failed");
            break;
          case 1:
            // Success
            break;
          default:
            dispatchRecieveError("Unknown PACKET_LOGON result 0x" + Integer.toHexString(result));
            disconnect(ConnectionState.LONG_PAUSE_BEFORE_CONNECT);
            return;
          }
          break;
        }
        case PACKET_ACCOUNT: {
          int command = is.readDWord();
          int result = is.readDWord();
          switch(result) {
          case 0:
            switch(command) {
            case 0:
              dispatchRecieveError("Account logon failed");
              break;
            case 1:
              dispatchRecieveError("Password change failed");
              break;
            case 2:
              dispatchRecieveError("Account create failed");
              break;
            default:
              dispatchRecieveError("Unknown PACKET_ACCOUNT command 0x" + Integer.toHexString(command));
              break;
            }
            dispatchRecieveError("Status update failed");
            break;
          case 1:
            // Success
            break;
          default:
            dispatchRecieveError("Unknown PACKET_ACCOUNT result 0x" + Integer.toHexString(result));
            disconnect(ConnectionState.LONG_PAUSE_BEFORE_CONNECT);
            return;
          }
          break;
        }
        case PACKET_USERINFO: {
          if(pr.data.length == 0) {
            userInit = false;
            break;
          }

          int number = is.readDWord();
          int dbflag = 0, ztff = 0;
          if(serverRevision >= 4) {
            dbflag = is.readDWord();
            ztff = is.readDWord();
          }
          String name = is.readNTString();

          BotNetUser user = new BotNetUser(this, number, name);
          user.dbflag = dbflag;
          user.ztff = ztff;

          user.channel = is.readNTString();
          user.server = is.readDWord();
          if(serverRevision >= 2)
            user.account = is.readNTString();
          if(serverRevision >= 3)
            user.database = is.readNTString();

          if(myUser == null)
            myUser = user;

          if(userInit)
            dispatchBotnetUserOnline(user);
          else
            dispatchBotnetUserStatus(user);
          //recieveInfo(user.toStringEx());
          break;
        }
        case PACKET_USERLOGGINGOFF: {
          int number = is.readDWord();
          dispatchBotnetUserLogoff(number);
          break;
        }
        case PACKET_BOTNETCHAT: {
          int command = is.readDWord();
          int action = is.readDWord();
          BotNetUser user = users.get(is.readDWord());
          ByteArray data = new ByteArray(is.readNTBytes());

          switch(command) {
          case 0: //broadcast
            // TODO: change this to recieveBroadcast()
            dispatchRecieveChat(user, data);
            break;
          case 1: // chat
            if(action == 0)
              dispatchRecieveChat(user, data);
            else
              dispatchRecieveEmote(user, data.toString());
            break;
          case 2: //whisper
            dispatchWhisperRecieved(user, data.toString());
            break;
          default:
            dispatchRecieveError("Unknown PACKET_BOTNETCHAT command 0x" + Integer.toHexString(command));
            disconnect(ConnectionState.LONG_PAUSE_BEFORE_CONNECT);
            break;
          }
          break;
        }

        case PACKET_COMMAND: {
          // PROTOCOL VIOLATION!
          int err = is.readDWord();
          byte id = is.readByte();
          int lenOffending = is.readWord();
          int lenUnprocessed = is.readWord();
          dispatchRecieveError("Protocol violation: err=" + err + ", packet=" + BotNetPacketId.values()[id].name() + ", offending packet len=" + lenOffending + ", unprocessed data len=" + lenUnprocessed);
          disconnect(ConnectionState.LONG_PAUSE_BEFORE_CONNECT);
          break;
        }
        default:
View Full Code Here

      Out.fatalException(e);
    }
  }

  private static BNetIcon[] readIconsDotBni(File f) {
    try (BNetInputStream is = new BNetInputStream(new FileInputStream(f))) {
      Out.debug(IconsDotBniReader.class, "Reading " + f.getName());

      is.skip(4); //int headerSize = is.readDWord();
      int bniVersion = is.readWord();
      is.skip(2)// Alignment Padding (unused)
      int numIcons = is.readDWord();
      is.skip(4)//int dataOffset = is.readDWord();

      if(bniVersion != 1)
        throw new Exception("Unknown BNI version");

      Out.debug(IconsDotBniReader.class, "Reading " + numIcons + " icons in format " + bniVersion);

      BNetIcon[] icons = new BNetIcon[numIcons];

      for(int i = 0; i < numIcons; i++) {
        BNetIcon icon = new BNetIcon();
        icon.flags = is.readDWord();
        icon.xSize = is.readDWord();
        icon.ySize = is.readDWord();
        if(icon.flags != 0)
          icon.sortIndex = i;
        else
          icon.sortIndex = numIcons;
        int numProducts;
        int products[] = new int[32];

        //Read in up to 32 products; stop if we see a null
        for(numProducts = 0; numProducts < 32; numProducts++) {
          products[numProducts] = is.readDWord();
          if(products[numProducts] == 0)
            break;
        }

        if(numProducts > 0) {
          icon.products = new int[numProducts];
          for(int j = 0; j < numProducts; j++)
            icon.products[j] = products[j];
        } else
          icon.products = null;
        icons[i] = icon;
        Out.debug(IconsDotBniReader.class, icon.toString());
      }

      //Image in targa format
      byte infoLength = is.readByte();
      is.skip(1);              // ColorMapType
      byte imageType = is.readByte();    // run-length true-color image types = 0x0A
      is.skip(5);              // ColorMapSpecification - color map data
      is.skip(2)//int xOrigin = is.readWord();
      is.skip(2)//int yOrigin = is.readWord();
      int width = is.readWord();
      int height = is.readWord();
      byte depth = is.readByte();      // 24 bit depth is good
      /*byte descriptor =*/ is.readByte()// bits 5 and 4 (00110000) specify the corner to start coloring pixels - 00=bl, 01=br, 10=tl, 11=tr
      is.skip(infoLength)//String info = is.readFixedLengthString(infoLength);

      if(imageType != 0x0A)
        throw new Exception("Unknown image type");
      if(depth != 24)
        throw new Exception("Unknown depth");

      //Pixel data
      int[] pixelData = new int[width*height];
      int currentPixel = 0;

      while(currentPixel < pixelData.length) {
        byte packetHeader = is.readByte()// if bit 7 (0x80) is set, run-length packet;
        int len = (packetHeader & 0x7F) + 1;
        if((packetHeader & 0x80) != 0) {
          //Run-length packet
          int blue = is.readByte() & 0xFF;
          int green = is.readByte() & 0xFF;
          int red = is.readByte() & 0xFF;
          int col = new Color(red, green, blue).getRGB();
          for(int i = 0; i < len; i++)
            pixelData[getRealPixelPosition(currentPixel++, height, width)] = col;
        } else {
          for(int i = 0; i < len; i++) {
            int blue = is.readByte() & 0xFF;
            int green = is.readByte() & 0xFF;
            int red = is.readByte() & 0xFF;
            int col = new Color(red, green, blue).getRGB();
            pixelData[getRealPixelPosition(currentPixel++, height, width)] = col;
          }
        }
      }
View Full Code Here

  public BNCSWarden(BNCSConnection c, byte[] seed) throws IOException {
    this.c = c;

    BNLSConnection bnls = BNLSManager.getWardenConnection();
    int cookie_out = c.getConnectionSettings().botNum;
    BNetInputStream is = bnls.sendWarden0(
        cookie_out,
        c.getProductID().getDword(),
        seed);

    byte u = is.readByte();
    int cookie = is.readDWord();
    byte result = is.readByte();
    short dataLen = is.readWord();
    if(u != 0)
      throw new IOException("wrong useage");
    if(cookie != cookie_out)
      throw new IOException("wrong cookie");
    if(result != 0)
View Full Code Here

  }

  public void processWardenPacket(byte[] payload, OutputStream os) throws IOException {
    BNLSConnection bnls = BNLSManager.getWardenConnection();
    int cookie_out = c.getConnectionSettings().botNum;
    BNetInputStream is = bnls.sendWarden1(cookie_out, payload);

    byte u = is.readByte();
    int cookie = is.readDWord();
    byte result = is.readByte();
    short dataLen = is.readWord();
    byte[] data = new byte[dataLen];
    is.read(data);

    if(u != 1)
      throw new IOException("wrong useage");
    if(cookie != cookie_out)
      throw new IOException("wrong cookie");
View Full Code Here

    int port = getPort();
    InetAddress address = MirrorSelector.getClosestMirror(getServer(), port);
    dispatchRecieveInfo("Connecting to " + address + ":" + port + ".");
    socket = new Socket(address, port);
    socket.setKeepAlive(true);
    dtInputStream = new BNetInputStream(socket.getInputStream());
    dtOutputStream = new DataOutputStream(socket.getOutputStream());

    // Connected
    connect.updateProgress("Connected");
  }
View Full Code Here

    while(isConnected() && !socket.isClosed() && !disposed) {
      if(dtInputStream.available() <= 0) {
        sleep(200);
      } else {
        DTPacketReader pr = new DTPacketReader(dtInputStream);
        BNetInputStream is = pr.getData();

        switch(pr.packetId) {
        case PKT_LOGON: {
          /* (BYTE)   Status
           *
           * Status codes:
           * 0x00 - (C) Passed
           * 0x01 - (A) Failed
           * 0x02 - (C) Account created
           * 0x03 - (A) Account online.
           * Other: Unknown (failure).
           */
          int status = is.readByte();
          switch(status) {
          case 0x00:
            dispatchRecieveInfo("Login accepted.");
            break;
          case 0x01:
View Full Code Here

      if(dtInputStream.available() <= 0) {
        sleep(200);
      } else {
        DTPacketReader pr = new DTPacketReader(dtInputStream);
        BNetInputStream is = pr.getData();

        switch(pr.packetId) {
        case PKT_ENTERCHANNEL: {
          /* (CString)   Channel
           * (UInt32)  Flags
           * (CString)  MOTD
           *
           * Flags:
           * 0x01 : Registered
           * 0x02 : Silent
           * 0x04 : Admin
           */

          String channel = is.readNTString();
          int flags = is.readDWord();
          String motd = is.readNTString();

          dispatchJoinedChannel(channel, flags);
          dispatchRecieveInfo(motd);
          break;
        }

        case PKT_CHANNELUSERS: {
          /* (Byte)    User Count
           * (void)    User Data
           *
           * For each user...
           *   (CString)  Username
           *   (UInt32)  Flags
           *
           * User Flags:
           * 0x01 : Operator
           * 0x02 : Ignored
           * 0x04 : Admin
           * 0x08 : NetOp
           */
          int numUsers = is.readByte();
          for(int i = 0; i < numUsers; i++) {
            String username = is.readNTString();
            int flags = is.readDWord();

            dispatchChannelUser(findCreateBNUser(username, flags));
          }
          break;
        }

        case PKT_USERUPDATE: {
          /* (CString)  Username
           * (UInt32)  Flags
           */
          String username = is.readNTString();
          int flags = is.readDWord();

          dispatchChannelUser(findCreateBNUser(username, flags));
          break;
        }

        case PKT_CHANNELJOIN: {
          /* (CString)  Username
           * (UInt32)  Flags
           */
          String username = is.readNTString();
          int flags = is.readDWord();

          dispatchChannelJoin(findCreateBNUser(username, flags));
          break;
        }

        case PKT_CHANNELLEAVE: {
          /* (CString)  Username
           */
          String username = is.readNTString();

          dispatchChannelLeave(findCreateBNUser(username, null));
          break;
        }

        case PKT_CHANNELCHAT: {
          /* (Byte)    Chat Type
           * (CString)  From
           * (CString)  Message
           *
           * Chat Type values:
           * 0x00 : Normal
           * 0x01 : Self Talking
           * 0x02 : Whisper To
           * 0x03 : Whisper From
           * 0x04 : Emote
           * 0x05 : Self Emote
           */
          int chatType = is.readByte();
          String username = is.readNTString();
          ByteArray text = new ByteArray(is.readNTBytes());

          // Get a BNetUser object for the user
          BNetUser user = null;
          if(myUser.equals(username))
            user = myUser;
          else
            user = getCreateBNetUser(username, myUser);

          switch(chatType) {
          case 0x00: // Normal
          case 0x01: // Self talking
            dispatchRecieveChat(user, text);
            break;
          case 0x02: // Whisper to
            dispatchWhisperSent(user, text.toString());
            break;
          case 0x03: // Whisper from
            dispatchWhisperRecieved(user, text.toString());
            break;
          case 0x04: // Emote
          case 0x05: // Self Emote
            dispatchRecieveEmote(user, text.toString());
            break;
          default:
            Out.debugAlways(getClass(), "Unexpected chat type 0x" + Integer.toHexString(chatType) + " from " + username + ": " + text);
            break;
          }

          break;
        }

        case PKT_UNKNOWN_0x22: {
          int unknown = is.readDWord();
          String text = is.readNTString();
          switch(unknown) {
          case 0x00:
            dispatchRecieveInfo(text);
            break;
          case 0x01:
View Full Code Here

  int packetLength;
  byte data[];

  public BNLSPacketReader(InputStream rawis) throws IOException {
    // Operates on a socket, which we don't want to close; suppress the warning
    @SuppressWarnings("resource")
    BNetInputStream is = new BNetInputStream(rawis);

    packetLength = is.readWord() & 0x0000FFFF;
    packetId = BNLSPacketId.values()[is.readByte() & 0x000000FF];
    assert(packetLength >= 3);

    data = new byte[packetLength-3];
    for(int i = 0; i < packetLength-3; i++)
      data[i] = is.readByte();

    if(GlobalSettings.packetLog) {
      String msg = "RECV " + packetId.name();
      if(Out.isDebug()) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
View Full Code Here

  public byte[] getData() {
    return data;
  }

  public BNetInputStream getInputStream() {
    return new BNetInputStream(new ByteArrayInputStream(data));
  }
View Full Code Here

TOP

Related Classes of net.bnubot.util.BNetInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.