Implement correctly loading from macos bundle resources

This commit is contained in:
Francesco Abbate 2021-04-18 08:51:31 -07:00
parent f913a8513f
commit 53f77a29ea
6 changed files with 21 additions and 59 deletions

View File

@ -91,13 +91,9 @@ lite_build_package_macosx () {
local os="macosx" local os="macosx"
local appdir=".package-build/lite-xl.app" local appdir=".package-build/lite-xl.app"
mkdir -p "$appdir/Contents"/{MacOS,Resources} local bindir="$appdir/Contents/MacOS"
local pdir="$appdir/Contents/MacOS" local datadir="$appdir/Contents/Resources"
mkdir -p "$bindir" "$datadir"
local bindir="$pdir"
local datadir="$pdir/data"
mkdir -p "$bindir"
mkdir -p "$datadir"
for module_name in core plugins colors fonts; do for module_name in core plugins colors fonts; do
copy_directory_from_repo --strip-components=1 "data/$module_name" "$datadir" copy_directory_from_repo --strip-components=1 "data/$module_name" "$datadir"
done done

View File

@ -4,10 +4,14 @@ VERSION = "1.16.5"
SCALE = tonumber(os.getenv("LITE_SCALE")) or SCALE SCALE = tonumber(os.getenv("LITE_SCALE")) or SCALE
PATHSEP = package.config:sub(1, 1) PATHSEP = package.config:sub(1, 1)
EXEDIR = EXEFILE:match("^(.+)[/\\][^/\\]+$")
local prefix = EXEDIR:match("^(.+)[/\\]bin$") EXEDIR = EXEFILE:match("^(.+)[/\\][^/\\]+$")
DATADIR = prefix and (prefix .. '/share/lite-xl') or (EXEDIR .. '/data') 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') USERDIR = HOME and (HOME .. '/.config/lite-xl') or (EXEDIR .. '/user')
package.path = DATADIR .. '/?.lua;' .. package.path package.path = DATADIR .. '/?.lua;' .. package.path

View File

@ -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') cc = meson.get_compiler('c')
libm = cc.find_library('m', required : false) libm = cc.find_library('m', required : false)

View File

@ -1,9 +1,8 @@
#ifndef BUNDLE_OPEN_H #ifndef BUNDLE_OPEN_H
#define BUNDLE_OPEN_H #define BUNDLE_OPEN_H
#include <stdio.h> #include "lua.h"
const char *resource_path_from_bundle(); void set_macos_bundle_resources(lua_State *L);
FILE* open_fp_from_bundle_or_fallback(const char *file, const char *mode);
#endif #endif

View File

@ -1,47 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include "bundle_open.h" #include "bundle_open.h"
const char *resource_path_from_bundle() void set_macos_bundle_resources(lua_State *L)
{ @autoreleasepool { @autoreleasepool
{ {
NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* resource_path = [[NSBundle mainBundle] resourcePath]; NSString* resource_path = [[NSBundle mainBundle] resourcePath];
const char *resource_path_c = [resource_path UTF8String]; lua_pushstring(L, [resource_path UTF8String]);
size_t len = strlen(resource_path_c); lua_setglobal(L, "MACOS_RESOURCES");
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;
}} }}

View File

@ -103,12 +103,6 @@ int main(int argc, char **argv) {
SetProcessDPIAware(); SetProcessDPIAware();
#endif #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_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
SDL_EnableScreenSaver(); SDL_EnableScreenSaver();
SDL_EventState(SDL_DROPFILE, SDL_ENABLE); SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
@ -155,13 +149,17 @@ init_lua:
lua_pushstring(L, exename); lua_pushstring(L, exename);
lua_setglobal(L, "EXEFILE"); lua_setglobal(L, "EXEFILE");
#ifdef __APPLE__
set_macos_bundle_resources(L);
#endif
const char *init_lite_code = \ const char *init_lite_code = \
"local core\n" "local core\n"
"xpcall(function()\n" "xpcall(function()\n"
" HOME = os.getenv('" LITE_OS_HOME "')\n" " HOME = os.getenv('" LITE_OS_HOME "')\n"
" local exedir = EXEFILE:match(\"^(.*)" LITE_PATHSEP_PATTERN LITE_NONPATHSEP_PATTERN "$\")\n" " local exedir = EXEFILE:match(\"^(.*)" LITE_PATHSEP_PATTERN LITE_NONPATHSEP_PATTERN "$\")\n"
" local prefix = exedir:match(\"^(.*)" LITE_PATHSEP_PATTERN "bin$\")\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 = require('core')\n"
" core.init()\n" " core.init()\n"
" core.run()\n" " core.run()\n"