Пример #1
0
        /// <summary>
        /// Parses a user statstring and returns an object representing the stats in a meaningful way.
        /// </summary>
        /// <param name="userName">The name of the user whose stats are being examined.</param>
        /// <param name="statsData">The stats of the user.</param>
        /// <returns>An instance of a class derived from <see>UserStats</see> based on the user's 
        /// <see cref="BNSharp.BattleNet.Product">Product</see>.  To check the product, check the 
        /// <see cref="UserStats.Product">Product property</see>.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="userName"/> or <paramref name="statsData"/>
        /// are <see langword="null" />.</exception>
        /// <remarks>
        /// <para>More specific information is available from most users by casting an object of this class to one of the 
        /// derived classes.  Products and their relevant classes are provided in the following table:</para>
        /// <list type="table">
        ///     <listheader>
        ///         <item>
        ///             <term>Product</term>
        ///             <description>Stats subclass type</description>
        ///         </item>
        ///     </listheader>
        ///     <item>
        ///         <term>Starcraft Retail, Starcraft: Brood War, Starcraft Shareware, Japan Starcraft,
        ///         Warcraft II: Battle.net Edition, Diablo Retail, Diablo Shareware</term>
        ///         <description><see>StarcraftStats</see></description>
        ///     </item>
        ///     <item>
        ///         <term>Diablo II Retail, Diablo II: Lord of Destruction</term>
        ///         <description><see>Diablo2Stats</see></description>
        ///     </item>
        ///     <item>
        ///         <term>Warcraft III: The Reign of Chaos, Warcraft III: The Frozen Throne</term>
        ///         <description><see>Warcraft3Stats</see></description>
        ///     </item>
        ///     <item>
        ///         <term>Others (unknown clients)</term>
        ///         <description><see>DefaultStats</see></description>
        ///     </item>
        /// </list>
        /// </remarks>
        public static UserStats Parse(string userName, byte[] statsData)
        {
            DataReader dr = new DataReader(statsData);
            string productCode = dr.ReadDwordString(0);
            UserStats result = null;
            switch (productCode)
            {
                //case "STAR":
                //case "SEXP":
                //case "SSHR":
                //case "JSTR":
                //case "W2BN":
                //case "DSHR":
                //case "DRTL":
                //    result = new StarcraftStats(statsData);
                //    break;
                //case "D2DV":
                //case "D2XP":
                //    result = new Diablo2Stats(userName, statsData);
                //    break;
                //case "WAR3":
                //case "W3XP":
                //    result = new Warcraft3Stats(statsData);
                //    break;
                default:
                    result = new DefaultStats(productCode, statsData);
                    break;
            }

            return result;
        }
Пример #2
0
        public override async Task ExecuteRequest()
        {
            using (AsyncConnectionBase connection = new AsyncConnectionBase(Gateway.ServerHost, Gateway.ServerPort, 0, 0))
            {
                byte[] fileNameBytes = Encoding.UTF8.GetBytes(FileName);

                DataBuffer buf = new DataBuffer();
                buf.InsertInt16(20); // Length
                buf.InsertInt16(0x0200); // Protocol version
                buf.InsertDwordString("IX86");
                buf.InsertDwordString(Product.ProductCode);
                if (_ad)
                {
                    buf.InsertInt32(_adId);
                    buf.InsertDwordString(_adExt);
                }
                else
                {
                    buf.InsertInt64(0);
                }

                bool connected = await connection.ConnectAsync();
                if (!connected)
                    throw new IOException("Battle.net refused the connection.");

                await connection.SendAsync(new byte[] { 2 });

                byte[] outgoingData = buf.UnderlyingStream.ToArray();
                await connection.SendAsync(outgoingData);

                byte[] incomingData = new byte[4];
                incomingData = await connection.ReceiveAsync(incomingData, 0, 4);
                if (incomingData == null)
                    throw new IOException("Battle.net rejected the request.");

                int serverToken = BitConverter.ToInt32(incomingData, 0);

                buf = new DataBuffer();
                buf.InsertInt32(0); // No resuming
                if (FileTime.HasValue)
                    buf.InsertInt64(FileTime.Value.ToFileTimeUtc());
                else
                    buf.InsertInt64(0);
                int clientToken = new Random().Next();
                buf.InsertInt32(clientToken);

                buf.InsertInt32(_key.Key.Length);
                buf.InsertInt32(_key.Product);
                buf.InsertInt32(_key.Value1);
                buf.InsertInt32(0);
                buf.InsertByteArray(_key.GetHash(clientToken, serverToken));
                buf.InsertByteArray(fileNameBytes);
                buf.InsertByte(0);

                outgoingData = buf.UnderlyingStream.ToArray();
                await connection.SendAsync(outgoingData);
                
                incomingData = new byte[8];
                incomingData = await connection.ReceiveAsync(incomingData, 0, 8);
                if (incomingData == null)
                    throw new IOException("Battle.net rejected the file request.");

                int remainingHeaderSize = BitConverter.ToInt32(incomingData, 0) - 8;
                int fileSize = BitConverter.ToInt32(incomingData, 4);
                this.FileSize = fileSize;
                incomingData = new byte[remainingHeaderSize];
                incomingData = await connection.ReceiveAsync(incomingData, 0, remainingHeaderSize);
                if (incomingData == null)
                    throw new IOException("Battle.net did not send a complete file header.");

                DataReader reader = new DataReader(incomingData);
                reader.Seek(8); // banner id / extension
                long fileTime = reader.ReadInt64();
                string name = reader.ReadCString();
                if (string.Compare(name, FileName, StringComparison.OrdinalIgnoreCase) != 0 || FileSize == 0)
                {
                    throw new FileNotFoundException("The specified file was not found by Battle.net.");
                }

                incomingData = new byte[fileSize];
                incomingData = await connection.ReceiveAsync(incomingData, 0, fileSize);
                if (incomingData == null)
                    throw new IOException("Battle.net did not send the file data.");

                File.WriteAllBytes(LocalFileName, incomingData);
                DateTime time = DateTime.FromFileTimeUtc(fileTime);
                File.SetLastWriteTimeUtc(LocalFileName, time);
            }
        }
Пример #3
0
        /// <summary>
        /// Executes the BnFTP request, downloading the file to where <see cref="BnFtpRequestBase.LocalFileName">LocalFileName</see>
        /// specifies, and closes the connection.
        /// </summary>
        /// <remarks>
        /// <para>By default, <c>LocalFileName</c> is the same name as the remote file, which will cause the file
        /// to be saved in the local application path.  The desired location of the file must be set before 
        /// <b>ExecuteRequest</b> is called.</para>
        /// </remarks>
        /// <exception cref="IOException">Thrown if the local file cannot be written.</exception>
        /// <exception cref="SocketException">Thrown if the remote host closes the connection prematurely.</exception>
        public override async Task ExecuteRequest()
        {
            using (AsyncConnectionBase connection = new AsyncConnectionBase(Gateway.ServerHost, Gateway.ServerPort, 0, 0))
            {
                byte[] fileNameBytes = Encoding.UTF8.GetBytes(FileName);

                DataBuffer buffer = new DataBuffer();
                buffer.InsertInt16((short)(33 + fileNameBytes.Length));
                buffer.InsertInt16(0x0100);
                buffer.InsertDwordString("IX86");
                buffer.InsertDwordString(Product.ProductCode);
                if (_ad)
                {
                    buffer.InsertInt32(_adId);
                    buffer.InsertDwordString(_adExt);
                }
                else
                {
                    buffer.InsertInt64(0);
                }
                // currently resuming is not supported
                buffer.InsertInt32(0);
                if (FileTime.HasValue)
                {
                    buffer.InsertInt64(FileTime.Value.ToFileTimeUtc());
                }
                else
                {
                    buffer.InsertInt64(0);
                }
                buffer.InsertByteArray(fileNameBytes);
                buffer.InsertByte(0);


                bool connected = await connection.ConnectAsync();
                if (!connected)
                    throw new IOException("Battle.net refused the connection to FTP.");

                await connection.SendAsync(new byte[] { 2 });

                byte[] byteData = buffer.UnderlyingStream.ToArray();
                await connection.SendAsync(byteData);

                byte[] header = new byte[8];
                header = await connection.ReceiveAsync(header, 0, 8);

                if (header == null)
                    throw new IOException("Battle.net did not respond to the FTP request.");

                DataReader headerReader = new DataReader(header);
                ushort headerLength = headerReader.ReadUInt16();
                headerReader.Seek(2);
                int fileSize = headerReader.ReadInt32();
                this.FileSize = fileSize;

                byte[] remainingHeader = new byte[headerLength - 8];
                remainingHeader = await connection.ReceiveAsync(remainingHeader, 0, headerLength - 8);
                if (remainingHeader == null)
                    throw new IOException("Battle.net did not send the complete header.");

                headerReader = new DataReader(remainingHeader);
                headerReader.Seek(8);
                long fileTime = headerReader.ReadInt64();
                string name = headerReader.ReadCString();
                if (string.Compare(name, FileName, StringComparison.OrdinalIgnoreCase) != 0 || FileSize == 0)
                {
                    throw new FileNotFoundException("The specified file was not found by Battle.net.");
                }
                Debug.WriteLine(fileSize, "File Size");

                byte[] fileData = new byte[fileSize];
                fileData = await connection.ReceiveAsync(fileData, 0, fileSize);
                if (fileData == null)
                    throw new IOException("Battle.net did not send the file data.");

                File.WriteAllBytes(LocalFileName, fileData);
                DateTime time = DateTime.FromFileTimeUtc(fileTime);
                File.SetLastWriteTimeUtc(LocalFileName, time);
            }
        }