コード例 #1
0
        private void Initialize()
        {
            try
            {
                if (!Directory.Exists("plugins"))
                {
                    Directory.CreateDirectory("plugins");
                }
            }
            catch (Exception ex)
            {
                Logger.Log(LogType.Error, "PluginManager.Initialize: " + ex);
                return;
            }

            // Load plugins
            string[] plugins = Directory.GetFiles("plugins", "*.dll");


            if (plugins.Length == 0)
            {
                Logger.Log(LogType.ConsoleOutput, "PluginManager: No plugins found");
            }
            else
            {
                Logger.Log(LogType.ConsoleOutput, "PluginManager: Loading " + plugins.Length + " plugins");

                foreach (string plugin in plugins)
                {
                    try
                    {
                        Type   pluginType = null;
                        string args       = plugin.Substring(plugin.LastIndexOf("\\") + 1, plugin.IndexOf(".dll") - plugin.LastIndexOf("\\") - 1);
                        string file       = Path.GetFullPath(plugin);
                        if (Path.GetExtension(file) != ".dll")
                        {
                            continue;                                    // Continue to next file
                        }
                        Assembly assembly = Assembly.LoadFile(file);
                        pluginType = assembly.GetType(args + ".Init");
                        if (pluginType == null)
                        {
                            continue;                     // Ignore dll's that are not comptatible, including plugins made for other fCraft forks
                        }
                        // Uses a temp variable so we can load the defaults of the plugin later
                        // This prevents the user from modifiying key elements important to the plugin in the config file
                        IPlugin pluginObj = null;
                        IPlugin temp      = (IPlugin)Activator.CreateInstance(pluginType);
                        // Checks to make sure the plugin is updated - this is crucial!
                        if (Version.Compare(temp.SoftwareVersion, Updater.LatestStable) != -1)
                        {
                            throw new Exception(
                                      $"Plugin {temp.Name} uses an outdated version of GemsCraft. Check with the plugin developer to have it updated.");
                        }
                        // Checks to see if a property file exists for the plugin.
                        string propertyName = $"plugins/{temp.Name}.json";
                        if (!File.Exists(propertyName))
                        {
                            Logger.Log(LogType.Warning,
                                       $"Property file does not exist for plugin {temp.Name}. Assuming defaults.");
                        }
                        else
                        {
                            pluginObj = JsonConvert.DeserializeObject <IPlugin>(propertyName);
                        }

                        if (pluginObj == null)
                        {
                            pluginObj = temp;
                        }
                        else
                        {
                            // Reinstates the key items as they should be, in case the user modified them in the config
                            pluginObj.Name        = temp.Name;
                            pluginObj.Version     = temp.Version;
                            pluginObj.FileName    = temp.FileName;
                            pluginObj.Author      = temp.Author;
                            pluginObj.ReleaseDate = temp.ReleaseDate;
                        }
                        // Adds the plugin to the list.
                        Plugins.Add(pluginObj);
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(LogType.Error, "PluginManager: Unable to load plugin at location " + plugin + ": " + ex);
                    }
                }

                LoadPlugins();
            }
        }