fix resource loading on macOS

This commit is contained in:
Nick Fisher
2023-09-06 16:43:56 +08:00
parent c689bf2978
commit 238c4bed14

View File

@@ -13,11 +13,11 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
var pixelBufferAttrs = [ var pixelBufferAttrs = [
kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32ABGR ), kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32ABGR ),
// kCVPixelBufferOpenGLCompatibilityKey: kCFBooleanTrue, kCVPixelBufferOpenGLCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferIOSurfacePropertiesKey: [:] kCVPixelBufferIOSurfacePropertiesKey: [:]
] as CFDictionary ] as CFDictionary
var resources:NSMutableDictionary = [:] var resources:[UInt32:NSData] = [:]
var viewer:UnsafeRawPointer? = nil var viewer:UnsafeRawPointer? = nil
var displayLink:CVDisplayLink? = nil var displayLink:CVDisplayLink? = nil
@@ -31,101 +31,38 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
let instance:SwiftPolyvoxFilamentPlugin = Unmanaged<SwiftPolyvoxFilamentPlugin>.fromOpaque(resourcesPtr!).takeUnretainedValue() let instance:SwiftPolyvoxFilamentPlugin = Unmanaged<SwiftPolyvoxFilamentPlugin>.fromOpaque(resourcesPtr!).takeUnretainedValue()
let uriString = String(cString:uri!) var uriString = String(cString:uri!)
var path:String? = nil var path:String? = nil
// check for hot-reloaded asset if(uriString.hasPrefix("file://")) {
var found : URL? = nil path = String(uriString.dropFirst(7))
} else {
if(uriString.hasPrefix("asset://")) { if(uriString.hasPrefix("asset://")) {
let assetPath = String(uriString.dropFirst(8)) uriString = String(uriString.dropFirst(8))
print("Searching for hot reloaded asset under path : \(assetPath)")
let appFolder = Bundle.main.resourceURL
let dirPaths = NSSearchPathForDirectoriesInDomains(.applicationDirectory,
.userDomainMask, true)
let supportDirPaths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory,
.userDomainMask, true)
let devFsPath = URL(fileURLWithPath: supportDirPaths.first!, isDirectory:true).deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("tmp")
let orderedURLs = try? FileManager.default.enumerator(at: devFsPath, includingPropertiesForKeys: [ .pathKey, .creationDateKey], options: .skipsHiddenFiles)
for case let fileURL as URL in orderedURLs! {
if !(fileURL.path.hasSuffix(assetPath)) {
continue
}
print("Found hot reloaded asset : \(fileURL)")
if found == nil {
found = fileURL
} else {
do {
let c1 = try found!.resourceValues(forKeys: [.creationDateKey]).creationDate
let c2 = try fileURL.resourceValues(forKeys: [.creationDateKey]).creationDate
if c1! < c2! {
found = fileURL
print("\(fileURL) is newer, replacing")
} else {
print("Ignoring older asset")
}
} catch {
}
}
} }
let bundle = Bundle.init(identifier: "io.flutter.flutter.app")!
path = bundle.path(forResource:uriString, ofType: nil, inDirectory: "flutter_assets")
} }
do {
if let cd = try found?.resourceValues(forKeys:[.creationDateKey]).creationDate {
if cd > instance.createdAt {
print("Using hot reloaded asset : \(found)")
path = found!.path
}
}
} catch {
}
if path == nil {
if(uriString.hasPrefix("file://")) {
path = String(uriString.dropFirst(7))
} else if(uriString.hasPrefix("asset://")) {
let key = instance.registrar.lookupKey(forAsset:String(uriString.dropFirst(8)))
path = Bundle.main.path(forResource: key, ofType:nil)
guard path != nil else { if(path != nil) {
print("File not present in bundle : \(uri)") do {
return ResourceBuffer() let data = try Data(contentsOf: URL(fileURLWithPath:path!))
} let nsData = data as NSData
} else { let resId = UInt32(instance.resources.count)
let key = instance.registrar.lookupKey(forAsset:String(uriString)) instance.resources[resId] = nsData
let bundle = Bundle.init(identifier: "io.flutter.flutter.app")! let length = nsData.length
path = bundle.path(forResource:uriString, ofType: nil, inDirectory: "flutter_assets") return ResourceBuffer(data:nsData.bytes, size:UInt32(nsData.count), id:UInt32(resId))
// let path = bundle.path(forResource: "assets/materials.uberz", ofType: nil) } catch {
guard path != nil else { print("ERROR LOADING RESOURCE")
print("File not present in bundle : \(uriString)") }
return ResourceBuffer()
}
}
}
do {
print("Opening data from path \(path)")
let data = try Data(contentsOf: URL(fileURLWithPath:path!))
let resId = instance.resources.count
let nsData = data as NSData
instance.resources[resId] = nsData
let rawPtr = nsData.bytes
return ResourceBuffer(data:rawPtr, size:UInt32(nsData.count), id:UInt32(resId))
} catch {
print("Error opening file: \(error)")
} }
return ResourceBuffer() return ResourceBuffer()
} }
var freeResource : @convention(c) (ResourceBuffer,UnsafeMutableRawPointer?) -> () = { rbuf, resourcesPtr in var freeResource : @convention(c) (ResourceBuffer,UnsafeMutableRawPointer?) -> () = { rbuf, resourcesPtr in
let instance:SwiftPolyvoxFilamentPlugin = Unmanaged<SwiftPolyvoxFilamentPlugin>.fromOpaque(resourcesPtr!).takeUnretainedValue() let instance:SwiftPolyvoxFilamentPlugin = Unmanaged<SwiftPolyvoxFilamentPlugin>.fromOpaque(resourcesPtr!).takeUnretainedValue()
instance.resources.removeObject(forKey:rbuf.id) instance.resources.removeValue(forKey:rbuf.id)
} }
var displayLinkRenderCallback : @convention(c) (CVDisplayLink, UnsafePointer<CVTimeStamp>, UnsafePointer<CVTimeStamp>, CVOptionFlags, UnsafeMutablePointer<CVOptionFlags>, UnsafeMutableRawPointer?) -> CVReturn = { displayLink, ts1, ts2, options, optionsPtr, resourcesPtr in var displayLinkRenderCallback : @convention(c) (CVDisplayLink, UnsafePointer<CVTimeStamp>, UnsafePointer<CVTimeStamp>, CVOptionFlags, UnsafeMutablePointer<CVOptionFlags>, UnsafeMutableRawPointer?) -> CVReturn = { displayLink, ts1, ts2, options, optionsPtr, resourcesPtr in