diff --git a/ProjectLighthouse/Helpers/ImageHelper.cs b/ProjectLighthouse/Helpers/ImageHelper.cs index 691dc426..931d9fda 100644 --- a/ProjectLighthouse/Helpers/ImageHelper.cs +++ b/ProjectLighthouse/Helpers/ImageHelper.cs @@ -1,9 +1,12 @@ #nullable enable +using System; using System.IO; using DDSReader; using ICSharpCode.SharpZipLib.Zip.Compression; using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Types.Files; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; namespace LBPUnion.ProjectLighthouse.Helpers; @@ -15,12 +18,27 @@ public static class ImageHelper { if (type != LbpFileType.Jpeg && type != LbpFileType.Png && type != LbpFileType.Texture) return false; + if (File.Exists($"png/{hash}.png")) return true; + using MemoryStream ms = new(data); using BinaryReader reader = new(ms); - if (type == LbpFileType.Texture) return TextureToPNG(hash, reader); - - return false; + try + { + return type switch + { + LbpFileType.Texture => TextureToPNG(hash, reader), + LbpFileType.Png => PNGToPNG(hash), + LbpFileType.Jpeg => JPGToPNG(hash, data), + _ => false, + }; + } + catch(Exception e) + { + Console.WriteLine($"Error while converting {hash}:"); + Console.WriteLine(e); + return false; + } } private static bool TextureToPNG(string hash, BinaryReader reader) @@ -83,4 +101,22 @@ public static class ImageHelper File.WriteAllBytes($"png/{hash}.png", stream.ToArray()); return true; } + + private static bool JPGToPNG(string hash, byte[] data) + { + using Image image = Image.Load(data); + using MemoryStream ms = new(); + image.SaveAsPng(ms); + + File.WriteAllBytes($"png/{hash}.png", ms.ToArray()); + return true; + } + + // it sounds dumb i know but hear me out: + // you're completely correct + private static bool PNGToPNG(string hash) + { + File.Copy(FileHelper.GetResourcePath(hash), $"png/{hash}.png"); + return true; + } } \ No newline at end of file diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index 2d8131f9..9c06c834 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -1,9 +1,14 @@ +#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; @@ -77,6 +82,41 @@ public static class Program return; } + 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/", "")); + 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); + } + } + stopwatch.Stop(); Logger.Log($"Ready! Startup took {stopwatch.ElapsedMilliseconds}ms. Passing off control to ASP.NET...", LoggerLevelStartup.Instance);