コード例 #1
0
ファイル: PluginManager.cs プロジェクト: tanmoydeb07/gmod-ide
        /// <summary>
        /// Load a plugin by assembly
        /// </summary>
        /// <param name="a">Plugin Assembly</param>
        /// <returns>Success</returns>
        public static bool LoadPlugin(Assembly a)
        {
            String ass = "Unknown";

            try
            {
                ass = a.GetName().Name;
                Type pluginType = a.GetType(ass + ".Plugin");
                if (pluginType != null)
                {
                    IPluginClient ipc = (IPluginClient)Activator.CreateInstance(pluginType);
                    ipc.Host = PluginServer;

                    Settings s = null;
                    if (!File.Exists("data\\" + ass + ".aps"))
                    {
                        try
                        {
                            SettingDefaults sd = SettingsHost.DefaultsFromInterface(ipc.DefaultSettings);
                            s      = new Settings(sd);
                            s.Data = sd.Defaults;
                            s.Save("data\\" + ass + ".aps");
                        }
                        catch (Exception ex)
                        {
                            ErrorHandler.CreateError(ex, "The plugin " + ipc.Name + " was unable to create it's settings file");
                        }
                    }
                    else
                    {
                        try
                        {
                            SettingDefaults sd = SettingsHost.DefaultsFromInterface(ipc.DefaultSettings);
                            s = new Settings(sd);
                            s.Load("data\\" + ass + ".aps");

                            if (sd.Defaults.Count != ipc.DefaultSettings.Defaults.Count)
                            {
                                try
                                {
                                    if (File.Exists("data\\" + ass + ".aps.old"))
                                    {
                                        File.Delete("data\\" + ass + ".aps.old");
                                    }

                                    File.Copy("data\\" + ass + ".aps", "data\\" + ass + ".aps.old");
                                    File.Delete("data\\" + ass + ".aps");
                                    ErrorHandler.CreateError(new Exception(""), "The plugin " + ipc.Name + " has outdated or corrupted setting defaults - the settings file has been deleted and backed up");
                                }
                                catch (Exception ex)
                                {
                                    ErrorHandler.CreateError(ex, "The plugin " + ipc.Name + " has outdated or corrupted setting defaults - the settings file could not be deleted automatically and must manually be deleted");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ErrorHandler.CreateError(ex, "The plugin " + ipc.Name + " was unable to load it's settings file");
                        }
                    }

                    PluginData pd = null;
                    pd = new PluginData(ipc, new SettingsHost(pd, s));
                    Plugins.Add(pd);
                    Utils.Log("Loaded plugin: " + CompilePluginDesc(ipc), LogType.INFO);
                }
                else
                {
                    Utils.Log("Invalid plugin type: " + ass + ".Plugin", LogType.INFO);
                }
                return(true);
            }
            catch (Exception ex)
            {
                Utils.Log("Failed to load plugin: " + ass + " (" + ex.Message + ")", LogType.ERROR);
            }
            return(false);
        }