Apply map upgrade rules without instantiating the Map object.

This avoids a crash that stops the maps from being updated
when the base mod ruleset is not valid (e.g. a trait is missing a
FieldLoader.Required property).  This also significantly improves the
upgrade performance.
This commit is contained in:
Paul Chote
2018-03-19 06:00:26 +00:00
committed by abcdefg30
parent d2ff5b49fd
commit 8ea1da1046
3 changed files with 55 additions and 19 deletions

View File

@@ -122,7 +122,7 @@ namespace OpenRA
} }
} }
public IEnumerable<Map> EnumerateMapsWithoutCaching(MapClassification classification = MapClassification.System) public IEnumerable<IReadWritePackage> EnumerateMapPackagesWithoutCaching(MapClassification classification = MapClassification.System)
{ {
// Utility mod that does not support maps // Utility mod that does not support maps
if (!modData.Manifest.Contains<MapGrid>()) if (!modData.Manifest.Contains<MapGrid>())
@@ -155,14 +155,20 @@ namespace OpenRA
{ {
foreach (var map in package.Contents) foreach (var map in package.Contents)
{ {
var mapPackage = package.OpenPackage(map, modData.ModFiles); var mapPackage = package.OpenPackage(map, modData.ModFiles) as IReadWritePackage;
if (mapPackage != null) if (mapPackage != null)
yield return new Map(modData, mapPackage); yield return mapPackage;
} }
} }
} }
} }
public IEnumerable<Map> EnumerateMapsWithoutCaching(MapClassification classification = MapClassification.System)
{
foreach (var mapPackage in EnumerateMapPackagesWithoutCaching(classification))
yield return new Map(modData, mapPackage);
}
public void QueryRemoteMapDetails(string repositoryUrl, IEnumerable<string> uids, Action<MapPreview> mapDetailsReceived = null, Action queryFailed = null) public void QueryRemoteMapDetails(string repositoryUrl, IEnumerable<string> uids, Action<MapPreview> mapDetailsReceived = null, Action queryFailed = null)
{ {
var maps = uids.Distinct() var maps = uids.Distinct()

View File

@@ -12,6 +12,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using OpenRA.FileSystem; using OpenRA.FileSystem;
@@ -28,7 +29,16 @@ namespace OpenRA.Mods.Common.UtilityCommands
delegate void UpgradeAction(ModData modData, int engineVersion, ref List<MiniYamlNode> nodes, MiniYamlNode parent, int depth); delegate void UpgradeAction(ModData modData, int engineVersion, ref List<MiniYamlNode> nodes, MiniYamlNode parent, int depth);
static void ProcessYaml(ModData modData, Map map, MiniYaml yaml, int engineDate, UpgradeAction processYaml) static Stream Open(string filename, IReadOnlyPackage package, IReadOnlyFileSystem fallback)
{
// Explicit package paths never refer to a map
if (!filename.Contains("|") && package.Contains(filename))
return package.GetStream(filename);
return fallback.Open(filename);
}
static void ProcessYaml(ModData modData, IReadOnlyPackage package, MiniYaml yaml, int engineDate, UpgradeAction processYaml)
{ {
if (yaml == null) if (yaml == null)
return; return;
@@ -38,16 +48,16 @@ namespace OpenRA.Mods.Common.UtilityCommands
var files = FieldLoader.GetValue<string[]>("value", yaml.Value); var files = FieldLoader.GetValue<string[]>("value", yaml.Value);
foreach (var filename in files) foreach (var filename in files)
{ {
var fileNodes = MiniYaml.FromStream(map.Open(filename), filename); var fileNodes = MiniYaml.FromStream(Open(filename, package, modData.DefaultFileSystem), filename);
processYaml(modData, engineDate, ref fileNodes, null, 0); processYaml(modData, engineDate, ref fileNodes, null, 0);
// HACK: Obtain the writable save path using knowledge of the underlying filesystem workings // HACK: Obtain the writable save path using knowledge of the underlying filesystem workings
var packagePath = filename; var packagePath = filename;
var package = map.Package; var p = package;
if (filename.Contains("|")) if (filename.Contains("|"))
modData.DefaultFileSystem.TryGetPackageContaining(filename, out package, out packagePath); modData.DefaultFileSystem.TryGetPackageContaining(filename, out p, out packagePath);
((IReadWritePackage)package).Update(packagePath, Encoding.ASCII.GetBytes(fileNodes.WriteToString())); ((IReadWritePackage)p).Update(packagePath, Encoding.ASCII.GetBytes(fileNodes.WriteToString()));
} }
} }
@@ -65,13 +75,33 @@ namespace OpenRA.Mods.Common.UtilityCommands
return; return;
} }
var map = new Map(modData, package); var mapStream = package.GetStream("map.yaml");
ProcessYaml(modData, map, map.WeaponDefinitions, engineDate, UpgradeRules.UpgradeWeaponRules); if (mapStream == null)
ProcessYaml(modData, map, map.RuleDefinitions, engineDate, UpgradeRules.UpgradeActorRules); return;
ProcessYaml(modData, map, map.SequenceDefinitions, engineDate, UpgradeRules.UpgradeSequences);
UpgradeRules.UpgradePlayers(modData, engineDate, ref map.PlayerDefinitions, null, 0); var yaml = new MiniYaml(null, MiniYaml.FromStream(mapStream, package.Name));
UpgradeRules.UpgradeActors(modData, engineDate, ref map.ActorDefinitions, null, 0);
map.Save(package); var rules = yaml.Nodes.FirstOrDefault(n => n.Key == "Rules");
if (rules != null)
ProcessYaml(modData, package, rules.Value, engineDate, UpgradeRules.UpgradeActorRules);
var weapons = yaml.Nodes.FirstOrDefault(n => n.Key == "Weapons");
if (weapons != null)
ProcessYaml(modData, package, weapons.Value, engineDate, UpgradeRules.UpgradeWeaponRules);
var sequences = yaml.Nodes.FirstOrDefault(n => n.Key == "Sequences");
if (sequences != null)
ProcessYaml(modData, package, sequences.Value, engineDate, UpgradeRules.UpgradeSequences);
var players = yaml.Nodes.FirstOrDefault(n => n.Key == "Players");
if (players != null)
UpgradeRules.UpgradePlayers(modData, engineDate, ref players.Value.Nodes, null, 0);
var actors = yaml.Nodes.FirstOrDefault(n => n.Key == "Actors");
if (actors != null)
UpgradeRules.UpgradeActors(modData, engineDate, ref actors.Value.Nodes, null, 0);
package.Update("map.yaml", Encoding.UTF8.GetBytes(yaml.Nodes.WriteToString()));
} }
[Desc("MAP", "OLDENGINE", "Upgrade map rules to the latest engine version.")] [Desc("MAP", "OLDENGINE", "Upgrade map rules to the latest engine version.")]

View File

@@ -77,16 +77,16 @@ namespace OpenRA.Mods.Common.UtilityCommands
// The map cache won't be valid if there was a map format upgrade, so walk the map packages manually // The map cache won't be valid if there was a map format upgrade, so walk the map packages manually
// Only upgrade system maps - user maps must be updated manually using --upgrade-map // Only upgrade system maps - user maps must be updated manually using --upgrade-map
Console.WriteLine("Processing System Maps:"); Console.WriteLine("Processing System Maps:");
foreach (var map in modData.MapCache.EnumerateMapsWithoutCaching()) foreach (var package in modData.MapCache.EnumerateMapPackagesWithoutCaching())
{ {
try try
{ {
Console.WriteLine(map.Package.Name); Console.WriteLine(package.Name);
UpgradeMapCommand.UpgradeMap(modData, (IReadWritePackage)map.Package, engineDate); UpgradeMapCommand.UpgradeMap(modData, package, engineDate);
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("Failed to upgrade map {0}", map); Console.WriteLine("Failed to upgrade map {0}", package.Name);
Console.WriteLine("Error was: {0}", e.ToString()); Console.WriteLine("Error was: {0}", e.ToString());
} }
} }