XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.IO.Compression;
using System.Text.Json;
using BCnEncoder.Decoder;
using BCnEncoder.Shared;
using XFEEDToolBox.Core.Models;

namespace XFEEDToolBox.Core.Rendering;

public sealed record EliteModuleIconReference(string Name,string ResourceHash,string SourceSha256,int DeflateOffset,int InflatedBytes,int GpuOffset,int Width,int Height,int RowPitch,int Format);
public sealed record ModuleIconPixels(int Width,int Height,byte[] Rgba,string ResourceHash);

public static class EliteModuleIcons
{
    public static IReadOnlyList<EliteModuleIconReference> References {get;}=ReadReferences();
    private static IReadOnlyList<EliteModuleIconReference> ReadReferences()
    {
        using var stream=typeof(EliteModuleIcons).Assembly.GetManifestResourceStream("XFEEDToolBox.Core.Rendering.local-module-icons.json")!;
        return JsonSerializer.Deserialize<EliteModuleIconReference[]>(stream,ToolboxSettings.JsonOptions)!;
    }
    public static string IconName(string item)
    {
        item=item.ToLowerInvariant();
        if(References.Any(r=>r.Name==item))return item;
        (string Token,string Icon)[] map=[("multicannon","multicannon"),("pulselaserburst","burstlaser"),("beamlaser","beamlaser"),("pulselaser","pulselaser"),("burstlaser","burstlaser"),("slugshot","fragcannon"),("railgun","railgun"),("plasmaaccelerator","plasmaaccelerator"),("minelauncher","mine"),("torpedo","torpedo"),("cannon","cannon"),("shieldcell","shieldcell"),("shieldgenerator","shieldgenerator"),("shieldbooster","shieldbooster"),("hyperdrive","fsd"),("engine","engine"),("powerplant","powerplant"),("powerdistributor","powerdistributor"),("lifesupport","lifesupport"),("sensor","sensor"),("armour","armour"),("cargorack","cargorack"),("fuelscoop","fuelscoop"),("fueltank","fueltank"),("dockingcomputer","dockingcomputer"),("fighterbay","fighterhangar"),("buggybay","planetaryvehiclebay"),("passenger","passengercabin"),("hullreinforcement","hullreinforcement"),("modulereinforcement","modulereinforcement"),("heatsink","heatsinklauncher"),("chaff","chafflauncher"),("pointdefence","pointdefenceturret"),("electroniccountermeasure","ecm"),("repairer","afm"),("refinery","refineries"),("dronecontrol_collection","collectionlimpet"),("dronecontrol_prospector","prospectorlimpet"),("dronecontrol","multilimpet"),("detailedsurfacescanner","detailedsurfacescanner"),("fsdinterdictor","fsdinterdictor"),("fsdbooster","fsdbooster"),("cargoscanner","cargoscanner"),("crimescanner","killwarrantscanner"),("cloudscanner","frameshiftwakescanner"),("missile","missile"),("supercruiseassist","supercruiseassist"),("planetapproach","planetaryapproachsuite")];
        var key=map.FirstOrDefault(p=>item.Contains(p.Token,StringComparison.Ordinal)).Icon;
        if(item.Length==0)return "";
        return References.FirstOrDefault(r=>r.Name=="module_category_"+key+"_icon")?.Name??References.FirstOrDefault(r=>r.Name=="item_"+key+"_icon")?.Name??"item_module_icon";
    }
    public static ModuleIconPixels Read(EliteLocalModels resources,string name)
    {
        var entry=References.Single(r=>r.Name==name);
        var raw=resources.ReadDisplayResource(entry.ResourceHash,entry.SourceSha256);
        if(entry.DeflateOffset<144||entry.DeflateOffset>=raw.Length||!raw.AsSpan(0,4).SequenceEqual("FRES"u8)||entry.InflatedBytes>4*1024*1024)throw new InvalidDataException("模块图标封装无效。");
        using var compressed=new ZLibStream(new MemoryStream(raw,entry.DeflateOffset,raw.Length-entry.DeflateOffset,false),CompressionMode.Decompress);
        var data=new byte[entry.InflatedBytes];compressed.ReadExactly(data);
        if(compressed.ReadByte()!=-1||entry.Width is <1 or >512||entry.Height is <1 or >512||entry.RowPitch%16!=0)throw new InvalidDataException("模块图标长度或尺寸无效。");
        // This Cobra resource type stores DXT5/BC3 blocks, including padded rows. Its 0x63 tag is not a DXGI format.
        var paddedWidth=entry.RowPitch/16*4;var bytes=entry.RowPitch*((entry.Height+3)/4);
        if(paddedWidth<entry.Width||entry.GpuOffset<320||entry.GpuOffset+bytes!=data.Length)throw new InvalidDataException("模块图标纹理越界。");
        var decoded=new BcDecoder().DecodeRaw(data.AsSpan(entry.GpuOffset,bytes).ToArray(),paddedWidth,entry.Height,CompressionFormat.Bc3);
        var pixels=new byte[entry.Width*entry.Height*4];
        for(var y=0;y<entry.Height;y++)for(var x=0;x<entry.Width;x++)
        {
            var value=decoded[y*paddedWidth+x];var at=(y*entry.Width+x)*4;
            pixels[at]=255;pixels[at+1]=174;pixels[at+2]=72;pixels[at+3]=value.a;
        }
        return new(entry.Width,entry.Height,pixels,entry.ResourceHash);
    }
}