From 0b29eb3bc37553aa2e9776e17727ba07018606ea Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 25 Aug 2021 20:01:59 -0400 Subject: minor refactoring in new symbol code --- src/SMAPI/Framework/ModLoading/AssemblyLoader.cs | 26 ++++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/SMAPI/Framework/ModLoading/AssemblyLoader.cs') diff --git a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs index 72cfd119..5180f072 100644 --- a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs +++ b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs @@ -35,8 +35,11 @@ namespace StardewModdingAPI.Framework.ModLoading /// A minimal assembly definition resolver which resolves references to known loaded assemblies. private readonly AssemblyDefinitionResolver AssemblyDefinitionResolver; - private readonly SymbolReaderProvider SymbolReaderProvider; - private readonly SymbolWriterProvider SymbolWriterProvider; + /// Provides assembly symbol readers for Mono.Cecil. + private readonly SymbolReaderProvider SymbolReaderProvider = new SymbolReaderProvider(); + + /// Provides assembly symbol writers for Mono.Cecil. + private readonly SymbolWriterProvider SymbolWriterProvider = new SymbolWriterProvider(); /// The objects to dispose as part of this instance. private readonly HashSet Disposables = new HashSet(); @@ -65,9 +68,6 @@ namespace StardewModdingAPI.Framework.ModLoading this.AssemblyDefinitionResolver = this.TrackForDisposal(new AssemblyDefinitionResolver()); Constants.ConfigureAssemblyResolver(this.AssemblyDefinitionResolver); - this.SymbolReaderProvider = new SymbolReaderProvider(); - this.SymbolWriterProvider = new SymbolWriterProvider(); - // generate type => assembly lookup for types which should be rewritten this.TypeAssemblies = new Dictionary(); foreach (Assembly assembly in this.AssemblyMap.Targets) @@ -121,7 +121,7 @@ namespace StardewModdingAPI.Framework.ModLoading // rewrite assembly bool changed = this.RewriteAssembly(mod, assembly.Definition, loggedMessages, logPrefix: " "); - + // detect broken assembly reference foreach (AssemblyNameReference reference in assembly.Definition.MainModule.AssemblyReferences) { @@ -142,10 +142,10 @@ namespace StardewModdingAPI.Framework.ModLoading this.Monitor.Log($" Loading {assembly.File.Name} (rewritten)...", LogLevel.Trace); // load assembly - using MemoryStream outStream = new MemoryStream(); + using MemoryStream outAssemblyStream = new MemoryStream(); using MemoryStream outSymbolStream = new MemoryStream(); - assembly.Definition.Write(outStream, new WriterParameters() { WriteSymbols = true, SymbolStream = outSymbolStream, SymbolWriterProvider = this.SymbolWriterProvider } ); - byte[] bytes = outStream.ToArray(); + assembly.Definition.Write(outAssemblyStream, new WriterParameters { WriteSymbols = true, SymbolStream = outSymbolStream, SymbolWriterProvider = this.SymbolWriterProvider }); + byte[] bytes = outAssemblyStream.ToArray(); lastAssembly = Assembly.Load(bytes, outSymbolStream.ToArray()); } else @@ -233,13 +233,13 @@ namespace StardewModdingAPI.Framework.ModLoading if (!file.Exists) yield break; // not a local assembly - // read assembly and PDB (if present) + // read assembly and symbols byte[] assemblyBytes = File.ReadAllBytes(file.FullName); Stream readStream = this.TrackForDisposal(new MemoryStream(assemblyBytes)); { - string symbolsPath = Path.Combine(Path.GetDirectoryName(file.FullName), Path.GetFileNameWithoutExtension(file.FullName)) + ".pdb"; - if ( File.Exists( symbolsPath ) ) - this.SymbolReaderProvider.AddSymbolMapping( Path.GetFileName( file.FullName ), this.TrackForDisposal( new MemoryStream( File.ReadAllBytes( symbolsPath ) ) ) ); + FileInfo symbolsFile = new FileInfo(Path.Combine(Path.GetDirectoryName(file.FullName)!, Path.GetFileNameWithoutExtension(file.FullName)) + ".pdb"); + if (symbolsFile.Exists) + this.SymbolReaderProvider.TryAddSymbolData(file.Name, this.TrackForDisposal(symbolsFile.OpenRead())); } AssemblyDefinition assembly = this.TrackForDisposal(AssemblyDefinition.ReadAssembly(readStream, new ReaderParameters(ReadingMode.Immediate) { AssemblyResolver = assemblyResolver, InMemory = true, ReadSymbols = true, SymbolReaderProvider = this.SymbolReaderProvider })); -- cgit