mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-02 18:18:39 +00:00
Convert all images to pngs on startup
This commit is contained in:
parent
831e367078
commit
c7909d7151
2 changed files with 79 additions and 3 deletions
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue