Convert all images to pngs on startup

This commit is contained in:
jvyden 2022-01-21 04:16:11 -05:00
commit c7909d7151
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 79 additions and 3 deletions

View file

@ -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<Rgba32> 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;
}
}

View file

@ -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<string> 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);