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

255 lines
6.7 KiB
C#
Raw Normal View History

2026-05-22 13:51:30 -05:00
using System.Text.RegularExpressions;
2026-04-26 18:02:45 -05:00
using BlitzKit.Game.Models;
2026-06-06 13:22:54 -05:00
using CUE4Parse_Conversion;
2026-06-09 12:43:14 -05:00
using CUE4Parse_Conversion.Materials;
2026-06-06 13:22:54 -05:00
using CUE4Parse_Conversion.Meshes;
2026-02-02 19:04:54 -06:00
using CUE4Parse_Conversion.Textures;
2026-06-06 13:22:54 -05:00
using CUE4Parse_Conversion.UEFormat.Enums;
2026-05-22 19:42:32 -05:00
using CUE4Parse.UE4.Assets.Exports;
2026-06-06 13:22:54 -05:00
using CUE4Parse.UE4.Assets.Exports.Engine;
2026-06-09 12:43:14 -05:00
using CUE4Parse.UE4.Assets.Exports.Material;
2026-06-06 13:22:54 -05:00
using CUE4Parse.UE4.Assets.Exports.StaticMesh;
2026-02-02 19:04:54 -06:00
using CUE4Parse.UE4.Assets.Exports.Texture;
2026-06-06 13:22:54 -05:00
using CUE4Parse.UE4.Assets.Objects;
2026-02-02 19:04:54 -06:00
using CUE4Parse.UE4.Objects.Engine;
using CUE4Parse.UE4.Objects.UObject;
2026-01-27 08:59:34 -06:00
using Microsoft.JavaScript.NodeApi;
2026-06-08 14:26:58 -05:00
using SharpGLTF.Materials;
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-06-08 14:26:58 -05:00
private readonly BlitzFileProvider provider;
2026-01-27 08:59:34 -06:00
2026-06-08 14:26:58 -05:00
public HashSet<string> files;
2026-01-27 08:59:34 -06:00
2026-06-08 14:26:58 -05:00
public GameInterface(string directory, string map, string temp)
{
provider = new(directory, map, temp);
files = [.. provider.Files.Keys];
}
2026-06-09 12:43:14 -05:00
readonly Dictionary<string, string> texturePaths = new();
public void DiscoverTextures(string[] tankTags)
{
Console.WriteLine("Discovering textures...");
var options = new ExporterOptions() { TextureFormat = ETextureFormat.Jpeg };
foreach (var tag in tankTags)
{
var pda = provider.Discovered<UPrimaryDataAsset>(tag);
foreach (var group in tankPartPrefixes.Values)
{
var dataTable = pda.Get<UDataTable>(group);
foreach (var row in dataTable.RowMap.Values)
{
var visualData = row.Get<UObject>("VisualData");
var meshSettings = visualData.Get<FStructFallback>("MeshSettings");
foreach (var property in meshSettings.Properties)
{
if (meshSettings.TryGet<UStaticMesh>(property.Name.Text, out var mesh))
{
mesh.TryConvert(out var convertedMesh);
var lod0 = convertedMesh.LODs.Find(lod => !lod.SkipLod);
if (lod0 == null || lod0.Sections == null)
{
continue;
}
foreach (var section in lod0.Sections.Value)
{
if (section.Material == null)
{
continue;
}
var materialInterface = section.Material.Load<UMaterialInterface>();
if (materialInterface == null)
{
continue;
}
var materialData = new MaterialData() { Parameters = new() };
materialInterface.GetParams(materialData.Parameters, options.MaterialFormat);
foreach (var parameterTexture in materialData.Parameters.Textures)
{
if (MonoGltf.knownChannels.TryGetValue(parameterTexture.Key, out var channel))
{
var path = parameterTexture.Value.GetPathName();
var name = Path.GetFileNameWithoutExtension(path);
texturePaths[name] = path;
}
}
}
}
}
}
}
}
Console.WriteLine($"Discovered {texturePaths.Count} textures");
}
public List<string> Textures => [.. texturePaths.Keys];
2026-06-09 12:48:52 -05:00
public byte[] Texture(string name)
{
var path = texturePaths[name];
var texture = provider.LoadPackageObject<UTexture2D>(path);
return provider.Image(texture);
}
2026-06-08 14:26:58 -05:00
public HashSet<string> Files => files;
public byte[] TankBigIcon(string tag)
{
var pda = provider.Discovered<UPrimaryDataAsset>(tag);
var icon = pda.Get<FSoftObjectPath>("BigIcon");
return provider.Image(icon);
}
[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)
2026-04-26 18:02:45 -05:00
{
2026-06-08 14:26:58 -05:00
if (!path.StartsWith(flagsBase))
{
continue;
}
2026-04-26 18:02:45 -05:00
2026-06-08 14:26:58 -05:00
var fileName = Path.GetFileNameWithoutExtension(path);
var match = FlagNameRegex().Match(fileName);
2026-04-26 18:02:45 -05:00
2026-06-08 14:26:58 -05:00
if (
match.Success
&& match.Groups[1].Value.Equals(nation, StringComparison.CurrentCultureIgnoreCase)
)
{
if (provider.TryLoadPackageObject<UTexture2D>($"{path}.{fileName}", out var uTexture))
2026-04-26 18:02:45 -05:00
{
2026-06-08 14:26:58 -05:00
CTexture? cTexture = uTexture.Decode(ETexturePlatform.DesktopMobile);
2026-05-23 14:30:52 -05:00
2026-06-08 14:26:58 -05:00
if (cTexture is null)
{
Console.WriteLine($"No flag found for {nation}");
return [];
}
2026-05-23 14:30:52 -05:00
2026-06-08 14:26:58 -05:00
SKBitmap bitmap = cTexture.ToSkBitmap();
SKData data = bitmap.Encode(SKEncodedImageFormat.Webp, 80);
2026-05-23 14:30:52 -05:00
2026-06-08 14:26:58 -05:00
return data.ToArray();
2026-04-26 18:02:45 -05:00
}
2026-05-22 19:42:32 -05:00
Console.WriteLine($"No flag found for {nation}");
2026-05-23 14:30:52 -05:00
return [];
2026-06-08 14:26:58 -05:00
}
2026-02-01 21:05:38 -06:00
}
2026-06-08 14:26:58 -05:00
Console.WriteLine($"No flag found for {nation}");
return [];
}
public byte[] ConsumableIcon(string tag)
{
var pda = provider.Discovered<UPrimaryDataAsset>(tag);
var icon = pda.Get<FSoftObjectPath>("Icon");
return provider.Image(icon);
}
public byte[] EquipmentIcon(string tag)
{
var pda = provider.Discovered<UPrimaryDataAsset>(tag);
var icon = pda.Get<FSoftObjectPath>("Icon");
return provider.Image(icon);
}
readonly Dictionary<string, string> tankPartPrefixes = new()
{
{ "chassis", "DT_Chassis" },
{ "hull", "DT_Hulls" },
{ "turret", "DT_Turrets" },
{ "gun", "DT_Guns" },
};
2026-06-08 19:46:18 -05:00
public byte[] TankPart(string tag, string part)
2026-06-08 14:26:58 -05:00
{
2026-06-08 19:17:27 -05:00
try
2026-06-05 20:53:28 -05:00
{
2026-06-08 19:17:27 -05:00
var pda = provider.Discovered<UPrimaryDataAsset>(tag);
UDataTable? dataTable = null;
foreach (var (prefix, name) in tankPartPrefixes)
2026-06-08 14:26:58 -05:00
{
2026-06-08 19:17:27 -05:00
if (!part.StartsWith(prefix))
{
continue;
}
dataTable = pda.Get<UDataTable>(name);
break;
2026-06-08 14:26:58 -05:00
}
2026-06-08 19:17:27 -05:00
if (dataTable == null)
{
throw new ArgumentException($"Unknown tank part: {part}");
}
2026-06-05 11:05:19 -05:00
2026-06-08 19:17:27 -05:00
dataTable.TryGetDataTableRow(part, StringComparison.Ordinal, out var row);
if (row == null)
{
throw new ArgumentException($"Unknown tank part: {part}");
}
var visualData = row.Get<UObject>("VisualData");
var meshSettings = visualData.Get<FStructFallback>("MeshSettings");
2026-06-09 14:00:24 -05:00
var meshes = new List<UStaticMesh>();
2026-06-08 19:17:27 -05:00
2026-06-09 14:00:24 -05:00
foreach (var property in meshSettings.Properties)
{
var name = property.Name.Text;
if (name == "CollisionMesh")
{
continue;
}
if (meshSettings.TryGet<UStaticMesh>(name, out var mesh))
{
meshes.Add(mesh);
}
}
var gltf = new MonoGltf(meshes);
2026-06-08 19:17:27 -05:00
2026-06-08 20:14:55 -05:00
return gltf.Write($"{tag}/{part}");
2026-06-08 19:17:27 -05:00
}
catch (Exception e)
2026-06-05 20:53:28 -05:00
{
2026-06-09 13:30:40 -05:00
Console.WriteLine(e);
2026-06-05 20:53:28 -05:00
}
2026-06-09 13:30:40 -05:00
return [];
2026-06-08 14:26:58 -05:00
}
2026-01-27 08:59:34 -06:00
}