Skip to content

Commit

Permalink
Merge pull request #2 from dhinakg/master
Browse files Browse the repository at this point in the history
Add cursed Mac model support
  • Loading branch information
ThatStella7922 authored Aug 1, 2023
2 parents 5fd9b6e + c70df08 commit 38621c7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
4 changes: 4 additions & 0 deletions dcbattwebhook-swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
5191FA032A77845D00463CF3 /* dcbattwebhook-swift-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "dcbattwebhook-swift-Bridging-Header.h"; sourceTree = "<group>"; };
550348C4291D7FB9006551B0 /* dcbattwebhook-swift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "dcbattwebhook-swift.app"; sourceTree = BUILT_PRODUCTS_DIR; };
550348C7291D7FB9006551B0 /* dcbattwebhook_swiftApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = dcbattwebhook_swiftApp.swift; sourceTree = "<group>"; };
550348C9291D7FB9006551B0 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -97,6 +98,7 @@
550348CB291D7FB9006551B0 /* Assets.xcassets */,
550348CD291D7FB9006551B0 /* dcbattwebhook_swift.entitlements */,
550348CE291D7FB9006551B0 /* Preview Content */,
5191FA032A77845D00463CF3 /* dcbattwebhook-swift-Bridging-Header.h */,
);
path = "dcbattwebhook-swift";
sourceTree = "<group>";
Expand Down Expand Up @@ -348,6 +350,7 @@
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_OBJC_BRIDGING_HEADER = "dcbattwebhook-swift/dcbattwebhook-swift-Bridging-Header.h";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,3";
TVOS_DEPLOYMENT_TARGET = 15.0;
Expand Down Expand Up @@ -394,6 +397,7 @@
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_OBJC_BRIDGING_HEADER = "dcbattwebhook-swift/dcbattwebhook-swift-Bridging-Header.h";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,3";
TVOS_DEPLOYMENT_TARGET = 15.0;
Expand Down
56 changes: 50 additions & 6 deletions dcbattwebhook-swift/DeviceInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,57 @@ public extension UIDevice {
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}

#if os(macOS) || targetEnvironment(macCatalyst)
#if arch(x86_64)

// it works in rosetta

// If this doesn't work we can use cachedSerial from the defaults instead
// but this seems to work
// Partially stolen from https://stackoverflow.com/a/57820994
var serialNumber: String? {
let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"))
if (service == 0) { return nil }
defer { IOObjectRelease(service) }
return IORegistryEntryCreateCFProperty(service, "IOPlatformSerialNumber" as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String
}

var modelName: String? {
guard let serial = serialNumber,
let defaults = UserDefaults.init(suiteName: "com.apple.SystemProfiler"),
let regionCode = Locale.current.regionCode,
let names = defaults.object(forKey: "CPU Names") as? [String: String],
!names.isEmpty else {
return nil
}
for language in Locale.preferredLanguages {
var key = "\(serial.suffix(4))-\(language)_\(regionCode)"
if let entry = names[key] {
return entry
}
key = "\(serial.suffix(3))-\(language)_\(regionCode)"
if let entry = names[key] {
return entry
}
}
return nil
}

return modelName ?? "Unknown: \(identifier)"


#else
let entry = IORegistryEntryFromPath(kIOMainPortDefault,"IOService:/AppleARMPE/product")
defer { IOObjectRelease(entry) }
let deviceName = IORegistryEntryCreateCFProperty(entry, "product-name" as CFString, kCFAllocatorDefault, 0)
if (deviceName == nil) {
return "Unknown: \(identifier)"
}
return String(decoding: deviceName!.takeUnretainedValue() as! Data, as: UTF8.self)
#endif
#endif

func mapToDevice(identifier: String) -> String { // swiftlint:disable:this cyclomatic_complexity
#if os(iOS)
switch identifier {
Expand Down Expand Up @@ -159,13 +209,7 @@ func isiOSPre16() -> Bool {

/// Returns the device name as a string (ex. `iPad Pro (12.9-inch) (2nd generation)`)
func getDeviceModel() -> String {
#if targetEnvironment(macCatalyst)
return "Mac - Stella is lazy and this is Catalyst"

#else
return UIDevice.modelName

#endif
}

/**
Expand Down
2 changes: 1 addition & 1 deletion dcbattwebhook-swift/SendBatteryInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func sendInfo(isCurrentlyCharging: Bool, didGetPluggedIn: Bool, didGetUnplugged:
print("broken ahh json: " + jsonString!)

// our actual post
let webhookURL = URL(string: userwebhookurl)! // grab the url from user settings
let webhookURL = URL(string: userwebhookurl.trimmingCharacters(in: .whitespacesAndNewlines))! // grab the url from user settings
var request = URLRequest(url: webhookURL) // create a urlrequest object with the grabbed url as the url
request.httpMethod = "POST" // make it a POST
request.addValue("application/json", forHTTPHeaderField: "content-type") // make sure its json
Expand Down
5 changes: 5 additions & 0 deletions dcbattwebhook-swift/dcbattwebhook-swift-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import <IOKit/IOKitLib.h>
4 changes: 4 additions & 0 deletions dcbattwebhook-swift/dcbattwebhook_swift.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.temporary-exception.shared-preference.read-only</key>
<array>
<string>com.apple.SystemProfiler</string>
</array>
</dict>
</plist>

0 comments on commit 38621c7

Please sign in to comment.