Merge pull request #12891 from pchote/register-mods-first-time

Add the current mod to the external mods database on launch.
This commit is contained in:
abcdefg30
2017-03-04 18:51:43 +01:00
committed by GitHub

View File

@@ -16,6 +16,7 @@ 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
{ {
@@ -35,7 +36,7 @@ namespace OpenRA
public class ExternalMods : IReadOnlyDictionary<string, ExternalMod> public class ExternalMods : IReadOnlyDictionary<string, ExternalMod>
{ {
readonly Dictionary<string, ExternalMod> mods; readonly Dictionary<string, ExternalMod> mods = new Dictionary<string, ExternalMod>();
readonly SheetBuilder sheetBuilder; readonly SheetBuilder sheetBuilder;
readonly string launchPath; readonly string launchPath;
@@ -47,31 +48,18 @@ namespace OpenRA
this.launchPath = launchPath; this.launchPath = launchPath;
sheetBuilder = new SheetBuilder(SheetType.BGRA, 256); sheetBuilder = new SheetBuilder(SheetType.BGRA, 256);
mods = LoadMods();
}
Dictionary<string, ExternalMod> LoadMods() // Load registered mods
{
var ret = new Dictionary<string, ExternalMod>();
var supportPath = Platform.ResolvePath(Path.Combine("^", "ModMetadata")); var supportPath = Platform.ResolvePath(Path.Combine("^", "ModMetadata"));
if (!Directory.Exists(supportPath)) if (!Directory.Exists(supportPath))
return ret; return;
foreach (var path in Directory.GetFiles(supportPath, "*.yaml")) foreach (var path in Directory.GetFiles(supportPath, "*.yaml"))
{ {
try try
{ {
var yaml = MiniYaml.FromStream(File.OpenRead(path), path).First().Value; var yaml = MiniYaml.FromStream(File.OpenRead(path), path).First().Value;
var mod = FieldLoader.Load<ExternalMod>(yaml); LoadMod(yaml);
var iconNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Icon");
if (iconNode != null && !string.IsNullOrEmpty(iconNode.Value.Value))
{
using (var stream = new MemoryStream(Convert.FromBase64String(iconNode.Value.Value)))
using (var bitmap = new Bitmap(stream))
mod.Icon = sheetBuilder.Add(bitmap);
}
ret.Add(ExternalMod.MakeKey(mod), mod);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -79,8 +67,20 @@ namespace OpenRA
Log.Write("debug", e.ToString()); Log.Write("debug", e.ToString());
} }
} }
}
return ret; void LoadMod(MiniYaml yaml)
{
var mod = FieldLoader.Load<ExternalMod>(yaml);
var iconNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Icon");
if (iconNode != null && !string.IsNullOrEmpty(iconNode.Value.Value))
{
using (var stream = new MemoryStream(Convert.FromBase64String(iconNode.Value.Value)))
using (var bitmap = new Bitmap(stream))
mod.Icon = sheetBuilder.Add(bitmap);
}
mods.Add(ExternalMod.MakeKey(mod), mod);
} }
internal void Register(Manifest mod) internal void Register(Manifest mod)
@@ -108,9 +108,20 @@ namespace OpenRA
}; };
var supportPath = Platform.ResolvePath(Path.Combine("^", "ModMetadata")); var supportPath = Platform.ResolvePath(Path.Combine("^", "ModMetadata"));
Directory.CreateDirectory(supportPath);
File.WriteAllLines(Path.Combine(supportPath, key + ".yaml"), yaml.ToLines(false).ToArray()); 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());
}
catch (Exception e)
{
Log.Write("debug", "Failed to register currrent mod metadata");
Log.Write("debug", e.ToString());
}
// Clean up stale mod registrations: // Clean up stale mod registrations:
// - LaunchPath no longer exists (uninstalled) // - LaunchPath no longer exists (uninstalled)