public PluginServer()
        {
            Handlers.Add(new ChatConsoleLoggingHandler());
            RawHandlers.Add(new MessageFileLogger());

            MinecraftPluginBase.OnConnected     = OnConnection;
            MinecraftPluginBase.MessageReceived = OnMessage;
            MinecraftPluginBase.ErrorReceived   = OnError;
        }
        public void Handle(RestartChannelMethodCommand request)
        {
            var handler = HandlerInfos.FirstOrDefault(x => x.HandlerId == request.HandlerId);

            if (handler == null)
            {
                throw new HandlerNotFoundException();
            }

            var rawHandler = RawHandlers.FirstOrDefault(x => x.HandlerId == request.HandlerId);

            Task.Run(() => { rawHandler.Restart(); });
            Thread.Sleep(1000);

            HandlerInfoCollection.UpdateHandlerState(GetType().Name, request.HandlerId, "Restarted");
        }
        public void Handle(StopChannelMethodCommand request)
        {
            var handlerInfo = HandlerInfos.FirstOrDefault(x => x.HandlerId == request.HandlerId);

            if (handlerInfo == null)
            {
                throw new HandlerNotFoundException();
            }

            if (handlerInfo.State.Equals("stopped", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var handler = RawHandlers.FirstOrDefault(x => x.HandlerId == request.HandlerId);

            handler.Stop();
            HandlerInfoCollection.UpdateHandlerState(GetType().Name, request.HandlerId, "Stopped");
        }
Exemple #4
0
        public void Handle(StartChannelMethodCommand request)
        {
            var handler = HandlerInfos.FirstOrDefault(x => x.HandlerId == request.HandlerId);

            if (handler == null)
            {
                throw new HandlerNotFoundException();
            }
            if (handler.State.Equals("started", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var rawHandler = RawHandlers.FirstOrDefault(x => x.HandlerId == request.HandlerId);

            Task.Run(() => { rawHandler.Start(); });
            Thread.Sleep(1000);

            HandlerInfoCollection.UpdateHandlerState(GetType().Name, request.HandlerId, "Running");
        }
        public void Handle(LoadPluginsCommand request)
        {
            if (!Directory.Exists(request.ServerPluginsPath))
            {
                var exception = new PluginsDirectoryException("Plugins directory not found");
                _globalExceptionHandler.AddExceptionInformation(exception, _invokationMethod);
                return;
            }

            var executingAssemblyPath = Assembly.GetExecutingAssembly().FullName;
            var pluginsPath           = Path.Combine(executingAssemblyPath, _pluginsDir);

            var serverPluginsDirectory = new DirectoryInfo(request.ServerPluginsPath);

            if (serverPluginsDirectory == null)
            {
                var exception = new PluginsDirectoryException("Plugins directory not found");
                _globalExceptionHandler.AddExceptionInformation(exception, _invokationMethod);
                return;
            }

            var pluginsDirectory = new DirectoryInfo(pluginsPath);

            if (!pluginsDirectory.Exists)
            {
                Directory.CreateDirectory(pluginsPath);
            }


            var plugins = serverPluginsDirectory.GetFiles();

            try
            {
                foreach (var plugin in plugins)
                {
                    var copyPluginPath = Path.Combine(pluginsPath, plugin.Name);
                    File.Copy(plugin.FullName, copyPluginPath);

                    AppDomain.CurrentDomain.Load(copyPluginPath);
                }

                //Dispose all ChannelMethods
                RawHandlers.ForEach(handler => handler.Dispose());

                //Get WebServer instance
                var webServer = ServiceFactory.GetExportedService <IChannelWebServer>();

                //Since user is defining IChannelServer options its important that they stay the same so we need to override
                //Server instace with the Copy of it which will be created by WebServer itself on first initialization
                webServer.Server = null;
                webServer.Server = webServer.ServerCopy;
                webServer.Start();

                var failedBefore = _globalExceptionHandler.Exceptions
                                   .FirstOrDefault(x => x.InvokationMethod == _invokationMethod) != null;

                if (failedBefore)
                {
                    _globalExceptionHandler.RemoveExceptionInformation(_invokationMethod);
                }
            }
            catch (Exception ex)
            {
                _globalExceptionHandler.AddExceptionInformation(ex, _invokationMethod);
            }
        }