/// <summary>
 /// invoke hooks having the given name
 /// </summary>
 /// <param name="context">command eval context</param>
 /// <param name="name">hook name</param>
 /// <param name="hookTriggerMode">how the hook should be tiggered</param>
 /// <param name="callBack">callback after hook</param>
 public void InvokeHooks(
     CommandEvaluationContext context,
     string name,
     HookTriggerMode hookTriggerMode,
     Action <object> callBack
     )
 => InvokeHooks <object, object>(context, name + "", null, hookTriggerMode, callBack);
 /// <summary>
 /// invoke hooks having the given name
 /// </summary>
 /// <param name="context">command eval context</param>
 /// <param name="name">hook name</param>
 /// <param name="parameter">parameter</param>
 /// <param name="hookTriggerMode">how the hook should be tiggered</param>
 /// <param name="callBack">callback after hook</param>
 public void InvokeHooks <ParameterType>(
     CommandEvaluationContext context,
     string name,
     ParameterType parameter         = default,
     HookTriggerMode hookTriggerMode = HookTriggerMode.EachTime,
     Action <object> callBack        = null
     )
 => InvokeHooks <ParameterType, object>(context, name + "", parameter, hookTriggerMode, callBack);
 /// <summary>
 /// invoke hooks having the given name
 /// </summary>
 /// <param name="context">command eval context</param>
 /// <param name="name">hook name</param>
 /// <param name="caller">caller</param>
 /// <param name="parameter">parameter</param>
 /// <param name="hookTriggerMode">how the hook should be tiggered</param>
 /// <param name="callBack">callback after hook</param>
 public AggregateHookResult <ResultType> InvokeHooks <ParameterType, ResultType>(
     CommandEvaluationContext context,
     Hooks name,
     ParameterType parameter         = default,
     HookTriggerMode hookTriggerMode = HookTriggerMode.EachTime,
     Action <object> callBack        = null
     )
 => InvokeHooks <ParameterType, ResultType>(context, name + "", parameter, hookTriggerMode, callBack);
        /// <summary>
        /// invoke hooks having the given name
        /// hooks crashes are catched here for kernel stability reasons
        /// </summary>
        /// <param name="context">context</param>
        /// <param name="name">hook name</param>
        /// <param name="parameter">parameter</param>
        /// <param name="hookTriggerMode">how the hook should be tiggered</param>
        /// <param name="callBack">called after a hook has finished exec (param is the hook method object owner)</param>
        public AggregateHookResult <ResultType> InvokeHooks <ParameterType, ResultType>(
            CommandEvaluationContext context,
            string name,
            ParameterType parameter         = default,
            HookTriggerMode hookTriggerMode = HookTriggerMode.EachTime,
            Action <object> callBack        = null
            )
        {
            if (context.ShellEnv.IsOptionSetted(ShellEnvironmentVar.debug_enableHookTrace, false))
            {
                context.Out.Echo(context.ShellEnv.Colors.Log + "[invoke hook: " + name + "](rdc) ");
            }

            AggregateHookResult <ResultType> result = null;

            if (_hooks.TryGetValue(name, out var hookList) && hookList.Any())
            {
                result = new AggregateHookResult <ResultType>();

                foreach (var hook in hookList)
                {
                    try
                    {
                        var triggerStateKey = hook.Owner.ToString() + hookTriggerMode;
                        if (_hooksTriggerState.ContainsKey(triggerStateKey) && hookTriggerMode == HookTriggerMode.FirstTimeOnly)
                        {
                            break;
                        }
                        _hooksTriggerState.AddOrReplace(triggerStateKey, hookTriggerMode);

                        if (context.ShellEnv.IsOptionSetted(ShellEnvironmentVar.debug_enableHookTrace))
                        {
                            context.Out.Echo(context.ShellEnv.Colors.Log + $"[hook '{hook.Name}' handled by: '{hook.Owner}.{hook.Method}'](rdc) ");
                        }

                        var hookResult = hook.Method.Invoke(hook.Owner, new object[] { context });
                        result.Results.Add((hook, (ResultType)hookResult));
                        callBack?.Invoke(hook.Owner);
                    }
                    catch (Exception ex)
                    {
                        var m = $"hook '{hook.Owner}.{hook.Method}' has crashed: {ex.InnerException?.Message}";
                        context.Out.Errorln(m);
                        context.Logger.LogError(m);
                    }
                }
            }

            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// invoke hooks having the given name
        /// hooks crashes are catched here for kernel stability reasons
        /// </summary>
        /// <param name="context">context</param>
        /// <param name="name">hook name</param>
        /// <param name="callBack">called after a hook has finished exec (param is the hook method object owner)</param>
        public void InvokeHooks(
            CommandEvaluationContext context,
            string name,
            HookTriggerMode hookTriggerMode = HookTriggerMode.EachTime,
            Action <object> callBack        = null
            )
        {
            if (context.ShellEnv.IsOptionSetted(ShellEnvironmentVar.debug_enableHookTrace))
            {
                context.Out.Echo(context.ShellEnv.Colors.Log + "[invoke hook: " + name + "](rdc) ");
            }

            if (_hooks.TryGetValue(name, out var hookList))
            {
                foreach (var hook in hookList)
                {
                    try
                    {
                        var triggerStateKey = hook.Owner.ToString() + hookTriggerMode;
                        if (HooksTriggerState.ContainsKey(triggerStateKey) && hookTriggerMode == HookTriggerMode.FirstTimeOnly)
                        {
                            break;
                        }
                        HooksTriggerState.AddOrReplace(triggerStateKey, hookTriggerMode);

                        if (context.ShellEnv.IsOptionSetted(ShellEnvironmentVar.debug_enableHookTrace))
                        {
                            context.Out.Echo(context.ShellEnv.Colors.Log + $"[hook '{hook.Name}' handled by: '{hook.Owner}.{hook.Method}'](rdc) ");
                        }

                        hook.Method.Invoke(hook.Owner, new object[] { context });
                        callBack?.Invoke(hook.Owner);
                    }
                    catch (Exception ex)
                    {
                        var m = $"hook '{hook.Owner}.{hook.Method}' has crashed: {ex.InnerException?.Message}";
                        context.Out.Errorln(m);
                        context.CommandLineProcessor.LogError(m);
                    }
                }
            }
        }
Beispiel #6
0
 /// <summary>
 /// invoke hooks having the given name
 /// </summary>
 /// <param name="context">command eval context</param>
 /// <param name="name">hook name</param>
 /// <param name="hookTriggerMode">how the hook should be tiggered</param>
 public void InvokeHooks(
     CommandEvaluationContext context,
     Hooks name,
     HookTriggerMode hookTriggerMode = HookTriggerMode.EachTime,
     Action <object> callBack        = null
     ) => InvokeHooks(context, name + "", hookTriggerMode, callBack);
 /// <summary>
 /// invoke hooks having the given name
 /// </summary>
 /// <param name="context">command eval context</param>
 /// <param name="name">hook name</param>
 /// <param name="hookTriggerMode">how the hook should be tiggered</param>
 public void InvokeHooks(
     CommandEvaluationContext context,
     string name,
     HookTriggerMode hookTriggerMode = HookTriggerMode.EachTime
     )
 => InvokeHooks <object, object>(context, name + "", null, hookTriggerMode);