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);
using (var zip = ZipFile.OpenRead(archName))
{
var entry = zip.Entries.FirstOrDefault(entry => entry.FullName == fileName || entry.Name == fileName); var entry = zip.Entries.FirstOrDefault(entry => entry.FullName == fileName || entry.Name == fileName);
if (entry is null) throw new Exception("File not found in archive"); if (entry is null) throw new Exception("File not found in archive");
var MemoryStream = new MemoryStream(); await using var memoryStream = new MemoryStream();
var stream = entry.Open(); var stream = entry.Open();
await stream.CopyToAsync(MemoryStream); await stream.CopyToAsync(memoryStream);
data = MemoryStream.ToArray(); var data = memoryStream.ToArray();
stream.Close(); stream.Close();
MemoryStream.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,14 +107,13 @@ 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 totalZIPFiles = archive.Entries.Count(); var currentZipFile = 0;
var currentZIPFile = 0;
foreach (var entry in archive.Entries) foreach (var entry in archive.Entries)
{ {
if (entry.FullName.EndsWith("/")) // it is a folder if (entry.FullName.EndsWith("/")) // it is a folder
@@ -134,10 +129,10 @@ public static class ArchiveManager
Config.Logger.Log(ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR); Config.Logger.Log(ex.Message, source: typeof(ArchiveManager), type: LogType.ERROR);
} }
currentZIPFile++; currentZipFile++;
await Task.Delay(10); await Task.Delay(10);
if (progress != null) if (progress != null)
progress.Report((float)currentZIPFile / totalZIPFiles * 100); progress.Report((float)currentZipFile / totalZipFiles * 100);
} }
} }
else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE) else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE)
@@ -172,5 +167,4 @@ public static class ArchiveManager
} }
} }
} }
}
} }