Support .dmg
Some checks are pending
MSBuild / build (push) Waiting to run
MSBuild / publish (push) Blocked by required conditions

This commit is contained in:
ema
2025-06-15 07:04:00 +08:00
parent 679b00fe71
commit c9fa5ff43b
10 changed files with 806 additions and 1 deletions

View File

@@ -0,0 +1,51 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using DiscUtils.HfsPlus;
using System;
using System.IO;
namespace QuickLook.Plugin.AppViewer.PackageParsers.Dmg;
public class DmgArchive : IDisposable
{
public string Entry { get; set; }
public HfsPlusFileSystem FileSystem { get; set; }
public void Dispose()
{
FileSystem?.Dispose();
FileSystem = null;
}
public byte[] GetBytes()
{
if (Entry is null)
return null;
if (FileSystem is null)
return null;
using var stream = FileSystem.OpenFile(Entry, FileMode.Open, FileAccess.Read);
using var ms = new MemoryStream();
stream.CopyTo(ms);
byte[] fileBytes = ms.ToArray();
return fileBytes;
}
}

View File

@@ -0,0 +1,43 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Drawing;
namespace QuickLook.Plugin.AppViewer.PackageParsers.Dmg;
public class DmgInfo
{
public string DisplayName { get; set; }
public string VersionName { get; set; }
public string VersionCode { get; set; }
public string Identifier { get; set; }
public string MinimumOSVersion { get; set; }
public string PlatformVersion { get; set; }
public string SupportedPlatforms { get; set; }
public string[] Permissions { get; set; } = [];
public Bitmap Logo { get; set; }
public bool HasIcon => Logo is not null;
}

View File

@@ -0,0 +1,41 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Linq;
namespace QuickLook.Plugin.AppViewer.PackageParsers.Dmg;
public static class DmgParser
{
public static DmgInfo Parse(string path)
{
using DmgReader reader = new(path);
return new DmgInfo()
{
DisplayName = reader.DisplayName,
VersionName = reader.ShortVersionString,
VersionCode = reader.Version,
Identifier = reader.Identifier,
MinimumOSVersion = reader.MinimumOSVersion,
SupportedPlatforms = reader.SupportedPlatforms,
PlatformVersion = reader.PlatformVersion,
Permissions = [.. reader.InfoPlistDict.Keys.Where(key => key.StartsWith("NS") && key.EndsWith("UsageDescription"))],
Logo = reader.Logo,
};
}
}

View File

@@ -0,0 +1,216 @@
// Copyright © 2017-2025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using DiscUtils;
using DiscUtils.HfsPlus;
using QuickLook.Plugin.AppViewer.PackageParsers.Ipa;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
namespace QuickLook.Plugin.AppViewer.PackageParsers.Dmg;
public class DmgReader : IDisposable
{
public string VolumeLabel { get; set; }
public string ContentsEntry { get; set; }
public Dictionary<string, DmgArchive> Archives { get; } = [];
public Dictionary<string, object> InfoPlistDict { get; set; }
public string DisplayName { get; set; }
public string ShortVersionString { get; set; }
public string Version { get; set; }
public string Identifier { get; set; }
public byte[] Icon { get; set; }
public string IconName { get; set; }
public string IconEntry { get; set; }
public Bitmap Logo { get; set; }
public string MinimumOSVersion { get; set; }
public string PlatformVersion { get; set; }
public string SupportedPlatforms { get; set; }
static DmgReader()
{
DiscUtils.Complete.SetupHelper.SetupComplete();
}
public DmgReader(string path)
{
Open(path);
}
public void Dispose()
{
if (Archives is not null)
{
foreach (var archive in Archives.Values)
{
archive.Dispose();
}
Archives.Clear();
}
}
private void Open(string path)
{
using var disk = VirtualDisk.OpenDisk(path, FileAccess.Read, useAsync: false);
if (disk is null)
{
Debug.WriteLine($"Failed to open '{path}' as virtual disk.");
return;
}
// Find the first (and supposedly, only, HFS partition)
foreach (var volume in VolumeManager.GetPhysicalVolumes(disk))
{
foreach (var fileSystem in FileSystemManager.DetectFileSystems(volume))
{
// Apple HFS+
if (fileSystem.Name == "HFS+")
{
using var hfs = (HfsPlusFileSystem)fileSystem.Open(volume);
VolumeLabel = hfs.VolumeLabel;
ListFiles(hfs, string.Empty);
}
}
}
byte[] infoPlistData = null;
foreach (var archive in Archives.Values)
{
Match m = Regex.Match(archive.Entry, @".*\.app\\Contents\\Info\.plist$");
if (m.Success)
{
ContentsEntry = Path.GetDirectoryName(archive.Entry);
infoPlistData = archive.GetBytes();
if (Plist.ReadPlist(infoPlistData) is Dictionary<string, object> dict)
{
InfoPlistDict = dict;
}
break;
}
}
{
if (InfoPlistDict.TryGetValue("CFBundleDisplayName", out object value) && value is string stringValue)
{
DisplayName = stringValue;
}
}
{
if (InfoPlistDict.TryGetValue("CFBundleShortVersionString", out object value) && value is string stringValue)
{
ShortVersionString = stringValue;
}
}
{
if (InfoPlistDict.TryGetValue("CFBundleVersion", out object value) && value is string stringValue)
{
Version = stringValue;
}
}
{
if (InfoPlistDict.TryGetValue("CFBundleIdentifier", out object value) && value is string stringValue)
{
Identifier = stringValue;
}
}
{
if (InfoPlistDict.TryGetValue("LSMinimumSystemVersion", out object value) && value is string stringValue)
{
MinimumOSVersion = $"macOS {stringValue}";
}
}
{
if (InfoPlistDict.TryGetValue("DTPlatformVersion", out object value) && value is string stringValue)
{
PlatformVersion = $"macOS {stringValue}";
}
}
{
if (InfoPlistDict.TryGetValue("CFBundleSupportedPlatforms", out object familyNode) && familyNode is IEnumerable<object> list)
{
SupportedPlatforms = string.Join(", ", list);
}
}
{
if (InfoPlistDict.TryGetValue("CFBundleIconFile", out object iconFilesNode) && iconFilesNode is object iconFile)
{
IconName = iconFile as string;
}
}
{
if (!string.IsNullOrWhiteSpace(IconName))
{
foreach (var archive in Archives.Values)
{
if (archive.Entry.StartsWith($@"{ContentsEntry}\Resources\{IconName}."))
{
IconEntry = archive.Entry;
Icon = archive.GetBytes();
if (Path.GetExtension(IconEntry).ToLower() == ".icns")
{
Logo = IcnsParser.Parse(Icon);
}
break;
}
}
}
}
}
private void ListFiles(HfsPlusFileSystem fs, string path)
{
foreach (var entry in fs.GetFileSystemEntries(path))
{
Debug.WriteLine(entry);
if (fs.DirectoryExists(entry))
{
ListFiles(fs, entry);
}
else if (fs.FileExists(entry))
{
Archives.Add(entry, new DmgArchive()
{
Entry = entry,
FileSystem = fs,
});
}
}
}
}

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
namespace QuickLook.Plugin.AppViewer.PackageParsers.Dmg;
public static class IcnsParser
{
public static Bitmap Parse(byte[] icnsBytes)
{
// Temporary method
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies()
.Where((assembly) => assembly.FullName.StartsWith("QuickLook.Plugin.ImageViewer"))
.FirstOrDefault();
if (assembly == null)
return null;
Type type = assembly.GetTypes()
.Where(type => type.Name.StartsWith("IcnsImageParser"))
.FirstOrDefault();
if (type == null)
return null;
MethodInfo method = type.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(method => method.Name == "GetImages"
&& method.GetParameters().FirstOrDefault()?.ParameterType == typeof(Stream))
.FirstOrDefault();
if (method == null)
return null;
using MemoryStream stream = new(icnsBytes);
dynamic[] images = method.Invoke(null, [stream]) as dynamic[];
List<Bitmap> bitmaps = [];
foreach (dynamic image in images)
{
if (image.GetType().GetProperty("Bitmap") is PropertyInfo property)
{
var bitmap = property.GetValue(image);
bitmaps.Add(bitmap);
}
}
Bitmap imageResult = bitmaps
.Where(bitmap => bitmap != null)
.OrderByDescending(bitmap => bitmap.Width)
.FirstOrDefault()
?.Clone() as Bitmap;
foreach (dynamic image in images)
{
if (image.GetType().GetProperty("Bitmap") is PropertyInfo property)
{
if (property.GetValue(image) is IDisposable disposable)
{
disposable.Dispose();
}
}
}
return imageResult;
}
}