/// <summary> Compares the command filters in order of precendence </summary>
        private int PrecedenceOrder(ICommandFilter x, ICommandFilter y)
        {
            int posX = _filterPrecedence.IndexOfAny(x.Keys);

            posX = posX >= 0 ? posX : int.MaxValue;
            int posY = _filterPrecedence.IndexOfAny(y.Keys);

            posY = posY >= 0 ? posY : int.MaxValue;
            return(posX.CompareTo(posY));
        }
Example #2
0
 void Update()
 {
     if (canvasGroup.alpha == 0)
     {
         canvasGroup.blocksRaycasts = false;
     }
     else
     {
         canvasGroup.blocksRaycasts = true;
     }
     if (activeScripts == null)
     {
         return;
     }
     if (activeDialog == null)
     {
         delayTimer += Time.deltaTime;
         foreach (Transform child in activeScripts)
         {
             ScriptedDialog dialog = child.GetComponent <ScriptedDialog>();
             if (dialog.IsTriggered && delayTimer > dialog.minDelay)
             {
                 ActiveDialog = dialog;
                 break;
             }
         }
         if (canvasGroup.alpha > 0)
         {
             canvasGroup.alpha -= Time.deltaTime / fadeTime;
         }
         helpArrow.Target = null;
     }
     else
     {
         if (canvasGroup.alpha < 1)
         {
             canvasGroup.alpha += Time.deltaTime / fadeTime;
         }
         if (activeDialog.IsSatisfied)
         {
             Destroy(activeDialog.gameObject);
             CommandManager.Instance.commandFilter = null;
             if (activeDialog.nextDialogPrefab != null)
             {
                 GameObject go = Instantiate(activeDialog.nextDialogPrefab);
                 go.transform.SetParent(activeScripts, false);
                 ICommandFilter commandFilter = go.GetComponent <ICommandFilter>();
                 CommandManager.Instance.commandFilter = commandFilter;
             }
             delayTimer = 0;
         }
         helpArrow.Target = activeDialog.PointerTarget;
     }
 }
Example #3
0
        private bool ExecuteCommand(MethodInfo method, User user, Chat chat, string[] args, Message messageData, out object result)
        {
            Core.Log.Info($"Execute command {method}");

            result = new object();
            CommandContext context = new CommandContext(Core, user, chat, messageData);

            var parameters = method.GetParameters();

            int addLenght = 0;

            if (parameters.Length > 0 && typeof(CommandContext).IsAssignableFrom(parameters[0].ParameterType))
            {
                addLenght = 1;
            }

            object[] objectArgs = new object[parameters.Length];

            try
            {
                int i = 0;
                for (int k = 0; k < parameters.Length; k++)
                {
                    var parameter = parameters[k];
                    if (k == 0 && addLenght == 1)
                    {
                        if (typeof(CommandContext).IsAssignableFrom(parameter.ParameterType))
                        {
                            objectArgs[k] = context;
                            continue;
                        }
                        Core.Log.Warn(chat, $"Command method {method.Name} missing Player as first argument.");
                        return(false);
                    }

                    bool isStringParam = IsParams(parameter) && parameter.ParameterType == typeof(string[]);

                    if ((parameter.IsOptional || isStringParam) && args.Length <= i)
                    {
                        if (isStringParam)
                        {
                            objectArgs[k] = new string[0];
                        }
                        else
                        {
                            objectArgs[k] = parameter.DefaultValue;
                        }
                        continue;
                    }

                    if (args.Length < k)
                    {
                        Core.Log.Error(chat, $"No math {k} arguments");
                        return(false);
                    }

                    if (typeof(IParameterSerializer).IsAssignableFrom(parameter.ParameterType))
                    {
                        var ctor = parameter.ParameterType.GetConstructor(Type.EmptyTypes);
                        IParameterSerializer defaultValue = ctor.Invoke(null) as IParameterSerializer;
                        defaultValue?.Deserialize(user, args[i++]);

                        objectArgs[k] = defaultValue;

                        continue;
                    }

                    if (parameter.ParameterType == typeof(User))
                    {
                        long   id;
                        string _id = args[i++].Split(' ', '|').First();
                        if (_id.Length < 4 || !long.TryParse(_id.Substring(3), out id))
                        {
                            return(false);
                        }
                        objectArgs[k] = new User(user.VkApi, id);
                        continue;
                    }

                    if (parameter.ParameterType == typeof(string))
                    {
                        objectArgs[k] = args[i++];
                        continue;
                    }
                    if (parameter.ParameterType == typeof(byte))
                    {
                        byte value;
                        if (!byte.TryParse(args[i++], out value))
                        {
                            return(false);
                        }
                        objectArgs[k] = value;
                        continue;
                    }
                    if (parameter.ParameterType == typeof(short))
                    {
                        short value;
                        if (!short.TryParse(args[i++], out value))
                        {
                            return(false);
                        }
                        objectArgs[k] = value;
                        continue;
                    }
                    if (parameter.ParameterType == typeof(int))
                    {
                        int value;
                        if (!int.TryParse(args[i++], out value))
                        {
                            return(false);
                        }
                        objectArgs[k] = value;
                        continue;
                    }
                    if (parameter.ParameterType == typeof(long))
                    {
                        long value;
                        if (!long.TryParse(args[i++], out value))
                        {
                            return(false);
                        }
                        objectArgs[k] = value;
                        continue;
                    }
                    if (parameter.ParameterType == typeof(bool))
                    {
                        bool value;
                        if (!bool.TryParse(args[i++], out value))
                        {
                            return(false);
                        }
                        objectArgs[k] = value;
                        continue;
                    }
                    if (parameter.ParameterType == typeof(float))
                    {
                        float value;
                        if (!float.TryParse(args[i++], out value))
                        {
                            return(false);
                        }
                        objectArgs[k] = value;
                        continue;
                    }
                    if (parameter.ParameterType == typeof(double))
                    {
                        double value;
                        if (!double.TryParse(args[i++], out value))
                        {
                            return(false);
                        }
                        objectArgs[k] = value;
                        continue;
                    }
                    if (parameter.ParameterType.IsEnum)
                    {
                        string val = args[i++];
                        if (!Enum.TryParse(parameter.ParameterType, val, true, out object value) || value as Enum == null)
                        {
                            Core.Log.Warn($"Could not convert to valid enum value: {val}");
                            return(false);
                        }

                        objectArgs[k] = value;
                        continue;
                    }

                    if (isStringParam)
                    {
                        List <string> strings = new List <string>();
                        for (; i < args.Length; i++)
                        {
                            strings.Add(args[i]);
                        }
                        objectArgs[k] = strings.ToArray();
                        continue;
                    }

                    return(false);
                }

                if (i < args.Length)
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                //if (Log.IsDebugEnabled)
                //{
                //	Log.Error("Trying to execute command overload", e);
                //}
                //chat.SendMessage(e);
                Core.Log.Error(chat, e.ToString());

                return(false);
            }

            try
            {
                object pluginInstance = _plugins.FirstOrDefault(plugin => method.DeclaringType.IsInstanceOfType(plugin)) ?? method.DeclaringType;
                if (pluginInstance == null)
                {
                    return(false);
                }

                ICommandFilter filter = pluginInstance as ICommandFilter;
                if (filter != null)
                {
                    filter.OnCommandExecuting(user);
                }

                if (method.IsStatic)
                {
                    result = method.Invoke(null, objectArgs);
                }
                else
                {
                    if (method.DeclaringType == null)
                    {
                        return(false);
                    }

                    Plugin.CurrentContext = context; // Setting thread local for call
                    result = method.Invoke(pluginInstance, objectArgs);
                    Plugin.CurrentContext = null;    // Done with thread local, we using pool to make sure it's reset.
                }

                if (filter != null)
                {
                    filter.OnCommandExecuted();
                }

                return(true);
            }
            catch (Exception e)
            {
                Core.Log.Error(chat, e.ToString());
                //Log.Error($"Error while executing command {method}", e);
                //chat.SendMessage(e);
            }

            return(false);
        }
Example #4
0
        private object ExecuteCommand(MethodInfo method, Player player, string[] args)
        {
            Log.Info($"Execute command {method}");

            var parameters = method.GetParameters();

            int addLenght = 0;

            if (parameters.Length > 0 && typeof(Player).IsAssignableFrom(parameters[0].ParameterType))
            {
                addLenght = 1;
            }

            object[] objectArgs = new object[parameters.Length];

            for (int k = 0; k < parameters.Length; k++)
            {
                var parameter = parameters[k];
                int i         = k - addLenght;
                if (k == 0 && addLenght == 1)
                {
                    if (typeof(Player).IsAssignableFrom(parameter.ParameterType))
                    {
                        objectArgs[k] = player;
                        continue;
                    }
                    Log.WarnFormat("Command method {0} missing Player as first argument.", method.Name);
                    return(null);
                }

                if (parameter.IsOptional && args.Length <= i)
                {
                    objectArgs[k] = parameter.DefaultValue;
                    continue;
                }

                if (typeof(IParameterSerializer).IsAssignableFrom(parameter.ParameterType))
                {
                    var ctor = parameter.ParameterType.GetConstructor(Type.EmptyTypes);
                    IParameterSerializer defaultValue = ctor.Invoke(null) as IParameterSerializer;
                    defaultValue?.Deserialize(player, args[i]);

                    objectArgs[k] = defaultValue;

                    continue;
                }

                if (parameter.ParameterType.BaseType == typeof(EnumBase))
                {
                    var      ctor     = parameter.ParameterType.GetConstructor(Type.EmptyTypes);
                    EnumBase instance = (EnumBase)ctor.Invoke(null);
                    instance.Value = args[i];
                    objectArgs[k]  = instance;
                    continue;
                }

                if (parameter.ParameterType == typeof(Target))
                {
                    var target = JsonConvert.DeserializeObject <Target>(args[i]);
                    target        = FillTargets(player, player.Level, target);
                    objectArgs[k] = target;
                    continue;
                }

                if (parameter.ParameterType == typeof(BlockPos))
                {
                    var blockpos = JsonConvert.DeserializeObject <BlockPos>(args[i]);
                    objectArgs[k] = blockpos;
                    continue;
                }

                if (parameter.ParameterType == typeof(string))
                {
                    objectArgs[k] = args[i];
                    continue;
                }
                if (parameter.ParameterType == typeof(byte))
                {
                    byte value;
                    if (!byte.TryParse(args[i], out value))
                    {
                        return(null);
                    }
                    objectArgs[k] = value;
                    continue;
                }
                if (parameter.ParameterType == typeof(short))
                {
                    short value;
                    if (!short.TryParse(args[i], out value))
                    {
                        return(null);
                    }
                    objectArgs[k] = value;
                    continue;
                }
                if (parameter.ParameterType == typeof(int))
                {
                    int value;
                    if (!int.TryParse(args[i], out value))
                    {
                        return(null);
                    }
                    objectArgs[k] = value;
                    continue;
                }
                if (parameter.ParameterType == typeof(bool))
                {
                    bool value;
                    if (!bool.TryParse(args[i], out value))
                    {
                        return(null);
                    }
                    objectArgs[k] = value;
                    continue;
                }
                if (parameter.ParameterType == typeof(float))
                {
                    float value;
                    if (!float.TryParse(args[i], out value))
                    {
                        return(null);
                    }
                    objectArgs[k] = value;
                    continue;
                }
                if (parameter.ParameterType == typeof(double))
                {
                    double value;
                    if (!double.TryParse(args[i], out value))
                    {
                        return(null);
                    }
                    objectArgs[k] = value;
                    continue;
                }
                if (parameter.ParameterType.IsEnum)
                {
                    Enum value = Enum.Parse(parameter.ParameterType, args[i], true) as Enum;
                    if (value == null)
                    {
                        Log.Error($"Could not convert to valid enum value: {args[i]}");
                        continue;
                    }

                    objectArgs[k] = value;
                    continue;
                }

                if (IsParams(parameter) && parameter.ParameterType == typeof(string[]))
                {
                    List <string> strings = new List <string>();
                    for (int j = i; j < args.Length; j++)
                    {
                        strings.Add(args[j]);
                    }
                    objectArgs[k] = strings.ToArray();
                    continue;
                }

                return(null);
            }

            object result = null;

            try
            {
                object pluginInstance = _plugins.FirstOrDefault(plugin => plugin.GetType() == method.DeclaringType);
                if (pluginInstance == null)
                {
                    return(null);
                }

                ICommandFilter filter = pluginInstance as ICommandFilter;
                if (filter != null)
                {
                    filter.OnCommandExecuting(player);
                }

                if (method.IsStatic)
                {
                    result = method.Invoke(null, objectArgs);
                }
                else
                {
                    if (method.DeclaringType == null)
                    {
                        return(false);
                    }

                    Plugin.CurrentPlayer = player;                   // Setting thread local for call
                    result = method.Invoke(pluginInstance, objectArgs);
                    Plugin.CurrentPlayer = null;                     // Done with thread local, we using pool to make sure it's reset.
                }

                if (filter != null)
                {
                    filter.OnCommandExecuted();
                }
            }
            catch (Exception e)
            {
                Log.Error($"Error while executing command {method}", e);
            }
            return(result);
        }
Example #5
0
        /// <summary>
        /// Adds the specified command filter implementation to the manager,
        /// with the specified explicit metadata.
        /// </summary>
        /// <param name="filter">The command filter instance, which does not need to
        /// be annotated with the <see cref="CommandFilterAttribute"/> attribute since
        /// it's provided explicitly.</param>
        /// <param name="metadata">Explicit metadata to use for the command filter,
        /// instead of reflecting the <see cref="CommandFilterAttribute"/>.</param>
        public void AddFilter(ICommandFilter filter, CommandFilterAttribute metadata)
        {
            Guard.NotNull(() => filter, filter);
            Guard.NotNull(() => metadata, metadata);

            var commandPackageGuid = new Guid(metadata.PackageId);
            var commandPackage = default(IVsPackage);
            vsShell.IsPackageLoaded(ref commandPackageGuid, out commandPackage);

            if (commandPackage == null)
                ErrorHandler.ThrowOnFailure(vsShell.LoadPackage(ref commandPackageGuid, out commandPackage));

            var serviceProvider = commandPackage as IServiceProvider;
            if (serviceProvider == null)
            {
                tracer.Error(Strings.CommandManager.CommandPackageNotServiceProvider(commandPackageGuid));
                return;
            }

            var mcs = serviceProvider.GetService<IMenuCommandService>();
            if (mcs == null)
            {
                tracer.Error(Strings.CommandManager.NoMenuCommandService(commandPackageGuid));
                return;
            }

            var groupId = new Guid(metadata.GroupId);
            var command = mcs.FindCommand(new CommandID(groupId, metadata.CommandId));
            if (command == null)
            {
                tracer.Error(Strings.CommandManager.CommandNotFound(commandPackageGuid, groupId, metadata.CommandId));
                return;
            }

            // \o/: for some reason this cast never works on VS2012, even with the proper assembly references :(.
            // So we resort to dynamic.
            // var command =  as OleMenuCommand;
            dynamic dynCommand = command.AsDynamicReflection();
            try
            {
                dynCommand.add_BeforeQueryStatus(new EventHandler((sender, args) =>
                {
                    try
                    {
                        filter.QueryStatus(new OleMenuCommandAdapter((OleMenuCommand)sender));
                    }
                    catch (Exception e)
                    {
                        tracer.Error(Strings.CommandManager.FilterFailed(filter, e));
                    }
                }));
            }
            catch (RuntimeBinderException)
            {
                // The command may not be an OleMenuCommand and therefore it wouldn't have the BeforeQueryStatus.
                tracer.Error(Strings.CommandManager.CommandNotOle(commandPackageGuid, metadata.GroupId, metadata.CommandId));
            }
        }
		public FilterChainItem(ICommandInterpreter ci, ICommandFilter filter, ICommandChain next)
		{
			_ci = Check.NotNull(ci);
			_filter = Check.NotNull(filter);
			_next = Check.NotNull(next);
		}
Example #7
0
 public FilterChainItem(ICommandInterpreter ci, ICommandFilter filter, ICommandChain next)
 {
     _ci     = Check.NotNull(ci);
     _filter = Check.NotNull(filter);
     _next   = Check.NotNull(next);
 }
 /// <summary>
 /// Adds a command 'filter' that is called for every command invoked enabling custom processing
 /// of arguments and pre/post processing.
 /// </summary>
 public void AddFilter(ICommandFilter filter)
 {
     _filters.Remove(filter);
     _filters.Add(filter);
     _head = null;
 }
Example #9
0
 public FilterChainItem(ICommandInterpreter ci, ICommandFilter filter, ICommandChain next)
 {
     _ci     = ci;
     _filter = filter;
     _next   = next;
 }
Example #10
0
 public FilterChainItem(ICommandInterpreter ci, ICommandFilter filter, ICommandChain next)
 {
     _ci = ci;
     _filter = filter;
     _next = next;
 }
Example #11
0
        /// <summary>
        /// Adds the specified command filter implementation to the manager,
        /// with the specified explicit metadata.
        /// </summary>
        /// <param name="filter">The command filter instance, which does not need to
        /// be annotated with the <see cref="CommandFilterAttribute"/> attribute since
        /// it's provided explicitly.</param>
        /// <param name="metadata">Explicit metadata to use for the command filter,
        /// instead of reflecting the <see cref="CommandFilterAttribute"/>.</param>
        public void AddFilter(ICommandFilter filter, CommandFilterAttribute metadata)
        {
            Guard.NotNull(() => filter, filter);
            Guard.NotNull(() => metadata, metadata);

            var commandPackageGuid = new Guid(metadata.PackageId);
            var commandPackage     = default(IVsPackage);

            vsShell.IsPackageLoaded(ref commandPackageGuid, out commandPackage);

            if (commandPackage == null)
            {
                ErrorHandler.ThrowOnFailure(vsShell.LoadPackage(ref commandPackageGuid, out commandPackage));
            }

            var serviceProvider = commandPackage as IServiceProvider;

            if (serviceProvider == null)
            {
                tracer.Error(Strings.CommandManager.CommandPackageNotServiceProvider(commandPackageGuid));
                return;
            }

            var mcs = serviceProvider.GetService <IMenuCommandService>();

            if (mcs == null)
            {
                tracer.Error(Strings.CommandManager.NoMenuCommandService(commandPackageGuid));
                return;
            }

            var groupId = new Guid(metadata.GroupId);
            var command = mcs.FindCommand(new CommandID(groupId, metadata.CommandId));

            if (command == null)
            {
                tracer.Error(Strings.CommandManager.CommandNotFound(commandPackageGuid, groupId, metadata.CommandId));
                return;
            }

            // \o/: for some reason this cast never works on VS2012, even with the proper assembly references :(.
            // So we resort to dynamic.
            // var command =  as OleMenuCommand;
            dynamic dynCommand = command.AsDynamicReflection();

            try
            {
                dynCommand.add_BeforeQueryStatus(new EventHandler((sender, args) =>
                {
                    try
                    {
                        filter.QueryStatus(new OleMenuCommandAdapter((OleMenuCommand)sender));
                    }
                    catch (Exception e)
                    {
                        tracer.Error(Strings.CommandManager.FilterFailed(filter, e));
                    }
                }));
            }
            catch (RuntimeBinderException)
            {
                // The command may not be an OleMenuCommand and therefore it wouldn't have the BeforeQueryStatus.
                tracer.Error(Strings.CommandManager.CommandNotOle(commandPackageGuid, metadata.GroupId, metadata.CommandId));
            }
        }