Skip to content

Commit

Permalink
feat: use fastify instead of koa (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
KotRikD committed Dec 18, 2023
1 parent 49afd72 commit edebd3c
Show file tree
Hide file tree
Showing 15 changed files with 669 additions and 692 deletions.
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"configurations": [
{
"type": "node",
"name": "run tosumemory:dev",
"name": "run tosu",
"request": "launch",
"args": ["src/index.ts"],
"outputCapture": "std",
"runtimeArgs": ["--nolazy", "--inspect", "-r", "ts-node/register", "-r", "tsconfig-paths/register"],
"cwd": "${workspaceRoot}",
"cwd": "${workspaceRoot}/packages/tosu",
},
{
"type": "node",
Expand All @@ -20,7 +20,7 @@
"args": ["${relativeFile}"],
"outputCapture": "std",
"runtimeArgs": ["--nolazy", "--inspect", "-r", "ts-node/register", "-r", "tsconfig-paths/register"],
"cwd": "${workspaceRoot}",
"cwd": "${workspaceRoot}/packages/tosu",
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You need unpack it to folder, and if you need overlays, download the [Blackshark

## Prerequisites

- typescript >=4.9.5
- typescript >=5.3.3
- node >=18.14.2

## Install
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
},
"dependencies": {
"@types/node": "^18.14.6",
"tsconfig-paths": "^3.14.2",
"typescript": "^4.9.5"
"tsconfig-paths": "^4.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
Expand All @@ -21,8 +21,7 @@
},
"lint-staged": {
"**/*.{js,ts}": [
"pnpm run prettier:fix",
"git add"
"pnpm run prettier:fix"
]
},
"homepage": "https://github.com/KotRikD/tosu#readme",
Expand Down
17 changes: 7 additions & 10 deletions packages/tosu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
"compile": "pnpm run ts:compile && pkg -t node18-win-x64 --output dist/tosu.exe --debug --config pkg.json --compress brotli dist/index.js && pnpm run ts:run src/postBuild.ts"
},
"dependencies": {
"@types/koa__router": "^12.0.0",
"@koa/router": "^12.0.0",
"@types/koa": "^2.13.5",
"@vercel/ncc": "^0.36.1",
"@fastify/send": "^2.1.0",
"@fastify/static": "^6.12.0",
"@fastify/websocket": "^8.3.0",
"@kotrikd/rosu-pp": "^0.10.0",
"@types/ws": "^8.5.10",
"@vercel/ncc": "^0.38.1",
"dotenv": "^16.0.3",
"fastify": "^4.25.1",
"find-process": "^1.4.7",
"game-overlay": "workspace:*",
"koa": "^2.14.1",
"koa-mount": "^4.0.0",
"koa-send": "^5.0.1",
"koa-static": "^5.0.0",
"koa-websocket": "^7.0.0",
"osu-catch-stable": "^4.0.0",
"osu-classes": "^3.0.0",
"osu-mania-stable": "^5.0.0",
Expand All @@ -30,7 +28,6 @@
"osu-taiko-stable": "^5.0.0",
"pkg": "^5.8.0",
"resedit": "^2.0.0",
"rosu-pp": "^0.9.4",
"ts-node": "^10.9.1",
"tsprocess": "workspace:*",
"winston": "^3.8.2"
Expand Down
7 changes: 7 additions & 0 deletions packages/tosu/src/@types/fastify.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { InstanceManager } from '@/objects/instanceManager/instanceManager';

declare module 'fastify' {
interface FastifyRequest {
instanceManager: InstanceManager;
}
}
54 changes: 54 additions & 0 deletions packages/tosu/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fastifyStatic from '@fastify/static';
import fastifyWebsocket from '@fastify/websocket';
import { fastify } from 'fastify';
import { readdir } from 'node:fs/promises';
import path from 'path';

import { buildV1Router } from '@/api/router/gosu/v1';
import { config } from '@/config';
import { OVERLAYS_STATIC } from '@/constants/overlaysStatic';
import { InstanceManager } from '@/objects/instanceManager/instanceManager';

export const buildFastifyApp = async (instanceManager: InstanceManager) => {
const app = fastify({
logger: false
});

// register websocket plugin
await app.register(fastifyWebsocket);

// apply instanceManager to request
app.addHook('onRequest', async (req) => {
req.instanceManager = instanceManager;
});
// apply cors rules to request
app.addHook('onRequest', async (req, reply) => {
reply.header('Access-Control-Allow-Origin', '*');
reply.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
reply.header(
'Access-Control-Allow-Methods',
'POST, GET, PUT, DELETE, OPTIONS'
);
});

// ---- REGISTERING STATIC ----
app.register(fastifyStatic, {
root: path.join(process.cwd(), 'static'),
prefix: '/' // optional: default '/'
});
const staticReply = (_, reply) => {
reply.header('content-type', 'html');
reply.send(OVERLAYS_STATIC);
};
app.get('/', staticReply);
app.get('/api/getOverlays', async (_, reply) => {
reply.send(await readdir(config.staticFolderPath));
});

buildV1Router(app);

return app;
};
79 changes: 79 additions & 0 deletions packages/tosu/src/api/router/gosu/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// @ts-nocheck
import send from '@fastify/send';
import { FastifyInstance } from 'fastify';

import { config } from '@/config';
import { wLogger } from '@/logger';
import { sleep } from '@/utils/sleep';

export const buildV1Router = (app: FastifyInstance) => {
app.register(async (app) => {
app.get('/ws', { websocket: true }, async (connection, req) => {
wLogger.debug('>>> ws: CONNECTED');
let isSocketConnected = true;

connection.socket.on('close', () => {
isSocketConnected = false;
wLogger.debug('>>> ws: CLOSED');
});

while (isSocketConnected) {
if (Object.keys(req.instanceManager.osuInstances).length < 1) {
await sleep(500);
continue;
}

connection.socket.send(
JSON.stringify(
Object.values(
req.instanceManager.osuInstances
)[0].getState(req.instanceManager)
)
);
await sleep(config.wsSendInterval);
}
});
});

app.get('/json', (req, reply) => {
if (Object.keys(req.instanceManager.osuInstances).length < 1) {
reply.code(500);
reply.send(null);
return;
}

reply.send(
Object.values(req.instanceManager.osuInstances)[0].getState(
req.instanceManager
)
);
});

app.get('/Songs/*', async (req, reply) => {
if (Object.keys(req.instanceManager.osuInstances).length < 1) {
reply.code(500);
reply.send(null);
return;
}

const { settings } = Object.values(
req.instanceManager.osuInstances
)[0].entities.getServices(['settings']);

if (settings.songsFolder === '') {
reply.code(404);
reply.send({
error: 'not_ready'
});
return;
}

const parsedURL = new URL(
`${req.protocol}://${req.hostname}${req.url}`
);
const mapPath = parsedURL.pathname.replace('/Songs', '');

reply.hijack();
send(req, mapPath, { root: settings.songsFolder }).pipe(reply.raw);
});
};
15 changes: 4 additions & 11 deletions packages/tosu/src/entities/AllTimesData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,10 @@ export class AllTimesData extends AbstractEntity {
this.MenuMods = process.readInt(process.readInt(menuModsAddr + 0x9));
// ChatChecker - 0x20
this.ChatStatus = process.readByte(chatCheckerAddr - 0x20);
try {
// [[[SkinData + 4] + 0] + 68]
this.SkinFolder = process.readSharpString(
process.readInt(process.readPointer(skinDataAddr + 4) + 68)
);
} catch (exc) {
console.log(exc);
wLogger.error(
"CANT UPDATE SKIN FOLDER (don't mind, this can happen sometimes)"
);
}
// [[[SkinData + 4] + 0] + 68]
this.SkinFolder = process.readSharpString(
process.readInt(process.readPointer(skinDataAddr + 4) + 68)
);
// [[SettingsClass + 0x8] + 0x4] + 0xC
this.ShowInterface = Boolean(
process.readByte(
Expand Down
2 changes: 1 addition & 1 deletion packages/tosu/src/entities/BeatmapPpData/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Beatmap, Calculator } from '@kotrikd/rosu-pp';
import { Beatmap as ParsedBeatmap } from 'osu-classes';
import { BeatmapDecoder } from 'osu-parsers';
import path from 'path';
import { Beatmap, Calculator } from 'rosu-pp';

import { BeatmapStrains } from '@/api/types';
import { config } from '@/config';
Expand Down
8 changes: 6 additions & 2 deletions packages/tosu/src/entities/GamePlayData/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Beatmap, Calculator } from '@kotrikd/rosu-pp';
import path from 'path';
import { Beatmap, Calculator } from 'rosu-pp';
import { Process } from 'tsprocess/dist/process';

import { config } from '@/config';
Expand Down Expand Up @@ -137,6 +137,10 @@ export class GamePlayData extends AbstractEntity {
}

const scoreBase = process.readInt(gameplayBase + 0x38);
if (scoreBase === 0) {
wLogger.debug('scoreBase is zero');
return;
}

// Resetting default state value, to define other componenets that we have touched gamePlayData
// needed for ex like you done with replay watching/gameplay and return to mainMenu, you need alteast one reset to gamePlayData/resultsScreenData
Expand All @@ -156,7 +160,7 @@ export class GamePlayData extends AbstractEntity {
this.Retries = process.readInt(process.readInt(baseAddr - 0x33) + 0x8);
// [[[Ruleset + 0x68] + 0x38] + 0x28]
this.PlayerName = process.readSharpString(
process.readInt(process.readInt(gameplayBase + 0x38) + 0x28)
process.readInt(scoreBase + 0x28)
);
// [[[Ruleset + 0x68] + 0x38] + 0x1C] + 0xC ^ [[[Ruleset + 0x68] + 0x38] + 0x1C] + 0x8
this.Mods =
Expand Down
5 changes: 2 additions & 3 deletions packages/tosu/src/entities/MenuData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ export class MenuData extends AbstractEntity {
wLogger.debug('beatmapAddr is 0');
return;
}
// [[Beatmap] + 0x6C]
// [[Beatmap] + 0x6C]
this.MD5 = process.readSharpString(process.readInt(beatmapAddr + 0x6c));
// [[Beatmap] + 0x90]
// [[Beatmap] + 0x90]
this.Path = process.readSharpString(
process.readInt(beatmapAddr + 0x90)
);

// [Base - 0x33]
this.MenuGameMode = process.readPointer(baseAddr - 0x33);

Expand Down
Loading

0 comments on commit edebd3c

Please sign in to comment.