From 6c7b687f345ff42cc71722f88eeab3ca07282716 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 6 Feb 2016 12:07:34 +0000 Subject: [PATCH] Move map UID calculation to a static method. --- OpenRA.Game/Map/Map.cs | 50 +++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index 50fa077c32..a896e00751 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -167,6 +167,34 @@ namespace OpenRA return videos; } + public static string ComputeUID(IReadOnlyPackage package) + { + // UID is calculated by taking an SHA1 of the yaml and binary data + using (var ms = new MemoryStream()) + { + // Read the relevant data into the buffer + using (var s = package.GetStream("map.yaml")) + { + if (s == null) + throw new FileNotFoundException("Required file map.yaml not present in this map"); + s.CopyTo(ms); + } + + using (var s = package.GetStream("map.bin")) + { + if (s == null) + throw new FileNotFoundException("Required file map.bin not present in this map"); + + s.CopyTo(ms); + } + + // Take the SHA1 + ms.Seek(0, SeekOrigin.Begin); + using (var csp = SHA1.Create()) + return new string(csp.ComputeHash(ms).SelectMany(a => a.ToString("x2")).ToArray()); + } + } + public Rectangle Bounds; /// @@ -399,7 +427,7 @@ namespace OpenRA if (MapFormat < 8) Save(path); - Uid = ComputeHash(); + Uid = ComputeUID(Container); } void PostInit() @@ -604,7 +632,7 @@ namespace OpenRA Container.Write(entries); // Update UID to match the newly saved data - Uid = ComputeHash(); + Uid = ComputeUID(Container); } public CellLayer LoadMapTiles() @@ -915,24 +943,6 @@ namespace OpenRA ProjectedCellBounds = new ProjectedCellRegion(this, tl, br); } - string ComputeHash() - { - // UID is calculated by taking an SHA1 of the yaml and binary data - using (var ms = new MemoryStream()) - { - // Read the relevant data into the buffer - using (var s = Container.GetStream("map.yaml")) - s.CopyTo(ms); - using (var s = Container.GetStream("map.bin")) - s.CopyTo(ms); - - // Take the SHA1 - ms.Seek(0, SeekOrigin.Begin); - using (var csp = SHA1.Create()) - return new string(csp.ComputeHash(ms).SelectMany(a => a.ToString("x2")).ToArray()); - } - } - public void FixOpenAreas(Ruleset rules) { var r = new Random();