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 References {get;}=ReadReferences(); private static IReadOnlyList ReadReferences() { using var stream=typeof(EliteModuleIcons).Assembly.GetManifestResourceStream("XFEEDToolBox.Core.Rendering.local-module-icons.json")!; return JsonSerializer.Deserialize(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