24.01.2015, 22:18
Quote:
Introduction
I've been working on my own SA-MP client. I was bored and I was up for a challenge so I thought why not? I have no intention of releasing it anytime soon. The UI (design) is simple, but I've overcome some big challenges and I'm proud of that. Below you'll find a list of some features. Features: - Links to the SA-MP website, forums and wiki. - Ability to save/delete favorites. - Ability to add/remove usernames. - Ability to launch samp-server.exe - Ability to edit server.cfg (config) - Ability to launch samp_debug.exe - Client will scan the game directory for any CLEO files. - Ability to directly connect to a SA-MP server. - If you click on an IP it will retrieve the server ID, hostname, players and locked status. Added in V2: - Ability to launch samp_debug.exe, launch server.cfg and launch the localhost server. - Ability to save & remove usernames. - Ability to import & export favorites. - Ability to save chatlogs. I'm pretty proud of it because I've learned so much. Some parts were pretty difficult but untill now I've always managed to find a solution or workaround. I'll show some examples below. Launching GTA This was one of the first challenges I faced. After some searching I found a video on *******, but I still had to add some things such as password support. In the spoiler you'll find the snippet. Код:
Registry.SetValue("HKEY_CURRENT_USER\\Software\\SAMP", "PlayerName", tbPlayername.Text); string arguments = tbIP.Text + " " + tbPassword.Text; System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(path + "\\samp.exe", arguments); System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procStartInfo; proc.Start(); Retrieving server information This was also very tricky. I used the SACNR API to retrieve the server information. I had to sent a request, store the request and cut out the pieces I needed. (such as the hostname) The API is written in JSON I believe, so I either had to find a JSON parser to come up with my own solution. Below you'll find a snippet. Код:
WebRequest request = WebRequest.Create("http://monitor.sacnr.com/api/?IP=" + IP + "&Port=" + port +"&Action=info"); WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); response.Close(); I had several options. Store the data in a .db file and use SQLite queries to write and read data or store the data internally. I found a way to store the data in the application so I don't have to rely on any external files. I stored the data in the so called 'properties' which are in a small .xml file. (appdata/local) Код:
<setting name="Path" serializeAs="String"> <value>C:\Program Files (x86)\Rockstar Games\GTA San Andreas</value> </setting> <setting name="Favorites" serializeAs="Xml"> <value> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> </ArrayOfString> </value> </setting> In SA-MP you can have a custom IP such as samp.*servername*.net, but because the API requires a regular IP and port, I had to convert it. I used a built-in function to retrieve the IP. Below you'll find a snippet. Код:
private string getIP(string IP) { IPAddress[] addresslist = Dns.GetHostAddresses(IP); foreach (IPAddress i in addresslist) { IP = i.ToString(); } return IP; } Formats This was probably the biggest issue I encountered. You can connect to a SA-MP server by using several formats: IP, IP: port, samp.*servername*.net, samp.*servernet*.net: port. IP and IP: port weren't really a big problem because I could just check if the string contains a ":" and cut out the port. The third format is a bit harder because Dns.GetHostAddress(IP) does not support a port, so I'd have to have to cut out the port, convert the IP and paste the port back. Chatlogs I figured it'd be useful if you could save your chatlogs. The way it works right now is that the application copies the chatlog.txt file from the GTA User Files to another folder. Below you'll find a snippet. Код:
void gta_Exited(object sender, EventArgs e) { if (Properties.Settings.Default.Logging == 1) { string date = DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss"); string fileName = "chatlog_" + date + ".txt"; try { if (Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\GTA San Andreas User Files\\SAMP\\chatlogs")) { File.Copy((System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) + "\\GTA San Andreas User Files\\SAMP\\chatlog.txt", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\GTA San Andreas User Files\\SAMP\\chatlogs\\" + fileName); } else { DirectoryInfo di = Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\GTA San Andreas User Files\\SAMP\\chatlogs"); File.Copy((System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) + "\\GTA San Andreas User Files\\SAMP\\chatlog.txt", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\GTA San Andreas User Files\\SAMP\\chatlogs\\" + fileName); } } catch (Exception ex) { MessageBox.Show("Something went wrong with saving the chatlogs, please try again."); } } } I've had a great time developing this application because I've learned many things. I didn't know 75% of the things I've used, but that's what programming is about. You'll always encounter problems and you have to come up with a solution for them. Be creative and don't be afraid to ask for help! (stackoverflow) Video [video=*******_share;UkRhUNnt6mA]http://*********/UkRhUNnt6mA[/video] Screenshots (v2) |