private void OnServerHosted()
        {
            //proxied
            var pluginManager = container.Resolve <IPluginLoader>();

            pluginManager.InitAsync().GetAwaiter().GetResult();

            IEvent @event = new ImplementationReadyEvent(this);

            eventManager.Emit(this, @event);

            ICommandHandler cmdHandler = container.Resolve <ICommandHandler>();

            ChatManager.onCheckPermissions += (SteamPlayer player, string commandLine, ref bool shouldExecuteCommand,
                                               ref bool shouldList) =>
            {
                if (commandLine.StartsWith("/"))
                {
                    commandLine = commandLine.Substring(1);
                    var caller = playerManager.GetPlayer(player.playerID.steamID.ToString());
                    @event = new PreCommandExecutionEvent(caller.GetUser(), commandLine);
                    eventManager.Emit(this, @event);
                    bool success = cmdHandler.HandleCommandAsync(caller.GetUser(), commandLine, "/").GetAwaiter().GetResult();
                    if (!success)
                    {
                        caller.GetUser().SendMessageAsync("Command not found", ConsoleColor.Red).GetAwaiter().GetResult();
                    }
                    shouldList = false;
                }

                shouldExecuteCommand = false;
            };

            CommandWindow.onCommandWindowInputted += (string commandline, ref bool shouldExecuteCommand) =>
            {
                if (commandline.StartsWith("/"))
                {
                    commandline = commandline.Substring(1);
                }

                @event = new PreCommandExecutionEvent(Console, commandline);
                eventManager.Emit(this, @event);
                bool success = cmdHandler.HandleCommandAsync(Console, commandline, "").GetAwaiter().GetResult();
                if (!success)
                {
                    Console.SendMessageAsync("Command not found", ConsoleColor.Red).GetAwaiter().GetResult();
                }

                shouldExecuteCommand = false;
            };
        }
Example #2
0
        internal bool _EmitPlayerChat(object user, string text)
        {
            if (user == null || !(user is User castedUser) || !castedUser.LoggedIn)
            {
                return(true);
            }

            EcoPlayer ecoPlayer = (EcoPlayer)playerManager.GetOnlinePlayerById(castedUser.SteamId);

            if (ecoPlayer == null)
            {
                logger.LogWarning("An unknown player has chatted. Please report this to a Rocket.Eco developer!");
                return(false);
            }

            if (text.StartsWith("/", StringComparison.InvariantCulture))
            {
                logger.LogDebug($"Emitting PreCommandExecutionEvent [{ecoPlayer.Id}, {text}]");
                PreCommandExecutionEvent commandEvent = new PreCommandExecutionEvent(ecoPlayer.User, text.Remove(0, 1));
                eventManager.Emit(this, commandEvent);

                if (commandEvent.IsCancelled)
                {
                    ecoPlayer.SendErrorMessage("Execution of your command has been cancelled!");

                    return(true);
                }

                taskScheduler
                .ScheduleNextFrame(this, () =>
                {
                    bool wasHandled = true;

                    try
                    {
                        wasHandled = commandHandler.HandleCommand(ecoPlayer.User, text.Remove(0, 1), string.Empty);
                    }
                    catch (NotEnoughPermissionsException)
                    {
                        ecoPlayer.SendErrorMessage("You do not have enough permission to execute this command!");
                    }
                    catch (Exception e)
                    {
                        logger.LogError($"{ecoPlayer.Name} failed to execute the command `{text.Remove(0, 1).Split(' ')[0]}`!");
                        logger.LogError($"{e.Message}\n{e.StackTrace}");

                        ecoPlayer.SendErrorMessage("A runtime error occurred while executing this command, please contact an administrator!");

                        return;
                    }

                    if (!wasHandled)
                    {
                        ecoPlayer.SendErrorMessage("That command could not be found!");
                    }
                }, "Player Chat Event");

                return(true);
            }

            logger.LogDebug($"Emitting UserChatEvent [{ecoPlayer.Id}, {text}]");
            UserChatEvent chatEvent = new UserChatEvent(ecoPlayer.User, text)
            {
                IsCancelled = false
            };

            eventManager.Emit(this, chatEvent);

            string commandCancelled = chatEvent.IsCancelled ? "[CANCELLED] " : "";

            logger.LogInformation($"{commandCancelled}[{ecoPlayer.Id}] {ecoPlayer.Name}: {text}");

            return(chatEvent.IsCancelled);
        }
Example #3
0
        private void OnServerHosted()
        {
            ICommandHandler cmdHandler = container.Resolve <ICommandHandler>();

            ChatManager.onCheckPermissions += (SteamPlayer player, string commandLine, ref bool shouldExecuteCommand, ref bool shouldList) =>
            {
                if (commandLine.StartsWith("/"))
                {
                    commandLine = commandLine.Substring(1);
                    var caller = playerManager.GetPlayer(player.playerID.steamID.ToString());
                    var playerExecutionEvent = new PreCommandExecutionEvent(caller.User, commandLine);
                    eventManager.Emit(this, playerExecutionEvent);

                    var commandTask = Task.Run(async() =>
                    {
                        await Task.Yield();

                        bool success = await cmdHandler.HandleCommandAsync(caller.User, commandLine, "/");
                        if (!success)
                        {
                            await caller.User.SendMessageAsync("Command not found", ConsoleColor.Red);
                        }
                    });
                    commandTask.GetAwaiter().GetResult();

                    shouldList = false;
                }

                shouldExecuteCommand = false;
            };

            CommandWindow.onCommandWindowInputted += (string commandline, ref bool shouldExecuteCommand) =>
            {
                shouldExecuteCommand = false;

                if (commandline.StartsWith("/"))
                {
                    commandline = commandline.Substring(1);
                }

                var consoleExecutionEvent = new PreCommandExecutionEvent(Console, commandline);
                eventManager.Emit(this, consoleExecutionEvent);

                var commandTask = Task.Run(async() =>
                {
                    await Task.Yield();

                    bool success = await cmdHandler.HandleCommandAsync(Console, commandline, "");
                    if (!success)
                    {
                        await Console.SendMessageAsync("Command not found", ConsoleColor.Red);
                    }
                });

                commandTask.GetAwaiter().GetResult();
            };

            eventManager.Emit(this, new ImplementationReadyEvent(this));

            var pluginManager = container.Resolve <IPluginLoader>();
            var task          = Task.Run(async() =>
            {
                await Task.Yield();
                await pluginManager.InitAsync();
            });

            task.GetAwaiter().GetResult();
        }