/// <summary> /// Update sampctl /// </summary> /// <returns>"true" if sampctl is up to date, otherwise "false"</returns> public static bool Update() { bool ret = false; if (lastReleaseInfo == null) { if (File.Exists(SAMPCTLInfoPath)) { try { using (FileStream stream = File.Open(SAMPCTLInfoPath, FileMode.Open)) { lastReleaseInfo = serializer.ReadObject(stream) as GitHubLatestReleaseDataContract; } } catch (Exception e) { MessageBox.Show(e.Message, Translator.GetTranslation("SAMPCTL_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } if (latestReleaseInfo == null) { 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(GitHubAPIURI))) { latestReleaseInfo = serializer.ReadObject(stream) as GitHubLatestReleaseDataContract; } } catch (Exception e) { MessageBox.Show(e.Message, Translator.GetTranslation("SAMPCTL_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } if (latestReleaseInfo != null) { bool download_file = ((lastReleaseInfo == null) ? true : ((lastReleaseInfo.TagName == latestReleaseInfo.TagName) ? (!(File.Exists(SAMPCTLDownloadPath))) : true)); if (download_file) { GitHubReleaseAssetDataContract asset = null; string sub_name = (Environment.Is64BitOperatingSystem ? "windows_amd64" : "windows_386"); foreach (GitHubReleaseAssetDataContract a in latestReleaseInfo.Assets) { if (a.Name.Contains(sub_name)) { asset = a; break; } } if (asset == null) { MessageBox.Show(Translator.GetTranslation("SAMPCTL_MISSING_ASSET"), Translator.GetTranslation("SAMPCTL_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } else { DownloadProgressForm dpf = new DownloadProgressForm(asset.BrowserDownloadURL, SAMPCTLArchiveFileName); ret = (dpf.ShowDialog() == DialogResult.OK); } } else { ret = true; } if (ret && (download_file ? true : (!(File.Exists(SAMPCTLPath))))) { try { if (File.Exists(SAMPCTLDownloadPath)) { using (FileStream archive_file_stream = File.Open(SAMPCTLDownloadPath, FileMode.Open)) { using (GZipInputStream gzip_stream = new GZipInputStream(archive_file_stream)) { using (TarInputStream tar_stream = new TarInputStream(gzip_stream)) { TarEntry tar_entry; while ((tar_entry = tar_stream.GetNextEntry()) != null) { if (!(tar_entry.IsDirectory)) { if (tar_entry.Name == SAMPCTLFileName) { if (File.Exists(SAMPCTLPath)) { File.Delete(SAMPCTLPath); } using (FileStream file_stream = File.Open(SAMPCTLPath, FileMode.Create)) { tar_stream.CopyEntryContents(file_stream); ret = true; } break; } } } } } } } else { ret = false; } if (!ret) { MessageBox.Show(Translator.GetTranslation("SAMPCTL_UNZIP_ERROR"), Translator.GetTranslation("SAMPCTL_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception e) { MessageBox.Show(e.Message, Translator.GetTranslation("SAMPCTL_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } if (ret) { try { if (File.Exists(SAMPCTLInfoPath)) { File.Delete(SAMPCTLInfoPath); } using (FileStream stream = File.Open(SAMPCTLInfoPath, FileMode.Create)) { serializer.WriteObject(stream, latestReleaseInfo); } } catch (Exception e) { ret = false; MessageBox.Show(e.Message, Translator.GetTranslation("SAMPCTL_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } else { ret = true; } return(ret); }
/// <summary> /// Update sampctl /// </summary> /// <returns>"true" if sampctl is up to date, otherwise "false"</returns> public static bool Update() { bool ret = false; if (lastReleaseInfo == null) { if (File.Exists(SAMPDiscordPluginPath)) { try { using (FileStream stream = File.Open(SAMPDiscordPluginInfoPath, FileMode.Open)) { lastReleaseInfo = serializer.ReadObject(stream) as GitHubLatestReleaseDataContract; } } catch (Exception e) { MessageBox.Show(e.Message, Translator.GetTranslation("SAMP_DISCORD_PLUGIN_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } if (latestReleaseInfo == null) { 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(GitHubAPIURI))) { latestReleaseInfo = serializer.ReadObject(stream) as GitHubLatestReleaseDataContract; } } catch (Exception e) { MessageBox.Show(e.Message, Translator.GetTranslation("SAMP_DISCORD_PLUGIN_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } if (latestReleaseInfo != null) { bool download_file = ((lastReleaseInfo == null) ? true : ((lastReleaseInfo.TagName == latestReleaseInfo.TagName) ? (!(File.Exists(SAMPDiscordPluginDownloadPath))) : true)); if (download_file) { GitHubReleaseAssetDataContract asset = null; foreach (GitHubReleaseAssetDataContract a in latestReleaseInfo.Assets) { if (a.Name.Contains(SAMPDiscordPluginFileName)) { asset = a; break; } } if (asset == null) { MessageBox.Show(Translator.GetTranslation("SAMP_DISCORD_PLUGIN_MISSING_ASSET"), Translator.GetTranslation("SAMP_DISCORD_PLUGIN_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } else { DownloadProgressForm dpf = new DownloadProgressForm(asset.BrowserDownloadURL, SAMPDiscordPluginFileName); ret = (dpf.ShowDialog() == DialogResult.OK); } } else { ret = true; } if (ret && (download_file ? true : (!(File.Exists(SAMPDiscordPluginPath))))) { try { if (File.Exists(SAMPDiscordPluginDownloadPath)) { File.Copy(SAMPDiscordPluginDownloadPath, SAMPDiscordPluginPath); } else { ret = false; } if (!ret) { MessageBox.Show(Translator.GetTranslation("SAMP_DISCORD_PLUGIN_COPY_ERROR"), Translator.GetTranslation("SAMP_DISCORD_PLUGIN_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception e) { MessageBox.Show(e.Message, Translator.GetTranslation("SAMP_DISCORD_PLUGIN_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } if (ret) { try { if (File.Exists(SAMPDiscordPluginInfoPath)) { File.Delete(SAMPDiscordPluginInfoPath); } using (FileStream stream = File.Open(SAMPDiscordPluginInfoPath, FileMode.Create)) { serializer.WriteObject(stream, latestReleaseInfo); } } catch (Exception e) { ret = false; MessageBox.Show(e.Message, Translator.GetTranslation("SAMP_DISCORD_PLUGIN_ERROR_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } else { ret = true; } return(ret); }