Skip to content

Commit 9c32a8f

Browse files
authored
Merge pull request #368 from runejs/kiko/reload-content-fix
Fixing the reload content functionality
2 parents 2af6af1 + 92037a9 commit 9c32a8f

File tree

13 files changed

+181
-151
lines changed

13 files changed

+181
-151
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"start:infra": "concurrently \"npm run start:update\" \"npm run start:login\"",
1313
"start:standalone": "concurrently \"npm run start:infra\" \"npm run start:game\"",
1414
"game": "npm run start:game",
15-
"game:dev": "npm run start:game",
15+
"game:dev": "npm run start:game:dev",
1616
"login": "npm run start:login",
1717
"update": "npm run start:update",
1818
"infra": "npm run start:infra",

src/engine/action/pipe/player-command.action.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Player } from '@engine/world/actor';
22
import { ActionHook, getActionHooks, ActionPipe, RunnableHooks } from '@engine/action';
3+
import { reloadContent, reloadContentCommands } from '@engine/plugins/reload-content';
4+
import { logger } from '@runejs/common';
35

46

57
/**
@@ -49,6 +51,12 @@ const playerCommandActionPipe = (player: Player, command: string, isConsole: boo
4951
inputArgs: string[]): RunnableHooks<PlayerCommandAction> => {
5052
command = command.toLowerCase();
5153

54+
// Reload game content
55+
if (reloadContentCommands.indexOf(command) !== -1) {
56+
reloadContent(player, isConsole).catch(logger.error);
57+
return;
58+
}
59+
5260
const actionArgs = {};
5361

5462
const plugins = getActionHooks<PlayerCommandActionHook>('player_command').filter(actionHook => {

src/engine/config/shop-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { findItem, widgets } from '@engine/config/config-handler';
33
import { loadConfigurationFiles } from '@runejs/common/fs';
44
import { Player } from '@engine/world/actor/player/player';
55
import { ItemDetails } from '@engine/config/item-config';
6-
import { WidgetClosedEvent } from '@engine/world/actor/player/interface-state';
6+
import { WidgetClosedEvent } from '@engine/interface';
77
import { Subscription } from 'rxjs';
88

99

src/engine/interface/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './interface-state';
File renamed without changes.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { sep } from 'path';
2+
import { loadGameConfigurations } from '@engine/config';
3+
import { loadPackets } from '@engine/net';
4+
import { loadPlugins } from '@engine/plugins/loader';
5+
import { logger } from '@runejs/common';
6+
import { Player } from '@engine/world/actor';
7+
8+
9+
export const reloadContentCommands = [
10+
'plugins', 'reload', 'content', 'hotload', 'refresh', 'restart', 'r'
11+
];
12+
13+
export const reloadContent = async (player: Player, isConsole: boolean = false) => {
14+
player.sendLogMessage(' ', isConsole);
15+
player.sendLogMessage('Deleting content cache...', isConsole);
16+
17+
const includeList = [
18+
'plugins'
19+
].map(p => sep + p + sep);
20+
21+
const ignoreList = [
22+
'node_modules',
23+
'engine',
24+
'server'
25+
].map(p => sep + p + sep);
26+
27+
const pluginCache = [];
28+
const cacheKeys = Object.keys(require.cache);
29+
30+
// Delete node cache for all the old JS plugins
31+
cacheLoop:
32+
for (const cacheKey of cacheKeys) {
33+
const cachedItem = require.cache[cacheKey];
34+
35+
if (!cachedItem) {
36+
continue;
37+
}
38+
39+
const path = typeof cachedItem === 'string' ? cachedItem : cachedItem?.path;
40+
41+
if (!path) {
42+
continue;
43+
}
44+
45+
for (const ignoreItem of ignoreList) {
46+
if (path.indexOf(ignoreItem) !== -1) {
47+
continue cacheLoop;
48+
}
49+
}
50+
51+
let includePath = false;
52+
53+
for (const includeItem of includeList) {
54+
if (path.indexOf(includeItem) !== -1) {
55+
includePath = true;
56+
break;
57+
}
58+
}
59+
60+
if (includePath) {
61+
pluginCache.push(cacheKey);
62+
}
63+
}
64+
65+
console.log(pluginCache);
66+
67+
for (const key of pluginCache) {
68+
delete require.cache[require.resolve(key)];
69+
}
70+
71+
try {
72+
player.sendLogMessage('Reloading plugins...', isConsole);
73+
await loadPlugins();
74+
} catch (error) {
75+
player.sendLogMessage('Error reloading content.', isConsole);
76+
logger.error(error);
77+
}
78+
79+
try {
80+
player.sendLogMessage('Reloading configurations...', isConsole);
81+
await loadGameConfigurations();
82+
} catch (error) {
83+
player.sendLogMessage('Error reloading configurations.', isConsole);
84+
logger.error(error);
85+
}
86+
87+
try {
88+
player.sendLogMessage('Reloading packets...', isConsole);
89+
await loadPackets();
90+
} catch (error) {
91+
player.sendLogMessage('Error reloading packets.', isConsole);
92+
logger.error(error);
93+
}
94+
95+
player.sendLogMessage('Reload completed.', isConsole);
96+
};

0 commit comments

Comments
 (0)