| 1 | #Region "Microsoft.VisualBasic::4997b427ff7828d26117155c6bab2511, Microsoft.VisualBasic.Core\CommandLine\CliResCommon.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 CliResCommon |
| 35 | ' |
| 36 | ' Constructor: (+1 Overloads) Sub New |
| 37 | ' Function: ToString, TryRelease |
| 38 | ' |
| 39 | ' |
| 40 | ' /********************************************************************************/ |
| 41 | |
| 42 | #End Region |
| 43 | |
| 44 | Imports System.Reflection |
| 45 | |
| 46 | Namespace CommandLine |
| 47 | |
| 48 | ''' <summary> |
| 49 | ''' CLI resources manager |
| 50 | ''' |
| 51 | ''' (将程序内部自身的资源数据释放到文件的帮助对象) |
| 52 | ''' </summary> |
| 53 | Public Class CliResCommon |
| 54 | |
| 55 | Private ReadOnly bufType As Type = GetType(Byte()) |
| 56 | Private ReadOnly Resource As Dictionary(Of String, Func(Of Byte())) |
| 57 | |
| 58 | ReadOnly EXPORT As String |
| 59 | |
| 60 | ''' <summary> |
| 61 | ''' 内部资源为一个可执行文件,其将会被释放到<paramref name="EXPORT"/>文件夹之中 |
| 62 | ''' </summary> |
| 63 | ''' <param name="EXPORT">资源文件的数据缓存文件夹</param> |
| 64 | Sub New(EXPORT As String, ResourceManager As Type) |
| 65 | Dim tag As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Static |
| 66 | Dim propBufs = From [Property] As PropertyInfo |
| 67 | In ResourceManager.GetProperties(bindingAttr:=tag) |
| 68 | Where [Property].PropertyType.Equals(bufType) |
| 69 | Select [Property] |
| 70 | |
| 71 | Me.EXPORT = EXPORT |
| 72 | Me.Resource = propBufs.ToDictionary(Of String, Func(Of Byte()))( |
| 73 | Function(x) x.Name, |
| 74 | Function(x) New Func(Of Byte())(Function() DirectCast(x.GetValue(Nothing, Nothing), Byte()))) |
| 75 | End Sub |
| 76 | |
| 77 | ''' <summary> |
| 78 | ''' |
| 79 | ''' </summary> |
| 80 | ''' <param name="Name">使用 NameOf 操作符来获取资源</param> |
| 81 | ''' <returns></returns> |
| 82 | Public Function TryRelease(Name As String, Optional ext As String = "exe") As String |
| 83 | Dim path As String = $"{EXPORT}/{Name}.{ext}" |
| 84 | |
| 85 | If path.FileExists Then |
| 86 | Return path |
| 87 | End If |
| 88 | |
| 89 | If Not Resource.ContainsKey(Name) Then |
| 90 | Return "" |
| 91 | End If |
| 92 | |
| 93 | Dim buf As Byte() = Resource(Name)() |
| 94 | Try |
| 95 | If buf.FlushStream(path) Then |
| 96 | Call Console.WriteLine(resReleaseMsg, path.ToFileURL, buf.Length) |
| 97 | Return path |
| 98 | Else |
| 99 | Return "" |
| 100 | End If |
| 101 | Catch ex As Exception |
| 102 | ex = New Exception(path, ex) |
| 103 | Call App.LogException(ex) |
| 104 | Return "" |
| 105 | End Try |
| 106 | End Function |
| 107 | |
| 108 | Const resReleaseMsg As String = "Release resource to {0} // length={1} bytes" |
| 109 | |
| 110 | Public Overrides Function ToString() As String |
| 111 | Return EXPORT |
| 112 | End Function |
| 113 | End Class |
| 114 | End Namespace |