|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Windows; |
| 6 | +using System.Windows.Controls; |
| 7 | +using System.Windows.Data; |
| 8 | +using System.Windows.Documents; |
| 9 | +using System.Windows.Input; |
| 10 | +using System.Windows.Media; |
| 11 | +using System.Windows.Media.Imaging; |
| 12 | +using System.Windows.Navigation; |
| 13 | +using System.Windows.Shapes; |
| 14 | +using Microsoft.Win32; |
| 15 | +using System.IO; |
| 16 | +using System.Net.NetworkInformation; |
| 17 | +using System.Net.Sockets; |
| 18 | +using System.Diagnostics; |
| 19 | +using System.Net; |
| 20 | + |
| 21 | +namespace SAMP_Launcher |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Interaction logic for MainWindow.xaml |
| 25 | + /// </summary> |
| 26 | + public partial class MainWindow : Window |
| 27 | + { |
| 28 | + private const string _launcherTitle = "SAMP Launcher"; |
| 29 | + // Tên miền hoặc địa chỉ IP của server. |
| 30 | + private const string _serverName = "127.0.0.1"; |
| 31 | + private const int _serverPort = 7777; |
| 32 | + // Password để vào server. |
| 33 | + private const string _serverPassword = ""; |
| 34 | + |
| 35 | + private string _serverIP; |
| 36 | + private string _playerName; |
| 37 | + private SAMP.Query _query; |
| 38 | + |
| 39 | + public MainWindow() |
| 40 | + { |
| 41 | + InitializeComponent(); |
| 42 | + |
| 43 | + MainWindow1.Title = _launcherTitle; |
| 44 | + lblSvrName.Content = _launcherTitle; |
| 45 | + lblStatus.Text = "Đang chờ"; |
| 46 | + } |
| 47 | + |
| 48 | + private void MainWindow1_Loaded(object sender, RoutedEventArgs e) |
| 49 | + { |
| 50 | + if (checkFiles() == false) |
| 51 | + Application.Current.Shutdown(); |
| 52 | + |
| 53 | + // Lấy tên nhân vật từ registry và cho vào textbox. |
| 54 | + RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\SAMP"); |
| 55 | + if (key != null) |
| 56 | + { |
| 57 | + Object value = key.GetValue("PlayerName"); |
| 58 | + if (value != null) |
| 59 | + { |
| 60 | + txtbxPlayerName.Text = value.ToString(); |
| 61 | + } |
| 62 | + } |
| 63 | + _playerName = txtbxPlayerName.Text; |
| 64 | + |
| 65 | + |
| 66 | + IPAddress svIP; |
| 67 | + if (IPAddress.TryParse(_serverName, out svIP)) |
| 68 | + { |
| 69 | + _serverIP = svIP.ToString(); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + // Phân giải tên miền. |
| 74 | + IPHostEntry hostEntry = Dns.GetHostEntry(_serverName); |
| 75 | + if (hostEntry.AddressList.Length == 0) |
| 76 | + { |
| 77 | + lblStatus.Text = "<!> Đã có lỗi xảy ra với DNS. Xin vui lòng liên hệ Admin."; |
| 78 | + return; |
| 79 | + } |
| 80 | + _serverIP = hostEntry.AddressList[0].ToString(); |
| 81 | + } |
| 82 | + _query = new SAMP.Query(_serverIP, _serverPort, false); |
| 83 | + |
| 84 | + updateServerInfo(); |
| 85 | + } |
| 86 | + |
| 87 | + private void updateServerInfo() |
| 88 | + { |
| 89 | + if (_query.Send((char)SAMP.Query.PaketOpcode.Info)) |
| 90 | + { |
| 91 | + int count = _query.Receive(); |
| 92 | + if (count == 0) |
| 93 | + { |
| 94 | + lblStatus.Text = "<!> Không thể kết nối đến server."; |
| 95 | + btnConnect.IsEnabled = false; |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + lblServer.Content = _query.hostname; |
| 100 | + lblGameMode.Content = _query.gamemode; |
| 101 | + lblPlayersCount.Content = _query.players + '/' + _query.max_players; |
| 102 | + |
| 103 | + if (_query.Send((char)SAMP.Query.PaketOpcode.Rules)) |
| 104 | + { |
| 105 | + count = _query.Receive(); |
| 106 | + if (count == 0) |
| 107 | + { |
| 108 | + lblStatus.Text = "<!> Không thể kết nối đến server."; |
| 109 | + btnConnect.IsEnabled = false; |
| 110 | + return; |
| 111 | + } |
| 112 | + string[] rules = _query.Store(count); |
| 113 | + |
| 114 | + for (int i = 0; i < rules.Length - 2; i += 2) |
| 115 | + { |
| 116 | + if (rules[i] == "mapname") |
| 117 | + { |
| 118 | + lblMap.Content = rules[i + 1]; |
| 119 | + } |
| 120 | + else if (rules[i] == "weburl") |
| 121 | + { |
| 122 | + lblWebsite.Content = rules[i + 1]; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + btnConnect.IsEnabled = false; |
| 130 | + lblStatus.Text = "<!> Không thể kết nối đến server."; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private bool checkFiles() |
| 135 | + { |
| 136 | + if (File.Exists("samp.exe") == false) |
| 137 | + { |
| 138 | + MessageBox.Show("<!> Không tìm thấy samp.exe\n<!> Vui lòng bỏ Launcher vào cùng thư mục với file samp.exe (trong folder game).", "Lỗi"); |
| 139 | + return false; |
| 140 | + } |
| 141 | + //if (File.Exists("d3d9.dll")) |
| 142 | + //{ |
| 143 | + // MessageBox.Show("<!> Hê thống nhận thấy bạn đang sử dụng phần mềm thứ 3 không hợp lệ (s0beit).\n<!> Hãy xóa các file đó hoặc re-install game.", "Lỗi"); |
| 144 | + // return false; |
| 145 | + //} |
| 146 | + //if (File.Exists("cleo.asi")) |
| 147 | + //{ |
| 148 | + // MessageBox.Show("<!> Hê thống nhận thấy bạn đang sử dụng phần mềm thứ 3 không hợp lệ (CLEO).\n<!> Hãy xóa các file đó hoặc re-install game.", "Lỗi"); |
| 149 | + // return false; |
| 150 | + //} |
| 151 | + return true; |
| 152 | + } |
| 153 | + |
| 154 | + private void btnConnect_Click(object sender, RoutedEventArgs e) |
| 155 | + { |
| 156 | + if (checkFiles() == false) |
| 157 | + return; |
| 158 | + |
| 159 | + // kiểm tra tên nhân vật có thay đổi không |
| 160 | + lblStatus.Text = "[*] Đang kiểm tra tên nhân vật."; |
| 161 | + if (txtbxPlayerName.Text != _playerName) |
| 162 | + { |
| 163 | + // nếu tên mới không thỏa điều kiện |
| 164 | + if (txtbxPlayerName.Text.Length > 20 || txtbxPlayerName.Text.Length < 4) |
| 165 | + { |
| 166 | + MessageBox.Show("<!> Tên không được ít hơn 4 hoặc nhiều hơn 20 ký tự.", "Tên không hợp lệ!"); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + // chỉnh lại giá trị trong registry |
| 171 | + RegistryKey setKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\SAMP"); |
| 172 | + if (setKey != null) |
| 173 | + { |
| 174 | + setKey.SetValue("PlayerName", txtbxPlayerName.Text); |
| 175 | + } |
| 176 | + } |
| 177 | + //MessageBox.Show(_serverIP + ":" + _serverPort); |
| 178 | + |
| 179 | + lblStatus.Text = "[*] Đang tiến hành kết nối đến server."; |
| 180 | + ProcessStartInfo startInfo = new ProcessStartInfo(); |
| 181 | + startInfo.FileName = "samp.exe"; |
| 182 | + startInfo.Arguments = _serverIP + ":" + _serverPort + " " + _serverPassword; |
| 183 | + lblStatus.Text = "[*] Đang vào game."; |
| 184 | + Process.Start(startInfo); |
| 185 | + lblStatus.Text = "[*] Thành công."; |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments