Fix invalid EXEFILE and EXEDIR on Windows (#1396)

* fix(main): fix get_exe_filename returning invalid result on Windows
* fix(main): fix bootstrap not intepreting UTF-8 properly
This commit is contained in:
Takase 2023-04-08 01:06:01 +08:00 committed by George Sokianos
parent 7ca0ec18ca
commit e935454992
1 changed files with 15 additions and 4 deletions

View File

@ -32,8 +32,18 @@ static double get_scale(void) {
static void get_exe_filename(char *buf, int sz) {
#if _WIN32
int len = GetModuleFileName(NULL, buf, sz - 1);
buf[len] = '\0';
int len;
wchar_t *buf_w = malloc(sizeof(wchar_t) * sz);
if (buf_w) {
len = GetModuleFileNameW(NULL, buf_w, sz - 1);
buf_w[len] = L'\0';
// if the conversion failed we'll empty the string
if (!WideCharToMultiByte(CP_UTF8, 0, buf_w, -1, buf, sz, NULL, NULL))
buf[0] = '\0';
free(buf_w);
} else {
buf[0] = '\0';
}
#elif __linux__
char path[] = "/proc/self/exe";
ssize_t len = readlink(path, buf, sz - 1);
@ -218,9 +228,10 @@ init_lua:
const char *init_lite_code = \
"local core\n"
"xpcall(function()\n"
" local match = require('utf8extra').match\n"
" HOME = os.getenv('" LITE_OS_HOME "')\n"
" local exedir = EXEFILE:match('^(.*)" LITE_PATHSEP_PATTERN LITE_NONPATHSEP_PATTERN "$')\n"
" local prefix = os.getenv('LITE_PREFIX') or exedir:match('^(.*)" LITE_PATHSEP_PATTERN "bin$')\n"
" local exedir = match(EXEFILE, '^(.*)" LITE_PATHSEP_PATTERN LITE_NONPATHSEP_PATTERN "$')\n"
" local prefix = os.getenv('LITE_PREFIX') or match(exedir, '^(.*)" LITE_PATHSEP_PATTERN "bin$')\n"
" dofile((MACOS_RESOURCES or (prefix and prefix .. '/share/lite-xl' or exedir .. '/data')) .. '/core/start.lua')\n"
" core = require(os.getenv('LITE_XL_RUNTIME') or 'core')\n"
" core.init()\n"