2019-08-20 13:34:29 +02:00
|
|
|
module darwin
|
|
|
|
|
|
|
|
#include <Cocoa/Cocoa.h>
|
2019-09-30 22:29:14 +02:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
|
2019-08-20 13:34:29 +02:00
|
|
|
#flag -framework Cocoa
|
2019-10-31 11:08:01 +01:00
|
|
|
#flag -framework Carbon
|
2019-08-20 13:34:29 +02:00
|
|
|
|
2021-03-08 16:18:26 +01:00
|
|
|
struct C.NSString {}
|
2019-08-20 13:34:29 +02:00
|
|
|
|
2021-04-19 18:01:47 +02:00
|
|
|
#include "@VEXEROOT/vlib/darwin/darwin.m"
|
2020-08-22 17:09:22 +02:00
|
|
|
|
|
|
|
fn C.nsstring2(s string) voidptr
|
|
|
|
|
2019-08-20 13:34:29 +02:00
|
|
|
// macOS and iOS helpers
|
2021-03-08 16:18:26 +01:00
|
|
|
// pub fn nsstring(s string) *C.NSString {
|
2019-10-31 22:57:16 +01:00
|
|
|
pub fn nsstring(s string) voidptr {
|
2020-08-22 17:09:22 +02:00
|
|
|
return C.nsstring2(s)
|
2019-08-20 13:34:29 +02:00
|
|
|
// println('ns $s len=$s.len')
|
2020-08-22 17:09:22 +02:00
|
|
|
//# return [ [ NSString alloc ] initWithBytesNoCopy:s.str length:s.len
|
|
|
|
//# encoding:NSUTF8StringEncoding freeWhenDone: false];
|
2021-03-08 16:18:26 +01:00
|
|
|
// return 0
|
2019-12-01 19:08:39 +01:00
|
|
|
|
2021-03-08 16:18:26 +01:00
|
|
|
// ns := C.alloc_NSString()
|
|
|
|
// return ns.initWithBytesNoCopy(s.str, length: s.len,
|
|
|
|
// encoding: NSUTF8StringEncoding, freeWhenDone: false)
|
2019-08-20 13:34:29 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 22:29:14 +02:00
|
|
|
// returns absolute path to folder where your resources should / will reside
|
|
|
|
// for .app packages: .../my.app/Contents/Resources
|
|
|
|
// for cli: .../parent_folder/Resources
|
2019-12-01 19:08:39 +01:00
|
|
|
|
2021-04-19 18:01:47 +02:00
|
|
|
fn C.CFBundleCopyResourcesDirectoryURL(bundle voidptr) &byte
|
2019-12-01 19:08:39 +01:00
|
|
|
fn C.CFBundleGetMainBundle() voidptr
|
2021-04-19 18:01:47 +02:00
|
|
|
fn C.CFURLGetFileSystemRepresentation(url &byte, resolve_against_base bool, buffer &byte, buffer_size int) int
|
|
|
|
fn C.CFRelease(url &byte)
|
2019-12-01 19:08:39 +01:00
|
|
|
|
2019-09-30 22:29:14 +02:00
|
|
|
pub fn resource_path() string {
|
|
|
|
main_bundle := C.CFBundleGetMainBundle()
|
|
|
|
resource_dir_url := C.CFBundleCopyResourcesDirectoryURL(main_bundle)
|
2020-03-29 10:08:42 +02:00
|
|
|
if isnil(resource_dir_url) {
|
2019-10-23 22:40:07 +02:00
|
|
|
panic('CFBundleCopyResourcesDirectoryURL failed')
|
|
|
|
}
|
2019-09-30 22:29:14 +02:00
|
|
|
buffer_size := 4096
|
2021-06-15 13:47:11 +02:00
|
|
|
mut buffer := unsafe { malloc_noscan(buffer_size) }
|
2021-04-19 18:01:47 +02:00
|
|
|
unsafe {
|
|
|
|
buffer[0] = 0
|
|
|
|
}
|
2021-03-08 16:18:26 +01:00
|
|
|
conv_result := C.CFURLGetFileSystemRepresentation(resource_dir_url, true, buffer,
|
|
|
|
buffer_size)
|
2020-05-05 20:07:43 +02:00
|
|
|
if conv_result == 0 {
|
2019-10-23 22:40:07 +02:00
|
|
|
panic('CFURLGetFileSystemRepresentation failed')
|
|
|
|
}
|
2020-08-10 18:05:26 +02:00
|
|
|
result := unsafe { buffer.vstring() }
|
2019-09-30 22:29:14 +02:00
|
|
|
C.CFRelease(resource_dir_url)
|
|
|
|
return result
|
|
|
|
}
|