Move PNG conversion to FileHelper

This commit is contained in:
jvyden 2022-02-17 16:12:21 -05:00
commit 487ec5bc8f
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 47 additions and 39 deletions

View file

@ -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<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" + 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);
}
}
}
}

View file

@ -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<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" + 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);