public ManifestInfo(ModuleAttribute moduleInfo) { base(); this._moduleInfo = moduleInfo; this._version = new Lazy <System.Version>(new Func <System.Version>(this.ParseVersion)); return; }
private void RegisterModule(Type moduleType, ModuleAttribute attribute, HelpFileGenerator helpFileGenerator) { if (attribute.moduleName == string.Empty || attribute.moduleName == null) { throw new ArgumentException("Module name can not be null or an empty string."); } if (registry.ContainsKey(attribute.moduleName)) { throw new ArgumentException($"Module registry already contains a module with the name \"{attribute.moduleName}\"."); } string logString = $"Registering Module \"{attribute.moduleName}\""; if (attribute.Version != null && !attribute.Version.Equals(string.Empty)) { logString += $" version \"{attribute.Version}\""; } TamaChan.Instance.Logger.LogInfo(logString + "..."); TamaChanModule module = Activator.CreateInstance(moduleType) as TamaChanModule; TamaChan.Instance.CommandRegistry.RegisterModuleCommands(module, helpFileGenerator); registry.Add(attribute.moduleName, module); RegisterModuleEventHandling(module); module.RegistryName = attribute.moduleName; module.Initialize(); TamaChan.Instance.Logger.LogInfo($"Module \"{attribute.moduleName}\" registered."); }
void LoadAssembly(System.Reflection.Assembly assembly) { if (assembly != null) { System.Type [] types = assembly.GetTypes(); foreach (System.Type T in types) { ModuleAttribute ma = Attribute.GetCustomAttribute(T, typeof(ModuleAttribute)) as ModuleAttribute; if (!Object.Equals(null, ma)) { Module module = new Module(); module.Assembly = MetaHelper.GetAssemblyString(T); module.Type = MetaHelper.GetTypeString(T); module.Developer = MetaHelper.GetDeveloperString(T); module.Version = MetaHelper.GetVersionString(T); module.Name = ma.Name; module.Guid = ma.Guid.ToString().ToUpper(); module.Description = ma.Description; module.Attributes = (int)GoComType.SilverUI; WorkflowAddInAttribute wfa = Attribute.GetCustomAttribute(T, typeof(WorkflowAddInAttribute)) as WorkflowAddInAttribute; if (!Object.Equals(null, wfa)) { module.Attributes |= 0x1000; } this.modules.Add(module); } } } }
public void ModuleAttributeIsAvailable() { ModuleAttribute attr = new ModuleAttribute("SomeModule"); Assert.IsNotNull(attr); Assert.IsTrue(attr is Attribute); }
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. protected TheGodfatherModule() #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. { var moduleAttr = ModuleAttribute.AttachedTo(this.GetType()); this.ModuleColor = moduleAttr?.Module.ToDiscordColor() ?? DiscordColor.Green; }
public ManifestInfo ( ModuleAttribute moduleInfo ) { _moduleInfo = moduleInfo; _version = new Lazy <Version>(ParseVersion); }
public ModuleInfo(ModuleAttribute moduleAttribute) { _moduleAttribute = moduleAttribute; Name = _moduleAttribute.Name; ModuleType = _moduleAttribute.ModuleType; Author = _moduleAttribute.Author; Tags = _moduleAttribute.Tags; Version = _moduleAttribute.Version; }
public ReflectionRuntimeModule(Type moduleType, ModuleAttribute attribute) : base(moduleType, attribute.Injects.Select(Key.GetMemberKey).ToArray(), attribute.IncludedModules, attribute.IsComplete, attribute.IsLibrary, attribute.IsOverride) { }
public void CanGetAndSetProperties() { var moduleAttribute = new ModuleAttribute(); moduleAttribute.ModuleName = "Test"; moduleAttribute.OnDemand = true; Assert.AreEqual("Test", moduleAttribute.ModuleName); Assert.AreEqual(true, moduleAttribute.OnDemand); }
public ManifestInfo ( ModuleAttribute moduleInfo ) { _moduleInfo = moduleInfo; _tags = new Lazy <IEnumerable <string> >(ParseTags); _version = new Lazy <Version>(ParseVersion); }
private static void BuildModules(IEnumerable <Assembly> assemblies) { Modules.Clear(); var types = from a in assemblies from t in a.GetTypes() where t.Name.EndsWith("Controller") select t; foreach (var t in types) { var moduleInfoType = t.GetCustomAttribute <ModuleAttribute>(); var moduleNo = t.Name.Replace("Controller", ""); var moduleParentNo = ""; if (t.Namespace != null) { moduleParentNo = t.Namespace.Substring(t.Namespace.LastIndexOf('.') + 1); } if (moduleInfoType != null) { if (!string.IsNullOrEmpty(moduleInfoType.ModuleNo)) { moduleNo = moduleInfoType.ModuleNo; } moduleParentNo = moduleInfoType.ParentModuleNo; } //for menu Modules.Add(new ModuleAttribute() { ParentModuleNo = moduleParentNo, ModuleNo = moduleNo }); var methods = t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (var m in methods) { var moduleInfo = new ModuleAttribute(); var attr = m.GetCustomAttribute <ModuleAttribute>(); if (attr != null) { moduleInfo = attr; } if (string.IsNullOrEmpty(moduleInfo.ParentModuleNo)) { moduleInfo.ParentModuleNo = moduleParentNo; } if (string.IsNullOrEmpty(moduleInfo.ModuleNo)) { moduleInfo.ModuleNo = moduleNo; } if (string.IsNullOrEmpty(moduleInfo.Action)) { moduleInfo.Action = m.Name; } Modules.Add(moduleInfo); } } Modules.Distinct(); }
internal T CreateModule <T>() where T : API.Business.Module { var attr = ModuleAttribute.GetAttributeByInterfaceType(typeof(T)); if (attr != null) { return((T)attr.Type.GetConstructor(new Type[] { typeof(Session) }).Invoke(new object[] { this })); } throw new Exception("Brak zarejestrowanego modułu dla interfejsu " + typeof(T).Name); }
public void CanGetAndSetProperties() { var moduleAttribute = new ModuleAttribute(); moduleAttribute.ModuleName = "Test"; moduleAttribute.StartupLoaded = false; Assert.AreEqual("Test", moduleAttribute.ModuleName); Assert.AreEqual(false, moduleAttribute.StartupLoaded); }
public Module(string name, bool isApplication = false) { if (!string.IsNullOrWhiteSpace(name)) { Name = name; SubPath = Application.ModulesRoot + Name; Root = SubPath + '/'; Assembly = Assembly.Load(new AssemblyName(name)); Assets = Assembly.GetCustomAttributes <ModuleAssetAttribute>() .Select(a => new Asset(a.Asset)).ToArray(); AssetPaths = Assets.Select(a => a.ModuleAssetPath).ToArray(); var moduleInfos = Assembly.GetCustomAttributes <ModuleAttribute>(); ModuleInfo = moduleInfos.Where(f => !(f is ModuleMarkerAttribute)).FirstOrDefault() ?? moduleInfos.Where(f => f is ModuleMarkerAttribute).FirstOrDefault() ?? new ModuleAttribute { Name = Name }; if (isApplication) { ModuleInfo.Name = Application.ModuleName; ModuleInfo.Description = "Provides core features defined at the host level"; ModuleInfo.Priority = int.MinValue.ToString(); ModuleInfo.Category = "Infrastructure"; } var features = Assembly.GetCustomAttributes <Manifest.FeatureAttribute>() .Where(f => !(f is ModuleAttribute)); ModuleInfo.Features.AddRange(features); ModuleInfo.Id = Name; } else { Name = Root = SubPath = String.Empty; Assets = Enumerable.Empty <Asset>(); AssetPaths = Enumerable.Empty <string>(); ModuleInfo = new ModuleAttribute(); } _baseNamespace = Name + '.'; _lastModified = DateTimeOffset.UtcNow; }
/// <summary> /// 模块验证 /// </summary> /// <param name="module"></param> /// <returns></returns> public bool ModuleVerify(ModuleAttribute module) { if (Contract.IsAdmin) { return(true); } else { if (Contract.Menus.FirstOrDefault(t => t.MenuName.Equals(module.Name)) != null) { return(true); } } return(false); }
private void AutoRegisterModule() { var types = Assembly.GetAssembly(typeof(ModuleManager)).GetTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(ModuleAttribute), false); foreach (object attr in attrs) { ModuleAttribute mdAttr = attr as ModuleAttribute; if (mdAttr.autoRegister) { Register(mdAttr.name); } } } }
/// <summary> /// 模块验证 /// </summary> /// <param name="module"></param> /// <returns></returns> public bool ModuleVerify(ModuleAttribute module) { bool result = false; if (Loginer.LoginerUser.IsAdmin) { result = true; } else { Authority = Loginer.LoginerUser.authorityEntity.FirstOrDefault(t => t.menuName.Equals(module.Name)); if (Authority != null) { result = true; } } return(result); }
private void RegisterCommand(TamaChanModule module, string commandName, Command command) { try { ModuleAttribute attribute = module.GetType().GetCustomAttribute <ModuleAttribute>(); if (!commandName.IsRegistryValid()) { throw new ArgumentException($"Command from module {attribute.moduleName} was given an invalid name: \"{commandName}\". Command names may only contain alphabetic characters and periods."); } TamaChan.Instance.Logger.LogDebug($"Registering \"{attribute.moduleName} command \"{commandName.ToLower()}\"..."); registry.Add(commandName.ToLower(), command); TamaChan.Instance.Logger.LogDebug($"Command \"{commandName.ToLower()}\" registered."); } catch (Exception ex) { TamaChan.Instance.Logger.LogError("An error occured while registering a command: " + ex.ToString()); } }
/// <summary> /// 获取类型自定义特性 /// </summary> /// <param name="asm"></param> /// <returns></returns> public static ModuleAttribute GetModuleAttribute(TypeInfo asm) { ModuleAttribute temp = new ModuleAttribute(ModuleType.None, "", "", "", "", 0, ""); if (asm == null) { return(temp); } object[] list = asm.GetCustomAttributes(typeof(ModuleAttribute), false); if (list.Length > 0) { return((ModuleAttribute)list[0]); } else { return(temp); } }
void LoadAssembly(System.Reflection.Assembly assembly) { if (assembly != null) { System.Type[] types = assembly.GetTypes(); foreach (System.Type type in types) { ModuleAttribute ma = Attribute.GetCustomAttribute(type, typeof(ModuleAttribute)) as ModuleAttribute; if (!Object.Equals(null, ma)) { ModuleInfo module = new ModuleInfo(); module.Type = type; module.Name = ma.Name; this.modules.Add(module); } } } }
public async Task HelpAsync(CommandContext context, [Description("HelpCommandParameter")] string commandName = null) { DiscordEmbedBuilder builder = new DiscordEmbedBuilder { Color = new DiscordColor(ColorConstant.embedColor) }; GuildsModel guild = context.Channel.IsPrivate ? null : await new GuildsDAO().GetAsync(new GuildsModel { ID = context.Guild.Id }); Locale locale = new LocaleExtension().GetLocale(guild); if (commandName == null) { builder.Title = (await new StringsDAO().LoadAsync(new StringsModel { Identifier = "HelpTitle", Locale = locale, })).String.Replace("$prefix", guild.Prefix); Type[] types = typeof(HelpModule).Assembly.GetTypes(); for (int i = 0; i < types.Length; i++) { ModuleAttribute moduleAttr = types[i].GetCustomAttribute <ModuleAttribute>(); if (moduleAttr != null) { MethodInfo[] methods = types[i].GetMethods(); string commands = string.Empty; for (int j = 0; j < methods.Length; j++) { CommandAttribute commandAttr = methods[j].GetCustomAttribute <CommandAttribute>(); if (commandAttr != null && methods[j].GetCustomAttribute <HiddenAttribute>() == null) { commands += $"`{commandAttr.Name}`, "; } } if (commands.Length != 0) { builder.AddField((await new StringsDAO().LoadAsync(new StringsModel { Locale = locale, Identifier = moduleAttr.Name })).String, commands[..^ 2]);
private void treeView1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && treeView1.SelectedNode != null && treeView1.SelectedNode.Level > 0) { Assembly a = assembly[treeView1.SelectedNode.Text]; Type[] types = a.GetExportedTypes(); foreach (var type in types) { ModuleAttribute attribute = (ModuleAttribute)type.GetCustomAttribute(typeof(ModuleAttribute)); if (attribute != null) { // 创建模块实例 ModuleBase module = (ModuleBase)a.CreateInstance(type.FullName); // 开始拖放操作 treeView1.DoDragDrop(new ModuleControl(module), DragDropEffects.Move); } } } }
/// <summary> /// 权限验证 /// </summary> /// <returns></returns> public bool ModuleVerify(ModuleAttribute bute) { bool result = false; if (Loginer.LoginerUser.IsAdmin) { result = true; } else { _UserAuthority = Loginer.LoginerUser.UserAuthority.FirstOrDefault(t => t.MenuName.Equals(bute.Name)); if (_UserAuthority != null) { result = true; } else { result = false; } } return(result); }
private void ToolBox_Load(object sender, EventArgs e) { // 获取当前文件夹信息,提取模块动态库文件 DirectoryInfo info = new DirectoryInfo(Application.StartupPath); FileInfo[] files = info.GetFiles("*Module.dll"); // 加载程序集 foreach (var file in files) { Assembly a = Assembly.LoadFile(file.FullName); Type[] types = a.GetExportedTypes(); // 更新到界面 foreach (var type in types) { // 获取类型自定义特性 ModuleAttribute attribute = (ModuleAttribute)type.GetCustomAttribute(typeof(ModuleAttribute)); if (attribute != null) { // 添加到集合 assembly.Add(attribute.Name, a); // 添加分类节点 if (treeView1.Nodes.Find(attribute.Category, false).Count() == 0) { treeView1.Nodes.Add(attribute.Category, attribute.Category); treeView1.Nodes.Find(attribute.Category, false)[0].ImageIndex = 1; treeView1.Nodes.Find(attribute.Category, false)[0].SelectedImageIndex = 1; } // 添加模块节点 treeView1.Nodes.Find(attribute.Category, false)[0].Nodes.Add(attribute.Name); } } } }
public Module(string name) { if (!string.IsNullOrWhiteSpace(name)) { Name = name; SubPath = Application.ModulesRoot + Name; Root = SubPath + '/'; Assembly = Assembly.Load(new AssemblyName(name)); Assets = Assembly.GetCustomAttributes <ModuleAssetAttribute>() .Select(a => new Asset(a.Asset)).ToArray(); AssetPaths = Assets.Select(a => a.ModuleAssetPath).ToArray(); ModuleInfo = Assembly.GetCustomAttribute <ModuleAttribute>() ?? new ModuleAttribute { Name = Name }; var features = Assembly.GetCustomAttributes <Manifest.FeatureAttribute>() .Where(f => !(f is ModuleAttribute)); ModuleInfo.Features.AddRange(features); ModuleInfo.Id = Name; } else { Name = Root = SubPath = String.Empty; Assets = Enumerable.Empty <Asset>(); AssetPaths = Enumerable.Empty <string>(); ModuleInfo = new ModuleAttribute(); } _baseNamespace = Name + '.'; _lastModified = DateTimeOffset.UtcNow; }
/// <summary> /// 获取当前程序集下模块 /// </summary> /// <returns></returns> public virtual async Task <IList <ModuleAttribute> > GetModules() { try { IList <ModuleAttribute> list = new List <ModuleAttribute>(); await Task.Run(() => { var ModList = ModuleAssembly.DefinedTypes.Where(t => t.Name.Contains("View")).ToList(); ModList.ForEach(t => { ModuleAttribute bute = GetModuleAttribute(t); if (bute.ModuleType != ModuleType.None) { list.Add(bute); } }); });; return(list); } catch { return(null); } }
private void RegisterModules(Assembly[] assemblies, HelpFileGenerator helpFileGenerator) { foreach (Assembly assembly in assemblies) { if (!assembly.FullName.StartsWith("Discord.Net")) { Type[] assemblyTypes = assembly.GetTypes(); foreach (Type type in assemblyTypes) { if (!type.IsAbstract && type.IsSubclassOf(typeof(TamaChanModule))) { ModuleAttribute moduleAttribute = type.GetCustomAttribute <ModuleAttribute>(); if (moduleAttribute == null) { TamaChan.Instance.Logger.LogWarning($"Class {type.FullName} from {assembly.FullName} inherits TamaChanModule, but does not contain the Module attribute. Module will be ignored."); } else if (moduleAttribute.moduleName.IsRegistryValid()) { try { RegisterModule(type, moduleAttribute, helpFileGenerator); } catch (Exception ex) { TamaChan.Instance.Logger.LogError($"Could not instantiate and register module {type.FullName} from {assembly.FullName}: " + ex.ToString()); } } else { TamaChan.Instance.Logger.LogError($"Module class {type.FullName} from {assembly.FullName} has invalid characters in its name: \"{moduleAttribute.moduleName}\". May only contain alphabetic characters and periods."); } } } } } }
public void RegisterAttributeCommands(Assembly assembly = null) { Bot b = Bot.Instance; if (assembly == null) { assembly = Assembly.GetEntryAssembly(); } var modules = assembly.GetTypes() .Where(m => m.GetCustomAttributes <ModuleAttribute>().Count() > 0) .ToArray(); foreach (var m in modules) { object instance = null; Module newModule = new Module(instance); try { instance = Activator.CreateInstance(Type.GetType(m.AssemblyQualifiedName), newModule, b); } catch { try { instance = Activator.CreateInstance(Type.GetType(m.AssemblyQualifiedName), newModule); } catch { instance = Activator.CreateInstance(Type.GetType(m.AssemblyQualifiedName)); } } newModule.SetInstance(instance); ModuleAttribute mAttrib = m.GetCustomAttribute <ModuleAttribute>(); newModule.Name = mAttrib.module.Name.ToLower(); newModule.Nsfw = mAttrib.module.Nsfw; newModule.CanBeDisabled = mAttrib.module.CanBeDisabled; var methods = m.GetMethods() .Where(t => t.GetCustomAttributes <CommandAttribute>().Count() > 0) .ToArray(); foreach (var x in methods) { CommandEvent newEvent = new CommandEvent(); CommandAttribute commandAttribute = x.GetCustomAttribute <CommandAttribute>(); newEvent = commandAttribute.command; newEvent.ProcessCommand = async(context) => await(Task) x.Invoke(instance, new object[] { context }); newEvent.Module = newModule; CommandEvent foundCommand = newModule.Events.Find(c => c.Name == newEvent.Name); if (foundCommand != null) { foundCommand.Default(newEvent.ProcessCommand); } else { newModule.AddCommand(newEvent); } foreach (var a in newEvent.Aliases) { commandCache.Add(a.ToLower(), newEvent); } commandCache.Add(newEvent.Name.ToLower(), newEvent); } var services = m.GetProperties().Where(x => x.GetCustomAttributes <ServiceAttribute>().Count() > 0).ToArray(); foreach (var s in services) { BaseService service = Activator.CreateInstance(s.PropertyType, true) as BaseService; var attrib = s.GetCustomAttribute <ServiceAttribute>(); service.Name = attrib.Name; newModule.Services.Add(service); } OnModuleLoaded?.Invoke(newModule); modulesLoaded.Add(newModule); } }
public void ModuleNameIsRecorded() { ModuleAttribute attr = new ModuleAttribute("SomeModule"); Assert.AreEqual("SomeModule", attr.Name); }
public void RegisterAttributeCommands() { Assembly assembly = Assembly.GetEntryAssembly(); var modules = assembly.GetTypes() .Where(m => m.GetCustomAttributes <ModuleAttribute>().Count() > 0) .ToArray(); foreach (var m in modules) { RuntimeModule newModule = new RuntimeModule(); object instance = null; try { instance = (object)Activator.CreateInstance(Type.GetType(m.AssemblyQualifiedName), newModule); } catch { instance = (object)Activator.CreateInstance(Type.GetType(m.AssemblyQualifiedName)); } newModule.EventSystem = this; ModuleAttribute mAttrib = m.GetCustomAttribute <ModuleAttribute>(); newModule.Name = mAttrib.module.Name.ToLower(); newModule.Nsfw = mAttrib.module.Nsfw; newModule.CanBeDisabled = mAttrib.module.CanBeDisabled; var methods = m.GetMethods() .Where(t => t.GetCustomAttributes <CommandAttribute>().Count() > 0) .ToArray(); foreach (var x in methods) { RuntimeCommandEvent newEvent = new RuntimeCommandEvent(); CommandAttribute commandAttribute = x.GetCustomAttribute <CommandAttribute>(); newEvent = commandAttribute.command; newEvent.ProcessCommand = async(context) => await(Task) x.Invoke(instance, new object[] { context }); newEvent.Module = newModule; ICommandEvent foundCommand = newModule.Events.Find(c => c.Name == newEvent.Name); if (foundCommand != null) { if (commandAttribute.on != "") { foundCommand.On(commandAttribute.On, newEvent.ProcessCommand); } else { foundCommand.Default(newEvent.ProcessCommand); } } else { newModule.AddCommand(newEvent); } } newModule.InstallAsync(bot).GetAwaiter().GetResult(); } }
private void PrintDutAttribute( ModuleAttribute sDUT) { DisplayOperateMes("<--------------------------->"); DisplayOperateMes("IQ = " + sDUT.dIQ.ToString("F3")); DisplayOperateMes("dVoutIPNative = " + sDUT.dVoutIPNative.ToString("F3")); DisplayOperateMes("dVout0ANative = " + sDUT.dVout0ANative.ToString("F3")); DisplayOperateMes("dVoutIPMiddle = " + sDUT.dVoutIPMiddle.ToString("F3")); DisplayOperateMes("dVout0AMiddle = " + sDUT.dVout0AMiddle.ToString("F3")); DisplayOperateMes("dVoutIPTrimmed = " + sDUT.dVoutIPTrimmed.ToString("F3")); DisplayOperateMes("dVout0ATrimmed = " + sDUT.dVout0ATrimmed.ToString("F3")); DisplayOperateMes("bDigitalCommFail = " + sDUT.bDigitalCommFail.ToString()); DisplayOperateMes("bNormalModeFail = " + sDUT.bNormalModeFail.ToString()); DisplayOperateMes("bReadMarginal = " + sDUT.bReadMarginal.ToString()); DisplayOperateMes("bReadSafety = " + sDUT.bReadSafety.ToString()); DisplayOperateMes("bTrimmed = " + sDUT.bTrimmed.ToString()); DisplayOperateMes("<--------------------------->"); }