Exemple #1
0
        private static List <RocketCommandManager.RegisteredRocketCommand> GetRocketCommands()
        {
            var commandsField  = ReflectUtil.GetField(R.Commands.GetType(), "commands");
            var rocketCommands = (List <RocketCommandManager.RegisteredRocketCommand>)commandsField.GetValue(R.Commands);

            return(rocketCommands);
        }
Exemple #2
0
        internal CommandManager()
        {
            CommandMap = new Dictionary <string, ICommand>();
            var commandsField = ReflectUtil.GetField(R.Commands.GetType(), "commands");

            _rocketCommands = (List <RocketCommandManager.RegisteredRocketCommand>)commandsField.GetValue(R.Commands);
        }
Exemple #3
0
        private void UnregisterRocketCommands(bool silent = false)
        {
            var commandsField   = ReflectUtil.GetField(R.Commands.GetType(), "commands");
            var _rocketCommands = (List <RocketCommandManager.RegisteredRocketCommand>)commandsField.GetValue(R.Commands);

            if (_rocketCommands == null)
            {
                Logger.LogError("Could not unregister Rocket commands.");
                return;
            }

            /* All names & aliases of uEssentials command */
            var essCommands = _rocketCommands
                              .Where(c => c.Command is CommandAdapter)
                              .Select(c => c.Name.ToLowerInvariant())
                              .ToList();

            _rocketCommands.RemoveAll(c => {
                if (essCommands.Contains(c.Name.ToLowerInvariant()) && !(c.Command is CommandAdapter))
                {
                    if (!silent)
                    {
                        Logger.LogInfo($"Overriding Rocket command ({c.Name.ToLowerInvariant()})");
                    }
                    return(true);
                }
                return(false);
            });
        }
        public static void ApplyFieldDeclaration(FieldDeclaration declaration, object target, bool throwExceptionOnMissingField)
        {
            MethodInfo setterMethod = ReflectUtil.GetSetter(declaration.Name, target.GetType(), declaration.Value.GetType());

            if (setterMethod != null)
            {
                try
                {
                    setterMethod.Invoke(target, new object[] { declaration.Value });
                }
                catch (System.ArgumentException e)
                {
                    throw new ActivitiException("Error while invoking '" + declaration.Name + "' on class " + target.GetType().FullName, e);
                }
                catch (Exception e)
                {
                    throw new ActivitiException("Illegal acces when calling '" + declaration.Name + "' on class " + target.GetType().FullName, e);
                }
                //catch (InvocationTargetException e)
                //{
                //    throw new ActivitiException("Exception while invoking '" + declaration.Name + "' on class " + target.GetType().FullName, e);
                //}
            }
            else
            {
                FieldInfo field = ReflectUtil.GetField(declaration.Name, target);
                if (field == null)
                {
                    if (throwExceptionOnMissingField)
                    {
                        throw new ActivitiIllegalArgumentException("Field definition uses unexisting field '" + declaration.Name + "' on class " + target.GetType().FullName);
                    }
                    else
                    {
                        return;
                    }
                }

                // Check if the delegate field's type is correct
                if (!FieldTypeCompatible(declaration, field))
                {
                    throw new ActivitiIllegalArgumentException("Incompatible type set on field declaration '" + declaration.Name + "' for class " + target.GetType().FullName + ". Declared value has type " + declaration.Value.GetType().FullName + ", while expecting " + field.DeclaringType.Name);
                }
                ReflectUtil.SetField(field, target, declaration.Value);
            }
        }
        private void OverrideCommands()
        {
            R.Plugins.OnPluginsLoaded -= OverrideCommands;

            var commandsField  = ReflectUtil.GetField(R.Commands.GetType(), "commands");
            var rocketCommands = (List <RocketCommandManager.RegisteredRocketCommand>)commandsField.GetValue(R.Commands);
            var essCommands    = new HashSet <string>(CommandManager.Commands.Select(c => c.Name.ToLowerInvariant()));

            // Used to check which commands are not "owned' by uEssentials
            var mappedRocketCommands = new Dictionary <string, RocketCommandManager.RegisteredRocketCommand>();

            if (rocketCommands == null)
            {
                Logger.LogError("Could not override Rocket commands.");
                return;
            }

            rocketCommands.RemoveAll(command => {
                var name    = command.Name.ToLowerInvariant();
                var wrapper = command.Command;

                // Override default commands from Rocket and Unturned
                if (wrapper.GetType().FullName.StartsWith("Rocket.Unturned.Commands") && essCommands.Contains(name))
                {
                    Logger.LogInfo($"Overriding Unturned/Rocket command ({command.Name.ToLowerInvariant()})");
                    return(true);
                }

                // It will override a command from another plugin only if specified in the config.json
                if (Config.CommandsToOverride.Contains(name) && !(command.Command is CommandAdapter))
                {
                    var pluginName = command.Command.GetType().Assembly.GetName().Name;
                    Logger.LogInfo($"Overriding command \"{command.Name.ToLowerInvariant()}\" from the plugin: {pluginName}");
                    return(true);
                }

                if (!mappedRocketCommands.ContainsKey(name))
                {
                    mappedRocketCommands.Add(name, command);
                }
                return(false);
            });

            // Check commands that are not "owned" by uEssentials.
            var commandsNotOwnedByEss = CommandManager.Commands
                                        .Select(command => {
                var rocketCommand = mappedRocketCommands[command.Name.ToLowerInvariant()];
                if (rocketCommand.Command is CommandAdapter)
                {
                    return(null);
                }
                return(rocketCommand);
            })
                                        .Where(command => command != null)
                                        .ToList();

            if (commandsNotOwnedByEss.Count == 0)
            {
                return;
            }

            lock (Console.Out) {
                Logger.LogWarning("The following commands cannot be used by uEssentials because them already exists in another plugin.");
                commandsNotOwnedByEss.ForEach(command => {
                    var pluginName = command.Command.GetType().Assembly.GetName().Name;
                    Logger.LogWarning($" The command \"{command.Name}\" is owned by the plugin: {pluginName}");
                });
                Logger.LogWarning("If you want to use a command from uEssentials instead of another plugin, add it in \"CommandsToOverride\" in the config.json");
            }
        }