Add multi-resolution mod icon support.

This commit is contained in:
Paul Chote
2020-02-15 14:13:44 +00:00
committed by abcdefg30
parent e5309ee586
commit fb7c781a66
11 changed files with 66 additions and 26 deletions

View File

@@ -16,6 +16,7 @@ using System.IO;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA
{
@@ -30,6 +31,8 @@ namespace OpenRA
public readonly string LaunchPath;
public readonly string[] LaunchArgs;
public Sprite Icon { get; internal set; }
public Sprite Icon2x { get; internal set; }
public Sprite Icon3x { get; internal set; }
public static string MakeKey(Manifest mod) { return MakeKey(mod.Id, mod.Metadata.Version); }
public static string MakeKey(ExternalMod mod) { return MakeKey(mod.Id, mod.Version); }
@@ -41,9 +44,22 @@ namespace OpenRA
readonly Dictionary<string, ExternalMod> mods = new Dictionary<string, ExternalMod>();
readonly SheetBuilder sheetBuilder;
Sheet CreateSheet()
{
var sheet = new Sheet(SheetType.BGRA, new Size(512, 512));
// 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.
sheet.CreateBuffer();
sheet.GetTexture().ScaleFilter = TextureScaleFilter.Linear;
return sheet;
}
public ExternalMods()
{
sheetBuilder = new SheetBuilder(SheetType.BGRA, 256);
sheetBuilder = new SheetBuilder(SheetType.BGRA, CreateSheet);
// Several types of support directory types are available, depending on
// how the player has installed and launched the game.
@@ -80,10 +96,18 @@ namespace OpenRA
var mod = FieldLoader.Load<ExternalMod>(yaml);
var iconNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Icon");
if (iconNode != null && !string.IsNullOrEmpty(iconNode.Value.Value))
{
using (var stream = new MemoryStream(Convert.FromBase64String(iconNode.Value.Value)))
mod.Icon = sheetBuilder.Add(new Png(stream));
}
var icon2xNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Icon2x");
if (icon2xNode != null && !string.IsNullOrEmpty(icon2xNode.Value.Value))
using (var stream = new MemoryStream(Convert.FromBase64String(icon2xNode.Value.Value)))
mod.Icon2x = sheetBuilder.Add(new Png(stream), 1f / 2);
var icon3xNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Icon3x");
if (icon3xNode != null && !string.IsNullOrEmpty(icon3xNode.Value.Value))
using (var stream = new MemoryStream(Convert.FromBase64String(icon3xNode.Value.Value)))
mod.Icon3x = sheetBuilder.Add(new Png(stream), 1f / 3);
// Avoid possibly overwriting a valid mod with an obviously bogus one
var key = ExternalMod.MakeKey(mod);
@@ -96,24 +120,27 @@ namespace OpenRA
if (mod.Metadata.Hidden)
return;
var iconData = "";
using (var stream = mod.Package.GetStream("icon.png"))
if (stream != null)
iconData = Convert.ToBase64String(stream.ReadAllBytes());
var key = ExternalMod.MakeKey(mod);
var yaml = new List<MiniYamlNode>()
{
new MiniYamlNode("Registration", new MiniYaml("", new List<MiniYamlNode>()
var yaml = new MiniYamlNode("Registration", new MiniYaml("", new List<MiniYamlNode>()
{
new MiniYamlNode("Id", mod.Id),
new MiniYamlNode("Version", mod.Metadata.Version),
new MiniYamlNode("Title", mod.Metadata.Title),
new MiniYamlNode("Icon", iconData),
new MiniYamlNode("LaunchPath", launchPath),
new MiniYamlNode("LaunchArgs", "Game.Mod=" + mod.Id)
}))
};
}));
using (var stream = mod.Package.GetStream("icon.png"))
if (stream != null)
yaml.Value.Nodes.Add(new MiniYamlNode("Icon", Convert.ToBase64String(stream.ReadAllBytes())));
using (var stream = mod.Package.GetStream("icon-2x.png"))
if (stream != null)
yaml.Value.Nodes.Add(new MiniYamlNode("Icon2x", Convert.ToBase64String(stream.ReadAllBytes())));
using (var stream = mod.Package.GetStream("icon-3x.png"))
if (stream != null)
yaml.Value.Nodes.Add(new MiniYamlNode("Icon3x", Convert.ToBase64String(stream.ReadAllBytes())));
var sources = new List<string>();
if (registration.HasFlag(ModRegistration.System))
@@ -131,8 +158,9 @@ namespace OpenRA
}
// Make sure the mod is available for this session, even if saving it fails
LoadMod(yaml.First().Value, forceRegistration: true);
LoadMod(yaml.Value, forceRegistration: true);
var lines = new List<MiniYamlNode> { yaml }.ToLines().ToArray();
foreach (var source in sources.Distinct())
{
var metadataPath = Path.Combine(source, "ModMetadata");
@@ -140,7 +168,7 @@ namespace OpenRA
try
{
Directory.CreateDirectory(metadataPath);
File.WriteAllLines(Path.Combine(metadataPath, key + ".yaml"), yaml.ToLines().ToArray());
File.WriteAllLines(Path.Combine(metadataPath, key + ".yaml"), lines);
}
catch (Exception e)
{

View File

@@ -159,9 +159,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var abortButton = panel.Get<ButtonWidget>("ABORT_BUTTON");
var switchButton = panel.Get<ButtonWidget>("SWITCH_BUTTON");
var modTitle = orderManager.ServerExternalMod.Title;
var modVersion = orderManager.ServerExternalMod.Version;
var modIcon = orderManager.ServerExternalMod.Icon;
var mod = orderManager.ServerExternalMod;
var modTitle = mod.Title;
var modVersion = mod.Version;
switchButton.OnClick = () =>
{
@@ -206,10 +206,22 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
var logo = panel.GetOrNull<RGBASpriteWidget>("MOD_ICON");
if (logo != null && modIcon != null)
logo.GetSprite = () => modIcon;
if (logo != null)
{
logo.GetSprite = () =>
{
var ws = Game.Renderer.WindowScale;
if (ws > 2 && mod.Icon3x != null)
return mod.Icon3x;
if (logo != null && modIcon == null)
if (ws > 1 && mod.Icon2x != null)
return mod.Icon2x;
return mod.Icon;
};
}
if (logo != null && mod.Icon == null)
{
// Hide the logo and center just the text
if (title != null)

BIN
mods/cnc/icon-2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

BIN
mods/cnc/icon-3x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
mods/d2k/icon-2x.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

BIN
mods/d2k/icon-3x.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
mods/ra/icon-2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
mods/ra/icon-3x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

BIN
mods/ts/icon-2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

BIN
mods/ts/icon-3x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB