From 94476384bd4767a4a9c42ea5cc22f3648bc4bc41 Mon Sep 17 00:00:00 2001 From: tangge233 Date: Tue, 1 Oct 2024 23:12:40 +0800 Subject: [PATCH 01/12] =?UTF-8?q?feat:=20MyImage=20=E6=8E=A7=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Plain Craft Launcher 2/Controls/MyImage.xaml | 7 ++++ .../Controls/MyImage.xaml.vb | 32 ++++++++++++++++ .../Modules/Base/MyBitmap.vb | 37 +++++++++++-------- .../Pages/PageLaunch/PageLaunchRight.xaml | 1 + .../Plain Craft Launcher 2.vbproj | 12 +++--- 5 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 Plain Craft Launcher 2/Controls/MyImage.xaml create mode 100644 Plain Craft Launcher 2/Controls/MyImage.xaml.vb diff --git a/Plain Craft Launcher 2/Controls/MyImage.xaml b/Plain Craft Launcher 2/Controls/MyImage.xaml new file mode 100644 index 00000000..b0fa06a0 --- /dev/null +++ b/Plain Craft Launcher 2/Controls/MyImage.xaml @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/Plain Craft Launcher 2/Controls/MyImage.xaml.vb b/Plain Craft Launcher 2/Controls/MyImage.xaml.vb new file mode 100644 index 00000000..9f4d9341 --- /dev/null +++ b/Plain Craft Launcher 2/Controls/MyImage.xaml.vb @@ -0,0 +1,32 @@ +Public Class MyImage + + '自定义属性 + Public Uuid As Integer = GetUuid() + Private _Uri As String = "" + + Public Property Source As String + Get + Return _Uri + End Get + Set(value As String) + _Uri = value + RefreshImage() + End Set + End Property '显示文本 + Public Shared ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( + Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs) + If Not IsNothing(sender) Then + CType(sender, MyImage)._Uri = e.NewValue.ToString() + CType(sender, MyImage).RefreshImage() + End If + End Sub))) + + Public Sub RefreshImage() + If Me Is Nothing Then Exit Sub + Try + PanContent.Source = New MyBitmap(_Uri.ToString()) + Catch ex As Exception + Log(ex, "刷新图片内容失败") + End Try + End Sub +End Class diff --git a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb index e60d27a8..8a0277a4 100644 --- a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb +++ b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb @@ -64,24 +64,29 @@ Public Class MyBitmap BitmapCache.Add(FilePathOrResourceName, Pic) End If Else + Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName) + If FilePathOrResourceName.StartsWith("http") Then + NetDownload(FilePathOrResourceName, tempFile, True) + FilePathOrResourceName = tempFile + End If '使用这种自己接管 FileStream 的方法加载才能解除文件占用 Using InputStream As New FileStream(FilePathOrResourceName, FileMode.Open) - '判断是否为 WebP 文件头 - Dim Header(1) As Byte - InputStream.Read(Header, 0, 2) - InputStream.Seek(0, SeekOrigin.Begin) - If Header(0) = 82 AndAlso Header(1) = 73 Then - '读取 WebP - If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。") - Dim FileBytes(InputStream.Length - 1) As Byte - InputStream.Read(FileBytes, 0, FileBytes.Length) - Dim Decoder As New Imazen.WebP.SimpleDecoder() - Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length) - Else - Pic = New System.Drawing.Bitmap(InputStream) - End If - End Using - End If + '判断是否为 WebP 文件头 + Dim Header(1) As Byte + InputStream.Read(Header, 0, 2) + InputStream.Seek(0, SeekOrigin.Begin) + If Header(0) = 82 AndAlso Header(1) = 73 Then + '读取 WebP + If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。") + Dim FileBytes(InputStream.Length - 1) As Byte + InputStream.Read(FileBytes, 0, FileBytes.Length) + Dim Decoder As New Imazen.WebP.SimpleDecoder() + Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length) + Else + Pic = New System.Drawing.Bitmap(InputStream) + End If + End Using + End If Catch ex As Exception Pic = My.Application.TryFindResource(FilePathOrResourceName) If Pic Is Nothing Then diff --git a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml index 1cc6a708..71642621 100644 --- a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml +++ b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml @@ -8,6 +8,7 @@ + Designer + + MyImage.xaml + @@ -191,9 +194,6 @@ MyIconTextButton.xaml - - - @@ -381,6 +381,10 @@ PageSpeedRight.xaml + + MSBuild:Compile + Designer + Designer MSBuild:Compile @@ -478,7 +482,6 @@ FormMain.xaml Code - MSBuild:Compile Designer @@ -913,7 +916,6 @@ - From 805a0e1544da253a35b0e5fd20ec6f2d27b01211 Mon Sep 17 00:00:00 2001 From: tangge233 Date: Tue, 1 Oct 2024 23:17:12 +0800 Subject: [PATCH 02/12] =?UTF-8?q?fix:=20=E6=B5=8B=E8=AF=95=E5=BF=98?= =?UTF-8?q?=E8=AE=B0=E5=88=A0=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml | 1 - 1 file changed, 1 deletion(-) diff --git a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml index 71642621..1cc6a708 100644 --- a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml +++ b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml @@ -8,7 +8,6 @@ - Date: Wed, 2 Oct 2024 12:07:42 +0800 Subject: [PATCH 03/12] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/{MyImage.xaml.vb => MyImage.vb} | 34 ++++++++------- Plain Craft Launcher 2/Controls/MyImage.xaml | 7 --- .../Modules/Base/MyBitmap.vb | 43 +++++++++++-------- .../Plain Craft Launcher 2.vbproj | 8 +--- 4 files changed, 44 insertions(+), 48 deletions(-) rename Plain Craft Launcher 2/Controls/{MyImage.xaml.vb => MyImage.vb} (55%) delete mode 100644 Plain Craft Launcher 2/Controls/MyImage.xaml diff --git a/Plain Craft Launcher 2/Controls/MyImage.xaml.vb b/Plain Craft Launcher 2/Controls/MyImage.vb similarity index 55% rename from Plain Craft Launcher 2/Controls/MyImage.xaml.vb rename to Plain Craft Launcher 2/Controls/MyImage.vb index 9f4d9341..f0c3d801 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.xaml.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -1,32 +1,34 @@ Public Class MyImage + Inherits Image + + '事件 - '自定义属性 Public Uuid As Integer = GetUuid() - Private _Uri As String = "" - Public Property Source As String + Private _SourceData As String = "" + + ''' + ''' 重写Image的Source属性 + ''' + Public Shadows Property Source As String Get - Return _Uri + Return _SourceData End Get Set(value As String) - _Uri = value - RefreshImage() + SetImage(value) End Set - End Property '显示文本 - Public Shared ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( + End Property + Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs) If Not IsNothing(sender) Then - CType(sender, MyImage)._Uri = e.NewValue.ToString() - CType(sender, MyImage).RefreshImage() + CType(sender, MyImage).SetImage(e.NewValue) End If End Sub))) - Public Sub RefreshImage() + Private Sub SetImage(source As String) If Me Is Nothing Then Exit Sub - Try - PanContent.Source = New MyBitmap(_Uri.ToString()) - Catch ex As Exception - Log(ex, "刷新图片内容失败") - End Try + If String.IsNullOrEmpty(source) Then Exit Sub + _SourceData = source + MyBase.Source = New MyBitmap(_SourceData) End Sub End Class diff --git a/Plain Craft Launcher 2/Controls/MyImage.xaml b/Plain Craft Launcher 2/Controls/MyImage.xaml deleted file mode 100644 index b0fa06a0..00000000 --- a/Plain Craft Launcher 2/Controls/MyImage.xaml +++ /dev/null @@ -1,7 +0,0 @@ - - - \ No newline at end of file diff --git a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb index 8a0277a4..33a31504 100644 --- a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb +++ b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb @@ -64,29 +64,36 @@ Public Class MyBitmap BitmapCache.Add(FilePathOrResourceName, Pic) End If Else - Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName) - If FilePathOrResourceName.StartsWith("http") Then + If FilePathOrResourceName.StartsWithF("http") Then '在线图片(这里判断 https:\\ 或 http:\\ 会出问题) + Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName) NetDownload(FilePathOrResourceName, tempFile, True) FilePathOrResourceName = tempFile + ElseIf FilePathOrResourceName.StartsWithF("data:image/png;base64,") Then 'base64 图片 + Dim base64Data = FilePathOrResourceName.Split(",")(1) + Dim imageData = Convert.FromBase64String(base64Data) + Using ms = New MemoryStream(imageData) + Pic = New System.Drawing.Bitmap(ms) + End Using + Return End If '使用这种自己接管 FileStream 的方法加载才能解除文件占用 Using InputStream As New FileStream(FilePathOrResourceName, FileMode.Open) - '判断是否为 WebP 文件头 - Dim Header(1) As Byte - InputStream.Read(Header, 0, 2) - InputStream.Seek(0, SeekOrigin.Begin) - If Header(0) = 82 AndAlso Header(1) = 73 Then - '读取 WebP - If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。") - Dim FileBytes(InputStream.Length - 1) As Byte - InputStream.Read(FileBytes, 0, FileBytes.Length) - Dim Decoder As New Imazen.WebP.SimpleDecoder() - Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length) - Else - Pic = New System.Drawing.Bitmap(InputStream) - End If - End Using - End If + '判断是否为 WebP 文件头 + Dim Header(1) As Byte + InputStream.Read(Header, 0, 2) + InputStream.Seek(0, SeekOrigin.Begin) + If Header(0) = 82 AndAlso Header(1) = 73 Then + '读取 WebP + If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。") + Dim FileBytes(InputStream.Length - 1) As Byte + InputStream.Read(FileBytes, 0, FileBytes.Length) + Dim Decoder As New Imazen.WebP.SimpleDecoder() + Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length) + Else + Pic = New System.Drawing.Bitmap(InputStream) + End If + End Using + End If Catch ex As Exception Pic = My.Application.TryFindResource(FilePathOrResourceName) If Pic Is Nothing Then diff --git a/Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj b/Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj index 323ad7da..85d3bd72 100644 --- a/Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj +++ b/Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj @@ -183,9 +183,6 @@ Designer - - MyImage.xaml - @@ -194,6 +191,7 @@ MyIconTextButton.xaml + @@ -381,10 +379,6 @@ PageSpeedRight.xaml - - MSBuild:Compile - Designer - Designer MSBuild:Compile From e782d0ed2de1797eff95e2982904600bc4ce0455 Mon Sep 17 00:00:00 2001 From: tangge233 Date: Wed, 2 Oct 2024 17:22:20 +0800 Subject: [PATCH 04/12] fix: apply suggestions --- Plain Craft Launcher 2/Controls/MyImage.vb | 15 +++++---------- Plain Craft Launcher 2/Modules/Base/MyBitmap.vb | 7 ------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/Plain Craft Launcher 2/Controls/MyImage.vb b/Plain Craft Launcher 2/Controls/MyImage.vb index f0c3d801..5e091754 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -3,8 +3,6 @@ '事件 - Public Uuid As Integer = GetUuid() - Private _SourceData As String = "" ''' @@ -15,20 +13,17 @@ Return _SourceData End Get Set(value As String) - SetImage(value) + If String.IsNullOrEmpty(value) Then Exit Property + _SourceData = value + MyBase.Source = New MyBitmap(_SourceData) End Set End Property Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs) If Not IsNothing(sender) Then - CType(sender, MyImage).SetImage(e.NewValue) + If String.IsNullOrEmpty(e.NewValue.ToString()) Then Exit Sub + CType(sender, MyImage).Source = e.NewValue.ToString() End If End Sub))) - Private Sub SetImage(source As String) - If Me Is Nothing Then Exit Sub - If String.IsNullOrEmpty(source) Then Exit Sub - _SourceData = source - MyBase.Source = New MyBitmap(_SourceData) - End Sub End Class diff --git a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb index 33a31504..0f63b315 100644 --- a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb +++ b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb @@ -68,13 +68,6 @@ Public Class MyBitmap Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName) NetDownload(FilePathOrResourceName, tempFile, True) FilePathOrResourceName = tempFile - ElseIf FilePathOrResourceName.StartsWithF("data:image/png;base64,") Then 'base64 图片 - Dim base64Data = FilePathOrResourceName.Split(",")(1) - Dim imageData = Convert.FromBase64String(base64Data) - Using ms = New MemoryStream(imageData) - Pic = New System.Drawing.Bitmap(ms) - End Using - Return End If '使用这种自己接管 FileStream 的方法加载才能解除文件占用 Using InputStream As New FileStream(FilePathOrResourceName, FileMode.Open) From 788f7c9a9664c280b8802508b57a77230310c5be Mon Sep 17 00:00:00 2001 From: tangge233 Date: Thu, 3 Oct 2024 13:49:54 +0800 Subject: [PATCH 05/12] =?UTF-8?q?fix:=20=E4=B8=8D=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=A3=81=E7=9B=98=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modules/Base/MyBitmap.vb | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb index 0f63b315..a7b8897f 100644 --- a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb +++ b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb @@ -64,28 +64,28 @@ Public Class MyBitmap BitmapCache.Add(FilePathOrResourceName, Pic) End If Else + Dim InputStream As Stream If FilePathOrResourceName.StartsWithF("http") Then '在线图片(这里判断 https:\\ 或 http:\\ 会出问题) - Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName) - NetDownload(FilePathOrResourceName, tempFile, True) - FilePathOrResourceName = tempFile + Dim Client = New Net.WebClient() + InputStream = New MemoryStream(Client.DownloadData(FilePathOrResourceName)) + Else + InputStream = New MemoryStream(File.ReadAllBytes(FilePathOrResourceName)) End If '使用这种自己接管 FileStream 的方法加载才能解除文件占用 - Using InputStream As New FileStream(FilePathOrResourceName, FileMode.Open) - '判断是否为 WebP 文件头 - Dim Header(1) As Byte - InputStream.Read(Header, 0, 2) - InputStream.Seek(0, SeekOrigin.Begin) - If Header(0) = 82 AndAlso Header(1) = 73 Then - '读取 WebP - If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。") - Dim FileBytes(InputStream.Length - 1) As Byte - InputStream.Read(FileBytes, 0, FileBytes.Length) - Dim Decoder As New Imazen.WebP.SimpleDecoder() - Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length) - Else - Pic = New System.Drawing.Bitmap(InputStream) - End If - End Using + '判断是否为 WebP 文件头 + Dim Header(1) As Byte + InputStream.Read(Header, 0, 2) + InputStream.Seek(0, SeekOrigin.Begin) + If Header(0) = 82 AndAlso Header(1) = 73 Then + '读取 WebP + If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。") + Dim FileBytes(InputStream.Length - 1) As Byte + InputStream.Read(FileBytes, 0, FileBytes.Length) + Dim Decoder As New Imazen.WebP.SimpleDecoder() + Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length) + Else + Pic = New System.Drawing.Bitmap(InputStream) + End If End If Catch ex As Exception Pic = My.Application.TryFindResource(FilePathOrResourceName) From e6c1b192ce6c5a71f8d58b78727f392f9707512d Mon Sep 17 00:00:00 2001 From: tangge233 Date: Tue, 8 Oct 2024 23:30:45 +0800 Subject: [PATCH 06/12] Revert MyBitmap.vb --- .../Modules/Base/MyBitmap.vb | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb index a7b8897f..ac9fb213 100644 --- a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb +++ b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb @@ -64,28 +64,23 @@ Public Class MyBitmap BitmapCache.Add(FilePathOrResourceName, Pic) End If Else - Dim InputStream As Stream - If FilePathOrResourceName.StartsWithF("http") Then '在线图片(这里判断 https:\\ 或 http:\\ 会出问题) - Dim Client = New Net.WebClient() - InputStream = New MemoryStream(Client.DownloadData(FilePathOrResourceName)) - Else - InputStream = New MemoryStream(File.ReadAllBytes(FilePathOrResourceName)) - End If '使用这种自己接管 FileStream 的方法加载才能解除文件占用 - '判断是否为 WebP 文件头 - Dim Header(1) As Byte - InputStream.Read(Header, 0, 2) - InputStream.Seek(0, SeekOrigin.Begin) - If Header(0) = 82 AndAlso Header(1) = 73 Then - '读取 WebP - If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。") - Dim FileBytes(InputStream.Length - 1) As Byte - InputStream.Read(FileBytes, 0, FileBytes.Length) - Dim Decoder As New Imazen.WebP.SimpleDecoder() - Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length) - Else - Pic = New System.Drawing.Bitmap(InputStream) - End If + Using InputStream As New FileStream(FilePathOrResourceName, FileMode.Open) + '判断是否为 WebP 文件头 + Dim Header(1) As Byte + InputStream.Read(Header, 0, 2) + InputStream.Seek(0, SeekOrigin.Begin) + If Header(0) = 82 AndAlso Header(1) = 73 Then + '读取 WebP + If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。") + Dim FileBytes(InputStream.Length - 1) As Byte + InputStream.Read(FileBytes, 0, FileBytes.Length) + Dim Decoder As New Imazen.WebP.SimpleDecoder() + Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length) + Else + Pic = New System.Drawing.Bitmap(InputStream) + End If + End Using End If Catch ex As Exception Pic = My.Application.TryFindResource(FilePathOrResourceName) @@ -157,4 +152,4 @@ Public Class MyBitmap End Using End Sub -End Class +End Class \ No newline at end of file From 71d915f3ef51f97bc5e94b328834e8f864d3f52a Mon Sep 17 00:00:00 2001 From: tangge233 Date: Wed, 9 Oct 2024 00:14:05 +0800 Subject: [PATCH 07/12] =?UTF-8?q?feat:=20UseCache=20=E9=80=89=E9=A1=B9?= =?UTF-8?q?=E5=92=8C=E5=BC=82=E6=AD=A5=E5=8A=A0=E8=BD=BD=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Plain Craft Launcher 2/Controls/MyImage.vb | 44 +++++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/Plain Craft Launcher 2/Controls/MyImage.vb b/Plain Craft Launcher 2/Controls/MyImage.vb index 5e091754..add33063 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -1,9 +1,24 @@ -Public Class MyImage +Imports System.Threading.Tasks + +Public Class MyImage Inherits Image '事件 Private _SourceData As String = "" + Private _UseCache As Boolean = False + Private _DownloadTask As Task + + Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期 + + Public Property UseCache As Boolean + Get + Return _UseCache + End Get + Set(value As Boolean) + _UseCache = value + End Set + End Property ''' ''' 重写Image的Source属性 @@ -15,7 +30,32 @@ Set(value As String) If String.IsNullOrEmpty(value) Then Exit Property _SourceData = value - MyBase.Source = New MyBitmap(_SourceData) + If Not value.StartsWithF("http") Then ' 本地资源直接使用 + MyBase.Source = New MyBitmap(_SourceData) + Exit Property + End If + If _DownloadTask IsNot Nothing AndAlso Not _DownloadTask.IsCompleted Then ' 之前下载任务还在,直接砍了 + _DownloadTask.Dispose() + End If + Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存 + Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png" + If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下 + If Not NeedDownload Then + MyBase.Source = New MyBitmap(TempFilePath) + Exit Property + End If + ' 异步下载图片 + Dim TaskID = 0 + _DownloadTask = New Task(Sub() + NetDownload(_SourceData, TempFilePath, True) + End Sub) + TaskID = _DownloadTask.Id + _DownloadTask.Start() + _DownloadTask.ContinueWith(Sub(t) + If t.IsCompleted AndAlso t.Id = TaskID Then ' 任务没有被干掉 + MyBase.Source = New MyBitmap(TempFilePath) + End If + End Sub, TaskScheduler.FromCurrentSynchronizationContext()) End Set End Property Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( From 50112b70bdbcc528bbc74f8a0bc51f7e542d003f Mon Sep 17 00:00:00 2001 From: tangge233 Date: Wed, 9 Oct 2024 00:24:47 +0800 Subject: [PATCH 08/12] chore: Try Catch --- Plain Craft Launcher 2/Controls/MyImage.vb | 56 ++++++++++++---------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/Plain Craft Launcher 2/Controls/MyImage.vb b/Plain Craft Launcher 2/Controls/MyImage.vb index add33063..b720fe1c 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -30,32 +30,36 @@ Public Class MyImage Set(value As String) If String.IsNullOrEmpty(value) Then Exit Property _SourceData = value - If Not value.StartsWithF("http") Then ' 本地资源直接使用 - MyBase.Source = New MyBitmap(_SourceData) - Exit Property - End If - If _DownloadTask IsNot Nothing AndAlso Not _DownloadTask.IsCompleted Then ' 之前下载任务还在,直接砍了 - _DownloadTask.Dispose() - End If - Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存 - Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png" - If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下 - If Not NeedDownload Then - MyBase.Source = New MyBitmap(TempFilePath) - Exit Property - End If - ' 异步下载图片 - Dim TaskID = 0 - _DownloadTask = New Task(Sub() - NetDownload(_SourceData, TempFilePath, True) - End Sub) - TaskID = _DownloadTask.Id - _DownloadTask.Start() - _DownloadTask.ContinueWith(Sub(t) - If t.IsCompleted AndAlso t.Id = TaskID Then ' 任务没有被干掉 - MyBase.Source = New MyBitmap(TempFilePath) - End If - End Sub, TaskScheduler.FromCurrentSynchronizationContext()) + Try + If Not value.StartsWithF("http") Then ' 本地资源直接使用 + MyBase.Source = New MyBitmap(_SourceData) + Exit Property + End If + If _DownloadTask IsNot Nothing AndAlso Not _DownloadTask.IsCompleted Then ' 之前下载任务还在,直接砍了 + _DownloadTask.Dispose() + End If + Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存 + Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png" + If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下 + If Not NeedDownload Then + MyBase.Source = New MyBitmap(TempFilePath) + Exit Property + End If + ' 异步下载图片 + Dim TaskID = 0 + _DownloadTask = New Task(Sub() + NetDownload(_SourceData, TempFilePath, True) + End Sub) + TaskID = _DownloadTask.Id + _DownloadTask.Start() + _DownloadTask.ContinueWith(Sub(t) + If t.IsCompleted AndAlso t.Id = TaskID Then ' 任务没有被干掉 + MyBase.Source = New MyBitmap(TempFilePath) + End If + End Sub, TaskScheduler.FromCurrentSynchronizationContext()) + Catch ex As Exception + Log(ex, "加载图片失败") + End Try End Set End Property Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( From 7ba082c3f261820ad015f3143d02426c4f73041c Mon Sep 17 00:00:00 2001 From: tangge233 Date: Sun, 13 Oct 2024 21:33:10 +0800 Subject: [PATCH 09/12] chore: apply suggestions --- Plain Craft Launcher 2/Controls/MyImage.vb | 45 ++++++++++++------- .../Pages/PageLaunch/PageLaunchRight.xaml | 1 + 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Plain Craft Launcher 2/Controls/MyImage.vb b/Plain Craft Launcher 2/Controls/MyImage.vb index b720fe1c..93f1588d 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -7,7 +7,6 @@ Public Class MyImage Private _SourceData As String = "" Private _UseCache As Boolean = False - Private _DownloadTask As Task Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期 @@ -35,9 +34,6 @@ Public Class MyImage MyBase.Source = New MyBitmap(_SourceData) Exit Property End If - If _DownloadTask IsNot Nothing AndAlso Not _DownloadTask.IsCompleted Then ' 之前下载任务还在,直接砍了 - _DownloadTask.Dispose() - End If Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存 Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png" If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下 @@ -45,18 +41,9 @@ Public Class MyImage MyBase.Source = New MyBitmap(TempFilePath) Exit Property End If - ' 异步下载图片 - Dim TaskID = 0 - _DownloadTask = New Task(Sub() - NetDownload(_SourceData, TempFilePath, True) - End Sub) - TaskID = _DownloadTask.Id - _DownloadTask.Start() - _DownloadTask.ContinueWith(Sub(t) - If t.IsCompleted AndAlso t.Id = TaskID Then ' 任务没有被干掉 - MyBase.Source = New MyBitmap(TempFilePath) - End If - End Sub, TaskScheduler.FromCurrentSynchronizationContext()) + ' 开一个线程处理在线图片 + RunInNewThread(Sub() PicLoader(_SourceData, TempFilePath), "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal) + Catch ex As Exception Log(ex, "加载图片失败") End Try @@ -70,4 +57,30 @@ Public Class MyImage End If End Sub))) + Private Sub PicLoader(FileUrl As String, TempFilePath As String) + Dim Retried As Boolean = False +RetryStart: + Try + If File.Exists(TempFilePath) Then '先显示着旧图片,下载新图片 + Rename(TempFilePath, TempFilePath & ".old") + RunInUi(Sub() + MyBase.Source = New MyBitmap(TempFilePath & ".old") + End Sub) + End If + NetDownload(FileUrl, TempFilePath) + RunInUi(Sub() + MyBase.Source = New MyBitmap(TempFilePath) + End Sub) + File.Delete(TempFilePath & ".old") + Catch ex As Exception + If Not Retried Then + Retried = True + GoTo RetryStart + Else + Log(ex, $"[MyImage] 下载图片失败") + RunInUi(Sub() MyBase.Source = New MyBitmap(PathImage & "Icons/NoIcon.png")) + End If + End Try + End Sub + End Class diff --git a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml index 1cc6a708..9c02a492 100644 --- a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml +++ b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml @@ -6,6 +6,7 @@ xmlns:local="clr-namespace:PCL"> + From e1d58151cf9c59ac83f8184589618de4fb747b45 Mon Sep 17 00:00:00 2001 From: tangge233 Date: Sun, 13 Oct 2024 21:39:33 +0800 Subject: [PATCH 10/12] chore: add summary tip --- Plain Craft Launcher 2/Controls/MyImage.vb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Plain Craft Launcher 2/Controls/MyImage.vb b/Plain Craft Launcher 2/Controls/MyImage.vb index 93f1588d..c3582e3f 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -10,6 +10,10 @@ Public Class MyImage Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期 + ''' + ''' 是否使用缓存,需要先于 Source 属性设置,否则无效 + ''' + ''' Public Property UseCache As Boolean Get Return _UseCache @@ -78,7 +82,6 @@ RetryStart: GoTo RetryStart Else Log(ex, $"[MyImage] 下载图片失败") - RunInUi(Sub() MyBase.Source = New MyBitmap(PathImage & "Icons/NoIcon.png")) End If End Try End Sub From 8d266c8a0b8a468a6a2e7dbf649fca74e303f669 Mon Sep 17 00:00:00 2001 From: tangge233 Date: Sun, 13 Oct 2024 22:43:31 +0800 Subject: [PATCH 11/12] chore: apply suggestions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: 龙腾猫跃 <1043137532@qq.com> --- Plain Craft Launcher 2/Controls/MyImage.vb | 36 +++++++++---------- .../Pages/PageLaunch/PageLaunchRight.xaml | 1 - 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/Plain Craft Launcher 2/Controls/MyImage.vb b/Plain Craft Launcher 2/Controls/MyImage.vb index c3582e3f..efe76bf6 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -1,28 +1,23 @@ -Imports System.Threading.Tasks - -Public Class MyImage +Public Class MyImage Inherits Image - '事件 - - Private _SourceData As String = "" - Private _UseCache As Boolean = False - Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期 ''' - ''' 是否使用缓存,需要先于 Source 属性设置,否则无效 + ''' 是否使用缓存 ''' ''' Public Property UseCache As Boolean Get - Return _UseCache + Return GetValue(UseCacheProperty) End Get Set(value As Boolean) - _UseCache = value + SetValue(UseCacheProperty, value) End Set End Property + Public Shared ReadOnly UseCacheProperty As DependencyProperty = DependencyProperty.Register("UseCache", GetType(Boolean), GetType(MyImage), New PropertyMetadata(True)) + Private _SourceData As String = "" ''' ''' 重写Image的Source属性 ''' @@ -40,12 +35,17 @@ Public Class MyImage End If Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存 Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png" - If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下 + If UseCache AndAlso File.Exists(TempFilePath) AndAlso (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下 If Not NeedDownload Then MyBase.Source = New MyBitmap(TempFilePath) Exit Property End If - ' 开一个线程处理在线图片 + + If File.Exists(TempFilePath) Then '先显示着旧图片,下载新图片 + Rename(TempFilePath, TempFilePath & ".old") + MyBase.Source = New MyBitmap(TempFilePath & ".old") + End If + ' 开一个线程下载在线图片 RunInNewThread(Sub() PicLoader(_SourceData, TempFilePath), "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal) Catch ex As Exception @@ -65,13 +65,9 @@ Public Class MyImage Dim Retried As Boolean = False RetryStart: Try - If File.Exists(TempFilePath) Then '先显示着旧图片,下载新图片 - Rename(TempFilePath, TempFilePath & ".old") - RunInUi(Sub() - MyBase.Source = New MyBitmap(TempFilePath & ".old") - End Sub) - End If - NetDownload(FileUrl, TempFilePath) + Dim UnCompleteFile As String = TempFilePath & ".dl" '加一个下载中的后缀,防止中途关闭程序下载中断但是没下完,从而导致第二次显示的是损坏的图片…… + NetDownload(FileUrl, UnCompleteFile) + Rename(UnCompleteFile, TempFilePath) RunInUi(Sub() MyBase.Source = New MyBitmap(TempFilePath) End Sub) diff --git a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml index 9c02a492..1cc6a708 100644 --- a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml +++ b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml @@ -6,7 +6,6 @@ xmlns:local="clr-namespace:PCL"> - From 6de59a7f5b7c5bde7823103b1ecda1f0627e445c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Mon, 14 Oct 2024 03:51:13 +0800 Subject: [PATCH 12/12] Update MyImage.vb --- Plain Craft Launcher 2/Controls/MyImage.vb | 145 +++++++++++++-------- 1 file changed, 88 insertions(+), 57 deletions(-) diff --git a/Plain Craft Launcher 2/Controls/MyImage.vb b/Plain Craft Launcher 2/Controls/MyImage.vb index efe76bf6..a5a71442 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -1,85 +1,116 @@ -Public Class MyImage +Public Class MyImage Inherits Image - Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期 +#Region "辅助属性注册" ''' - ''' 是否使用缓存 + ''' 网络图片的缓存有效期。 + ''' 在这个时间后,才会重新尝试下载图片。 ''' - ''' - Public Property UseCache As Boolean + Public FileCacheExpiredTime As New TimeSpan(7, 0, 0, 0) '7 天 + + ''' + ''' 是否允许将网络图片存储到本地用作缓存。 + ''' + Public Property EnableCache As Boolean Get - Return GetValue(UseCacheProperty) + Return _EnableCache End Get Set(value As Boolean) - SetValue(UseCacheProperty, value) + _EnableCache = value End Set End Property - Public Shared ReadOnly UseCacheProperty As DependencyProperty = DependencyProperty.Register("UseCache", GetType(Boolean), GetType(MyImage), New PropertyMetadata(True)) + Private _EnableCache As Boolean = True + + '将 Source 属性映射到 XAML + Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register( + "Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( + Sub(sender, e) If sender IsNot Nothing Then CType(sender, MyImage).Source = e.NewValue.ToString()))) + +#End Region - Private _SourceData As String = "" + Private _Source As String = "" ''' - ''' 重写Image的Source属性 + ''' 与 Image 的 Source 类似。 + ''' 若输入以 http 开头的字符串,则会尝试下载图片然后显示,图片会保存为本地缓存。 + ''' 支持 WebP 格式的图片。 ''' - Public Shadows Property Source As String + Public Shadows Property Source As String '覆写 Image 的 Source 属性 Get - Return _SourceData + Return _Source End Get Set(value As String) - If String.IsNullOrEmpty(value) Then Exit Property - _SourceData = value + If value = "" Then value = Nothing + If _Source = value Then Exit Property + _Source = value + Dim TempPath As String = $"{PathTemp}MyImage\{GetHash(value)}.png" Try - If Not value.StartsWithF("http") Then ' 本地资源直接使用 - MyBase.Source = New MyBitmap(_SourceData) + '空 + If value Is Nothing Then + MyBase.Source = Nothing Exit Property End If - Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存 - Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png" - If UseCache AndAlso File.Exists(TempFilePath) AndAlso (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下 - If Not NeedDownload Then - MyBase.Source = New MyBitmap(TempFilePath) + '本地图片 + If Not value.StartsWithF("http") Then + MyBase.Source = New MyBitmap(value) Exit Property End If - - If File.Exists(TempFilePath) Then '先显示着旧图片,下载新图片 - Rename(TempFilePath, TempFilePath & ".old") - MyBase.Source = New MyBitmap(TempFilePath & ".old") + '从缓存加载网络图片 + If EnableCache AndAlso File.Exists(TempPath) Then + MyBase.Source = New MyBitmap(TempPath) + If (Date.Now - File.GetCreationTime(TempPath)) < FileCacheExpiredTime Then + Exit Property '无需刷新缓存 + Else + File.Delete(TempPath) '需要刷新缓存 + End If + Else + MyBase.Source = Nothing '清空显示 End If - ' 开一个线程下载在线图片 - RunInNewThread(Sub() PicLoader(_SourceData, TempFilePath), "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal) - + '下载网络图片 + RunInNewThread( + Sub() + Dim Url As String = value '重新捕获变量,以检测在下载过程中 Source 被修改的情况 + Dim Retried As Boolean = False +RetryStart: + Dim TempDownloadingPath As String = TempPath & RandomInteger(0, 10000000) + Try + Log("[MyImage] 正在下载图片:" & Url) + NetDownload(Url, TempDownloadingPath, True) + If Url <> Source Then + '若 Source 在下载时被修改,则不显示 + File.Delete(TempDownloadingPath) + ElseIf EnableCache Then + '保存缓存并显示 + Rename(TempDownloadingPath, TempPath) + RunInUi(Sub() MyBase.Source = New MyBitmap(TempPath)) + Else + '直接显示 + RunInUiWait(Sub() MyBase.Source = New MyBitmap(TempDownloadingPath)) + File.Delete(TempDownloadingPath) + End If + Catch ex As Exception + Try + File.Delete(TempDownloadingPath) + Catch + End Try + If Not Retried Then + Log(ex, $"下载图片可重试地失败({Url})", LogLevel.Developer) + Retried = True + Thread.Sleep(1000) + GoTo RetryStart + Else + Log(ex, $"下载图片失败({Url})", LogLevel.Hint) + End If + End Try + End Sub, "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal) Catch ex As Exception - Log(ex, "加载图片失败") + Log(ex, $"加载图片失败({value})", LogLevel.Hint) + Try + File.Delete(TempPath) '删除缓存,以免缓存出现问题导致一直加载失败 + Catch + End Try End Try End Set End Property - Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( - Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs) - If Not IsNothing(sender) Then - If String.IsNullOrEmpty(e.NewValue.ToString()) Then Exit Sub - CType(sender, MyImage).Source = e.NewValue.ToString() - End If - End Sub))) - - Private Sub PicLoader(FileUrl As String, TempFilePath As String) - Dim Retried As Boolean = False -RetryStart: - Try - Dim UnCompleteFile As String = TempFilePath & ".dl" '加一个下载中的后缀,防止中途关闭程序下载中断但是没下完,从而导致第二次显示的是损坏的图片…… - NetDownload(FileUrl, UnCompleteFile) - Rename(UnCompleteFile, TempFilePath) - RunInUi(Sub() - MyBase.Source = New MyBitmap(TempFilePath) - End Sub) - File.Delete(TempFilePath & ".old") - Catch ex As Exception - If Not Retried Then - Retried = True - GoTo RetryStart - Else - Log(ex, $"[MyImage] 下载图片失败") - End If - End Try - End Sub End Class