Parcourir la source

feat: 支持转换 webp 图片

tangge233 il y a 2 ans
Parent
commit
08faaf1f46

+ 16 - 0
Plain Craft Launcher 2/Application.xaml.vb

@@ -96,6 +96,12 @@ Public Class Application
             End If
             '动态 DLL 调用
             AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf AssemblyResolve
+            SetDllDirectory(Path & "PCL\")
+            If Is32BitSystem Then
+                File.WriteAllBytes(Path & "PCL\libwebp.dll", GetResources("libwebp32"))
+            Else
+                File.WriteAllBytes(Path & "PCL\libwebp.dll", GetResources("libwebp64"))
+            End If
             '日志初始化
             LogStart()
             '添加日志
@@ -171,9 +177,11 @@ Public Class Application
     Private Shared AssemblyNAudio As Assembly
     Private Shared AssemblyJson As Assembly
     Private Shared AssemblyDialog As Assembly
+    Private Shared AssemblyWebp As Assembly
     Private Shared ReadOnly AssemblyNAudioLock As New Object
     Private Shared ReadOnly AssemblyJsonLock As New Object
     Private Shared ReadOnly AssemblyDialogLock As New Object
+    Private Shared ReadOnly AssemblyWebpLock As New Object
     Public Shared Function AssemblyResolve(sender As Object, args As ResolveEventArgs) As Assembly
         If args.Name.StartsWithF("NAudio") Then
             SyncLock AssemblyNAudioLock
@@ -199,6 +207,14 @@ Public Class Application
                 End If
                 Return AssemblyDialog
             End SyncLock
+        ElseIf args.Name.StartsWithF("Imazen.WebP") Then
+            SyncLock AssemblyWebpLock
+                If AssemblyWebp Is Nothing Then
+                    Log("[Start] 加载 DLL:Imazen.WebP")
+                    AssemblyWebp = Assembly.Load(GetResources("WebP"))
+                End If
+                Return AssemblyWebp
+            End SyncLock
         Else
             Return Nothing
         End If

+ 6 - 3
Plain Craft Launcher 2/Modules/Minecraft/MyCompItem.xaml.vb

@@ -18,9 +18,6 @@
                     '网络图片
                     If File.Exists(FileAddress) Then
                         PathLogo.Source = New MyBitmap(FileAddress)
-                    ElseIf _Logo.EndsWithF(".webp", True) Then 'Modrinth 林业 Mod 使用了不支持的 WebP 格式 Logo
-                        Log($"[Comp] 发现不支持的 WebP 格式图标,已更改为默认图标:{_Logo}")
-                        PathLogo.Source = New MyBitmap(PathImage & "Icons/NoIcon.png")
                     Else
                         PathLogo.Source = New MyBitmap(PathImage & "Icons/NoIcon.png")
                         RunInNewThread(Sub() LogoLoader(FileAddress), "Comp Logo Loader " & Uuid & "#", ThreadPriority.BelowNormal)
@@ -58,6 +55,12 @@ RetryStart:
             End If
             '下载图片
             NetDownload(Url, LocalFileAddress & DownloadEnd, True)
+            If Url.EndsWithF("webp") Then
+                Log($"[Comp] Webp 格式转换:{LocalFileAddress}")
+                Dim dec = New Imazen.WebP.SimpleDecoder()
+                Dim picFile = File.ReadAllBytes(LocalFileAddress & DownloadEnd)
+                dec.DecodeFromBytes(picFile, picFile.Length).Save(LocalFileAddress & DownloadEnd)
+            End If
             Dim LoadError As Exception = Nothing
             RunInUiWait(
             Sub()

+ 6 - 3
Plain Craft Launcher 2/Modules/Minecraft/MyLocalModItem.xaml.vb

@@ -20,9 +20,6 @@ Public Class MyLocalModItem
                     '网络图片
                     If File.Exists(FileAddress) Then
                         PathLogo.Source = New MyBitmap(FileAddress)
-                    ElseIf _Logo.EndsWithF(".webp", True) Then 'Modrinth 林业 Mod 使用了不支持的 WebP 格式 Logo
-                        Log($"[LocalModItem] 发现不支持的 WebP 格式图标,已更改为默认图标:{_Logo}")
-                        PathLogo.Source = New MyBitmap(PathImage & "Icons/NoIcon.png")
                     Else
                         PathLogo.Source = New MyBitmap(PathImage & "Icons/NoIcon.png")
                         RunInNewThread(Sub() LogoLoader(FileAddress), "Comp Logo Loader " & Uuid & "#", ThreadPriority.BelowNormal)
@@ -60,6 +57,12 @@ RetryStart:
             End If
             '下载图片
             NetDownload(Url, LocalFileAddress & DownloadEnd, True)
+            If Url.EndsWithF("webp") Then
+                Log($"[Comp] Webp 格式转换:{LocalFileAddress}")
+                Dim dec = New Imazen.WebP.SimpleDecoder()
+                Dim picFile = File.ReadAllBytes(LocalFileAddress & DownloadEnd)
+                dec.DecodeFromBytes(picFile, picFile.Length).Save(LocalFileAddress & DownloadEnd)
+            End If
             Dim LoadError As Exception = Nothing
             RunInUiWait(
             Sub()

+ 2 - 0
Plain Craft Launcher 2/Modules/ModMain.vb

@@ -798,6 +798,8 @@ NextFile:
     Public Declare Function SetForegroundWindow Lib "user32" (hWnd As IntPtr) As Integer
     Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (hWnd As IntPtr, msg As UInteger, wParam As Long, lParam As Long) As Boolean
 
+    Public Declare Function SetDllDirectory Lib "kernel32" Alias "SetDllDirectoryA" (lpPathName As String) As Boolean
+
 #End Region
 
     Public DragControl = Nothing

+ 30 - 0
Plain Craft Launcher 2/My Project/Resources.Designer.vb

@@ -120,6 +120,26 @@ Namespace My.Resources
             End Get
         End Property
         
+        '''<summary>
+        '''  查找 System.Byte[] 类型的本地化资源。
+        '''</summary>
+        Friend ReadOnly Property libwebp32() As Byte()
+            Get
+                Dim obj As Object = ResourceManager.GetObject("libwebp32", resourceCulture)
+                Return CType(obj,Byte())
+            End Get
+        End Property
+        
+        '''<summary>
+        '''  查找 System.Byte[] 类型的本地化资源。
+        '''</summary>
+        Friend ReadOnly Property libwebp64() As Byte()
+            Get
+                Dim obj As Object = ResourceManager.GetObject("libwebp64", resourceCulture)
+                Return CType(obj,Byte())
+            End Get
+        End Property
+        
         '''<summary>
         '''  查找 System.Byte[] 类型的本地化资源。
         '''</summary>
@@ -149,5 +169,15 @@ Namespace My.Resources
                 Return CType(obj,Byte())
             End Get
         End Property
+        
+        '''<summary>
+        '''  查找 System.Byte[] 类型的本地化资源。
+        '''</summary>
+        Friend ReadOnly Property WebP() As Byte()
+            Get
+                Dim obj As Object = ResourceManager.GetObject("WebP", resourceCulture)
+                Return CType(obj,Byte())
+            End Get
+        End Property
     End Module
 End Namespace

+ 9 - 0
Plain Craft Launcher 2/My Project/Resources.resx

@@ -136,6 +136,12 @@
   <data name="Json" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>..\Resources\Newtonsoft.Json.dll;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </data>
+  <data name="libwebp32" type="System.Resources.ResXFileRef, System.Windows.Forms">
+    <value>..\Resources\libwebp32.dll;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </data>
+  <data name="libwebp64" type="System.Resources.ResXFileRef, System.Windows.Forms">
+    <value>..\Resources\libwebp64.dll;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </data>
   <data name="ModData" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>..\Resources\ModData.txt;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </data>
@@ -145,4 +151,7 @@
   <data name="Transformer" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>..\Resources\transformer.jar;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </data>
+  <data name="WebP" type="System.Resources.ResXFileRef, System.Windows.Forms">
+    <value>..\Resources\Imazen.WebP.dll;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </data>
 </root>

+ 7 - 5
Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj

@@ -140,6 +140,10 @@
     <Prefer32Bit>false</Prefer32Bit>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Imazen.WebP, Version=10.0.1.0, Culture=neutral, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>Resources\Imazen.WebP.dll</HintPath>
+    </Reference>
     <Reference Include="NAudio, Version=1.7.3.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
       <Private>False</Private>
@@ -185,9 +189,6 @@
     <Compile Include="Controls\MyIconTextButton.xaml.vb">
       <DependentUpon>MyIconTextButton.xaml</DependentUpon>
     </Compile>
-
-
-
     <Compile Include="Modules\Minecraft\ModComp.vb" />
     <Compile Include="Modules\Minecraft\ModJava.vb" />
     <Compile Include="Modules\Minecraft\ModMod.vb" />
@@ -472,7 +473,6 @@
       <DependentUpon>FormMain.xaml</DependentUpon>
       <SubType>Code</SubType>
     </Compile>
-
     <Page Include="Modules\Minecraft\MyLocalModItem.xaml">
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
@@ -927,7 +927,9 @@
     <Resource Include="Images\Heads\MCBBS.png" />
   </ItemGroup>
   <ItemGroup>
-
+    <Content Include="Resources\Imazen.WebP.dll" />
+    <Content Include="Resources\libwebp32.dll" />
+    <Content Include="Resources\libwebp64.dll" />
   </ItemGroup>
   <ItemGroup>
     <Resource Include="Images\Blocks\NeoForge.png" />

BIN
Plain Craft Launcher 2/Resources/Imazen.WebP.dll


BIN
Plain Craft Launcher 2/Resources/libwebp32.dll


BIN
Plain Craft Launcher 2/Resources/libwebp64.dll