Thursday, September 19, 2024 9:25:32 PM
> settings

Customize


Authenticate

> MainWindow.xaml.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using WebSocketSharp;

namespace ArcasDevTools
{
    public partial class MainWindow : Window
    {
        public WebSocket sWS;
        public WebSocket cWS;
        private Task _connectToServer;
        private Task _connectToClient;

        public MainWindow()
        {
            InitializeComponent();

            lb_ClientInfo.Content = "FPS: N/A | Threads: N/A";
            lb_ClientStatus.Content = "NOT CONNECTED";
            lb_ClientStatus.Foreground = (SolidColorBrush)Application.Current.Resources["ADT_Error"];

            lb_ServerInfo.Content = "FPS: N/A | Threads: N/A";
            lb_ServerStatus.Content = "NOT CONNECTED";
            lb_ServerStatus.Foreground = (SolidColorBrush)Application.Current.Resources["ADT_Error"];

            btn_ExecClient.IsEnabled = false;
            btn_ExecServer.IsEnabled = false;

            ConnectToServer();
            ConnectToClient();
        }

        private void ConnectToServer()
        {
            sWS = new WebSocket("ws://127.0.0.1:4649/Echo");
            sWS.OnOpen += (sender, e) =>
            {
                lb_ServerStatus.Dispatcher.Invoke(() => { lb_ServerStatus.Content = "CONNECTED"; });
                lb_ServerStatus.Dispatcher.Invoke(() => { lb_ServerStatus.Foreground = (SolidColorBrush)Application.Current.Resources["ADT_Green"]; });
                tb_ServerConsole.AddText("Successfully connected to server DLL", Application.Current.Resources["ADT_Green"].ToString());
                btn_ExecServer.Dispatcher.Invoke(() => { btn_ExecServer.IsEnabled = true; });
                tb_ServerConsole.Dispatcher.Invoke(() => { tb_ServerConsole.Document.Blocks.Clear(); });
            };

            sWS.OnMessage += (sender, e) =>
            {
                string[] passthrough = e.Data.Split(':');

                switch (passthrough[0])
                {
                    case "0":
                        tb_ServerConsole.AddText(passthrough[1], passthrough[2]);
                        break;
                    case "1":
                        lb_ServerInfo.Dispatcher.Invoke(() => { lb_ServerInfo.Content = $"FPS: {Math.Round(double.Parse(passthrough[1]), 2)} | Threads: {passthrough[3]}"; });
                        break;
                    default:
                        break;
                }
            };

            sWS.OnError += (sender, e) =>
            {
                lb_ServerStatus.Dispatcher.Invoke(() => { lb_ServerStatus.Content = "ERROR"; });
                lb_ServerStatus.Dispatcher.Invoke(() => { lb_ServerStatus.Foreground = (SolidColorBrush)Application.Current.Resources["ADT_Error"]; });
                tb_ServerConsole.AddText(e.Message, Application.Current.Resources["ADT_Error"].ToString());
                btn_ExecServer.Dispatcher.Invoke(() => { btn_ExecServer.IsEnabled = false; });
            };

            sWS.OnClose += (sender, e) =>
            {
                lb_ServerStatus.Dispatcher.Invoke(() => { lb_ServerStatus.Content = "NOT CONNECTED"; });
                lb_ServerStatus.Dispatcher.Invoke(() => { lb_ServerStatus.Foreground = (SolidColorBrush)Application.Current.Resources["ADT_Error"]; });
                btn_ExecServer.Dispatcher.Invoke(() => { btn_ExecServer.IsEnabled = false; });
                lb_ServerInfo.Dispatcher.Invoke(() => { lb_ServerInfo.Content = "FPS: N/A | Threads: N/A"; });

                if (_connectToServer == null || _connectToServer.IsCompleted)
                {
                    _connectToServer = new Task(() =>
                    {
                        while (true)
                        {
                            tb_ServerConsole.AddText("Attempting to connect to server...", Application.Current.Resources["ADT_Error"].ToString());
                            sWS.Connect();
                            if (sWS.IsAlive) break;
                            Thread.Sleep(1000);
                        }
                    });

                    _connectToServer.Start();
                }
            };

            sWS.Connect();
        }

        private void ConnectToClient()
        {
            cWS = new WebSocket("ws://127.0.0.1:4648/Echo");
            cWS.OnOpen += (sender, e) =>
            {
                lb_ClientStatus.Dispatcher.Invoke(() => { lb_ClientStatus.Content = "CONNECTED"; });
                lb_ClientStatus.Dispatcher.Invoke(() => { lb_ClientStatus.Foreground = (SolidColorBrush)Application.Current.Resources["ADT_Green"]; });
                tb_ClientConsole.AddText("Successfully connected to client DLL", Application.Current.Resources["ADT_Green"].ToString());
                btn_ExecClient.Dispatcher.Invoke(() => { btn_ExecClient.IsEnabled = true; });
                tb_ClientConsole.Dispatcher.Invoke(() => { tb_ClientConsole.Document.Blocks.Clear(); });
            };

            cWS.OnMessage += (sender, e) =>
            {
                string[] passthrough = e.Data.Split(':');

                switch (passthrough[0])
                {
                    case "0":
                        tb_ClientConsole.AddText(passthrough[1], passthrough[2]);
                        break;
                    case "1":
                        lb_ClientInfo.Dispatcher.Invoke(() => { lb_ClientInfo.Content = $"FPS: {Math.Round(double.Parse(passthrough[1]), 2)} | Threads: {passthrough[3]}"; });
                        break;
                    default:
                        break;
                }
            };

            cWS.OnError += (sender, e) =>
            {
                lb_ClientStatus.Dispatcher.Invoke(() => { lb_ClientStatus.Content = "ERROR"; });
                lb_ClientStatus.Dispatcher.Invoke(() => { lb_ClientStatus.Foreground = (SolidColorBrush)Application.Current.Resources["ADT_Error"]; });
                tb_ClientConsole.AddText(e.Message, Application.Current.Resources["ADT_Error"].ToString());
                btn_ExecClient.Dispatcher.Invoke(() => { btn_ExecClient.IsEnabled = false; });
            };

            cWS.OnClose += (sender, e) =>
            {
                lb_ClientStatus.Dispatcher.Invoke(() => { lb_ClientStatus.Content = "NOT CONNECTED"; });
                lb_ClientStatus.Dispatcher.Invoke(() => { lb_ClientStatus.Foreground = (SolidColorBrush)Application.Current.Resources["ADT_Error"]; });
                btn_ExecClient.Dispatcher.Invoke(() => { btn_ExecClient.IsEnabled = false; });
                lb_ClientInfo.Dispatcher.Invoke(() => { lb_ClientInfo.Content = "FPS: N/A | Threads: N/A"; });

                if (_connectToClient == null || _connectToClient.IsCompleted)
                {
                    _connectToClient = new Task(() =>
                    {
                        while (true)
                        {
                            tb_ClientConsole.AddText("Attempting to connect to client...", Application.Current.Resources["ADT_Error"].ToString());
                            cWS.Connect();
                            if (cWS.IsAlive) break;
                            Thread.Sleep(1000);
                        }
                    });

                    _connectToClient.Start();
                }
            };

            cWS.Connect();
        }

        private void ArcasDevTools_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                this.DragMove();
        }

        private void btn_Exit_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void btn_ExecServer_Click(object sender, RoutedEventArgs e)
        {
            string text = tb_DebugConsole.Text;
            text = text = Util.PrepareCode(text);
            sWS.SendAsync($"0:{text}", null);
        }

        private void btn_ExecClient_Click(object sender, RoutedEventArgs e)
        {
            string text = tb_DebugConsole.Text;
            text = text = Util.PrepareCode(text);
            cWS.SendAsync($"0:{text}", null);
        }

        private void btn_Settings_Click(object sender, RoutedEventArgs e)
        {
            SettingsWindow settings = new SettingsWindow();
            settings.Show();
        }
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b