181 lines
5.8 KiB
C#
Raw Normal View History

2022-04-02 18:11:30 +02:00
using System.IO;
using System.Linq;
using System.Collections.Generic;
2022-07-01 12:22:31 +08:00
using RageCoop.Core;
using System;
2022-04-02 16:40:24 +02:00
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
2022-04-02 16:40:24 +02:00
{
internal static class DownloadManager
2022-04-02 16:40:24 +02:00
{
2022-07-01 12:22:31 +08:00
static DownloadManager()
{
Networking.RequestHandlers.Add(PacketType.FileTransferRequest, (data) =>
{
var fr = new Packets.FileTransferRequest();
fr.Unpack(data);
return new Packets.FileTransferResponse()
{
ID= fr.ID,
Response=AddFile(fr.ID,fr.Name,fr.FileLength) ? FileResponse.NeedToDownload : FileResponse.AlreadyExists
};
});
Networking.RequestHandlers.Add(PacketType.FileTransferComplete, (data) =>
{
Packets.FileTransferComplete packet = new Packets.FileTransferComplete();
packet.Unpack(data);
Main.Logger.Debug($"Finalizing download:{packet.ID}");
Complete(packet.ID);
// Inform the server that the download is completed
return new Packets.FileTransferResponse()
{
ID= packet.ID,
Response=FileResponse.Completed
};
});
Networking.RequestHandlers.Add(PacketType.AllResourcesSent, (data) =>
{
try
{
Main.Resources.Load(downloadFolder);
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 };
}
});
}
static string downloadFolder = Path.Combine(Main.Settings.ResourceDirectory, Main.Settings.LastServerAddress.Replace(":", "."));
2022-04-02 16:40:24 +02:00
private static readonly Dictionary<int, DownloadFile> InProgressDownloads = new Dictionary<int, DownloadFile>();
2022-07-01 12:22:31 +08:00
public static bool AddFile(int id, string name, long length)
2022-04-02 16:40:24 +02:00
{
Main.Logger.Debug($"Downloading file to {downloadFolder}\\{name} , id:{id}");
2022-04-02 18:11:30 +02:00
if (!Directory.Exists(downloadFolder))
{
Directory.CreateDirectory(downloadFolder);
}
if (FileAlreadyExists(downloadFolder, name, length))
{
Main.Logger.Debug($"File already exists! canceling download:{name}");
2022-07-01 12:22:31 +08:00
return false;
}
2022-04-11 13:37:38 +02:00
if (!name.EndsWith(".zip"))
{
2022-07-01 12:22:31 +08:00
Main.Logger.Error($"File download blocked! [{name}]");
return false;
}
lock (InProgressDownloads)
{
InProgressDownloads.Add(id, new DownloadFile()
{
FileID = id,
FileName = name,
FileLength = length,
Stream = new FileStream($"{downloadFolder}\\{name}", FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)
});
2022-04-02 18:11:30 +02:00
}
2022-07-01 12:22:31 +08:00
return true;
2022-04-02 16:40:24 +02:00
}
2022-04-02 18:11:30 +02:00
/// <summary>
/// Check if the file already exists and if the size correct otherwise delete this file
/// </summary>
/// <param name="folder"></param>
/// <param name="name"></param>
/// <param name="length"></param>
/// <returns></returns>
private static bool FileAlreadyExists(string folder, string name, long length)
2022-04-02 16:40:24 +02:00
{
string filePath = $"{folder}\\{name}";
2022-04-02 18:11:30 +02:00
if (File.Exists(filePath))
{
if (new FileInfo(filePath).Length == length)
2022-04-02 18:11:30 +02:00
{
return true;
}
// Delete the file because the length is wrong (maybe the file was updated)
File.Delete(filePath);
}
return false;
}
public static void Write(int id, byte[] chunk)
{
lock (InProgressDownloads)
{
DownloadFile file;
if (InProgressDownloads.TryGetValue(id, out file))
2022-04-02 18:11:30 +02:00
{
2022-04-02 16:40:24 +02:00
file.Stream.Write(chunk, 0, chunk.Length);
}
else
{
Main.Logger.Trace($"Received unhandled file chunk:{id}");
}
}
}
public static void Complete(int id)
{
DownloadFile f;
if (InProgressDownloads.TryGetValue(id, out f))
{
2022-07-01 12:22:31 +08:00
InProgressDownloads.Remove(id);
f.Dispose();
Main.Logger.Info($"Download finished:{f.FileName}");
}
else
{
Main.Logger.Error($"Download not found! {id}");
}
2022-04-02 18:11:30 +02:00
}
public static void Cleanup()
2022-04-02 18:11:30 +02:00
{
lock (InProgressDownloads)
2022-04-02 18:11:30 +02:00
{
foreach (var file in InProgressDownloads.Values)
{
file.Dispose();
}
InProgressDownloads.Clear();
2022-04-02 18:11:30 +02:00
}
2022-04-02 16:40:24 +02:00
}
}
2022-07-01 13:54:18 +08:00
internal class DownloadFile: System.IDisposable
{
public int FileID { get; set; } = 0;
public string FileName { get; set; } = string.Empty;
public long FileLength { get; set; } = 0;
2022-04-06 06:52:02 +02:00
public long FileWritten { get; set; } = 0;
public FileStream Stream { get; set; }
public void Dispose()
{
if(Stream!= null)
{
Stream.Flush();
Stream.Close();
Stream.Dispose();
}
}
}
2022-04-02 16:40:24 +02:00
}