2022-04-02 18:11:30 +02:00
|
|
|
|
using System.IO;
|
2022-04-06 05:10:29 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Collections.Generic;
|
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
|
|
|
|
{
|
2022-05-31 09:14:30 +08:00
|
|
|
|
internal static class DownloadManager
|
2022-04-02 16:40:24 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
static string downloadFolder = $"RageCoop\\Resources\\{Main.Settings.LastServerAddress.Replace(":", ".")}";
|
2022-04-02 16:40:24 +02:00
|
|
|
|
|
2022-06-11 18:41:10 +08:00
|
|
|
|
private static readonly Dictionary<int, DownloadFile> InProgressDownloads = new Dictionary<int, DownloadFile>();
|
2022-06-06 17:37:11 +08:00
|
|
|
|
public static void AddFile(int id, string name, long length)
|
2022-04-02 16:40:24 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08: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);
|
2022-04-06 05:10:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FileAlreadyExists(downloadFolder, name, length))
|
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
Main.Logger.Debug($"File already exists! canceling download:{name}");
|
|
|
|
|
Cancel(id);
|
|
|
|
|
if (name=="Resources.zip")
|
|
|
|
|
{
|
|
|
|
|
Main.Logger.Debug("Loading resources...");
|
2022-06-12 15:39:32 +08:00
|
|
|
|
Main.Resources.Load(Path.Combine(downloadFolder));
|
2022-06-11 18:41:10 +08:00
|
|
|
|
}
|
2022-04-06 05:10:29 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-04-11 13:37:38 +02:00
|
|
|
|
|
2022-06-11 18:41:10 +08:00
|
|
|
|
if (!name.EndsWith(".zip"))
|
2022-04-11 13:07:46 +02:00
|
|
|
|
{
|
2022-04-11 15:10:27 +02:00
|
|
|
|
Cancel(id);
|
2022-04-11 13:07:46 +02:00
|
|
|
|
|
|
|
|
|
GTA.UI.Notification.Show($"The download of a file from the server was blocked! [{name}]", true);
|
2022-05-22 15:55:26 +08:00
|
|
|
|
Main.Logger.Error($"The download of a file from the server was blocked! [{name}]");
|
2022-04-11 13:07:46 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-11 18:41:10 +08:00
|
|
|
|
lock (InProgressDownloads)
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
InProgressDownloads.Add(id, new DownloadFile()
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
|
|
|
|
FileID = id,
|
|
|
|
|
FileName = name,
|
2022-06-11 18:41:10 +08:00
|
|
|
|
FileLength = length,
|
|
|
|
|
Stream = new FileStream($"{downloadFolder}\\{name}", FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)
|
2022-04-06 05:10:29 +02:00
|
|
|
|
});
|
2022-04-02 18:11:30 +02:00
|
|
|
|
}
|
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>
|
2022-04-06 05:10:29 +02:00
|
|
|
|
/// <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
|
|
|
|
{
|
2022-04-06 05:10:29 +02:00
|
|
|
|
string filePath = $"{folder}\\{name}";
|
|
|
|
|
|
2022-04-02 18:11:30 +02:00
|
|
|
|
if (File.Exists(filePath))
|
|
|
|
|
{
|
2022-04-06 05:10:29 +02:00
|
|
|
|
if (new FileInfo(filePath).Length == length)
|
2022-04-02 18:11:30 +02:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-04-06 05:10:29 +02:00
|
|
|
|
|
|
|
|
|
// Delete the file because the length is wrong (maybe the file was updated)
|
|
|
|
|
File.Delete(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 17:37:11 +08:00
|
|
|
|
public static void Write(int id, byte[] chunk)
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
lock (InProgressDownloads)
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
DownloadFile file;
|
|
|
|
|
if (InProgressDownloads.TryGetValue(id, out file))
|
2022-04-02 18:11:30 +02:00
|
|
|
|
{
|
2022-04-02 16:40:24 +02:00
|
|
|
|
|
2022-06-11 18:41:10 +08:00
|
|
|
|
file.Stream.Write(chunk, 0, chunk.Length);
|
2022-04-06 05:10:29 +02:00
|
|
|
|
}
|
2022-06-11 18:41:10 +08:00
|
|
|
|
else
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
Main.Logger.Trace($"Received unhandled file chunk:{id}");
|
|
|
|
|
return;
|
2022-04-06 05:10:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-11 18:41:10 +08:00
|
|
|
|
|
2022-04-06 05:10:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 17:37:11 +08:00
|
|
|
|
public static void Cancel(int id)
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
Main.Logger.Debug($"Canceling download:{id}");
|
|
|
|
|
|
|
|
|
|
// Tell the server to stop sending chunks
|
|
|
|
|
Networking.SendDownloadFinish(id);
|
|
|
|
|
|
|
|
|
|
DownloadFile file;
|
|
|
|
|
lock (InProgressDownloads)
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
if (InProgressDownloads.TryGetValue(id, out file))
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
InProgressDownloads.Remove(id);
|
|
|
|
|
file.Dispose();
|
2022-04-06 05:10:29 +02:00
|
|
|
|
}
|
2022-06-11 18:41:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static void Complete(int id)
|
|
|
|
|
{
|
|
|
|
|
DownloadFile f;
|
2022-04-06 05:10:29 +02:00
|
|
|
|
|
2022-06-11 18:41:10 +08:00
|
|
|
|
if (InProgressDownloads.TryGetValue(id, out f))
|
|
|
|
|
{
|
|
|
|
|
lock (InProgressDownloads)
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
InProgressDownloads.Remove(id);
|
|
|
|
|
f.Dispose();
|
|
|
|
|
Main.Logger.Info($"Download finished:{f.FileName}");
|
|
|
|
|
if (f.FileName=="Resources.zip")
|
|
|
|
|
{
|
|
|
|
|
Main.Logger.Debug("Loading resources...");
|
2022-06-12 15:39:32 +08:00
|
|
|
|
Main.Resources.Load(Path.Combine(downloadFolder));
|
2022-06-11 18:41:10 +08:00
|
|
|
|
}
|
|
|
|
|
Networking.SendDownloadFinish(id);
|
2022-04-06 05:10:29 +02:00
|
|
|
|
}
|
2022-06-11 18:41:10 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Main.Logger.Error($"Download not found! {id}");
|
2022-04-06 05:10:29 +02:00
|
|
|
|
}
|
2022-04-02 18:11:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 18:41:10 +08:00
|
|
|
|
public static void Cleanup()
|
2022-04-02 18:11:30 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
lock (InProgressDownloads)
|
2022-04-02 18:11:30 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
foreach (var file in InProgressDownloads.Values)
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
file.Dispose();
|
2022-04-06 05:10:29 +02:00
|
|
|
|
}
|
2022-06-11 18:41:10 +08:00
|
|
|
|
InProgressDownloads.Clear();
|
2022-04-02 18:11:30 +02:00
|
|
|
|
}
|
2022-04-06 05:54:03 +02:00
|
|
|
|
|
2022-04-02 16:40:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-06 05:10:29 +02:00
|
|
|
|
|
2022-06-11 18:41:10 +08:00
|
|
|
|
public class DownloadFile: System.IDisposable
|
2022-04-06 05:10:29 +02:00
|
|
|
|
{
|
2022-06-06 17:37:11 +08:00
|
|
|
|
public int FileID { get; set; } = 0;
|
2022-04-06 05:10:29 +02:00
|
|
|
|
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;
|
2022-06-11 18:41:10 +08:00
|
|
|
|
public FileStream Stream { get; set; }
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if(Stream!= null)
|
|
|
|
|
{
|
|
|
|
|
Stream.Flush();
|
|
|
|
|
Stream.Close();
|
|
|
|
|
Stream.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-06 05:10:29 +02:00
|
|
|
|
}
|
2022-04-02 16:40:24 +02:00
|
|
|
|
}
|