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

View File

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