Skip to content

Commit

Permalink
Merge pull request #599 from xxfttkx/plugin-drag
Browse files Browse the repository at this point in the history
fix plugin save
  • Loading branch information
Predidit authored Jan 13, 2025
2 parents d90a7f0 + 0f23cd6 commit 48ea69d
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/plugins/plugins_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,30 @@ abstract class _PluginsController with Store {

String pluginsFileName = "plugins.json";

Directory? pluginDirectory;
Directory? oldPluginDirectory;

Directory? newPluginDirectory;

// Initializes the plugin directory and loads all plugins
Future<void> init() async {
final directory = await getApplicationSupportDirectory();
pluginDirectory = Directory('${directory.path}/plugins');
oldPluginDirectory = Directory('${directory.path}/plugins');
if (!await oldPluginDirectory!.exists()) {
await oldPluginDirectory!.create(recursive: true);
}
newPluginDirectory = Directory('${directory.path}/plugins/v2');
if (!await newPluginDirectory!.exists()) {
await newPluginDirectory!.create(recursive: true);
}
await loadAllPlugins();
}

// Loads all plugins from the directory, populates the plugin list, and saves to plugins.json if needed
Future<void> loadAllPlugins() async {
pluginList.clear();
KazumiLogger().log(Level.info, '插件目录 ${pluginDirectory!.path}');
if (await pluginDirectory!.exists()) {
final pluginsFile = File('${pluginDirectory!.path}/$pluginsFileName');
KazumiLogger().log(Level.info, '插件目录 ${newPluginDirectory!.path}');
if (await newPluginDirectory!.exists()) {
final pluginsFile = File('${newPluginDirectory!.path}/$pluginsFileName');
if (await pluginsFile.exists()) {
final jsonString = await pluginsFile.readAsString();
pluginList.addAll(getPluginListFromJson(jsonString));
Expand All @@ -75,8 +84,8 @@ abstract class _PluginsController with Store {

// Retrieves a list of JSON plugin file paths from the plugin directory
Future<List<String>> getPluginFiles() async {
if (await pluginDirectory!.exists()) {
final jsonFiles = pluginDirectory!
if (await oldPluginDirectory!.exists()) {
final jsonFiles = oldPluginDirectory!
.listSync()
.where((file) => file.path.endsWith('.json') && file is File)
.map((file) => file.path)
Expand All @@ -89,10 +98,6 @@ abstract class _PluginsController with Store {

// Copies plugin JSON files from the assets to the plugin directory
Future<void> copyPluginsToExternalDirectory() async {
if (!await pluginDirectory!.exists()) {
await pluginDirectory!.create(recursive: true);
}

final manifestContent = await rootBundle.loadString('AssetManifest.json');
final Map<String, dynamic> manifestMap = json.decode(manifestContent);

Expand All @@ -106,7 +111,7 @@ abstract class _PluginsController with Store {
}
await savePlugins();
KazumiLogger().log(
Level.info, '已将 ${jsonFiles.length} 个插件文件拷贝到 ${pluginDirectory!.path}');
Level.info, '已将 ${jsonFiles.length} 个插件文件拷贝到 ${newPluginDirectory!.path}');
}

List<dynamic> pluginListToJson() {
Expand Down Expand Up @@ -159,9 +164,7 @@ abstract class _PluginsController with Store {

Future<void> savePlugins() async {
final jsonData = jsonEncode(pluginListToJson());
final directory = await getApplicationSupportDirectory();
final pluginDirectory = Directory('${directory.path}/plugins');
final pluginsFile = File('${pluginDirectory.path}/$pluginsFileName');
final pluginsFile = File('${newPluginDirectory!.path}/$pluginsFileName');
await pluginsFile.writeAsString(jsonData);
KazumiLogger().log(Level.info, '已更新插件文件 $pluginsFileName');
}
Expand Down

0 comments on commit 48ea69d

Please sign in to comment.