Skip to content

Commit

Permalink
Removed ECP service circular dependencies (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvcabral authored Feb 3, 2025
1 parent be7bdf4 commit e2f1489
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 125 deletions.
2 changes: 2 additions & 0 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ function appLoaded(appData) {
api.enableMenuItem("close-channel", true);
api.enableMenuItem("save-screen", true);
api.enableMenuItem("copy-screen", true);
api.send("currentApp", appData);
}

function appTerminated() {
Expand All @@ -320,6 +321,7 @@ function appTerminated() {
api.enableMenuItem("close-channel", false);
api.enableMenuItem("save-screen", false);
api.enableMenuItem("copy-screen", false);
api.send("currentApp", currentApp);
}

function redrawEvent(redraw) {
Expand Down
1 change: 1 addition & 0 deletions src/app/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ contextBridge.exposeInMainWorld("api", {
"keySent",
"runCode",
"runUrl",
"currentApp",
"reset",
];
if (validChannels.includes(channel)) {
Expand Down
33 changes: 27 additions & 6 deletions src/helpers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { app, BrowserWindow, ipcMain } from "electron";
import { getAudioMuted, getSimulatorOption } from "./settings";
import { runOnPeerRoku, resetPeerRoku } from "./roku";
import { appFocused } from "./window";
import { subscribeECP } from "../server/ecp";
import { isValidUrl } from "./util";
import { zipSync, strToU8 } from "fflate";
import path from "path";
import fs from "fs";
Expand Down Expand Up @@ -144,11 +146,30 @@ function focusWindow(window) {
}
}

function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (err) {
return false;
subscribeECP("files", launchApp);

function launchApp(event, data) {
if (event === "launch") {
const appID = data.appID;
const query = data.query;
let zipPath;
if (appID.toLowerCase() === "dev") {
zipPath = path.join(app.getPath("userData"), "dev.zip");
} else {
const index = getChannelIds().indexOf(appID);
zipPath = getRecentPackage(index);
}
if (zipPath && fs.existsSync(zipPath)) {
const input = new Map();
input.set("source", "external-control");
if (query) {
for (let key in query) {
input.set(key, query[key]);
}
}
loadFile([zipPath], input);
} else {
window?.webContents.send("console", `ECP Launch: File not found! App Id=${appID}`, true);
}
}
}
15 changes: 1 addition & 14 deletions src/helpers/roku.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*--------------------------------------------------------------------------------------------*/
import { app, BrowserWindow, ipcMain } from "electron";
import { getPeerRoku } from "./settings";
import { isValidIP } from "./util";
import request from "postman-request";

let sendECPKeys = false;
Expand Down Expand Up @@ -140,17 +141,3 @@ function postEcpRequest(device, path, callback) {
function isCompileError(responseHtml) {
return !!/install\sfailure:\scompilation\sfailed/i.exec(responseHtml);
}

export function isValidIP(ip) {
if (typeof ip !== "string") {
return false;
}
const parts = ip.split(".");
return (
parts.length === 4 &&
parts.every((part) => {
const num = Number(part);
return !isNaN(num) && num >= 0 && num <= 255;
})
);
}
34 changes: 27 additions & 7 deletions src/helpers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DateTime } from "luxon";
import path from "path";
import ElectronPreferences from "electron-preferences";
import { setAspectRatio } from "./window";
import { enableECP, disableECP } from "../server/ecp";
import { enableECP, disableECP, subscribeECP, ECPPORT } from "../server/ecp";
import { enableTelnet, disableTelnet } from "../server/telnet";
import {
enableInstaller,
Expand Down Expand Up @@ -91,7 +91,7 @@ export function getSettings(window) {
},
browserWindowOverrides: {
title: "Settings",
titleBarStyle: isWindows || isMacOS ? "hidden": null,
titleBarStyle: isWindows || isMacOS ? "hidden" : null,
titleBarOverlay: getTitleOverlayTheme("purple"),
frame: false,
parent: window,
Expand Down Expand Up @@ -675,7 +675,6 @@ export function showSettings() {
checkMenuItem(`theme-${userTheme}`, true);
checkMenuItem("peer-roku-deploy", getPeerRoku().deploy);
checkMenuItem("peer-roku-control", getPeerRoku().syncControl);

}
});
}
Expand Down Expand Up @@ -807,7 +806,7 @@ export function getPeerRoku() {
username: "rokudev",
password: settings.value("peerRoku.password"),
syncControl: settings.value("peerRoku.syncControl")?.includes("enabled") || false,
}
};
}

export function setLocaleId(locale) {
Expand Down Expand Up @@ -877,6 +876,19 @@ export function getModelName(model) {
return modelName ? modelName[0].replace(/ *\([^)]*\) */g, "") : `Roku (${model})`;
}

// Services Status
subscribeECP("settings", updateECPStatus);

export function updateECPStatus(event, enabled) {
if (event === "enabled") {
setPreference("services.ecp", enabled ? ["enabled"] : []);
checkMenuItem("ecp-api", enabled);
const window = BrowserWindow.fromId(1);
window?.webContents.send("serverStatus", "ECP", enabled, ECPPORT);
window?.webContents.send("refreshMenu");
}
}

// Settings Helper Functions

function saveSimulatorSettings(options, window) {
Expand Down Expand Up @@ -958,9 +970,17 @@ function convertChar(keyChar) {
return `Key${keyChar}`;
} else {
const keyMap = new Map([
["`", "Backquote"], ["-", "Minus"], ["=", "Equal"], ["[", "BracketLeft"],
["]", "BracketRight"], [";", "Semicolon"], ["'", "quote"], [",", "Comma"],
[".", "Period"], ["\\", "Backslash"], ["/", "Slash"]
["`", "Backquote"],
["-", "Minus"],
["=", "Equal"],
["[", "BracketLeft"],
["]", "BracketRight"],
[";", "Semicolon"],
["'", "quote"],
[",", "Comma"],
[".", "Period"],
["\\", "Backslash"],
["/", "Slash"],
]);
return keyMap.get(keyChar) ?? keyChar;
}
Expand Down
30 changes: 30 additions & 0 deletions src/helpers/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* BrightScript Simulation Desktop Application (https://github.com/lvcabral/brs-desktop)
*
* Copyright (c) 2019-2025 Marcelo Lv Cabral. All Rights Reserved.
*
* Licensed under the MIT License. See LICENSE in the repository root for license information.
*--------------------------------------------------------------------------------------------*/

export function isValidIP(ip) {
if (typeof ip !== "string") {
return false;
}
const parts = ip.split(".");
return (
parts.length === 4 &&
parts.every((part) => {
const num = Number(part);
return !isNaN(num) && num >= 0 && num <= 255;
})
);
}

export function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (err) {
return false;
}
}
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import jetpack from "fs-jetpack";
import { app, screen } from "electron";
import { DateTime } from "luxon";
import { setPassword, setPort, enableInstaller, updateInstallerStatus } from "./server/installer";
import { initECP, enableECP, updateECPStatus } from "./server/ecp";
import { initECP, enableECP } from "./server/ecp";
import { enableTelnet, updateTelnetStatus } from "./server/telnet";
import {
createMenu,
Expand All @@ -32,6 +32,7 @@ import {
setRemoteKeys,
setThemeSource,
setTimeZone,
updateECPStatus,
} from "./helpers/settings";
import {
createWindow,
Expand Down
20 changes: 0 additions & 20 deletions src/menu/menuService.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,10 @@ export function getChannelIds() {
return recentFiles.ids;
}

export function getPackages() {
return recentFiles.zip;
}

export function getRecentPackage(index) {
return recentFiles.zip[index];
}

export function getRecentId(index) {
const devFile = path.join(app.getPath("userData"), "dev.zip");
if (devFile.hashCode() === recentFiles.ids[index]) {
return "dev";
}
return recentFiles.ids[index];
}

export function getRecentName(index) {
return recentFiles.names[index];
}

export function getRecentVersion(index) {
return recentFiles.versions[index];
}

export function getRecentSource(index) {
return recentFiles.brs[index];
}
Expand Down
Loading

0 comments on commit e2f1489

Please sign in to comment.