Implement new mod/map updater framework.

This commit is contained in:
Paul Chote
2018-03-21 20:58:50 +00:00
committed by reaperrr
parent 7552afeb16
commit ea68f1abb9
15 changed files with 1006 additions and 1257 deletions

View File

@@ -0,0 +1,121 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.FileSystem;
using OpenRA.Mods.Common.UpdateRules;
namespace OpenRA.Mods.Common.UtilityCommands
{
using YamlFileSet = List<Tuple<IReadWritePackage, string, List<MiniYamlNode>>>;
class UpdateMapCommand : IUtilityCommand
{
string IUtilityCommand.Name { get { return "--update-map"; } }
bool IUtilityCommand.ValidateArguments(string[] args)
{
return args.Length >= 2;
}
[Desc("MAP SOURCE [--detailed] [--apply]", "Updates a map to the latest engine version. SOURCE is either a known tag or the name of an update rule.")]
void IUtilityCommand.Run(Utility utility, string[] args)
{
// HACK: The engine code assumes that Game.modData is set.
var modData = Game.ModData = utility.ModData;
// HACK: We know that maps can only be oramap or folders, which are ReadWrite
var package = new Folder(".").OpenPackage(args[1], modData.ModFiles) as IReadWritePackage;
if (package == null)
throw new FileNotFoundException(args[1]);
IEnumerable<UpdateRule> rules = null;
if (args.Length > 1)
rules = UpdatePath.FromSource(modData.ObjectCreator, args[2]);
if (rules == null)
{
Console.WriteLine("--update-map MAP SOURCE [--detailed] [--apply]");
if (args.Length > 2)
Console.WriteLine("Unknown source: " + args[2]);
Console.WriteLine("Valid sources are:");
// Print known tags
Console.WriteLine(" Update Paths:");
foreach (var p in UpdatePath.KnownPaths)
Console.WriteLine(" " + p);
// Print known rules
Console.WriteLine(" Individual Rules:");
foreach (var r in UpdatePath.KnownRules(modData.ObjectCreator))
Console.WriteLine(" " + r);
return;
}
if (args.Contains("--apply"))
ApplyRules(modData, package, rules);
else
UpdateModCommand.PrintSummary(rules, args.Contains("--detailed"));
}
static void ApplyRules(ModData modData, IReadWritePackage mapPackage, IEnumerable<UpdateRule> rules)
{
foreach (var rule in rules)
{
Console.WriteLine("{0}: {1}", rule.GetType().Name, rule.Name);
var mapFiles = new YamlFileSet();
var manualSteps = new List<string>();
Console.Write(" Updating map... ");
try
{
manualSteps = UpdateUtils.UpdateMap(modData, mapPackage, rule, out mapFiles);
}
catch (Exception ex)
{
Console.WriteLine("FAILED");
Console.WriteLine();
Console.WriteLine(" The automated changes for this rule were not applied because of an error.");
Console.WriteLine(" After the issue reported below is resolved you should run the updater");
Console.WriteLine(" with SOURCE set to {0} to retry these changes", rule.GetType().Name);
Console.WriteLine();
Console.WriteLine(" The exception reported was:");
Console.WriteLine(" " + ex.ToString().Replace("\n", "\n "));
continue;
}
// Files are saved after each successful automated rule update
mapFiles.Save();
Console.WriteLine("COMPLETE");
if (manualSteps.Any())
{
Console.WriteLine(" Manual changes are required to complete this update:");
foreach (var manualStep in manualSteps)
Console.WriteLine(" * " + manualStep.Replace("\n", "\n "));
}
Console.WriteLine();
}
Console.WriteLine("Semi-automated update complete.");
Console.WriteLine("Please review the messages above for any manual actions that must be applied.");
}
}
}

View File

@@ -0,0 +1,206 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileSystem;
using OpenRA.Mods.Common.UpdateRules;
namespace OpenRA.Mods.Common.UtilityCommands
{
using YamlFileSet = List<Tuple<IReadWritePackage, string, List<MiniYamlNode>>>;
class UpdateModCommand : IUtilityCommand
{
string IUtilityCommand.Name { get { return "--update-mod"; } }
bool IUtilityCommand.ValidateArguments(string[] args) { return true; }
[Desc("SOURCE [--detailed] [--apply] [--skip-maps]", "Updates a mod to the latest version. SOURCE is either a known tag or the name of an update rule.")]
void IUtilityCommand.Run(Utility utility, string[] args)
{
// HACK: The engine code assumes that Game.modData is set.
var modData = Game.ModData = utility.ModData;
IEnumerable<UpdateRule> rules = null;
if (args.Length > 1)
rules = UpdatePath.FromSource(modData.ObjectCreator, args[1]);
if (rules == null)
{
Console.WriteLine("--update-mod SOURCE [--detailed] [--apply] [--skip-maps]");
if (args.Length > 1)
Console.WriteLine("Unknown source: " + args[1]);
Console.WriteLine("Valid sources are:");
// Print known tags
Console.WriteLine(" Update Paths:");
foreach (var p in UpdatePath.KnownPaths)
Console.WriteLine(" " + p);
// Print known rules
Console.WriteLine(" Individual Rules:");
foreach (var r in UpdatePath.KnownRules(modData.ObjectCreator))
Console.WriteLine(" " + r);
return;
}
if (args.Contains("--apply"))
{
if (!args.Contains("--yes"))
{
Console.WriteLine("WARNING: This command will automatically rewrite your mod rules.");
Console.WriteLine("Side effects of this command may include changing the whitespace to ");
Console.WriteLine("match the default conventions, and any yaml comments will be removed.");
Console.WriteLine();
Console.WriteLine("We strongly recommend that you have a backup of your mod rules, and ");
Console.WriteLine("for best results, to use a Git client to review the line-by-line ");
Console.WriteLine("changes and discard any unwanted side effects.");
Console.WriteLine();
Console.Write("Press y to continue, or any other key to cancel: ");
var confirmKey = Console.ReadKey().KeyChar;
Console.WriteLine();
if (confirmKey != 'y')
{
Console.WriteLine("Update cancelled.");
return;
}
}
ApplyRules(modData, rules, args.Contains("--skip-maps"));
}
else
PrintSummary(rules, args.Contains("--detailed"));
}
public static void PrintSummary(IEnumerable<UpdateRule> rules, bool detailed)
{
var count = rules.Count();
if (count == 1)
Console.WriteLine("Found 1 API change:");
else
Console.WriteLine("Found {0} API changes:", count);
foreach (var rule in rules)
{
Console.WriteLine(" * {0}: {1}", rule.GetType().Name, rule.Name);
if (detailed)
{
Console.WriteLine(" " + rule.Description.Replace("\n", "\n "));
Console.WriteLine();
}
}
if (!detailed)
{
Console.WriteLine();
Console.WriteLine("Run this command with the --detailed flag to view detailed information on each change.");
}
Console.WriteLine("Run this command with the --apply flag to apply the update rules.");
}
static void ApplyRules(ModData modData, IEnumerable<UpdateRule> rules, bool skipMaps)
{
Console.WriteLine();
foreach (var rule in rules)
{
var manualSteps = new List<string>();
var allFiles = new YamlFileSet();
Console.WriteLine("{0}: {1}", rule.GetType().Name, rule.Name);
try
{
Console.Write(" Updating mod... ");
manualSteps.AddRange(UpdateUtils.UpdateMod(modData, rule, out allFiles));
Console.WriteLine("COMPLETE");
}
catch (Exception ex)
{
Console.WriteLine("FAILED");
Console.WriteLine();
Console.WriteLine(" The automated changes for this rule were not applied because of an error.");
Console.WriteLine(" After the issue reported below is resolved you should run the updater");
Console.WriteLine(" with SOURCE set to {0} to retry these changes", rule.GetType().Name);
Console.WriteLine();
Console.WriteLine(" The exception reported was:");
Console.WriteLine(" " + ex.ToString().Replace("\n", "\n "));
continue;
}
Console.Write(" Updating system maps... ");
if (!skipMaps)
{
var mapsFailed = false;
foreach (var package in modData.MapCache.EnumerateMapPackagesWithoutCaching())
{
try
{
YamlFileSet mapFiles;
var mapSteps = UpdateUtils.UpdateMap(modData, package, rule, out mapFiles);
allFiles.AddRange(mapFiles);
if (mapSteps.Any())
manualSteps.Add("Map: " + package.Name + ":\n" + UpdateUtils.FormatMessageList(mapSteps));
}
catch (Exception ex)
{
Console.WriteLine("FAILED");
Console.WriteLine();
Console.WriteLine(" The automated changes for this rule were not applied because of an error.");
Console.WriteLine(" After the issue reported below is resolved you should run the updater");
Console.WriteLine(" with SOURCE set to {0} to retry these changes", rule.GetType().Name);
Console.WriteLine();
Console.WriteLine(" The map that caused the error was:");
Console.WriteLine(" " + package.Name);
Console.WriteLine();
Console.WriteLine(" The exception reported was:");
Console.WriteLine(" " + ex.ToString().Replace("\n", "\n "));
mapsFailed = true;
break;
}
}
if (mapsFailed)
continue;
Console.WriteLine("COMPLETE");
}
else
Console.WriteLine("SKIPPED");
// Files are saved after each successful automated rule update
allFiles.Save();
if (manualSteps.Any())
{
Console.WriteLine(" Manual changes are required to complete this update:");
Console.WriteLine(UpdateUtils.FormatMessageList(manualSteps, 1));
}
Console.WriteLine();
}
Console.WriteLine("Semi-automated update complete.");
Console.WriteLine("Please review the messages above for any manual actions that must be applied.");
}
}
}

View File

@@ -1,122 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using OpenRA.FileSystem;
namespace OpenRA.Mods.Common.UtilityCommands
{
class UpgradeMapCommand : IUtilityCommand
{
string IUtilityCommand.Name { get { return "--upgrade-map"; } }
bool IUtilityCommand.ValidateArguments(string[] args)
{
return args.Length >= 3;
}
delegate void UpgradeAction(ModData modData, int engineVersion, ref List<MiniYamlNode> nodes, MiniYamlNode parent, int depth);
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)
return;
if (yaml.Value != null)
{
var files = FieldLoader.GetValue<string[]>("value", yaml.Value);
foreach (var filename in files)
{
var fileNodes = MiniYaml.FromStream(Open(filename, package, modData.DefaultFileSystem), filename);
processYaml(modData, engineDate, ref fileNodes, null, 0);
// HACK: Obtain the writable save path using knowledge of the underlying filesystem workings
var packagePath = filename;
var p = package;
if (filename.Contains("|"))
modData.DefaultFileSystem.TryGetPackageContaining(filename, out p, out packagePath);
((IReadWritePackage)p).Update(packagePath, Encoding.ASCII.GetBytes(fileNodes.WriteToString()));
}
}
processYaml(modData, engineDate, ref yaml.Nodes, null, 1);
}
public static void UpgradeMap(ModData modData, IReadWritePackage package, int engineDate)
{
UpgradeRules.UpgradeMapFormat(modData, package);
if (engineDate < UpgradeRules.MinimumSupportedVersion)
{
Console.WriteLine("Unsupported engine version. Use the release-{0} utility to update to that version, and then try again",
UpgradeRules.MinimumSupportedVersion);
return;
}
var mapStream = package.GetStream("map.yaml");
if (mapStream == null)
return;
var yaml = new MiniYaml(null, MiniYaml.FromStream(mapStream, package.Name));
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.")]
void IUtilityCommand.Run(Utility utility, string[] args)
{
// HACK: The engine code assumes that Game.modData is set.
var modData = Game.ModData = utility.ModData;
// HACK: We know that maps can only be oramap or folders, which are ReadWrite
var package = new Folder(".").OpenPackage(args[1], modData.ModFiles) as IReadWritePackage;
if (package == null)
throw new FileNotFoundException(args[1]);
var engineDate = Exts.ParseIntegerInvariant(args[2]);
UpgradeMap(modData, package, engineDate);
}
}
}

View File

@@ -1,95 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.FileSystem;
namespace OpenRA.Mods.Common.UtilityCommands
{
class UpgradeModCommand : IUtilityCommand
{
string IUtilityCommand.Name { get { return "--upgrade-mod"; } }
bool IUtilityCommand.ValidateArguments(string[] args)
{
return args.Length >= 2;
}
delegate void UpgradeAction(ModData modData, int engineVersion, ref List<MiniYamlNode> nodes, MiniYamlNode parent, int depth);
void ProcessYaml(string type, IEnumerable<string> files, ModData modData, int engineDate, UpgradeAction processFile)
{
Console.WriteLine("Processing {0}:", type);
foreach (var filename in files)
{
Console.WriteLine("\t" + filename);
string name;
IReadOnlyPackage package;
if (!modData.ModFiles.TryGetPackageContaining(filename, out package, out name) || !(package is Folder))
{
Console.WriteLine("\t\tFile cannot be opened for writing! Ignoring...");
continue;
}
var yaml = MiniYaml.FromStream(package.GetStream(name), name);
processFile(modData, engineDate, ref yaml, null, 0);
// Generate the on-disk path
var path = Path.Combine(package.Name, name);
using (var file = new StreamWriter(path))
file.Write(yaml.WriteToString());
}
}
[Desc("OLDENGINE", "Upgrade mod rules to the latest engine version.")]
void IUtilityCommand.Run(Utility utility, string[] args)
{
// HACK: The engine code assumes that Game.modData is set.
var modData = Game.ModData = utility.ModData;
var engineDate = Exts.ParseIntegerInvariant(args[1]);
if (engineDate < UpgradeRules.MinimumSupportedVersion)
{
Console.WriteLine("Unsupported engine version. Use the release-{0} utility to update to that version, and then try again",
UpgradeRules.MinimumSupportedVersion);
return;
}
ProcessYaml("Rules", modData.Manifest.Rules, modData, engineDate, UpgradeRules.UpgradeActorRules);
ProcessYaml("Weapons", modData.Manifest.Weapons, modData, engineDate, UpgradeRules.UpgradeWeaponRules);
ProcessYaml("Sequences", modData.Manifest.Sequences, modData, engineDate, UpgradeRules.UpgradeSequences);
ProcessYaml("Tilesets", modData.Manifest.TileSets, modData, engineDate, UpgradeRules.UpgradeTileset);
ProcessYaml("Cursors", modData.Manifest.Cursors, modData, engineDate, UpgradeRules.UpgradeCursors);
ProcessYaml("Chrome Metrics", modData.Manifest.ChromeMetrics, modData, engineDate, UpgradeRules.UpgradeChromeMetrics);
ProcessYaml("Chrome Layout", modData.Manifest.ChromeLayout, modData, engineDate, UpgradeRules.UpgradeChromeLayout);
// 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
Console.WriteLine("Processing System Maps:");
foreach (var package in modData.MapCache.EnumerateMapPackagesWithoutCaching())
{
try
{
Console.WriteLine(package.Name);
UpgradeMapCommand.UpgradeMap(modData, package, engineDate);
}
catch (Exception e)
{
Console.WriteLine("Failed to upgrade map {0}", package.Name);
Console.WriteLine("Error was: {0}", e.ToString());
}
}
}
}
}

File diff suppressed because it is too large Load Diff