diff --git a/packages/find-process/.gitignore b/packages/find-process/.gitignore new file mode 100644 index 00000000..39aca037 --- /dev/null +++ b/packages/find-process/.gitignore @@ -0,0 +1,5 @@ +target +find-process.node +**/node_modules +**/.DS_Store +npm-debug.log* diff --git a/packages/find-process/Cargo.lock b/packages/find-process/Cargo.lock new file mode 100644 index 00000000..89d64b45 --- /dev/null +++ b/packages/find-process/Cargo.lock @@ -0,0 +1,326 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + +[[package]] +name = "either" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" + +[[package]] +name = "find-process" +version = "0.0.1" +dependencies = [ + "neon", + "sysinfo", +] + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "libloading" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883" +dependencies = [ + "cfg-if", + "winapi", +] + +[[package]] +name = "neon" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28e15415261d880aed48122e917a45e87bb82cf0260bb6db48bbab44b7464373" +dependencies = [ + "neon-build", + "neon-macros", + "neon-runtime", + "semver", + "smallvec", +] + +[[package]] +name = "neon-build" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bac98a702e71804af3dacfde41edde4a16076a7bbe889ae61e56e18c5b1c811" + +[[package]] +name = "neon-macros" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7288eac8b54af7913c60e0eb0e2a7683020dffa342ab3fd15e28f035ba897cf" +dependencies = [ + "quote", + "syn", + "syn-mid", +] + +[[package]] +name = "neon-runtime" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4676720fa8bb32c64c3d9f49c47a47289239ec46b4bdb66d0913cc512cb0daca" +dependencies = [ + "cfg-if", + "libloading", + "smallvec", +] + +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rayon" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7237101a77a10773db45d62004a272517633fbcc3df19d96455ede1122e051" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + +[[package]] +name = "smallvec" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn-mid" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea305d57546cc8cd04feb14b62ec84bf17f50e3f7b12560d7bfa9265f39d9ed" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "sysinfo" +version = "0.30.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fb4f3438c8f6389c864e61221cbc97e9bca98b4daf39a5beb7bea660f528bb2" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "libc", + "ntapi", + "once_cell", + "rayon", + "windows", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" +dependencies = [ + "windows-core", + "windows-targets", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" diff --git a/packages/find-process/Cargo.toml b/packages/find-process/Cargo.toml new file mode 100644 index 00000000..2f5afb9d --- /dev/null +++ b/packages/find-process/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "find-process" +version = "0.0.1" +authors = ["ck"] +license = "ISC" +edition = "2018" +exclude = ["find-process.node"] + +[lib] +crate-type = ["cdylib"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sysinfo = "0.30.5" + +[dependencies.neon] +version = "0.10" +default-features = false +features = ["napi-6"] \ No newline at end of file diff --git a/packages/find-process/index.js b/packages/find-process/index.js new file mode 100644 index 00000000..20575b1a --- /dev/null +++ b/packages/find-process/index.js @@ -0,0 +1,3 @@ +const { process_by_name } = require('./find-process.node'); + +module.exports = { process_by_name }; diff --git a/packages/find-process/package.json b/packages/find-process/package.json new file mode 100644 index 00000000..c3895eb7 --- /dev/null +++ b/packages/find-process/package.json @@ -0,0 +1,20 @@ +{ + "name": "@tosu/find-process", + "version": "0.0.1", + "description": "", + "main": "index.js", + "scripts": { + "build-artifact": "cargo-cp-artifact -nc find-process.node -- cargo build --message-format=json-render-diagnostics", + "build-debug": "npm run build --", + "build-release": "npm run build-artifact -- --release", + "prepare": "npm run build-release", + "build": "tsc", + "test": "cargo test" + }, + "keywords": [], + "author": "cyperdark", + "license": "ISC", + "devDependencies": { + "cargo-cp-artifact": "^0.1" + } +} \ No newline at end of file diff --git a/packages/find-process/src/lib.rs b/packages/find-process/src/lib.rs new file mode 100644 index 00000000..0cbfe17e --- /dev/null +++ b/packages/find-process/src/lib.rs @@ -0,0 +1,28 @@ +use neon::prelude::*; +use sysinfo::System; + +fn process_by_name(mut cx: FunctionContext) -> JsResult { + let sys = System::new_all(); + let process_name = cx.argument::(0)?.value(&mut cx); + let obj = cx.empty_object(); + + for process in sys.processes_by_name(&process_name) { + let id = cx.number(process.pid().as_u32()); + let name = cx.string(process.name().to_string()); + let cmd = cx.string(process.cmd()[0].to_string()); + + let _ = obj.set(&mut cx, "pid", id); + let _ = obj.set(&mut cx, "name", name); + let _ = obj.set(&mut cx, "cmd", cmd); + + return Ok(obj.upcast()); + } + + Ok(cx.null().upcast()) +} + +#[neon::main] +fn main(mut cx: ModuleContext) -> NeonResult<()> { + cx.export_function("process_by_name", process_by_name)?; + Ok(()) +} diff --git a/packages/tosu/package.json b/packages/tosu/package.json index 783ec3b0..e872488c 100644 --- a/packages/tosu/package.json +++ b/packages/tosu/package.json @@ -15,10 +15,12 @@ "@fastify/static": "^6.12.0", "@fastify/websocket": "^8.3.0", "@kotrikd/rosu-pp": "^0.10.0", + "@tosu/common": "workspace:*", + "@tosu/find-process": "workspace:*", + "@tosu/updater": "workspace:*", "@types/ws": "^8.5.10", "@vercel/ncc": "^0.38.1", "fastify": "^4.25.1", - "find-process": "^1.4.7", "game-overlay": "workspace:*", "genversion": "^3.1.1", "osu-catch-stable": "^4.0.0", diff --git a/packages/tosu/src/objects/instanceManager/instanceManager.ts b/packages/tosu/src/objects/instanceManager/instanceManager.ts index efc0b0f5..567b627e 100644 --- a/packages/tosu/src/objects/instanceManager/instanceManager.ts +++ b/packages/tosu/src/objects/instanceManager/instanceManager.ts @@ -1,5 +1,5 @@ import { sleep } from '@tosu/common'; -import findProcess from 'find-process'; +import { process_by_name } from '@tosu/find-process'; import { OsuInstance } from './osuInstance'; @@ -23,33 +23,36 @@ export class InstanceManager { delete this.osuInstances[pid]; } + private lookupProcess() { + const osuProcess = process_by_name('osu!.exe'); + if (osuProcess == null || osuProcess?.pid == null) { + return null; + } + + if (osuProcess.pid in this.osuInstances) { + return 'old'; + } + + const osuInstance = new OsuInstance(osuProcess.pid); + if (osuProcess.cmd.includes('-spectateclient')) { + osuInstance.setIsTourneySpectator(true); + } + + osuInstance.emitter.on('onDestroy', this.onProcessDestroy.bind(this)); + osuInstance.emitter.on( + 'onResolveFailed', + this.onProcessDestroy.bind(this) + ); + + this.osuInstances[osuProcess.pid] = osuInstance; + osuInstance.start(); + + return osuProcess.pid; + } + async runWatcher() { while (true) { - const osuProcesses = await findProcess('name', OSU_REGEX); - for (const process of osuProcesses) { - if (process.pid in this.osuInstances) { - // dont deploy not needed instances - continue; - } - - const osuInstance = new OsuInstance(process.pid); - if (process.cmd.includes('-spectateclient')) { - osuInstance.setIsTourneySpectator(true); - } - - osuInstance.emitter.on( - 'onDestroy', - this.onProcessDestroy.bind(this) - ); - osuInstance.emitter.on( - 'onResolveFailed', - this.onProcessDestroy.bind(this) - ); - - this.osuInstances[process.pid] = osuInstance; - osuInstance.start(); - } - + this.lookupProcess(); await sleep(5000); } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e4680adb..4f048d8c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -43,6 +43,12 @@ importers: specifier: ^3.8.2 version: 3.11.0 + packages/find-process: + devDependencies: + cargo-cp-artifact: + specifier: ^0.1 + version: 0.1.8 + packages/gameOverlay: dependencies: '@tosu/common': @@ -72,6 +78,9 @@ importers: '@tosu/common': specifier: workspace:* version: link:../common + '@tosu/find-process': + specifier: workspace:* + version: link:../find-process '@tosu/updater': specifier: workspace:* version: link:../updater @@ -84,9 +93,6 @@ importers: fastify: specifier: ^4.25.1 version: 4.25.1 - find-process: - specifier: ^1.4.7 - version: 1.4.7 game-overlay: specifier: workspace:* version: link:../gameOverlay @@ -1585,17 +1591,6 @@ packages: parents: 1.0.1 dev: false - /find-process@1.4.7: - resolution: {integrity: sha512-/U4CYp1214Xrp3u3Fqr9yNynUrr5Le4y0SsJh2lMDDSbpwYSz3M2SMWQC+wqcx79cN8PQtHQIL8KnuY9M66fdg==} - hasBin: true - dependencies: - chalk: 4.1.2 - commander: 5.1.0 - debug: 4.3.4 - transitivePeerDependencies: - - supports-color - dev: false - /find-up@2.1.0: resolution: {integrity: sha512-NWzkk0jSJtTt08+FBFMvXoeZnOJD+jTtsRmBYbAIzJdX6l7dLgR7CTubCM5/eDdPUBvLCeVasP1brfVR/9/EZQ==} engines: {node: '>=4'}