Improve external file reading

This commit is contained in:
Perfare
2018-02-27 18:20:12 +08:00
parent ee6c050330
commit 351228e45c
4 changed files with 55 additions and 41 deletions

View File

@ -93,8 +93,6 @@ namespace Unity_Studio
m_3D = m_Legacy3D;
m_Source = a_Stream.ReadAlignedString(a_Stream.ReadInt32());
if (m_Source != "")
m_Source = Path.Combine(Path.GetDirectoryName(sourceFile.filePath), m_Source.Replace("archive:/", ""));
m_Offset = a_Stream.ReadInt64();
m_Size = a_Stream.ReadInt64();
m_CompressionFormat = (AudioCompressionFormat)a_Stream.ReadInt32();
@ -102,31 +100,41 @@ namespace Unity_Studio
if (readSwitch)
{
if (string.IsNullOrEmpty(m_Source))
if (!string.IsNullOrEmpty(m_Source))
{
if (m_Size > 0)
m_AudioData = a_Stream.ReadBytes((int)m_Size);
}
else if (File.Exists(m_Source) ||
File.Exists(m_Source = Path.Combine(Path.GetDirectoryName(sourceFile.filePath), Path.GetFileName(m_Source))))
{
BinaryReader reader = new BinaryReader(File.OpenRead(m_Source));
reader.BaseStream.Position = m_Offset;
m_AudioData = reader.ReadBytes((int)m_Size);
reader.Close();
}
else
{
if (UnityStudio.assetsfileandstream.TryGetValue(Path.GetFileName(m_Source), out var estream))
var resourceFileName = Path.GetFileName(m_Source);
var resourceFilePath = Path.GetDirectoryName(sourceFile.filePath) + "\\" + resourceFileName;
if (!File.Exists(resourceFilePath))
{
estream.Position = m_Offset;
m_AudioData = estream.ReadBytes((int)m_Size);
var findFiles = Directory.GetFiles(Path.GetDirectoryName(sourceFile.filePath), resourceFileName, SearchOption.AllDirectories);
if (findFiles.Length > 0) { resourceFilePath = findFiles[0]; }
}
if (File.Exists(resourceFilePath))
{
using (var reader = new BinaryReader(File.OpenRead(resourceFilePath)))
{
reader.BaseStream.Position = m_Offset;
m_AudioData = reader.ReadBytes((int)m_Size);
}
}
else
{
MessageBox.Show($"can't find the resource file {Path.GetFileName(m_Source)}");
if (UnityStudio.assetsfileandstream.TryGetValue(resourceFileName, out var reader))
{
reader.Position = m_Offset;
m_AudioData = reader.ReadBytes((int)m_Size);
}
else
{
MessageBox.Show($"can't find the resource file {resourceFileName}");
}
}
}
else
{
if (m_Size > 0)
m_AudioData = a_Stream.ReadBytes((int)m_Size);
}
}
else
{

View File

@ -166,25 +166,31 @@ namespace Unity_Studio
{
if (!string.IsNullOrEmpty(path))
{
path = Path.Combine(Path.GetDirectoryName(sourceFile.filePath), path.Replace("archive:/", ""));
if (File.Exists(path) ||
File.Exists(path = Path.Combine(Path.GetDirectoryName(sourceFile.filePath), Path.GetFileName(path))))
var resourceFileName = Path.GetFileName(path);
var resourceFilePath = Path.GetDirectoryName(sourceFile.filePath) + "\\" + resourceFileName;
if (!File.Exists(resourceFilePath))
{
BinaryReader reader = new BinaryReader(File.OpenRead(path));
reader.BaseStream.Position = offset;
image_data = reader.ReadBytes(image_data_size);
reader.Close();
var findFiles = Directory.GetFiles(Path.GetDirectoryName(sourceFile.filePath), resourceFileName, SearchOption.AllDirectories);
if (findFiles.Length > 0) { resourceFilePath = findFiles[0]; }
}
if (File.Exists(resourceFilePath))
{
using (var reader = new BinaryReader(File.OpenRead(resourceFilePath)))
{
reader.BaseStream.Position = offset;
image_data = reader.ReadBytes(image_data_size);
}
}
else
{
if (UnityStudio.assetsfileandstream.TryGetValue(Path.GetFileName(path), out var estream))
if (UnityStudio.assetsfileandstream.TryGetValue(resourceFileName, out var reader))
{
estream.Position = offset;
image_data = estream.ReadBytes(image_data_size);
reader.Position = offset;
image_data = reader.ReadBytes(image_data_size);
}
else
{
MessageBox.Show($"can't find the resource file {Path.GetFileName(path)}");
MessageBox.Show($"can't find the resource file {resourceFileName}");
}
}
}