|
| 1 | +// LiteLoaderScript Dev Helper |
| 2 | +/// <reference path="d:\Coding\LLSEAids/dts/llaids/src/index.d.ts"/> |
| 3 | +/* global logger ll mc logger */ |
| 4 | + |
| 5 | +const { existsSync, mkdirSync, writeFileSync, readFileSync } = require('fs'); |
| 6 | +const { resolve, join } = require('path'); |
| 7 | +const checkDiskSpace = require('check-disk-space').default; |
| 8 | + |
| 9 | +const config = { |
| 10 | + checkInterval: 60, |
| 11 | + percent: 0.95, |
| 12 | + stopInterval: 20, |
| 13 | + showDiskLog: true, |
| 14 | +}; |
| 15 | + |
| 16 | +const pluginName = 'DiskUsageStop'; |
| 17 | +const pluginVer = [0, 1, 0]; |
| 18 | + |
| 19 | +const pluginDataPath = `plugins/${pluginName}`; |
| 20 | +const pluginConfigPath = join(pluginDataPath, 'config.json'); |
| 21 | +if (!existsSync(pluginDataPath)) mkdirSync(pluginDataPath); |
| 22 | + |
| 23 | +logger.setTitle(pluginName); |
| 24 | + |
| 25 | +function formatError(e) { |
| 26 | + let msg = e; |
| 27 | + if (e instanceof Error) msg = e.stack || e.message; |
| 28 | + return String(msg); |
| 29 | +} |
| 30 | + |
| 31 | +function logError(e) { |
| 32 | + logger.error(`插件出错!\n${formatError(e)}`); |
| 33 | +} |
| 34 | + |
| 35 | +function reloadConfigSync() { |
| 36 | + if (!existsSync(pluginConfigPath)) { |
| 37 | + logger.warn(`插件配置文件不存在,已写入默认配置到 ${pluginConfigPath}`); |
| 38 | + writeFileSync(pluginConfigPath, JSON.stringify(config, undefined, 2), { |
| 39 | + encoding: 'utf-8', |
| 40 | + }); |
| 41 | + } else { |
| 42 | + let loadedConf; |
| 43 | + try { |
| 44 | + loadedConf = JSON.parse( |
| 45 | + readFileSync(pluginConfigPath, { encoding: 'utf-8' }) |
| 46 | + ); |
| 47 | + } catch (e) { |
| 48 | + logger.error(`读取插件配置失败,将使用默认配置\n${formatError(e)}`); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + Object.entries(loadedConf).forEach((e) => { |
| 53 | + const [k, v] = e; |
| 54 | + config[k] = v; |
| 55 | + }); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * @param {(...args:any[])=>Promise<Any>} func |
| 61 | + */ |
| 62 | +function warpAsyncFunction(func) { |
| 63 | + return (...args) => { |
| 64 | + func(...args).catch(logError); |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * @param {number} space |
| 70 | + * @param {number} pointLength |
| 71 | + * @returns |
| 72 | + */ |
| 73 | +function formatSpace(space, pointLength = 2) { |
| 74 | + const units = ['B', 'K', 'M', 'G']; |
| 75 | + let currentUnit = units.shift(); |
| 76 | + while (units.length > 0 && space >= 1024) { |
| 77 | + currentUnit = units.shift(); |
| 78 | + space /= 1024; |
| 79 | + } |
| 80 | + return `${space.toFixed(pointLength)}${currentUnit}`; |
| 81 | +} |
| 82 | + |
| 83 | +mc.listen('onServerStarted', () => { |
| 84 | + const checkTask = async () => { |
| 85 | + const { stopInterval, percent, showDiskLog } = config; |
| 86 | + const { free, size } = await checkDiskSpace(resolve(__dirname)); |
| 87 | + |
| 88 | + const usedSpace = size - free; |
| 89 | + const usedPercent = usedSpace / size; |
| 90 | + |
| 91 | + const formattedUsed = formatSpace(usedSpace); |
| 92 | + const formattedTotal = formatSpace(size); |
| 93 | + const formattedPercent = (usedPercent * 100).toFixed(2); |
| 94 | + |
| 95 | + if (showDiskLog) |
| 96 | + logger.info( |
| 97 | + `[定时监测] 磁盘已用 ${formattedUsed} / ${formattedTotal} (${formattedPercent}%)` |
| 98 | + ); |
| 99 | + |
| 100 | + if (usedPercent >= percent) { |
| 101 | + logger.warn('磁盘使用达到限制,即将关服'); |
| 102 | + |
| 103 | + const toastTitle = |
| 104 | + `§g服务器存储占用已到达 §c§l${formattedPercent}%%§r ` + |
| 105 | + `§7(§e${formattedUsed} §7/ §6${formattedTotal}§7)§g!`; |
| 106 | + const toastMsg = `§g将在 §d§l${stopInterval} §r§g秒后关闭服务器!`; |
| 107 | + mc.getOnlinePlayers().forEach((p) => p.sendToast(toastTitle, toastMsg)); |
| 108 | + mc.broadcast(`${toastTitle}\n${toastMsg}`); |
| 109 | + |
| 110 | + let countdown = stopInterval; |
| 111 | + const stopTask = setInterval(() => { |
| 112 | + countdown -= 1; |
| 113 | + if (countdown <= 0) { |
| 114 | + mc.runcmd('stop'); |
| 115 | + clearInterval(stopTask); |
| 116 | + } else { |
| 117 | + mc.broadcast(`§g还有 §d§l${countdown} §r§g秒关闭服务器!`); |
| 118 | + } |
| 119 | + }, 1000); |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + setInterval(warpAsyncFunction(checkTask), config.checkInterval * 1000); |
| 124 | + checkTask(); |
| 125 | +}); |
| 126 | + |
| 127 | +reloadConfigSync(); |
| 128 | +ll.registerPlugin( |
| 129 | + pluginName, |
| 130 | + '当你BDS所在硬盘分区使用量超过指定比例时停服', |
| 131 | + pluginVer, |
| 132 | + { |
| 133 | + Author: 'student_2333', |
| 134 | + License: 'Apache-2.0', |
| 135 | + } |
| 136 | +); |
0 commit comments