Skip to content

Commit

Permalink
Fix load addon in electron and add warning to .asar
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirherobrine23 committed Jul 9, 2024
1 parent a0c5e7e commit e646164
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"engines": {
"node": ">=16.0.0"
},
"binary": {
"napi_versions": [8]
},
"scripts": {
"install": "cmake-js compile",
"build": "cmake-js rebuild",
Expand Down
18 changes: 10 additions & 8 deletions src/addons.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from "node:path";
import fs from "node:fs/promises";

const __dirname = import.meta.dirname || path.dirname((await import("node:url")).fileURLToPath(import.meta.url));
export const projectRoot: string = !process["resourcesPath"] ? path.resolve(__dirname, "..") : process["resourcesPath"];
const __dirname = import.meta.dirname || path.dirname((await import("node:url")).fileURLToPath(import.meta.url)); // Solve current __dirname in ESM module
export const projectRoot = path.resolve(__dirname, "..");
if (__dirname.includes(".asar")) {
console.warn("Check if addon nothing includes in .asar file")
}

declare global {
namespace NodeJS {
Expand Down Expand Up @@ -43,11 +45,11 @@ export async function LoadAddon<T = any>(addonFile: string, exports?: Record<st
let _addonFile: string = null
if (await exists(addonFile)) _addonFile = addonFile;
else if (await exists(path.resolve(projectRoot, addonFile))) _addonFile = path.resolve(projectRoot, addonFile)
else if (await exists(path.resolve(projectRoot, addonFile+".node"))) _addonFile = path.resolve(projectRoot, addonFile+".node")
else if (await exists(path.resolve(projectRoot, "build/Release", addonFile))) _addonFile = path.resolve(projectRoot, "build/Release", addonFile)
else if (await exists(path.resolve(projectRoot, "build/Release", addonFile+".node"))) _addonFile = path.resolve(projectRoot, "build/Release", addonFile+".node")
else if (await exists(path.resolve(projectRoot, "build/Debug", addonFile))) _addonFile = path.resolve(projectRoot, "build/Debug", addonFile)
else if (await exists(path.resolve(projectRoot, "build/Debug", addonFile+".node"))) _addonFile = path.resolve(projectRoot, "build/Debug", addonFile+".node")
else if (await exists(path.resolve(projectRoot, addonFile+".node"))) _addonFile = path.resolve(projectRoot, addonFile+".node")
else if (await exists(path.resolve(projectRoot, "build/Release", addonFile))) _addonFile = path.resolve(projectRoot, "build/Release", addonFile)
else if (await exists(path.resolve(projectRoot, "build/Release", addonFile+".node"))) _addonFile = path.resolve(projectRoot, "build/Release", addonFile+".node")
else if (await exists(path.resolve(projectRoot, "build/Debug", addonFile))) _addonFile = path.resolve(projectRoot, "build/Debug", addonFile)
else if (await exists(path.resolve(projectRoot, "build/Debug", addonFile+".node"))) _addonFile = path.resolve(projectRoot, "build/Debug", addonFile+".node")
if (!_addonFile) throw new Error("Cannot load required addon")
let ext: NodeJS.Moduledlopen = {exports: Object.assign({}, exports)}
process.dlopen(ext, _addonFile)
Expand Down
2 changes: 1 addition & 1 deletion src/wginterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const addon = await LoadAddon<{
*/
setConfig(config: SetConfig): Promise<void>;
}>("wg", {
WIN32DLLPATH: path.resolve(projectRoot, "addon/win", (process.arch === "x64" && "amd64") || (process.arch === "ia32" && "x86") || process.arch, "wireguard.dll")
WIN32DLLPATH: !process.env.WGWIN32DLL ? path.resolve(projectRoot, "addon/win", (process.arch === "x64" && "amd64") || (process.arch === "ia32" && "x86") || process.arch, "wireguard.dll") : path.resolve(process.env.WGWIN32DLL),
});

export const {
Expand Down

0 comments on commit e646164

Please sign in to comment.