Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.io.Files;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import dev.felnull.fnjl.util.FNDataUtil;
import dev.felnull.fnjl.util.FNStringUtil;
import dev.felnull.itts.core.dict.CustomDictionaryEntry;
Expand Down Expand Up @@ -47,10 +48,39 @@ public class LegacyMigrator {
*/
private final Gson gson = new Gson();


/**
* Jsonの保存先ディレクトリ
*/
private final File jsonSaveDir;

/**
* グローバル辞書ファイル
*/
private final File globalDictFile;

private LegacyMigrator() {
this(JSON_SAVE_DIR, GLOBAL_DICT_DIR);
}

private void execute(DataRepository repo) {
LegacyMigrator(File jsonSaveDir, File globalDictFile) {
this.jsonSaveDir = jsonSaveDir;
this.globalDictFile = globalDictFile;
}

void moveOldData(File moveDir) throws IOException {
FNDataUtil.wishMkdir(moveDir);

if (jsonSaveDir.exists()) {
Files.move(jsonSaveDir, new File(moveDir, jsonSaveDir.getName()));
}

if (globalDictFile.exists()) {
Files.move(globalDictFile, new File(moveDir, globalDictFile.getName()));
}
}

void execute(DataRepository repo) {
migrateServerData(repo);
migrateServerUserData(repo);
migrateServerDictUseData(repo);
Expand All @@ -59,7 +89,7 @@ private void execute(DataRepository repo) {
}

private void migrateServerData(DataRepository repo) {
File dir = new File(JSON_SAVE_DIR, "server");
File dir = new File(jsonSaveDir, "server");
if (!dir.exists()) {
return;
}
Expand Down Expand Up @@ -104,7 +134,7 @@ private void migrateServerData(DataRepository repo) {
}

private void migrateServerUserData(DataRepository repo) {
File dir = new File(JSON_SAVE_DIR, "server_users");
File dir = new File(jsonSaveDir, "server_users");
if (!dir.exists()) {
return;
}
Expand Down Expand Up @@ -139,9 +169,9 @@ private void migrateServerUserData(DataRepository repo) {
.forEach((entry) -> {
long userId;
try {
userId = Long.parseLong(name);
userId = Long.parseLong(entry.getKey());
} catch (NumberFormatException e) {
LOGGER.error("Invalid data name {}", file.getName());
LOGGER.error("Invalid data name {}", entry.getKey());
return;
}
JsonObject entryJo = entry.getValue().getAsJsonObject();
Expand All @@ -159,7 +189,7 @@ private void migrateServerUserData(DataRepository repo) {
}

private void migrateServerDictUseData(DataRepository repo) {
File dir = new File(JSON_SAVE_DIR, "dict_use");
File dir = new File(jsonSaveDir, "dict_use");
if (!dir.exists()) {
return;
}
Expand Down Expand Up @@ -210,7 +240,7 @@ private void migrateServerDictUseData(DataRepository repo) {
}

private void migrateServerDictData(DataRepository repo) {
File dir = new File(JSON_SAVE_DIR, "server_dict");
File dir = new File(jsonSaveDir, "server_dict");
if (!dir.exists()) {
return;
}
Expand Down Expand Up @@ -238,24 +268,30 @@ private void migrateServerDictData(DataRepository repo) {
Map<String, String> dictEntry = loadDict(jo);
CustomDictionaryData dictionaryData = repo.getServerCustomDictionaryData(serverId);
dictEntry.forEach((target, read) -> {
dictionaryData.add(new CustomDictionaryEntry(target, read, ReplaceType.WORD));
if (dictionaryData.getByTarget(target).isEmpty()) {
dictionaryData.add(new CustomDictionaryEntry(target, read, ReplaceType.WORD));
}
});
});
}

private void migrateGlobalDictData(DataRepository repo) {
if (!GLOBAL_DICT_DIR.exists()) {
if (!globalDictFile.exists()) {
return;
}

JsonObject jo = loadJson(GLOBAL_DICT_DIR);
JsonObject jo = loadJson(globalDictFile);
if (jo == null) {
return;
}

Map<String, String> dictEntry = loadDict(jo);
CustomDictionaryData dictionaryData = repo.getGlobalCustomDictionaryData();
dictEntry.forEach((target, read) -> dictionaryData.add(new CustomDictionaryEntry(target, read, ReplaceType.WORD)));
dictEntry.forEach((target, read) -> {
if (dictionaryData.getByTarget(target).isEmpty()) {
dictionaryData.add(new CustomDictionaryEntry(target, read, ReplaceType.WORD));
}
});
}

private Map<String, String> loadDict(JsonObject jo) {
Expand All @@ -280,11 +316,16 @@ private JsonObject loadJson(File file) {

try (Reader reader = new FileReader(file); Reader bufReader = new BufferedReader(reader)) {
jo = gson.fromJson(bufReader, JsonObject.class);
} catch (IOException e) {
} catch (IOException | JsonSyntaxException e) {
LOGGER.error("Loading failed {}", file.getName(), e);
return null;
}

if (jo == null) {
LOGGER.error("Empty json file {}", file.getName());
return null;
}

int version = JsonUtils.getInt(jo, "version", -1);
if (version != 0) {
LOGGER.error("Unsupported config version {}", file.getName());
Expand Down Expand Up @@ -324,14 +365,7 @@ public static void checkAndExecution(Supplier<DAO> daoProvider) {
File moveDir = new File("old_save_data-" + timeText);

try {
if (JSON_SAVE_DIR.exists()) {
Files.move(JSON_SAVE_DIR, moveDir);
}

if (GLOBAL_DICT_DIR.exists()) {
FNDataUtil.wishMkdir(moveDir);
Files.move(GLOBAL_DICT_DIR, new File(moveDir, "global_dict.json"));
}
migrator.moveOldData(moveDir);
} catch (IOException e) {
throw new IllegalStateException("Failed to move Old SaveData", e);
}
Expand Down
Loading