From c352ff644f200c21258e4a62cfcaff3320dc923f Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 7 Jul 2014 21:48:07 +1200 Subject: [PATCH 1/2] Use a shared url for news updates. Fixes #5603. --- OpenRA.Game/Manifest.cs | 4 ---- OpenRA.Game/Settings.cs | 1 + OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs | 4 ++-- mods/cnc/mod.yaml | 2 -- mods/d2k/mod.yaml | 2 -- mods/ra/mod.yaml | 2 -- mods/ts/mod.yaml | 2 -- 7 files changed, 3 insertions(+), 14 deletions(-) diff --git a/OpenRA.Game/Manifest.cs b/OpenRA.Game/Manifest.cs index 3ae9e78ff6..1d2ad01433 100644 --- a/OpenRA.Game/Manifest.cs +++ b/OpenRA.Game/Manifest.cs @@ -35,7 +35,6 @@ namespace OpenRA public readonly InstallData ContentInstaller; public readonly Dictionary> Fonts; public readonly Size TileSize = new Size(24, 24); - public readonly string NewsUrl; public readonly TileShape TileShape = TileShape.Rectangle; public Manifest(string mod) @@ -97,9 +96,6 @@ namespace OpenRA compat.Add(c.Trim()); MapCompatibility = compat.ToArray(); - - if (yaml.ContainsKey("NewsUrl")) - NewsUrl = yaml["NewsUrl"].Value; } static string[] YamlList(Dictionary yaml, string key) diff --git a/OpenRA.Game/Settings.cs b/OpenRA.Game/Settings.cs index ed5bc9d9f9..9878eedbba 100644 --- a/OpenRA.Game/Settings.cs +++ b/OpenRA.Game/Settings.cs @@ -141,6 +141,7 @@ namespace OpenRA public string MapRepository = "http://resource.openra.net/map/"; public bool FetchNews = true; + public string NewsUrl = "http://www.openra.net/gamenews"; public DateTime NewsFetchedDate; } diff --git a/OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs index 70be5d890e..31085dbde6 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs @@ -147,7 +147,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic newsStatus = newsPanel.Get("NEWS_STATUS"); SetNewsStatus("Loading news"); - if (Game.modData.Manifest.NewsUrl != null) + if (Game.Settings.Game.NewsUrl != null) { var cacheFile = GetNewsCacheFile(); var cacheValid = File.Exists(cacheFile) && DateTime.Today.ToUniversalTime() <= Game.Settings.Game.NewsFetchedDate; @@ -155,7 +155,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic if (cacheValid) DisplayNews(ReadNews(File.ReadAllBytes(cacheFile))); else - new Download(Game.modData.Manifest.NewsUrl, e => { }, NewsDownloadComplete); + new Download(Game.Settings.Game.NewsUrl, e => { }, NewsDownloadComplete); } var newsButton = newsBG.GetOrNull("NEWS_BUTTON"); diff --git a/mods/cnc/mod.yaml b/mods/cnc/mod.yaml index d7f27f1565..b62ceafb47 100644 --- a/mods/cnc/mod.yaml +++ b/mods/cnc/mod.yaml @@ -201,5 +201,3 @@ Missions: mods/cnc/missions.yaml SupportsMapsFrom: cnc - -NewsUrl: http://www.openra.net/gamenews diff --git a/mods/d2k/mod.yaml b/mods/d2k/mod.yaml index e1ef44bebd..e0ede7b273 100644 --- a/mods/d2k/mod.yaml +++ b/mods/d2k/mod.yaml @@ -176,5 +176,3 @@ LuaScripts: mods/common/lua/facing.lua SupportsMapsFrom: d2k - -NewsUrl: http://www.openra.net/gamenews \ No newline at end of file diff --git a/mods/ra/mod.yaml b/mods/ra/mod.yaml index f7b62a84f4..6db8254436 100644 --- a/mods/ra/mod.yaml +++ b/mods/ra/mod.yaml @@ -196,5 +196,3 @@ Missions: mods/ra/missions.yaml SupportsMapsFrom: ra - -NewsUrl: http://www.openra.net/gamenews \ No newline at end of file diff --git a/mods/ts/mod.yaml b/mods/ts/mod.yaml index d9a0aa62e1..3c9a66cbcd 100644 --- a/mods/ts/mod.yaml +++ b/mods/ts/mod.yaml @@ -217,5 +217,3 @@ LuaScripts: mods/common/lua/facing.lua SupportsMapsFrom: ts - -NewsUrl: http://www.openra.net/gamenews From ad375c573430a9fa53d0573ced602b65fdab9191 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 7 Jul 2014 22:23:04 +1200 Subject: [PATCH 2/2] Reorganize news parsing. Fixes #5647. --- OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs | 108 ++++++++---------- 1 file changed, 48 insertions(+), 60 deletions(-) diff --git a/OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs index 31085dbde6..e58e1243ca 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/MainMenuLogic.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Linq; using System.Net; @@ -147,16 +148,15 @@ namespace OpenRA.Mods.RA.Widgets.Logic newsStatus = newsPanel.Get("NEWS_STATUS"); SetNewsStatus("Loading news"); - if (Game.Settings.Game.NewsUrl != null) - { - var cacheFile = GetNewsCacheFile(); - var cacheValid = File.Exists(cacheFile) && DateTime.Today.ToUniversalTime() <= Game.Settings.Game.NewsFetchedDate; + var cacheFile = Path.Combine(Platform.SupportDir, "news.yaml"); + var currentNews = ParseNews(cacheFile); + if (currentNews != null) + DisplayNews(currentNews); - if (cacheValid) - DisplayNews(ReadNews(File.ReadAllBytes(cacheFile))); - else - new Download(Game.Settings.Game.NewsUrl, e => { }, NewsDownloadComplete); - } + // Only query for new stories once per day + var cacheValid = currentNews != null && DateTime.Today.ToUniversalTime() <= Game.Settings.Game.NewsFetchedDate; + if (!cacheValid) + new Download(Game.Settings.Game.NewsUrl, cacheFile, e => { }, (e, c) => NewsDownloadComplete(e, c, cacheFile, currentNews)); var newsButton = newsBG.GetOrNull("NEWS_BUTTON"); newsButton.OnClick = () => @@ -169,13 +169,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic } } - static string GetNewsCacheFile() - { - var cacheDir = Path.Combine(Platform.SupportDir, "Cache", Game.modData.Manifest.Mod.Id); - Directory.CreateDirectory(cacheDir); - return Path.Combine(cacheDir, "news.yaml"); - } - void SetNewsStatus(string message) { message = WidgetUtils.WrapText(message, newsStatus.Bounds.Width, Game.Renderer.Fonts[newsStatus.Font]); @@ -190,20 +183,54 @@ namespace OpenRA.Mods.RA.Widgets.Logic public string Content; } - static IEnumerable ReadNews(byte[] bytes) + NewsItem[] ParseNews(string path) { - var str = Encoding.UTF8.GetString(bytes); + if (!File.Exists(path)) + return null; - return MiniYaml.FromString(str).Select(node => + try { - var nodesDict = node.Value.ToDictionary(); - return new NewsItem + return MiniYaml.FromFile(path).Select(node => + { + var nodesDict = node.Value.ToDictionary(); + return new NewsItem { Title = nodesDict["Title"].Value, Author = nodesDict["Author"].Value, DateTime = FieldLoader.GetValue("DateTime", node.Key), Content = nodesDict["Content"].Value }; + }).ToArray(); + } + catch (Exception ex) + { + SetNewsStatus("Failed to parse news: {0}".F(ex.Message)); + } + + return null; + } + + void NewsDownloadComplete(AsyncCompletedEventArgs e, bool cancelled, string cacheFile, NewsItem[] oldNews) + { + Game.RunAfterTick(() => // run on the main thread + { + if (e.Error != null) + { + SetNewsStatus("Failed to retrieve news: {0}".F(Download.FormatErrorMessage(e.Error))); + return; + } + + var newNews = ParseNews(cacheFile); + if (newNews == null) + return; + + DisplayNews(newNews); + + if (oldNews == null || newNews.Any(n => !oldNews.Select(c => c.DateTime).Contains(n.DateTime))) + newsHighlighted = true; + + Game.Settings.Game.NewsFetchedDate = DateTime.Today.ToUniversalTime(); + Game.Settings.Save(); }); } @@ -237,45 +264,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic } } - void NewsDownloadComplete(DownloadDataCompletedEventArgs e, bool cancelled) - { - Game.RunAfterTick(() => // run on the main thread - { - if (e.Error != null) - { - SetNewsStatus("Failed to retrieve news: {0}".F(Download.FormatErrorMessage(e.Error))); - return; - } - - IEnumerable newNews; - try - { - newNews = ReadNews(e.Result); - DisplayNews(newNews); - } - catch (Exception ex) - { - SetNewsStatus("Failed to retrieve news: {0}".F(ex.Message)); - return; - } - - Game.Settings.Game.NewsFetchedDate = DateTime.Today.ToUniversalTime(); - Game.Settings.Save(); - - var cacheFile = GetNewsCacheFile(); - if (File.Exists(cacheFile)) - { - var oldNews = ReadNews(File.ReadAllBytes(cacheFile)); - if (newNews.Any(n => !oldNews.Select(c => c.DateTime).Contains(n.DateTime))) - newsHighlighted = true; - } - else - newsHighlighted = true; - - File.WriteAllBytes(cacheFile, e.Result); - }); - } - void RemoveShellmapUI() { rootMenu.Parent.RemoveChild(rootMenu);