Esempio n. 1
0
        void InitEvents()
        {
            // Main window events
            mainWindow.OnStartServerPressed += (s, e) => StartServer();
            mainWindow.OnStopServerPressed  += (s, e) => {
                server.Stop();
                clientConnectingDialog.Close();
            };
            mainWindow.OnHide += (s, e) => {
                config.Load();
                config.MainWindowVisible = false;
                config.Save();
            };
            mainWindow.OnShow += (s, e) => {
                config.Load();
                config.MainWindowVisible = true;
                config.Save();
            };
            mainWindow.OnMoved += (s, e) => {
                config.Load();
                var window = s as MainWindow;
                config.MainWindowPosition = window.Position;
                config.Save();
            };

            // Info window events
            infoWindow.OnHide += (s, e) => {
                config.Load();
                config.InfoWindowVisible = false;
                config.Save();
            };
            infoWindow.OnShow += (s, e) => {
                config.Load();
                config.InfoWindowVisible = true;
                config.Save();
            };
            infoWindow.OnMoved += (s, e) => {
                config.Load();
                var window = s as InfoWindow;
                config.InfoWindowPosition = window.Position;
                config.Save();
            };

            // Server events
            server.OnClientRequestingConnection += (s, e) => {
                if (config.AutoAcceptConnections)
                {
                    e.Request.Allow();
                }
                else
                {
                    clientConnectingDialog.OnClientRequestingConnection(s, e);
                }
            };
        }
Esempio n. 2
0
        void InitEvents()
        {
            // Main window events
            mainWindow.OnStartServerPressed += (s, e) => {
                try {
                    e.Server.Start();
                } catch (ServerException exn) {
                    Utils.Logger.WriteLine("Server exception: " + exn.Message, Utils.Logger.Severity.Error);
                    mainWindow.Errors.Add(exn.Message);
                }
            };
            mainWindow.OnStopServerPressed += (s, e) => {
                e.Server.Stop();
                clientConnectingDialog.Close();
            };
            mainWindow.OnHide += (s, e) => {
                config.Load();
                config.Configuration.MainWindowVisible = false;
                config.Save();
            };
            mainWindow.OnShow += (s, e) => {
                config.Load();
                config.Configuration.MainWindowVisible = true;
                config.Save();
            };
            mainWindow.OnStartMoving += (s, e) => {
                config.Load();
            };
            mainWindow.OnMoved += (s, e) => {
                var window = s as MainWindow;
                config.Configuration.MainWindowPosition = window.Position.ToTuple();
            };
            mainWindow.OnFinishMoving += (s, e) => {
                config.Save();
            };

            // Info window events
            infoWindow.OnHide += (s, e) => {
                config.Load();
                config.Configuration.InfoWindowVisible = false;
                config.Save();
            };
            infoWindow.OnShow += (s, e) => {
                config.Load();
                config.Configuration.InfoWindowVisible = true;
                config.Save();
            };
            infoWindow.OnStartMoving += (s, e) => {
                config.Load();
            };
            infoWindow.OnMoved += (s, e) => {
                var window = s as InfoWindow;
                config.Configuration.InfoWindowPosition = window.Position.ToTuple();
            };
            infoWindow.OnFinishMoving += (s, e) => {
                config.Save();
            };

            // Server events
            core.OnClientRequestingConnection += (s, e) => {
                if (config.Configuration.AutoAcceptConnections)
                {
                    Utils.Logger.WriteLine("Auto-accepting client connection (" + e.Client.Address + ")");
                    e.Request.Allow();
                }
                else
                {
                    Utils.Logger.WriteLine("Asking player to accept client connection (" + e.Client.Address + ")");
                    clientConnectingDialog.OnClientRequestingConnection(s, e);
                }
            };

            // KSP events
            IsPaused = false;
            GameEvents.onGamePause.Add(() => {
                IsPaused = true;
            });
            GameEvents.onGameUnpause.Add(() => {
                IsPaused = false;
            });
        }
Esempio n. 3
0
        /// <summary>
        /// Wake the addon. Creates the server instance and UI.
        /// </summary>
        public void Awake()
        {
            if (!ServicesChecker.OK)
            {
                return;
            }

            Init();

            KRPCServer.Context.SetGameScene(KSPAddonImproved.CurrentGameScene.ToGameScene());
            Logger.WriteLine("Game scene switched to " + KRPCServer.Context.GameScene);

            GUILayoutExtensions.Init(gameObject);

            server.GetUniversalTime = Planetarium.GetUniversalTime;

            // Disconnect client dialog
            clientDisconnectDialog = gameObject.AddComponent <ClientDisconnectDialog> ();

            // Create info window
            infoWindow          = gameObject.AddComponent <InfoWindow> ();
            infoWindow.Server   = server;
            infoWindow.Closable = true;
            infoWindow.Visible  = config.InfoWindowVisible;
            infoWindow.Position = config.InfoWindowPosition;

            // Create main window
            mainWindow          = gameObject.AddComponent <MainWindow> ();
            mainWindow.Config   = config;
            mainWindow.Server   = server;
            mainWindow.Visible  = config.MainWindowVisible;
            mainWindow.Position = config.MainWindowPosition;
            mainWindow.ClientDisconnectDialog = clientDisconnectDialog;
            mainWindow.InfoWindow             = infoWindow;

            // Create new connection dialog
            clientConnectingDialog = gameObject.AddComponent <ClientConnectingDialog> ();

            // Main window events
            mainWindow.OnStartServerPressed += (s, e) => StartServer();
            mainWindow.OnStopServerPressed  += (s, e) => {
                server.Stop();
                clientConnectingDialog.Close();
            };
            mainWindow.OnHide += (s, e) => {
                config.Load();
                config.MainWindowVisible = false;
                config.Save();
            };
            mainWindow.OnShow += (s, e) => {
                config.Load();
                config.MainWindowVisible = true;
                config.Save();
            };
            mainWindow.OnMoved += (s, e) => {
                config.Load();
                var window = s as MainWindow;
                config.MainWindowPosition = window.Position;
                config.Save();
            };

            // Info window events
            infoWindow.OnHide += (s, e) => {
                config.Load();
                config.InfoWindowVisible = false;
                config.Save();
            };
            infoWindow.OnShow += (s, e) => {
                config.Load();
                config.InfoWindowVisible = true;
                config.Save();
            };
            infoWindow.OnMoved += (s, e) => {
                config.Load();
                var window = s as InfoWindow;
                config.InfoWindowPosition = window.Position;
                config.Save();
            };

            // Server events
            server.OnClientRequestingConnection += (s, e) => {
                if (config.AutoAcceptConnections)
                {
                    e.Request.Allow();
                }
                else
                {
                    clientConnectingDialog.OnClientRequestingConnection(s, e);
                }
            };


            // Add a button to the applauncher
            mainWindow.Closable = true;
            textureOnline       = GameDatabase.Instance.GetTexture("kRPC/icons/applauncher-online", false);
            textureOffline      = GameDatabase.Instance.GetTexture("kRPC/icons/applauncher-offline", false);
            GameEvents.onGUIApplicationLauncherReady.Add(OnGUIApplicationLauncherReady);
            GameEvents.onGUIApplicationLauncherDestroyed.Add(OnGUIApplicationLauncherDestroyed);
            server.OnStarted += (s, e) => {
                if (applauncherButton != null)
                {
                    applauncherButton.SetTexture(textureOnline);
                }
            };
            server.OnStopped += (s, e) => {
                if (applauncherButton != null)
                {
                    applauncherButton.SetTexture(textureOffline);
                }
            };
        }