public void Handle_connection(IAsyncResult result) //the parameter is a delegate, used to communicate between threads { Accept_connection(); //once again, checking for any other incoming connections TcpClient client = server.EndAcceptTcpClient(result); //creates the TcpClient NetworkStream ns = client.GetStream(); while (client.Connected) //while the client is connected, we look for incoming messages { try { byte[] generationBytes = new byte[8]; ns.Read(generationBytes, 0, generationBytes.Length); string generationStr = Encoding.UTF8.GetString(generationBytes, 0, generationBytes.Length); int.TryParse(generationStr, out int generation); //Console.WriteLine("Generation sent over is: " + generationStr); byte[] versionBytes = new byte[8]; string version = ""; if (type == "legal_fix") { ns.Read(versionBytes, 0, versionBytes.Length); version = Encoding.UTF8.GetString(versionBytes, 0, versionBytes.Length); Console.WriteLine("Version sent over is: " + version); } byte[] size = new byte[8]; ns.Read(size, 0, size.Length); string dataSizeStr = Encoding.UTF8.GetString(size, 0, size.Length); int.TryParse(dataSizeStr, out int dataSize); byte[] msg = new byte[dataSize]; //the messages arrive as byte array ns.Read(msg, 0, msg.Length); //the same networkstream reads the message sent by the client var pk = PKMConverter.GetPKMfromBytes(msg, generation); if (type == "legality_check") { var lc = new LegalityCheck(pk); byte[] report = Encoding.Default.GetBytes(lc.Report); ns.Write(report, 0, report.Length); ns.Flush(); } else if (type == "info_get") { string data = ""; data += pk.Nickname.ToString() + "," + pk.OT_Name.ToString() + "," + pk.CurrentLevel.ToString() + "," + pk.Species.ToString() + ","; foreach (int move in pk.Moves) { data += move.ToString() + ","; } data += pk.Nature.ToString() + "," + pk.IV_HP.ToString() + "," + pk.IV_ATK.ToString() + "," + pk.IV_DEF.ToString() + "," + pk.IV_SPD.ToString() + "," + pk.IV_SPE + "," + pk.IV_SPA + "," + pk.Gender.ToString() + "," + pk.IsShiny.ToString() + "," + pk.Ability.ToString() + "," + pk.HeldItem.ToString() + "," + pk.TID.ToString() + "," + pk.Ball.ToString() + "," + pk.PKRS_Infected.ToString(); data += "," + pk.SIZE_STORED.ToString(); if (pk.GetType() == typeof(PK4)) { data += ",4"; } else if (pk.GetType() == typeof(PK5)) { data += ",5"; } else if (pk.GetType() == typeof(PK6)) { data += ",6"; } else if (pk.GetType() == typeof(PK7)) { data += ",7"; } else if (pk.GetType() == typeof(PB7)) { data += ",LGPE"; } byte[] info = Encoding.UTF8.GetBytes(data); ns.Write(info, 0, info.Length); ns.Flush(); } else if (type == "legal_fix") { var alm = new AutoLegality(pk, version); ns.Write(alm.GetLegalPKM().DecryptedBoxData, 0, alm.GetLegalPKM().DecryptedBoxData.Length); } client.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); byte[] err = Encoding.Default.GetBytes("Not a Pokemon!"); ns.Write(err, 0, err.Length); ns.Flush(); client.Close(); } } }
static void Main(string[] args) { string appPath = Environment.CurrentDirectory; if (args.Contains("-server")) { // Init the database Legal.RefreshMGDB(string.Empty); var server1 = new Server(); server1.Server_start(7272, "legality_check", false); var server2 = new Server(); server2.Server_start(7273, "info_get", false); var server3 = new Server(); server3.Server_start(7274, "legal_fix", true); } else { Initialize(args); if (args.Contains("-l")) { // Legality API calls var lc = new LegalityCheck(pk); if (args.Contains("--verbose")) { Console.WriteLine(lc.VerboseReport); } else { Console.WriteLine(lc.Report); } } if (args.Contains("-alm")) { if (!args.Contains("--version")) { Console.WriteLine("Specify version with the [--version] tag"); } else { var alm = new AutoLegality(pk, args[Array.IndexOf(args, "--version") + 1]); if (alm != null) { if (!args.Contains("-o")) { string output = Util.CleanFileName(alm.GetLegalPKM().FileName); File.WriteAllBytes(Path.Combine(appPath, "output", output), alm.GetLegalPKM().DecryptedBoxData); } else { string output = GetOutputPath(args); File.WriteAllBytes(output, alm.GetLegalPKM().DecryptedBoxData); } } else { Console.WriteLine("Invalid version"); } } } } }
static void Main(string[] args) { string appPath = Environment.CurrentDirectory; mt = File.ReadAllLines("./coreconsole/Moves.csv").Skip(1).Select(m => MoveType.ReadCsv(m)).ToList(); if (args.Contains("-server")) { // Init the database EncounterEvent.RefreshMGDB(string.Empty); RibbonStrings.ResetDictionary(GameInfo.Strings.ribbons); LegalityAnalysis.MoveStrings = GameInfo.Strings.movelist; LegalityAnalysis.SpeciesStrings = GameInfo.Strings.specieslist; moveNames = Util.GetMovesList(GameLanguage.DefaultLanguage); // Init Move Types DB var server1 = new Server(); server1.Server_start(7272, "legality_check", false); var server2 = new Server(); server2.Server_start(7273, "info_get", false); var server3 = new Server(); server3.Server_start(7274, "legal_fix", false); var server4 = new Server(); server4.Server_start(7275, "bot_info", true); } else { Initialize(args); if (args.Contains("-l")) { // Legality API calls var lc = new LegalityCheck(pk); if (args.Contains("--verbose")) { Console.WriteLine(lc.VerboseReport); } else { Console.WriteLine(lc.Report); } } if (args.Contains("-alm")) { if (!args.Contains("--version")) { Console.WriteLine("Specify version with the [--version] tag"); } else { var alm = new AutoLegality(pk, args[Array.IndexOf(args, "--version") + 1]); if (alm != null) { if (!args.Contains("-o")) { string output = Util.CleanFileName(alm.GetLegalPKM().FileName); File.WriteAllBytes(Path.Combine(appPath, "output", output), alm.GetLegalPKM().DecryptedBoxData); } else { string output = GetOutputPath(args); File.WriteAllBytes(output, alm.GetLegalPKM().DecryptedBoxData); } } else { Console.WriteLine("Invalid version"); } } } } }