Fixed some warnings

This commit is contained in:
2023-11-21 22:07:28 +02:00
parent 944d59d9a3
commit 2280957ea9

View File

@@ -8,19 +8,20 @@ namespace PluginManager.Others;
public static class ArchiveManager public static class ArchiveManager
{ {
private static string? archiveFolder; private static string? _archiveFolder;
public static bool isInitialized { get; private set; } private static bool IsInitialized { get; set; }
public static void Initialize() public static void Initialize()
{ {
if (isInitialized) throw new Exception("ArchiveManager is already initialized"); if (IsInitialized)
throw new Exception("ArchiveManager is already initialized");
if (!Config.AppSettings.ContainsKey("ArchiveFolder")) if (!Config.AppSettings.ContainsKey("ArchiveFolder"))
Config.AppSettings["ArchiveFolder"] = "./Data/PAKS/"; Config.AppSettings["ArchiveFolder"] = "./Data/Archives/";
archiveFolder = Config.AppSettings["ArchiveFolder"]; _archiveFolder = Config.AppSettings["ArchiveFolder"];
isInitialized = true; IsInitialized = true;
} }
/// <summary> /// <summary>
@@ -29,31 +30,26 @@ public static class ArchiveManager
/// <param name="fileName">The file name in the archive</param> /// <param name="fileName">The file name in the archive</param>
/// <param name="archName">The archive location on the disk</param> /// <param name="archName">The archive location on the disk</param>
/// <returns>An array of bytes that represents the Stream value from the file that was read inside the archive</returns> /// <returns>An array of bytes that represents the Stream value from the file that was read inside the archive</returns>
public async static Task<byte[]?> ReadStreamFromPakAsync(string fileName, string archName) public static async Task<byte[]?> ReadStreamFromPakAsync(string fileName, string archName)
{ {
if (!isInitialized) throw new Exception("ArchiveManager is not initialized"); if (!IsInitialized) throw new Exception("ArchiveManager is not initialized");
archName = archiveFolder + archName; archName = _archiveFolder + archName;
if (!File.Exists(archName)) if (!File.Exists(archName))
throw new Exception("Failed to load file !"); throw new Exception("Failed to load file !");
byte[]? data = null; using var zip = ZipFile.OpenRead(archName);
var entry = zip.Entries.FirstOrDefault(entry => entry.FullName == fileName || entry.Name == fileName);
if (entry is null) throw new Exception("File not found in archive");
using (var zip = ZipFile.OpenRead(archName)) await using var memoryStream = new MemoryStream();
{ var stream = entry.Open();
var entry = zip.Entries.FirstOrDefault(entry => entry.FullName == fileName || entry.Name == fileName); await stream.CopyToAsync(memoryStream);
if (entry is null) throw new Exception("File not found in archive"); var data = memoryStream.ToArray();
var MemoryStream = new MemoryStream(); stream.Close();
memoryStream.Close();
var stream = entry.Open();
await stream.CopyToAsync(MemoryStream);
data = MemoryStream.ToArray();
stream.Close();
MemoryStream.Close();
}
return data; return data;
} }
@@ -61,13 +57,13 @@ public static class ArchiveManager
/// <summary> /// <summary>
/// Read data from a file that is inside an archive (ZIP format) /// Read data from a file that is inside an archive (ZIP format)
/// </summary> /// </summary>
/// <param name="FileName">The file name that is inside the archive or its full path</param> /// <param name="fileName">The file name that is inside the archive or its full path</param>
/// <param name="archFile">The archive location from the PAKs folder</param> /// <param name="archFile">The archive location from the PAKs folder</param>
/// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns> /// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns>
public static async Task<string?> ReadFromPakAsync(string FileName, string archFile) public static async Task<string?> ReadFromPakAsync(string fileName, string archFile)
{ {
if (!isInitialized) throw new Exception("ArchiveManager is not initialized"); if (!IsInitialized) throw new Exception("ArchiveManager is not initialized");
archFile = archiveFolder + archFile; archFile = _archiveFolder + archFile;
if (!File.Exists(archFile)) if (!File.Exists(archFile))
throw new Exception("Failed to load file !"); throw new Exception("Failed to load file !");
@@ -78,7 +74,7 @@ public static class ArchiveManager
using (var zip = new ZipArchive(fs, ZipArchiveMode.Read)) using (var zip = new ZipArchive(fs, ZipArchiveMode.Read))
{ {
foreach (var entry in zip.Entries) foreach (var entry in zip.Entries)
if (entry.Name == FileName || entry.FullName == FileName) if (entry.Name == fileName || entry.FullName == fileName)
using (var s = entry.Open()) using (var s = entry.Open())
using (var reader = new StreamReader(s)) using (var reader = new StreamReader(s))
{ {
@@ -95,7 +91,7 @@ public static class ArchiveManager
{ {
Config.Logger.Log(message: ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR); // Write the error to a file Config.Logger.Log(message: ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR); // Write the error to a file
await Task.Delay(100); await Task.Delay(100);
return await ReadFromPakAsync(FileName, archFile); return await ReadFromPakAsync(fileName, archFile);
} }
} }
@@ -111,65 +107,63 @@ public static class ArchiveManager
string zip, string folder, IProgress<float> progress, string zip, string folder, IProgress<float> progress,
UnzipProgressType type) UnzipProgressType type)
{ {
if (!isInitialized) throw new Exception("ArchiveManager is not initialized"); if (!IsInitialized) throw new Exception("ArchiveManager is not initialized");
Directory.CreateDirectory(folder); Directory.CreateDirectory(folder);
using (var archive = ZipFile.OpenRead(zip)) using var archive = ZipFile.OpenRead(zip);
var totalZipFiles = archive.Entries.Count();
if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES)
{ {
if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES) var currentZipFile = 0;
foreach (var entry in archive.Entries)
{ {
var totalZIPFiles = archive.Entries.Count(); if (entry.FullName.EndsWith("/")) // it is a folder
var currentZIPFile = 0; Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
foreach (var entry in archive.Entries)
{
if (entry.FullName.EndsWith("/")) // it is a folder
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
else
try
{
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
}
catch (Exception ex)
{
Config.Logger.Log(ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR);
}
currentZIPFile++;
await Task.Delay(10);
if (progress != null)
progress.Report((float)currentZIPFile / totalZIPFiles * 100);
}
}
else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE)
{
ulong zipSize = 0;
foreach (var entry in archive.Entries)
zipSize += (ulong)entry.CompressedLength;
ulong currentSize = 0;
foreach (var entry in archive.Entries)
{
if (entry.FullName.EndsWith("/"))
{
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
continue;
}
else
try try
{ {
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true); entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
currentSize += (ulong)entry.CompressedLength;
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log(ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR); Config.Logger.Log(ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR);
} }
await Task.Delay(10); currentZipFile++;
if (progress != null) await Task.Delay(10);
progress.Report((float)currentSize / zipSize * 100); if (progress != null)
progress.Report((float)currentZipFile / totalZipFiles * 100);
}
}
else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE)
{
ulong zipSize = 0;
foreach (var entry in archive.Entries)
zipSize += (ulong)entry.CompressedLength;
ulong currentSize = 0;
foreach (var entry in archive.Entries)
{
if (entry.FullName.EndsWith("/"))
{
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
continue;
} }
try
{
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
currentSize += (ulong)entry.CompressedLength;
}
catch (Exception ex)
{
Config.Logger.Log(ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR);
}
await Task.Delay(10);
if (progress != null)
progress.Report((float)currentSize / zipSize * 100);
} }
} }
} }