From 972215ca8bebf2071de491b75bfbd25e034b8c4c Mon Sep 17 00:00:00 2001 From: kvan7 Date: Wed, 8 Jul 2026 06:54:58 -0500 Subject: [PATCH] technically builds and runs now --- main/build/script.mjs | 15 ++++++------ main/src/main.ts | 3 ++- main/src/vision/RuneRecipeFinder.ts | 2 ++ main/src/vision/opencv/CppAdapter.ts | 2 ++ main/src/vision/opencv/JsAdapter.ts | 2 ++ main/src/vision/opencv/constants.ts | 6 ++--- main/src/vision/opencv/cvWrapper.ts | 34 ++++++++++++++++------------ 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/main/build/script.mjs b/main/build/script.mjs index 85a9da91..f755356b 100644 --- a/main/build/script.mjs +++ b/main/build/script.mjs @@ -18,22 +18,23 @@ const electronRunner = (() => { } })() -const visionBuild = await esbuild.build({ - entryPoints: ['src/vision/link-worker.ts'], - bundle: true, - platform: 'node', - outfile: 'dist/vision.js' -}) +// const visionBuild = await esbuild.build({ +// entryPoints: ['src/vision/link-worker.ts'], +// bundle: true, +// platform: 'node', +// outfile: 'dist/vision.js' +// }) const mainContext = await esbuild.context({ entryPoints: ['src/main.ts'], bundle: true, minify: !isDev, platform: 'node', - external: ['electron', 'uiohook-napi', 'electron-overlay-window', 'canvas', "@u4/opencv-build", "@u4/opencv4nodejs",], + external: ['electron', 'uiohook-napi', 'electron-overlay-window', 'canvas', '@u4/opencv-build', '@u4/opencv4nodejs'], outfile: 'dist/main.js', define: { 'process.env.STATIC': (isDev) ? '"../build/icons"' : '"."', + 'process.env.GENERATED': (isDev) ? '"../build/generated"' : '"."', 'process.env.VITE_DEV_SERVER_URL': (isDev) ? '"http://localhost:5173"' : 'null' }, plugins: (isDev) ? [{ diff --git a/main/src/main.ts b/main/src/main.ts index 49a8aec1..3cd8105d 100644 --- a/main/src/main.ts +++ b/main/src/main.ts @@ -25,7 +25,8 @@ if (process.platform !== "darwin") { } app.enableSandbox(); let tray: AppTray; - +process.on("uncaughtException", console.error); +process.on("unhandledRejection", console.error); (async () => { if (process.platform === "darwin") { async function ensureAccessibilityPermission(): Promise { diff --git a/main/src/vision/RuneRecipeFinder.ts b/main/src/vision/RuneRecipeFinder.ts index 306db0fe..d64d4605 100644 --- a/main/src/vision/RuneRecipeFinder.ts +++ b/main/src/vision/RuneRecipeFinder.ts @@ -23,6 +23,7 @@ export class RuneRecipeFinder { } async calibrate(screenshot: ImageData): Promise { + console.log("calibrate"); return await this.cv.calibrate(screenshot); } @@ -30,6 +31,7 @@ export class RuneRecipeFinder { screenshot: ImageData, calibration: CalibrationResult, ): Promise { + console.log("findRecipeId"); const start = performance.now(); const res = await this.cv.findRecipeId(screenshot, calibration); const elapsed = performance.now() - start; diff --git a/main/src/vision/opencv/CppAdapter.ts b/main/src/vision/opencv/CppAdapter.ts index 999bccfe..0c722e9b 100644 --- a/main/src/vision/opencv/CppAdapter.ts +++ b/main/src/vision/opencv/CppAdapter.ts @@ -17,6 +17,7 @@ export class CppCvAdapter implements ICvAdapter { //#region Interface Methods async calibrate(screenshot: ImageData): Promise { + console.log("calibrate cpp"); const img = await this.loadImage(Buffer.from(screenshot.data)); const { scale, recipeBBox } = calibrateBBox(img); return { @@ -32,6 +33,7 @@ export class CppCvAdapter implements ICvAdapter { highlightedSlot: number; tomeCount: number; }> { + console.log("findRecipeId cpp"); const img = await this.loadImage(Buffer.from(screenshot.data)); const { bbox, scale } = calibration; const tomeSize = getTomePxSize(scale, img.rows); diff --git a/main/src/vision/opencv/JsAdapter.ts b/main/src/vision/opencv/JsAdapter.ts index 3e52e3ba..d449d921 100644 --- a/main/src/vision/opencv/JsAdapter.ts +++ b/main/src/vision/opencv/JsAdapter.ts @@ -17,6 +17,7 @@ export class JsCvAdapter implements ICvAdapter { //#region Interface Methods async calibrate(screenshot: ImageData): Promise { // REVIEW: ALL ALLOCATIONS MUST BE DELETED AFTER USE + console.log("calibrate js"); const img = await getImage(Buffer.from(screenshot.data)); try { const { scale, recipeBBox } = await calibrateBBox(img); @@ -37,6 +38,7 @@ export class JsCvAdapter implements ICvAdapter { tomeCount: number; }> { // REVIEW: ALL ALLOCATIONS MUST BE DELETED AFTER USE + console.log("findRecipeId js"); const img = await getImage(Buffer.from(screenshot.data)); const { bbox, scale } = calibration; const firstRecipe = img.roi( diff --git a/main/src/vision/opencv/constants.ts b/main/src/vision/opencv/constants.ts index ccc11a2f..48750bd6 100644 --- a/main/src/vision/opencv/constants.ts +++ b/main/src/vision/opencv/constants.ts @@ -48,19 +48,19 @@ export const GENERATED_DIR = "./generated/"; export const PREPROCESSED_TOME_BG = path.join( __dirname, - process.env.STATIC!, + process.env.GENERATED!, GENERATED_DIR, "bg.png", ); export const PREPROCESSED_ACTIVE_TOME_BG = path.join( __dirname, - process.env.STATIC!, + process.env.GENERATED!, GENERATED_DIR, "active_bg.png", ); export const PREPROCESSED_MATCH_LIST = path.join( __dirname, - process.env.STATIC!, + process.env.GENERATED!, GENERATED_DIR, "match_list.png", ); diff --git a/main/src/vision/opencv/cvWrapper.ts b/main/src/vision/opencv/cvWrapper.ts index 84667332..e7c2b04c 100644 --- a/main/src/vision/opencv/cvWrapper.ts +++ b/main/src/vision/opencv/cvWrapper.ts @@ -21,7 +21,8 @@ export class CvWrapper { } constructor() { - this._ready = this.reload(); + this._cv = undefined; + this._ready = this._load(); } async reload(): Promise { @@ -36,7 +37,7 @@ export class CvWrapper { this._cv = new CppCvAdapter(cpp); return; } - this._cv = new JsCvAdapter(await this._loadJs()); + this._cv = new JsCvAdapter((await this._loadJs()).cv); } private async _tryCpp(): Promise { @@ -53,21 +54,24 @@ export class CvWrapper { } } - private async _loadJs(): Promise { + private async _loadJs(): Promise<{ cv: typeof cvModule }> { + let cv: typeof cvModule; if (cvModule instanceof Promise) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- should be fine - return await cvModule; + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- is of correct type + cv = await cvModule; + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- actually not always true + } else if (cvModule.Mat) { + cv = cvModule; + } else { + await new Promise((resolve) => { + cvModule.onRuntimeInitialized = () => { + resolve(); + }; + }); + cv = cvModule; } - if (cvModule.Mat) { - return cvModule; - } - await new Promise((resolve) => { - cvModule.onRuntimeInitialized = () => { - console.log("OpenCV JS is ready"); - resolve(); - }; - }); - return cvModule; + + return { cv }; } }