SkyNX/SkyNX-Streamer/main.js

286 lines
7.8 KiB
JavaScript
Raw Normal View History

2020-04-16 00:15:44 +00:00
2020-04-16 03:58:28 +00:00
const { spawn } = require("child_process");
2020-04-26 22:08:05 +00:00
const { exec } = require('child_process');
const elevate = require("windows-elevate");
2020-04-16 00:15:44 +00:00
const windowStateKeeper = require('electron-window-state');
const fs = require('fs');
2020-04-16 03:58:28 +00:00
const DB = require('./Devlord_modules/DB.js');
const isDev = require('electron-is-dev');
2020-04-26 23:46:42 +00:00
var AutoLaunch = require('auto-launch');
2020-04-22 22:53:59 +00:00
var AU = require('ansi_up');
var ansi_up = new AU.default;
const { app, BrowserWindow, ipcMain, Menu, Tray, screen } = require('electron')
2020-04-16 00:15:44 +00:00
let mainWindow
var usingUI = true;
2020-04-26 23:46:42 +00:00
var minimizeToTray = false;
var autoChangeResolution = false;
app.commandLine.appendSwitch('high-dpi-support', 'true');
//var sr = require('screenres');
// sr.set(800, 600);
2020-04-16 00:15:44 +00:00
function createWindow() {
let mainWindowState = windowStateKeeper({
2020-04-26 22:56:40 +00:00
defaultWidth: 500,
defaultHeight: 400
2020-04-16 00:15:44 +00:00
});
// Create the browser window.
mainWindow = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
2020-04-16 03:58:28 +00:00
// width: mainWindowState.width,
// height: mainWindowState.height,
width: 500,
2020-04-26 22:56:40 +00:00
height: 320,
2020-04-16 03:58:28 +00:00
// minWidth: 350,
// minHeight: 300,
2020-04-16 00:15:44 +00:00
webPreferences: {
nodeIntegration: true
},
transparent: true,
2020-04-16 03:58:28 +00:00
resizable: false,
2020-04-16 00:15:44 +00:00
frame: false
})
mainWindowState.manage(mainWindow);
mainWindow.setMenu(null);
2020-04-26 22:08:05 +00:00
// load the index.html of the app.
2020-04-16 00:15:44 +00:00
mainWindow.loadFile('index.html');
//fix transparency bug in windows 10
mainWindow.reload();
// Emitted when the window is closed.
mainWindow.on('closed', function () {
mainWindow = null
2020-04-22 22:11:08 +00:00
try {
streamerProcess.kill();
} catch (error) {
}
2020-04-16 00:15:44 +00:00
});
2020-04-26 23:46:42 +00:00
// mainWindow.on('minimize', function (event) {
// if (minimizeToTray) {
// event.preventDefault();
// mainWindow.hide();
// }
// })
// var iconPath = "icon.ico";
// if (isDev) {
// iconPath = __dirname + "/resources/app/icon.ico"
// }
// var appIcon = new Tray(iconPath)
// var contextMenu = Menu.buildFromTemplate([
// {
// label: 'Show App', click: function () {
// mainWindow.show();
// }
// },
// {
// label: 'Quit', click: function () {
// application.isQuiting = true;
// application.quit();
// }
// }
// ]);
// appIcon.setContextMenu(contextMenu);
mainWindow.on('show', function () {
appIcon.setHighlightMode('always');
});
2020-04-16 00:15:44 +00:00
}
var screenWidth;
var screenHeight;
app.on('ready', function () {
if (usingUI) setTimeout(createWindow, 300);
var mainScreen = screen.getPrimaryDisplay();
screenWidth = mainScreen.bounds.width * screen.getPrimaryDisplay().scaleFactor;
screenHeight = mainScreen.bounds.height * screen.getPrimaryDisplay().scaleFactor;
});
2020-04-16 00:15:44 +00:00
// Quit when all windows are closed.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
2020-04-16 03:58:28 +00:00
streamerProcess.kill();
2020-04-16 00:15:44 +00:00
})
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow()
})
app.on('browser-window-created', function (e, window) {
window.setMenu(null);
});
2020-04-16 03:58:28 +00:00
var streamerProcess;
var clientSender;
2020-04-24 19:14:48 +00:00
var streamerProcessIsRunning = false;
2020-04-16 03:58:28 +00:00
function startStreamer(arg) {
if (autoChangeResolution) {
changeScreenRes("1280", "720");
}
var cwd = './NxStreamingService/';
if (!isDev) {
cwd = "./resources/app/NxStreamingService"
}
var args = ["/ip", arg.ip, "/q", arg.q, "/w", screenWidth, "/h", screenHeight];
if (arg.disableVideo) {
args.push("/noVideo");
}
2020-04-24 19:14:48 +00:00
if (arg.disableAudio) {
args.push("/noAudio");
}
if (arg.abxySwap) {
args.push("/abxySwap");
}
2020-04-26 22:56:40 +00:00
if (arg.encoding == "NVENC") {
args.push("/e");
args.push("NVENC");
}
2020-04-27 00:24:33 +00:00
if (arg.limitFPS) {
args.push("/limitFPS");
}
2020-04-16 03:58:28 +00:00
streamerProcess = spawn(
"./NxStreamingService.exe",
args,
{ cwd: cwd, stdio: "pipe" }
2020-04-16 03:58:28 +00:00
);
2020-04-24 19:14:48 +00:00
2020-04-16 03:58:28 +00:00
streamerProcess.stdout.on("data", data => {
2020-04-22 22:53:59 +00:00
log(`${data}`);
2020-04-24 19:14:48 +00:00
if (!streamerProcessIsRunning) {
streamerProcessIsRunning = true;
clientSender.send("started");
}
2020-04-16 03:58:28 +00:00
});
streamerProcess.stderr.on('data', (data) => {
2020-04-22 23:00:27 +00:00
log(`${data}`);
2020-04-24 19:14:48 +00:00
if (!streamerProcessIsRunning) {
streamerProcessIsRunning = true;
clientSender.send("started");
}
2020-04-16 03:58:28 +00:00
});
streamerProcess.on('close', (code) => {
clientSender.send("close");
2020-04-22 22:53:59 +00:00
log(`streamerProcess process exited with code ${code}`);
2020-04-24 19:14:48 +00:00
streamerProcessIsRunning = false;
if (autoChangeResolution) {
changeScreenRes(screenWidth, screenHeight);
}
2020-04-16 03:58:28 +00:00
});
}
ipcMain.on('connect', (event, arg) => {
clientSender = event.sender;
startStreamer(arg);
})
2020-04-16 00:15:44 +00:00
2020-04-16 03:58:28 +00:00
ipcMain.on('restart', (event, arg) => {
streamerProcess.kill();
startStreamer(arg);
2020-04-16 00:58:13 +00:00
});
2020-04-16 03:58:28 +00:00
ipcMain.on('kill', (event, arg) => {
streamerProcess.kill();
2020-04-16 00:58:13 +00:00
});
ipcMain.on('installScpVBus', (event, arg) => {
2020-04-22 22:53:59 +00:00
log("Installing ScpVBus driver..")
var df = __dirname + "\\NxStreamingService\\lib\\"
elevate.exec(df + "devcon.exe", ["install", df + "ScpVBus.inf", "Root\\ScpVBus"],
function (error, stdout, stderr) {
2020-04-22 22:53:59 +00:00
log(`${stdout}`);
log(`${stderr}`);
if (error) {
2020-04-22 22:53:59 +00:00
log("driver install error: " + error);
} else {
2020-04-22 22:53:59 +00:00
log("Driver installed!");
}
});
});
ipcMain.on('unInstallScpVBus', (event, arg) => {
2020-04-22 22:53:59 +00:00
log("Un-Installing ScpVBus driver..")
var df = __dirname + "\\NxStreamingService\\lib\\"
elevate.exec(df + "devcon.exe", ["remove", "Root\\ScpVBus"],
function (error, stdout, stderr) {
if (error !== null) {
2020-04-22 22:53:59 +00:00
log('driver uninstall error: ' + error);
} else {
2020-04-22 22:53:59 +00:00
log("Driver un-installed!");
}
});
});
ipcMain.on('installAudioDriver', (event, arg) => {
2020-04-22 22:53:59 +00:00
log("Installing audio driver..")
var df = __dirname + "\\NxStreamingService\\lib\\"
elevate.exec("regsvr32", [df + "audio_sniffer.dll"],
function (error, stdout, stderr) {
if (error !== null) {
2020-04-22 22:53:59 +00:00
log('driver install error: ' + error);
} else {
2020-04-22 22:53:59 +00:00
log("Driver installed!");
}
});
});
ipcMain.on('unInstallAudioDriver', (event, arg) => {
2020-04-22 22:53:59 +00:00
log("Un-Installing audio driver..")
var df = __dirname + "\\NxStreamingService\\lib\\"
elevate.exec("regsvr32", ["/u", df + "audio_sniffer.dll"],
function (error, stdout, stderr) {
if (error !== null) {
2020-04-22 22:53:59 +00:00
log('driver uninstall error: ' + error);
} else {
2020-04-22 22:53:59 +00:00
log("Driver un-installed!");
}
});
2020-04-22 22:53:59 +00:00
});
var htmlLoggingSender
ipcMain.on('registerForHTMLLogging', (event, arg) => {
htmlLoggingSender = event.sender
})
ipcMain.on('consoleCommand', (event, fullMessage) => {
var args = fullMessage.split(" ");
var command = args.shift().toLowerCase();
//Will add later
})
function log(str) {
console.log(str);
if (htmlLoggingSender) {
htmlLoggingSender.send('log', ansi_up.ansi_to_html(str.replace(" ", "\xa0")) + "<br>");
}
2020-04-26 22:08:05 +00:00
}
ipcMain.on('donate', (event, fullMessage) => {
var url = 'https://www.paypal.me/SkyNX';
var start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open');
exec(start + ' ' + url);
2020-04-26 22:08:05 +00:00
})
2020-04-26 23:46:42 +00:00
var autoLauncher = new AutoLaunch({
name: 'SkyNX',
path: __dirname.replace("resources\\app\\", "") + '\\SkyNXStreamer.exe',
});
ipcMain.on('autoStartupOn', (event, fullMessage) => {
if (!autoLauncher.isEnabled) {
autoLauncher.enable();
}
});
ipcMain.on('autoStartupOff', (event, fullMessage) => {
if (autoLauncher.isEnabled) {
autoLauncher.disable();
}
});
ipcMain.on('autoChangeResolutionOn', (event, fullMessage) => {
autoChangeResolution = true;
});
ipcMain.on('autoChangeResolutionOff', (event, fullMessage) => {
if (autoChangeResolution) {
changeScreenRes(screenWidth, screenHeight);
}
autoChangeResolution = false;
});
ipcMain.on("restartComputer", (event, fullMessage) => {
exec("shutdown -r -t 0");
});
function changeScreenRes(width, height) {
var df = __dirname + "\\NxStreamingService\\lib\\ChangeScreenResolution.exe"
exec(df + " /w=" + width + " /h=" + height + " /d=0");
}