Ejemplo n.º 1
0
        private async Task <(CSPlugin, PluginState, string)> CastToPlugin(Type type)
        {
            CSPlugin    plugin   = null;
            PluginState state    = PluginState.Error;
            string      message  = null;
            dynamic     instance = Activator.CreateInstance(type);

            try
            {
                plugin = (CSPlugin)instance;                 //* Can be casted into IPlugin -> Valid.
                state  = PluginState.Valid;

                await Task.Run(() =>
                {
                    plugin.OnValidated();                     //* If ends, plugin is ready.
                    state = PluginState.Ready;
                });
            }
            catch (InvalidCastException e)
            {
                message = e.Message;                 //* Error at casting (maybe bad impementation).
            }

            return(plugin, state, message);
        }
Ejemplo n.º 2
0
        public void LoadPlugin(string dir)
        {
            FileInfo info = null;

            foreach (var file in Directory.GetFiles(dir, Pattern))
            {
                try
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    info = new FileInfo(file);
                    string name = Path.GetFileNameWithoutExtension(info.Name);
                    if (Plugins.TryGetValue(name, out CSPlugin p))
                    {
                        if (p.Container.Running)
                        {
                            logger.LogWarning(string.Format("[CSharp] Failed to load {0} because its already initialized", p.Title));
                            break;
                        }
                        logger.LogInfo(string.Format("[CSharp] Succesfully loaded plugin {0}, {1}, Author {2} ({3})", p.Title, p.Version, p.Author, p.Description));
                        p.Container.Start();
                        break;
                    }
                    if (!Assemblies.TryGetValue(name, out Assembly assembly))
                    {
                        assembly = Assembly.Load(File.ReadAllBytes(file));
                        Assemblies.Add(name, assembly);
                    }


                    if (this.IsSecure(assembly, out ViolationType violationType))
                    {
                        foreach (Type type in assembly.GetExportedTypes())
                        {
                            if (type.IsSubclassOf(typeof(CSPlugin)) && type.IsPublic && !type.IsAbstract)
                            {
                                object   instance = Activator.CreateInstance(type);
                                CSPlugin plugin   = (CSPlugin)instance;

                                if (name != plugin.Title)
                                {
                                    logger.LogWarning($"[CSharp] Failed to load plugin {plugin.Title} because the file name is not the same as the title");
                                    return;
                                }
                                if (((plugin.CoreVersion.ToString() == "0.0.0.0") || (plugin.CoreVersion >= Redox.Version)) || Bootstrap.RedoxMod.Config.LoadIncompitablePlugins)
                                {
                                    plugin.FileInfo = info;
                                    PluginContainer container = new PluginContainer(plugin, instance, Language);
                                    PluginCollector.GetCollector().AddPlugin(container);
                                    Plugins.Add(plugin.Title, plugin);
                                    logger.LogInfo(string.Format("[CSharp] Succesfully loaded plugin {0}, {1}, Author {2} ({3})", plugin.Title, plugin.Version, plugin.Author, plugin.Description));
                                }
                                else
                                {
                                    logger.LogWarning($"[Redox] Plugin \"{plugin.Title}\" is not compitable with the current redox version!");
                                }
                            }
                        }
                        sw.Stop();
                        int time = sw.Elapsed.Milliseconds;

                        if (time > 500)
                        {
                            logger.LogSpeed(string.Format("[CSharp] Plugin {0} took {1} milliseconds to load", name, time));
                        }
                    }
                    else
                    {
                        logger.LogWarning(string.Format("[CSharp] {0} has been blocked due security violation: {1}", assembly.GetName().Name, violationType));
                    }
                }


                catch (Exception ex)
                {
                    logger.LogError(string.Format("[CSharp] Failed to load {0}, Error: {1}", info.Name, ex));
                }
            }
        }
Ejemplo n.º 3
0
 public Configuration(string name, CSPlugin plugin)
 {
     Settings    = new Dictionary <string, object>();
     this.plugin = plugin;
     this.name   = name + ".json";
 }
Ejemplo n.º 4
0
 public PluginInfo(CSPlugin plug, PluginState state, string msg)
 {
     Plugin  = plug;
     State   = state;
     Message = msg;
 }