Merge pull request #5860 from pchote/news-fix

News fixes
This commit is contained in:
Matthias Mailänder
2014-07-07 13:25:25 +02:00
7 changed files with 49 additions and 72 deletions

View File

@@ -35,7 +35,6 @@ namespace OpenRA
public readonly InstallData ContentInstaller; public readonly InstallData ContentInstaller;
public readonly Dictionary<string, Pair<string, int>> Fonts; public readonly Dictionary<string, Pair<string, int>> Fonts;
public readonly Size TileSize = new Size(24, 24); public readonly Size TileSize = new Size(24, 24);
public readonly string NewsUrl;
public readonly TileShape TileShape = TileShape.Rectangle; public readonly TileShape TileShape = TileShape.Rectangle;
public Manifest(string mod) public Manifest(string mod)
@@ -97,9 +96,6 @@ namespace OpenRA
compat.Add(c.Trim()); compat.Add(c.Trim());
MapCompatibility = compat.ToArray(); MapCompatibility = compat.ToArray();
if (yaml.ContainsKey("NewsUrl"))
NewsUrl = yaml["NewsUrl"].Value;
} }
static string[] YamlList(Dictionary<string, MiniYaml> yaml, string key) static string[] YamlList(Dictionary<string, MiniYaml> yaml, string key)

View File

@@ -141,6 +141,7 @@ namespace OpenRA
public string MapRepository = "http://resource.openra.net/map/"; public string MapRepository = "http://resource.openra.net/map/";
public bool FetchNews = true; public bool FetchNews = true;
public string NewsUrl = "http://www.openra.net/gamenews";
public DateTime NewsFetchedDate; public DateTime NewsFetchedDate;
} }

View File

@@ -10,6 +10,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@@ -147,16 +148,15 @@ namespace OpenRA.Mods.RA.Widgets.Logic
newsStatus = newsPanel.Get<LabelWidget>("NEWS_STATUS"); newsStatus = newsPanel.Get<LabelWidget>("NEWS_STATUS");
SetNewsStatus("Loading news"); SetNewsStatus("Loading news");
if (Game.modData.Manifest.NewsUrl != null) var cacheFile = Path.Combine(Platform.SupportDir, "news.yaml");
{ var currentNews = ParseNews(cacheFile);
var cacheFile = GetNewsCacheFile(); if (currentNews != null)
var cacheValid = File.Exists(cacheFile) && DateTime.Today.ToUniversalTime() <= Game.Settings.Game.NewsFetchedDate; DisplayNews(currentNews);
if (cacheValid) // Only query for new stories once per day
DisplayNews(ReadNews(File.ReadAllBytes(cacheFile))); var cacheValid = currentNews != null && DateTime.Today.ToUniversalTime() <= Game.Settings.Game.NewsFetchedDate;
else if (!cacheValid)
new Download(Game.modData.Manifest.NewsUrl, e => { }, NewsDownloadComplete); new Download(Game.Settings.Game.NewsUrl, cacheFile, e => { }, (e, c) => NewsDownloadComplete(e, c, cacheFile, currentNews));
}
var newsButton = newsBG.GetOrNull<DropDownButtonWidget>("NEWS_BUTTON"); var newsButton = newsBG.GetOrNull<DropDownButtonWidget>("NEWS_BUTTON");
newsButton.OnClick = () => 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) void SetNewsStatus(string message)
{ {
message = WidgetUtils.WrapText(message, newsStatus.Bounds.Width, Game.Renderer.Fonts[newsStatus.Font]); message = WidgetUtils.WrapText(message, newsStatus.Bounds.Width, Game.Renderer.Fonts[newsStatus.Font]);
@@ -190,11 +183,14 @@ namespace OpenRA.Mods.RA.Widgets.Logic
public string Content; public string Content;
} }
static IEnumerable<NewsItem> 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
{
return MiniYaml.FromFile(path).Select(node =>
{ {
var nodesDict = node.Value.ToDictionary(); var nodesDict = node.Value.ToDictionary();
return new NewsItem return new NewsItem
@@ -204,6 +200,37 @@ namespace OpenRA.Mods.RA.Widgets.Logic
DateTime = FieldLoader.GetValue<DateTime>("DateTime", node.Key), DateTime = FieldLoader.GetValue<DateTime>("DateTime", node.Key),
Content = nodesDict["Content"].Value 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<NewsItem> 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() void RemoveShellmapUI()
{ {
rootMenu.Parent.RemoveChild(rootMenu); rootMenu.Parent.RemoveChild(rootMenu);

View File

@@ -201,5 +201,3 @@ Missions:
mods/cnc/missions.yaml mods/cnc/missions.yaml
SupportsMapsFrom: cnc SupportsMapsFrom: cnc
NewsUrl: http://www.openra.net/gamenews

View File

@@ -176,5 +176,3 @@ LuaScripts:
mods/common/lua/facing.lua mods/common/lua/facing.lua
SupportsMapsFrom: d2k SupportsMapsFrom: d2k
NewsUrl: http://www.openra.net/gamenews

View File

@@ -196,5 +196,3 @@ Missions:
mods/ra/missions.yaml mods/ra/missions.yaml
SupportsMapsFrom: ra SupportsMapsFrom: ra
NewsUrl: http://www.openra.net/gamenews

View File

@@ -219,5 +219,3 @@ LuaScripts:
mods/common/lua/facing.lua mods/common/lua/facing.lua
SupportsMapsFrom: ts SupportsMapsFrom: ts
NewsUrl: http://www.openra.net/gamenews