|
| 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