Skip to content

Commit

Permalink
Fix edit texture "change format" option not reading resS
Browse files Browse the repository at this point in the history
  • Loading branch information
nesrak1 committed Mar 8, 2024
1 parent eaf2f7f commit 6f1d85e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
20 changes: 16 additions & 4 deletions TexturePlugin/EditDialog.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
using AssetsTools.NET.Texture;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Platform.Storage;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using UABEAvalonia;
using Image = SixLabors.ImageSharp.Image;

Expand Down Expand Up @@ -91,7 +87,23 @@ private async void BtnSave_Click(object sender, Avalonia.Interactivity.RoutedEve
Image<Rgba32> imgToImport;
if (imagePath == null)
{
if (!TextureHelper.GetResSTexture(tex, fileInst))
{
string dialogText = "Texture uses resS, but the resS file wasn't found";
await MessageBoxUtil.ShowDialog(this, "Error", dialogText);
Close(false);
return;
}

byte[] data = TextureHelper.GetRawTextureBytes(tex, fileInst);
if (data == null)
{
string dialogText = "Couldn't get texture data";
await MessageBoxUtil.ShowDialog(this, "Error", dialogText);
Close(false);
return;
}

imgToImport = TextureImportExport.Export(data, tex.m_Width, tex.m_Height, (TextureFormat)tex.m_TextureFormat, platform, platformBlob);
}
else
Expand Down
52 changes: 7 additions & 45 deletions TexturePlugin/ExportTextureOption.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using AssetsTools.NET.Extra;
using AssetsTools.NET;
using AssetsTools.NET.Extra;
using AssetsTools.NET.Texture;
using AssetsTools.NET;
using Avalonia.Controls;
using System;
using Avalonia.Platform.Storage;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UABEAvalonia.Plugins;
using UABEAvalonia;
using System.IO;
using Avalonia.Platform.Storage;
using UABEAvalonia.Plugins;

namespace TexturePlugin
{
Expand Down Expand Up @@ -44,43 +43,6 @@ public async Task<bool> ExecutePlugin(Window win, AssetWorkspace workspace, List
return await SingleExport(win, workspace, selection);
}

private bool GetResSTexture(TextureFile texFile, AssetContainer cont)
{
TextureFile.StreamingInfo streamInfo = texFile.m_StreamData;
if (streamInfo.path != null && streamInfo.path != "" && cont.FileInstance.parentBundle != null)
{
//some versions apparently don't use archive:/
string searchPath = streamInfo.path;
if (searchPath.StartsWith("archive:/"))
searchPath = searchPath.Substring(9);

searchPath = Path.GetFileName(searchPath);

AssetBundleFile bundle = cont.FileInstance.parentBundle.file;

AssetsFileReader reader = bundle.DataReader;
AssetBundleDirectoryInfo[] dirInf = bundle.BlockAndDirInfo.DirectoryInfos;
for (int i = 0; i < dirInf.Length; i++)
{
AssetBundleDirectoryInfo info = dirInf[i];
if (info.Name == searchPath)
{
reader.Position = info.Offset + (long)streamInfo.offset;
texFile.pictureData = reader.ReadBytes((int)streamInfo.size);
texFile.m_StreamData.offset = 0;
texFile.m_StreamData.size = 0;
texFile.m_StreamData.path = "";
return true;
}
}
return false;
}
else
{
return true;
}
}

public async Task<bool> BatchExport(Window win, AssetWorkspace workspace, List<AssetContainer> selection)
{
for (int i = 0; i < selection.Count; i++)
Expand Down Expand Up @@ -122,7 +84,7 @@ public async Task<bool> BatchExport(Window win, AssetWorkspace workspace, List<A
string file = Path.Combine(dir, $"{assetName}-{Path.GetFileName(cont.FileInstance.path)}-{cont.PathId}.{fileType.ToLower()}");

//bundle resS
if (!GetResSTexture(texFile, cont))
if (!TextureHelper.GetResSTexture(texFile, cont.FileInstance))
{
string resSName = Path.GetFileName(texFile.m_StreamData.path);
errorBuilder.AppendLine($"[{errorAssetName}]: resS was detected but {resSName} was not found in bundle");
Expand Down Expand Up @@ -195,7 +157,7 @@ public async Task<bool> SingleExport(Window win, AssetWorkspace workspace, List<
string errorAssetName = $"{Path.GetFileName(cont.FileInstance.path)}/{cont.PathId}";

//bundle resS
if (!GetResSTexture(texFile, cont))
if (!TextureHelper.GetResSTexture(texFile, cont.FileInstance))
{
string resSName = Path.GetFileName(texFile.m_StreamData.path);
await MessageBoxUtil.ShowDialog(win, "Error", $"[{errorAssetName}]: resS was detected but {resSName} was not found in bundle");
Expand Down
44 changes: 39 additions & 5 deletions TexturePlugin/TextureHelper.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using AssetsTools.NET.Extra;
using AssetsTools.NET;
using AssetsTools.NET.Extra;
using AssetsTools.NET.Texture;
using AssetsTools.NET;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UABEAvalonia;

namespace TexturePlugin
Expand All @@ -32,6 +29,43 @@ public static AssetTypeValueField GetByteArrayTexture(AssetWorkspace workspace,
return baseField;
}

public static bool GetResSTexture(TextureFile texFile, AssetsFileInstance fileInst)
{
TextureFile.StreamingInfo streamInfo = texFile.m_StreamData;
if (streamInfo.path != null && streamInfo.path != "" && fileInst.parentBundle != null)
{
//some versions apparently don't use archive:/
string searchPath = streamInfo.path;
if (searchPath.StartsWith("archive:/"))
searchPath = searchPath.Substring(9);

searchPath = Path.GetFileName(searchPath);

AssetBundleFile bundle = fileInst.parentBundle.file;

AssetsFileReader reader = bundle.DataReader;
AssetBundleDirectoryInfo[] dirInf = bundle.BlockAndDirInfo.DirectoryInfos;
for (int i = 0; i < dirInf.Length; i++)
{
AssetBundleDirectoryInfo info = dirInf[i];
if (info.Name == searchPath)
{
reader.Position = info.Offset + (long)streamInfo.offset;
texFile.pictureData = reader.ReadBytes((int)streamInfo.size);
texFile.m_StreamData.offset = 0;
texFile.m_StreamData.size = 0;
texFile.m_StreamData.path = "";
return true;
}
}
return false;
}
else
{
return true;
}
}

public static byte[] GetRawTextureBytes(TextureFile texFile, AssetsFileInstance inst)
{
string rootPath = Path.GetDirectoryName(inst.path);
Expand Down

0 comments on commit 6f1d85e

Please sign in to comment.