|
8 | 8 |
|
9 | 9 | namespace nanoFramework.nanoCLR.Host
|
10 | 10 | {
|
11 |
| - /// <summary> |
12 |
| - /// Information about a native assembly. |
13 |
| - /// </summary> |
14 |
| - public sealed class NativeAssemblyDetails |
15 |
| - { |
16 |
| - #region Properties |
17 |
| - /// <summary> |
18 |
| - /// Gets the name of the assembly. |
19 |
| - /// </summary> |
20 |
| - public string Name |
21 |
| - { |
22 |
| - get; |
23 |
| - } |
24 |
| - |
25 |
| - /// <summary> |
26 |
| - /// Gets the version of the assembly. |
27 |
| - /// </summary> |
28 |
| - public Version Version |
29 |
| - { |
30 |
| - get; |
31 |
| - } |
32 |
| - |
33 |
| - /// <summary> |
34 |
| - /// Gets the checksum of the assembly. |
35 |
| - /// </summary> |
36 |
| - public uint CheckSum |
37 |
| - { |
38 |
| - get; |
39 |
| - } |
40 |
| - #endregion |
41 |
| - |
42 |
| - #region Construction |
43 |
| - /// <summary> |
44 |
| - /// Get the assembly information from the nanoCLR instance. |
45 |
| - /// </summary> |
46 |
| - /// <returns>Returns a list with the metadata of native assemblies. If the runtime does |
47 |
| - /// not support this, <c>null</c> is returned.</returns> |
48 |
| - public static List<NativeAssemblyDetails>? Get () |
49 |
| - { |
50 |
| - var result = new List<NativeAssemblyDetails> (); |
51 |
| - |
52 |
| - ushort numAssemblies; |
53 |
| - try |
54 |
| - { |
55 |
| - numAssemblies = Interop.nanoCLR.nanoCLR_GetNativeAssemblyCount (); |
56 |
| - } |
57 |
| - catch (EntryPointNotFoundException) |
58 |
| - { |
59 |
| - return null; |
60 |
| - } |
61 |
| - |
62 |
| - if (numAssemblies == 0) |
63 |
| - { |
64 |
| - return result; |
65 |
| - } |
66 |
| - |
67 |
| - // assembly data size is coming from debugger library: NativeAssemblyDetails.Size |
68 |
| - const int assemblyDataSize = 4 + 4 * 2 + 128 * 1; |
69 |
| - |
70 |
| - byte[] data = new byte[numAssemblies * assemblyDataSize]; |
71 |
| - |
72 |
| - if (!Interop.nanoCLR.nanoCLR_GetNativeAssemblyInformation (data, data.Length)) |
73 |
| - { |
74 |
| - return null; |
75 |
| - } |
76 |
| - |
77 |
| - using var buffer = new MemoryStream (data); |
78 |
| - using var reader = new BinaryReader (buffer); |
79 |
| - for (int i = 0; i < numAssemblies; i++) |
80 |
| - { |
81 |
| - result.Add (new NativeAssemblyDetails (reader)); |
82 |
| - } |
83 |
| - |
84 |
| - return result; |
85 |
| - } |
86 |
| - |
87 |
| - /// <summary> |
88 |
| - /// Deserialize the description of the native assembly. |
89 |
| - /// In nf-debugger this is done via a converter. The code in this |
90 |
| - /// constructor is essentially the same. |
91 |
| - /// </summary> |
92 |
| - /// <param name="reader"></param> |
93 |
| - private NativeAssemblyDetails (BinaryReader reader) |
94 |
| - { |
95 |
| - CheckSum = reader.ReadUInt32 (); |
96 |
| - Version = new Version ( |
97 |
| - reader.ReadUInt16 (), |
98 |
| - reader.ReadUInt16 (), |
99 |
| - reader.ReadUInt16 (), |
100 |
| - reader.ReadUInt16 () |
101 |
| - ); |
102 |
| - Name = GetZeroTerminatedString (reader.ReadBytes (128), true); |
103 |
| - } |
104 |
| - #endregion |
105 |
| - |
106 |
| - #region Helpers |
107 |
| - /// <summary> |
108 |
| - /// Original is in nf-debugger\nanoFramework.Tools.DebugLibrary.Shared\WireProtocol\Commands.cs |
109 |
| - /// </summary> |
110 |
| - /// <param name="buf"></param> |
111 |
| - /// <param name="fUTF8"></param> |
112 |
| - /// <returns></returns> |
113 |
| - private static string GetZeroTerminatedString (byte[] buf, bool fUTF8) |
114 |
| - { |
115 |
| - if (buf is null) |
116 |
| - { |
117 |
| - return null; |
118 |
| - } |
119 |
| - |
120 |
| - int len = 0; |
121 |
| - int num = buf.Length; |
122 |
| - |
123 |
| - while (len < num && buf[len] != 0) |
124 |
| - { |
125 |
| - len++; |
126 |
| - } |
127 |
| - |
128 |
| - return fUTF8 ? Encoding.UTF8.GetString (buf, 0, len) : Encoding.ASCII.GetString (buf, 0, len); |
129 |
| - } |
130 |
| - #endregion |
131 |
| - } |
| 11 | + /// <summary> |
| 12 | + /// Information about a native assembly. |
| 13 | + /// </summary> |
| 14 | + public sealed class NativeAssemblyDetails |
| 15 | + { |
| 16 | + #region Properties |
| 17 | + /// <summary> |
| 18 | + /// Gets the name of the assembly. |
| 19 | + /// </summary> |
| 20 | + public string Name |
| 21 | + { |
| 22 | + get; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Gets the version of the assembly. |
| 27 | + /// </summary> |
| 28 | + public Version Version |
| 29 | + { |
| 30 | + get; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Gets the checksum of the assembly. |
| 35 | + /// </summary> |
| 36 | + public uint CheckSum |
| 37 | + { |
| 38 | + get; |
| 39 | + } |
| 40 | + #endregion |
| 41 | + |
| 42 | + #region Construction |
| 43 | + /// <summary> |
| 44 | + /// Get the assembly information from the nanoCLR instance. |
| 45 | + /// </summary> |
| 46 | + /// <returns>Returns a list with the metadata of native assemblies. If the runtime does |
| 47 | + /// not support this, <c>null</c> is returned.</returns> |
| 48 | + public static List<NativeAssemblyDetails> Get() |
| 49 | + { |
| 50 | + var result = new List<NativeAssemblyDetails>(); |
| 51 | + |
| 52 | + ushort numAssemblies; |
| 53 | + try |
| 54 | + { |
| 55 | + numAssemblies = Interop.nanoCLR.nanoCLR_GetNativeAssemblyCount(); |
| 56 | + } |
| 57 | + catch (EntryPointNotFoundException) |
| 58 | + { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + if (numAssemblies == 0) |
| 63 | + { |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + // assembly data size is coming from debugger library: NativeAssemblyDetails.Size |
| 68 | + const int assemblyDataSize = 4 + 4 * 2 + 128 * 1; |
| 69 | + |
| 70 | + byte[] data = new byte[numAssemblies * assemblyDataSize]; |
| 71 | + |
| 72 | + if (!Interop.nanoCLR.nanoCLR_GetNativeAssemblyInformation(data, data.Length)) |
| 73 | + { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + using var buffer = new MemoryStream(data); |
| 78 | + using var reader = new BinaryReader(buffer); |
| 79 | + for (int i = 0; i < numAssemblies; i++) |
| 80 | + { |
| 81 | + result.Add(new NativeAssemblyDetails(reader)); |
| 82 | + } |
| 83 | + |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Deserialize the description of the native assembly. |
| 89 | + /// In nf-debugger this is done via a converter. The code in this |
| 90 | + /// constructor is essentially the same. |
| 91 | + /// </summary> |
| 92 | + /// <param name="reader"></param> |
| 93 | + private NativeAssemblyDetails(BinaryReader reader) |
| 94 | + { |
| 95 | + CheckSum = reader.ReadUInt32(); |
| 96 | + Version = new Version( |
| 97 | + reader.ReadUInt16(), |
| 98 | + reader.ReadUInt16(), |
| 99 | + reader.ReadUInt16(), |
| 100 | + reader.ReadUInt16() |
| 101 | + ); |
| 102 | + Name = GetZeroTerminatedString(reader.ReadBytes(128), true); |
| 103 | + } |
| 104 | + #endregion |
| 105 | + |
| 106 | + #region Helpers |
| 107 | + /// <summary> |
| 108 | + /// Original is in nf-debugger\nanoFramework.Tools.DebugLibrary.Shared\WireProtocol\Commands.cs |
| 109 | + /// </summary> |
| 110 | + /// <param name="buf"></param> |
| 111 | + /// <param name="fUTF8"></param> |
| 112 | + /// <returns></returns> |
| 113 | + private static string GetZeroTerminatedString(byte[] buf, bool fUTF8) |
| 114 | + { |
| 115 | + if (buf is null) |
| 116 | + { |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + int len = 0; |
| 121 | + int num = buf.Length; |
| 122 | + |
| 123 | + while (len < num && buf[len] != 0) |
| 124 | + { |
| 125 | + len++; |
| 126 | + } |
| 127 | + |
| 128 | + return fUTF8 ? Encoding.UTF8.GetString(buf, 0, len) : Encoding.ASCII.GetString(buf, 0, len); |
| 129 | + } |
| 130 | + #endregion |
| 131 | + } |
132 | 132 | }
|
0 commit comments