Add TXTG loading for loaded bfres

This commit is contained in:
KillzXGaming 2023-05-28 16:59:31 -04:00
parent a06e1fe8e0
commit 45d20e8f44
6 changed files with 91 additions and 6 deletions

View file

@ -20,7 +20,7 @@ using OpenTK;
namespace FirstPlugin namespace FirstPlugin
{ {
public class BFRES : BFRESWrapper, IFileFormat, ITextureContainer, IExportableModelContainer public class BFRES : BFRESWrapper, IFileFormat, ITextureContainer, IExportableModelContainer, IDisposable
{ {
public FileType FileType { get; set; } = FileType.Resource; public FileType FileType { get; set; } = FileType.Resource;
@ -786,6 +786,11 @@ namespace FirstPlugin
} }
public BFRESRenderBase BFRESRender; public BFRESRenderBase BFRESRender;
private MeshCodec MeshCodec;
public void Dispose() { MeshCodec.Dispose(); }
public void Load(System.IO.Stream stream) public void Load(System.IO.Stream stream)
{ {
CanSave = true; CanSave = true;
@ -802,7 +807,10 @@ namespace FirstPlugin
BFRESRender.ModelTransform = MarioCostumeEditor.SetTransform(FileName); BFRESRender.ModelTransform = MarioCostumeEditor.SetTransform(FileName);
BFRESRender.ResFileNode = this; BFRESRender.ResFileNode = this;
if (MeshCodec.IsMeshCodec(stream)) MeshCodec = new MeshCodec();
bool isMeshCodec = MeshCodec.IsMeshCodec(stream);
if (isMeshCodec)
MeshCodec.Prepare(); MeshCodec.Prepare();
if (IsWiiU) if (IsWiiU)
@ -826,6 +834,9 @@ namespace FirstPlugin
} }
} }
if (isMeshCodec)
MeshCodec.PrepareTexToGo(resFile);
DrawableContainer.Drawables.Add(BFRESRender); DrawableContainer.Drawables.Add(BFRESRender);
var Models = GetModels(); var Models = GetModels();

View file

@ -6,9 +6,9 @@ using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using AampLibraryCSharp.IO;
using Syroot.NintenTools.NSW.Bfres; using Syroot.NintenTools.NSW.Bfres;
using Toolbox.Library; using Toolbox.Library;
using Toolbox.Library.IO;
using ZstdSharp.Unsafe; using ZstdSharp.Unsafe;
namespace FirstPlugin namespace FirstPlugin
@ -17,6 +17,8 @@ namespace FirstPlugin
{ {
static ResFile ExternalStringBinary; static ResFile ExternalStringBinary;
List<TXTG> TextureList = new List<TXTG>();
public static bool IsMeshCodec(Stream stream) public static bool IsMeshCodec(Stream stream)
{ {
using (var reader = new FileReader(stream, true)) using (var reader = new FileReader(stream, true))
@ -55,6 +57,38 @@ namespace FirstPlugin
LoadExternalStrings(); 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<string> 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() static void LoadExternalStrings()
{ {
if (ExternalStringBinary != null) if (ExternalStringBinary != null)

View file

@ -18,7 +18,7 @@ using VGAudio.Utilities;
namespace FirstPlugin namespace FirstPlugin
{ {
public class TXTG : STGenericTexture, IFileFormat, ILeaveOpenOnLoad public class TXTG : STGenericTexture, IFileFormat, ILeaveOpenOnLoad, IDisposable
{ {
public FileType FileType { get; set; } = FileType.Image; public FileType FileType { get; set; } = FileType.Image;
@ -144,7 +144,6 @@ namespace FirstPlugin
public void Load(Stream stream) public void Load(Stream stream)
{ {
Text = FileName;
Tag = this; Tag = this;
CanReplace = true; CanReplace = true;
@ -152,6 +151,15 @@ namespace FirstPlugin
ImageKey = "Texture"; ImageKey = "Texture";
SelectedImageKey = "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)) using (var reader = new FileReader(stream, true))
{ {
reader.SetByteOrder(false); 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) public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0)
{ {
var data = ImageList[ArrayLevel][MipLevel]; var data = ImageList[ArrayLevel][MipLevel];
@ -373,6 +387,7 @@ namespace FirstPlugin
{ 0x505, TEX_FORMAT.BC3_UNORM_SRGB }, { 0x505, TEX_FORMAT.BC3_UNORM_SRGB },
{ 0x602, TEX_FORMAT.BC4_UNORM },
{ 0x606, TEX_FORMAT.BC4_UNORM }, { 0x606, TEX_FORMAT.BC4_UNORM },
{ 0x607, TEX_FORMAT.BC4_UNORM }, { 0x607, TEX_FORMAT.BC4_UNORM },
{ 0x702, TEX_FORMAT.BC5_UNORM }, { 0x702, TEX_FORMAT.BC5_UNORM },

View file

@ -436,6 +436,22 @@ namespace FirstPlugin
return BindBNTX(bntx, tex, shader, activeTex); 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; return true;

View file

@ -45,6 +45,8 @@ namespace FirstPlugin
foreach (TextureData tex in bntx.Textures.Values) foreach (TextureData tex in bntx.Textures.Values)
listView1.Items.Add(tex.Text, tex.Text, 0); listView1.Items.Add(tex.Text, tex.Text, 0);
} }
foreach (var tex in PluginRuntime.TextureCache.Values)
listView1.Items.Add(tex.Text, tex.Text, 0);
} }
@ -90,6 +92,9 @@ namespace FirstPlugin
if (bntx.Textures.ContainsKey(TexName)) if (bntx.Textures.ContainsKey(TexName))
DisplayTexture(bntx.Textures[TexName]); DisplayTexture(bntx.Textures[TexName]);
} }
if (PluginRuntime.TextureCache.ContainsKey(TexName))
DisplayTexture(PluginRuntime.TextureCache[TexName]);
} }
} }
} }

View file

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bfres.Structs; using Bfres.Structs;
using Toolbox.Library;
namespace FirstPlugin namespace FirstPlugin
{ {
@ -24,6 +25,9 @@ namespace FirstPlugin
public static bool UseSimpleBfresEditor = false; public static bool UseSimpleBfresEditor = false;
//I really don't recall why I never just used a global texture cache
public static Dictionary<string, STGenericTexture> TextureCache = new Dictionary<string, STGenericTexture>();
public static Dictionary<string, BFLIM> bflimTextures = new Dictionary<string, BFLIM>(); public static Dictionary<string, BFLIM> bflimTextures = new Dictionary<string, BFLIM>();
public static List<BNTX> bntxContainers = new List<BNTX>(); public static List<BNTX> bntxContainers = new List<BNTX>();
public static List<BFRESGroupNode> ftexContainers = new List<BFRESGroupNode>(); public static List<BFRESGroupNode> ftexContainers = new List<BFRESGroupNode>();