Skip to content

Commit 5d220e3

Browse files
authored
Merge pull request OpenRA#12891 from pchote/register-mods-first-time
Add the current mod to the external mods database on launch.
2 parents 72ad76d + 1dddb43 commit 5d220e3

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

OpenRA.Game/ExternalMods.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.IO;
1717
using System.Linq;
1818
using OpenRA.Graphics;
19+
using OpenRA.Primitives;
1920

2021
namespace OpenRA
2122
{
@@ -35,7 +36,7 @@ public class ExternalMod
3536

3637
public class ExternalMods : IReadOnlyDictionary<string, ExternalMod>
3738
{
38-
readonly Dictionary<string, ExternalMod> mods;
39+
readonly Dictionary<string, ExternalMod> mods = new Dictionary<string, ExternalMod>();
3940
readonly SheetBuilder sheetBuilder;
4041
readonly string launchPath;
4142

@@ -47,40 +48,39 @@ public ExternalMods(string launchPath)
4748

4849
this.launchPath = launchPath;
4950
sheetBuilder = new SheetBuilder(SheetType.BGRA, 256);
50-
mods = LoadMods();
51-
}
5251

53-
Dictionary<string, ExternalMod> LoadMods()
54-
{
55-
var ret = new Dictionary<string, ExternalMod>();
52+
// Load registered mods
5653
var supportPath = Platform.ResolvePath(Path.Combine("^", "ModMetadata"));
5754
if (!Directory.Exists(supportPath))
58-
return ret;
55+
return;
5956

6057
foreach (var path in Directory.GetFiles(supportPath, "*.yaml"))
6158
{
6259
try
6360
{
6461
var yaml = MiniYaml.FromStream(File.OpenRead(path), path).First().Value;
65-
var mod = FieldLoader.Load<ExternalMod>(yaml);
66-
var iconNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Icon");
67-
if (iconNode != null && !string.IsNullOrEmpty(iconNode.Value.Value))
68-
{
69-
using (var stream = new MemoryStream(Convert.FromBase64String(iconNode.Value.Value)))
70-
using (var bitmap = new Bitmap(stream))
71-
mod.Icon = sheetBuilder.Add(bitmap);
72-
}
73-
74-
ret.Add(ExternalMod.MakeKey(mod), mod);
62+
LoadMod(yaml);
7563
}
7664
catch (Exception e)
7765
{
7866
Log.Write("debug", "Failed to parse mod metadata file '{0}'", path);
7967
Log.Write("debug", e.ToString());
8068
}
8169
}
70+
}
71+
72+
void LoadMod(MiniYaml yaml)
73+
{
74+
var mod = FieldLoader.Load<ExternalMod>(yaml);
75+
var iconNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Icon");
76+
if (iconNode != null && !string.IsNullOrEmpty(iconNode.Value.Value))
77+
{
78+
using (var stream = new MemoryStream(Convert.FromBase64String(iconNode.Value.Value)))
79+
using (var bitmap = new Bitmap(stream))
80+
mod.Icon = sheetBuilder.Add(bitmap);
81+
}
8282

83-
return ret;
83+
mods.Add(ExternalMod.MakeKey(mod), mod);
8484
}
8585

8686
internal void Register(Manifest mod)
@@ -108,9 +108,20 @@ internal void Register(Manifest mod)
108108
};
109109

110110
var supportPath = Platform.ResolvePath(Path.Combine("^", "ModMetadata"));
111-
Directory.CreateDirectory(supportPath);
112111

113-
File.WriteAllLines(Path.Combine(supportPath, key + ".yaml"), yaml.ToLines(false).ToArray());
112+
try
113+
{
114+
// Make sure the mod is available for this session, even if saving it fails
115+
LoadMod(yaml.First().Value);
116+
117+
Directory.CreateDirectory(supportPath);
118+
File.WriteAllLines(Path.Combine(supportPath, key + ".yaml"), yaml.ToLines(false).ToArray());
119+
}
120+
catch (Exception e)
121+
{
122+
Log.Write("debug", "Failed to register currrent mod metadata");
123+
Log.Write("debug", e.ToString());
124+
}
114125

115126
// Clean up stale mod registrations:
116127
// - LaunchPath no longer exists (uninstalled)

0 commit comments

Comments
 (0)