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

299 lines
8.1 KiB
C#
Raw Normal View History

2026-06-10 20:12:18 -05:00
using System.Text.Json;
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-12 13:43:58 -05:00
using CUE4Parse.UE4.Assets.Exports.Component;
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-12 13:43:58 -05:00
using CUE4Parse.UE4.Assets.Exports.SkeletalMesh;
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-06-12 13:43:58 -05:00
using CUE4Parse.UE4.Assets.Objects.Properties;
2026-02-02 19:04:54 -06:00
using CUE4Parse.UE4.Objects.Engine;
2026-06-12 13:43:58 -05:00
using CUE4Parse.UE4.Objects.GameplayTags;
2026-02-02 19:04:54 -06:00
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)
{
2026-06-10 13:36:24 -05:00
provider = new(directory, map, temp);
2026-06-08 14:26:58 -05:00
files = [.. provider.Files.Keys];
}
2026-06-09 12:43:14 -05:00
readonly Dictionary<string, string> texturePaths = new();
2026-06-10 20:12:18 -05:00
readonly string textureCache = "../../temp/textures.json";
2026-06-09 12:43:14 -05:00
public void DiscoverTextures(string[] tankTags)
{
2026-06-10 20:12:18 -05:00
if (File.Exists(textureCache))
{
Console.WriteLine("Found textures cache");
var content = File.ReadAllText(textureCache);
var cached = JsonSerializer.Deserialize<Dictionary<string, string>>(content);
foreach (var (key, value) in cached)
{
texturePaths[key] = value;
}
return;
}
Console.WriteLine("Textures cache not found, building...");
2026-06-09 12:43:14 -05:00
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");
2026-06-10 20:12:18 -05:00
Directory.CreateDirectory(Path.GetDirectoryName(textureCache));
var json = JsonSerializer.Serialize(texturePaths);
File.WriteAllText(textureCache, json);
Console.WriteLine("Saved textures cache");
2026-06-09 12:43:14 -05:00
}
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);
2026-06-12 20:59:43 -05:00
var isRMAO = name.EndsWith("_RMAO");
var postProcess = isRMAO ? ImagePostProcess.RMAO : ImagePostProcess.None;
return provider.Image(texture, postProcess);
2026-06-09 12:48:52 -05:00
}
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-10 20:12:18 -05:00
var pda = provider.Discovered<UPrimaryDataAsset>(tag);
UDataTable? dataTable = null;
foreach (var (prefix, name) in tankPartPrefixes)
2026-06-05 20:53:28 -05:00
{
2026-06-10 20:12:18 -05:00
if (!part.StartsWith(prefix))
2026-06-08 14:26:58 -05:00
{
2026-06-10 20:12:18 -05:00
continue;
2026-06-08 14:26:58 -05:00
}
2026-06-10 20:12:18 -05:00
dataTable = pda.Get<UDataTable>(name);
break;
2026-06-05 20:53:28 -05:00
}
2026-06-09 13:30:40 -05:00
2026-06-10 20:12:18 -05:00
if (dataTable == null)
{
throw new ArgumentException($"Unknown tank part: {part}");
}
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");
// var meshes = CollectMeshes(meshSettings);
// var gltf = new MonoGltf(meshes);
var gltf = new MonoGltf(meshSettings);
2026-06-10 20:12:18 -05:00
return gltf.Write($"{tag}/{part}");
2026-06-08 14:26:58 -05:00
}
2026-06-12 13:43:58 -05:00
2026-06-12 14:17:29 -05:00
static List<(string, UObject)> CollectMeshes(FStructFallback settings)
2026-06-12 13:43:58 -05:00
{
2026-06-12 14:17:29 -05:00
var meshes = new List<(string, UObject)>();
2026-06-12 13:43:58 -05:00
foreach (var property in settings.Properties)
{
var key = property.Name.Text;
if (settings.TryGet<UStaticMesh>(key, out var staticMesh))
{
2026-06-12 14:17:29 -05:00
meshes.Add((key, staticMesh));
2026-06-12 13:43:58 -05:00
continue;
}
if (settings.TryGet<USkeletalMesh>(key, out var skeletalMesh))
{
2026-06-12 14:17:29 -05:00
meshes.Add((key, skeletalMesh));
2026-06-12 13:43:58 -05:00
continue;
}
if (settings.TryGet<FStructFallback[]>(key, out var array))
{
foreach (var item in array)
{
meshes.AddRange(CollectMeshes(item));
}
continue;
}
2026-06-12 14:17:29 -05:00
// Console.WriteLine(
// $"{key} -> {property.Tag} ({property.TagData}) [{property.PropertyTagFlags}]"
// );
2026-06-12 13:43:58 -05:00
}
return meshes;
}
2026-01-27 08:59:34 -06:00
}