Exemple #1
0
        public ModuleSpecification RegisterModule(
            CommandEvaluationContext context,
            Assembly assembly)
        {
            ModuleSpecification moduleSpecification;

            var moduleAttr = assembly.GetCustomAttribute <ShellModuleAttribute>();

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

            var id = assembly.GetCustomAttribute <AssemblyTitleAttribute>()?.Title ??
                     throw new Exception($"module id missing in assembly '{assembly.ManifestModule.Name}' ('AssemblyTitle' attribute missing)");
            var ver = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion ??
                      throw new Exception($"module version missing in assembly '{assembly.ManifestModule.Name}' ('AssemblyInformationalVersion' attribute missing)");
            var modKey = GetModuleLowerId(id, ver);

            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
                        )
                    ));

            // run module hook init
            ModuleHookManager.InvokeHooks(
                context,
                Hooks.ModuleInit,
                (o) =>
            {
                moduleSpecification.IsInitialized = true;
            }
                );

            return(moduleSpecification);
        }
Exemple #2
0
        public ModuleSpecification RegisterModule(
            CommandEvaluationContext context,
            Assembly assembly)
        {
            ModuleSpecification moduleSpecification;

            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());

            // run module hook init
            ModuleHookManager.InvokeHooks(
                context,
                Hooks.ModuleInit,
                HookTriggerMode.FirstTimeOnly,
                (o) =>
            {
                moduleSpecification.IsInitialized = true;
            }
                );

            return(moduleSpecification);
        }