Having multiple sub files with the same name doesn't prevent clients from selecting or requesting the correct one

This commit is contained in:
Regalis
2017-03-28 21:01:44 +03:00
parent 2a5a2bc6f6
commit 5bdd801da4
4 changed files with 33 additions and 20 deletions
+3 -3
View File
@@ -94,9 +94,9 @@ namespace Barotrauma
return sb.ToString(); return sb.ToString();
} }
private string GetShortHash(string fullHash) public static string GetShortHash(string fullHash)
{ {
return fullHash; return fullHash.Length < 7 ? fullHash : fullHash.Substring(0, 7);
} }
} }
} }
@@ -271,7 +271,8 @@ namespace Barotrauma.Networking
{ {
case (byte)FileTransferType.Submarine: case (byte)FileTransferType.Submarine:
string fileName = inc.ReadString(); string fileName = inc.ReadString();
var requestedSubmarine = Submarine.SavedSubmarines.Find(s => s.Name == fileName); string fileHash = inc.ReadString();
var requestedSubmarine = Submarine.SavedSubmarines.Find(s => s.Name == fileName && s.MD5Hash.Hash == fileHash);
if (requestedSubmarine != null) if (requestedSubmarine != null)
{ {
+15 -8
View File
@@ -740,7 +740,7 @@ namespace Barotrauma.Networking
string subName = inc.ReadString(); string subName = inc.ReadString();
string subHash = inc.ReadString(); string subHash = inc.ReadString();
var matchingSub = Submarine.SavedSubmarines.Find(s => s.Name == subName); var matchingSub = Submarine.SavedSubmarines.Find(s => s.Name == subName && s.MD5Hash.Hash == subHash);
if (matchingSub != null) if (matchingSub != null)
{ {
submarines.Add(matchingSub); submarines.Add(matchingSub);
@@ -987,13 +987,14 @@ namespace Barotrauma.Networking
chatMsgQueue.Add(chatMessage); chatMsgQueue.Add(chatMessage);
} }
public void RequestFile(string file, FileTransferType fileType) public void RequestFile(FileTransferType fileType, string file, string fileHash)
{ {
NetOutgoingMessage msg = client.CreateMessage(); NetOutgoingMessage msg = client.CreateMessage();
msg.Write((byte)ClientPacketHeader.FILE_REQUEST); msg.Write((byte)ClientPacketHeader.FILE_REQUEST);
msg.Write((byte)FileTransferMessageType.Initiate); msg.Write((byte)FileTransferMessageType.Initiate);
msg.Write((byte)fileType); msg.Write((byte)fileType);
msg.Write(file); msg.Write(file);
msg.Write(fileHash);
client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered); client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered);
} }
@@ -1012,17 +1013,23 @@ namespace Barotrauma.Networking
switch (transfer.FileType) switch (transfer.FileType)
{ {
case FileTransferType.Submarine: case FileTransferType.Submarine:
Submarine.SavedSubmarines.RemoveAll(s => s.Name + ".sub" == transfer.FileName); var newSub = new Submarine(transfer.FilePath);
Submarine.SavedSubmarines.RemoveAll(s => s.Name == newSub.Name && s.MD5Hash.Hash == newSub.MD5Hash.Hash);
Submarine.SavedSubmarines.Add(newSub);
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
var textBlock = ((i == 0) ? List<GUIComponent> subListChildren = (i == 0) ?
GameMain.NetLobbyScreen.ShuttleList.ListBox.children.Find(c => (c.UserData as Submarine).Name + ".sub" == transfer.FileName) : GameMain.NetLobbyScreen.ShuttleList.ListBox.children :
GameMain.NetLobbyScreen.SubList.children.Find(c => (c.UserData as Submarine).Name + ".sub" == transfer.FileName)) as GUITextBlock; GameMain.NetLobbyScreen.SubList.children;
var textBlock = subListChildren.Find(c =>
((Submarine)c.UserData).Name == newSub.Name &&
((Submarine)c.UserData).MD5Hash.Hash == newSub.MD5Hash.Hash) as GUITextBlock;
if (textBlock == null) continue; if (textBlock == null) continue;
textBlock.TextColor = Color.White; textBlock.TextColor = Color.White;
var newSub = new Submarine(transfer.FilePath);
Submarine.SavedSubmarines.Add(newSub);
textBlock.UserData = newSub; textBlock.UserData = newSub;
textBlock.ToolTip = newSub.Description; textBlock.ToolTip = newSub.Description;
} }
+13 -8
View File
@@ -716,7 +716,9 @@ namespace Barotrauma
}; };
var matchingSub = Submarine.SavedSubmarines.Find(s => s.Name == sub.Name); var matchingSub = Submarine.SavedSubmarines.Find(s => s.Name == sub.Name && s.MD5Hash.Hash == sub.MD5Hash.Hash);
if (matchingSub == null) matchingSub = Submarine.SavedSubmarines.Find(s => s.Name == sub.Name);
if (matchingSub == null) if (matchingSub == null)
{ {
subTextBlock.TextColor = Color.Gray; subTextBlock.TextColor = Color.Gray;
@@ -1213,7 +1215,10 @@ namespace Barotrauma
return false; return false;
} }
var matchingListSub = subList.children.Find(c => c.UserData != null && (c.UserData as Submarine).Name == subName) as GUITextBlock; Submarine sub = Submarine.SavedSubmarines.Find(m => m.Name == subName && m.MD5Hash.Hash == md5Hash);
if (sub == null) sub = Submarine.SavedSubmarines.Find(m => m.Name == subName);
var matchingListSub = subList.children.Find(c => c.UserData == sub) as GUITextBlock;
if (matchingListSub != null) if (matchingListSub != null)
{ {
subList.OnSelected -= VotableClicked; subList.OnSelected -= VotableClicked;
@@ -1221,7 +1226,6 @@ namespace Barotrauma
subList.OnSelected += VotableClicked; subList.OnSelected += VotableClicked;
} }
Submarine sub = Submarine.SavedSubmarines.Find(m => m.Name == subName);
if (sub == null || sub.MD5Hash.Hash != md5Hash) if (sub == null || sub.MD5Hash.Hash != md5Hash)
{ {
string errorMsg = ""; string errorMsg = "";
@@ -1237,9 +1241,9 @@ namespace Barotrauma
} }
else else
{ {
errorMsg = "Your version of the submarine file \"" + sub.Name + "\" doesn't match the server's version! " errorMsg = "Your version of the submarine file \"" + sub.Name + "\" doesn't match the server's version!\n"
+ "Your MD5 hash: " + sub.MD5Hash.Hash + " \n" + "Your MD5 hash: " + sub.MD5Hash.ShortHash + "\n"
+ "Server's MD5 hash: " + md5Hash + ". "; + "Server's MD5 hash: " + Md5Hash.GetShortHash(md5Hash) + "\n";
} }
errorMsg += "Do you want to download the file from the server host?"; errorMsg += "Do you want to download the file from the server host?";
@@ -1252,11 +1256,12 @@ namespace Barotrauma
var requestFileBox = new GUIMessageBox("Submarine not found!", errorMsg, new string[] { "Yes", "No" }, 400, 300); var requestFileBox = new GUIMessageBox("Submarine not found!", errorMsg, new string[] { "Yes", "No" }, 400, 300);
requestFileBox.UserData = "request" + subName; requestFileBox.UserData = "request" + subName;
requestFileBox.Buttons[0].UserData = subName; requestFileBox.Buttons[0].UserData = new string[] { subName, md5Hash };
requestFileBox.Buttons[0].OnClicked += requestFileBox.Close; requestFileBox.Buttons[0].OnClicked += requestFileBox.Close;
requestFileBox.Buttons[0].OnClicked += (GUIButton button, object userdata) => requestFileBox.Buttons[0].OnClicked += (GUIButton button, object userdata) =>
{ {
GameMain.Client.RequestFile(userdata.ToString(), FileTransferType.Submarine); string[] fileInfo = (string[])userdata;
GameMain.Client.RequestFile(FileTransferType.Submarine, fileInfo[0], fileInfo[1]);
return true; return true;
}; };
requestFileBox.Buttons[1].OnClicked += requestFileBox.Close; requestFileBox.Buttons[1].OnClicked += requestFileBox.Close;