Exemplo n.º 1
0
        public static List <Assembly> LoadAssembliesFromDirectory(string directory, string extension = "*.dll")
        {
            List <Assembly>        assemblies       = new List <Assembly>();
            IEnumerable <FileInfo> pluginsLibraries = new DirectoryInfo(directory).GetFiles(extension, SearchOption.AllDirectories);

            foreach (FileInfo library in pluginsLibraries)
            {
                try
                {
                    Assembly assembly = Assembly.LoadFile(library.FullName);//Assembly.Load(File.ReadAllBytes(library.FullName));

                    List <Type> types = RocketHelper.NewGetTypesFromInterface(assembly, typeof(IRocketPlugin));

                    if (types.Count == 1)
                    {
                        Logging.Logger.Log("Loading " + assembly.GetName().Name + " from " + assembly.Location);
                        assemblies.Add(assembly);
                    }
                    else
                    {
                        Logging.Logger.LogError("Invalid or outdated plugin assembly: " + assembly.GetName().Name);
                    }
                }
                catch (Exception ex)
                {
                    Logging.Logger.LogError(ex, "Could not load plugin assembly: " + library.Name);
                }
            }
            return(assemblies);
        }
        public void RegisterFromAssembly(Assembly assembly)
        {
            foreach (Type commandType in RocketHelper.NewGetTypesFromInterface(assembly, typeof(IRocketCommand)))
            {
                if (commandType.GetConstructor(Type.EmptyTypes) != null)
                {
                    IRocketCommand command = (IRocketCommand)Activator.CreateInstance(commandType);
                    Register(command);

                    foreach (string alias in command.Aliases)
                    {
                        Register(command, alias);
                    }
                }
            }

            Type plugin = RocketHelper.NewGetTypeFromInterface(assembly, typeof(IRocketPlugin));

            if (plugin != null)
            {
                MethodInfo[] methodInfos = plugin.GetMethods(BindingFlags.Public | BindingFlags.Instance);

                foreach (MethodInfo method in methodInfos)
                {
                    RocketCommandAttribute             commandAttribute            = (RocketCommandAttribute)Attribute.GetCustomAttribute(method, typeof(RocketCommandAttribute));
                    RocketCommandAliasAttribute[]      commandAliasAttributes      = (RocketCommandAliasAttribute[])Attribute.GetCustomAttributes(method, typeof(RocketCommandAliasAttribute));
                    RocketCommandPermissionAttribute[] commandPermissionAttributes = (RocketCommandPermissionAttribute[])Attribute.GetCustomAttributes(method, typeof(RocketCommandPermissionAttribute));

                    if (commandAttribute != null)
                    {
                        List <string> Permissions = new List <string>();
                        List <string> Aliases     = new List <string>();

                        if (commandAliasAttributes != null)
                        {
                            foreach (RocketCommandAliasAttribute commandAliasAttribute in commandAliasAttributes)
                            {
                                Aliases.Add(commandAliasAttribute.Name);
                            }
                        }

                        if (commandPermissionAttributes != null)
                        {
                            foreach (RocketCommandPermissionAttribute commandPermissionAttribute in commandPermissionAttributes)
                            {
                                Aliases.Add(commandPermissionAttribute.Name);
                            }
                        }

                        IRocketCommand command = new RocketAttributeCommand(commandAttribute.Name, commandAttribute.Help, commandAttribute.Syntax, commandAttribute.AllowedCaller, Permissions, Aliases, method);
                        Register(command);
                        foreach (string alias in command.Aliases)
                        {
                            Register(command, alias);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        internal void loadPlugins()
        {
            libraries = GetAssembliesFromDirectory(Environment.LibrariesDirectory);
            foreach (KeyValuePair <string, string> pair in GetAssembliesFromDirectory(Environment.PluginsDirectory))
            {
                if (!libraries.ContainsKey(pair.Key))
                {
                    libraries.Add(pair.Key, pair.Value);
                }
            }

            pluginAssemblies = LoadAssembliesFromDirectory(Environment.PluginsDirectory);
            List <Type> pluginImplemenations = RocketHelper.NewGetTypesFromInterface(pluginAssemblies, typeof(IRocketPlugin));

            foreach (Type pluginType in pluginImplemenations)
            {
                GameObject plugin = new GameObject(pluginType.Name, pluginType);
                UnityEngine.Object.DontDestroyOnLoad(plugin);
                plugins.Add(plugin);
            }
            OnPluginsLoaded.TryInvoke();
        }