Skip to content

Commit

Permalink
feat: Allow running both lazer and stable, and change data accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
cyperdark committed Nov 1, 2024
1 parent bcf624d commit c6204ba
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 40 deletions.
32 changes: 20 additions & 12 deletions packages/common/utils/platforms.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
export function platformResolver(platform: string) {
let platformType = '';
let platformFileType = '';
let platformCommand = '';
export type Platform = 'windows' | 'linux' | 'macos' | 'unknown';

interface IPlatform {
type: Platform;
fileType: string;
command: string;
}

export function platformResolver(platform: string): IPlatform {
let type: Platform = 'unknown';
let fileType = '';
let command = '';

switch (platform) {
case 'win32':
platformType = 'windows';
platformFileType = '.exe';
platformCommand = 'start ""';
type = 'windows';
fileType = '.exe';
command = 'start ""';
break;

case 'linux':
platformType = 'linux';
platformCommand = 'xdg-open';
type = 'linux';
command = 'xdg-open';
break;

case 'darwin':
platformType = 'macos';
platformCommand = 'open -R';
type = 'macos';
command = 'open -R';
break;
}

return { platformType, platformFileType, platformCommand };
return { type, fileType, command };
}
12 changes: 8 additions & 4 deletions packages/server/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const pkgAssetsPath =

export default function buildBaseApi(server: Server) {
server.app.route('/json', 'GET', (req, res) => {
const osuInstance: any = req.instanceManager.getInstance();
const osuInstance: any = req.instanceManager.getInstance(
req.instanceManager.focusedClient
);
if (!osuInstance) {
res.statusCode = 500;
wLogger.debug('/json', 'not_ready');
Expand Down Expand Up @@ -175,8 +177,8 @@ export default function buildBaseApi(server: Server) {
`PP Counter opened: ${folderName} (${req.headers.referer})`
);

const { platformCommand } = platformResolver(process.platform);
exec(`${platformCommand} "${folderPath}"`, (err) => {
const platform = platformResolver(process.platform);
exec(`${platform.command} "${folderPath}"`, (err) => {
if (err) {
wLogger.error('Error opening file explorer:');
wLogger.debug(err);
Expand Down Expand Up @@ -393,7 +395,9 @@ export default function buildBaseApi(server: Server) {
try {
const query: any = req.query;

const osuInstance: any = req.instanceManager.getInstance();
const osuInstance: any = req.instanceManager.getInstance(
req.instanceManager.focusedClient
);
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
Expand Down
4 changes: 3 additions & 1 deletion packages/server/router/scApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { beatmapFileShortcut } from '../scripts/beatmapFile';

export default function buildSCApi(app: HttpServer) {
app.route('/json/sc', 'GET', (req, res) => {
const osuInstance: any = req.instanceManager.getInstance();
const osuInstance: any = req.instanceManager.getInstance(
req.instanceManager.focusedClient
);
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
Expand Down
4 changes: 3 additions & 1 deletion packages/server/router/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default function buildV1Api(app: HttpServer) {
try {
const url = req.pathname || '/';

const osuInstance: any = req.instanceManager.getInstance();
const osuInstance: any = req.instanceManager.getInstance(
req.instanceManager.focusedClient
);
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
Expand Down
16 changes: 12 additions & 4 deletions packages/server/router/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { directoryWalker } from '../utils/directories';

export default function buildV2Api(app: HttpServer) {
app.route('/json/v2', 'GET', (req, res) => {
const osuInstance: any = req.instanceManager.getInstance();
const osuInstance: any = req.instanceManager.getInstance(
req.instanceManager.focusedClient
);
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
Expand All @@ -18,7 +20,9 @@ export default function buildV2Api(app: HttpServer) {
});

app.route('/json/v2/precise', 'GET', (req, res) => {
const osuInstance: any = req.instanceManager.getInstance();
const osuInstance: any = req.instanceManager.getInstance(
req.instanceManager.focusedClient
);
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
Expand All @@ -38,7 +42,9 @@ export default function buildV2Api(app: HttpServer) {
try {
const url = req.pathname || '/';

const osuInstance: any = req.instanceManager.getInstance();
const osuInstance: any = req.instanceManager.getInstance(
req.instanceManager.focusedClient
);
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
Expand Down Expand Up @@ -69,7 +75,9 @@ export default function buildV2Api(app: HttpServer) {
try {
const url = req.pathname || '/';

const osuInstance: any = req.instanceManager.getInstance();
const osuInstance: any = req.instanceManager.getInstance(
req.instanceManager.focusedClient
);
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
Expand Down
4 changes: 3 additions & 1 deletion packages/server/scripts/beatmapFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export function beatmapFileShortcut(
try {
const url = req.pathname || '/';

const osuInstance: any = req.instanceManager.getInstance();
const osuInstance: any = req.instanceManager.getInstance(
req.instanceManager.focusedClient
);
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
Expand Down
4 changes: 3 additions & 1 deletion packages/server/utils/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ export class Websocket {
startLoop() {
this.loopInterval = setInterval(() => {
try {
const osuInstance: any = this.instanceManager.getInstance();
const osuInstance: any = this.instanceManager.getInstance(
this.instanceManager.focusedClient
);
if (!osuInstance || this.clients.size === 0) {
return; // Exit the loop if conditions are not met
}
Expand Down
4 changes: 3 additions & 1 deletion packages/tosu/src/api/utils/buildResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const convertMemoryPlayerToResult = (
});

export const buildResult = (instanceManager: InstanceManager): ApiAnswer => {
const osuInstance = instanceManager.getInstance();
const osuInstance = instanceManager.getInstance(
instanceManager.focusedClient
);
if (!osuInstance) {
return { error: 'not_ready' };
}
Expand Down
4 changes: 3 additions & 1 deletion packages/tosu/src/api/utils/buildResultSC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { fixDecimals } from '@/utils/converters';
import { ApiAnswer } from '../types/sc';

export const buildResult = (instanceManager: InstanceManager): ApiAnswer => {
const osuInstance = instanceManager.getInstance();
const osuInstance = instanceManager.getInstance(
instanceManager.focusedClient
);
if (!osuInstance) {
return { error: 'not_ready' };
}
Expand Down
4 changes: 3 additions & 1 deletion packages/tosu/src/api/utils/buildResultV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ const convertMemoryPlayerToResult = (
};

export const buildResult = (instanceManager: InstanceManager): ApiAnswer => {
const osuInstance = instanceManager.getInstance();
const osuInstance = instanceManager.getInstance(
instanceManager.focusedClient
);
if (!osuInstance) {
return { error: 'not_ready' };
}
Expand Down
4 changes: 3 additions & 1 deletion packages/tosu/src/api/utils/buildResultV2Precise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ const buildTourneyData = (
};

export const buildResult = (instanceManager: InstanceManager): ApiAnswer => {
const osuInstance = instanceManager.getInstance();
const osuInstance = instanceManager.getInstance(
instanceManager.focusedClient
);
if (!osuInstance) {
return { error: 'not_ready' };
}
Expand Down
1 change: 1 addition & 0 deletions packages/tosu/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ const currentVersion = require(process.cwd() + '/_version.js');

httpServer.start();
instanceManager.runWatcher();
instanceManager.runDetemination();
})();
64 changes: 60 additions & 4 deletions packages/tosu/src/instances/manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { argumetsParser, wLogger } from '@tosu/common';
import {
ClientType,
Platform,
argumetsParser,
platformResolver,
wLogger
} from '@tosu/common';
import { Process } from 'tsprocess/dist/process';

import { AbstractInstance } from '@/instances';
Expand All @@ -7,6 +13,9 @@ import { LazerInstance } from './lazerInstance';
import { OsuInstance } from './osuInstance';

export class InstanceManager {
platformType: Platform;
focusedClient: ClientType;

osuInstances: {
[key: number]: AbstractInstance;
};
Expand All @@ -15,19 +24,33 @@ export class InstanceManager {
this.osuInstances = {};

this.runWatcher = this.runWatcher.bind(this);
this.runDetemination = this.runDetemination.bind(this);
}

/**
* Gets a regular instance if osu running in normal mode, else gets tournament manager
*/
public getInstance() {
public getInstance(clientType?: ClientType) {
if (Object.keys(this.osuInstances).length === 0) return;

for (const key in this.osuInstances) {
if (this.osuInstances[key].isTourneyManager) {
return this.osuInstances[key];
const instance = this.osuInstances[key];
const ClientFilter =
typeof ClientType[clientType as any] !== 'undefined'
? instance.client === clientType
: true;

if (instance.isTourneyManager && ClientFilter) {
return instance;
}
}

if (typeof clientType === 'number') {
const search = Object.values(this.osuInstances).find(
(r) => r.client === clientType
);
return search;
}
return Object.values(this.osuInstances)[0];
}

Expand Down Expand Up @@ -100,4 +123,37 @@ export class InstanceManager {

setTimeout(this.runWatcher, 1000);
}

runDetemination() {
if (!this.platformType) {
const platform = platformResolver(process.platform);
this.platformType = platform.type;
}

if (this.platformType !== 'windows') return;

const s1 = performance.now();
const focusedPID = Process.getFocusedProcess();
const instance = Object.values(this.osuInstances).find(
(r) => r.pid === focusedPID
);
if (instance) this.focusedClient = instance.client;

if (this.focusedClient === undefined) {
this.focusedClient =
this.getInstance()?.client || ClientType.stable;
}

console.log(
'focused',
ClientType[this.focusedClient],
Object.values(this.osuInstances).map((r) => ({
id: r.pid,
client: r.client
})),
performance.now() - s1
);

setTimeout(this.runDetemination, 100);
}
}
12 changes: 4 additions & 8 deletions packages/updater/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const platform = platformResolver(process.platform);
const fileDestination = path.join(getProgramPath(), 'update.zip');
const backupExecutablePath = path.join(
getProgramPath(),
`tosu_old${platform.platformFileType}`
`tosu_old${platform.fileType}`
);

const deleteNotLocked = async (filePath: string) => {
Expand All @@ -41,7 +41,7 @@ export const checkUpdates = async () => {
wLogger.info('Checking updates');

try {
if (platform.platformType === '') {
if (platform.type === 'unknown') {
wLogger.warn(
`Unsupported platform (${process.platform}). Unable to run updater`
);
Expand Down Expand Up @@ -107,14 +107,10 @@ export const autoUpdater = async () => {
}

const findAsset = assets.find(
(r) =>
r.name.includes(platform.platformType) &&
r.name.endsWith('.zip')
(r) => r.name.includes(platform.type) && r.name.endsWith('.zip')
);
if (!findAsset) {
wLogger.info(
`Files to update not found (${platform.platformType})`
);
wLogger.info(`Files to update not found (${platform.type})`);
return 'noFiles';
}

Expand Down

0 comments on commit c6204ba

Please sign in to comment.