hiitiger / goverlay

DirectX hook and game overlay solution for Electron, Qt and CEF, just like discord/steam game overlay,inject any app to overlay in your game
Other
783 stars 108 forks source link

Demo works fine, crashes when trying on my own project #101

Closed DDDASHXD closed 1 year ago

DDDASHXD commented 1 year ago

I'm using Electron alongside React. When i run the electron app, a second (weird) terminal opens up, which shows the log, like PostThreatMessage to hook WindowPostThreadMessage to hook windowHookApp::hookD3d9, 0 over and over again for some time, until it shows PostThreadMessage to hook windowsafeInject, pid: 12164, result: 1. After this, clicking the hotkey does nothing, and then the overlay crashes. The weird terminal window closes, and my original terminal logs [electron] OverlayMain::onClientClose,13436.

This is the code i use to initialize the browserwindow:

const path = require("path");
const IOverlay = require("electron-overlay");
const fs = require("fs");

const { app, BrowserWindow, globalShortcut } = require("electron");
const isDev = require("electron-is-dev");

IOverlay.start();
IOverlay.setHotkeys([
  {
    name: "overlay.toggle",
    keyCode: 9,
    modifiers: { ctrl: true },
  },
]);
IOverlay.setEventCallback((event, payload) => {
  if (event === "game.input") {
    const window = BrowserWindow.fromId(payload.windowId);
    if (window) {
      const inputEvent = IOverlay.translateInputEvent(payload);
      if (payload.msg !== 512) {
        console.log(event, payload);
        console.log(`translate ${JSON.stringify(inputEvent)}`);
      }

      if (inputEvent) {
        window.webContents.sendInputEvent(inputEvent);
      }

      const focusWin = BrowserWindow.fromId(payload.focusWindowId);
      if (focusWin) {
        focusWin.focusOnWebView();
      }
    }
  }
});

function createWindow() {
  // Create the browser window.
  screen = require("electron").screen;
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    show: false,
    webPreferences: {
      nodeIntegration: true,
      webviewTag: true,
      offscreen: true,
    },
    transparent: true,
    frame: false,
    fullscreen: true,
    webSecurity: false,
    skipTaskbar: true,
    resizable: false,
    title: "chroverlay",
  });
  addOverlayWindows("chroverlay", win, 0, 0, true);

  win.webContents.on("paint", (event, dirty, image) => {
    IOverlay.sendFrameBuffer(
      win.id,
      image.getBitmap(),
      image.getSize().width,
      image.getSize().height
    );
  });

  win.on("resize", () => {
    IOverlay.sendWindowBounds(window.id, { rect: window.getBounds() });
  });

  let process;
  for (let i = 0; i < IOverlay.getTopWindows(true).length; i++) {
    const element = IOverlay.getTopWindows(true)[i];
    if (element.title.includes("GameWindow")) {
      process = element;
    }
  }
  if (process) {
    console.log("inject:" + IOverlay.injectProcess(process));
  }

  win.loadURL(
    isDev
      ? "http://localhost:3000"
      : `file://${path.join(__dirname, "../build/index.html")}`
  );

  let window = null;
  if (isDev) {
    win.webContents.openDevTools({ mode: "detach" });
  }
}

const addOverlayWindows = (
  name,
  window,
  dragborder = 0,
  captionHeight = 0,
  transparent = false
) => {
  const display = screen.getDisplayNearestPoint(screen.getCursorScreenPoint());
  IOverlay.addWindow(window.id, {
    name,
    transparent,
    resizable: window.isResizable(),
    maxWidth: window.isResizable
      ? display.bounds.width
      : window.getBounds().width,
    maxHeight: window.isResizable
      ? display.bounds.height
      : window.getBounds().height,
    minWidth: window.isResizable ? 100 : window.getBounds().width,
    minHeight: window.isResizable ? 100 : window.getBounds().height,
    nativeHandle: window.getNativeWindowHandle().readUInt32LE(0),
    rect: window.getBounds(),
    caption: {
      left: dragborder,
      right: dragborder,
      top: dragborder,
      height: captionHeight,
    },
    dragBorderWidth: dragborder,
  });
};

app.whenReady().then(() => {
  createWindow();
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

And i get this in the terminal when running the app.

PS C:\Users\sebas\Documents\github\chroverlay> npm run dev
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

> chroverlay@0.1.0 dev
> concurrently -k "npm start" "npm:electron"

[0] npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
[electron] npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
[0]
[0] > chroverlay@0.1.0 start
[0] > react-scripts start
[0]
[electron]
[electron] > chroverlay@0.1.0 electron
[electron] > wait-on tcp:3000 && electron .
[electron]
[0] (node:21912) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
[0] (Use `node --trace-deprecation ...` to show where the warning was created)
[0] (node:21912) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
[electron]
[electron] OverlayMain::addWindow
[electron] create share mem:1920,1080
[electron] inject:[object Object]
[electron] OverlayMain::onClientConnect,29604
[electron] OverlayMain::_sendOverlayInit{"directMessageInput":false,"dragMode":1,"fpsPosition":1,"hotkeys":[{"alt":false,"ctrl":true,"keyCode":9,"name":"overlay.toggle","passthrough":false,"shift":false}],"processEnabled":true,"shareMemMutex":"electron-overlay-sharemem-{4C4BD948-0F75-413F-9667-AC64A7944D8E}15748-520443218","showfps":false,"type":"overlay.init","windows":[{"bufferName":"electron-overlay-15748-520443375-1-image-1","caption":{"height":0,"left":0,"right":0,"top":0},"dragBorderWidth":0,"maxHeight":1080,"maxWidth":1920,"minHeight":100,"minWidth":100,"name":"chroverlay","nativeHandle":15076874,"rect":{"height":1080,"width":1920,"x":0,"y":0},"resizable":false,"transparent":true,"type":"window","windowId":1}]}
[0] Starting the development server...
[0]
[0] Compiled successfully!
[0]
[0] You can now view chroverlay in the browser.
[0]
[0]   Local:            http://localhost:3000
[0]   On Your Network:  http://192.168.1.94:3000
[0]
[0] Note that the development build is not optimized.
[0] To create a production build, use npm run build.
[0]
[0] webpack compiled successfully
[electron] OverlayMain::onClientClose,29604

and this is the full log of the weird terminal (before it closes): image