Merge pull request #10786 from pchote/mappreview-packages

Remove internal use of map paths.
This commit is contained in:
Oliver Brakmann
2016-02-23 22:52:54 +01:00
21 changed files with 172 additions and 174 deletions

View File

@@ -79,10 +79,10 @@ namespace OpenRA.Mods.Common.UtilityCommands
modData.MapCache.LoadMaps();
maps.AddRange(modData.MapCache
.Where(m => m.Status == MapStatus.Available)
.Select(m => new Map(m.Path)));
.Select(m => new Map(modData, m.Package)));
}
else
maps.Add(new Map(args[1]));
maps.Add(new Map(modData, modData.ModFiles.OpenPackage(args[1])));
foreach (var testMap in maps)
{

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
{
Game.ModData = modData;
var map = new Map(args[1]);
var map = new Map(modData, modData.ModFiles.OpenPackage(args[1]));
var minimap = Minimap.RenderMapPreview(map.Rules.TileSets[map.Tileset], map, true);
var dest = Path.GetFileNameWithoutExtension(args[1]) + ".png";

View File

@@ -61,7 +61,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
ValidateMapFormat(format);
var tileset = GetTileset(mapSection);
Map = new Map(Rules.TileSets[tileset], MapSize, MapSize)
Map = new Map(modData, Rules.TileSets[tileset], MapSize, MapSize)
{
Title = basic.GetValue("Name", Path.GetFileNameWithoutExtension(filename)),
Author = "Westwood Studios"
@@ -94,9 +94,9 @@ namespace OpenRA.Mods.Common.UtilityCommands
Map.FixOpenAreas(Rules);
var fileName = Path.GetFileNameWithoutExtension(args[1]);
var dest = fileName + ".oramap";
Map.Save(dest);
var dest = Path.GetFileNameWithoutExtension(args[1]) + ".oramap";
var package = modData.ModFiles.CreatePackage(dest);
Map.Save(package);
Console.WriteLine(dest + " saved.");
}

View File

@@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileSystem;
namespace OpenRA.Mods.Common.UtilityCommands
{
@@ -48,7 +49,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
public void Run(ModData modData, string[] args)
{
Game.ModData = modData;
map = new Map(args[1]);
map = new Map(modData, modData.ModFiles.OpenPackage(args[1]));
Console.WriteLine("Resizing map {0} from {1} to {2},{3}", map.Title, map.MapSize, width, height);
map.Resize(width, height);
@@ -68,7 +69,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
foreach (var kv in forRemoval)
map.ActorDefinitions.Remove(kv);
map.Save(map.Path);
map.Save((IReadWritePackage)map.Package);
}
}
}

View File

@@ -9,6 +9,8 @@
*/
#endregion
using OpenRA.FileSystem;
namespace OpenRA.Mods.Common.UtilityCommands
{
class UpgradeMapCommand : IUtilityCommand
@@ -26,15 +28,16 @@ namespace OpenRA.Mods.Common.UtilityCommands
// HACK: The engine code assumes that Game.modData is set.
Game.ModData = modData;
UpgradeRules.UpgradeMapFormat(modData, args[1]);
var package = modData.ModFiles.OpenWritablePackage(args[1]);
UpgradeRules.UpgradeMapFormat(modData, package);
var map = new Map(args[1]);
var map = new Map(modData, package);
var engineDate = Exts.ParseIntegerInvariant(args[2]);
UpgradeRules.UpgradeWeaponRules(engineDate, ref map.WeaponDefinitions, null, 0);
UpgradeRules.UpgradeActorRules(engineDate, ref map.RuleDefinitions, null, 0);
UpgradeRules.UpgradePlayers(engineDate, ref map.PlayerDefinitions, null, 0);
UpgradeRules.UpgradeActors(engineDate, ref map.ActorDefinitions, null, 0);
map.Save(args[1]);
map.Save(package);
}
}
}

View File

@@ -12,6 +12,7 @@
using System;
using System.IO;
using System.Linq;
using OpenRA.FileSystem;
namespace OpenRA.Mods.Common.UtilityCommands
{
@@ -100,21 +101,21 @@ namespace OpenRA.Mods.Common.UtilityCommands
}
Console.WriteLine("Processing Maps:");
var mapPaths = modData.MapCache
.Where(m => m.Status == MapStatus.Available)
.Select(m => m.Path);
var mapPreviews = modData.MapCache
.Where(m => m.Status == MapStatus.Available);
foreach (var path in mapPaths)
foreach (var p in mapPreviews)
{
Console.WriteLine("\t" + path);
UpgradeRules.UpgradeMapFormat(modData, path);
var package = (IReadWritePackage)p.Package;
Console.WriteLine("\t" + package.Name);
UpgradeRules.UpgradeMapFormat(modData, package);
var map = new Map(path);
var map = new Map(modData, package);
UpgradeRules.UpgradeActorRules(engineDate, ref map.RuleDefinitions, null, 0);
UpgradeRules.UpgradeWeaponRules(engineDate, ref map.WeaponDefinitions, null, 0);
UpgradeRules.UpgradePlayers(engineDate, ref map.PlayerDefinitions, null, 0);
UpgradeRules.UpgradeActors(engineDate, ref map.ActorDefinitions, null, 0);
map.Save(map.Path);
map.Save(package);
}
}
}

View File

@@ -16,6 +16,7 @@ using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Traits;
@@ -3602,95 +3603,92 @@ namespace OpenRA.Mods.Common.UtilityCommands
}
}
internal static void UpgradeMapFormat(ModData modData, string path)
internal static void UpgradeMapFormat(ModData modData, IReadWritePackage package)
{
using (var package = modData.ModFiles.OpenWritablePackage(path))
if (package == null)
return;
var yamlStream = package.GetStream("map.yaml");
if (yamlStream == null)
return;
var yaml = new MiniYaml(null, MiniYaml.FromStream(yamlStream, package.Name));
var nd = yaml.ToDictionary();
var mapFormat = FieldLoader.GetValue<int>("MapFormat", nd["MapFormat"].Value);
if (mapFormat < 6)
throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(mapFormat, package.Name));
// Nothing to do
if (mapFormat >= 8)
return;
// Format 6 -> 7 combined the Selectable and UseAsShellmap flags into the Class enum
if (mapFormat < 7)
{
if (package == null)
return;
MiniYaml useAsShellmap;
if (nd.TryGetValue("UseAsShellmap", out useAsShellmap) && bool.Parse(useAsShellmap.Value))
yaml.Nodes.Add(new MiniYamlNode("Visibility", new MiniYaml("Shellmap")));
else if (nd["Type"].Value == "Mission" || nd["Type"].Value == "Campaign")
yaml.Nodes.Add(new MiniYamlNode("Visibility", new MiniYaml("MissionSelector")));
}
var yamlStream = package.GetStream("map.yaml");
if (yamlStream == null)
return;
var yaml = new MiniYaml(null, MiniYaml.FromStream(yamlStream, path));
var nd = yaml.ToDictionary();
var mapFormat = FieldLoader.GetValue<int>("MapFormat", nd["MapFormat"].Value);
if (mapFormat < 6)
throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(mapFormat, path));
// Nothing to do
if (mapFormat >= 8)
return;
// Format 6 -> 7 combined the Selectable and UseAsShellmap flags into the Class enum
if (mapFormat < 7)
// Format 7 -> 8 replaced normalized HSL triples with rgb(a) hex colors
if (mapFormat < 8)
{
var players = yaml.Nodes.FirstOrDefault(n => n.Key == "Players");
if (players != null)
{
MiniYaml useAsShellmap;
if (nd.TryGetValue("UseAsShellmap", out useAsShellmap) && bool.Parse(useAsShellmap.Value))
yaml.Nodes.Add(new MiniYamlNode("Visibility", new MiniYaml("Shellmap")));
else if (nd["Type"].Value == "Mission" || nd["Type"].Value == "Campaign")
yaml.Nodes.Add(new MiniYamlNode("Visibility", new MiniYaml("MissionSelector")));
}
// Format 7 -> 8 replaced normalized HSL triples with rgb(a) hex colors
if (mapFormat < 8)
{
var players = yaml.Nodes.FirstOrDefault(n => n.Key == "Players");
if (players != null)
bool noteHexColors = false;
bool noteColorRamp = false;
foreach (var player in players.Value.Nodes)
{
bool noteHexColors = false;
bool noteColorRamp = false;
foreach (var player in players.Value.Nodes)
var colorRampNode = player.Value.Nodes.FirstOrDefault(n => n.Key == "ColorRamp");
if (colorRampNode != null)
{
var colorRampNode = player.Value.Nodes.FirstOrDefault(n => n.Key == "ColorRamp");
if (colorRampNode != null)
Color dummy;
var parts = colorRampNode.Value.Value.Split(',');
if (parts.Length == 3 || parts.Length == 4)
{
Color dummy;
var parts = colorRampNode.Value.Value.Split(',');
if (parts.Length == 3 || parts.Length == 4)
// Try to convert old normalized HSL value to a rgb hex color
try
{
// Try to convert old normalized HSL value to a rgb hex color
try
{
HSLColor color = new HSLColor(
(byte)Exts.ParseIntegerInvariant(parts[0].Trim()).Clamp(0, 255),
(byte)Exts.ParseIntegerInvariant(parts[1].Trim()).Clamp(0, 255),
(byte)Exts.ParseIntegerInvariant(parts[2].Trim()).Clamp(0, 255));
colorRampNode.Value.Value = FieldSaver.FormatValue(color);
noteHexColors = true;
}
catch (Exception)
{
throw new InvalidDataException("Invalid ColorRamp value.\n File: " + path);
}
HSLColor color = new HSLColor(
(byte)Exts.ParseIntegerInvariant(parts[0].Trim()).Clamp(0, 255),
(byte)Exts.ParseIntegerInvariant(parts[1].Trim()).Clamp(0, 255),
(byte)Exts.ParseIntegerInvariant(parts[2].Trim()).Clamp(0, 255));
colorRampNode.Value.Value = FieldSaver.FormatValue(color);
noteHexColors = true;
}
catch (Exception)
{
throw new InvalidDataException("Invalid ColorRamp value.\n File: " + package.Name);
}
else if (parts.Length != 1 || !HSLColor.TryParseRGB(parts[0], out dummy))
throw new InvalidDataException("Invalid ColorRamp value.\n File: " + path);
colorRampNode.Key = "Color";
noteColorRamp = true;
}
else if (parts.Length != 1 || !HSLColor.TryParseRGB(parts[0], out dummy))
throw new InvalidDataException("Invalid ColorRamp value.\n File: " + package.Name);
colorRampNode.Key = "Color";
noteColorRamp = true;
}
Console.WriteLine("Converted " + path + " to MapFormat 8.");
if (noteHexColors)
Console.WriteLine("ColorRamp is now called Color and uses rgb(a) hex value - rrggbb[aa].");
else if (noteColorRamp)
Console.WriteLine("ColorRamp is now called Color.");
}
}
var entries = new Dictionary<string, byte[]>();
entries.Add("map.yaml", Encoding.UTF8.GetBytes(yaml.Nodes.WriteToString()));
foreach (var file in package.Contents)
{
if (file == "map.yaml")
continue;
entries.Add(file, package.GetStream(file).ReadAllBytes());
Console.WriteLine("Converted " + package.Name + " to MapFormat 8.");
if (noteHexColors)
Console.WriteLine("ColorRamp is now called Color and uses rgb(a) hex value - rrggbb[aa].");
else if (noteColorRamp)
Console.WriteLine("ColorRamp is now called Color.");
}
}
var entries = new Dictionary<string, byte[]>();
entries.Add("map.yaml", Encoding.UTF8.GetBytes(yaml.Nodes.WriteToString()));
foreach (var file in package.Contents)
{
if (file == "map.yaml")
continue;
entries.Add(file, package.GetStream(file).ReadAllBytes());
}
}
}
}