コード例 #1
0
ファイル: GridServer.xaml.cs プロジェクト: Apache553/BMCL
        private void GetServerInfo(object obj)
        {
            Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate { btnReflushServer.Content = LangManager.GetLangFromResource("ServerListGetting"); }));
            if (File.Exists(@".minecraft\servers.dat"))
            {
                _sl = new serverlist.serverlist();
                foreach (serverlist.serverinfo info in _sl.info)
                {
                    var start = DateTime.Now;
                    var server = new object[7];
                    server[0] = info.Name;
                    server[1] = info.IsHide ? LangManager.GetLangFromResource("ServerListYes") : LangManager.GetLangFromResource("ServerListNo");
                    if (info.IsHide)
                        server[2] = LangManager.GetLangFromResource("btnMiniSize");//要的只是这两个字
                    else
                        server[2] = info.Address;
                    server[3] = " ";
                    server[4] = " ";
                    server[5] = " ";
                    try
                    {
                        var bytes = 512;
                        var recive = new byte[bytes];

                        using (var con = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { ReceiveTimeout = 3000, SendTimeout = 3000 })
                        {
                            con.Connect(Dns.GetHostAddresses(info.Address.Split(':')[0] == "0" ? "127.0.0.1" : info.Address.Split(':')[0]),
                                info.Address.Split(':').Length == 1 ? 25565 : int.Parse(info.Address.Split(':')[1]));
                            con.Send(new byte[] { 254 });
                            con.Send(new byte[] { 1 });
                            recive = new byte[512];
                            bytes = con.Receive(recive);
                            con.Shutdown(SocketShutdown.Both);
                            con.Close();
                        }

                        if (recive[0] != 255)
                        {
                            throw new Exception(LangManager.GetLangFromResource("ServerListInvildReply"));
                        }

                        var unirecive = new System.Collections.Generic.List<byte>();
                        for (var index = 1; index < bytes; index += 2)
                        {
                            unirecive.Add(recive[index + 1]);
                            unirecive.Add(recive[index]);
                        }
                        var message = Encoding.Unicode.GetString(unirecive.ToArray());

                        var end = DateTime.Now;
                        //Logger.info(message);
                        var astring = message.Split('\u00a7');
                        if (astring.Length == 3)
                        {
                            server[3] = astring[0].IndexOf('\r') == 0 ? astring[0].Substring(1) : astring[0];
                            server[4] = LangManager.GetLangFromResource("Unknown");//未知
                            server[5] = astring[1] + "/" + astring[2];
                        }
                        if (astring.Length == 2)
                        {
                            astring = astring[1].Split('\0');
                            server[3] = astring[3];
                            server[4] = astring[2];
                            server[5] = astring[4] + "/" + astring[5];
                        }
                        server[6] = (end - start).Milliseconds + "ms";
                        server[3] = ((string)server[3]).Replace("  ", "").Replace(new String(new char[] { (char)0x1c }), "");
                    }
                    catch (SocketException ex)
                    {
                        server[6] = LangManager.GetLangFromResource("ServerListSocketException") + ex.Message;
                    }
                    catch (Exception ex)
                    {
                        server[6] = LangManager.GetLangFromResource("ServerListUnknowServer") + ex.Message;
                    }
                    finally
                    {
                        var logger = new StringBuilder();
                        foreach (string str in server)
                        {
                            logger.Append(str + ",");
                        }
                        Logger.log(logger.ToString());
                        lock (_serverListDataTable)
                        {
                            _serverListDataTable.Rows.Add(server);
                            Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate
                            {
                                listServer.DataContext = null;
                                listServer.DataContext = _serverListDataTable;
                            }));
                        }
                    }
                }
                Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate { btnReflushServer.Content = LangManager.GetLangFromResource("btnReflushServer"); btnReflushServer.IsEnabled = true; }));
            }
            else
            {
                if (MessageBox.Show(LangManager.GetLangFromResource("ServerListNotFound"), "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    if (!Directory.Exists(".minecraft"))
                    {
                        Directory.CreateDirectory(".minecraft");
                    }
                    var serverdat = new FileStream(@".minecraft\servers.dat", FileMode.Create);
                    serverdat.Write(Convert.FromBase64String(Resource.ServerDat.Header), 0, Convert.FromBase64String(Resource.ServerDat.Header).Length);
                    serverdat.WriteByte(0);
                    serverdat.Close();
                    _sl = new serverlist.serverlist();
                    Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate
                    {
                        btnAddServer.IsEnabled = true;
                        btnDeleteServer.IsEnabled = true;
                        btnEditServer.IsEnabled = true;
                        btnReflushServer.IsEnabled = true;
                    }));
                }
                else
                {
                    Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate
                    {
                        btnAddServer.IsEnabled = false;
                        btnDeleteServer.IsEnabled = false;
                        btnEditServer.IsEnabled = false;
                        btnReflushServer.IsEnabled = false;
                    }));
                }
            }
        }
コード例 #2
0
ファイル: GridServer.xaml.cs プロジェクト: xfl03/BMCL
        private void GetServerInfo(object obj)
        {
            Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate { btnReflushServer.Content = LangManager.GetLangFromResource("ServerListGetting"); }));
            if (File.Exists(@".minecraft\servers.dat"))
            {
                _sl = new serverlist.serverlist();
                foreach (serverlist.serverinfo info in _sl.info)
                {
                    DateTime start = DateTime.Now;
                    var server = new object[7];
                    server[0] = info.Name;
                    server[1] = info.IsHide ? LangManager.GetLangFromResource("ServerListYes") : LangManager.GetLangFromResource("ServerListNo");
                    if (info.IsHide)
                        server[2] = string.Empty;
                    else
                        server[2] = info.Address;
                    try
                    {
                        var con = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                        {
                            ReceiveTimeout = 3000,
                            SendTimeout = 3000
                        };
                        con.Connect(Dns.GetHostAddresses(info.Address.Split(':')[0]),
                            info.Address.Split(':').Length == 1 ? 25565 : int.Parse(info.Address.Split(':')[1]));
                        con.Send(new byte[] { 254 });
                        con.Send(new byte[] { 1 });
                        var recive = new byte[512];
                        int bytes = con.Receive(recive);
                        if (recive[0] != 255)
                        {
                            throw new Exception(LangManager.GetLangFromResource("ServerListInvildReply"));
                        }
                        string message = Encoding.UTF8.GetString(recive, 4, bytes - 4);
                        var remessage = new StringBuilder(30);
                        for (int i = 0; i <= message.Length; i += 2)
                        {
                            remessage.Append(message[i]);
                        }
                        message = remessage.ToString();
                        con.Shutdown(SocketShutdown.Both);
                        con.Close();
                        DateTime end = DateTime.Now;
                        char[] achar = message.ToCharArray();

                        for (int i = 0; i < achar.Length; ++i)
                        {
                            if (achar[i] != 167 && achar[i] != 0 && char.IsControl(achar[i]))
                            {
                                achar[i] = (char)63;
                            }
                        }
                        message = new String(achar);
                        if (message[0] == (char)253 || message[0] == (char)65533)
                        {
                            message = (char)167 + message.Substring(1);
                        }
                        if (message.StartsWith("\u00a7") && message.Length > 1)
                        {
                            string[] astring = message.Substring(1).Split('\0');
                            if (MathHelper.parseIntWithDefault(astring[0], 0) == 1)
                            {
                                server[3] = astring[3];
                                server[4] = astring[2];
                                int online = MathHelper.parseIntWithDefault(astring[4], 0);
                                int maxplayer = MathHelper.parseIntWithDefault(astring[5], 0);
                                server[5] = online + "/" + maxplayer;
                            }
                        }
                        else
                        {
                            server[3] = " ";
                            server[4] = " ";
                            server[5] = " ";
                        }
                        server[6] = (end - start).Milliseconds + " ms";
                    }
                    catch (SocketException ex)
                    {
                        server[3] = " ";
                        server[4] = " ";
                        server[5] = " ";
                        server[6] = LangManager.GetLangFromResource("ServerListSocketException") + ex.Message;
                        //server.SubItems[0].ForeColor = Color.Red;
                    }
                    catch (Exception ex)
                    {
                        server[3] = " ";
                        server[4] = " ";
                        server[5] = " ";
                        server[6] = LangManager.GetLangFromResource("ServerListUnknowServer") + ex.Message;
                        //server.SubItems[0].ForeColor = Color.Red;
                    }
                    finally
                    {
                        var logger = new StringBuilder();
                        foreach (string str in server)
                        {
                            logger.Append(str + ",");
                        }
                        Logger.log(logger.ToString());
                        lock (_serverListDataTable)
                        {
                            _serverListDataTable.Rows.Add(server);
                            Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate
                            {
                                listServer.DataContext = null;
                                listServer.DataContext = _serverListDataTable;
                            }));
                        }
                    }
                }
                Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate { btnReflushServer.Content = LangManager.GetLangFromResource("btnReflushServer"); btnReflushServer.IsEnabled = true; }));
            }
            else
            {
                if (MessageBox.Show(LangManager.GetLangFromResource("ServerListNotFound"), "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    if (!Directory.Exists(".minecraft"))
                    {
                        Directory.CreateDirectory(".minecraft");
                    }
                    var serverdat = new FileStream(@".minecraft\servers.dat", FileMode.Create);
                    serverdat.Write(Convert.FromBase64String(Resource.ServerDat.Header), 0, Convert.FromBase64String(Resource.ServerDat.Header).Length);
                    serverdat.WriteByte(0);
                    serverdat.Close();
                    _sl = new serverlist.serverlist();
                    Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate
                    {
                        btnAddServer.IsEnabled = true;
                        btnDeleteServer.IsEnabled = true;
                        btnEditServer.IsEnabled = true;
                        btnReflushServer.IsEnabled = true;
                    }));
                }
                else
                {
                    Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate
                    {
                        btnAddServer.IsEnabled = false;
                        btnDeleteServer.IsEnabled = false;
                        btnEditServer.IsEnabled = false;
                        btnReflushServer.IsEnabled = false;
                    }));
                }
            }
        }
コード例 #3
0
ファイル: GridServer.xaml.cs プロジェクト: vifyan/BMCL
        private void GetServerInfo(object obj)
        {
            Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate { btnReflushServer.Content = LangManager.GetLangFromResource("ServerListGetting"); }));
            if (File.Exists(@".minecraft\servers.dat"))
            {
                _sl = new serverlist.serverlist();
                foreach (serverlist.serverinfo info in _sl.info)
                {
                    DateTime start  = DateTime.Now;
                    var      server = new object[7];
                    server[0] = info.Name;
                    server[1] = info.IsHide ? LangManager.GetLangFromResource("ServerListYes") : LangManager.GetLangFromResource("ServerListNo");
                    if (info.IsHide)
                    {
                        server[2] = string.Empty;
                    }
                    else
                    {
                        server[2] = info.Address;
                    }
                    try
                    {
                        var con = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                        {
                            ReceiveTimeout = 3000,
                            SendTimeout    = 3000
                        };
                        con.Connect(Dns.GetHostAddresses(info.Address.Split(':')[0]),
                                    info.Address.Split(':').Length == 1 ? 25565 : int.Parse(info.Address.Split(':')[1]));
                        con.Send(new byte[] { 254 });
                        con.Send(new byte[] { 1 });
                        var recive = new byte[512];
                        int bytes  = con.Receive(recive);
                        if (recive[0] != 255)
                        {
                            throw new Exception(LangManager.GetLangFromResource("ServerListInvildReply"));
                        }
                        string message   = Encoding.UTF8.GetString(recive, 4, bytes - 4);
                        var    remessage = new StringBuilder(30);
                        for (int i = 0; i <= message.Length; i += 2)
                        {
                            remessage.Append(message[i]);
                        }
                        message = remessage.ToString();
                        con.Shutdown(SocketShutdown.Both);
                        con.Close();
                        DateTime end   = DateTime.Now;
                        char[]   achar = message.ToCharArray();

                        for (int i = 0; i < achar.Length; ++i)
                        {
                            if (achar[i] != 167 && achar[i] != 0 && char.IsControl(achar[i]))
                            {
                                achar[i] = (char)63;
                            }
                        }
                        message = new String(achar);
                        if (message[0] == (char)253 || message[0] == (char)65533)
                        {
                            message = (char)167 + message.Substring(1);
                        }
                        if (message.StartsWith("\u00a7") && message.Length > 1)
                        {
                            string[] astring = message.Substring(1).Split('\0');
                            if (MathHelper.parseIntWithDefault(astring[0], 0) == 1)
                            {
                                server[3] = astring[3];
                                server[4] = astring[2];
                                int online    = MathHelper.parseIntWithDefault(astring[4], 0);
                                int maxplayer = MathHelper.parseIntWithDefault(astring[5], 0);
                                server[5] = online + "/" + maxplayer;
                            }
                        }
                        else
                        {
                            server[3] = " ";
                            server[4] = " ";
                            server[5] = " ";
                        }
                        server[6] = (end - start).Milliseconds + " ms";
                    }
                    catch (SocketException ex)
                    {
                        server[3] = " ";
                        server[4] = " ";
                        server[5] = " ";
                        server[6] = LangManager.GetLangFromResource("ServerListSocketException") + ex.Message;
                        //server.SubItems[0].ForeColor = Color.Red;
                    }
                    catch (Exception ex)
                    {
                        server[3] = " ";
                        server[4] = " ";
                        server[5] = " ";
                        server[6] = LangManager.GetLangFromResource("ServerListUnknowServer") + ex.Message;
                        //server.SubItems[0].ForeColor = Color.Red;
                    }
                    finally
                    {
                        var logger = new StringBuilder();
                        foreach (string str in server)
                        {
                            logger.Append(str + ",");
                        }
                        Logger.log(logger.ToString());
                        lock (_serverListDataTable)
                        {
                            _serverListDataTable.Rows.Add(server);
                            Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate
                            {
                                listServer.DataContext = null;
                                listServer.DataContext = _serverListDataTable;
                            }));
                        }
                    }
                }
                Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate { btnReflushServer.Content = LangManager.GetLangFromResource("btnReflushServer"); btnReflushServer.IsEnabled = true; }));
            }
            else
            {
                if (MessageBox.Show(LangManager.GetLangFromResource("ServerListNotFound"), "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    if (!Directory.Exists(".minecraft"))
                    {
                        Directory.CreateDirectory(".minecraft");
                    }
                    var serverdat = new FileStream(@".minecraft\servers.dat", FileMode.Create);
                    serverdat.Write(Convert.FromBase64String(Resource.ServerDat.Header), 0, Convert.FromBase64String(Resource.ServerDat.Header).Length);
                    serverdat.WriteByte(0);
                    serverdat.Close();
                    _sl = new serverlist.serverlist();
                    Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate
                    {
                        btnAddServer.IsEnabled     = true;
                        btnDeleteServer.IsEnabled  = true;
                        btnEditServer.IsEnabled    = true;
                        btnReflushServer.IsEnabled = true;
                    }));
                }
                else
                {
                    Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate
                    {
                        btnAddServer.IsEnabled     = false;
                        btnDeleteServer.IsEnabled  = false;
                        btnEditServer.IsEnabled    = false;
                        btnReflushServer.IsEnabled = false;
                    }));
                }
            }
        }