Skip to content

Commit

Permalink
feat: Make enabling ingame overlay a little bit better
Browse files Browse the repository at this point in the history
  • Loading branch information
cyperdark committed Mar 6, 2024
1 parent ac5c286 commit 7338af4
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 227 deletions.
29 changes: 22 additions & 7 deletions packages/common/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export const config = {
enableGosuOverlay: (process.env.ENABLE_GOSU_OVERLAY || '') === 'true'
};

let HTTPServer: any;

export const updateConfigFile = () => {
let newOptions = '';

Expand Down Expand Up @@ -115,15 +117,17 @@ export const updateConfigFile = () => {
};

export const watchConfigFile = ({ httpServer }: { httpServer: any }) => {
refreshConfig(httpServer, false);
HTTPServer = httpServer;

refreshConfig(false);
updateConfigFile();

fs.watchFile(configPath, (current, previous) => {
refreshConfig(httpServer, true);
fs.watchFile(configPath, () => {
refreshConfig(true);
});
};

export const refreshConfig = (httpServer: any, refresh: boolean) => {
export const refreshConfig = (refresh: boolean) => {
let updated = false;
const status = refresh == true ? 'reload' : 'load';

Expand Down Expand Up @@ -159,7 +163,18 @@ export const refreshConfig = (httpServer: any, refresh: boolean) => {
config.serverIP = serverIP;
config.serverPort = serverPort;

httpServer.restart();
HTTPServer.restart();
}

const osuInstances: any = Object.values(
HTTPServer.instanceManager.osuInstances || {}
);
if (
osuInstances.length == 1 &&
enableGosuOverlay == true &&
updated == true
) {
osuInstances[0].injectGameOverlay();
}

config.debugLogging = debugLogging;
Expand All @@ -181,7 +196,7 @@ export const refreshConfig = (httpServer: any, refresh: boolean) => {
if (updated) wLogger.info(`Config ${status}ed`);
};

export const writeConfig = (httpServer: any, text: string) => {
export const writeConfig = (text: string) => {
fs.writeFileSync(configPath, text, 'utf8');
refreshConfig(httpServer, true);
refreshConfig(true);
};
1 change: 0 additions & 1 deletion packages/game-overlay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"build": "tsc"
},
"dependencies": {
"decompress": "^4.2.1",
"tsprocess": "workspace:*",
"@tosu/common": "workspace:*"
}
Expand Down
49 changes: 38 additions & 11 deletions packages/game-overlay/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { downloadFile, wLogger } from '@tosu/common';
import decompress from 'decompress';
import { downloadFile, unzip, wLogger } from '@tosu/common';
import { execFile } from 'node:child_process';
import { existsSync, writeFileSync } from 'node:fs';
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { mkdir, rm } from 'node:fs/promises';
import path from 'node:path';
import { Process } from 'tsprocess/dist/process';

const configPath = path.join(process.cwd(), 'config.ini');
const checkGameOverlayConfig = () => {
const configPath = path.join(process.cwd(), 'config.ini');
if (!existsSync(configPath)) {
writeFileSync(
configPath,
`[GameOverlay]; https://github.com/l3lackShark/gosumemory/wiki/GameOverlay
enabled = false
enabled = true
gameWidth = 1920
gameHeight = 1080
overlayURL = http://127.0.0.1:24050/InGame2/index.html
overlayURL =
overlayWidth = 380
overlayHeight = 110
overlayOffsetX = 0
Expand All @@ -25,11 +24,28 @@ overlayScale = 10`
}
};

const checkGosuConfig = (p: Process, checking?: boolean) => {
if (!existsSync(configPath)) return null;

const read = readFileSync(configPath, 'utf8');
const parseURL = /overlayURL[ ]*=[ ]*(.*)/.exec(read);
if (!parseURL || !parseURL?.[1]) {
setTimeout(() => {
checkGosuConfig(p, true);
}, 1000);
return 'empty';
}

if (checking == true) injectGameOverlay(p);
return 'specified';
};

export const injectGameOverlay = async (p: Process) => {
if (process.platform !== 'win32') {
throw new Error(
'Gameoverlay can run only under windows, sorry linux/darwin user!'
wLogger.error(
`[gosu-overlay] Ingame overlay can run only under windows, sorry linux/darwin user!`
);
return;
}

// Check for DEPRECATED GOSU CONFIG, due its needed to read [GameOverlay] section from original configuration
Expand All @@ -44,7 +60,8 @@ export const injectGameOverlay = async (p: Process) => {
'https://dl.kotworks.cyou/gosu-gameoverlay.zip',
archivePath
);
await decompress(archivePath, gameOverlayPath);

await unzip(archivePath, gameOverlayPath);
await rm(archivePath);
}

Expand All @@ -53,8 +70,18 @@ export const injectGameOverlay = async (p: Process) => {
path.join(process.cwd(), 'gameOverlay', 'gosumemoryoverlay.dll')
)
) {
wLogger.info('Please delete gameOverlay folder, and restart program!');
process.exit(1);
wLogger.info(
'[gosu-overlay] Please delete gameOverlay folder, and restart program!'
);
return;
}

const overlayURLstatus = checkGosuConfig(p);
if (overlayURLstatus == 'empty') {
wLogger.warn(
`[gosu-overlay] Specify overlayURL for gameOverlay in config.ini`
);
return;
}

return await new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export default function buildBaseApi(app: HttpServer) {
});
}

writeConfig(app.Server, body);
writeConfig(body);

sendJson(res, {
status: 'updated'
Expand Down
8 changes: 4 additions & 4 deletions packages/tosu/src/entities/AllTimesData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ export class AllTimesData extends AbstractEntity {

if (this.configStateErrorAttempts > 5) {
wLogger.error(
"ATD(updateConfigState) Can't update config state",
"ATD(updateConfigState) Can't update config state",
exc
);
);
}
}
}
Expand Down Expand Up @@ -421,9 +421,9 @@ export class AllTimesData extends AbstractEntity {

if (this.bindingStateErrorAttempts > 5) {
wLogger.error(
"ATD(updateBindingState) Can't update binding state",
"ATD(updateBindingState) Can't update binding state",
exc
);
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tosu/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { InstanceManager } from './objects/instanceManager/instanceManager';
const instanceManager = new InstanceManager();
const httpServer = new Server({ instanceManager });

httpServer.start();
watchConfigFile({ httpServer });

const { update } = argumetsParser(process.argv);
Expand All @@ -23,5 +24,4 @@ import { InstanceManager } from './objects/instanceManager/instanceManager';
wLogger.info('Searching for osu!');

instanceManager.runWatcher();
httpServer.start();
})();
16 changes: 10 additions & 6 deletions packages/tosu/src/objects/instanceManager/osuInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class OsuInstance {
this.isTourneySpectator = newVal;
}

async start() {
start() {
wLogger.info(
`Running memory chimera... RESOLVING PATTERNS FOR ${this.pid}`
);
Expand Down Expand Up @@ -205,7 +205,7 @@ export class OsuInstance {
* ENABLING GOSU OVERLAY
*/
if (config.enableGosuOverlay) {
await injectGameOverlay(this.process);
this.injectGameOverlay();
}

this.update();
Expand All @@ -216,6 +216,10 @@ export class OsuInstance {
this.watchProcessHealth();
}

async injectGameOverlay() {
await injectGameOverlay(this.process);
}

async update() {
wLogger.debug('OI(update) starting');

Expand Down Expand Up @@ -429,9 +433,9 @@ export class OsuInstance {
beatmapPpData.updateMapMetadata(currentMods);
} catch (exc) {
wLogger.error(
"OI(updateMapMetadata) Can't update beatmap metadata",
"OI(updateMapMetadata) Can't update beatmap metadata",
exc
);
);
}
}

Expand All @@ -452,8 +456,8 @@ export class OsuInstance {
if (!Process.isProcessExist(this.process.handle)) {
this.isDestroyed = true;
wLogger.info(
`OI(watchProcessHealth) osu!.exe at ${this.pid} got destroyed`
);
`OI(watchProcessHealth) osu!.exe at ${this.pid} got destroyed`
);
this.emitter.emit('onDestroy', this.pid);
}

Expand Down
Loading

0 comments on commit 7338af4

Please sign in to comment.