Skip to content

Commit 97f7725

Browse files
Making inbound packets hot-reloadable
1 parent b21ef5b commit 97f7725

File tree

4 files changed

+73
-44
lines changed

4 files changed

+73
-44
lines changed

src/game-server.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { setNpcInitPlugins } from '@server/world/actor/npc/npc';
2727
import { setQuestPlugins } from '@server/world/config/quests';
2828
import { setPlayerPlugins } from '@server/world/actor/player/action/player-action';
2929
import { loadPackets } from '@server/net/inbound-packets';
30+
import { watchForChanges } from '@server/util/files';
3031

3132

3233
export let serverConfig: ServerConfig;
@@ -141,14 +142,6 @@ export async function runGameServer(): Promise<void> {
141142

142143
openServer();
143144

144-
const watcher = watch('dist/plugins/');
145-
watcher.on('ready', () => {
146-
watcher.on('all', () => {
147-
Object.keys(require.cache).forEach((id) => {
148-
if(/[\/\\]plugins[\/\\]/.test(id)) {
149-
delete require.cache[id];
150-
}
151-
});
152-
});
153-
});
145+
watchForChanges('dist/plugins/', /[\/\\]plugins[\/\\]/);
146+
watchForChanges('dist/net/inbound-packets/', /[\/\\]inbound-packets[\/\\]/);
154147
}

src/plugins/commands/reload-plugins.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/plugins/commands/reload.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { ActionType, RunePlugin } from '@server/plugins/plugin';
2+
import { commandAction } from '@server/world/actor/player/action/input-command-action';
3+
import { injectPlugins } from '@server/game-server';
4+
import { loadPackets } from '@server/net/inbound-packets';
5+
6+
const action: commandAction = (details) => {
7+
const { player } = details;
8+
9+
player.sendLogMessage('Reloading content...', details.isConsole);
10+
11+
// Delete node cache for all the old JS plugins
12+
for(const path in require.cache) {
13+
if(!path.endsWith('.js')) {
14+
continue;
15+
}
16+
17+
const mustContain = [
18+
'/plugins/',
19+
'/inbound-packets/'
20+
];
21+
22+
if(path.indexOf('node_modules') !== -1) {
23+
continue;
24+
}
25+
26+
let found = false;
27+
for(const s of mustContain) {
28+
if(path.indexOf(s) !== -1) {
29+
found = true;
30+
break;
31+
}
32+
}
33+
34+
if(!found) {
35+
continue;
36+
}
37+
38+
const blacklist = [ 'plugin-loader.js', 'plugin.js', 'rune.js' ];
39+
const invalid = blacklist.some(component => path.endsWith(component) || path.endsWith('.map'));
40+
41+
if(invalid) {
42+
continue;
43+
}
44+
45+
delete require.cache[path];
46+
}
47+
48+
injectPlugins()
49+
.then(() => player.sendLogMessage('Content reloaded.', details.isConsole))
50+
.catch(() => player.sendLogMessage('Error reloading content.', details.isConsole));
51+
loadPackets();
52+
};
53+
54+
export default new RunePlugin({ type: ActionType.COMMAND, commands: [
55+
'plugins', 'reload', 'content', 'hotload', 'refresh', 'restart', 'clear', 'r'
56+
], action });

src/util/files.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import util from 'util';
22
import fs from 'fs';
3+
import { watch } from 'chokidar';
34

45
const readdir = util.promisify(fs.readdir);
56
const stat = util.promisify(fs.stat);
@@ -26,3 +27,16 @@ export async function* getFiles(directory: string, blacklist: string[]): AsyncGe
2627
}
2728
}
2829
}
30+
31+
export function watchForChanges(dir: string, regex: RegExp): void {
32+
const watcher = watch(dir);
33+
watcher.on('ready', () => {
34+
watcher.on('all', () => {
35+
Object.keys(require.cache).forEach((id) => {
36+
if(regex.test(id)) {
37+
delete require.cache[id];
38+
}
39+
});
40+
});
41+
});
42+
}

0 commit comments

Comments
 (0)