summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModLoading/Symbols/SymbolReader.cs
blob: 2171895d9137fa441038aadab5dd1a8cb2924bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.IO;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Pdb;

namespace StardewModdingAPI.Framework.ModLoading.Symbols
{
    /// <summary>Reads symbol data for an assembly.</summary>
    internal class SymbolReader : ISymbolReader
    {
        /*********
        ** Fields
        *********/
        /// <summary>The module for which to read symbols.</summary>
        private readonly ModuleDefinition Module;

        /// <summary>The symbol file stream.</summary>
        private readonly Stream Stream;

        /// <summary>The underlying symbol reader.</summary>
        private ISymbolReader Reader;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="module">The module for which to read symbols.</param>
        /// <param name="stream">The symbol file stream.</param>
        public SymbolReader(ModuleDefinition module, Stream stream)
        {
            this.Module = module;
            this.Stream = stream;
            this.Reader = new NativePdbReaderProvider().GetSymbolReader(module, stream);
        }

        /// <summary>Get the symbol writer provider for the assembly.</summary>
        public ISymbolWriterProvider GetWriterProvider()
        {
            return new PortablePdbWriterProvider();
        }

        /// <summary>Process a debug header in the symbol file.</summary>
        /// <param name="header">The debug header.</param>
        public bool ProcessDebugHeader(ImageDebugHeader header)
        {
            try
            {
                return this.Reader.ProcessDebugHeader(header);
            }
            catch
            {
                this.Reader.Dispose();
                this.Reader = new PortablePdbReaderProvider().GetSymbolReader(this.Module, this.Stream);
                return this.Reader.ProcessDebugHeader(header);
            }
        }

        /// <summary>Read the method debug information for a method in the assembly.</summary>
        /// <param name="method">The method definition.</param>
        public MethodDebugInformation Read(MethodDefinition method)
        {
            return this.Reader.Read(method);
        }

        /// <inheritdoc />
        public void Dispose()
        {
            this.Reader.Dispose();
        }
    }
}