From 2f4950a295ec57da7eb2e465ba0d27cdc7b87268 Mon Sep 17 00:00:00 2001 From: Greg Edwards Date: Wed, 2 Jul 2014 22:46:58 -0400 Subject: [PATCH] Core of GenV global terminal server --- GlobalTerminalService/GTServer4.cs | 16 +-- GlobalTerminalService/GTServer5.cs | 105 ++++++++++++++++++ GlobalTerminalService/GTServerBase.cs | 9 +- .../GlobalTerminalService.csproj | 1 + library/Structures/Enums.cs | 4 +- 5 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 GlobalTerminalService/GTServer5.cs diff --git a/GlobalTerminalService/GTServer4.cs b/GlobalTerminalService/GTServer4.cs index 84b46873..6c1c2197 100644 --- a/GlobalTerminalService/GTServer4.cs +++ b/GlobalTerminalService/GTServer4.cs @@ -51,7 +51,11 @@ namespace PkmnFoundations.GlobalTerminalService try { - // todo: implement each of the request types here + // todo: fact check this for GenIV. + //int pid = BitConverter.ToInt32(data, 8); + //byte version = data[0x0c]; + //byte language = data[0x0d]; + switch (requestType) { case RequestTypes4.BoxUpload: @@ -105,7 +109,6 @@ namespace PkmnFoundations.GlobalTerminalService } Console.WriteLine("Retrieved {0} boxes.", results.Length); - } break; case RequestTypes4.DressupUpload: { @@ -245,6 +248,9 @@ namespace PkmnFoundations.GlobalTerminalService Console.WriteLine("Retrieved battle video {0}.", BattleVideoHeader4.FormatSerial(serial)); } break; + default: + response.Write(new byte[] { 0x02, 0x00 }, 0, 2); + break; } } catch (Exception ex) @@ -260,12 +266,6 @@ namespace PkmnFoundations.GlobalTerminalService return responseData; } - private void WriteLength(byte[] message) - { - byte[] data = BitConverter.GetBytes(message.Length); - Array.Copy(data, 0, message, 0, 4); - } - private void CryptMessage(byte[] message) { if (message.Length < 5) return; diff --git a/GlobalTerminalService/GTServer5.cs b/GlobalTerminalService/GTServer5.cs new file mode 100644 index 00000000..c489b1de --- /dev/null +++ b/GlobalTerminalService/GTServer5.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using PkmnFoundations.Support; +using System.IO; + +namespace PkmnFoundations.GlobalTerminalService +{ + public class GTServer5 : GTServerBase + { + public GTServer5() + : base(12401, true) + { + Initialize(); + } + + public GTServer5(int threads) + : base(12401, true, threads) + { + Initialize(); + } + + private void Initialize() + { + + } + + protected override byte[] ProcessRequest(byte[] data) + { + int length = BitConverter.ToInt32(data, 0); + AssertHelper.Equals(length, data.Length); + + RequestTypes5 requestType = (RequestTypes5)data[4]; + Console.WriteLine("Handling Generation V {0} request.", requestType); + + MemoryStream response = new MemoryStream(); + response.Write(new byte[] { 0x00, 0x00, 0x00, 0x00 }, 0, 4); // placeholder for length + response.WriteByte((byte)requestType); + response.WriteByte(Byte6(requestType)); + + try + { + int pid = BitConverter.ToInt32(data, 8); + byte version = data[0x0c]; + byte language = data[0x0d]; + + switch (requestType) + { + case RequestTypes5.MusicalUpload: + { + + } break; + default: + response.Write(new byte[] { 0x02, 0x00 }, 0, 2); + break; + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + response.Write(new byte[] { 0x02, 0x00 }, 0, 2); + } + + response.Flush(); + byte[] responseData = response.ToArray(); + WriteLength(responseData); + return responseData; + } + + private byte Byte6(RequestTypes5 type) + { + switch (type) + { + case RequestTypes5.MusicalUpload: + case RequestTypes5.MusicalSearch: + return 0x52; + case RequestTypes5.BattleVideoUpload: + case RequestTypes5.BattleVideoSearch: + case RequestTypes5.BattleVideoWatch: + return 0x55; + default: + return 0x00; + } + } + + public override string Title + { + get + { + return "Generation V Global Terminal"; + } + } + } + + internal enum RequestTypes5 : byte + { + MusicalUpload = 0x08, + MusicalSearch = 0x09, + + BattleVideoUpload = 0xf0, + BattleVideoSearch = 0xf1, + BattleVideoWatch = 0xf2 + } +} diff --git a/GlobalTerminalService/GTServerBase.cs b/GlobalTerminalService/GTServerBase.cs index 87b18cdc..17162767 100644 --- a/GlobalTerminalService/GTServerBase.cs +++ b/GlobalTerminalService/GTServerBase.cs @@ -28,8 +28,7 @@ namespace PkmnFoundations.GlobalTerminalService Threads = threads; UseSsl = useSsl; m_workers = new List(threads); - // todo: enumerate nic bindings - m_listener = new TcpListener(port); + m_listener = new TcpListener(IPAddress.Any, port); if (UseSsl) { Certificate = new X509Certificate2("cert.pfx", "letmein"); @@ -138,5 +137,11 @@ namespace PkmnFoundations.GlobalTerminalService protected abstract byte[] ProcessRequest(byte[] data); public abstract String Title { get; } + + protected void WriteLength(byte[] message) + { + byte[] data = BitConverter.GetBytes(message.Length); + Array.Copy(data, 0, message, 0, 4); + } } } diff --git a/GlobalTerminalService/GlobalTerminalService.csproj b/GlobalTerminalService/GlobalTerminalService.csproj index a6486465..34ebb3e3 100644 --- a/GlobalTerminalService/GlobalTerminalService.csproj +++ b/GlobalTerminalService/GlobalTerminalService.csproj @@ -49,6 +49,7 @@ + Component diff --git a/library/Structures/Enums.cs b/library/Structures/Enums.cs index 5ee929f2..5b0f440b 100644 --- a/library/Structures/Enums.cs +++ b/library/Structures/Enums.cs @@ -32,7 +32,9 @@ namespace PkmnFoundations.Structures Pearl = 0x0b, Platinum = 0x0c, HeartGold = 0x07, - SoulSilver = 0x08 + SoulSilver = 0x08, + White = 0x14, + Black = 0x15, } public enum EvolutionTriggers