From 45d20e8f4427113eb0ae54b14a23f1d64d24917b Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Sun, 28 May 2023 16:59:31 -0400 Subject: [PATCH] Add TXTG loading for loaded bfres --- .../FileFormats/BFRES/BFRES.cs | 15 ++++++-- .../FileFormats/MeshCodec/MeshCodec.cs | 36 ++++++++++++++++++- .../FileFormats/Texture/TXTG.cs | 19 ++++++++-- File_Format_Library/GL/BFRES/BFRES_Render.cs | 16 +++++++++ .../GUI/BFRES/Materials/Texture Selector.cs | 7 +++- File_Format_Library/PluginRuntime.cs | 4 +++ 6 files changed, 91 insertions(+), 6 deletions(-) diff --git a/File_Format_Library/FileFormats/BFRES/BFRES.cs b/File_Format_Library/FileFormats/BFRES/BFRES.cs index d67c2ed5..ab0934b1 100644 --- a/File_Format_Library/FileFormats/BFRES/BFRES.cs +++ b/File_Format_Library/FileFormats/BFRES/BFRES.cs @@ -20,7 +20,7 @@ using OpenTK; namespace FirstPlugin { - public class BFRES : BFRESWrapper, IFileFormat, ITextureContainer, IExportableModelContainer + public class BFRES : BFRESWrapper, IFileFormat, ITextureContainer, IExportableModelContainer, IDisposable { public FileType FileType { get; set; } = FileType.Resource; @@ -786,6 +786,11 @@ namespace FirstPlugin } public BFRESRenderBase BFRESRender; + + private MeshCodec MeshCodec; + + public void Dispose() { MeshCodec.Dispose(); } + public void Load(System.IO.Stream stream) { CanSave = true; @@ -802,7 +807,10 @@ namespace FirstPlugin BFRESRender.ModelTransform = MarioCostumeEditor.SetTransform(FileName); BFRESRender.ResFileNode = this; - if (MeshCodec.IsMeshCodec(stream)) + MeshCodec = new MeshCodec(); + + bool isMeshCodec = MeshCodec.IsMeshCodec(stream); + if (isMeshCodec) MeshCodec.Prepare(); if (IsWiiU) @@ -826,6 +834,9 @@ namespace FirstPlugin } } + if (isMeshCodec) + MeshCodec.PrepareTexToGo(resFile); + DrawableContainer.Drawables.Add(BFRESRender); var Models = GetModels(); diff --git a/File_Format_Library/FileFormats/MeshCodec/MeshCodec.cs b/File_Format_Library/FileFormats/MeshCodec/MeshCodec.cs index 3a16b1f7..5cc0e2bb 100644 --- a/File_Format_Library/FileFormats/MeshCodec/MeshCodec.cs +++ b/File_Format_Library/FileFormats/MeshCodec/MeshCodec.cs @@ -6,9 +6,9 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -using AampLibraryCSharp.IO; using Syroot.NintenTools.NSW.Bfres; using Toolbox.Library; +using Toolbox.Library.IO; using ZstdSharp.Unsafe; namespace FirstPlugin @@ -17,6 +17,8 @@ namespace FirstPlugin { static ResFile ExternalStringBinary; + List TextureList = new List(); + public static bool IsMeshCodec(Stream stream) { using (var reader = new FileReader(stream, true)) @@ -55,6 +57,38 @@ namespace FirstPlugin LoadExternalStrings(); } + public void Dispose() + { + foreach (var tex in this.TextureList) + { + tex.Dispose(); + } + TextureList.Clear(); + } + + public void PrepareTexToGo(ResFile resFile) + { + var materials = resFile.Models.SelectMany(x => x.Materials); + + List textureList = materials.SelectMany(x => x.TextureRefs).Distinct().ToList(); + foreach (var tex in textureList) + { + string path = Path.Combine(Runtime.TotkGamePath, "TexToGo", $"{tex}.txtg"); + if (File.Exists(path)) + { + try + { + //File will be loaded and cached from TXTG load method + TextureList.Add((TXTG)STFileLoader.OpenFileFormat(path)); + } + catch + { + + } + } + } + } + static void LoadExternalStrings() { if (ExternalStringBinary != null) diff --git a/File_Format_Library/FileFormats/Texture/TXTG.cs b/File_Format_Library/FileFormats/Texture/TXTG.cs index 54000739..6e069d02 100644 --- a/File_Format_Library/FileFormats/Texture/TXTG.cs +++ b/File_Format_Library/FileFormats/Texture/TXTG.cs @@ -18,7 +18,7 @@ using VGAudio.Utilities; namespace FirstPlugin { - public class TXTG : STGenericTexture, IFileFormat, ILeaveOpenOnLoad + public class TXTG : STGenericTexture, IFileFormat, ILeaveOpenOnLoad, IDisposable { public FileType FileType { get; set; } = FileType.Image; @@ -144,7 +144,6 @@ namespace FirstPlugin public void Load(Stream stream) { - Text = FileName; Tag = this; CanReplace = true; @@ -152,6 +151,15 @@ namespace FirstPlugin ImageKey = "Texture"; SelectedImageKey = "Texture"; + string name = Path.GetFileNameWithoutExtension(FileName); + Text = name; + + //cache for loading file + if (PluginRuntime.TextureCache.ContainsKey(name)) + PluginRuntime.TextureCache.Remove(name); + + PluginRuntime.TextureCache.Add(name, this); + using (var reader = new FileReader(stream, true)) { reader.SetByteOrder(false); @@ -266,6 +274,12 @@ namespace FirstPlugin } } + public void Dispose() + { + if (PluginRuntime.TextureCache.ContainsKey(FileName)) + PluginRuntime.TextureCache.Remove(FileName); + } + public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0) { var data = ImageList[ArrayLevel][MipLevel]; @@ -373,6 +387,7 @@ namespace FirstPlugin { 0x505, TEX_FORMAT.BC3_UNORM_SRGB }, + { 0x602, TEX_FORMAT.BC4_UNORM }, { 0x606, TEX_FORMAT.BC4_UNORM }, { 0x607, TEX_FORMAT.BC4_UNORM }, { 0x702, TEX_FORMAT.BC5_UNORM }, diff --git a/File_Format_Library/GL/BFRES/BFRES_Render.cs b/File_Format_Library/GL/BFRES/BFRES_Render.cs index c3f24382..a2a4f9e4 100644 --- a/File_Format_Library/GL/BFRES/BFRES_Render.cs +++ b/File_Format_Library/GL/BFRES/BFRES_Render.cs @@ -436,6 +436,22 @@ namespace FirstPlugin return BindBNTX(bntx, tex, shader, activeTex); } } + + foreach (var bntx in PluginRuntime.bntxContainers) + { + if (bntx.Textures.ContainsKey(activeTex)) + { + return BindBNTX(bntx, tex, shader, activeTex); + } + } + if (PluginRuntime.TextureCache.ContainsKey(activeTex)) + { + var t = PluginRuntime.TextureCache[activeTex]; + if (t.RenderableTex == null || !t.RenderableTex.GLInitialized) + t.LoadOpenGLTexture(); + + BindGLTexture(tex, shader, t); + } } return true; diff --git a/File_Format_Library/GUI/BFRES/Materials/Texture Selector.cs b/File_Format_Library/GUI/BFRES/Materials/Texture Selector.cs index ad71a4f0..5b953788 100644 --- a/File_Format_Library/GUI/BFRES/Materials/Texture Selector.cs +++ b/File_Format_Library/GUI/BFRES/Materials/Texture Selector.cs @@ -45,6 +45,8 @@ namespace FirstPlugin foreach (TextureData tex in bntx.Textures.Values) listView1.Items.Add(tex.Text, tex.Text, 0); } + foreach (var tex in PluginRuntime.TextureCache.Values) + listView1.Items.Add(tex.Text, tex.Text, 0); } @@ -90,7 +92,10 @@ namespace FirstPlugin if (bntx.Textures.ContainsKey(TexName)) DisplayTexture(bntx.Textures[TexName]); } - } + + if (PluginRuntime.TextureCache.ContainsKey(TexName)) + DisplayTexture(PluginRuntime.TextureCache[TexName]); + } } } private void DisplayTexture(STGenericTexture texData) diff --git a/File_Format_Library/PluginRuntime.cs b/File_Format_Library/PluginRuntime.cs index ba4488a4..99e2c330 100644 --- a/File_Format_Library/PluginRuntime.cs +++ b/File_Format_Library/PluginRuntime.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Bfres.Structs; +using Toolbox.Library; namespace FirstPlugin { @@ -24,6 +25,9 @@ namespace FirstPlugin public static bool UseSimpleBfresEditor = false; + //I really don't recall why I never just used a global texture cache + public static Dictionary TextureCache = new Dictionary(); + public static Dictionary bflimTextures = new Dictionary(); public static List bntxContainers = new List(); public static List ftexContainers = new List();