From d36dab42f8907013c285e9fa3f02a9b68c36c83e Mon Sep 17 00:00:00 2001 From: shoter Date: Sat, 27 Jan 2018 20:55:23 +0100 Subject: [PATCH 1/2] XML files are calculated according to XML content. --- .../BarotraumaShared/Source/ContentPackage.cs | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Source/ContentPackage.cs b/Barotrauma/BarotraumaShared/Source/ContentPackage.cs index 6721fe5c0..179fa47d5 100644 --- a/Barotrauma/BarotraumaShared/Source/ContentPackage.cs +++ b/Barotrauma/BarotraumaShared/Source/ContentPackage.cs @@ -155,10 +155,10 @@ namespace Barotrauma try { - using (var stream = File.OpenRead(file.path)) - { - hashes.Add(md5.ComputeHash(stream)); - } + if (file.path.EndsWith(".xml", true, System.Globalization.CultureInfo.InvariantCulture)) + hashes.Add(calculateXmlHash(file, ref md5)); + else + hashes.Add(calculateFileHash(file, ref md5)); } catch (Exception e) @@ -179,6 +179,34 @@ namespace Barotrauma md5Hash = new Md5Hash(bytes); } + + private byte[] calculateXmlHash(ContentFile file, ref MD5 md5) //todo: Change ref to in (in C# 7.2) + { + var doc = XMLExtensions.TryLoadXml(file.path); + + if (doc == null) + throw new Exception($"file {file.path} could not be opened as XML document"); + + using (var memoryStream = new MemoryStream()) + { + using (var writer = new StreamWriter(memoryStream)) + { + writer.Write(doc.ToString()); + writer.Flush(); + + return md5.ComputeHash(memoryStream); + } + } + } + + private byte[] calculateFileHash(ContentFile file, ref MD5 md5) + { + using (var stream = File.OpenRead(file.path)) + { + return md5.ComputeHash(stream); + } + } + public List GetFilesOfType(ContentType type) { List contentFiles = files.FindAll(f => f.type == type); From 9feb5b694da0ff8d31b15d46062a1094b718f693 Mon Sep 17 00:00:00 2001 From: shoter Date: Sat, 27 Jan 2018 21:26:56 +0100 Subject: [PATCH 2/2] Stream position was not reset for MD5 calculation for xml files. --- Barotrauma/BarotraumaShared/Source/ContentPackage.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Barotrauma/BarotraumaShared/Source/ContentPackage.cs b/Barotrauma/BarotraumaShared/Source/ContentPackage.cs index 179fa47d5..4fb40c6e9 100644 --- a/Barotrauma/BarotraumaShared/Source/ContentPackage.cs +++ b/Barotrauma/BarotraumaShared/Source/ContentPackage.cs @@ -194,6 +194,7 @@ namespace Barotrauma writer.Write(doc.ToString()); writer.Flush(); + memoryStream.Position = 0; return md5.ComputeHash(memoryStream); } }