Add GetCandidateMods() method to ModMetadata to preload all folders that may contain a mod

This allows for loading of mods from both the game's mods directory and the support directory
This commit is contained in:
Pavel Penev
2015-09-30 02:32:34 +03:00
parent 778492840f
commit 71c2d6b9d7
2 changed files with 36 additions and 23 deletions

View File

@@ -61,13 +61,16 @@ 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 mod) public Manifest(string modId, string modPath = null)
{ {
var path = Platform.ResolvePath(".", "mods", mod, "mod.yaml"); if (modPath == null)
modPath = ModMetadata.CandidateModPaths[modId];
var path = Path.Combine(modPath, "mod.yaml");
yaml = new MiniYaml(null, MiniYaml.FromFile(path)).ToDictionary(); yaml = new MiniYaml(null, MiniYaml.FromFile(path)).ToDictionary();
Mod = FieldLoader.Load<ModMetadata>(yaml["Metadata"]); Mod = FieldLoader.Load<ModMetadata>(yaml["Metadata"]);
Mod.Id = mod; Mod.Id = modId;
// TODO: Use fieldloader // TODO: Use fieldloader
Folders = YamlList(yaml, "Folders", true); Folders = YamlList(yaml, "Folders", true);
@@ -202,21 +205,16 @@ namespace OpenRA
static Dictionary<string, Manifest> LoadMods() static Dictionary<string, Manifest> LoadMods()
{ {
// Get mods that are in the game folder.
var basePath = Platform.ResolvePath(".", "mods");
var mods = Directory.GetDirectories(basePath)
.Select(x => x.Substring(basePath.Length + 1));
var ret = new Dictionary<string, Manifest>(); var ret = new Dictionary<string, Manifest>();
foreach (var mod in mods) foreach (var mod in ModMetadata.CandidateModPaths)
{ {
if (!File.Exists(Platform.ResolvePath(".", "mods", mod, "mod.yaml"))) if (!File.Exists(Path.Combine(mod.Value, "mod.yaml")))
continue; continue;
try try
{ {
var manifest = new Manifest(mod); var manifest = new Manifest(mod.Key, mod.Value);
ret.Add(mod, manifest); ret.Add(mod.Key, manifest);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -17,6 +17,7 @@ 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;
@@ -29,16 +30,12 @@ namespace OpenRA
static Dictionary<string, ModMetadata> ValidateMods() static Dictionary<string, ModMetadata> ValidateMods()
{ {
var basePath = Platform.ResolvePath(".", "mods");
var mods = Directory.GetDirectories(basePath)
.Select(x => x.Substring(basePath.Length + 1));
var ret = new Dictionary<string, ModMetadata>(); var ret = new Dictionary<string, ModMetadata>();
foreach (var m in mods) foreach (var pair in CandidateModPaths)
{ {
try try
{ {
var yamlPath = Platform.ResolvePath(".", "mods", m, "mod.yaml"); var yamlPath = Path.Combine(pair.Value, "mod.yaml");
if (!File.Exists(yamlPath)) if (!File.Exists(yamlPath))
continue; continue;
@@ -47,22 +44,40 @@ namespace OpenRA
if (!nd.ContainsKey("Metadata")) if (!nd.ContainsKey("Metadata"))
continue; continue;
var mod = FieldLoader.Load<ModMetadata>(nd["Metadata"]); var metadata = FieldLoader.Load<ModMetadata>(nd["Metadata"]);
mod.Id = m; metadata.Id = pair.Key;
if (nd.ContainsKey("ContentInstaller")) if (nd.ContainsKey("ContentInstaller"))
mod.Content = FieldLoader.Load<ContentInstaller>(nd["ContentInstaller"]); metadata.Content = FieldLoader.Load<ContentInstaller>(nd["ContentInstaller"]);
ret.Add(m, mod); ret.Add(pair.Key, metadata);
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("An exception occurred when trying to load ModMetadata for `{0}`:".F(m)); Console.WriteLine("An exception occurred when trying to load ModMetadata for `{0}`:".F(pair.Key));
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
} }
} }
return ret; return ret;
} }
static Dictionary<string, string> GetCandidateMods()
{
// Get mods that are in the game folder.
var basePath = Platform.ResolvePath(Path.Combine(".", "mods"));
var mods = Directory.GetDirectories(basePath)
.ToDictionary(x => x.Substring(basePath.Length + 1));
// Get mods that are in the support folder.
var supportPath = Platform.ResolvePath(Path.Combine("^", "mods"));
if (!Directory.Exists(supportPath))
return mods;
foreach (var pair in Directory.GetDirectories(supportPath).ToDictionary(x => x.Substring(supportPath.Length + 1)))
mods.Add(pair.Key, pair.Value);
return mods;
}
} }
} }