From eab2ff1dfca3163ffc48ac89a88117f40444ebbd Mon Sep 17 00:00:00 2001 From: Venomalia Date: Tue, 13 Jun 2023 12:17:13 +0200 Subject: [PATCH] support for brtex & brplt pairs --- TextureExtraction tool/Data/ScanBase.cs | 2 +- lib/AuroraLip/Archives/Formats/bres.cs | 31 ++++++++++++++++++++++++- lib/AuroraLip/Texture/Formats/TEX0.cs | 10 +++++--- lib/AuroraLip/Texture/ImageEX.cs | 4 ++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/TextureExtraction tool/Data/ScanBase.cs b/TextureExtraction tool/Data/ScanBase.cs index 28ac879d..9226f476 100644 --- a/TextureExtraction tool/Data/ScanBase.cs +++ b/TextureExtraction tool/Data/ScanBase.cs @@ -404,7 +404,7 @@ protected virtual bool TryExtract(ScanObjekt so) else { ArchiveFile file = so.File; - archive.FileRequest = new Events.FileRequestDelegate(N => ((ArchiveFile)file.Parent[N]).FileData); + archive.FileRequest = new Events.FileRequestDelegate(N => ((ArchiveFile)file?.Parent[N]).FileData); } archive.Open(so.Stream, so.SubPath.ToString()); long size = archive.Root.Size; diff --git a/lib/AuroraLip/Archives/Formats/bres.cs b/lib/AuroraLip/Archives/Formats/bres.cs index f41efe59..a9a75a1e 100644 --- a/lib/AuroraLip/Archives/Formats/bres.cs +++ b/lib/AuroraLip/Archives/Formats/bres.cs @@ -45,8 +45,26 @@ protected override void Read(Stream stream) throw new InvalidIdentifierException("root"); uint RootSize = stream.ReadUInt32(ByteOrder); Root = new ArchiveDirectory() { Name = "root", OwnerArchive = this }; - ReadIndex(stream, (int)(stream.Position + RootSize - 8), Root); //Index Group + ReadIndex(stream, (int)(stream.Position + RootSize - 8), Root); + + //is brtex & brplt pair + if (Root.Items.Count == 1 && Root.ItemExists("Textures(NW4R)")) + { + //try to request an external file. + string datname = Path.ChangeExtension(Path.GetFileNameWithoutExtension(FullPath), ".brplt"); + try + { + reference_stream = FileRequest.Invoke(datname); + Bres plt = new (reference_stream, FullPath); + foreach (var item in plt.Root.Items) + { + Root.Items.Add(item.Key, item.Value); + } + } + catch (Exception) + { } + } } private void ReadIndex(Stream stream, in int EndOfRoot, ArchiveDirectory ParentDirectory) @@ -118,6 +136,17 @@ protected override void Write(Stream ArchiveFile) throw new NotImplementedException(); } + private Stream reference_stream; + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + if (disposing) + { + reference_stream?.Dispose(); + } + } + public unsafe struct Header { private fixed char magic[4]; diff --git a/lib/AuroraLip/Texture/Formats/TEX0.cs b/lib/AuroraLip/Texture/Formats/TEX0.cs index 1beee5b6..28169a85 100644 --- a/lib/AuroraLip/Texture/Formats/TEX0.cs +++ b/lib/AuroraLip/Texture/Formats/TEX0.cs @@ -71,15 +71,15 @@ protected void Read(Stream stream, byte[] PaletteData, GXPaletteFormat PaletteFo Archive ParentBres = substream.Parent.OwnerArchive; if (ParentBres.ItemExists("Palettes(NW4R)")) { - var PalletNames = ((ArchiveDirectory)ParentBres["Palettes(NW4R)"]).FindItems(name + "*"); + List PalletNames = ((ArchiveDirectory)ParentBres["Palettes(NW4R)"]).FindItems(name + "*"); if (PalletNames.Count == 0) { - throw new PaletteException("No palette data could be found"); + throw new PaletteException("No linked pallete palette data could be found"); } stream.Position = SectionOffsets; - var tex = new TexEntry(stream, Format, ImageWidth, ImageHeight, TotalImageCount - 1) + TexEntry tex = new TexEntry(stream, Format, ImageWidth, ImageHeight, TotalImageCount - 1) { LODBias = 0, MagnificationFilter = GXFilterMode.Nearest, @@ -103,6 +103,10 @@ protected void Read(Stream stream, byte[] PaletteData, GXPaletteFormat PaletteFo Add(tex); return; } + else + { + throw new PaletteException("No palette data could be found"); + } } } stream.Position = SectionOffsets; diff --git a/lib/AuroraLip/Texture/ImageEX.cs b/lib/AuroraLip/Texture/ImageEX.cs index bea5ba6b..6462e298 100644 --- a/lib/AuroraLip/Texture/ImageEX.cs +++ b/lib/AuroraLip/Texture/ImageEX.cs @@ -6,6 +6,7 @@ using SixLabors.ImageSharp.Processing.Processors.Transforms; using System.Drawing; using System.Numerics; +using System.Reflection.PortableExecutable; namespace AuroraLib.Texture { @@ -75,6 +76,9 @@ public static Image DecodeImage(this GXImageFormat Format, ReadOnlySpan< public static Image ApplyPalette(this Image image, ReadOnlySpan palette, Converter pixelToIndex) where TPixel : unmanaged, IPixel where XPixel : unmanaged, IPixel { + if (palette.Length == 0) + throw new PaletteException("No palette data to apply."); + Image paletteImage = new(image.Width, image.Height); IMemoryGroup pixelsPalette = paletteImage.GetPixelMemoryGroup();