From 487ec5bc8fb3f3fc840fb454a271e0be8039a469 Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 17 Feb 2022 16:12:21 -0500 Subject: [PATCH] Move PNG conversion to FileHelper --- ProjectLighthouse/Helpers/FileHelper.cs | 46 +++++++++++++++++++++++++ ProjectLighthouse/Program.cs | 40 +-------------------- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/ProjectLighthouse/Helpers/FileHelper.cs b/ProjectLighthouse/Helpers/FileHelper.cs index 9d8c4f20..e3e39d17 100644 --- a/ProjectLighthouse/Helpers/FileHelper.cs +++ b/ProjectLighthouse/Helpers/FileHelper.cs @@ -1,7 +1,13 @@ +#nullable enable using System; +using System.Collections.Concurrent; using System.IO; using System.Linq; using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Kettu; +using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types.Files; using LBPUnion.ProjectLighthouse.Types.Settings; @@ -101,4 +107,44 @@ public static class FileHelper } public static string[] ResourcesNotUploaded(params string[] hashes) => hashes.Where(hash => !ResourceExists(hash)).ToArray(); + + public static void ConvertAllTexturesToPng() + { + EnsureDirectoryCreated(Path.Combine(Environment.CurrentDirectory, "png")); + if (Directory.Exists("r")) + { + Logger.Log + ("Converting all textures to PNG. This may take a while if this is the first time running this operation...", LoggerLevelStartup.Instance); + + ConcurrentQueue fileQueue = new(); + + foreach (string filename in Directory.GetFiles("r")) fileQueue.Enqueue(filename); + + for(int i = 0; i < Environment.ProcessorCount; i++) + { + Task.Factory.StartNew + ( + () => + { + while (fileQueue.TryDequeue(out string? filename)) + { + LbpFile? file = LbpFile.FromHash(filename.Replace("r" + Path.DirectorySeparatorChar, "")); + if (file == null) continue; + + if (file.FileType == LbpFileType.Jpeg || file.FileType == LbpFileType.Png || file.FileType == LbpFileType.Texture) + { + ImageHelper.LbpFileToPNG(file); + } + } + } + ); + } + + while (!fileQueue.IsEmpty) + { + Thread.Sleep(100); + } + } + } + } \ No newline at end of file diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index eda0cb43..65a7077f 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -1,14 +1,10 @@ #nullable enable using System; -using System.Collections.Concurrent; using System.Diagnostics; -using System.IO; using System.Threading; -using System.Threading.Tasks; using Kettu; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; -using LBPUnion.ProjectLighthouse.Types.Files; using LBPUnion.ProjectLighthouse.Types.Settings; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; @@ -82,41 +78,7 @@ public static class Program return; } - FileHelper.EnsureDirectoryCreated(Path.Combine(Environment.CurrentDirectory, "png")); - if (Directory.Exists("r")) - { - Logger.Log - ("Converting all textures to PNG. This may take a while if this is the first time running this operation...", LoggerLevelStartup.Instance); - - ConcurrentQueue fileQueue = new(); - - foreach (string filename in Directory.GetFiles("r")) fileQueue.Enqueue(filename); - - for(int i = 0; i < Environment.ProcessorCount; i++) - { - Task.Factory.StartNew - ( - () => - { - while (fileQueue.TryDequeue(out string? filename)) - { - LbpFile? file = LbpFile.FromHash(filename.Replace("r" + Path.DirectorySeparatorChar, "")); - if (file == null) continue; - - if (file.FileType == LbpFileType.Jpeg || file.FileType == LbpFileType.Png || file.FileType == LbpFileType.Texture) - { - ImageHelper.LbpFileToPNG(file); - } - } - } - ); - } - - while (!fileQueue.IsEmpty) - { - Thread.Sleep(100); - } - } + FileHelper.ConvertAllTexturesToPng(); stopwatch.Stop(); Logger.Log($"Ready! Startup took {stopwatch.ElapsedMilliseconds}ms. Passing off control to ASP.NET...", LoggerLevelStartup.Instance);