コード例 #1
0
        protected void btnSend_Click(object sender, EventArgs e)
        {
            byte[] data = fuBox.FileBytes;
            if (data.Length < 0xf8)
            {
                Fail(); return;
            }

            Common.CryptMessage(data);
            if (data[0x04] != 0x09 ||
                data[0x05] != 0x52 ||
                data[0x06] != 0x00 ||
                data[0x07] != 0x00)
            {
                Fail(); return;
            }

            int results = BitConverter.ToInt32(data, 0x08);
            if (data.Length != 12 + 556 * results)
            {
                Fail(); return;
            }

            int added = 0;

            for (int x = 0; x < results; x++)
            {
                int pid = BitConverter.ToInt32(data, 12 + 556 * x);
                BoxLabels4 label = (BoxLabels4)BitConverter.ToInt32(data, 16 + 556 * x);
                ulong serial = BitConverter.ToUInt64(data, 20 + 556 * x);
                if (serial == 0) continue;

                byte[] result = new byte[540];
                Array.Copy(data, 28 + 556 * x, result, 0, 540);

                BoxRecord4 record = new BoxRecord4(pid, label, serial, result);
                if (Database.Instance.BoxUpload4(record) != 0) added++;
            }

            litMessage.Text = "Added " + added.ToString() + " boxes to the database.";
        }
コード例 #2
0
        protected override byte[] ProcessRequest(byte[] data, TcpClient c)
        {
            int length = BitConverter.ToInt32(data, 0);
            AssertHelper.Equals(length, data.Length);

            RequestTypes4 requestType = (RequestTypes4)data[4];
            StringBuilder logEntry = new StringBuilder();
            logEntry.AppendFormat("Handling Generation IV {0} request.\nHost: {1}", requestType, c.Client.RemoteEndPoint);
            logEntry.AppendLine();
            EventLogEntryType type = EventLogEntryType.Information;

            CryptMessage(data);

            MemoryStream response = new MemoryStream();
            response.Write(new byte[] { 0x00, 0x00, 0x00, 0x00 }, 0, 4); // placeholder for length
            response.WriteByte((byte)requestType);
            response.WriteByte(data[5]);

            try
            {
                int pid = BitConverter.ToInt32(data, 8);
                byte version = data[0x0c];
                byte language = data[0x0d];
                logEntry.AppendFormat("pid: {0}", pid);
                logEntry.AppendLine();

                switch (requestType)
                {
                    #region Box upload
                    case RequestTypes4.BoxUpload:
                    {
                        if (data.Length != 0x360)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        BoxLabels4 label = (BoxLabels4)BitConverter.ToInt32(data, 0x140);
                        byte[] boxData = new byte[0x21c];
                        Array.Copy(data, 0x144, boxData, 0, 0x21c);
                        BoxRecord4 record = new BoxRecord4(pid, label, 0, boxData);
                        ulong serial = Database.Instance.BoxUpload4(record);

                        if (serial == 0)
                        {
                            logEntry.AppendLine("Uploaded box already in server.");
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        logEntry.AppendFormat("Box {0} uploaded successfully.", serial);
                        logEntry.AppendLine();
                        response.Write(new byte[] { 0x00, 0x00 }, 0, 2); // result code (0 for OK)
                        response.Write(BitConverter.GetBytes(serial), 0, 8);

                    } break;
                    case RequestTypes4.BoxSearch:
                    {
                        if (data.Length != 0x14c)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        // todo: validate or log some of this?
                        BoxLabels4 label = (BoxLabels4)BitConverter.ToInt32(data, 0x144);

                        logEntry.AppendFormat("Searching for {0} boxes.", label);
                        logEntry.AppendLine();

                        BoxRecord4[] results = Database.Instance.BoxSearch4(label, 20);
                        response.Write(new byte[] { 0x00, 0x00 }, 0, 2); // result code (0 for OK)
                        response.Write(BitConverter.GetBytes(results.Length), 0, 4);

                        foreach (BoxRecord4 result in results)
                        {
                            response.Write(BitConverter.GetBytes(result.PID), 0, 4);
                            response.Write(BitConverter.GetBytes((int)result.Label), 0, 4);
                            response.Write(BitConverter.GetBytes(result.SerialNumber), 0, 8);
                            response.Write(result.Data, 0, 0x21c);
                        }
                        logEntry.AppendFormat("Retrieved {0} boxes.", results.Length);
                        logEntry.AppendLine();

                    } break;
                    #endregion

                    #region Dressup
                    case RequestTypes4.DressupUpload:
                    {
                        if (data.Length != 0x220)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        byte[] dressupData = new byte[0xe0];
                        Array.Copy(data, 0x140, dressupData, 0, 0xe0);
                        DressupRecord4 record = new DressupRecord4(pid, 0, dressupData);
                        ulong serial = Database.Instance.DressupUpload4(record);

                        if (serial == 0)
                        {
                            logEntry.AppendLine("Uploaded dressup already in server.");
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        logEntry.AppendFormat("Dressup {0} uploaded successfully.", serial);
                        response.Write(new byte[] { 0x00, 0x00 }, 0, 2); // result code (0 for OK)
                        response.Write(BitConverter.GetBytes(serial), 0, 8);

                    } break;
                    case RequestTypes4.DressupSearch:
                    {
                        if (data.Length != 0x14c)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        // todo: validate or log some of this?
                        ushort species = BitConverter.ToUInt16(data, 0x144);

                        logEntry.AppendFormat("Searching for dressups of species {0}.", species);
                        logEntry.AppendLine();

                        DressupRecord4[] results = Database.Instance.DressupSearch4(species, 10);
                        response.Write(new byte[] { 0x00, 0x00 }, 0, 2); // result code (0 for OK)
                        response.Write(BitConverter.GetBytes(results.Length), 0, 4);

                        foreach (DressupRecord4 result in results)
                        {
                            response.Write(BitConverter.GetBytes(result.PID), 0, 4);
                            response.Write(BitConverter.GetBytes(result.SerialNumber), 0, 8);
                            response.Write(result.Data, 0, 0xe0);
                        }
                        logEntry.AppendFormat("Retrieved {0} dressup results.", results.Length);
                        logEntry.AppendLine();

                    } break;
                    #endregion

                    #region Battle videos
                    case RequestTypes4.BattleVideoUpload:
                    {
                        if (data.Length != 0x1e8c)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        byte[] battlevidData = new byte[0x1d4c];
                        Array.Copy(data, 0x140, battlevidData, 0, 0x1d4c);
                        BattleVideoRecord4 record = new BattleVideoRecord4(pid, 0, battlevidData);
                        ulong serial = Database.Instance.BattleVideoUpload4(record);

                        if (serial == 0)
                        {
                            logEntry.AppendFormat("Uploaded battle video already in server.");
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        logEntry.AppendFormat("Battle video {0} uploaded successfully.", BattleVideoHeader4.FormatSerial(serial));
                        logEntry.AppendLine();
                        response.Write(new byte[] { 0x00, 0x00 }, 0, 2); // result code (0 for OK)
                        response.Write(BitConverter.GetBytes(serial), 0, 8);

                    } break;
                    case RequestTypes4.BattleVideoSearch:
                    {
                        if (data.Length != 0x15c)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        // todo: validate or log some of this?
                        BattleVideoRankings4 ranking = (BattleVideoRankings4)BitConverter.ToUInt32(data, 0x140);
                        ushort species = BitConverter.ToUInt16(data, 0x144);
                        BattleVideoMetagames4 meta = (BattleVideoMetagames4)data[0x146];
                        byte country = data[0x147];
                        byte region = data[0x148];

                        logEntry.Append("Searching for ");
                        if (ranking != BattleVideoRankings4.None)
                            logEntry.AppendFormat("{0}", ranking);
                        else
                        {
                            if (species != 0xffff)
                                logEntry.AppendFormat("species {0}, ", species);
                            logEntry.AppendFormat("{0}", meta);
                            if (country != 0xff)
                                logEntry.AppendFormat(", country {0}", country);
                            if (region != 0xff)
                                logEntry.AppendFormat(", region {0}", region);
                        }
                        logEntry.AppendLine(".");

                        if ((byte)meta == 254)
                        {
                            // todo: Figure out how to make the game perform this search!
                            logEntry.AppendLine("Reverting to latest 30.");
                            meta = BattleVideoMetagames4.SearchLatest30;
                        }

                        BattleVideoHeader4[] results = Database.Instance.BattleVideoSearch4(species, ranking, meta, country, region, 30);
                        response.Write(new byte[] { 0x00, 0x00 }, 0, 2); // result code (0 for OK)
                        response.Write(BitConverter.GetBytes(results.Length), 0, 4);

                        foreach (BattleVideoHeader4 result in results)
                        {
                            response.Write(BitConverter.GetBytes(result.PID), 0, 4);
                            response.Write(BitConverter.GetBytes(result.SerialNumber), 0, 8);
                            response.Write(result.Data, 0, 0xe4);
                        }
                        logEntry.AppendFormat("Retrieved {0} battle video results.", results.Length);
                        logEntry.AppendLine();

                    } break;
                    case RequestTypes4.BattleVideoWatch:
                    {
                        if (data.Length != 0x14c)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        ulong serial = BitConverter.ToUInt64(data, 0x140);
                        BattleVideoRecord4 record = Database.Instance.BattleVideoGet4(serial, true);
                        if (record == null)
                        {
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            logEntry.AppendFormat("Requested battle video {0} was missing.", BattleVideoHeader4.FormatSerial(serial));
                            logEntry.AppendLine();
                            type = EventLogEntryType.FailureAudit;
                            break;
                        }

                        response.Write(new byte[] { 0x00, 0x00 }, 0, 2); // result code (0 for OK)
                        response.Write(BitConverter.GetBytes(record.PID), 0, 4);
                        response.Write(BitConverter.GetBytes(record.SerialNumber), 0, 8);
                        response.Write(record.Header.Data, 0, 0xe4);
                        response.Write(record.Data, 0, 0x1c68);
                        logEntry.AppendFormat("Retrieved battle video {0}.", BattleVideoHeader4.FormatSerial(serial));
                        logEntry.AppendLine();

                    } break;
                    case RequestTypes4.BattleVideoSaved:
                    {
                        if (data.Length != 0x148)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        ulong serial = BitConverter.ToUInt64(data, 0x140);

                        if (Database.Instance.BattleVideoFlagSaved4(serial))
                        {
                            response.Write(new byte[] { 0x00, 0x00 }, 0, 2); // result code (0 for OK)
                            logEntry.AppendFormat("Battle video {0} flagged saved.", BattleVideoHeader4.FormatSerial(serial));
                            logEntry.AppendLine();
                        }
                        else
                        {
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            logEntry.AppendFormat("Requested battle video {0} was missing.", BattleVideoHeader4.FormatSerial(serial));
                            logEntry.AppendLine();
                        }
                    } break;
                    #endregion

                    default:
                        logEntry.AppendLine("Unrecognized request type.");
                        response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                        break;
                }
            }
            catch (Exception ex)
            {
                logEntry.AppendFormat("Unhandled exception while handling request.\nException: {0}", ex.ToString());
                logEntry.AppendLine();
                type = EventLogEntryType.Error;
                response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
            }

            response.Flush();
            byte[] responseData = response.ToArray();
            WriteLength(responseData);
            CryptMessage(responseData);

            LogHelper.Write(logEntry.ToString(), type);
            return responseData;
        }
コード例 #3
0
 public abstract ulong BoxUpload4(BoxRecord4 record);