From 2f465061e7300d8a08b145e5ca2c4559007fbf6c Mon Sep 17 00:00:00 2001
From: Sam Wray <sam@wray.pro>
Date: Sat, 23 May 2020 20:15:10 +0100
Subject: [PATCH] fix(__dirname): adds horrible wrapper hack for __dirname with
 webpack & electron & workers

This is an abominable hack which works around the fact webpack just does whatever the fuck it wants
with workers. We stall everything in the worker until we've sent it the correct path to the .asar,
then in the NDI store module we temporarily set the location of grandiose within the .asar.
Guaranteed this will come back to bite us in the arse.

fix #114
---
 src/application/index.js                    |  7 +++-
 src/application/worker/index.worker.js      | 37 +++++++++++++--------
 src/application/worker/store/modules/ndi.js | 11 +++++-
 3 files changed, 40 insertions(+), 15 deletions(-)

diff --git a/src/application/index.js b/src/application/index.js
index 08d27ba..1a2c396 100644
--- a/src/application/index.js
+++ b/src/application/index.js
@@ -9,7 +9,7 @@ import use from "./use";
 
 import PromiseWorker from "promise-worker-transferable";
 
-import { ipcRenderer } from "electron";
+import { ipcRenderer, remote } from "electron";
 
 let imageBitmap;
 const imageBitmapQueue = [];
@@ -35,6 +35,11 @@ export default class ModV {
     this.$worker = new Worker();
     this.$asyncWorker = new PromiseWorker(this.$worker);
 
+    this.$worker.postMessage({
+      type: "__dirname",
+      payload: remote.app.getAppPath()
+    });
+
     this.$worker.addEventListener("message", e => {
       if (e.data.type === "tick" && this.ready) {
         this.tick(e.data.payload);
diff --git a/src/application/worker/index.worker.js b/src/application/worker/index.worker.js
index 004a06b..73d246e 100644
--- a/src/application/worker/index.worker.js
+++ b/src/application/worker/index.worker.js
@@ -1,18 +1,15 @@
 /* eslint-env worker node */
-import registerPromiseWorker from "promise-worker/register";
-import fs from "fs";
-import store from "./store";
-import loop from "./loop";
-import { tick as frameTick } from "./frame-counter";
-import { getFeatures, setFeatures } from "./audio-features";
-import featureAssignmentPlugin from "../plugins/feature-assignment";
 
 let lastKick = false;
 
-(async function() {
-  self.addEventListener("unhandledrejection", e => {
-    console.log(e);
-  });
+async function start() {
+  const registerPromiseWorker = require("promise-worker/register");
+  const fs = require("fs");
+  const store = require("./store").default;
+  const loop = require("./loop").default;
+  const { tick: frameTick } = require("./frame-counter");
+  const { getFeatures, setFeatures } = require("./audio-features");
+  // const featureAssignmentPlugin = require("../plugins/feature-assignment");
 
   store.subscribe(mutation => {
     const { type, payload } = mutation;
@@ -112,7 +109,7 @@ let lastKick = false;
     store.dispatch("modules/registerModule", module);
   }
 
-  store.dispatch("plugins/add", featureAssignmentPlugin);
+  // store.dispatch("plugins/add", featureAssignmentPlugin);
 
   const webcamOutput = await store.dispatch("outputs/getAuxillaryOutput", {
     name: "webcam",
@@ -256,4 +253,18 @@ let lastKick = false;
   });
 
   self.store = store;
-})();
+}
+
+function handleDirname(e) {
+  const message = e.data;
+  const { type, payload } = message;
+
+  if (type === "__dirname") {
+    self.__dirname = payload;
+
+    self.removeEventListener("message", handleDirname);
+    start();
+  }
+}
+
+self.addEventListener("message", handleDirname);
diff --git a/src/application/worker/store/modules/ndi.js b/src/application/worker/store/modules/ndi.js
index 8c1e52a..6049a46 100644
--- a/src/application/worker/store/modules/ndi.js
+++ b/src/application/worker/store/modules/ndi.js
@@ -1,8 +1,17 @@
-import grandiose from "grandiose";
+/* globals __dirname */
+
 import uuidv4 from "uuid/v4";
 import Vue from "vue";
 import store from "../";
 
+// eslint-disable-next-line
+__dirname = `${__dirname}/node_modules/grandiose`;
+
+const grandiose = require("grandiose");
+
+// eslint-disable-next-line
+__dirname = __dirname.replace("/node_modules/grandiose", "");
+
 const state = {
   discovering: false,