From 0b2d3f85aab6e663507f34a3cad42b4766ad389b Mon Sep 17 00:00:00 2001 From: Armaldio Date: Fri, 10 Jan 2025 18:00:27 +0100 Subject: [PATCH] improvements --- TODO | 2 + assets/electron/template/app/src/index.js | 37 +++++++++------ src/main.ts | 5 ++ src/main/handler-func.ts | 3 +- .../libs/plugin-construct/assets/script.ts | 47 +++++++++++++++++-- .../libs/plugin-construct/export-shared.ts | 31 ++++++++---- 6 files changed, 96 insertions(+), 29 deletions(-) diff --git a/TODO b/TODO index 06ad18b..517fa73 100644 --- a/TODO +++ b/TODO @@ -64,3 +64,5 @@ Filesystem plugin: overwrite parameter + better sentences/descriptions Convert dektop app to web app + node backend Communication via rest api (for now) or websocket + +yin: test c3p need manual exporting instead of automated because of bundled addons diff --git a/assets/electron/template/app/src/index.js b/assets/electron/template/app/src/index.js index 0aa3708..387df74 100644 --- a/assets/electron/template/app/src/index.js +++ b/assets/electron/template/app/src/index.js @@ -111,24 +111,28 @@ if (config.enableSteamSupport) { * @param {BrowserWindow} mainWindow * @returns {Promise} */ -const createAppServer = (mainWindow) => { +const createAppServer = (mainWindow, serveStatic = true) => { // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve) => { // @ts-expect-error import.meta const dir = app.isPackaged ? join(import.meta.dirname, './app') : './src/app' - const server = createServer((req, res) => { - return serve(req, res, { - maxAge: 0, - public: dir, - rewrites: [ - { - source: 'sw.js', - destination: 'index.html' - } - ] + const server = createServer() + + if (serveStatic) { + server.on('request', (req, res) => { + return serve(req, res, { + maxAge: 0, + public: dir, + rewrites: [ + { + source: 'sw.js', + destination: 'index.html' + } + ] + }) }) - }) + } const wss = new WebSocketServer({ server }) wss.on('connection', function connection(ws) { @@ -315,11 +319,12 @@ const createWindow = async () => { if (argUrl) { console.log('argUrl', argUrl) - const port = await createAppServer(mainWindow) + const port = await createAppServer(mainWindow, false) console.log('port', port) await mainWindow?.loadURL(argUrl) + console.log('URL loaded') } else { const port = await createAppServer(mainWindow) @@ -327,6 +332,8 @@ const createWindow = async () => { await mainWindow?.loadURL(`http://localhost:${port}`) } + + return mainWindow } const registerHandlers = async () => { @@ -339,7 +346,9 @@ const registerHandlers = async () => { app.whenReady().then(async () => { await registerHandlers() - await createWindow() + const mainWindow = await createWindow() + + mainWindow.show() app.on('activate', async () => { if (BrowserWindow.getAllWindows().length === 0) { diff --git a/src/main.ts b/src/main.ts index d2f5729..40f2f79 100644 --- a/src/main.ts +++ b/src/main.ts @@ -212,6 +212,9 @@ exec "${process.execPath}" "$@" output: { type: 'string', short: 'o' + }, + inspect: { + type: 'boolean' } } } satisfies ParseArgsConfig @@ -222,6 +225,8 @@ exec "${process.execPath}" "$@" createWindow() + delete values['inspect'] + // exit if values are passed if (Object.keys(values).length > 0) { logger().info('Processing graph...') diff --git a/src/main/handler-func.ts b/src/main/handler-func.ts index f7fa3d0..ccc185f 100644 --- a/src/main/handler-func.ts +++ b/src/main/handler-func.ts @@ -33,7 +33,8 @@ const checkParams = (definitionParams: InputsDefinition, elementParams: Record x !== param) } else { - throw new Error('Unexpected param "' + param + '"') + // throw new Error('Unexpected param "' + param + '"') + console.warn('Unexpected param "' + param + '"') } } diff --git a/src/shared/libs/plugin-construct/assets/script.ts b/src/shared/libs/plugin-construct/assets/script.ts index b1ee9db..26636fe 100644 --- a/src/shared/libs/plugin-construct/assets/script.ts +++ b/src/shared/libs/plugin-construct/assets/script.ts @@ -1,5 +1,6 @@ import { Page } from 'playwright' -import { join } from 'node:path' +import { join, extname } from 'node:path' +import { readdir } from 'node:fs/promises' const registerInstallButtonListener = (page: Page, log: typeof console.log) => { // as soon as it appear, without blocking flow @@ -71,7 +72,7 @@ const registerMissingAddonErrorListener = (page: Page, log: typeof console.log) timeout: 0 }) .then(async () => { - throw new Error("Missing addon. You should bundle addons with your project") + throw new Error('Missing addon. You should bundle addons with your project') }) .catch(async () => { log('webglErrorButton.click() failed') @@ -85,7 +86,8 @@ export const script = async ( username: string | undefined, password: string | undefined, version: string | undefined, - downloadDir: string + downloadDir: string, + // addonsFolder: string | undefined ) => { let url = 'https://editor.construct.net/' if (version) { @@ -99,6 +101,42 @@ export const script = async ( await page.getByText('No thanks, not now').click() log('Clicked No thanks button') + // if (addonsFolder) { + // const _files = await readdir(addonsFolder) + // const addonFiles = _files + // .filter((x) => extname(x) === '.c3addon') + // .map((x) => join(addonsFolder, x)) + // console.log('addonFiles', addonFiles) + + // await page.pause() + // await page.getByRole('button', { name: 'Menu' }).click(); + // await page.mouse.move(30, 150) + // await page.mouse.move(150, 100) + // await page.mouse.click(150, 100); + + // const [fileChooserAddons] = await Promise.all([ + // page.waitForEvent('filechooser'), + // await page.getByRole('button', { name: 'Install new addon...' }).click() + // ]) + + // await fileChooserAddons.setFiles(addonFiles) + + // // await page.pause() + // // if (addonFiles.length > 0) { + // // for (let i = 0; i < addonFiles.length - 1; i += 1) { + // // const [fileChooser] = await Promise.all([ + // // page.waitForEvent('filechooser'), + // // page.keyboard.press('ControlOrMeta+O') + // // ]) + + // // await fileChooser.setFiles(addonFiles[i]) + // // log('Set addon files', addonFiles[i]) + + // // await page.pause() + // // } + // // } + // } + const [fileChooser] = await Promise.all([ page.waitForEvent('filechooser'), page.keyboard.press('ControlOrMeta+O') @@ -193,10 +231,9 @@ export const script = async ( .click() log('"Web" clicked') - await page.locator('#exportSelectPlatformDialog').getByRole('button', { name: 'Next' }).click() - await page.getByLabel('Offline support').uncheck(); + await page.getByLabel('Offline support').uncheck() log('Disabled offline support') await page.locator('#exportStandardOptionsDialog').getByRole('button', { name: 'Next' }).click() diff --git a/src/shared/libs/plugin-construct/export-shared.ts b/src/shared/libs/plugin-construct/export-shared.ts index b1a3b55..bcf91d2 100644 --- a/src/shared/libs/plugin-construct/export-shared.ts +++ b/src/shared/libs/plugin-construct/export-shared.ts @@ -69,8 +69,21 @@ export const sharedParams = { } }, value: 120, - label: 'Tiemout' - } + label: 'Timeout' + }, + // addonsFolder: { + // description: 'Folder containing addons to import in the editor', + // required: false, + // control: { + // type: 'path', + // options: { + // buttonLabel: 'Addons folder', + // properties: ['openDirectory'] + // } + // }, + // value: '', + // label: 'Addons folder' + // } } satisfies InputsDefinition type Inputs = ParamsToInput @@ -81,6 +94,8 @@ export const exportc3p = async ( ) => { const newInputs = inputs as Inputs + // const { addonsFolder } = newInputs + const playwright = await import('playwright') const { join } = await import('node:path') @@ -115,11 +130,7 @@ export const exportc3p = async ( log('newInputs', newInputs) - // must run in firefox because otherwise - // the local file system access api of chrome take precedence - // https://github.com/microsoft/playwright/issues/18267 const browserInstance = playwright[browserName] - // const { firefox } = playwright; const version = newInputs.version const headless = newInputs.headless @@ -160,17 +171,19 @@ export const exportc3p = async ( newInputs.username, newInputs.password, version, - downloadDir + downloadDir, + // addonsFolder, ) log('setting output result to ', result) setOutput('folder', result) } catch (e) { - log('no result, crashed') + log('error, no result, crashed', e) throw new Error('ConstructExport failed: ' + e.message) } finally { - await browser.close() + await context.browser().close() + await context.close() } }