Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Barotrauma/BarotraumaClient/ClientSource/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static void CrashDump(GameMain game, string filePath, Exception exception)
try
{
string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
exeHash = Md5Hash.CalculateForFile(exePath, Md5Hash.StringHashOptions.BytePerfect);
exeHash = Md5Hash.CalculateForFile(exePath);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public virtual void Preload(Action<Sprite> addPreloadedSprite) { }

public virtual Md5Hash CalculateHash()
{
return Md5Hash.CalculateForFile(Path.Value, Md5Hash.StringHashOptions.IgnoreWhitespace);
return Md5Hash.CalculateForFile(Path.Value);
}

public bool NotSyncedInMultiplayer => Types.Any(t => t.Type == GetType() && t.NotSyncedInMultiplayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void Sort()

// Use byte-perfect hash because this is compressed, trimming whitespace is incorrect and needlessly slow here
public override Md5Hash CalculateHash()
=> Md5Hash.CalculateForFile(Path.FullPath, Md5Hash.StringHashOptions.BytePerfect);
=> Md5Hash.CalculateForFile(Path.FullPath);
}

[NotSyncedInMultiplayer]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ private static void Init()
Md5Hash? exeHash = null;
try
{
exeHash = Md5Hash.CalculateForFile(exePath, Md5Hash.StringHashOptions.BytePerfect);
exeHash = Md5Hash.CalculateForFile(exePath);
}
catch (Exception e)
{
Expand Down
9 changes: 4 additions & 5 deletions Barotrauma/BarotraumaShared/SharedSource/Map/SubmarineInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ public Md5Hash MD5Hash
{
if (hashTask == null)
{
XDocument doc = OpenFile(FilePath);
StartHashDocTask(doc);
StartHashDocTask();
}
hashTask.Wait();
hashTask = null;
Expand Down Expand Up @@ -381,7 +380,7 @@ public void Reload()
}
if (hash == null)
{
StartHashDocTask(doc);
StartHashDocTask();
}
SubmarineElement = doc.Root;
}
Expand Down Expand Up @@ -528,14 +527,14 @@ public bool IsVanillaSubmarine()
return false;
}

public void StartHashDocTask(XDocument doc)
public void StartHashDocTask()
{
if (hash != null) { return; }
if (hashTask != null) { return; }

hashTask = new Task(() =>
{
hash = Md5Hash.CalculateForString(doc.ToString(), Md5Hash.StringHashOptions.IgnoreWhitespace);
hash = Md5Hash.CalculateForFile(FilePath);
});
hashTask.Start();
}
Expand Down
32 changes: 22 additions & 10 deletions Barotrauma/BarotraumaShared/SharedSource/Utils/Md5Hash.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#nullable enable

using Barotrauma.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -43,6 +43,17 @@ private static void CalculateHash(byte[] bytes, out string stringRepresentation,
}
}

private static void CalculateHashStream(Stream stream, out string stringRepresentation, out byte[] byteRepresentation)
{
using (MD5 md5 = MD5.Create())
{
byte[] byteHash = md5.ComputeHash(stream);

byteRepresentation = byteHash;
stringRepresentation = ByteRepresentationToStringRepresentation(byteHash);
}
}

private static string ByteRepresentationToStringRepresentation(byte[] byteHash)
=> ToolBoxCore.ByteArrayToHexString(byteHash);

Expand Down Expand Up @@ -76,6 +87,13 @@ private Md5Hash(byte[] bytes, bool calculate)

ShortRepresentation = GetShortHash(StringRepresentation);
}

private Md5Hash(Stream stream)
{
CalculateHashStream(stream, out StringRepresentation, out ByteRepresentation);

ShortRepresentation = GetShortHash(StringRepresentation);
}

public static Md5Hash StringAsHash(string hash)
{
Expand Down Expand Up @@ -111,17 +129,11 @@ public enum StringHashOptions
IgnoreWhitespace = 0x2
}

public static Md5Hash CalculateForFile(string path, StringHashOptions options)
public static Md5Hash CalculateForFile(string path)
{
if (options.HasFlag(StringHashOptions.IgnoreWhitespace) || options.HasFlag(StringHashOptions.IgnoreCase))
{
string str = File.ReadAllText(path, Encoding.UTF8);
return CalculateForString(str, options);
}
else
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] bytes = File.ReadAllBytes(path);
return CalculateForBytes(bytes);
return new Md5Hash(stream);
}
}

Expand Down