Skip to content

Commit

Permalink
新增插件开关相关逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
XTxiaoting14332 committed Oct 2, 2024
1 parent b2d256e commit fae2d15
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 23 deletions.
31 changes: 28 additions & 3 deletions lib/utils/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ Future<String> getnbcliver() async {
createLog(path) {
File stdout = File('$path/nbgui_stdout.log');
File stderr = File('$path/nbgui_stderr.log');
File disable = File('$path/.disabled_plugins');
if (!stdout.existsSync()) {
stdout.createSync();
}
if (!stderr.existsSync()) {
stderr.createSync();
}
if (!disable.existsSync()) {
disable.createSync();
}
}

///清除日志
Expand All @@ -84,15 +88,36 @@ clearLog(path) async {
}

///从pyproject.toml中读取插件列表
getPluginList() {
List getPluginList() {
File pyprojectFile = File('${Bot.path()}/pyproject.toml');
String pyproject = pyprojectFile.readAsStringSync();
var toml = TomlDocument.parse(pyproject).toMap();
String pyprojectContent = pyprojectFile.readAsStringSync();
List<String> linesWithoutComments = pyprojectContent
.split('\n')
.map((line) {
int commentIndex = line.indexOf('#');
if (commentIndex != -1) {
return line.substring(0, commentIndex).trim();
}
return line;
})
.where((line) => line.isNotEmpty)
.toList();
String pyprojectWithoutComments = linesWithoutComments.join('\n');
// 解析 TOML 文件
var toml = TomlDocument.parse(pyprojectWithoutComments).toMap();
var nonebot = toml['tool']['nonebot'];
List pluginsList = nonebot['plugins'];

return pluginsList;
}

getDisabledPluginList() {
File disable = File('${Bot.path()}/.disabled_plugins');
String content = disable.readAsStringSync();
List disabledPlugins = content.split('\n');
return disabledPlugins;
}

///获取协议端文件名
getProtocolFileName() {
String ucmd = FastDeploy.cmd.replaceAll('./', '').replaceAll('.\\', '');
Expand Down
88 changes: 68 additions & 20 deletions lib/utils/manage.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:convert';
import 'dart:io';
import 'package:NoneBotGUI/utils/core.dart';
import 'package:NoneBotGUI/utils/global.dart';
import 'package:NoneBotGUI/utils/userConfig.dart';
import 'package:toml/toml.dart';

class Bot {
static final File _configFile = File('$userDir/bots/$gOnOpen.json');
Expand Down Expand Up @@ -42,29 +44,18 @@ class Bot {
return jsonMap['pid'].toString();
}

/// 获取Bot的Python Pid
static String pypid(path) {
/// 直接抓取Bot日志的的Python Pid
static pypid(path) {
File file = File('$path/nbgui_stdout.log');
RegExp regex = RegExp(r'Started server process \[(\d+)\]');
Match? match =
regex.firstMatch(file.readAsStringSync(encoding: systemEncoding));
if (match != null && match.groupCount >= 1) {
String pid = match.group(1)!;
Bot.setPyPid(pid);
return pid;
} else {
Bot.setPyPid("Null");
return "Null";
}
}

/// 设置Bot的Python Pid
static void setPyPid(pid) {
Map<String, dynamic> jsonMap = _config();
jsonMap['pypid'] = pid;
_configFile.writeAsStringSync(jsonEncode(jsonMap));
}

/// 唤起Bot进程
static Future run() async {
File cfgFile = File('$userDir/bots/$gOnOpen.json');
Expand Down Expand Up @@ -107,13 +98,11 @@ class Bot {
botInfo['pid'] = 'Null';
cfgFile.writeAsStringSync(json.encode(botInfo));
//如果平台为Windows则释放端口
//有bug,先注释掉(
// if (Platform.isWindows) {
// await Process.start(
// "taskkill.exe", ['/f', '/pid', Bot.pypid(Bot.path()).toString()],
// runInShell: true);
// }
// setPyPid('Null');
if (Platform.isWindows) {
await Process.start(
"taskkill.exe", ['/f', '/pid', Bot.pypid(Bot.path()).toString()],
runInShell: true);
}
}

///重命名Bot
Expand Down Expand Up @@ -361,3 +350,62 @@ class Cli {
}
}
}

///插件
class Plugin {
///禁用插件
static disable(name) {
File disable = File('${Bot.path()}/.disabled_plugins');
File pyprojectFile = File('${Bot.path()}/pyproject.toml');
String pyprojectContent = pyprojectFile.readAsStringSync();
List<String> linesWithoutComments = pyprojectContent
.split('\n')
.map((line) {
int commentIndex = line.indexOf('#');
if (commentIndex != -1) {
return line.substring(0, commentIndex).trim();
}
return line;
})
.where((line) => line.isNotEmpty)
.toList();
String pyprojectWithoutComments = linesWithoutComments.join('\n');
var toml = TomlDocument.parse(pyprojectWithoutComments).toMap();
var nonebot = toml['tool']['nonebot'];
List pluginsList = nonebot['plugins'];

// 移除指定的插件
pluginsList.remove(name);
nonebot['plugins'] = pluginsList;
String updatedTomlContent = TomlDocument.fromMap(toml).toString();

pyprojectFile.writeAsStringSync(updatedTomlContent);
if (disable.readAsStringSync().isEmpty) {
disable.writeAsStringSync(name);
} else {
disable.writeAsStringSync('${disable.readAsStringSync()}\n$name');
}
}

///启用插件
static enable(name) {
File disable = File('${Bot.path()}/.disabled_plugins');
File pyprojectFile = File('${Bot.path()}/pyproject.toml');
String pyprojectContent = pyprojectFile.readAsStringSync();
var toml = TomlDocument.parse(pyprojectContent).toMap();
var nonebot = toml['tool']['nonebot'];
List pluginsList = nonebot['plugins'];

if (!pluginsList.contains(name)) {
pluginsList.add(name);
}

nonebot['plugins'] = pluginsList;
String updatedTomlContent = TomlDocument.fromMap(toml).toString();
pyprojectFile.writeAsStringSync(updatedTomlContent);
String disabled = disable.readAsStringSync();
List<String> disabledList = disabled.split('\n');
disabledList.remove(name);
disable.writeAsStringSync(disabledList.join('\n'));
}
}

0 comments on commit fae2d15

Please sign in to comment.