Unify static mod metadata collections.

This commit is contained in:
Paul Chote
2016-01-21 17:36:06 +00:00
parent d4aa28e221
commit 62402e0e2e
6 changed files with 34 additions and 44 deletions

View File

@@ -293,7 +293,7 @@ namespace OpenRA
public static bool IsModInstalled(string modId) public static bool IsModInstalled(string modId)
{ {
return Manifest.AllMods[modId].RequiresMods.All(IsModInstalled); return ModMetadata.AllMods[modId].RequiresMods.All(IsModInstalled);
} }
public static bool IsModInstalled(KeyValuePair<string, string> mod) public static bool IsModInstalled(KeyValuePair<string, string> mod)

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using OpenRA.FileSystem;
using OpenRA.Primitives; using OpenRA.Primitives;
namespace OpenRA namespace OpenRA
@@ -32,8 +33,6 @@ namespace OpenRA
/// <summary> Describes what is to be loaded in order to run a mod. </summary> /// <summary> Describes what is to be loaded in order to run a mod. </summary>
public class Manifest public class Manifest
{ {
public static readonly Dictionary<string, Manifest> AllMods = LoadMods();
public readonly ModMetadata Mod; public readonly ModMetadata Mod;
public readonly string[] public readonly string[]
Packages, Rules, ServerTraits, Packages, Rules, ServerTraits,
@@ -60,13 +59,11 @@ namespace OpenRA
readonly TypeDictionary modules = new TypeDictionary(); readonly TypeDictionary modules = new TypeDictionary();
readonly Dictionary<string, MiniYaml> yaml; readonly Dictionary<string, MiniYaml> yaml;
public Manifest(string modId, string modPath = null) public Manifest(string modId)
{ {
if (modPath == null) var package = ModMetadata.AllMods[modId].Package;
modPath = ModMetadata.CandidateModPaths[modId];
var path = Path.Combine(modPath, "mod.yaml"); yaml = new MiniYaml(null, MiniYaml.FromStream(package.GetStream("mod.yaml"))).ToDictionary();
yaml = new MiniYaml(null, MiniYaml.FromFile(path)).ToDictionary();
Mod = FieldLoader.Load<ModMetadata>(yaml["Metadata"]); Mod = FieldLoader.Load<ModMetadata>(yaml["Metadata"]);
Mod.Id = modId; Mod.Id = modId;
@@ -200,28 +197,5 @@ namespace OpenRA
return module; return module;
} }
static Dictionary<string, Manifest> LoadMods()
{
var ret = new Dictionary<string, Manifest>();
foreach (var mod in ModMetadata.CandidateModPaths)
{
if (!File.Exists(Path.Combine(mod.Value, "mod.yaml")))
continue;
try
{
var manifest = new Manifest(mod.Key, mod.Value);
ret.Add(mod.Key, manifest);
}
catch (Exception ex)
{
Log.Write("debug", "An exception occurred while trying to load mod {0}:", mod);
Log.Write("debug", ex.ToString());
}
}
return ret;
}
} }
} }

View File

@@ -12,12 +12,12 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using OpenRA.FileSystem;
namespace OpenRA namespace OpenRA
{ {
public class ModMetadata public class ModMetadata
{ {
public static readonly Dictionary<string, string> CandidateModPaths = GetCandidateMods();
public static readonly Dictionary<string, ModMetadata> AllMods = ValidateMods(); public static readonly Dictionary<string, ModMetadata> AllMods = ValidateMods();
public string Id; public string Id;
@@ -26,26 +26,40 @@ namespace OpenRA
public string Version; public string Version;
public string Author; public string Author;
public bool Hidden; public bool Hidden;
public Dictionary<string, string> RequiresMods;
public ContentInstaller Content; public ContentInstaller Content;
public IReadOnlyPackage Package;
static Dictionary<string, ModMetadata> ValidateMods() static Dictionary<string, ModMetadata> ValidateMods()
{ {
var ret = new Dictionary<string, ModMetadata>(); var ret = new Dictionary<string, ModMetadata>();
foreach (var pair in CandidateModPaths) foreach (var pair in GetCandidateMods())
{ {
try try
{ {
var yamlPath = Path.Combine(pair.Value, "mod.yaml"); IReadOnlyPackage package = null;
if (!File.Exists(yamlPath)) if (Directory.Exists(pair.Value))
package = new Folder(pair.Value);
else
throw new InvalidDataException(pair.Value + " is not a valid mod package");
if (!package.Contains("mod.yaml"))
continue; continue;
var yaml = new MiniYaml(null, MiniYaml.FromFile(yamlPath)); var yaml = new MiniYaml(null, MiniYaml.FromStream(package.GetStream("mod.yaml")));
var nd = yaml.ToDictionary(); var nd = yaml.ToDictionary();
if (!nd.ContainsKey("Metadata")) if (!nd.ContainsKey("Metadata"))
continue; continue;
var metadata = FieldLoader.Load<ModMetadata>(nd["Metadata"]); var metadata = FieldLoader.Load<ModMetadata>(nd["Metadata"]);
metadata.Id = pair.Key; metadata.Id = pair.Key;
metadata.Package = package;
if (nd.ContainsKey("RequiresMods"))
metadata.RequiresMods = nd["RequiresMods"].ToDictionary(my => my.Value);
else
metadata.RequiresMods = new Dictionary<string, string>();
if (nd.ContainsKey("ContentInstaller")) if (nd.ContainsKey("ContentInstaller"))
metadata.Content = FieldLoader.Load<ContentInstaller>(nd["ContentInstaller"]); metadata.Content = FieldLoader.Load<ContentInstaller>(nd["ContentInstaller"]);

View File

@@ -124,7 +124,7 @@ namespace OpenRA
{ {
// Resolve mod package paths. // Resolve mod package paths.
if (ModMetadata.AllMods.ContainsKey(package)) if (ModMetadata.AllMods.ContainsKey(package))
package = ModMetadata.CandidateModPaths[package]; package = ModMetadata.AllMods[package].Package.Name;
return ResolvePath(Path.Combine(package, target)); return ResolvePath(Path.Combine(package, target));
} }

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ {
var panel = widget.Get("INSTALL_MOD_PANEL"); var panel = widget.Get("INSTALL_MOD_PANEL");
var mods = Manifest.AllMods[modId].RequiresMods.Where(m => !Game.IsModInstalled(m)).Select(m => "{0} ({1})".F(m.Key, m.Value)); var mods = ModMetadata.AllMods[modId].RequiresMods.Where(m => !Game.IsModInstalled(m)).Select(m => "{0} ({1})".F(m.Key, m.Value));
var text = string.Join(", ", mods); var text = string.Join(", ", mods);
panel.Get<LabelWidget>("MOD_LIST").Text = text; panel.Get<LabelWidget>("MOD_LIST").Text = text;

View File

@@ -82,7 +82,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ {
try try
{ {
using (var preview = new Bitmap(Platform.ResolvePath(ModMetadata.CandidateModPaths[mod.Id], "preview.png"))) using (var stream = ModMetadata.AllMods[mod.Id].Package.GetStream("preview.png"))
using (var preview = new Bitmap(stream))
if (preview.Width == 296 && preview.Height == 196) if (preview.Width == 296 && preview.Height == 196)
previews.Add(mod.Id, sheetBuilder.Add(preview)); previews.Add(mod.Id, sheetBuilder.Add(preview));
} }
@@ -90,7 +91,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
try try
{ {
using (var logo = new Bitmap(Platform.ResolvePath(ModMetadata.CandidateModPaths[mod.Id], "logo.png"))) using (var stream = ModMetadata.AllMods[mod.Id].Package.GetStream("logo.png"))
using (var logo = new Bitmap(stream))
if (logo.Width == 96 && logo.Height == 96) if (logo.Width == 96 && logo.Height == 96)
logos.Add(mod.Id, sheetBuilder.Add(logo)); logos.Add(mod.Id, sheetBuilder.Add(logo));
} }