public NewUpdateState NewUpdatesAvailable(out int configUpdateVersion) { NewUpdateState rv = NewUpdateState.NoNewUpdate; configUpdateVersion = -1; Uri updateUri = new Uri(UpdatesXmlFileLocation); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(updateUri); httpRequest.Method = "GET"; httpRequest.Timeout = 30000; //30 sec HttpWebResponse response = null; try { response = (HttpWebResponse)httpRequest.GetResponse(); string updateXml = null; Stream streamResponse = response.GetResponseStream(); try { using (TextReader reader = new StreamReader(streamResponse)) { updateXml = reader.ReadToEnd(); } } finally { streamResponse.Close(); } if (updateXml != null) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(updateXml); int latestVersion = CurrentlyInstalledTangra3Version(); XmlNode latestVersionNode = null; foreach (XmlNode updateNode in xmlDoc.SelectNodes("/Tangra3/Update")) { int Version = int.Parse(updateNode.Attributes["Version"].Value); if (latestVersion < Version) { if (CurrentOS.IsWindows) { Trace.WriteLine("Update location: " + updateUri.ToString()); Trace.WriteLine("Current version: " + latestVersion.ToString()); Trace.WriteLine("New version: " + Version.ToString()); } else { Console.WriteLine("Update location: " + updateUri.ToString()); Console.WriteLine("Current version: " + latestVersion.ToString()); Console.WriteLine("New version: " + Version.ToString()); } #if WIN32 XmlNode tangra3UpdateNode = xmlDoc.SelectSingleNode("/Tangra3/AutoUpdate"); if (tangra3UpdateNode != null) { tangra3UpdateServerVersion = tangra3UpdateNode.Attributes["Version"].Value; Trace.WriteLine("Tangra3Update new version: " + tangra3UpdateServerVersion); } else { tangra3UpdateServerVersion = null; } latestVersion = Version; latestVersionNode = updateNode; rv = NewUpdateState.TangraUpdate; #endif } } #if WIN32 foreach (XmlNode updateNode in xmlDoc.SelectNodes("/Tangra3/ModuleUpdate")) { if (updateNode.Attributes["Version"] == null) { continue; } bool mustExist = updateNode.Attributes["MustExist"] != null && updateNode.Attributes["MustExist"].Value == "true"; bool isAddin = updateNode.Attributes["IsAddin"] != null && updateNode.Attributes["IsAddin"].Value == "true"; int Version = int.Parse(updateNode.Attributes["Version"].Value); latestVersion = CurrentlyInstalledModuleVersion(updateNode.Attributes["File"].Value); if (mustExist && latestVersion == 0) { continue; } if (latestVersion < Version) { Trace.WriteLine("Update location: " + updateUri.ToString()); Trace.WriteLine("Module: " + updateNode.Attributes["File"].Value); Trace.WriteLine("Current version: " + latestVersion.ToString()); Trace.WriteLine("New version: " + Version.ToString()); XmlNode tangra3UpdateNode = xmlDoc.SelectSingleNode("/Tangra3/AutoUpdate"); if (tangra3UpdateNode != null) { tangra3UpdateServerVersion = tangra3UpdateNode.Attributes["Version"].Value; Trace.WriteLine("Tangra3Update new version: " + tangra3UpdateServerVersion); } else { tangra3UpdateServerVersion = null; } latestVersion = Version; latestVersionNode = updateNode; rv = isAddin ? NewUpdateState.AddinModuleUpdate : NewUpdateState.TangraUpdate; break; } } XmlNode cfgUpdateNode = xmlDoc.SelectSingleNode("/Tangra3/ConfigurationUpdate"); if (cfgUpdateNode != null) { XmlNode cfgUpdVer = cfgUpdateNode.Attributes["Version"]; if (cfgUpdVer != null) { configUpdateVersion = int.Parse(cfgUpdVer.InnerText); } } #endif } } finally { // Close response if (response != null) { response.Close(); } } return(rv); }
private void CheckForUpdates(object state) { if (CurrentOS.IsWinTangraEndOfLife) { return; } try { m_LastUpdateTime = DateTime.Now; int serverConfigVersion; NewUpdateState updateState = NewUpdatesAvailable(out serverConfigVersion); if (updateState == NewUpdateState.TangraUpdate) { Trace.WriteLine("There is a new update.", "Update"); m_MainFrom.Invoke(new OnUpdateEventDelegate(OnUpdateEvent), MSG_ID_NEW_TANGRA3_UPDATE_AVAILABLE); } if (updateState == NewUpdateState.AddinModuleUpdate) { Trace.WriteLine("There is a new add-in update.", "Update"); m_MainFrom.Invoke(new OnUpdateEventDelegate(OnUpdateEvent), MSG_ID_NEW_TANGRA3_ADDIN_UPDATE_AVAILABLE); } else { Trace.WriteLine( string.Format("There are no new {0}updates.", TangraConfig.Settings.Generic.AcceptBetaUpdates ? "beta " : ""), "Update"); m_MainFrom.Invoke(new OnUpdateEventDelegate(OnUpdateEvent), MSG_ID_NO_TANGRA3_UPDATES_AVAILABLE); } Settings.Default.LastCheckedForUpdates = m_LastUpdateTime; Settings.Default.Save(); } catch (Exception ex) { Trace.WriteLine(ex, "Update"); } try { if (TangraConfig.Settings.Generic.SubmitUsageStats && Settings.Default.UsageStatsLastSent.AddDays(30) < DateTime.Now) { UsageStats.Instance.Save(); string usageStats = Settings.Default.UsageStatistics; WebRequest req = WebRequest.Create("http://www.occultwatcher.net/CGI-BIN/TangraUsageStats.ashx"); req.ContentType = "application/xml"; req.Method = "POST"; byte[] bytes = Encoding.UTF8.GetBytes(usageStats); req.ContentLength = bytes.Length; System.IO.Stream os = req.GetRequestStream(); os.Write(bytes, 0, bytes.Length); os.Close(); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); if (resp.StatusCode == HttpStatusCode.OK) { UsageStats.ClearStats(); Settings.Default.UsageStatsLastSent = DateTime.Now; Settings.Default.Save(); } } } catch (Exception ex) { Trace.WriteLine(ex, "UsageStats"); } }