Include map.png into uid generation

This commit is contained in:
Gustas
2022-01-21 00:03:43 +02:00
committed by Matthias Mailänder
parent bcf4ff3b7c
commit c40675cfba
3 changed files with 28 additions and 8 deletions

View File

@@ -15,6 +15,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.FileSystem; using OpenRA.FileSystem;
using OpenRA.Graphics; using OpenRA.Graphics;
@@ -151,6 +152,7 @@ namespace OpenRA
public class Map : IReadOnlyFileSystem public class Map : IReadOnlyFileSystem
{ {
public const int SupportedMapFormat = 11; public const int SupportedMapFormat = 11;
public const int CurrentMapFormat = 12;
const short InvalidCachedTerrainIndex = -1; const short InvalidCachedTerrainIndex = -1;
/// <summary>Defines the order of the fields in map.yaml</summary> /// <summary>Defines the order of the fields in map.yaml</summary>
@@ -253,6 +255,11 @@ namespace OpenRA
internal Translation Translation; internal Translation Translation;
public static string ComputeUID(IReadOnlyPackage package) public static string ComputeUID(IReadOnlyPackage package)
{
return ComputeUID(package, GetMapFormat(package));
}
static string ComputeUID(IReadOnlyPackage package, int format)
{ {
// UID is calculated by taking an SHA1 of the yaml and binary data // UID is calculated by taking an SHA1 of the yaml and binary data
var requiredFiles = new[] { "map.yaml", "map.bin" }; var requiredFiles = new[] { "map.yaml", "map.bin" };
@@ -265,7 +272,7 @@ namespace OpenRA
try try
{ {
foreach (var filename in contents) foreach (var filename in contents)
if (filename.EndsWith(".yaml") || filename.EndsWith(".bin") || filename.EndsWith(".lua")) if (filename.EndsWith(".yaml") || filename.EndsWith(".bin") || filename.EndsWith(".lua") || (format >= 12 && filename == "map.png"))
streams.Add(package.GetStream(filename)); streams.Add(package.GetStream(filename));
// Take the SHA1 // Take the SHA1
@@ -285,6 +292,19 @@ namespace OpenRA
} }
} }
static int GetMapFormat(IReadOnlyPackage p)
{
foreach (var line in p.GetStream("map.yaml").ReadAllLines())
{
// PERF This is a way to get MapFormat without expensive yaml parsing
var search = Regex.Match(line, "^MapFormat:\\s*(\\d*)\\s*$");
if (search.Success && search.Groups.Count > 0)
return FieldLoader.GetValue<int>("MapFormat", search.Groups[1].Value);
}
throw new InvalidDataException($"MapFormat is not definedt\n File: {p.Name}");
}
/// <summary> /// <summary>
/// Initializes a new map created by the editor or importer. /// Initializes a new map created by the editor or importer.
/// The map will not receive a valid UID until after it has been saved and reloaded. /// The map will not receive a valid UID until after it has been saved and reloaded.
@@ -332,7 +352,7 @@ namespace OpenRA
foreach (var field in YamlFields) foreach (var field in YamlFields)
field.Deserialize(this, yaml.Nodes); field.Deserialize(this, yaml.Nodes);
if (MapFormat != SupportedMapFormat) if (MapFormat < SupportedMapFormat)
throw new InvalidDataException($"Map format {MapFormat} is not supported.\n File: {package.Name}"); throw new InvalidDataException($"Map format {MapFormat} is not supported.\n File: {package.Name}");
PlayerDefinitions = MiniYaml.NodesOrEmpty(yaml, "Players"); PlayerDefinitions = MiniYaml.NodesOrEmpty(yaml, "Players");
@@ -400,7 +420,7 @@ namespace OpenRA
PostInit(); PostInit();
Uid = ComputeUID(Package); Uid = ComputeUID(Package, MapFormat);
} }
void PostInit() void PostInit()
@@ -600,7 +620,7 @@ namespace OpenRA
public void Save(IReadWritePackage toPackage) public void Save(IReadWritePackage toPackage)
{ {
MapFormat = SupportedMapFormat; MapFormat = CurrentMapFormat;
var root = new List<MiniYamlNode>(); var root = new List<MiniYamlNode>();
foreach (var field in YamlFields) foreach (var field in YamlFields)
@@ -625,7 +645,7 @@ namespace OpenRA
Package = toPackage; Package = toPackage;
// Update UID to match the newly saved data // Update UID to match the newly saved data
Uid = ComputeUID(toPackage); Uid = ComputeUID(toPackage, MapFormat);
} }
public byte[] SaveBinaryData() public byte[] SaveBinaryData()

View File

@@ -320,7 +320,7 @@ namespace OpenRA
if (yaml.TryGetValue("MapFormat", out var temp)) if (yaml.TryGetValue("MapFormat", out var temp))
{ {
var format = FieldLoader.GetValue<int>("MapFormat", temp.Value); var format = FieldLoader.GetValue<int>("MapFormat", temp.Value);
if (format != Map.SupportedMapFormat) if (format < Map.SupportedMapFormat)
throw new InvalidDataException($"Map format {format} is not supported."); throw new InvalidDataException($"Map format {format} is not supported.");
} }

View File

@@ -28,8 +28,8 @@ namespace OpenRA.Mods.Common.Lint
void Run(Action<string> emitError, int mapFormat, string author, string title, string[] categories) void Run(Action<string> emitError, int mapFormat, string author, string title, string[] categories)
{ {
if (mapFormat != Map.SupportedMapFormat) if (mapFormat < Map.SupportedMapFormat)
emitError($"Map format {mapFormat} does not match the supported version {Map.SupportedMapFormat}."); emitError($"Map format {mapFormat} does not match the supported version {Map.CurrentMapFormat}.");
if (author == null) if (author == null)
emitError("Map does not define a valid author."); emitError("Map does not define a valid author.");