Skip to content
Merged
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
11 changes: 10 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import PackageDescription
let package = Package(
name: "xload",
platforms: [
.macOS(.v14),
.iOS(.v17),
.macOS(.v15),
.iOS(.v18),
],
products: [
.library(
Expand All @@ -25,6 +25,10 @@ let package = Package(
branch: "debug-trait",
traits: [], // disable DEBUG_ONLY
),
.package(
url: "https://github.com/kabiroberai/ellekit",
branch: "swiftpm-fixes",
),
],
targets: [
.target(
Expand All @@ -35,6 +39,7 @@ let package = Package(
dependencies: [
"CXLoad",
.product(name: "InjectionImpl", package: "InjectionLite"),
.product(name: "ellekit", package: "ellekit"),
]
),
],
Expand Down
7 changes: 7 additions & 0 deletions Sources/CXLoad/include/CXLoad.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#ifndef CXLoad_h
#define CXLoad_h

#include <stdint.h>

typedef struct {
const void *value;
uint8_t state;
} AGValue;

void xload_load();

#endif
63 changes: 63 additions & 0 deletions Sources/XLoad/InjectionWatcher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Foundation
import CXLoad
import Synchronization
import ellekit

enum SwiftUIInterceptor {
fileprivate typealias CGetValue = @convention(c) (UInt32, UInt32, UnsafeRawPointer?) -> AGValue

static func install() {
let target = dlsym(dlopen(nil, 0), "AGGraphGetValue")!
let hooked = unsafeBitCast(hookedGetValue as CGetValue, to: UnsafeMutableRawPointer.self)

_orig.withLock {
// we have to hook inside the lock otherwise there's
// a brief period between when we install the hook and
// when we set orig. if someone calls AGGraphGetValue during that time,
// we'll crash.
let res = ellekit::hook(target, hooked)
$0 = unsafeBitCast(res, to: CGetValue.self)
}
}

fileprivate static let _orig = Mutex<CGetValue?>(nil)
// "upgrades" the mutex to an atomic. safe because we never read it before it's set.
fileprivate static let orig = SwiftUIInterceptor._orig.withLock(\.self)!
}

@c private func hookedGetValue(
_ attribute: UInt32,
_ options: UInt32,
_ type: UnsafeRawPointer?,
) -> AGValue {
// AGGraphGetValue is called on basically every view render (afaict; I hope).
// by registering an Observation access in this method, we make it so any time
// we call notify(), it triggers an update on every SwiftUI view in the app.
InjectionWatcher.shared.subscribe()
let origFunc = SwiftUIInterceptor.orig
return origFunc(attribute, options, type)
}

private struct InjectionWatcher: Observable, Sendable {
private let registrar = ObservationRegistrar()

static let shared = InjectionWatcher()

private init() {
Task { [self] in await watch() }
}

private func watch() async {
for await _ in NotificationCenter.default.notifications(named: .init("INJECTION_BUNDLE_NOTIFICATION")) {
notify()
}
}

private func notify() {
registrar.withMutation(of: self, keyPath: \.self) {}
}

func subscribe() {
registrar.access(self, keyPath: \.self)
}
}
18 changes: 15 additions & 3 deletions Sources/XLoad/XLoad.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@ import System
}

private func load() async throws {
guard let watchPath = ProcessInfo.processInfo.environment["XLOAD_WATCH_DIR"] else {
throw StringError("Did not receive XLOAD_WATCH_DIR")
}
loadInterceptor()
try await loadWatcher()
}

private func loadInterceptor() {
guard ProcessInfo.processInfo.environment["XLOAD_INTERCEPT"] == "1" else { return }
print("[XLoad] Loading interceptor...")
SwiftUIInterceptor.install()
}

private func loadWatcher() async throws {
guard let watchPath = ProcessInfo.processInfo.environment["XLOAD_WATCH_DIR"]
else { return }

print("[XLoad] Loading watcher...")

let changes = try await watchDirectory(watchPath)

Expand Down