Add player badges.

This commit is contained in:
Paul Chote
2018-07-07 17:10:58 +00:00
committed by abcdefg30
parent 7ec19b82e3
commit 6ec93bd8cf
11 changed files with 306 additions and 3 deletions

View File

@@ -9,10 +9,79 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using OpenRA.Graphics;
namespace OpenRA
{
public class PlayerDatabase : IGlobalModData
{
public readonly string Profile = "https://forum.openra.net/openra/info/";
[FieldLoader.Ignore]
readonly object syncObject = new object();
[FieldLoader.Ignore]
readonly Dictionary<string, Sprite> spriteCache = new Dictionary<string, Sprite>();
// 128x128 is large enough for 25 unique 24x24 sprites
[FieldLoader.Ignore]
SheetBuilder sheetBuilder;
public PlayerBadge LoadBadge(MiniYaml yaml)
{
if (sheetBuilder == null)
{
sheetBuilder = new SheetBuilder(SheetType.BGRA, 128);
// We must manually force the buffer creation to avoid a crash
// that is indirectly triggered by rendering from a Sheet that
// has not yet been written to.
sheetBuilder.Current.CreateBuffer();
}
var labelNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Label");
var icon24Node = yaml.Nodes.FirstOrDefault(n => n.Key == "Icon24");
if (labelNode == null || icon24Node == null)
return null;
Sprite sprite;
lock (syncObject)
{
if (!spriteCache.TryGetValue(icon24Node.Value.Value, out sprite))
{
sprite = spriteCache[icon24Node.Value.Value] = sheetBuilder.Allocate(new Size(24, 24));
Action<DownloadDataCompletedEventArgs> onComplete = i =>
{
if (i.Error != null)
return;
try
{
var icon = new Bitmap(new MemoryStream(i.Result));
if (icon.Width == 24 && icon.Height == 24)
{
Game.RunAfterTick(() =>
{
Util.FastCopyIntoSprite(sprite, icon);
sprite.Sheet.CommitBufferedData();
});
}
}
catch { }
};
new Download(icon24Node.Value.Value, _ => { }, onComplete);
}
}
return new PlayerBadge(labelNode.Value.Value, sprite);
}
}
}

View File

@@ -9,6 +9,10 @@
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
namespace OpenRA
{
public class PlayerProfile
@@ -20,5 +24,46 @@ namespace OpenRA
public readonly int ProfileID;
public readonly string ProfileName;
public readonly string ProfileRank = "Registered Player";
[FieldLoader.LoadUsing("LoadBadges")]
public readonly List<PlayerBadge> Badges;
static object LoadBadges(MiniYaml yaml)
{
var badges = new List<PlayerBadge>();
var badgesNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Badges");
if (badgesNode != null)
{
try
{
var playerDatabase = Game.ModData.Manifest.Get<PlayerDatabase>();
foreach (var badgeNode in badgesNode.Value.Nodes)
{
var badge = playerDatabase.LoadBadge(badgeNode.Value);
if (badge != null)
badges.Add(badge);
}
}
catch
{
// Discard badges on error
}
}
return badges;
}
}
public class PlayerBadge
{
public readonly string Label;
public readonly Sprite Icon24;
public PlayerBadge(string label, Sprite icon24)
{
Label = label;
Icon24 = icon24;
}
}
}