コード例 #1
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: bvRestorer5 <path>");
                Console.WriteLine("Attempts to insert all the files in path\ninto the database in app configuration.");
                return;
            }

            String[] filenames = Directory.GetFiles(args[0]);
            int successCount = 0;

            foreach (String filename in filenames)
            {
                FileStream fs = File.OpenRead(filename);
                if (fs.Length != 0x18b8)
                {
                    Console.WriteLine("{0}: file size is wrong, skipped.", filename);
                    continue;
                }

                byte[] data = new byte[0x18b8];
                fs.ReadBlock(data, 0, 0x18b8);
                fs.Close();

                int length = BitConverter.ToInt32(data, 0);
                if (length != 0x18b8)
                {
                    Console.WriteLine("{0}: size field is wrong, skipped.", filename);
                    continue;
                }

                if (data[4] != 0xf2)
                {
                    Console.WriteLine("{0}: request type is wrong, skipped.", filename);
                    continue;
                }

                if (data[5] != 0x55 || data[6] != 0x00 || data[7] != 0x00)
                {
                    Console.WriteLine("{0}: sanity bytes are wrong, skipped.", filename);
                    continue;
                }

                int pid = BitConverter.ToInt32(data, 0x08);
                ulong serial = BitConverter.ToUInt64(data, 0x0c);
                byte[] mainData = new byte[0x18a4];
                Array.Copy(data, 0x14, mainData, 0, 0x18a4);

                BattleVideoRecord5 record = new BattleVideoRecord5(pid, serial, mainData);

                Database.Instance.BattleVideoUpload5(record);

                Console.WriteLine("Video {0} added successfully.", BattleVideoHeader4.FormatSerial(serial));
                successCount++;
            }
            Console.WriteLine("{0} battle videos successfully added.", successCount);
            Console.ReadKey();
        }
コード例 #2
0
        protected override byte[] ProcessRequest(byte[] data, TcpClient c)
        {
            int length = BitConverter.ToInt32(data, 0);
            AssertHelper.Equals(length, data.Length);

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

            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 Musicals
                    case RequestTypes5.MusicalUpload:
                    {
                        if (data.Length != 0x370)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        byte[] musicalData = new byte[0x230];
                        Array.Copy(data, 0x140, musicalData, 0, 0x230);
                        MusicalRecord5 record = new MusicalRecord5(pid, 0, musicalData);
                        ulong serial = Database.Instance.MusicalUpload5(record);

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

                        logEntry.AppendFormat("Musical {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 RequestTypes5.MusicalSearch:
                    {
                        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 musical photos of species {0}.", species);
                        logEntry.AppendLine();

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

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

                    } break;
                    #endregion

                    #region Battle videos
                    case RequestTypes5.BattleVideoUpload:
                    {
                        if (data.Length != 0x1ae8)
                        {
                            logEntry.AppendLine("Length did not validate.");
                            type = EventLogEntryType.FailureAudit;
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }
                        int sigLength = BitConverter.ToInt32(data, 0x19e4);
                        if (sigLength > 0x100 || sigLength < 0x00)
                        {
                            response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
                            break;
                        }

                        byte[] battlevidData = new byte[0x18a4];

                        Array.Copy(data, 0x140, battlevidData, 0, 0x18a4);
                        BattleVideoRecord5 record = new BattleVideoRecord5(pid, 0, battlevidData);
                        byte[] vldtSignature = new byte[sigLength];
                        Array.Copy(data, 0x19e8, vldtSignature, 0, sigLength);
                        // todo: validate signature.

                        ulong serial = Database.Instance.BattleVideoUpload5(record);

                        if (serial == 0)
                        {
                            logEntry.AppendLine("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 RequestTypes5.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?
                        BattleVideoRankings5 ranking = (BattleVideoRankings5)BitConverter.ToUInt32(data, 0x140);
                        ushort species = BitConverter.ToUInt16(data, 0x144);
                        BattleVideoMetagames5 meta = (BattleVideoMetagames5)data[0x146];

                        // Byte 148 contains a magic number related to the searched metagame.
                        // If 0, disable metagame search. Metagame being 00 is insufficient
                        // since that value could mean Battle Subway Single.
                        if (data[0x148] == 0x00) meta = BattleVideoMetagames5.SearchNone;

                        byte country = data[0x14a];
                        byte region = data[0x14b];

                        logEntry.Append("Searching for ");
                        if (ranking != BattleVideoRankings5.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(".");

                        BattleVideoHeader5[] results = Database.Instance.BattleVideoSearch5(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 (BattleVideoHeader5 result in results)
                        {
                            response.Write(BitConverter.GetBytes(result.PID), 0, 4);
                            response.Write(BitConverter.GetBytes(result.SerialNumber), 0, 8);
                            response.Write(result.Data, 0, 0xc4);
                        }
                        logEntry.AppendFormat("Retrieved {0} battle video results.", results.Length);
                        logEntry.AppendLine();

                    } break;
                    case RequestTypes5.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);
                        BattleVideoRecord5 record = Database.Instance.BattleVideoGet5(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, 0xc4);
                        response.Write(record.Data, 0, 0x17e0);
                        logEntry.AppendFormat("Retrieved battle video {0}.", BattleVideoHeader4.FormatSerial(serial));
                        logEntry.AppendLine();

                    } break;
                    case RequestTypes5.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.BattleVideoFlagSaved5(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();
                response.Write(new byte[] { 0x02, 0x00 }, 0, 2);
            }

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

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