Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Commit 182a42a

Browse files
committed
optimize ResourceReader
1 parent 06fbe69 commit 182a42a

9 files changed

Lines changed: 25 additions & 26 deletions

File tree

AssetStudio/AssetsManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class AssetsManager
1010
{
1111
public List<SerializedFile> assetsFileList = new List<SerializedFile>();
1212
internal Dictionary<string, int> assetsFileIndexCache = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
13-
internal Dictionary<string, EndianBinaryReader> resourceFileReaders = new Dictionary<string, EndianBinaryReader>(StringComparer.OrdinalIgnoreCase);
13+
internal Dictionary<string, BinaryReader> resourceFileReaders = new Dictionary<string, BinaryReader>(StringComparer.OrdinalIgnoreCase);
1414

1515
private List<string> importFiles = new List<string>();
1616
private HashSet<string> importFilesHash = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

AssetStudio/Classes/AudioClip.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public sealed class AudioClip : NamedObject
2929
public string m_Source;
3030
public long m_Offset;
3131
public long m_Size;
32-
public Lazy<byte[]> m_AudioData;
32+
public ResourceReader m_AudioData;
3333

3434
public AudioClip(ObjectReader reader) : base(reader)
3535
{
@@ -87,7 +87,7 @@ public AudioClip(ObjectReader reader) : base(reader)
8787
{
8888
resourceReader = new ResourceReader(reader, reader.BaseStream.Position, (int)m_Size);
8989
}
90-
m_AudioData = new Lazy<byte[]>(resourceReader.GetData);
90+
m_AudioData = resourceReader;
9191
}
9292
}
9393

AssetStudio/Classes/Texture2D.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public sealed class Texture2D : Texture
5151
public bool m_MipMap;
5252
public int m_MipCount;
5353
public GLTextureSettings m_TextureSettings;
54-
public Lazy<byte[]> image_data;
54+
public ResourceReader image_data;
5555
public StreamingInfo m_StreamData;
5656

5757
public Texture2D(ObjectReader reader) : base(reader)
@@ -102,7 +102,7 @@ public Texture2D(ObjectReader reader) : base(reader)
102102
{
103103
resourceReader = new ResourceReader(reader, reader.BaseStream.Position, image_data_size);
104104
}
105-
image_data = new Lazy<byte[]>(resourceReader.GetData);
105+
image_data = resourceReader;
106106
}
107107
}
108108

AssetStudio/Classes/VideoClip.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace AssetStudio
88
{
99
public sealed class VideoClip : NamedObject
1010
{
11-
public Lazy<byte[]> m_VideoData;
11+
public ResourceReader m_VideoData;
1212
public string m_OriginalPath;
1313
public string m_Source;
1414
public ulong m_Size;
@@ -47,7 +47,7 @@ public VideoClip(ObjectReader reader) : base(reader)
4747
{
4848
resourceReader = new ResourceReader(reader, reader.BaseStream.Position, (int)m_Size);
4949
}
50-
m_VideoData = new Lazy<byte[]>(resourceReader.GetData);
50+
m_VideoData = resourceReader;
5151
}
5252
}
5353
}

AssetStudio/ResourceReader.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,30 @@ public byte[] GetData()
3434
{
3535
var resourceFileName = Path.GetFileName(path);
3636

37-
if (assetsFile.assetsManager.resourceFileReaders.TryGetValue(resourceFileName, out var reader))
37+
if (assetsFile.assetsManager.resourceFileReaders.TryGetValue(resourceFileName, out reader))
3838
{
39-
reader.Position = offset;
39+
needSearch = false;
40+
reader.BaseStream.Position = offset;
4041
return reader.ReadBytes(size);
4142
}
4243

43-
var currentDirectory = Path.GetDirectoryName(assetsFile.fullName);
44-
var resourceFilePath = currentDirectory + "\\" + resourceFileName;
44+
var assetsFileDirectory = Path.GetDirectoryName(assetsFile.fullName);
45+
var resourceFilePath = assetsFileDirectory + Path.DirectorySeparatorChar + resourceFileName;
4546
if (!File.Exists(resourceFilePath))
4647
{
47-
var findFiles = Directory.GetFiles(currentDirectory, resourceFileName, SearchOption.AllDirectories);
48+
var findFiles = Directory.GetFiles(assetsFileDirectory, resourceFileName, SearchOption.AllDirectories);
4849
if (findFiles.Length > 0)
4950
{
5051
resourceFilePath = findFiles[0];
5152
}
5253
}
5354
if (File.Exists(resourceFilePath))
5455
{
55-
using (var resourceReader = new BinaryReader(File.OpenRead(resourceFilePath)))
56-
{
57-
resourceReader.BaseStream.Position = offset;
58-
return resourceReader.ReadBytes(size);
59-
}
56+
reader = new BinaryReader(File.OpenRead(resourceFilePath));
57+
needSearch = false;
58+
assetsFile.assetsManager.resourceFileReaders.Add(resourceFileName, reader);
59+
reader.BaseStream.Position = offset;
60+
return reader.ReadBytes(size);
6061
}
6162

6263
throw new FileNotFoundException($"Can't find the resource file {resourceFileName}");

AssetStudioGUI/AssetStudioGUIForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ private void PreviewAudioClip(AssetItem assetItem, AudioClip m_AudioClip)
831831
}
832832
}
833833

834-
var m_AudioData = m_AudioClip.m_AudioData.Value;
834+
var m_AudioData = m_AudioClip.m_AudioData.GetData();
835835
if (m_AudioData == null || m_AudioData.Length == 0)
836836
return;
837837
var exinfo = new FMOD.CREATESOUNDEXINFO();

AssetStudioGUI/Exporter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ public static bool ExportTexture2D(AssetItem item, string exportPathName)
5454
var exportFullName = exportPathName + item.Text + ".tex";
5555
if (ExportFileExists(exportFullName))
5656
return false;
57-
File.WriteAllBytes(exportFullName, m_Texture2D.image_data.Value);
57+
File.WriteAllBytes(exportFullName, m_Texture2D.image_data.GetData());
5858
return true;
5959
}
6060
}
6161

6262
public static bool ExportAudioClip(AssetItem item, string exportPath)
6363
{
6464
var m_AudioClip = (AudioClip)item.Asset;
65-
var m_AudioData = m_AudioClip.m_AudioData.Value;
65+
var m_AudioData = m_AudioClip.m_AudioData.GetData();
6666
if (m_AudioData == null || m_AudioData.Length == 0)
6767
return false;
6868
var converter = new AudioClipConverter(m_AudioClip);
@@ -242,7 +242,7 @@ public static bool ExportMesh(AssetItem item, string exportPath)
242242
public static bool ExportVideoClip(AssetItem item, string exportPath)
243243
{
244244
var m_VideoClip = (VideoClip)item.Asset;
245-
var m_VideoData = m_VideoClip.m_VideoData.Value;
245+
var m_VideoData = m_VideoClip.m_VideoData.GetData();
246246
if (m_VideoData != null && m_VideoData.Length != 0)
247247
{
248248
var exportFullName = exportPath + item.Text + Path.GetExtension(m_VideoClip.m_OriginalPath);

AssetStudioUtility/AudioClipConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public AudioClipConverter(AudioClip audioClip)
1515

1616
public byte[] ConvertToWav()
1717
{
18-
var m_AudioData = m_AudioClip.m_AudioData.Value;
18+
var m_AudioData = m_AudioClip.m_AudioData.GetData();
1919
if (m_AudioData == null || m_AudioData.Length == 0)
2020
return null;
2121
var exinfo = new FMOD.CREATESOUNDEXINFO();

AssetStudioUtility/Texture2DConverter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ public class Texture2DConverter
1919

2020
public Texture2DConverter(Texture2D m_Texture2D)
2121
{
22-
var image_data_value = m_Texture2D.image_data.Value;
23-
image_data_size = image_data_value.Length;
24-
image_data = new byte[image_data_size];
25-
Buffer.BlockCopy(image_data_value, 0, image_data, 0, image_data_size);
22+
image_data = m_Texture2D.image_data.GetData();
23+
image_data_size = image_data.Length;
2624
m_Width = m_Texture2D.m_Width;
2725
m_Height = m_Texture2D.m_Height;
2826
m_TextureFormat = m_Texture2D.m_TextureFormat;

0 commit comments

Comments
 (0)