Merge pull request #237 from shoter/master

MD5 of XML are calculated according to their content.
This commit is contained in:
Joonas Rikkonen
2018-01-28 14:35:48 +02:00
committed by GitHub

View File

@@ -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,35 @@ 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();
memoryStream.Position = 0;
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<string> GetFilesOfType(ContentType type)
{
List<ContentFile> contentFiles = files.FindAll(f => f.type == type);