16
16
using System . IO ;
17
17
using System . Linq ;
18
18
using OpenRA . Graphics ;
19
+ using OpenRA . Primitives ;
19
20
20
21
namespace OpenRA
21
22
{
@@ -35,7 +36,7 @@ public class ExternalMod
35
36
36
37
public class ExternalMods : IReadOnlyDictionary < string , ExternalMod >
37
38
{
38
- readonly Dictionary < string , ExternalMod > mods ;
39
+ readonly Dictionary < string , ExternalMod > mods = new Dictionary < string , ExternalMod > ( ) ;
39
40
readonly SheetBuilder sheetBuilder ;
40
41
readonly string launchPath ;
41
42
@@ -47,40 +48,39 @@ public ExternalMods(string launchPath)
47
48
48
49
this . launchPath = launchPath ;
49
50
sheetBuilder = new SheetBuilder ( SheetType . BGRA , 256 ) ;
50
- mods = LoadMods ( ) ;
51
- }
52
51
53
- Dictionary < string , ExternalMod > LoadMods ( )
54
- {
55
- var ret = new Dictionary < string , ExternalMod > ( ) ;
52
+ // Load registered mods
56
53
var supportPath = Platform . ResolvePath ( Path . Combine ( "^" , "ModMetadata" ) ) ;
57
54
if ( ! Directory . Exists ( supportPath ) )
58
- return ret ;
55
+ return ;
59
56
60
57
foreach ( var path in Directory . GetFiles ( supportPath , "*.yaml" ) )
61
58
{
62
59
try
63
60
{
64
61
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 ) ;
75
63
}
76
64
catch ( Exception e )
77
65
{
78
66
Log . Write ( "debug" , "Failed to parse mod metadata file '{0}'" , path ) ;
79
67
Log . Write ( "debug" , e . ToString ( ) ) ;
80
68
}
81
69
}
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
+ }
82
82
83
- return ret ;
83
+ mods . Add ( ExternalMod . MakeKey ( mod ) , mod ) ;
84
84
}
85
85
86
86
internal void Register ( Manifest mod )
@@ -108,9 +108,20 @@ internal void Register(Manifest mod)
108
108
} ;
109
109
110
110
var supportPath = Platform . ResolvePath ( Path . Combine ( "^" , "ModMetadata" ) ) ;
111
- Directory . CreateDirectory ( supportPath ) ;
112
111
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
+ }
114
125
115
126
// Clean up stale mod registrations:
116
127
// - LaunchPath no longer exists (uninstalled)
0 commit comments