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);