Ejemplo n.º 1
0
    internal void SaveServer(string ip, string port, string name)
    {
        try {
            serversManager.savedServerList.savedServers.Clear();
            UnityEngine.Debug.Log("\tSaved Servers Cleared from Memory");

            if (File.Exists(path + "SavedServers.xml"))
            {
                UnityEngine.Debug.Log("\tReading Current Saved Servers to Memory");

                StreamReader streamReaderS = new StreamReader(path + "SavedServers.xml");
                string       xmlS          = streamReaderS.ReadToEnd();
                streamReaderS.Close();

                UnityEngine.Debug.Log("\t\tRead into Memory");

                serversManager.savedServerList = xmlS.DeserializeXml <SavedServerList>();

                UnityEngine.Debug.Log("\t\tDeserialized");
            }

            UnityEngine.Debug.Log("\tCreating New Server Entry");

            SavedServer newServer = new SavedServer();
            newServer.ipaddress = ip;
            UnityEngine.Debug.Log("\t\t" + newServer.ipaddress);

            newServer.port = port;
            UnityEngine.Debug.Log("\t\t" + newServer.port);

            newServer.name = name;
            UnityEngine.Debug.Log("\t\t" + newServer.name);

            newServer.index = serversManager.savedServerList.savedServers.Count + 1;
            UnityEngine.Debug.Log("\t\t" + newServer.index);

            serversManager.savedServerList.savedServers.Add(newServer);

            UnityEngine.Debug.Log("\tNew Server Added to Memory");

            XmlSerializer serializerS = new XmlSerializer(serversManager.savedServerList.GetType());
            using (StreamWriter writerS = new StreamWriter(path + "SavedServers.xml"))
            {
                serializerS.Serialize(writerS.BaseStream, serversManager.savedServerList);

                UnityEngine.Debug.Log("\tUpdated Saved Servers List Written to Disk");
            }
        } catch (Exception e)
        {
            error = true;
            UnityEngine.Debug.LogError("\tERROR: " + e);
        }
    }
Ejemplo n.º 2
0
        public static SavedServers ReadFromStream(Stream stream, bool keepOpen = false)
        {
            SavedServers servers = new SavedServers();

            using (BinaryReader reader = new BinaryReader(stream, Encoding.Unicode, keepOpen))
            {
                uint count = reader.ReadUInt32();
                for (int i = 0; i < count; i++)
                {
                    SavedServer s = SavedServer.ReadFrom(reader);
                    servers.Servers.Add(s);
                }
            }

            return(servers);
        }
Ejemplo n.º 3
0
            internal static SavedServer ReadFrom(BinaryReader stream)
            {
                SavedServer s = new SavedServer();

                s.Host      = stream.ReadString();
                s.Port      = stream.ReadUInt16();
                s.IsBedrock = stream.ReadBoolean();

                s.Height = stream.ReadInt32();
                s.Width  = stream.ReadInt32();

                var dataSize = s.Width * s.Height;

                uint[] data = new uint[dataSize];
                for (int i = 0; i < dataSize; i++)
                {
                    data[i] = stream.ReadUInt32();
                }

                s.Icon    = data;
                s.HasIcon = data.Length > 0;

                return(s);
            }
Ejemplo n.º 4
0
        public UIServerListItem(SavedServer server)
        {
            BorderColor = new Color(89, 116, 213) * 0.7f;
            _buttonFavoriteActiveTexture   = TextureManager.Load("Images/UI/ButtonFavoriteActive");
            _buttonFavoriteInactiveTexture = TextureManager.Load("Images/UI/ButtonFavoriteInactive");
            _buttonPlayTexture             = TextureManager.Load("Images/UI/ButtonPlay");
            _buttonDeleteTexture           = TextureManager.Load("Images/UI/ButtonDelete");
            Height.Set(64, 0f);
            Width.Set(0f, 1f);
            SetPadding(6f);
            OnDoubleClick += JoinServer;
            Server         = server;

            UIImageButton JoinButton = new UIImageButton(_buttonPlayTexture);

            JoinButton.VAlign = 1f;
            JoinButton.Left.Set(4f, 0f);
            JoinButton.OnClick     += JoinServer;
            JoinButton.OnMouseOver += JoinMouseOver;
            JoinButton.OnMouseOut  += ButtonMouseOut;
            Append(JoinButton);

            UIImageButton FavoriteButton = new UIImageButton(Server.IsFavorite ? _buttonFavoriteActiveTexture : _buttonFavoriteInactiveTexture);

            FavoriteButton.VAlign = 1f;
            FavoriteButton.Left.Set(28f, 0f);
            FavoriteButton.OnClick     += FavoriteButtonClick;
            FavoriteButton.OnMouseOver += FavoriteMouseOver;
            FavoriteButton.OnMouseOut  += ButtonMouseOut;
            FavoriteButton.SetVisibility(1f, Server.IsFavorite ? 0.8f : 0.4f);
            Append(FavoriteButton);

            UIImageButton ConfigButton = new UIImageButton(BetterServerList.ConfigButtonTexture);

            ConfigButton.VAlign = 1f;
            ConfigButton.Left.Set(28f + 24, 0f);
            ConfigButton.OnClick     += ConfigButtonClick;
            ConfigButton.OnMouseOver += ConfigMouseOver;
            ConfigButton.OnMouseOut  += ButtonMouseOut;
            Append(ConfigButton);

            UIImageButton ForgetButton = new UIImageButton(_buttonDeleteTexture);

            ForgetButton.VAlign       = 1f;
            ForgetButton.HAlign       = 1f;
            ForgetButton.OnClick     += DeleteButtonClick;
            ForgetButton.OnMouseOver += DeleteMouseOver;
            ForgetButton.OnMouseOut  += DeleteMouseOut;
            _deleteButton             = ForgetButton;
            if (!Server.IsFavorite)
            {
                Append(ForgetButton);
            }

            int buttonLabelLeft = 80;

            primaryButtonLabel        = new UIText("");
            primaryButtonLabel.VAlign = 1f;
            primaryButtonLabel.Left.Set(buttonLabelLeft, 0f);
            primaryButtonLabel.Top.Set(-3f, 0f);
            Append(primaryButtonLabel);

            secondaryButtonLabel        = new UIText("");
            secondaryButtonLabel.VAlign = 1f;
            secondaryButtonLabel.HAlign = 1f;
            secondaryButtonLabel.Left.Set(-30f, 0f);
            secondaryButtonLabel.Top.Set(-3f, 0f);
            Append(secondaryButtonLabel);
        }
Ejemplo n.º 5
0
 public void RemoveServer(SavedServer server)
 {
     Servers.Remove(server);
 }
Ejemplo n.º 6
0
 public void AddServer(SavedServer server)
 {
     Servers.Add(server);
 }
Ejemplo n.º 7
0
        private async Task Scanner()
        {
            var payload = new byte[] {
                14,
            };

            const int multicastPort = 17444;
            var       udpEndpoint   = new IPEndPoint(IPAddress.Any, multicastPort);

            using var udp = new UdpClient();
            udp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            udp.Client.Bind(udpEndpoint);

            var multicastAddr = IPAddress.Parse("224.0.0.147");

            udp.JoinMulticastGroup(multicastAddr);

            var timerChannel = Channel.CreateBounded <byte>(1);

            _ = Task.Run(async() => {
                while (!this.cts.IsCancellationRequested)
                {
                    await timerChannel.Writer.WriteAsync(1);
                    await Task.Delay(5_000, this.cts.Token);
                }
            });

            var cancelTask   = this.cancelChannel.Reader.ReadAsync().AsTask();
            var timerTask    = timerChannel.Reader.ReadAsync().AsTask();
            var incomingTask = udp.ReceiveAsync();

            while (!this.cts.IsCancellationRequested)
            {
                var result = await Task.WhenAny(cancelTask, timerTask, incomingTask);

                if (result == cancelTask)
                {
                    break;
                }

                if (result == timerTask)
                {
                    timerTask = timerChannel.Reader.ReadAsync().AsTask();
                    await udp.SendAsync(payload, payload.Length, new IPEndPoint(multicastAddr, multicastPort));
                }
                else if (result == incomingTask)
                {
                    var incoming = await incomingTask;
                    incomingTask = udp.ReceiveAsync();

                    var server = ScannedServer.Decode(incoming.Buffer, incoming.RemoteEndPoint.Address.ToString());
                    if (server == null)
                    {
                        continue;
                    }

                    var saved = new SavedServer(server.playerName, server.address, server.port);
                    if (this.Servers.Contains(saved))
                    {
                        continue;
                    }

                    this.Dispatch(() => {
                        this.Servers.Add(saved);
                    });
                }
            }
        }
Ejemplo n.º 8
0
        private void AddServer(SavedServer server)
        {
            var dialog = new ManageServer(this, server, true);

            dialog.ShowDialog();
        }
Ejemplo n.º 9
0
 private void SavedServers_OnItemDoubleClick(SavedServer server)
 {
     this.AddServer(server);
 }