Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added French localization and Swiftify code #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Silicon.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
0586D6202566CED200CBBD48 /* DropView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropView.swift; sourceTree = "<group>"; };
05F025522575FF1200ADB3EA /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MainWindowController.strings"; sourceTree = "<group>"; };
05F025562575FF1A00ADB3EA /* zh-HK */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-HK"; path = "zh-HK.lproj/MainWindowController.strings"; sourceTree = "<group>"; };
A0765F9625763C1000A333A1 /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/MainWindowController.strings; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -380,6 +381,7 @@
Base,
"zh-Hans",
"zh-HK",
fr,
);
mainGroup = 0586D53E256696D900CBBD48;
productRefGroup = 0586D548256696D900CBBD48 /* Products */;
Expand Down Expand Up @@ -499,6 +501,7 @@
children = (
05F025522575FF1200ADB3EA /* zh-Hans */,
05F025562575FF1A00ADB3EA /* zh-HK */,
A0765F9625763C1000A333A1 /* fr */,
);
name = MainWindowController.strings;
sourceTree = "<group>";
Expand Down
65 changes: 2 additions & 63 deletions Silicon/Classes/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import Cocoa
@objc public private( set ) var path: String
@objc public private( set ) var version: String?
@objc public private( set ) var icon: NSImage?
@objc public private( set ) var architectures: [ String ]
@objc public private( set ) var isAppleSiliconReady: Bool
@objc public private( set ) var architecture: String
@objc public private( set ) var bundleID: String?
Expand Down Expand Up @@ -88,69 +87,9 @@ import Cocoa
self.name = FileManager.default.displayName( atPath: path )
self.path = path
self.icon = NSWorkspace.shared.icon( forFile: path )
self.architectures = macho.architectures

if( macho.architectures.count == 1 )
{
if( macho.architectures.contains( "arm64" ) )
{
self.isAppleSiliconReady = true
self.architecture = "Apple"
}
else if( macho.architectures.contains( "x86_64" ) )
{
self.isAppleSiliconReady = false
self.architecture = "Intel 64"
}
else if( macho.architectures.contains( "i386" ) )
{
self.isAppleSiliconReady = false
self.architecture = "Intel 32"
}
else if( macho.architectures.contains( "ppc" ) )
{
self.isAppleSiliconReady = false
self.architecture = "PowerPC"
}
else
{
self.isAppleSiliconReady = false
self.architecture = "Unknown"
}
}
else
{
if( macho.architectures.contains( "arm64" ) )
{
self.isAppleSiliconReady = true
self.architecture = "Universal"
}
else if( macho.architectures.contains( "ppc" ) && macho.architectures.contains( "i386" ) && macho.architectures.contains( "x86_64" ) )
{
self.isAppleSiliconReady = false
self.architecture = "PowerPC/Intel 32/64"
}
else if( macho.architectures.contains( "ppc" ) && macho.architectures.contains( "x86_64" ) )
{
self.isAppleSiliconReady = false
self.architecture = "PowerPC/Intel 64"
}
else if( macho.architectures.contains( "ppc" ) && macho.architectures.contains( "i386" ) )
{
self.isAppleSiliconReady = false
self.architecture = "PowerPC/Intel 32"
}
else if( macho.architectures.contains( "i386" ) && macho.architectures.contains( "x86_64" ) )
{
self.isAppleSiliconReady = false
self.architecture = "Intel 32/64"
}
else
{
self.isAppleSiliconReady = false
self.architecture = "Unknown"
}
}
self.isAppleSiliconReady = macho.isAppleSiliconReady
self.architecture = macho.architecturesName
}

@IBAction public func showInFinder( _ sender: Any? )
Expand Down
254 changes: 188 additions & 66 deletions Silicon/Classes/MachOFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,88 +26,210 @@ import Foundation

public class MachOFile
{
public private( set ) var architectures: [ String ] = []
public enum Architecture: String {
case i386
case x86_64
case arm
case arm64
case ppc
case unknown = "<unknown>"
}

public private( set ) var architectures: [ Architecture ] = []

public init?( path: String )
{
do
{
let stream = try BinaryStream( path: path )
let magic = try stream.readBigEndianUnsignedInteger()

if magic == 0xCAFEBABE
{
let count = try stream.readBigEndianUnsignedInteger()

for _ in 0 ..< count
{
let cpu = try stream.readBigEndianUnsignedInteger()
let _ = try stream.readBigEndianUnsignedInteger()
let _ = try stream.readBigEndianUnsignedInteger()
let _ = try stream.readBigEndianUnsignedInteger()
let _ = try stream.readBigEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
}
}
else if magic == 0xCEFAEDFE
{
let cpu = try stream.readLittleEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
}
else if magic == 0xFEEDFACE
{
let cpu = try stream.readBigEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
}
else if magic == 0xCFFAEDFE
{
let cpu = try stream.readLittleEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
}
else if magic == 0xFEEDFACF
{
let cpu = try stream.readBigEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
}
else
{
return nil
}
try setupArchitectures(path: path)
}
catch
{
return nil
}
}
}


// MARK: - Helpers

extension MachOFile {

public static func cpuToArch( type: UInt32 ) -> String
{
if type == 7
{
return "i386"
public var isAppleSiliconReady: Bool {
includesAppleArchitecture
}

public var architecturesName: String {
[
uniqueArchitectureName,
universalName,
legacyArchitecturesNames
].compactMap { $0 }.first ?? "Unknown"
}
}

extension MachOFile.Architecture {

public var name: String {
switch self {
case .arm64:
return "Apple"
case .x86_64:
return "Intel 64"
case .i386:
return "Intel 32"
case .ppc:
return "PowerPC"
default:
return "Unknown"
}
else if type == 7 | 0x01000000
{
return "x86_64"
}

public var isApple: Bool {
self == .arm64
}

public var isIntel32: Bool {
self == .i386
}

public var isIntel64: Bool {
self == .x86_64
}

public var isIntel: Bool {
isIntel32 || isIntel64
}

public var isPPC: Bool {
self == .ppc
}
}

// MARK: - Private

extension MachOFile {

private enum Error: Swift.Error {
case failedToReadArchitectures
}

private var isUniqueArchitecture: Bool {
architectures.count == 1
}

private var includesAppleArchitecture: Bool {
architectures.filter({ $0.isApple }).count > 0
}

private var includesIntelArchitecture: Bool {
architectures.filter({ $0.isIntel }).count > 0
}

private var includesIntel32Architecture: Bool {
architectures.filter({ $0.isIntel32 }).count > 0
}

private var includesIntel64Architecture: Bool {
architectures.filter({ $0.isIntel64 }).count > 0
}

private var includesPPCArchitecture: Bool {
architectures.filter({ $0.isPPC }).count > 0
}

private var includesLegacyArchitectures: Bool {
includesPPCArchitecture || includesIntelArchitecture
}

private var universalName: String? {
includesAppleArchitecture ? "Universal" : nil
}

private var ppcName: String? {
includesPPCArchitecture ? "PowerPC" : nil
}

private var intelName: String? {
if includesIntel32Architecture && includesIntel64Architecture {
return "Intel 32/64"
} else if includesIntel32Architecture {
return "Intel 32"
} else if includesIntel64Architecture {
return "Intel 64"
} else {
return nil
}
else if type == 12
{
return "arm"
}

private var legacyArchitecturesNames: String? {
if !includesAppleArchitecture && includesLegacyArchitectures {
return [ppcName, intelName]
.compactMap { $0 }
.joined(separator: "/")
} else {
return nil
}
else if type == 12 | 0x01000000
{
return "arm64"
}

private var uniqueArchitectureName: String? {
isUniqueArchitecture ? architectures.first?.name : nil
}

private func setupArchitectures ( path: String ) throws {
let stream = try BinaryStream( path: path )
let magic = try stream.readBigEndianUnsignedInteger()

switch magic {
case 0xCAFEBABE:
let count = try stream.readBigEndianUnsignedInteger()

for _ in 0 ..< count
{
let cpu = try stream.readBigEndianUnsignedInteger()
let _ = try stream.readBigEndianUnsignedInteger()
let _ = try stream.readBigEndianUnsignedInteger()
let _ = try stream.readBigEndianUnsignedInteger()
let _ = try stream.readBigEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
}
case 0xCEFAEDFE:
let cpu = try stream.readLittleEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
case 0xFEEDFACE:
let cpu = try stream.readBigEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
case 0xCFFAEDFE:
let cpu = try stream.readLittleEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
case 0xFEEDFACF:
let cpu = try stream.readBigEndianUnsignedInteger()

self.architectures.append( MachOFile.cpuToArch( type: cpu ) )
default:
throw Error.failedToReadArchitectures
}
else if type == 18
{
return "ppc"
}

private static func cpuToArch( type: UInt32 ) -> Architecture
{
switch type {
case 7:
return .i386
case 7 | 0x01000000:
return .x86_64
case 12:
return .arm
case 12 | 0x01000000:
return .arm64
case 18:
return .ppc
default:
return .unknown
}

return "<unknown>"
}
}
2 changes: 1 addition & 1 deletion Silicon/Classes/MainWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class MainWindowController: NSWindowController
return true
}

if app.architectures.contains( "arm64" )
if app.isAppleSiliconReady
{
let alert = NSAlert()

Expand Down
Loading