Skip to content

Commit

Permalink
fix: Fix local counters path (#104)
Browse files Browse the repository at this point in the history
* fix: Fix 0 and false not saving in config
* fix: Fix local counters path
* fix: Fix crash on ENOENT
* fix: Fix local counters are not opening nor deleting
* fix kotrik typo
* Fix: Move precise build result to own file
* fix: Fix potential memory leak
* fix: Incorrect config save for new WS send method
  • Loading branch information
cyperdark authored Mar 21, 2024
1 parent 24a00f8 commit cf32255
Show file tree
Hide file tree
Showing 19 changed files with 283 additions and 170 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
**/static/**/*
**/static/**/*

packages/server/assets/homepage.js
57 changes: 43 additions & 14 deletions packages/common/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,32 @@ export const refreshConfig = (httpServer: any, refresh: boolean) => {
httpServer.restart();
}

if (
config.pollRate !== pollRate ||
config.preciseDataPollRate !== preciseDataPollRate
) {
config.pollRate = pollRate >= 0 ? pollRate : 100;
config.preciseDataPollRate =
preciseDataPollRate >= 0 ? preciseDataPollRate : 100;
httpServer.restartWS();
}

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

config.debugLogging = debugLogging;
config.calculatePP = calculatePP;
config.enableKeyOverlay = enableKeyOverlay;
config.pollRate = pollRate >= 0 ? pollRate : 100;
config.preciseDataPollRate =
preciseDataPollRate >= 0 ? preciseDataPollRate : 100;
config.staticFolderPath = staticFolderPath;
config.enableGosuOverlay = enableGosuOverlay;

if (
config.staticFolderPath === './static' &&
Expand All @@ -221,22 +232,40 @@ export const refreshConfig = (httpServer: any, refresh: boolean) => {
export const writeConfig = (httpServer: any, options: any) => {
let text = '';

text += `DEBUG_LOG=${options.DEBUG_LOG || config.debugLogging}\n\n`;
text += `CALCULATE_PP=${options.CALCULATE_PP || config.calculatePP}\n\n`;
text += `DEBUG_LOG=${
options.DEBUG_LOG != null ? options.DEBUG_LOG : config.debugLogging
}\n\n`;
text += `CALCULATE_PP=${
options.CALCULATE_PP != null ? options.CALCULATE_PP : config.calculatePP
}\n\n`;
text += `ENABLE_GOSU_OVERLAY=${
options.ENABLE_GOSU_OVERLAY || config.enableGosuOverlay
options.ENABLE_GOSU_OVERLAY != null
? options.ENABLE_GOSU_OVERLAY
: config.enableGosuOverlay
}\n`;
text += `ENABLE_KEY_OVERLAY=${
options.ENABLE_KEY_OVERLAY || config.enableKeyOverlay
options.ENABLE_KEY_OVERLAY != null
? options.ENABLE_KEY_OVERLAY
: config.enableKeyOverlay
}\n\n`;
text += `POLL_RATE=${options.POLL_RATE || config.pollRate}\n`;
text += `POLL_RATE=${
options.POLL_RATE != null ? options.POLL_RATE : config.pollRate
}\n`;
text += `PRECISE_DATA_POLL_RATE=${
options.PRECISE_DATA_POLL_RATE || config.preciseDataPollRate
options.PRECISE_DATA_POLL_RATE != null
? options.PRECISE_DATA_POLL_RATE
: config.preciseDataPollRate
}\n\n`;
text += `SERVER_IP=${
options.SERVER_IP != null ? options.SERVER_IP : config.serverIP
}\n`;
text += `SERVER_PORT=${
options.SERVER_PORT != null ? options.SERVER_PORT : config.serverPort
}\n\n`;
text += `SERVER_IP=${options.SERVER_IP || config.serverIP}\n`;
text += `SERVER_PORT=${options.SERVER_PORT || config.serverPort}\n\n`;
text += `STATIC_FOLDER_PATH=${
options.STATIC_FOLDER_PATH || config.staticFolderPath
options.STATIC_FOLDER_PATH != null
? options.STATIC_FOLDER_PATH
: config.staticFolderPath
}\n`;

fs.writeFileSync(configPath, text, 'utf8');
Expand Down
1 change: 1 addition & 0 deletions packages/common/utils/unzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const unzip = (zipPath: string, extractPath: string): Promise<string> =>
resolve(extractPath);
} catch (error) {
wLogger.error((error as any).message);
wLogger.debug(error);
reject(error);
}
});
2 changes: 1 addition & 1 deletion packages/server/assets/homepage.js

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions packages/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ export class Server {
instanceManager: any;
app = new HttpServer();

WS_V1: Websocket;
WS_V2: Websocket;
WS_V2_PRECISE: Websocket;

constructor({ instanceManager }: { instanceManager: any }) {
this.instanceManager = instanceManager;

this.middlrewares();
}

start() {
const WS_V1 = new Websocket({
this.WS_V1 = new Websocket({
instanceManager: this.instanceManager,
pollRateFieldName: 'pollRate',
stateFunctionName: 'getState'
});
const WS_V2 = new Websocket({
this.WS_V2 = new Websocket({
instanceManager: this.instanceManager,
pollRateFieldName: 'pollRate',
stateFunctionName: 'getStateV2'
});
const WS_V2_PRECISE = new Websocket({
this.WS_V2_PRECISE = new Websocket({
instanceManager: this.instanceManager,
pollRateFieldName: 'preciseDataPollRate',
stateFunctionName: 'getPreciseData'
Expand All @@ -36,12 +40,12 @@ export class Server {
buildBaseApi(this);
buildV1Api({
app: this.app,
websocket: WS_V1
websocket: this.WS_V1
});
buildV2Api({
app: this.app,
websocket: WS_V2,
preciseWebsocket: WS_V2_PRECISE
websocket: this.WS_V2,
preciseWebsocket: this.WS_V2_PRECISE
});

this.app.listen(config.serverPort, config.serverIP);
Expand All @@ -52,6 +56,16 @@ export class Server {
this.app.listen(config.serverPort, config.serverIP);
}

restartWS() {
this.WS_V1.stopLoop();
this.WS_V2.stopLoop();
this.WS_V2_PRECISE.stopLoop();

this.WS_V1.startLoop();
this.WS_V2.startLoop();
this.WS_V2_PRECISE.startLoop();
}

middlrewares() {
const instanceManager = this.instanceManager;

Expand Down
14 changes: 14 additions & 0 deletions packages/server/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default function buildBaseApi(server: Server) {
return buildLocalCounters(res, query);
} catch (error) {
wLogger.error((error as any).message);
wLogger.debug(error);

return sendJson(res, {
error: (error as any).message
Expand Down Expand Up @@ -122,6 +123,7 @@ export default function buildBaseApi(server: Server) {
});
} catch (error) {
wLogger.error((error as any).message);
wLogger.debug(error);

sendJson(res, {
error: (error as any).message
Expand Down Expand Up @@ -162,6 +164,7 @@ export default function buildBaseApi(server: Server) {
exec(`start "" "${folderPath}"`, (err) => {
if (err) {
wLogger.error('Error opening file explorer:');
wLogger.debug(err);
return sendJson(res, {
error: `Error opening file explorer: ${err.message}`
});
Expand Down Expand Up @@ -242,8 +245,11 @@ export default function buildBaseApi(server: Server) {
res.writeHead(404, {
'Content-Type': 'text/html'
});

res.end('<html>page not found</html>');
return;
}

res.writeHead(200, {
'Content-Type': getContentType(req.params.filePath)
});
Expand All @@ -263,8 +269,11 @@ export default function buildBaseApi(server: Server) {
res.writeHead(404, {
'Content-Type': 'text/html'
});

res.end('<html>page not found</html>');
return;
}

res.writeHead(200, {
'Content-Type': getContentType('homepage.min.css')
});
Expand All @@ -283,8 +292,11 @@ export default function buildBaseApi(server: Server) {
res.writeHead(404, {
'Content-Type': 'text/html'
});

res.end('<html>page not found</html>');
return;
}

res.writeHead(200, {
'Content-Type': getContentType('homepage.js')
});
Expand Down Expand Up @@ -381,6 +393,8 @@ export default function buildBaseApi(server: Server) {
folderPath
});
} catch (error) {
wLogger.debug(error);

return sendJson(res, {
error: (error as any).message
});
Expand Down
45 changes: 28 additions & 17 deletions packages/server/router/v1.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { wLogger } from '@tosu/common';

import { HttpServer, Websocket, sendJson } from '../index';
import { directoryWalker } from '../utils/directories';

Expand All @@ -22,25 +24,34 @@ export default function buildV1Api({
});

app.route(/^\/Songs\/(?<filePath>.*)/, 'GET', (req, res) => {
const url = req.pathname || '/';
try {
const url = req.pathname || '/';

const osuInstance: any = req.instanceManager.getInstance();
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
}
const osuInstance: any = req.instanceManager.getInstance();
if (!osuInstance) {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
}

const { settings } = osuInstance.entities.getServices(['settings']);
if (settings.songsFolder === '') {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
}
const { settings } = osuInstance.entities.getServices(['settings']);
if (settings.songsFolder === '') {
res.statusCode = 500;
return sendJson(res, { error: 'not_ready' });
}

directoryWalker({
res,
baseUrl: url,
pathname: req.params.filePath,
folderPath: settings.songsFolder
});
directoryWalker({
res,
baseUrl: url,
pathname: req.params.filePath,
folderPath: settings.songsFolder
});
} catch (error) {
wLogger.error((error as any).message);
wLogger.debug(error);

return sendJson(res, {
error: (error as any).message
});
}
});
}
Loading

0 comments on commit cf32255

Please sign in to comment.