Skip to content

Commit

Permalink
Merge pull request #18 from Z233/main
Browse files Browse the repository at this point in the history
fix mirai-console moves file error
  • Loading branch information
BillYang2016 authored Jan 10, 2022
2 parents d049ad0 + 413d9f7 commit cc86211
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ plugins {
val kotlinVersion = "1.4.30"
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
id("net.mamoe.mirai-console") version "2.6.7"
id("net.mamoe.mirai-console") version "2.9.2"
}

group = "com.billyang"
version = "1.1.1"

repositories {
maven{ url =uri("https://maven.aliyun.com/nexus/content/groups/public/")}
maven("https://maven.aliyun.com/repository/public")
jcenter()
mavenCentral()
mavenLocal()
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/billyang/entrylib/Database/Database.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.billyang.entrylib.Database;

import com.billyang.entrylib.EntryLib;
import com.billyang.entrylib.Subgroup.Subgroup;

import java.io.File;
Expand All @@ -22,8 +23,9 @@ public class Database {
* @return 成功状态
*/
public boolean init() {
File file = new File("data/EntryLib/databases/");
File file = new File(EntryLib.DATABASES_FOLDER);
if(!file.exists()) {
EntryLib.INSTANCE.getLogger().info("创建数据库目录");
file.mkdirs();
}

Expand Down Expand Up @@ -117,7 +119,7 @@ public boolean connect(String database) {
* @return 连接情况
*/
public boolean connect(long groupId) {
return connect("jdbc:sqlite:data/EntryLib/databases/" + groupId + ".db");
return connect("jdbc:sqlite:" + EntryLib.DATABASES_FOLDER + groupId + ".db");
}

/**
Expand All @@ -127,7 +129,7 @@ public boolean connect(long groupId) {
* @see Subgroup
*/
public boolean connect(Subgroup subgroup) {
return connect("jdbc:sqlite:data/EntryLib/databases/" + subgroup.getName() + ".db");
return connect("jdbc:sqlite:" + EntryLib.DATABASES_FOLDER + subgroup.getName() + ".db");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* 清理没有用过的图片
*/
class ImageCleaner implements Runnable {

private Thread t;

EntryLib entrylib;
Expand All @@ -35,7 +36,7 @@ class ImageCleaner implements Runnable {
* 线程运行方法
*/
public void run() {
File imageFolder = new File("data/EntryLib/images/");
File imageFolder = new File(EntryLib.IMAGES_FOLDER);
if(imageFolder.exists()) {
List<File> tempList = Arrays.asList(imageFolder.listFiles());
List<File> imageFiles = new ArrayList(tempList);
Expand Down Expand Up @@ -113,7 +114,7 @@ public boolean exists(Statement stmt, String table) {
boolean connect(String database) {
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:data/EntryLib/databases/" + database);
c = DriverManager.getConnection("jdbc:sqlite:" + EntryLib.DATABASES_FOLDER + database);

stmt = c.createStatement();
if(!exists(stmt, "__MAIN_TABLE")) return false;
Expand Down Expand Up @@ -315,7 +316,7 @@ public List<File> Text2ImageFile(String text) {
end = mt.end();

String imageId = ImageParser.MiraiCode2Id(text.substring(start, end));
File file = new File("data/EntryLib/images/", imageId);
File file = new File(EntryLib.IMAGES_FOLDER, imageId);

if(file.exists())list.add(file);
}
Expand Down Expand Up @@ -365,7 +366,7 @@ void listImage(String fileName) {
*/
@Override
public void run() {
File file = new File("data/EntryLib/databases/");
File file = new File(EntryLib.DATABASES_FOLDER);
if(!file.exists()) return;

File[] files = file.listFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public boolean v0tov1() {
* 数据库升级任务
*/
public void update() {
File file = new File("data/EntryLib/databases/");
File file = new File(EntryLib.DATABASES_FOLDER);
if(!file.exists()) return;

File[] files = file.listFiles();
Expand All @@ -121,7 +121,7 @@ public void update() {
String fileName = dbFile.getName();
if(!fileName.endsWith(".db")) continue; //保证文件是数据库文件

if(!database.connect("jdbc:sqlite:data/EntryLib/databases/" + fileName)) {
if(!database.connect("jdbc:sqlite:" + EntryLib.DATABASES_FOLDER + fileName)) {
entrylib.getLogger().warning("无法升级数据库" + fileName + ":数据库连接失败!");
database.close();
continue;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/billyang/entrylib/EntryLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*/
public final class EntryLib extends JavaPlugin {
public static final EntryLib INSTANCE = new EntryLib();
public static final String IMAGES_FOLDER = INSTANCE.getDataFolder().getAbsolutePath() + "/images/";
public static final String DATABASES_FOLDER = INSTANCE.getDataFolder().getAbsolutePath() + "/databases/";
private EntryLib() {
super(new JvmPluginDescriptionBuilder("EntryLib", "1.1.1")
.id("com.billyang.entrylib")
Expand Down Expand Up @@ -469,6 +471,8 @@ public String getVersion() {
@Override
public void onEnable() {

pl.init();

String DataFolderPath = getDataFolder().getAbsolutePath();
getLogger().info("配置文件目录:" + DataFolderPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void init() {
* 自动初始化
*/
public PackageLoader() {
init();
// 自动初始化会导致数据文件被占用,mirai-console 无法自动迁移
// init();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/billyang/entrylib/ui/FloatingWindow.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.billyang.entrylib.ui;

import com.billyang.entrylib.Config.UserIO;
import com.billyang.entrylib.Database.Database;
import com.billyang.entrylib.EntryLib;
import com.billyang.entrylib.Matcher.MatchLoader;

Expand Down Expand Up @@ -344,7 +345,7 @@ void addPackageLeadingPage() {
groupId = 0;
}

File database = new File("data/EntryLib/databases/", groupId + ".db");
File database = new File(EntryLib.DATABASES_FOLDER, groupId + ".db");
if(!database.exists()) {
JOptionPane.showMessageDialog(
panel, "目标群数据库不存在", "错误", JOptionPane.ERROR_MESSAGE
Expand Down

0 comments on commit cc86211

Please sign in to comment.