Example #1
0
        public ModuleSpecification UnregisterModule(
            CommandEvaluationContext context,
            string moduleName)
        {
            moduleName = moduleName?.ToLower() ?? throw new ArgumentNullException(nameof(moduleName));
            var moduleSpecification = GetModuleByLowerPackageId(moduleName);

            if (moduleSpecification == null)
            {
                return(null);
            }
            var r = ModuleCommandManager.UnregisterModuleCommands(context, moduleName);

            _modules.Remove(moduleSpecification.Key);
            return(r);
        }
Example #2
0
 public ModuleManager(SyntaxAnalyser syntaxAnalyser)
 {
     _syntaxAnalyzer      = syntaxAnalyser;
     ModuleCommandManager = new ModuleCommandManager(_syntaxAnalyzer, _modules);
     ModuleHookManager    = new ModuleHookManager(_modules);
 }
Example #3
0
        public ModuleSpecification RegisterModule(
            CommandEvaluationContext context,
            Assembly assembly)
        {
            ModuleSpecification moduleSpecification = null;

            try
            {
                var moduleAttr = assembly.GetCustomAttribute <ShellModuleAttribute>();
                if (moduleAttr == null)
                {
                    context.Errorln($"assembly is not a shell module: '{assembly.FullName}'");
                    return(ModuleSpecification.ModuleSpecificationNotDefined);
                }

                // id is the name of the assembly (/!\ should not fit nuget packet id)

                var modKey = ModuleKey(assembly, out var id, out var ver);
                var assKey = AssemblyKey(assembly);
                if (_loadedModules.Contains(assKey))
                {
                    throw new Exception($"assembly already loaded: '{assKey}'");
                }

                if (_modules.ContainsKey(modKey))
                {
                    context.Errorln($"module already registered: {modKey} (path={assembly.FullName})");
                    return(ModuleSpecification.ModuleSpecificationNotDefined);
                }

                var typesCount  = 0;
                var comTotCount = 0;
                var hooksCount  = 0;

                foreach (var type in assembly.GetTypes())
                {
                    // register hooks

                    var hookAttr = type.GetCustomAttribute <HooksAttribute>();
                    if (hookAttr != null)
                    {
                        // module,class owns hooks
                        foreach (var mi in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
                        {
                            var hook = mi.GetCustomAttribute <HookAttribute>();
                            if (hook != null)
                            {
                                ModuleHookManager.RegisterHook(context, hook.HookName, mi);
                                hooksCount++;
                            }
                        }
                    }

                    // register commands

                    var comsAttr = type.GetCustomAttribute <CommandsAttribute>();

                    var comCount = 0;
                    if (comsAttr != null && type.GetInterface(typeof(ICommandsDeclaringType).FullName) != null)
                    {
                        comCount = ModuleCommandManager.RegisterCommandClass(context, type, false);
                    }
                    if (comCount > 0)
                    {
                        typesCount++;
                    }
                    comTotCount += comCount;
                }

                // register module

                var descAttr    = assembly.GetCustomAttribute <AssemblyDescriptionAttribute>();
                var description = (descAttr != null) ? descAttr.Description : "";
                _modules.Add(
                    modKey,
                    moduleSpecification = new ModuleSpecification(
                        modKey,
                        Path.GetFileNameWithoutExtension(assembly.Location),
                        description,
                        assembly,
                        new ModuleInfo(
                            typesCount,
                            comTotCount,
                            hooksCount
                            )
                        ));
                _loadedModules.Add(AssemblyKey(assembly));
                _loadedAssemblies.Add(assembly.Location.ToLower(), assembly);

                // run module hook init
                ModuleHookManager.InvokeHooks(
                    context,
                    Hooks.ModuleInit,
                    HookTriggerMode.FirstTimeOnly,
                    (o) =>
                {
                    moduleSpecification.IsInitialized = true;
                }
                    );
            }
            catch (Exception ex)
            {
                throw new Exception($"register module assembly '{assembly.FullName}' failed due to error: '{ex.Message}'", ex);
            }

            return(moduleSpecification);
        }