optimize ResourceReader

This commit is contained in:
Perfare 2020-03-28 13:33:37 +08:00
parent 06fbe69a97
commit 182a42ace2
9 changed files with 25 additions and 26 deletions

View File

@ -10,7 +10,7 @@ namespace AssetStudio
{ {
public List<SerializedFile> assetsFileList = new List<SerializedFile>(); public List<SerializedFile> assetsFileList = new List<SerializedFile>();
internal Dictionary<string, int> assetsFileIndexCache = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); internal Dictionary<string, int> assetsFileIndexCache = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
internal Dictionary<string, EndianBinaryReader> resourceFileReaders = new Dictionary<string, EndianBinaryReader>(StringComparer.OrdinalIgnoreCase); internal Dictionary<string, BinaryReader> resourceFileReaders = new Dictionary<string, BinaryReader>(StringComparer.OrdinalIgnoreCase);
private List<string> importFiles = new List<string>(); private List<string> importFiles = new List<string>();
private HashSet<string> importFilesHash = new HashSet<string>(StringComparer.OrdinalIgnoreCase); private HashSet<string> importFilesHash = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

View File

@ -29,7 +29,7 @@ namespace AssetStudio
public string m_Source; public string m_Source;
public long m_Offset; public long m_Offset;
public long m_Size; public long m_Size;
public Lazy<byte[]> m_AudioData; public ResourceReader m_AudioData;
public AudioClip(ObjectReader reader) : base(reader) public AudioClip(ObjectReader reader) : base(reader)
{ {
@ -87,7 +87,7 @@ namespace AssetStudio
{ {
resourceReader = new ResourceReader(reader, reader.BaseStream.Position, (int)m_Size); resourceReader = new ResourceReader(reader, reader.BaseStream.Position, (int)m_Size);
} }
m_AudioData = new Lazy<byte[]>(resourceReader.GetData); m_AudioData = resourceReader;
} }
} }

View File

@ -51,7 +51,7 @@ namespace AssetStudio
public bool m_MipMap; public bool m_MipMap;
public int m_MipCount; public int m_MipCount;
public GLTextureSettings m_TextureSettings; public GLTextureSettings m_TextureSettings;
public Lazy<byte[]> image_data; public ResourceReader image_data;
public StreamingInfo m_StreamData; public StreamingInfo m_StreamData;
public Texture2D(ObjectReader reader) : base(reader) public Texture2D(ObjectReader reader) : base(reader)
@ -102,7 +102,7 @@ namespace AssetStudio
{ {
resourceReader = new ResourceReader(reader, reader.BaseStream.Position, image_data_size); resourceReader = new ResourceReader(reader, reader.BaseStream.Position, image_data_size);
} }
image_data = new Lazy<byte[]>(resourceReader.GetData); image_data = resourceReader;
} }
} }

View File

@ -8,7 +8,7 @@ namespace AssetStudio
{ {
public sealed class VideoClip : NamedObject public sealed class VideoClip : NamedObject
{ {
public Lazy<byte[]> m_VideoData; public ResourceReader m_VideoData;
public string m_OriginalPath; public string m_OriginalPath;
public string m_Source; public string m_Source;
public ulong m_Size; public ulong m_Size;
@ -47,7 +47,7 @@ namespace AssetStudio
{ {
resourceReader = new ResourceReader(reader, reader.BaseStream.Position, (int)m_Size); resourceReader = new ResourceReader(reader, reader.BaseStream.Position, (int)m_Size);
} }
m_VideoData = new Lazy<byte[]>(resourceReader.GetData); m_VideoData = resourceReader;
} }
} }
} }

View File

@ -34,17 +34,18 @@ namespace AssetStudio
{ {
var resourceFileName = Path.GetFileName(path); var resourceFileName = Path.GetFileName(path);
if (assetsFile.assetsManager.resourceFileReaders.TryGetValue(resourceFileName, out var reader)) if (assetsFile.assetsManager.resourceFileReaders.TryGetValue(resourceFileName, out reader))
{ {
reader.Position = offset; needSearch = false;
reader.BaseStream.Position = offset;
return reader.ReadBytes(size); return reader.ReadBytes(size);
} }
var currentDirectory = Path.GetDirectoryName(assetsFile.fullName); var assetsFileDirectory = Path.GetDirectoryName(assetsFile.fullName);
var resourceFilePath = currentDirectory + "\\" + resourceFileName; var resourceFilePath = assetsFileDirectory + Path.DirectorySeparatorChar + resourceFileName;
if (!File.Exists(resourceFilePath)) if (!File.Exists(resourceFilePath))
{ {
var findFiles = Directory.GetFiles(currentDirectory, resourceFileName, SearchOption.AllDirectories); var findFiles = Directory.GetFiles(assetsFileDirectory, resourceFileName, SearchOption.AllDirectories);
if (findFiles.Length > 0) if (findFiles.Length > 0)
{ {
resourceFilePath = findFiles[0]; resourceFilePath = findFiles[0];
@ -52,11 +53,11 @@ namespace AssetStudio
} }
if (File.Exists(resourceFilePath)) if (File.Exists(resourceFilePath))
{ {
using (var resourceReader = new BinaryReader(File.OpenRead(resourceFilePath))) reader = new BinaryReader(File.OpenRead(resourceFilePath));
{ needSearch = false;
resourceReader.BaseStream.Position = offset; assetsFile.assetsManager.resourceFileReaders.Add(resourceFileName, reader);
return resourceReader.ReadBytes(size); reader.BaseStream.Position = offset;
} return reader.ReadBytes(size);
} }
throw new FileNotFoundException($"Can't find the resource file {resourceFileName}"); throw new FileNotFoundException($"Can't find the resource file {resourceFileName}");

View File

@ -831,7 +831,7 @@ namespace AssetStudioGUI
} }
} }
var m_AudioData = m_AudioClip.m_AudioData.Value; var m_AudioData = m_AudioClip.m_AudioData.GetData();
if (m_AudioData == null || m_AudioData.Length == 0) if (m_AudioData == null || m_AudioData.Length == 0)
return; return;
var exinfo = new FMOD.CREATESOUNDEXINFO(); var exinfo = new FMOD.CREATESOUNDEXINFO();

View File

@ -54,7 +54,7 @@ namespace AssetStudioGUI
var exportFullName = exportPathName + item.Text + ".tex"; var exportFullName = exportPathName + item.Text + ".tex";
if (ExportFileExists(exportFullName)) if (ExportFileExists(exportFullName))
return false; return false;
File.WriteAllBytes(exportFullName, m_Texture2D.image_data.Value); File.WriteAllBytes(exportFullName, m_Texture2D.image_data.GetData());
return true; return true;
} }
} }
@ -62,7 +62,7 @@ namespace AssetStudioGUI
public static bool ExportAudioClip(AssetItem item, string exportPath) public static bool ExportAudioClip(AssetItem item, string exportPath)
{ {
var m_AudioClip = (AudioClip)item.Asset; var m_AudioClip = (AudioClip)item.Asset;
var m_AudioData = m_AudioClip.m_AudioData.Value; var m_AudioData = m_AudioClip.m_AudioData.GetData();
if (m_AudioData == null || m_AudioData.Length == 0) if (m_AudioData == null || m_AudioData.Length == 0)
return false; return false;
var converter = new AudioClipConverter(m_AudioClip); var converter = new AudioClipConverter(m_AudioClip);
@ -242,7 +242,7 @@ namespace AssetStudioGUI
public static bool ExportVideoClip(AssetItem item, string exportPath) public static bool ExportVideoClip(AssetItem item, string exportPath)
{ {
var m_VideoClip = (VideoClip)item.Asset; var m_VideoClip = (VideoClip)item.Asset;
var m_VideoData = m_VideoClip.m_VideoData.Value; var m_VideoData = m_VideoClip.m_VideoData.GetData();
if (m_VideoData != null && m_VideoData.Length != 0) if (m_VideoData != null && m_VideoData.Length != 0)
{ {
var exportFullName = exportPath + item.Text + Path.GetExtension(m_VideoClip.m_OriginalPath); var exportFullName = exportPath + item.Text + Path.GetExtension(m_VideoClip.m_OriginalPath);

View File

@ -15,7 +15,7 @@ namespace AssetStudio
public byte[] ConvertToWav() public byte[] ConvertToWav()
{ {
var m_AudioData = m_AudioClip.m_AudioData.Value; var m_AudioData = m_AudioClip.m_AudioData.GetData();
if (m_AudioData == null || m_AudioData.Length == 0) if (m_AudioData == null || m_AudioData.Length == 0)
return null; return null;
var exinfo = new FMOD.CREATESOUNDEXINFO(); var exinfo = new FMOD.CREATESOUNDEXINFO();

View File

@ -19,10 +19,8 @@ namespace AssetStudio
public Texture2DConverter(Texture2D m_Texture2D) public Texture2DConverter(Texture2D m_Texture2D)
{ {
var image_data_value = m_Texture2D.image_data.Value; image_data = m_Texture2D.image_data.GetData();
image_data_size = image_data_value.Length; image_data_size = image_data.Length;
image_data = new byte[image_data_size];
Buffer.BlockCopy(image_data_value, 0, image_data, 0, image_data_size);
m_Width = m_Texture2D.m_Width; m_Width = m_Texture2D.m_Width;
m_Height = m_Texture2D.m_Height; m_Height = m_Texture2D.m_Height;
m_TextureFormat = m_Texture2D.m_TextureFormat; m_TextureFormat = m_Texture2D.m_TextureFormat;