Skip to content

Commit

Permalink
Remove PListNet lib, update webview2 lib, Remove unsafe code
Browse files Browse the repository at this point in the history
  • Loading branch information
HeHang0 committed Jul 27, 2023
1 parent 27472af commit 1837f21
Show file tree
Hide file tree
Showing 27 changed files with 1,116 additions and 595 deletions.
1 change: 1 addition & 0 deletions FileViewer.Hook/FileViewer.Hook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<BaseOutputPath>..\FileViewer\bin</BaseOutputPath>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 0 additions & 9 deletions FileViewer.Icns/FileViewer.Icns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<WarningLevel>0</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<WarningLevel>0</WarningLevel>
</PropertyGroup>

<ItemGroup>
Expand Down
55 changes: 51 additions & 4 deletions FileViewer.Icns/IcnsParser/IcnsDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#if _FREEIMAGE
using FreeImageAPI;
Expand Down Expand Up @@ -330,15 +331,61 @@ private static IcnsImage TryDecodingPng(IcnsImageParser.IcnsElement element, Icn
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void SetPixel(BitmapData data, int x, int y, uint color)
internal static void SetPixel(BitmapData bmpData, int x, int y, uint color)
{
*(((uint*)data.Scan0) + y * data.Width + x) = color;
// Calculate the position of the pixel in the byte array.
int position = (y * bmpData.Stride) + (x * 4);

// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmpData.Height;
byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.
Marshal.Copy(ptr, rgbValues, 0, bytes);

// Set the pixel to the given color.
uint blue = color & 0xff;
uint green = (color >> 8) & 0xff;
uint red = (color >> 16) & 0xff;
uint alpha = (color >> 24) & 0xff;

rgbValues[position] = (byte)blue;
rgbValues[position + 1] = (byte)green;
rgbValues[position + 2] = (byte)red;
rgbValues[position + 3] = (byte)alpha;

// Copy the RGB values back to the bitmap.
Marshal.Copy(rgbValues, 0, ptr, bytes);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe uint GetPixel(BitmapData data, int x, int y)
internal static uint GetPixel(BitmapData bmpData, int x, int y)
{
return *(((uint*)data.Scan0) + y * data.Width + x);
// Calculate the position of the pixel in the byte array.
int position = (y * bmpData.Stride) + (x * 4);

// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmpData.Height;
byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.
Marshal.Copy(ptr, rgbValues, 0, bytes);

// Get the color of the pixel.
byte blue = rgbValues[position];
byte green = rgbValues[position + 1];
byte red = rgbValues[position + 2];
byte alpha = rgbValues[position + 3];

uint color = (uint)((alpha << 24) | (red << 16) | (green << 8) | blue);

return color;
}

private static void Decode1BPPImage(IcnsType imageType, byte[] imageData, BitmapData image)
Expand Down
9 changes: 2 additions & 7 deletions FileViewer.Icns/IcnsParser/IcnsImageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace FileViewer.Icns.IcnsParser
{
Expand Down Expand Up @@ -110,13 +111,7 @@ public static IcnsImage GetImage(Stream stream)
List<IcnsImage> result = IcnsDecoder.DecodeAllImages(icnsContents);
if (result.Count <= 0)
throw new NotSupportedException("No icons in ICNS file");

IcnsImage max = null;
foreach (IcnsImage bitmap in result)
if (bitmap.Bitmap != null && (max == null || (bitmap.Bitmap.Width > bitmap.Bitmap.Height)))
max = bitmap;

return max;
return result.OrderByDescending(m => m.Bitmap.Height).FirstOrDefault()!;
}

public static List<IcnsImage> GetImages(string filename)
Expand Down
182 changes: 55 additions & 127 deletions FileViewer.Plugins/FileViewer.Plugins.App/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using FileViewer.Base;
using FileViewer.Icns.IcnsParser;
using FileViewer.Tools;
using PListNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -162,51 +161,48 @@ private long GetDirectorySize(DirectoryInfo dirInfo)
byte[]? appIcon = null;
var fileSize = GetDirectorySize(appPath);
ApkInfo appInfo = new();
using (FileStream stream = File.OpenRead(infoPath))
Dictionary<string, object> node = (Dictionary<string, object>)PList.ReadPlist(infoPath);
node.TryGetValue("CFBundleName", out object? nameNode);
appInfo.Label = PList.ParsePNodeString(nameNode);
node.TryGetValue("CFBundleIdentifier", out object? packageNode);
appInfo.PackageName = PList.ParsePNodeString(packageNode);
node.TryGetValue("CFBundleShortVersionString", out object? versionNode);
appInfo.VersionName = PList.ParsePNodeString(versionNode);
node.TryGetValue("LSMinimumSystemVersion", out object? minOSNode);
appInfo.MinSdkVersion = "MacOS " + PList.ParsePNodeString(minOSNode);
node.TryGetValue("DTSDKName", out object? targetOSNode);
appInfo.TargetSdkVersion = "MacOS " + Regex.Replace(PList.ParsePNodeString(targetOSNode), "[a-zA-Z]", "");
node.TryGetValue("CFBundleSupportedPlatforms", out object? familyNode);
if (familyNode != null && familyNode.GetType().IsAssignableTo(typeof(IList<object>)))
{
PListNet.Nodes.DictionaryNode node = (PListNet.Nodes.DictionaryNode)PList.Load(stream);
node.TryGetValue("CFBundleName", out PNode nameNode);
appInfo.Label = ParsePNodeString(nameNode);
node.TryGetValue("CFBundleIdentifier", out PNode packageNode);
appInfo.PackageName = ParsePNodeString(packageNode);
node.TryGetValue("CFBundleShortVersionString", out PNode versionNode);
appInfo.VersionName = ParsePNodeString(versionNode);
node.TryGetValue("LSMinimumSystemVersion", out PNode minOSNode);
appInfo.MinSdkVersion = "MacOS " + ParsePNodeString(minOSNode);
node.TryGetValue("DTSDKName", out PNode targetOSNode);
appInfo.TargetSdkVersion = "MacOS " + Regex.Replace(ParsePNodeString(targetOSNode), "[a-zA-Z]", "");
node.TryGetValue("CFBundleSupportedPlatforms", out PNode familyNode);
if (familyNode != null && familyNode.GetType() == typeof(PListNet.Nodes.ArrayNode))
foreach (var item in (IList<object>)familyNode)
{
foreach (var item in ((PListNet.Nodes.ArrayNode)familyNode))
{
appInfo.Densities.Add(ParsePNodeString(item));
}
appInfo.Densities.Add(PList.ParsePNodeString(item));
}
}

node.TryGetValue("CFBundleIconFile", out PNode iconsNode);
string appIconName = ParsePNodeString(iconsNode);
string resourcePath = Path.Combine(appPath, "Contents", "Resources");
if (Directory.Exists(resourcePath))
{
node.TryGetValue("CFBundleIconFile", out object? iconsNode);
string appIconName = PList.ParsePNodeString(iconsNode);
string resourcePath = Path.Combine(appPath, "Contents", "Resources");
if (Directory.Exists(resourcePath))
{

foreach (var resourceInfo in Directory.EnumerateFiles(resourcePath, appIconName + "*"))
foreach (var resourceInfo in Directory.EnumerateFiles(resourcePath, appIconName + "*"))
{
try
{
try
{
var iconBitmap = GetIcnsMax(resourceInfo);
if (iconBitmap != null)
{
using MemoryStream ms = new();
iconBitmap.Save(ms, ImageFormat.Png);
appIcon = ms.ToArray();
break;
}
}
catch (Exception)
var iconBitmap = GetIcnsMax(resourceInfo);
if (iconBitmap != null)
{
using MemoryStream ms = new();
iconBitmap.Save(ms, ImageFormat.Png);
appIcon = ms.ToArray();
break;
}
}
catch (Exception)
{
}
}
}
return (appInfo, fileSize, appIcon);
Expand Down Expand Up @@ -234,23 +230,23 @@ private long GetDirectorySize(DirectoryInfo dirInfo)
}
using (MemoryStream stream = new(plistBytes))
{
PListNet.Nodes.DictionaryNode node = (PListNet.Nodes.DictionaryNode)PList.Load(stream);
node.TryGetValue("CFBundleDisplayName", out PNode nameNode);
ipaInfo.Label = ParsePNodeString(nameNode);
node.TryGetValue("CFBundleIdentifier", out PNode packageNode);
ipaInfo.PackageName = ParsePNodeString(packageNode);
node.TryGetValue("CFBundleShortVersionString", out PNode versionNode);
ipaInfo.VersionName = ParsePNodeString(versionNode);
node.TryGetValue("MinimumOSVersion", out PNode minOSNode);
ipaInfo.MinSdkVersion = "iOS " + ParsePNodeString(minOSNode);
node.TryGetValue("DTPlatformVersion", out PNode targetOSNode);
ipaInfo.TargetSdkVersion = "iOS " + ParsePNodeString(targetOSNode);
node.TryGetValue("UIDeviceFamily", out PNode familyNode);
if (familyNode != null && familyNode.GetType() == typeof(PListNet.Nodes.ArrayNode))
Dictionary<string, object> node = (Dictionary<string, object>)PList.ReadPlist(stream, PlistType.Auto);
node.TryGetValue("CFBundleDisplayName", out object? nameNode);
ipaInfo.Label = PList.ParsePNodeString(nameNode);
node.TryGetValue("CFBundleIdentifier", out object? packageNode);
ipaInfo.PackageName = PList.ParsePNodeString(packageNode);
node.TryGetValue("CFBundleShortVersionString", out object? versionNode);
ipaInfo.VersionName = PList.ParsePNodeString(versionNode);
node.TryGetValue("MinimumOSVersion", out object? minOSNode);
ipaInfo.MinSdkVersion = "iOS " + PList.ParsePNodeString(minOSNode);
node.TryGetValue("DTPlatformVersion", out object? targetOSNode);
ipaInfo.TargetSdkVersion = "iOS " + PList.ParsePNodeString(targetOSNode);
node.TryGetValue("UIDeviceFamily", out object? familyNode);
if (familyNode != null && familyNode.GetType() == typeof(List<object>))
{
foreach (var item in ((PListNet.Nodes.ArrayNode)familyNode))
foreach (var item in ((List<object>)familyNode))
{
var deviceFamily = ParsePNodeString(item);
var deviceFamily = PList.ParsePNodeString(item);
iosDeviceFamily.TryGetValue(deviceFamily, out string? deviceFamilyDesc);
if (!string.IsNullOrWhiteSpace(deviceFamilyDesc))
{
Expand All @@ -259,16 +255,16 @@ private long GetDirectorySize(DirectoryInfo dirInfo)
}
}

node.TryGetValue("CFBundleIcons", out PNode iconsNode);
if (iconsNode != null && iconsNode.GetType() == typeof(PListNet.Nodes.DictionaryNode))
node.TryGetValue("CFBundleIcons", out object? iconsNode);
if (iconsNode != null && iconsNode.GetType() == typeof(Dictionary<string, object>))
{
((PListNet.Nodes.DictionaryNode)iconsNode).TryGetValue("CFBundlePrimaryIcon", out PNode primaryIconNode);
if (primaryIconNode != null && primaryIconNode.GetType() == typeof(PListNet.Nodes.DictionaryNode))
((Dictionary<string, object>)iconsNode).TryGetValue("CFBundlePrimaryIcon", out object? primaryIconNode);
if (primaryIconNode != null && primaryIconNode.GetType() == typeof(Dictionary<string, object>))
{
((PListNet.Nodes.DictionaryNode)primaryIconNode).TryGetValue("CFBundleIconFiles", out PNode iconFilesNode);
((Dictionary<string, object>)primaryIconNode).TryGetValue("CFBundleIconFiles", out object? iconFilesNode);
if (iconFilesNode != null)
{
iconName = ParsePNodeString(iconFilesNode, arrayLast: true);
iconName = PList.ParsePNodeString(iconFilesNode, arrayLast: true);
}
}
}
Expand Down Expand Up @@ -347,75 +343,7 @@ private long GetDirectorySize(DirectoryInfo dirInfo)

public static Bitmap? GetIcnsMax(string icnsPath)
{
return IcnsImageParser.GetImages(icnsPath).OrderByDescending(m => m.Bitmap.Height).FirstOrDefault()?.Bitmap;
}

private static string ParsePNodeString(PNode node, bool arrayFirst = false, bool arrayLast = false)
{
if (node == null) return string.Empty;
var nodeType = node.GetType();
if (nodeType == typeof(PListNet.Nodes.DictionaryNode))
{
PListNet.Nodes.DictionaryNode value = (PListNet.Nodes.DictionaryNode)node;
return string.Join(", ", value.Values.Select(m => ParsePNodeString(m)));
}
else if (nodeType == typeof(PListNet.Nodes.BooleanNode))
{
PListNet.Nodes.BooleanNode value = (PListNet.Nodes.BooleanNode)node;
return value.Value.ToString();
}
else if (nodeType == typeof(PListNet.Nodes.ArrayNode))
{
PListNet.Nodes.ArrayNode value = (PListNet.Nodes.ArrayNode)node;
if (arrayFirst && value.Count > 0)
{
return ParsePNodeString(value[0]);
}
else if (arrayLast && value.Count > 0)
{
return ParsePNodeString(value[^1]);
}
return string.Join(", ", value.Select(m => ParsePNodeString(m)));
}
else if (nodeType == typeof(PListNet.Nodes.DataNode))
{
PListNet.Nodes.DataNode value = (PListNet.Nodes.DataNode)node;
return Convert.ToBase64String(value.Value);
}
else if (nodeType == typeof(PListNet.Nodes.DateNode))
{
PListNet.Nodes.DateNode value = (PListNet.Nodes.DateNode)node;
return value.Value.ToString();
}
else if (nodeType == typeof(PListNet.Nodes.FillNode))
{
PListNet.Nodes.FillNode value = (PListNet.Nodes.FillNode)node;
return value?.ToString() ?? string.Empty;
}
else if (nodeType == typeof(PListNet.Nodes.IntegerNode))
{
PListNet.Nodes.IntegerNode value = (PListNet.Nodes.IntegerNode)node;
return value.Value.ToString();
}
else if (nodeType == typeof(PListNet.Nodes.RealNode))
{
PListNet.Nodes.RealNode value = (PListNet.Nodes.RealNode)node;
return value.Value.ToString();
}
else if (nodeType == typeof(PListNet.Nodes.StringNode))
{
PListNet.Nodes.StringNode value = (PListNet.Nodes.StringNode)node;
return value.Value.ToString();
}
else if (nodeType == typeof(PListNet.Nodes.UidNode))
{
PListNet.Nodes.UidNode value = (PListNet.Nodes.UidNode)node;
return value.Value.ToString();
}
else
{
return node?.ToString() ?? string.Empty;
}
return IcnsImageParser.GetImage(icnsPath).Bitmap;
}

class MyApkInfoHandler : IApkInfoHandler<ApkInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ApkReader" Version="2.0.1.1">
<PrivateAssets>All</PrivateAssets>
<Private>true</Private>
</PackageReference>
<PackageReference Include="PListNet" Version="3.2.0" Private="All" />
<PackageReference Include="ApkReader" Version="2.0.1.1" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
Expand All @@ -39,9 +35,4 @@
</EmbeddedResource>
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Copy SourceFiles="$(USERPROFILE)\.nuget\packages\apkreader\2.0.1.1\lib\netstandard2.0\ApkReader.dll" DestinationFolder="$(OutputPath)" />
<Copy SourceFiles="$(USERPROFILE)\.nuget\packages\plistnet\3.2.0\lib\netstandard1.0\PListNet.dll" DestinationFolder="$(OutputPath)" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class PluginBrowser : IPlugin

public PluginBrowser()
{
WebView2.WebView2.LoadWebView2();
}

public UserControl GetUserControl(IManager manager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@
</EmbeddedResource>
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Copy SourceFiles="$(USERPROFILE)\.nuget\packages\sharpcompress\0.33.0\lib\net6.0\SharpCompress.dll" DestinationFolder="$(OutputPath)" />
</Target>

</Project>
Loading

0 comments on commit 1837f21

Please sign in to comment.