This commit is contained in:
sardelka9515
2022-10-23 19:02:39 +08:00
parent 6b34ab6e36
commit 2828b9b74f
114 changed files with 7374 additions and 7205 deletions

View File

@ -1,71 +1,73 @@
using RageCoop.Core;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using RageCoop.Core;
namespace RageCoop.Client
{
internal static class DownloadManager
{
public static event EventHandler<string> DownloadCompleted;
private static readonly Dictionary<int, DownloadFile> InProgressDownloads = new Dictionary<int, DownloadFile>();
private static readonly HashSet<string> _resources = new HashSet<string>();
static DownloadManager()
{
Networking.RequestHandlers.Add(PacketType.FileTransferRequest, (data) =>
Networking.RequestHandlers.Add(PacketType.FileTransferRequest, data =>
{
var fr = new Packets.FileTransferRequest();
fr.Deserialize(data);
if (fr.Name.EndsWith(".res"))
{
_resources.Add(fr.Name);
}
return new Packets.FileTransferResponse()
if (fr.Name.EndsWith(".res")) _resources.Add(fr.Name);
return new Packets.FileTransferResponse
{
ID = fr.ID,
Response = AddFile(fr.ID, fr.Name, fr.FileLength) ? FileResponse.NeedToDownload : FileResponse.AlreadyExists
Response = AddFile(fr.ID, fr.Name, fr.FileLength)
? FileResponse.NeedToDownload
: FileResponse.AlreadyExists
};
});
Networking.RequestHandlers.Add(PacketType.FileTransferComplete, (data) =>
Networking.RequestHandlers.Add(PacketType.FileTransferComplete, data =>
{
Packets.FileTransferComplete packet = new Packets.FileTransferComplete();
var packet = new Packets.FileTransferComplete();
packet.Deserialize(data);
Main.Logger.Debug($"Finalizing download:{packet.ID}");
Complete(packet.ID);
// Inform the server that the download is completed
return new Packets.FileTransferResponse()
return new Packets.FileTransferResponse
{
ID = packet.ID,
Response = FileResponse.Completed
};
});
Networking.RequestHandlers.Add(PacketType.AllResourcesSent, (data) =>
Networking.RequestHandlers.Add(PacketType.AllResourcesSent, data =>
{
try
{
Main.Resources.Load(ResourceFolder, _resources.ToArray());
return new Packets.FileTransferResponse() { ID = 0, Response = FileResponse.Loaded };
return new Packets.FileTransferResponse { ID = 0, Response = FileResponse.Loaded };
}
catch (Exception ex)
{
Main.Logger.Error("Error occurred when loading server resource");
Main.Logger.Error(ex);
return new Packets.FileTransferResponse() { ID = 0, Response = FileResponse.LoadFailed };
return new Packets.FileTransferResponse { ID = 0, Response = FileResponse.LoadFailed };
}
});
}
public static string ResourceFolder => Path.GetFullPath(Path.Combine(Main.Settings.DataDirectory, "Resources", Main.Settings.LastServerAddress.Replace(":", ".")));
private static readonly Dictionary<int, DownloadFile> InProgressDownloads = new Dictionary<int, DownloadFile>();
private static readonly HashSet<string> _resources = new HashSet<string>();
public static string ResourceFolder => Path.GetFullPath(Path.Combine(Main.Settings.DataDirectory, "Resources",
Main.Settings.LastServerAddress.Replace(":", ".")));
public static event EventHandler<string> DownloadCompleted;
public static bool AddFile(int id, string name, long length)
{
var path = $"{ResourceFolder}\\{name}";
Main.Logger.Debug($"Downloading file to {path} , id:{id}");
if (!Directory.Exists(Directory.GetParent(path).FullName))
{
Directory.CreateDirectory(Directory.GetParent(path).FullName);
}
if (FileAlreadyExists(ResourceFolder, name, length))
{
@ -73,6 +75,7 @@ namespace RageCoop.Client
DownloadCompleted?.Invoke(null, Path.Combine(ResourceFolder, name));
return false;
}
/*
if (!name.EndsWith(".zip"))
{
@ -82,7 +85,7 @@ namespace RageCoop.Client
*/
lock (InProgressDownloads)
{
InProgressDownloads.Add(id, new DownloadFile()
InProgressDownloads.Add(id, new DownloadFile
{
FileID = id,
FileName = name,
@ -90,11 +93,12 @@ namespace RageCoop.Client
Stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)
});
}
return true;
}
/// <summary>
/// Check if the file already exists and if the size correct otherwise delete this file
/// Check if the file already exists and if the size correct otherwise delete this file
/// </summary>
/// <param name="folder"></param>
/// <param name="name"></param>
@ -102,14 +106,11 @@ namespace RageCoop.Client
/// <returns></returns>
private static bool FileAlreadyExists(string folder, string name, long length)
{
string filePath = $"{folder}\\{name}";
var filePath = $"{folder}\\{name}";
if (File.Exists(filePath))
{
if (new FileInfo(filePath).Length == length)
{
return true;
}
if (new FileInfo(filePath).Length == length) return true;
// Delete the file because the length is wrong (maybe the file was updated)
File.Delete(filePath);
@ -122,22 +123,16 @@ namespace RageCoop.Client
{
lock (InProgressDownloads)
{
if (InProgressDownloads.TryGetValue(id, out DownloadFile file))
{
if (InProgressDownloads.TryGetValue(id, out var file))
file.Stream.Write(chunk, 0, chunk.Length);
}
else
{
Main.Logger.Trace($"Received unhandled file chunk:{id}");
}
}
}
public static void Complete(int id)
{
if (InProgressDownloads.TryGetValue(id, out DownloadFile f))
if (InProgressDownloads.TryGetValue(id, out var f))
{
InProgressDownloads.Remove(id);
f.Dispose();
@ -154,24 +149,22 @@ namespace RageCoop.Client
{
lock (InProgressDownloads)
{
foreach (var file in InProgressDownloads.Values)
{
file.Dispose();
}
foreach (var file in InProgressDownloads.Values) file.Dispose();
InProgressDownloads.Clear();
}
_resources.Clear();
_resources.Clear();
}
}
internal class DownloadFile : IDisposable
{
public int FileID { get; set; } = 0;
public int FileID { get; set; }
public string FileName { get; set; } = string.Empty;
public long FileLength { get; set; } = 0;
public long FileLength { get; set; }
public long FileWritten { get; set; } = 0;
public FileStream Stream { get; set; }
public void Dispose()
{
if (Stream != null)
@ -182,4 +175,4 @@ namespace RageCoop.Client
}
}
}
}
}