From fc8f9827b8111334dcc71ec093fa3fbb4f75c271 Mon Sep 17 00:00:00 2001 From: GittyMac Date: Thu, 30 Jun 2022 20:53:26 -0400 Subject: [PATCH] Code Cleanup --- A1Emu/A1_FKPlugins/A1_Galaxy.cs | 170 ----------------------- A1Emu/A1_FKPlugins/A1_System.cs | 168 ++++++++++++++++++---- A1Emu/A1_FKPlugins/A1_Trunk.cs | 170 ----------------------- A1Emu/A1_FKPlugins/A1_User.cs | 237 -------------------------------- A1Emu/A1_Utils/A1_Parser.cs | 96 ++++++------- A1Emu/FKObjects/FKUser.cs | 17 ++- A1Emu/Program.cs | 66 ++++----- 7 files changed, 237 insertions(+), 687 deletions(-) delete mode 100644 A1Emu/A1_FKPlugins/A1_Galaxy.cs delete mode 100644 A1Emu/A1_FKPlugins/A1_Trunk.cs delete mode 100644 A1Emu/A1_FKPlugins/A1_User.cs diff --git a/A1Emu/A1_FKPlugins/A1_Galaxy.cs b/A1Emu/A1_FKPlugins/A1_Galaxy.cs deleted file mode 100644 index d856b9a..0000000 --- a/A1Emu/A1_FKPlugins/A1_Galaxy.cs +++ /dev/null @@ -1,170 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Runtime.InteropServices; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading; -using System.Xml; -using NetCoreServer; - -//ArkONE Plugin 7 -//Galaxy - -class A1_Galaxy : TcpSession -{ - A1_Parser a1parser; - - FKUser a1user; - - int sessionPort = 80; - - public A1_Galaxy(TcpServer server, int port) : base(server){a1parser = new A1_Parser(); a1user = new FKUser(); sessionPort = port;} - - protected override void OnReceived(byte[] buffer, long offset, long size) - { - string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size); - Console.WriteLine("[7 - Galaxy] Recieved: " + message); - - bool isMultiOutput = false; - string response = ""; - - string[] commands = a1parser.ParseReceivedMessage(message); - foreach(string command in commands) - { - string[] commandInfo = a1parser.ParseCommand(command); - switch(commandInfo[0]){ - case "a_lgu": - response += LoginGuestUser(commandInfo[1],commandInfo[2],commandInfo[3]); - break; - case "a_gpd": - //TODO - start other A1 plugins - response += GetPluginDetails(commandInfo[1]); - break; - } - } - - //Message Output - List d = new List(); - Byte[] reply = Encoding.ASCII.GetBytes(response); - byte[] b2 = new byte[] {0x00}; - d.Add(reply); - d.Add(b2); - byte[] b3 = d.SelectMany(a => a).ToArray(); - if(!isMultiOutput) - SendAsync(b3,0,b3.Length); - else Server.Multicast(b3,0,b3.Length); SendAsync(b3,0,b3.Length); - - // Multicast message to all connected sessions - //Server.Multicast(message); - } - - string LoginGuestUser(string d, string a, string c){ - var responseStream = new MemoryStream(); - using (XmlWriter writer = XmlWriter.Create(responseStream)) - { - writer.WriteStartElement("a_lgu"); - - //Result - writer.WriteElementString("r", "0"); - - //User ID - writer.WriteElementString("u", "0"); - - //Username - writer.WriteElementString("n", "GUESTUSER"); - - //Password - writer.WriteElementString("p", ""); - - //Service ID - writer.WriteElementString("s", "1"); - writer.WriteEndElement(); - writer.WriteEndDocument(); - } - - return System.Text.ASCIIEncoding.UTF8.GetString(responseStream.ToArray()); - } - - string GetPluginDetails(string p){ - var responseStream = new MemoryStream(); - - string serviceID = p; - - string xIPAddress = "localhost"; - string xPort = "80"; - - string bIPAddress = "localhost"; - string bPort = "80"; - - switch(p){ - //Galaxy - case "7": - xPort = (sessionPort + 7).ToString(); - bPort = (sessionPort + 7).ToString(); - break; - - //Trunk - case "10": - xPort = (sessionPort + 10).ToString(); - bPort = (sessionPort + 10).ToString(); - break; - } - - using (XmlWriter writer = XmlWriter.Create(responseStream)) - { - writer.WriteStartElement("a_gpd"); - - //Service ID - writer.WriteElementString("s", serviceID); - - //xIPAddress - writer.WriteElementString("xi", xIPAddress); - - //xPort - writer.WriteElementString("xp", xPort); - - //bIPAddress - writer.WriteElementString("bi", bIPAddress); - - //Plugin ID - writer.WriteElementString("p", p); - writer.WriteEndElement(); - writer.WriteEndDocument(); - } - - return System.Text.ASCIIEncoding.UTF8.GetString(responseStream.ToArray()); - } - - protected override void OnConnected() - { - Console.WriteLine($"[7 - Galaxy] TCP session with Id {Id} connected!"); - } - - protected override void OnDisconnected() - { - Console.WriteLine($"[7 - Galaxy] TCP session with Id {Id} disconnected!"); - } - - protected override void OnError(SocketError error) - { - Console.WriteLine($"[7 - Galaxy] Error - {error}"); - } -} - -class ServerGalaxy : TcpServer -{ - int serverPort = 80; - - public ServerGalaxy(IPAddress address, int port) : base(address, port) {serverPort = port;} - - protected override TcpSession CreateSession() { return new A1_Galaxy(this, serverPort); } - - protected override void OnError(SocketError error) - { - Console.WriteLine($"[7 - Galaxy] TCP server caught an error with code {error}"); - } -} \ No newline at end of file diff --git a/A1Emu/A1_FKPlugins/A1_System.cs b/A1Emu/A1_FKPlugins/A1_System.cs index 6e646c9..a348661 100644 --- a/A1Emu/A1_FKPlugins/A1_System.cs +++ b/A1Emu/A1_FKPlugins/A1_System.cs @@ -6,8 +6,8 @@ using System.Net; using System.Net.Sockets; using System.Text; using System.Security.Cryptography; -using MySqlConnector; using System.Xml; +using MySqlConnector; using NetCoreServer; //ArkONE Plugin 0 @@ -25,17 +25,26 @@ class A1_System : TcpSession string serverDirectory = ""; - public A1_System(TcpServer server, int port, string sqServerInput, string directory) : base(server){a1parser = new A1_Parser(); a1user = new FKUser(); sessionPort = port; sqServer = sqServerInput; serverDirectory = directory;} + int chunksLeft = 0; + + string saveData = ""; + + public A1_System(TcpServer server, int port, string sqServerInput, string directory) : base(server) + { + a1parser = new A1_Parser(); + a1user = new FKUser(); + sessionPort = port; sqServer = sqServerInput; + serverDirectory = directory; + } protected override void OnReceived(byte[] buffer, long offset, long size) { string message = Encoding.ASCII.GetString(buffer, (int)offset, (int)size - 1); - Console.WriteLine("[0 - System] Recieved: " + message); - - bool isMultiOutput = false; + Console.WriteLine("[0 - System - " + this.Id + "] Recieved: " + message); List responses = new List(); + //Initializes a buddy list if null. if(a1user.buddies == null){ a1user.buddies = new List(); } @@ -54,7 +63,7 @@ class A1_System : TcpSession responses.Add(GetPluginDetails(commandInfo[1]).Remove(0, 1)); break; case "a_gsd": - responses.Add(GetServiceDetails(commandInfo[1]).Remove(0, 1)); + responses.Add(GetServerDetails(commandInfo[1]).Remove(0, 1)); break; case "a_lru": responses.Add(LoginRegisteredUser(commandInfo[1], commandInfo[2], commandInfo[3]).Remove(0, 1)); @@ -75,25 +84,41 @@ class A1_System : TcpSession case "lpv": responses.Add(LoadProfileVersion()); break; + case "vsu": + responses.Add(VersionStatisticsRequest(commandInfo[1])); + break; + case "sp": + responses.Add(SaveProfile(commandInfo[1])); + break; + case "spp": + responses.Add(SaveProfilePart(commandInfo[1], commandInfo[2])); + break; + + default: + responses.Add(@""); + break; } } foreach(string responseString in responses){ + //Formats the reponse to send. List d = new List(); Byte[] reply = Encoding.ASCII.GetBytes(responseString); byte[] b2 = new byte[] {0x00}; d.Add(reply); d.Add(b2); byte[] b3 = d.SelectMany(a => a).ToArray(); - if(!isMultiOutput) - SendAsync(b3, 0, b3.Length); - else Server.Multicast(b3,0,b3.Length); SendAsync(b3,0,b3.Length); - } - // Multicast message to all connected sessions - //Server.Multicast(message); + Send(b3, 0, b3.Length); + } } + //======== + //Commands + //======== + + //Plugin 0 + string LoginGuestUser(string d, string a, string c){ var responseStream = new MemoryStream(); @@ -129,12 +154,12 @@ class A1_System : TcpSession string GetPluginDetails(string p){ var responseStream = new MemoryStream(); - string serviceID = "1"; + string serverID = "1"; - string xIPAddress = "127.0.0.1"; + string xIPAddress = "localhost"; string xPort = "80"; - string bIPAddress = "127.0.0.1"; + string bIPAddress = "localhost"; string bPort = "80"; switch(p){ @@ -165,7 +190,7 @@ class A1_System : TcpSession writer.WriteStartElement("a_gpd"); //Server ID - writer.WriteAttributeString("s", serviceID); + writer.WriteAttributeString("s", serverID); //xIPAddress writer.WriteAttributeString("xi", xIPAddress); @@ -189,16 +214,16 @@ class A1_System : TcpSession return System.Text.ASCIIEncoding.UTF8.GetString(responseStream.ToArray()); } - string GetServiceDetails(string s) + string GetServerDetails(string s) { var responseStream = new MemoryStream(); - string serviceID = "1"; + string serverID = "1"; - string xIPAddress = "127.0.0.1"; + string xIPAddress = "localhost"; string xPort = "80"; - string bIPAddress = "127.0.0.1"; + string bIPAddress = "localhost"; string bPort = "80"; switch(s){ @@ -228,8 +253,8 @@ class A1_System : TcpSession { writer.WriteStartElement("a_gsd"); - //Service ID - writer.WriteAttributeString("s", serviceID); + //Server ID + writer.WriteAttributeString("s", serverID); //xIPAddress writer.WriteAttributeString("xi", xIPAddress); @@ -258,6 +283,7 @@ class A1_System : TcpSession int resultCode = 0; //Making the password more secure. + //TODO - Find an alternative to the obsolete RNGCryptoServiceProvider. byte[] salt; new RNGCryptoServiceProvider().GetBytes(salt = new byte[16]); var encryptedP = new Rfc2898DeriveBytes(p,salt,10000); @@ -547,6 +573,95 @@ class A1_System : TcpSession writer.Close(); } + return System.Text.ASCIIEncoding.ASCII.GetString(responseStream.ToArray()); + } + + string VersionStatisticsRequest(string id){ + var responseStream = new MemoryStream(); + XmlWriterSettings settings = new XmlWriterSettings(); + settings.OmitXmlDeclaration = true; + settings.ConformanceLevel = ConformanceLevel.Fragment; + settings.Encoding = Encoding.ASCII; + using (XmlWriter writer = XmlWriter.Create(responseStream, settings)) + { + writer.WriteStartElement("h7_0"); + writer.WriteStartElement("vsu"); + + writer.WriteAttributeString("id", "0"); + + writer.WriteEndElement(); + writer.WriteEndElement(); + writer.Flush(); + writer.Close(); + } + + return System.Text.ASCIIEncoding.ASCII.GetString(responseStream.ToArray()); + } + + string SaveProfile(string c){ + var responseStream = new MemoryStream(); + XmlWriterSettings settings = new XmlWriterSettings(); + settings.OmitXmlDeclaration = true; + settings.ConformanceLevel = ConformanceLevel.Fragment; + settings.Encoding = Encoding.ASCII; + + //Gets the number of chunks to save. + chunksLeft = int.Parse(c); + + using (XmlWriter writer = XmlWriter.Create(responseStream, settings)) + { + writer.WriteStartElement("h7_0"); + writer.WriteStartElement("rr"); + + writer.WriteEndElement(); + writer.WriteEndElement(); + writer.Flush(); + writer.Close(); + } + + return System.Text.ASCIIEncoding.ASCII.GetString(responseStream.ToArray()); + } + + string SaveProfilePart(string v, string n){ + var responseStream = new MemoryStream(); + XmlWriterSettings settings = new XmlWriterSettings(); + settings.OmitXmlDeclaration = true; + settings.ConformanceLevel = ConformanceLevel.Fragment; + settings.Encoding = Encoding.ASCII; + + using (XmlWriter writer = XmlWriter.Create(responseStream, settings)) + { + writer.WriteStartElement("h7_0"); + + saveData = v + saveData; + + if(chunksLeft == 1){ + writer.WriteStartElement("sp"); + + saveData = WebUtility.HtmlDecode(saveData); + + XmlDocument save = new XmlDocument(); + save.LoadXml(saveData); + XmlElement rootSave = save.DocumentElement; + + string profileName = rootSave.GetAttribute("gname"); + writer.WriteAttributeString("v", (int.Parse(rootSave.GetAttribute("sid")) + 1).ToString()); + + if(!Directory.Exists(serverDirectory + profileName)){ + Directory.CreateDirectory(serverDirectory + profileName); + } + File.WriteAllText(serverDirectory + profileName + @"/profile", saveData); + }else{ + writer.WriteStartElement("rr"); + chunksLeft -= 1; + } + + writer.WriteEndElement(); + writer.WriteEndElement(); + writer.Flush(); + writer.Close(); + } + return System.Text.ASCIIEncoding.ASCII.GetString(responseStream.ToArray()); } @@ -575,7 +690,14 @@ class ServerSystem : TcpServer string serverDirectory = ""; - public ServerSystem(IPAddress address, int port, string sqServerInput, string directory) : base(address, port) {serverPort = port; sqServer = sqServerInput; serverDirectory = directory;} + public ServerSystem(IPAddress address, int port, string sqServerInput, string directory) : base(address, port) + { + serverPort = port; + sqServer = sqServerInput; + serverDirectory = directory; + this.OptionReceiveBufferSize = 51200; //FK sends very large packets for saves, bigger than the NCS default of 8192. + this.OptionKeepAlive = true; + } protected override TcpSession CreateSession() { return new A1_System(this, serverPort, sqServer, serverDirectory); } diff --git a/A1Emu/A1_FKPlugins/A1_Trunk.cs b/A1Emu/A1_FKPlugins/A1_Trunk.cs deleted file mode 100644 index 3f99489..0000000 --- a/A1Emu/A1_FKPlugins/A1_Trunk.cs +++ /dev/null @@ -1,170 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Runtime.InteropServices; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading; -using System.Xml; -using NetCoreServer; - -//ArkONE Plugin 10 -//Trunk - -class A1_Trunk : TcpSession -{ - A1_Parser a1parser; - - FKUser a1user; - - int sessionPort = 80; - - public A1_Trunk(TcpServer server, int port) : base(server){a1parser = new A1_Parser(); a1user = new FKUser(); sessionPort = port;} - - protected override void OnReceived(byte[] buffer, long offset, long size) - { - string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size); - Console.WriteLine("[10 - Trunk] Recieved: " + message); - - bool isMultiOutput = false; - string response = ""; - - string[] commands = a1parser.ParseReceivedMessage(message); - foreach(string command in commands) - { - string[] commandInfo = a1parser.ParseCommand(command); - switch(commandInfo[0]){ - case "a_lgu": - response += LoginGuestUser(commandInfo[1],commandInfo[2],commandInfo[3]); - break; - case "a_gpd": - //TODO - start other A1 plugins - response += GetPluginDetails(commandInfo[1]); - break; - } - } - - //Message Output - List d = new List(); - Byte[] reply = Encoding.ASCII.GetBytes(response); - byte[] b2 = new byte[] {0x00}; - d.Add(reply); - d.Add(b2); - byte[] b3 = d.SelectMany(a => a).ToArray(); - if(!isMultiOutput) - SendAsync(b3,0,b3.Length); - else Server.Multicast(b3,0,b3.Length); SendAsync(b3,0,b3.Length); - - // Multicast message to all connected sessions - //Server.Multicast(message); - } - - string LoginGuestUser(string d, string a, string c){ - var responseStream = new MemoryStream(); - using (XmlWriter writer = XmlWriter.Create(responseStream)) - { - writer.WriteStartElement("a_lgu"); - - //Result - writer.WriteElementString("r", "0"); - - //User ID - writer.WriteElementString("u", "0"); - - //Username - writer.WriteElementString("n", "GUESTUSER"); - - //Password - writer.WriteElementString("p", ""); - - //Service ID - writer.WriteElementString("s", "1"); - writer.WriteEndElement(); - writer.WriteEndDocument(); - } - - return System.Text.ASCIIEncoding.UTF8.GetString(responseStream.ToArray()); - } - - string GetPluginDetails(string p){ - var responseStream = new MemoryStream(); - - string serviceID = p; - - string xIPAddress = "localhost"; - string xPort = "80"; - - string bIPAddress = "localhost"; - string bPort = "80"; - - switch(p){ - //Galaxy - case "7": - xPort = (sessionPort + 7).ToString(); - bPort = (sessionPort + 7).ToString(); - break; - - //Trunk - case "10": - xPort = (sessionPort + 10).ToString(); - bPort = (sessionPort + 10).ToString(); - break; - } - - using (XmlWriter writer = XmlWriter.Create(responseStream)) - { - writer.WriteStartElement("a_gpd"); - - //Service ID - writer.WriteElementString("s", serviceID); - - //xIPAddress - writer.WriteElementString("xi", xIPAddress); - - //xPort - writer.WriteElementString("xp", xPort); - - //bIPAddress - writer.WriteElementString("bi", bIPAddress); - - //Plugin ID - writer.WriteElementString("p", p); - writer.WriteEndElement(); - writer.WriteEndDocument(); - } - - return System.Text.ASCIIEncoding.UTF8.GetString(responseStream.ToArray()); - } - - protected override void OnConnected() - { - Console.WriteLine($"[10 - Trunk] TCP session with Id {Id} connected!"); - } - - protected override void OnDisconnected() - { - Console.WriteLine($"[10 - Trunk] TCP session with Id {Id} disconnected!"); - } - - protected override void OnError(SocketError error) - { - Console.WriteLine($"[10 - Trunk] Error - {error}"); - } -} - -class ServerTrunk : TcpServer -{ - int serverPort = 80; - - public ServerTrunk(IPAddress address, int port) : base(address, port) {serverPort = port;} - - protected override TcpSession CreateSession() { return new A1_Trunk(this, serverPort); } - - protected override void OnError(SocketError error) - { - Console.WriteLine($"[10 - Trunk] TCP server caught an error with code {error}"); - } -} \ No newline at end of file diff --git a/A1Emu/A1_FKPlugins/A1_User.cs b/A1Emu/A1_FKPlugins/A1_User.cs deleted file mode 100644 index 5dd9057..0000000 --- a/A1Emu/A1_FKPlugins/A1_User.cs +++ /dev/null @@ -1,237 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Runtime.InteropServices; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading; -using System.Xml; -using NetCoreServer; - -//ArkONE Plugin 1 -//User - -class A1_User : TcpSession -{ - A1_Parser a1parser; - - FKUser a1user; - - int sessionPort = 80; - - public A1_User(TcpServer server, int port) : base(server){a1parser = new A1_Parser(); a1user = new FKUser(); sessionPort = port;} - - protected override void OnReceived(byte[] buffer, long offset, long size) - { - string message = Encoding.ASCII.GetString(buffer, (int)offset, (int)size - 1); - Console.WriteLine("[1 - User] Recieved: " + message); - - bool isMultiOutput = false; - string response = ""; - - string[] commands = a1parser.ParseReceivedMessage(message); - foreach (string command in commands) - { - string[] commandInfo = a1parser.ParseCommand(command); - switch (commandInfo[0]) - { - case "a_lgu": - response += LoginGuestUser(commandInfo[1], commandInfo[2], commandInfo[3]).Remove(0, 1); - break; - case "a_gpd": - response += GetPluginDetails(commandInfo[1]).Remove(0, 1); - break; - case "a_gsd": - response += GetServiceDetails(commandInfo[1]).Remove(0, 1); - break; - } - } - - - Console.WriteLine(response); - - //Message Output - List d = new List(); - Byte[] reply = Encoding.ASCII.GetBytes(response); - byte[] b2 = new byte[] {0x00}; - d.Add(reply); - d.Add(b2); - byte[] b3 = d.SelectMany(a => a).ToArray(); - if(!isMultiOutput) - SendAsync(b3, 0, b3.Length); - else Server.Multicast(b3,0,b3.Length); SendAsync(b3,0,b3.Length); - - // Multicast message to all connected sessions - //Server.Multicast(message); - } - - string LoginGuestUser(string d, string a, string c){ - var responseStream = new MemoryStream(); - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.OmitXmlDeclaration = true; - settings.ConformanceLevel = ConformanceLevel.Fragment; - using (XmlWriter writer = XmlWriter.Create(responseStream, settings)) - { - writer.WriteStartElement("a_lgu"); - - //Result - writer.WriteAttributeString("r", "0"); - - //User ID - writer.WriteAttributeString("u", "0"); - - //Username - writer.WriteAttributeString("n", "GUESTUSER"); - - //Password - writer.WriteAttributeString("p", ""); - - //Service ID - writer.WriteAttributeString("s", "1"); - writer.WriteEndElement(); - writer.Flush(); - writer.Close(); - } - - return System.Text.ASCIIEncoding.UTF8.GetString(responseStream.ToArray()); - } - - string GetPluginDetails(string p){ - var responseStream = new MemoryStream(); - - string serviceID = "1"; - - string xIPAddress = "localhost"; - string xPort = "80"; - - string bIPAddress = "localhost"; - string bPort = "80"; - - switch(p){ - //User - case "1": - xPort = (sessionPort + 1).ToString(); - bPort = (sessionPort + 1).ToString(); - break; - - //Galaxy - case "7": - xPort = (sessionPort + 7).ToString(); - bPort = (sessionPort + 7).ToString(); - break; - - //Trunk - case "10": - xPort = (sessionPort + 10).ToString(); - bPort = (sessionPort + 10).ToString(); - break; - } - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.OmitXmlDeclaration = true; - settings.ConformanceLevel = ConformanceLevel.Fragment; - using (XmlWriter writer = XmlWriter.Create(responseStream, settings)) - { - writer.WriteStartElement("a_gpd"); - - //Service ID - writer.WriteAttributeString("s", serviceID); - - //xIPAddress - writer.WriteAttributeString("xi", xIPAddress); - - //xPort - writer.WriteAttributeString("xp", xPort); - - //bIPAddress - writer.WriteAttributeString("bi", bIPAddress); - - //bPort - writer.WriteAttributeString("bp", bPort); - - //Plugin ID - writer.WriteAttributeString("p", p); - writer.WriteEndElement(); - writer.Flush(); - writer.Close(); - } - - return System.Text.ASCIIEncoding.UTF8.GetString(responseStream.ToArray()); - } - - string GetServiceDetails(string s) - { - var responseStream = new MemoryStream(); - - string serviceID = s; - - string xIPAddress = "localhost"; - string xPort = "80"; - - string bIPAddress = "localhost"; - string bPort = "80"; - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.OmitXmlDeclaration = true; - settings.ConformanceLevel = ConformanceLevel.Fragment; - using (XmlWriter writer = XmlWriter.Create(responseStream, settings)) - { - writer.WriteStartElement("a_gsd"); - - //Service ID - writer.WriteAttributeString("s", serviceID); - - //xIPAddress - writer.WriteAttributeString("xi", xIPAddress); - - //xPort - writer.WriteAttributeString("xp", xPort); - - //bIPAddress - writer.WriteAttributeString("bi", bIPAddress); - - //bPort - writer.WriteAttributeString("bp", bPort); - - writer.WriteEndElement(); - writer.Flush(); - writer.Close(); - } - - return System.Text.ASCIIEncoding.UTF8.GetString(responseStream.ToArray()); - } - - protected override void OnConnected() - { - Console.WriteLine($"[1 - User] TCP session with Id {Id} connected!"); - } - - protected override void OnDisconnected() - { - Console.WriteLine($"[1 - User] TCP session with Id {Id} disconnected!"); - } - - protected override void OnError(SocketError error) - { - Console.WriteLine($"[1 - User] Error - {error}"); - } - -} - -class ServerUser : TcpServer -{ - int serverPort = 80; - - public ServerUser(IPAddress address, int port) : base(address, port) {serverPort = port;} - - protected override TcpSession CreateSession() { return new A1_User(this, serverPort); } - - protected override void OnError(SocketError error) - { - Console.WriteLine($"[1 - User] TCP server caught an error with code {error}"); - } -} \ No newline at end of file diff --git a/A1Emu/A1_Utils/A1_Parser.cs b/A1Emu/A1_Utils/A1_Parser.cs index e669ad4..3e83422 100644 --- a/A1Emu/A1_Utils/A1_Parser.cs +++ b/A1Emu/A1_Utils/A1_Parser.cs @@ -1,68 +1,68 @@ -using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Runtime.InteropServices; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading; -using System.Xml; using System.Xml.Linq; -public class A1_Parser { +public class A1_Parser +{ - public string[] ParseReceivedMessage(string xmlCommand){ - List commandsList = new List(); + //Breaks each message into their own XML string. + public string[] ParseReceivedMessage(string xmlCommand) + { + List commandsList = new List(); - //Splits raw commands by the null character placed in between. To parse Routing Strings. - string[] rawCommandsList = xmlCommand.Split("\0").Where(x => !string.IsNullOrWhiteSpace(x)).ToArray(); + //Splits raw commands by the null character placed in between. To parse Routing Strings. + string[] rawCommandsList = xmlCommand.Split("\0").Where(x => !string.IsNullOrWhiteSpace(x)).ToArray(); - xmlCommand = xmlCommand.Replace("\0", string.Empty); + xmlCommand = xmlCommand.Replace("\0", string.Empty); - XDocument breakableXMLMessage = XDocument.Parse(@"" + xmlCommand + @""); + XDocument breakableXMLMessage = XDocument.Parse(@"" + xmlCommand + @""); - foreach(var comElement in breakableXMLMessage.Descendants("A1Command").Elements()) - { - commandsList.Add(comElement.ToString()); - } + foreach (var comElement in breakableXMLMessage.Descendants("A1Command").Elements()) + { + commandsList.Add(comElement.ToString()); + } - return rawCommandsList.ToArray(); - } + return rawCommandsList.ToArray(); + } - public string[] ParseCommand(string command){ - List commandInfo = new List(); + //Breaks the command into an array of attributes. + public string[] ParseCommand(string command) + { + List commandInfo = new List(); - string substring = ""; + string routingString = ""; - if(command.EndsWith("#")){ - substring = command.Substring(command.LastIndexOf(">") + 1, command.LastIndexOf("#") - command.LastIndexOf(">") - 1); - command = command.Substring(0, command.LastIndexOf(">") + 1); - } + if (command.EndsWith("#")) + { + routingString = command.Substring(command.LastIndexOf(">") + 1, command.LastIndexOf("#") - command.LastIndexOf(">") - 1); + command = command.Substring(0, command.LastIndexOf(">") + 1); + } - XDocument commandXML = XDocument.Parse(command); - XElement commandRoot = commandXML.Root; + XDocument commandXML = XDocument.Parse(command); + XElement commandRoot = commandXML.Root; - commandInfo.Add(commandRoot.Name.LocalName); - - var attrNames = ( - from a in commandRoot.Attributes() - select a.Value - ); + commandInfo.Add(commandRoot.Name.LocalName); - foreach(string value in attrNames){ - commandInfo.Add(value); - } + var attrNames = ( + from a in commandRoot.Attributes() + select a.Value + ); - if(substring != ""){ - string[] substringData = substring.Split("|"); - foreach(string substringID in substringData){ - commandInfo.Add(substringID); - } - } + foreach (string value in attrNames) + { + commandInfo.Add(value); + } - return commandInfo.ToArray(); - } + if (routingString != "") + { + string[] routingData = routingString.Split("|"); + foreach (string routeID in routingData) + { + commandInfo.Add(routeID); + } + } + + return commandInfo.ToArray(); + } } \ No newline at end of file diff --git a/A1Emu/FKObjects/FKUser.cs b/A1Emu/FKObjects/FKUser.cs index 092de7d..ccf22a2 100644 --- a/A1Emu/FKObjects/FKUser.cs +++ b/A1Emu/FKObjects/FKUser.cs @@ -2,20 +2,33 @@ using System; using System.Collections.Generic; using System.Text; -public class FKUser{ +public class FKUser +{ public string connectionID; //A1Data //User ID in Database public int userID; + //Username public string username; + //Password (SENT VIA PLAIN TEXT --- FK USES REALLY BAD SECURITY!!!) public string password; + + //Chat Status + //0 - Ready to Party + //1 - Do Not Disturb + //2 - Playing + //3 - Partying public int status; + + //If the user is online. + //TODO - See if this is set by the game. public int isOnline; - //Buddy List + + //Buddy Lists public string[] rawBuddies; public List buddies; } \ No newline at end of file diff --git a/A1Emu/Program.cs b/A1Emu/Program.cs index d8f1741..9dc33cf 100644 --- a/A1Emu/Program.cs +++ b/A1Emu/Program.cs @@ -1,8 +1,6 @@ using System; -using System.Threading; using System.IO; using System.Reflection; -using System.Data; class Program { @@ -13,59 +11,53 @@ class Program string directory = ""; string sqServer = ""; string ip = "localhost"; - if(!File.Exists(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/config")){ + + if (!File.Exists(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/config")) + { Console.WriteLine("[ERROR] A1Emu did not find a config file in the application's directory."); } - try{ + + try + { foreach (string line in File.ReadLines(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/config")) - { - if(line.Contains("port=")){ - port = Int32.Parse(line.Replace("port=","")); - }else if(line.Contains("directory=")){ - directory = line.Replace("directory=",""); - }else if(line.Contains("sq=")){ - sqServer = line.Replace("sq=",""); - }else if(line.Contains("ip=")){ - ip = line.Replace("ip=",""); + { + if (line.Contains("port=")) + { + port = Int32.Parse(line.Replace("port=", "")); } - } - }catch{ + else if (line.Contains("directory=")) + { + directory = line.Replace("directory=", ""); + } + else if (line.Contains("sq=")) + { + sqServer = line.Replace("sq=", ""); + } + else if (line.Contains("ip=")) + { + ip = line.Replace("ip=", ""); + } + } + } + catch + { Console.WriteLine("[A1Emu] ERROR! Failed to load the config file."); } - - //TODO - Generate a SQL DB. - //Plugin 0/1/7 - //Thread t = new Thread(delegate () - //{ - //replace the IP with your system IP Address... - //ServerRewrite myserver = new ServerRewrite(ip, port, directory, sqServer); - //}); - //t.Start(); + //TODO - Generate a SQL DB. var A1_Plugin0 = new ServerSystem(System.Net.IPAddress.Parse(ip), port, sqServer, directory); A1_Plugin0.Start(); - //var A1_Plugin1 = new ServerUser(System.Net.IPAddress.Parse(ip), port + 1); - //A1_Plugin1.Start(); - - //var A1_Plugin7 = new ServerGalaxy(System.Net.IPAddress.Parse(ip), port + 7); - //A1_Plugin7.Start(); - - //var A1_Plugin10 = new ServerTrunk(System.Net.IPAddress.Parse(ip), port + 10); - //A1_Plugin10.Start(); - Console.WriteLine("[A1Emu] Started!"); - for(;;){ + for (;;) + { string line = Console.ReadLine(); if (string.IsNullOrEmpty(line)) break; } A1_Plugin0.Stop(); - //A1_Plugin1.Stop(); - //A1_Plugin7.Stop(); - //A1_Plugin10.Stop(); } }