/// <summary> /// Constructor /// </summary> /// <param name="pluginData">Plugin data</param> public PluginDataForm(PluginDataContract pluginData) { InitializeComponent(); Translator.LoadTranslation(this); Translator.EnumToComboBox <EPluginProvider>(pluginProviderComboBox); Translator.EnumToComboBox <EUpdateFrequency>(pluginUpdateFrequencyComboBox); if (pluginData != null) { Text = Translator.GetTranslation("EDIT_PLUGIN_TITLE"); pluginEnabledCheckBox.Checked = pluginData.Enabled; pluginNameSingleLineTextField.Text = pluginData.Name; pluginProviderComboBox.SelectedIndex = (int)(pluginData.Provider); pluginURISingleLineTextField.Text = pluginData.URI; pluginUpdateFrequencyComboBox.SelectedIndex = (int)(pluginData.UpdateFrequency); } }
/// <summary> /// Revert plugins data /// </summary> /// <returns>Plugins data</returns> public static PluginDataContract[] RevertPluginsData() { PluginDataContract[] ret = null; try { using (WebClientEx wc = new WebClientEx(3000)) { wc.Headers.Set(HttpRequestHeader.Accept, "application/json"); wc.Headers.Set(HttpRequestHeader.UserAgent, "Mozilla/3.0 (compatible; SA:MP launcher .NET)"); try { using (MemoryStream stream = new MemoryStream(wc.DownloadData(GitHubProjectURL + "/master/plugins.json"))) { ret = pluginsDataSerializer.ReadObject(stream) as PluginDataContract[]; } } catch (Exception e) { Console.Error.WriteLine(e); } } } catch (Exception e) { Console.Error.WriteLine(e); } if (ret == null) { ret = new PluginDataContract[] { new PluginDataContract("SA:MP Discord Rich Presence plugin", EPluginProvider.GitHub, "https://github.com/Hual/samp-discord-plugin", false, EUpdateFrequency.WhenNewer) }; } PluginsDataIO = ret; return(ret); }
/// <summary> /// Update plugin /// </summary> /// <param name="plugin">Plugin</param> /// <returns>Installed plugin information if plugin is up to date, otherwise "null"</returns> public static InstalledPlugin Update(PluginDataContract plugin) { InstalledPlugin ret = null; if (plugin != null) { string download_path = null; string download_url = null; GitHubLatestReleaseDataContract last_release_info = null; GitHubLatestReleaseDataContract latest_release_info = null; if (plugin.Enabled) { try { InstalledPlugin installed_plugin = null; Uri uri = new Uri(plugin.URI); if (!(Directory.Exists("./plugins"))) { Directory.CreateDirectory("./plugins"); } if (installedPlugins.ContainsKey(plugin)) { installed_plugin = installedPlugins[plugin]; } switch (plugin.Provider) { case EPluginProvider.URI: if (uri.IsFile) { if (!(File.Exists(plugin.URI))) { ret = new InstalledPlugin(plugin.URI); } } else { download_path = Path.Combine(Path.GetFullPath("./plugins/"), uri.LocalPath); switch (plugin.UpdateFrequency) { case EUpdateFrequency.IfMissing: case EUpdateFrequency.WhenNewer: if (File.Exists(download_path)) { ret = new InstalledPlugin(download_path); installedPlugins.Add(plugin, ret); } else { if (File.Exists(download_path)) { File.Delete(download_path); } download_url = plugin.URI; } break; case EUpdateFrequency.Always: download_url = plugin.URI; break; } } break; case EPluginProvider.GitHub: string github_user_repo = uri.AbsolutePath.Trim('/'); string github_api_url = "https://api.github.com/repos/" + github_user_repo + "/releases/latest"; download_path = Path.GetFullPath("./plugins/" + github_user_repo.Replace('/', '$') + ".asi"); string info_path = download_path + ".json"; if (installed_plugin != null) { last_release_info = installed_plugin.LastReleaseInfo; latest_release_info = installed_plugin.LatestReleaseInfo; } switch (plugin.UpdateFrequency) { case EUpdateFrequency.IfMissing: if (File.Exists(download_path)) { ret = new InstalledPlugin(download_path); } else { download_url = GetDownloadURL(info_path, github_api_url, ref last_release_info, ref latest_release_info); } break; case EUpdateFrequency.WhenNewer: string dl_url = GetDownloadURL(info_path, github_api_url, ref last_release_info, ref latest_release_info); if (latest_release_info != null) { if (last_release_info != null) { if (last_release_info.TagName != latest_release_info.TagName) { download_url = dl_url; } else { ret = new InstalledPlugin(download_path); } } else { download_url = dl_url; } } break; case EUpdateFrequency.Always: download_url = GetDownloadURL(info_path, github_api_url, ref last_release_info, ref latest_release_info); break; } break; } } catch (Exception e) { MessageBox.Show(e.Message, Translator.GetTranslation("PLUGIN_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } if ((download_path != null) && (download_url != null)) { if (File.Exists(download_path)) { File.Delete(download_path); } DownloadProgressForm dpf = new DownloadProgressForm(download_url, download_path); if (latest_release_info != null) { last_release_info = latest_release_info; } if (dpf.ShowDialog() == DialogResult.OK) { ret = new InstalledPlugin(download_path, last_release_info, latest_release_info); if (plugin.Provider == EPluginProvider.GitHub) { try { string info_path = download_path + ".json"; if (File.Exists(info_path)) { File.Delete(info_path); } using (FileStream stream = File.Open(info_path, FileMode.Create)) { serializer.WriteObject(stream, latest_release_info); } } catch (Exception e) { MessageBox.Show(e.Message, Translator.GetTranslation("PLUGIN_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } if (ret != null) { if (installedPlugins.ContainsKey(plugin)) { installedPlugins[plugin] = ret; } else { installedPlugins.Add(plugin, ret); } } } return(ret); }