Reverted d36dab4 (XML files are calculated according to XML content). Caused content package hashes to differ between linux and windows versions. Closes #275

This commit is contained in:
Joonas Rikkonen
2018-02-24 18:39:47 +02:00
parent af3edda188
commit 313fb35b4a
@@ -120,19 +120,11 @@ namespace Barotrauma
new XAttribute("name", name), new XAttribute("name", name),
new XAttribute("path", Path))); new XAttribute("path", Path)));
//doc.Root.Add(
// new XElement("jobs", new XAttribute("file", JobFile)),
// new XElement("structures", new XAttribute("file", StructureFile)));
foreach (ContentFile file in files) foreach (ContentFile file in files)
{ {
doc.Root.Add(new XElement(file.type.ToString(), new XAttribute("file", file.path))); doc.Root.Add(new XElement(file.type.ToString(), new XAttribute("file", file.path)));
} }
//foreach (string itemFile in itemFiles)
//{
// doc.Root.Add(new XElement("item", new XAttribute("file", itemFile)));
//}
doc.Save(System.IO.Path.Combine(filePath, name+".xml")); doc.Save(System.IO.Path.Combine(filePath, name+".xml"));
} }
@@ -140,14 +132,6 @@ namespace Barotrauma
{ {
List<byte[]> hashes = new List<byte[]>(); List<byte[]> hashes = new List<byte[]>();
//foreach (ContentFile file in files)
//{
// if (file.path.EndsWith(".xml", true, System.Globalization.CultureInfo.InvariantCulture))
// {
// XDocument doc = ToolBox.TryLoadXml(file.path);
// sb.Append(doc.ToString());
// }
//}
var md5 = MD5.Create(); var md5 = MD5.Create();
foreach (ContentFile file in files) foreach (ContentFile file in files)
{ {
@@ -155,10 +139,10 @@ namespace Barotrauma
try try
{ {
if (file.path.EndsWith(".xml", true, System.Globalization.CultureInfo.InvariantCulture)) using (var stream = File.OpenRead(file.path))
hashes.Add(CalculateXmlHash(file, ref md5)); {
else hashes.Add(md5.ComputeHash(stream));
hashes.Add(CalculateFileHash(file, ref md5)); }
} }
catch (Exception e) catch (Exception e)
@@ -168,46 +152,15 @@ namespace Barotrauma
} }
//string str = sb.ToString(); byte[] bytes = new byte[hashes.Count * 16];
byte[] bytes = new byte[hashes.Count*16]; for (int i = 0; i < hashes.Count; i++)
for (int i = 0; i < hashes.Count; i++ )
{ {
hashes[i].CopyTo(bytes, i*16); hashes[i].CopyTo(bytes, i * 16);
} }
//System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
md5Hash = new Md5Hash(bytes); 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) public List<string> GetFilesOfType(ContentType type)
{ {
List<ContentFile> contentFiles = files.FindAll(f => f.type == type); List<ContentFile> contentFiles = files.FindAll(f => f.type == type);