Unify static mod metadata collections.
This commit is contained in:
@@ -293,7 +293,7 @@ namespace OpenRA
|
||||
|
||||
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)
|
||||
|
||||
@@ -12,6 +12,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using OpenRA.FileSystem;
|
||||
using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA
|
||||
@@ -32,8 +33,6 @@ namespace OpenRA
|
||||
/// <summary> Describes what is to be loaded in order to run a mod. </summary>
|
||||
public class Manifest
|
||||
{
|
||||
public static readonly Dictionary<string, Manifest> AllMods = LoadMods();
|
||||
|
||||
public readonly ModMetadata Mod;
|
||||
public readonly string[]
|
||||
Packages, Rules, ServerTraits,
|
||||
@@ -60,13 +59,11 @@ namespace OpenRA
|
||||
readonly TypeDictionary modules = new TypeDictionary();
|
||||
readonly Dictionary<string, MiniYaml> yaml;
|
||||
|
||||
public Manifest(string modId, string modPath = null)
|
||||
public Manifest(string modId)
|
||||
{
|
||||
if (modPath == null)
|
||||
modPath = ModMetadata.CandidateModPaths[modId];
|
||||
var package = ModMetadata.AllMods[modId].Package;
|
||||
|
||||
var path = Path.Combine(modPath, "mod.yaml");
|
||||
yaml = new MiniYaml(null, MiniYaml.FromFile(path)).ToDictionary();
|
||||
yaml = new MiniYaml(null, MiniYaml.FromStream(package.GetStream("mod.yaml"))).ToDictionary();
|
||||
|
||||
Mod = FieldLoader.Load<ModMetadata>(yaml["Metadata"]);
|
||||
Mod.Id = modId;
|
||||
@@ -200,28 +197,5 @@ namespace OpenRA
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,12 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using OpenRA.FileSystem;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
public class ModMetadata
|
||||
{
|
||||
public static readonly Dictionary<string, string> CandidateModPaths = GetCandidateMods();
|
||||
public static readonly Dictionary<string, ModMetadata> AllMods = ValidateMods();
|
||||
|
||||
public string Id;
|
||||
@@ -26,26 +26,40 @@ namespace OpenRA
|
||||
public string Version;
|
||||
public string Author;
|
||||
public bool Hidden;
|
||||
|
||||
public Dictionary<string, string> RequiresMods;
|
||||
public ContentInstaller Content;
|
||||
public IReadOnlyPackage Package;
|
||||
|
||||
static Dictionary<string, ModMetadata> ValidateMods()
|
||||
{
|
||||
var ret = new Dictionary<string, ModMetadata>();
|
||||
foreach (var pair in CandidateModPaths)
|
||||
foreach (var pair in GetCandidateMods())
|
||||
{
|
||||
try
|
||||
{
|
||||
var yamlPath = Path.Combine(pair.Value, "mod.yaml");
|
||||
if (!File.Exists(yamlPath))
|
||||
IReadOnlyPackage package = null;
|
||||
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;
|
||||
|
||||
var yaml = new MiniYaml(null, MiniYaml.FromFile(yamlPath));
|
||||
var yaml = new MiniYaml(null, MiniYaml.FromStream(package.GetStream("mod.yaml")));
|
||||
var nd = yaml.ToDictionary();
|
||||
if (!nd.ContainsKey("Metadata"))
|
||||
continue;
|
||||
|
||||
var metadata = FieldLoader.Load<ModMetadata>(nd["Metadata"]);
|
||||
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"))
|
||||
metadata.Content = FieldLoader.Load<ContentInstaller>(nd["ContentInstaller"]);
|
||||
|
||||
@@ -124,7 +124,7 @@ namespace OpenRA
|
||||
{
|
||||
// Resolve mod package paths.
|
||||
if (ModMetadata.AllMods.ContainsKey(package))
|
||||
package = ModMetadata.CandidateModPaths[package];
|
||||
package = ModMetadata.AllMods[package].Package.Name;
|
||||
|
||||
return ResolvePath(Path.Combine(package, target));
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
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);
|
||||
panel.Get<LabelWidget>("MOD_LIST").Text = text;
|
||||
|
||||
|
||||
@@ -82,7 +82,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
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)
|
||||
previews.Add(mod.Id, sheetBuilder.Add(preview));
|
||||
}
|
||||
@@ -90,7 +91,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
|
||||
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)
|
||||
logos.Add(mod.Id, sheetBuilder.Add(logo));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user