diff --git a/package.json b/package.json index 95a589b..5fc2eeb 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,9 @@ "start": "cross-env NODE_ENV=production npm run build:after && electron dist/bundled/background.js", "build": "vue-cli-service electron:build", "build:after": "node scripts/afterBuild.js", - "build:bindings": "cmake-js build -d bindings -O bindings/build && ./collect-dylib-deps.sh", + "build:bindings": "cmake-js build -d bindings -O bindings/build", + "move:libs": "node scripts/moveLibs.js", + "collect:dylibs": "./collect-dylib-deps.sh", "pack": "npm run build -- --dir", "dist": "npm run dist:mac && npm run dist:win && npm run dist:linux", "dist:mac": "cross-env NODE_ENV=production npm run build -- --mac", diff --git a/scripts/moveLibs.js b/scripts/moveLibs.js new file mode 100644 index 0000000..a1bd5cc --- /dev/null +++ b/scripts/moveLibs.js @@ -0,0 +1,22 @@ +const { readdirSync, copyFileSync } = require('fs') +const { join } = require('path') + +const PUBLIC_DIR = join(__dirname, '..', 'public') +const BINDINGS_BUILD_DIR = join(__dirname, '..', 'bindings', 'build', 'Release') + +const FILE_NAME = { + darwin: 'libtorrent-rasterbar.10.dylib', + win32: 'libtorrent-rasterbar.dll' +}[process.platform] || '.so' + +readdirSync(BINDINGS_BUILD_DIR) + .forEach((file) => { + if (file !== FILE_NAME) return + + console.log(`[KawAnime] Moving ${file} to public directory.`) + + copyFileSync( + join(BINDINGS_BUILD_DIR, file), + join(PUBLIC_DIR, file) + ) + }) diff --git a/src/main/externals/mpv/index.js b/src/main/externals/mpv/index.js index 1dadc11..921d60c 100644 --- a/src/main/externals/mpv/index.js +++ b/src/main/externals/mpv/index.js @@ -1,6 +1,61 @@ /* global __static */ -import { join } from 'path' +import { join, dirname, relative, sep } from 'path' + +/** + * The MIME type associated with mpv.js plugin. + * This is coming straight from mpvjs lib + */ +const PLUGIN_MIME_TYPE = 'application/x-mpvjs' + +function containsNonASCII (str) { + for (let i = 0; i < str.length; i++) { + if (str.charCodeAt(i) > 255) { + return true + } + } + + return false +} + +/** + * Return value to be passed to `register-pepper-plugins` switch. + * This is coming straight from mpvjs lib + * + * @param {string} pluginDir - Plugin directory + * @param {string} [pluginName=mpvjs.node] - Plugin name + * @throws {Error} Resulting path contains non-ASCII characters. + */ +function getPluginEntry (pluginDir, pluginName = 'mpvjs.node') { + const fullPluginPath = join(pluginDir, pluginName) + + // Try relative path to workaround ASCII-only path restriction. + let pluginPath = relative(process.cwd(), fullPluginPath) + + if (dirname(pluginPath) === '.') { + // "./plugin" is required only on Linux. + if (process.platform === 'linux') { + pluginPath = `.${sep}${pluginPath}` + } + } else { + // Relative plugin paths doesn't work reliably on Windows, see + // . + if (process.platform === 'win32') { + pluginPath = fullPluginPath + } + } + + if (containsNonASCII(pluginPath)) { + if (containsNonASCII(fullPluginPath)) { + throw new Error('Non-ASCII plugin path is not supported') + } else { + pluginPath = fullPluginPath + } + } + + return `${pluginPath};${PLUGIN_MIME_TYPE}` +} + /** * Configure app to use MPV player. * @@ -9,12 +64,11 @@ import { join } from 'path' export default function (app) { // Absolute path to the plugin directory. const pluginDir = join(__static, 'mpv') - const pluginPath = join(pluginDir, 'mpvjs.node;application/x-mpvjs') // See pitfalls section for details. - // if (process.platform !== 'linux') process.chdir(pluginDir) + if (process.platform !== 'linux') process.chdir(pluginDir) // To support a broader number of systems. app.commandLine.appendSwitch('ignore-gpu-blacklist') - app.commandLine.appendSwitch('register-pepper-plugins', pluginPath) + app.commandLine.appendSwitch('register-pepper-plugins', getPluginEntry(pluginDir)) }