Read mod registrations from system support dir.

This commit is contained in:
Paul Chote
2017-06-22 19:52:36 +01:00
committed by reaperrr
parent e69f129fed
commit 739f357090
2 changed files with 93 additions and 60 deletions

View File

@@ -16,10 +16,12 @@ using System.Drawing;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA
{
[Flags]
enum ModRegistration { User = 1, System = 2 }
public class ExternalMod
{
public readonly string Id;
@@ -43,12 +45,16 @@ namespace OpenRA
{
sheetBuilder = new SheetBuilder(SheetType.BGRA, 256);
// Load registered mods
var supportPath = Platform.ResolvePath(Path.Combine("^", "ModMetadata"));
if (!Directory.Exists(supportPath))
return;
// If the player has defined a local support directory (in the game directory)
// then this will override both the regular and system support dirs
var sources = new[] { Platform.SystemSupportDir, Platform.SupportDir };
foreach (var source in sources.Distinct())
{
var metadataPath = Path.Combine(source, "ModMetadata");
if (!Directory.Exists(metadataPath))
continue;
foreach (var path in Directory.GetFiles(supportPath, "*.yaml"))
foreach (var path in Directory.GetFiles(metadataPath, "*.yaml"))
{
try
{
@@ -62,6 +68,7 @@ namespace OpenRA
}
}
}
}
void LoadMod(MiniYaml yaml, string path = null)
{
@@ -80,7 +87,7 @@ namespace OpenRA
mods[key] = mod;
}
internal void Register(Manifest mod, string launchPath)
internal void Register(Manifest mod, string launchPath, ModRegistration registration)
{
if (mod.Metadata.Hidden)
return;
@@ -104,15 +111,27 @@ namespace OpenRA
}))
};
var supportPath = Platform.ResolvePath(Path.Combine("^", "ModMetadata"));
var sources = new List<string>();
if (registration.HasFlag(ModRegistration.System))
sources.Add(Platform.SystemSupportDir);
if (registration.HasFlag(ModRegistration.User))
sources.Add(Platform.SupportDir);
try
{
// Make sure the mod is available for this session, even if saving it fails
LoadMod(yaml.First().Value);
Directory.CreateDirectory(supportPath);
File.WriteAllLines(Path.Combine(supportPath, key + ".yaml"), yaml.ToLines(false).ToArray());
foreach (var source in sources.Distinct())
{
if (!Directory.Exists(source))
continue;
var metadataPath = Path.Combine(source, "ModMetadata");
try
{
Directory.CreateDirectory(metadataPath);
File.WriteAllLines(Path.Combine(metadataPath, key + ".yaml"), yaml.ToLines(false).ToArray());
}
catch (Exception e)
{
@@ -120,6 +139,7 @@ namespace OpenRA
Log.Write("debug", e.ToString());
}
}
}
/// <summary>
/// Removes invalid mod registrations:
@@ -128,9 +148,21 @@ namespace OpenRA
/// * Filename doesn't match internal key
/// * Fails to parse as a mod registration
/// </summary>
internal void ClearInvalidRegistrations(ExternalMod activeMod)
internal void ClearInvalidRegistrations(ExternalMod activeMod, ModRegistration registration)
{
var metadataPath = Platform.ResolvePath(Path.Combine("^", "ModMetadata"));
var sources = new List<string>();
if (registration.HasFlag(ModRegistration.System))
sources.Add(Platform.SystemSupportDir);
if (registration.HasFlag(ModRegistration.User))
sources.Add(Platform.SupportDir);
foreach (var source in sources.Distinct())
{
var metadataPath = Path.Combine(source, "ModMetadata");
if (!Directory.Exists(metadataPath))
continue;
foreach (var path in Directory.GetFiles(metadataPath, "*.yaml"))
{
string modKey = null;
@@ -168,6 +200,7 @@ namespace OpenRA
}
}
}
}
public ExternalMod this[string key] { get { return mods[key]; } }
public int Count { get { return mods.Count; } }

View File

@@ -350,11 +350,11 @@ namespace OpenRA
if (launchPath.First() == '"' && launchPath.Last() == '"')
launchPath = launchPath.Substring(1, launchPath.Length - 2);
ExternalMods.Register(Mods[modID], launchPath);
ExternalMods.Register(Mods[modID], launchPath, ModRegistration.User);
ExternalMod activeMod;
if (ExternalMods.TryGetValue(ExternalMod.MakeKey(Mods[modID]), out activeMod))
ExternalMods.ClearInvalidRegistrations(activeMod);
ExternalMods.ClearInvalidRegistrations(activeMod, ModRegistration.User);
}
Console.WriteLine("External mods:");