From b3ab0cbd74ec1d67e6f43b56d56cf0ea7b0f0472 Mon Sep 17 00:00:00 2001 From: Westin Miller Date: Wed, 1 Nov 2017 19:50:02 -0700 Subject: [PATCH] Fix loading plugins from ZIP files --- Torch/Plugins/PluginManager.cs | 35 ++++++++++++++++++++++++++++++---- Torch/Utils/MiscExtensions.cs | 31 ++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/Torch/Plugins/PluginManager.cs b/Torch/Plugins/PluginManager.cs index f200bf6..7c32ad3 100644 --- a/Torch/Plugins/PluginManager.cs +++ b/Torch/Plugins/PluginManager.cs @@ -236,7 +236,8 @@ namespace Torch.Managers if (!file.Contains(".dll", StringComparison.CurrentCultureIgnoreCase)) continue; - if (false) { + if (false) + { var asm = Assembly.LoadFrom(file); assemblies.Add(asm); TorchBase.RegisterAuxAssembly(asm); @@ -251,8 +252,15 @@ namespace Torch.Managers var symbolPath = Path.Combine(Path.GetDirectoryName(file) ?? ".", Path.GetFileNameWithoutExtension(file) + ".pdb"); if (File.Exists(symbolPath)) - using (var symbolStream = File.OpenRead(symbolPath)) - symbol = symbolStream.ReadToEnd(); + try + { + using (var symbolStream = File.OpenRead(symbolPath)) + symbol = symbolStream.ReadToEnd(); + } + catch (Exception e) + { + _log.Warn(e, $"Failed to read debugging symbols from {symbolPath}"); + } Assembly asm = symbol != null ? Assembly.Load(data, symbol) : Assembly.Load(data); #else Assembly asm = Assembly.Load(data); @@ -283,10 +291,29 @@ namespace Torch.Managers if (!entry.Name.Contains(".dll", StringComparison.CurrentCultureIgnoreCase)) continue; + using (var stream = entry.Open()) { - var data = stream.ReadToEnd(); + var data = stream.ReadToEnd((int)entry.Length); +#if DEBUG + byte[] symbol = null; + var symbolEntryName = entry.FullName.Substring(0, entry.FullName.Length - "dll".Length) + "pdb"; + var symbolEntry = zipFile.GetEntry(symbolEntryName); + if (symbolEntry != null) + try + { + using (var symbolStream = symbolEntry.Open()) + symbol = symbolStream.ReadToEnd((int)symbolEntry.Length); + } + catch (Exception e) + { + _log.Warn(e, $"Failed to read debugging symbols from {path}:{symbolEntryName}"); + } + Assembly asm = symbol != null ? Assembly.Load(data, symbol) : Assembly.Load(data); +#else Assembly asm = Assembly.Load(data); +#endif + assemblies.Add(asm); TorchBase.RegisterAuxAssembly(asm); } } diff --git a/Torch/Utils/MiscExtensions.cs b/Torch/Utils/MiscExtensions.cs index 4198b03..c3a0362 100644 --- a/Torch/Utils/MiscExtensions.cs +++ b/Torch/Utils/MiscExtensions.cs @@ -12,24 +12,39 @@ namespace Torch.Utils { private static readonly ThreadLocal> _streamBuffer = new ThreadLocal>(() => new WeakReference(null)); - public static byte[] ReadToEnd(this Stream stream) + private static long LengthSafe(this Stream stream) + { + try + { + return stream.Length; + } + catch + { + return 512; + } + } + + public static byte[] ReadToEnd(this Stream stream, int optionalDataLength = -1) { byte[] buffer; if (!_streamBuffer.Value.TryGetTarget(out buffer)) - buffer = new byte[stream.Length]; - if (buffer.Length < stream.Length) - buffer = new byte[stream.Length]; + buffer = new byte[stream.LengthSafe()]; + var initialBufferSize = optionalDataLength > 0 ? optionalDataLength : stream.LengthSafe(); + if (buffer.Length < initialBufferSize) + buffer = new byte[initialBufferSize]; if (buffer.Length < 1024) buffer = new byte[1024]; + var streamPosition = 0; while (true) { - if (buffer.Length == stream.Position) - Array.Resize(ref buffer, Math.Max((int)stream.Length, buffer.Length * 2)); - int count = stream.Read(buffer, (int)stream.Position, buffer.Length - (int)stream.Position); + if (buffer.Length == streamPosition) + Array.Resize(ref buffer, Math.Max((int)stream.LengthSafe(), buffer.Length * 2)); + int count = stream.Read(buffer, streamPosition, buffer.Length - streamPosition); if (count == 0) break; + streamPosition += count; } - var result = new byte[(int)stream.Position]; + var result = new byte[streamPosition]; Array.Copy(buffer, 0, result, 0, result.Length); _streamBuffer.Value.SetTarget(buffer); return result;