Files
blitzkit/packages/game/JSExport/GameInterface.cs
T

114 lines
2.9 KiB
C#
Raw Normal View History

2026-04-26 18:02:45 -05:00
using System.IO;
using System.Text.RegularExpressions;
using BlitzKit.Game.Models;
2026-02-02 19:04:54 -06:00
using CUE4Parse_Conversion.Textures;
using CUE4Parse.UE4.Assets.Exports.Texture;
using CUE4Parse.UE4.Objects.Engine;
using CUE4Parse.UE4.Objects.UObject;
2026-01-27 08:59:34 -06:00
using Microsoft.JavaScript.NodeApi;
2026-04-18 00:30:21 -05:00
using SkiaSharp;
2026-01-27 08:59:34 -06:00
2026-02-02 17:28:24 -06:00
namespace BlitzKit.Game.JSExport;
2026-01-27 08:59:34 -06:00
[JSExport]
2026-04-26 18:02:45 -05:00
public partial class GameInterface
2026-01-27 08:59:34 -06:00
{
2026-02-02 17:08:37 -06:00
private readonly BlitzFileProvider provider;
2026-01-27 08:59:34 -06:00
2026-02-01 19:34:36 -06:00
public string[] files;
public string[] tanksDirectoryNations;
2026-01-27 08:59:34 -06:00
2026-02-02 17:08:37 -06:00
public GameInterface(string directory)
2026-02-01 19:34:36 -06:00
{
2026-02-02 17:08:37 -06:00
provider = new(directory);
2026-02-01 21:05:38 -06:00
2026-02-01 19:34:36 -06:00
files = [.. provider.Files.Keys];
var tanksBase = "Blitz/Content/Tanks/";
tanksDirectoryNations =
[
.. files
.Where(path => path.StartsWith(tanksBase))
.Select(path => path[tanksBase.Length..])
.Select(path => path.Split('/')[0])
.Where(nation => !nation.EndsWith(".uasset") && nation != "TankStub")
.Distinct(),
];
}
public string[] Files => files;
public string[] TanksDirectoryNations => tanksDirectoryNations;
public byte[] TankBigIcon(string tankId, string pdaName)
{
2026-02-01 21:05:38 -06:00
foreach (var nation in tanksDirectoryNations)
{
var path = $"Blitz/Content/Tanks/{nation}/{tankId}/{pdaName}.{pdaName}";
2026-02-02 19:04:54 -06:00
if (provider.TryLoadPackageObject<UPrimaryDataAsset>(path, out var pda))
2026-02-01 21:05:38 -06:00
{
2026-04-18 00:30:21 -05:00
FSoftObjectPath objectPath = pda.Get<FSoftObjectPath>("BigIcon");
UTexture2D uTexture = objectPath.Load<UTexture2D>();
CTexture? cTexture = uTexture.Decode(ETexturePlatform.DesktopMobile);
if (cTexture is null)
2026-02-02 19:57:15 -06:00
{
2026-04-26 18:02:45 -05:00
return [];
2026-02-02 19:57:15 -06:00
}
2026-04-18 00:30:21 -05:00
SKBitmap bitmap = cTexture.ToSkBitmap();
SKData data = bitmap.Encode(SKEncodedImageFormat.Webp, 80);
byte[] bytes = data.ToArray();
return bytes;
2026-02-01 21:05:38 -06:00
}
2026-04-26 18:02:45 -05:00
}
return [];
}
[GeneratedRegex(@"T_UI_Flag_(\w+)_S")]
private static partial Regex FlagNameRegex();
public byte[] Flag(string nation)
{
var flagsBase = "Blitz/Content/UI/Textures/Flag/TankCard/";
foreach (var path in provider.Files.Keys)
{
if (!path.StartsWith(flagsBase))
2026-02-01 21:05:38 -06:00
{
continue;
}
2026-04-26 18:02:45 -05:00
var fileName = Path.GetFileNameWithoutExtension(path);
var match = FlagNameRegex().Match(fileName);
if (
match.Success
&& match.Groups[1].Value.Equals(nation, StringComparison.CurrentCultureIgnoreCase)
)
{
if (provider.TryLoadPackageObject<UTexture2D>($"{path}.{fileName}", out var uTexture))
{
CTexture? cTexture = uTexture.Decode(ETexturePlatform.DesktopMobile);
if (cTexture is null)
{
return [];
}
SKBitmap bitmap = cTexture.ToSkBitmap();
SKData data = bitmap.Encode(SKEncodedImageFormat.Webp, 80);
byte[] bytes = data.ToArray();
return bytes;
}
return [];
}
2026-02-01 21:05:38 -06:00
}
2026-04-18 00:30:21 -05:00
return [];
2026-02-01 19:34:36 -06:00
}
2026-01-27 08:59:34 -06:00
}