From 53f77a29ea3fc5721de88eb4d122b5575f53de49 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Sun, 18 Apr 2021 08:51:31 -0700 Subject: [PATCH] Implement correctly loading from macos bundle resources --- build-packages.sh | 10 +++------- data/core/start.lua | 10 +++++++--- meson.build | 2 +- src/bundle_open.h | 5 ++--- src/bundle_open.m | 41 +++-------------------------------------- src/main.c | 12 +++++------- 6 files changed, 21 insertions(+), 59 deletions(-) diff --git a/build-packages.sh b/build-packages.sh index b286efd5..d90b22d2 100755 --- a/build-packages.sh +++ b/build-packages.sh @@ -91,13 +91,9 @@ lite_build_package_macosx () { local os="macosx" local appdir=".package-build/lite-xl.app" - mkdir -p "$appdir/Contents"/{MacOS,Resources} - local pdir="$appdir/Contents/MacOS" - - local bindir="$pdir" - local datadir="$pdir/data" - mkdir -p "$bindir" - mkdir -p "$datadir" + local bindir="$appdir/Contents/MacOS" + local datadir="$appdir/Contents/Resources" + mkdir -p "$bindir" "$datadir" for module_name in core plugins colors fonts; do copy_directory_from_repo --strip-components=1 "data/$module_name" "$datadir" done diff --git a/data/core/start.lua b/data/core/start.lua index 8d3a4e3a..494d943f 100644 --- a/data/core/start.lua +++ b/data/core/start.lua @@ -4,10 +4,14 @@ VERSION = "1.16.5" SCALE = tonumber(os.getenv("LITE_SCALE")) or SCALE PATHSEP = package.config:sub(1, 1) -EXEDIR = EXEFILE:match("^(.+)[/\\][^/\\]+$") -local prefix = EXEDIR:match("^(.+)[/\\]bin$") -DATADIR = prefix and (prefix .. '/share/lite-xl') or (EXEDIR .. '/data') +EXEDIR = EXEFILE:match("^(.+)[/\\][^/\\]+$") +if MACOS_RESOURCES then + DATADIR = MACOS_RESOURCES +else + local prefix = EXEDIR:match("^(.+)[/\\]bin$") + DATADIR = prefix and (prefix .. '/share/lite-xl') or (EXEDIR .. '/data') +end USERDIR = HOME and (HOME .. '/.config/lite-xl') or (EXEDIR .. '/user') package.path = DATADIR .. '/?.lua;' .. package.path diff --git a/meson.build b/meson.build index c1a996d4..e9beee87 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,4 @@ -project('lite', 'c', 'cpp', default_options : ['c_std=gnu11', 'cpp_std=c++03']) +project('lite', 'c', 'cpp', 'objc', default_options : ['c_std=gnu11', 'cpp_std=c++03']) cc = meson.get_compiler('c') libm = cc.find_library('m', required : false) diff --git a/src/bundle_open.h b/src/bundle_open.h index 4932075f..48a478c2 100644 --- a/src/bundle_open.h +++ b/src/bundle_open.h @@ -1,9 +1,8 @@ #ifndef BUNDLE_OPEN_H #define BUNDLE_OPEN_H -#include +#include "lua.h" -const char *resource_path_from_bundle(); -FILE* open_fp_from_bundle_or_fallback(const char *file, const char *mode); +void set_macos_bundle_resources(lua_State *L); #endif diff --git a/src/bundle_open.m b/src/bundle_open.m index 847c07dd..73ff9048 100644 --- a/src/bundle_open.m +++ b/src/bundle_open.m @@ -1,47 +1,12 @@ -#include -#include -#include - #import #include "bundle_open.h" -const char *resource_path_from_bundle() +void set_macos_bundle_resources(lua_State *L) { @autoreleasepool { - NSFileManager* file_manager = [NSFileManager defaultManager]; NSString* resource_path = [[NSBundle mainBundle] resourcePath]; - const char *resource_path_c = [resource_path UTF8String]; - size_t len = strlen(resource_path_c); - char *resource_path_s = malloc(len + 1); - if (resource_path_s) { - memcpy(resource_path_s, resource_path_c, len + 1); - } - return resource_path_s; -}} - -FILE* open_fp_from_bundle_or_fallback(const char *file, const char *mode) -{ @autoreleasepool -{ - FILE* fp = NULL; - - /* If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only. */ - if(strcmp("r", mode) && strcmp("rb", mode)) { - return fopen(file, mode); - } - - NSFileManager* file_manager = [NSFileManager defaultManager]; - NSString* resource_path = [[NSBundle mainBundle] resourcePath]; - - NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)]; - - NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component]; - if([file_manager fileExistsAtPath:full_path_with_file_to_try]) { - fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode); - } else { - fp = fopen(file, mode); - } - - return fp; + lua_pushstring(L, [resource_path UTF8String]); + lua_setglobal(L, "MACOS_RESOURCES"); }} diff --git a/src/main.c b/src/main.c index 3fe8743d..85b10deb 100644 --- a/src/main.c +++ b/src/main.c @@ -103,12 +103,6 @@ int main(int argc, char **argv) { SetProcessDPIAware(); #endif -#ifdef __APPLE__ - const char *resource_path = resource_path_from_bundle(); - fprintf(stderr, "resource path: %s\n", resource_path); - free(resource_path); -#endif - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); SDL_EnableScreenSaver(); SDL_EventState(SDL_DROPFILE, SDL_ENABLE); @@ -155,13 +149,17 @@ init_lua: lua_pushstring(L, exename); lua_setglobal(L, "EXEFILE"); +#ifdef __APPLE__ + set_macos_bundle_resources(L); +#endif + const char *init_lite_code = \ "local core\n" "xpcall(function()\n" " HOME = os.getenv('" LITE_OS_HOME "')\n" " local exedir = EXEFILE:match(\"^(.*)" LITE_PATHSEP_PATTERN LITE_NONPATHSEP_PATTERN "$\")\n" " local prefix = exedir:match(\"^(.*)" LITE_PATHSEP_PATTERN "bin$\")\n" - " dofile((prefix and prefix .. '/share/lite-xl' or exedir .. '/data') .. '/core/start.lua')\n" + " dofile((MACOS_RESOURCES or (prefix and prefix .. '/share/lite-xl' or exedir .. '/data')) .. '/core/start.lua')\n" " core = require('core')\n" " core.init()\n" " core.run()\n"