1 #Region "Microsoft.VisualBasic::a57c102b5e9701293f33360cded13977, Microsoft.VisualBasic.Core\ApplicationServices\Tools\Resources.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class ResourcesSatellite
35     
36     '         Properties: FileName, MyResource, Resources
37     
38     '         Constructor: (+5 OverloadsSub New
39     
40     '         Function: DirectLoadFrom, (+2 OverloadsGetObject, (+2 OverloadsGetStream, (+3 OverloadsGetString, LoadMy
41     
42     '         Sub: __load, __resParser
43     
44     
45     ' /********************************************************************************/
46
47 #End Region
48
49 Imports System.ComponentModel.Composition
50 Imports System.Globalization
51 Imports System.IO
52 Imports System.Reflection
53 Imports System.Resources
54 Imports System.Runtime.CompilerServices
55 Imports System.Runtime.InteropServices
56 Imports Microsoft.VisualBasic.Language
57 Imports Microsoft.VisualBasic.Text
58
59 Namespace ApplicationServices
60
61     ''' <summary>
62     ''' Represents a resource manager that provides convenient access to culture-specific
63     ''' resources at run time.Security Note: Calling methods in this class with untrusted
64     ''' data is a security risk. Call the methods in the class only with trusted data.
65     ''' For more information, see Untrusted Data Security Risks.
66     ''' (资源卫星程序集)
67     ''' </summary>
68     <Export(GetType(ResourceManager))> Public Class ResourcesSatellite
69
70         ''' <summary>
71         ''' The file path of the resources satellite assembly.
72         ''' </summary>
73         ''' <returns></returns>
74         Public ReadOnly Property FileName As String
75         ''' <summary>
76         ''' <see cref="System.Resources.ResourceManager"/> object in the satellite assembly.
77         ''' </summary>
78         ''' <returns></returns>
79         Public ReadOnly Property Resources As ResourceManager
80
81         ' Exceptions:
82         '   T:System.ArgumentNullException:
83         '     The name parameter is null.
84         '         '   T:System.Resources.MissingManifestResourceException:
85         '     No usable set of localized resources has been found, and there are no default
86         '     culture resources. For information about how to handle this exception, see the
87         '     "Handling MissingManifestResourceException and MissingSatelliteAssemblyException
88         '     Exceptions" section in the System.Resources.ResourceManager class topic.
89         '         '   T:System.Resources.MissingSatelliteAssemblyException:
90         '     The default culture's resources reside in a satellite assembly that could not
91         '     be found. For information about how to handle this exception, see the "Handling
92         '     MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions"
93         '     section in the System.Resources.ResourceManager class topic.
94
95         ''' <summary>
96         ''' Returns the value of the specified non-string resource.
97         ''' </summary>
98         ''' <param name="name">The name of the resource to get.</param>
99         ''' <returns>The value of the resource localized for the caller's current culture settings.
100         ''' If an appropriate resource set exists but name cannot be found, the method returns
101         ''' null.</returns>
102         ''' 
103         <MethodImpl(MethodImplOptions.AggressiveInlining)>
104         Public Overridable Function GetObject(name As StringAs Object
105             Return Resources.GetObject(name)
106         End Function
107
108         ' Exceptions:
109         '   T:System.ArgumentNullException:
110         '     The name parameter is null.
111         '         '   T:System.Resources.MissingManifestResourceException:
112         '     No usable set of resources have been found, and there are no default culture
113         '     resources. For information about how to handle this exception, see the "Handling
114         '     MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions"
115         '     section in the System.Resources.ResourceManager class topic.
116         '         '   T:System.Resources.MissingSatelliteAssemblyException:
117         '     The default culture's resources reside in a satellite assembly that could not
118         '     be found. For information about how to handle this exception, see the "Handling
119         '     MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions"
120         '     section in the System.Resources.ResourceManager class topic.
121
122         ''' <summary>
123         ''' Gets the value of the specified non-string resource localized for the specified
124         ''' culture.
125         ''' </summary>
126         ''' <param name="name">The name of the resource to get.</param>
127         ''' <param name="culture">The culture for which the resource is localized. If the resource is not localized
128         ''' for this culture, the resource manager uses fallback rules to locate an appropriate
129         ''' resource.If this value is null, the System.Globalization.CultureInfo object is
130         ''' obtained by using the System.Globalization.CultureInfo.CurrentUICulture property.</param>
131         ''' <returns>The value of the resource, localized for the specified culture. If an appropriate
132         ''' resource set exists but name cannot be found, the method returns null.</returns>
133         ''' 
134         <MethodImpl(MethodImplOptions.AggressiveInlining)>
135         Public Overridable Function GetObject(name As String, culture As CultureInfo) As Object
136             Return Resources.GetObject(name, culture)
137         End Function
138
139         ' Exceptions:
140         '   T:System.InvalidOperationException:
141         '     The value of the specified resource is not a System.IO.MemoryStream object.
142         '
143         '   T:System.ArgumentNullException:
144         '     name is null.
145         '         '   T:System.Resources.MissingManifestResourceException:
146         '     No usable set of resources is found, and there are no default resources. For
147         '     information about how to handle this exception, see the "Handling MissingManifestResourceException
148         '     and MissingSatelliteAssemblyException Exceptions" section in the System.Resources.ResourceManager
149         '     class topic.
150         '         '   T:System.Resources.MissingSatelliteAssemblyException:
151         '     The default culture's resources reside in a satellite assembly that could not
152         '     be found. For information about how to handle this exception, see the "Handling
153         '     MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions"
154         '     section in the System.Resources.ResourceManager class topic.
155
156         ''' <summary>
157         ''' Returns an unmanaged memory stream object from the specified resource.
158         ''' </summary>
159         ''' <param name="name">The name of a resource.</param>
160         ''' <returns>An unmanaged memory stream object that represents a resource .</returns>
161         ''' 
162         <MethodImpl(MethodImplOptions.AggressiveInlining)>
163         <ComVisible(False)>
164         Public Function GetStream(name As StringAs UnmanagedMemoryStream
165             Return Resources.GetStream(name)
166         End Function
167
168         ' Exceptions:
169         '   T:System.InvalidOperationException:
170         '     The value of the specified resource is not a System.IO.MemoryStream object.
171         '
172         '   T:System.ArgumentNullException:
173         '     name is null.
174         '         '   T:System.Resources.MissingManifestResourceException:
175         '     No usable set of resources is found, and there are no default resources. For
176         '     information about how to handle this exception, see the "Handling MissingManifestResourceException
177         '     and MissingSatelliteAssemblyException Exceptions" section in the System.Resources.ResourceManager
178         '     class topic.
179         '         '   T:System.Resources.MissingSatelliteAssemblyException:
180         '     The default culture's resources reside in a satellite assembly that could not
181         '     be found. For information about how to handle this exception, see the "Handling
182         '     MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions"
183         '     section in the System.Resources.ResourceManager class topic.
184
185         ''' <summary>
186         ''' Returns an unmanaged memory stream object from the specified resource, using
187         ''' the specified culture.
188         ''' </summary>
189         ''' <param name="name">The name of a resource.</param>
190         ''' <param name="culture">An object that specifies the culture to use for the resource lookup. If culture
191         ''' is null, the culture for the current thread is used.</param>
192         ''' <returns>An unmanaged memory stream object that represents a resource.</returns>
193         ''' 
194         <MethodImpl(MethodImplOptions.AggressiveInlining)>
195         <ComVisible(False)>
196         Public Function GetStream(name As String, culture As CultureInfo) As UnmanagedMemoryStream
197             Return Resources.GetStream(name, culture)
198         End Function
199
200         <MethodImpl(MethodImplOptions.AggressiveInlining)>
201         Public Function GetString(name As String, encoding As Encodings) As String
202             Dim stream As New StreamReader(GetStream(name), encoding.CodePage)
203             Return stream.ReadToEnd
204         End Function
205
206         ' Exceptions:
207         '   T:System.ArgumentNullException:
208         '     The name parameter is null.
209         '
210         '   T:System.InvalidOperationException:
211         '     The value of the specified resource is not a string.
212         '         '   T:System.Resources.MissingManifestResourceException:
213         '     No usable set of resources has been found, and there are no resources for the
214         '     default culture. For information about how to handle this exception, see the
215         '     "Handling MissingManifestResourceException and MissingSatelliteAssemblyException
216         '     Exceptions" section in the System.Resources.ResourceManager class topic.
217         '         '   T:System.Resources.MissingSatelliteAssemblyException:
218         '     The default culture's resources reside in a satellite assembly that could not
219         '     be found. For information about how to handle this exception, see the "Handling
220         '     MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions"
221         '     section in the System.Resources.ResourceManager class topic.
222
223         ''' <summary>
224         ''' Returns the value of the specified string resource.
225         ''' </summary>
226         ''' <param name="name">The name of the resource to retrieve.</param>
227         ''' <returns>The value of the resource localized for the caller's current UI culture, or null
228         ''' if name cannot be found in a resource set.</returns>
229         ''' 
230         <MethodImpl(MethodImplOptions.AggressiveInlining)>
231         Public Overridable Function GetString(name As StringAs String
232             Return Resources.GetString(name)
233         End Function
234
235         ' Exceptions:
236         '   T:System.ArgumentNullException:
237         '     The name parameter is null.
238         '
239         '   T:System.InvalidOperationException:
240         '     The value of the specified resource is not a string.
241         '         '   T:System.Resources.MissingManifestResourceException:
242         '     No usable set of resources has been found, and there are no resources for a default
243         '     culture. For information about how to handle this exception, see the "Handling
244         '     MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions"
245         '     section in the System.Resources.ResourceManager class topic.
246         '         '   T:System.Resources.MissingSatelliteAssemblyException:
247         '     The default culture's resources reside in a satellite assembly that could not
248         '     be found. For information about how to handle this exception, see the "Handling
249         '     MissingManifestResourceException and MissingSatelliteAssemblyException Exceptions"
250         '     section in the System.Resources.ResourceManager class topic.
251
252         ''' <summary>
253         ''' Returns the value of the string resource localized for the specified culture.
254         ''' </summary>
255         ''' <param name="name">The name of the resource to retrieve.</param>
256         ''' <param name="culture">An object that represents the culture for which the resource is localized.</param>
257         ''' <returns>The value of the resource localized for the specified culture, or null if name
258         ''' cannot be found in a resource set.</returns>
259         ''' 
260         <MethodImpl(MethodImplOptions.AggressiveInlining)>
261         Public Overridable Function GetString(name As String, culture As CultureInfo) As String
262             Return Resources.GetString(name, culture)
263         End Function
264
265         Sub New()
266             Call Me.New(Assembly.GetExecutingAssembly)
267         End Sub
268
269         ''' <summary>
270         ''' Load external resource data from current module's satellite assembly.
271         ''' </summary>
272         ''' <param name="type"></param>
273         Sub New(type As Type)
274             Call Me.New(type.Assembly)
275         End Sub
276
277         ''' <summary>
278         ''' 默认是<see cref="App.HOME"/>/Resources/assmFile
279         ''' </summary>
280         ''' <param name="assm"></param>
281         Sub New(assm As Assembly)
282             Call Me.New(dll:=assm.Location.ParentPath & "/Resources/" & FileIO.FileSystem.GetFileInfo(assm.Location).Name)
283         End Sub
284
285         Sub New(dll As String)
286             FileName = FileIO.FileSystem.GetFileInfo(dll).FullName
287             Call __resParser()
288         End Sub
289
290         Private Sub __resParser()
291             Call __load(Assembly.LoadFile(FileName))
292         End Sub
293
294         Private Sub __load(assm As Assembly)
295 #If NET_40 = 0 Then
296             Dim resource As Type = GetType(ExportAttribute)
297             Dim resourceMgr As Type = LinqAPI.DefaultFirst(Of Type) _
298  _
299                 () <= From type As Type
300                       In assm.GetTypes
301                       Let exp As ExportAttribute = type.GetCustomAttribute(resource)
302                       Where Not exp Is Nothing AndAlso
303                           exp.ContractType.Equals(GetType(ResourceManager))
304                       Select type
305
306             If Not resourceMgr Is Nothing Then
307                 Dim container = LinqAPI.DefaultFirst(Of PropertyInfo) _
308  _
309                     () <= From prop As PropertyInfo
310                           In resourceMgr.GetProperties(BindingFlags.Public Or BindingFlags.Static)
311                           Let exp As ExportAttribute = prop.GetCustomAttribute(resource)
312                           Where prop.CanRead AndAlso
313                               Not exp Is Nothing AndAlso
314                               exp.ContractType.Equals(GetType(ResourceManager))
315                           Select prop
316
317                 If Not container Is Nothing Then
318                     Dim mgr = container.GetValue(Nothing, Nothing)
319                     _Resources = DirectCast(mgr, ResourceManager)
320                 End If
321             End If
322 #End If
323         End Sub
324
325         ''' <summary>
326         ''' 
327         ''' </summary>
328         ''' <param name="my">null</param>
329         ''' <param name="assm"></param>
330         Private Sub New(my As Type, assm As Assembly)
331             Call __load(assm)
332         End Sub
333
334         ''' <summary>
335         ''' 从自身的程序集之中加载数据
336         ''' </summary>
337         ''' <returns></returns>
338         Public Shared Function LoadMy() As ResourcesSatellite
339             Return New ResourcesSatellite(Nothing, Assembly.GetExecutingAssembly)
340         End Function
341
342         Public Shared Function DirectLoadFrom(assm As Assembly) As ResourcesSatellite
343             Return New ResourcesSatellite(Nothing, assm)
344         End Function
345
346         ''' <summary>
347         ''' Returns the cached ResourceManager instance used by this class.
348         ''' </summary>
349         ''' <returns></returns>
350         ''' 
351         <Export(GetType(ResourceManager))>
352         Public Shared ReadOnly Property MyResource As ResourceManager
353             Get
354                 Return My.Resources.ResourceManager
355             End Get
356         End Property
357     End Class
358 End Namespace