diff options
author | Keuin <[email protected]> | 2024-01-20 12:44:17 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2024-01-20 13:02:22 +0800 |
commit | 6d53613a57d542a424d6368cb91c69dfd5c6772e (patch) | |
tree | 640510455e37cc32064da5c495256b4c0f912744 | |
parent | 6087003104bcc21a66bc3534b5606b999acf95db (diff) |
add plugin-wide config; enable CoW through config file
-rw-r--r-- | build.gradle | 21 | ||||
-rw-r--r-- | gradle.properties | 2 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java | 12 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java | 16 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java | 56 |
5 files changed, 98 insertions, 9 deletions
diff --git a/build.gradle b/build.gradle index 67b3e86..22bac88 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ plugins { id 'fabric-loom' version '1.0-SNAPSHOT' id 'maven-publish' + id 'com.github.johnrengelman.shadow' version '7.1.2' } sourceCompatibility = JavaVersion.VERSION_1_8 @@ -20,6 +21,20 @@ repositories { // for more information about repositories. } +shadowJar { + configurations = [project.configurations.shadow] + + // mitigate log4j security problem + exclude 'org/apache/logging/log4j/core/lookup/JndiLookup.class' + exclude 'META-INF' +} + +remapJar { + dependsOn(shadowJar) + mustRunAfter(shadowJar) + inputFile = tasks.shadowJar.archiveFile +} + dependencies { implementation 'junit:junit:4.13.2' @@ -31,6 +46,10 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind + modImplementation 'com.fasterxml.jackson.core:jackson-databind:2.16.1' + shadow 'com.fasterxml.jackson.core:jackson-databind:2.16.1' + // Uncomment the following line to enable the deprecated Fabric API modules. // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. @@ -54,7 +73,7 @@ java { jar { from("LICENSE") { - rename { "${it}_${project.archivesBaseName}"} + rename { "${it}_${project.archivesBaseName}" } } } diff --git a/gradle.properties b/gradle.properties index f9215ba..c369c38 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.14.4 yarn_mappings=1.14.4+build.18 loader_version=0.14.12 # Mod Properties -mod_version=1.8.0 +mod_version=1.8.1 maven_group=com.keuin.kbackupfabric archives_base_name=kbackup-fabric # Dependencies diff --git a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java index 362abb8..8b55d1e 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java +++ b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java @@ -2,6 +2,7 @@ package com.keuin.kbackupfabric; import com.keuin.kbackupfabric.backup.BackupFilesystemUtil; import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider; +import com.keuin.kbackupfabric.config.KBackupConfig; import com.keuin.kbackupfabric.event.OnPlayerConnect; import com.keuin.kbackupfabric.metadata.BackupMetadata; import com.keuin.kbackupfabric.metadata.MetadataHolder; @@ -29,10 +30,17 @@ import static org.apache.commons.io.FileUtils.forceDelete; public final class KBPluginEvents implements ModInitializer { //private static final Logger LOGGER = LogManager.getLogger(); - @Override public void onInitialize() { - System.out.println("Binding events and commands ..."); + PrintUtil.info("Reading config..."); + try { + KBackupConfig.load(); + } catch (IOException e) { + PrintUtil.error("Failed to read config file, using default values: " + e + e.getMessage()); + } + boolean incCow = KBackupConfig.getInstance().getIncbakCow(); + PrintUtil.info("Incremental backup CoW: " + (incCow ? "enabled" : "disabled")); + PrintUtil.info("Binding events and commands..."); CommandRegistrationCallback.EVENT.register(KBCommandsRegister::registerCommands); OnPlayerConnect.ON_PLAYER_CONNECT.register((connection, player) -> NotificationManager.INSTANCE.notifyPlayer(DistinctNotifiable.fromServerPlayerEntity(player))); diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java index a680db7..53143f8 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -4,6 +4,7 @@ import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2; import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionIterator; import com.keuin.kbackupfabric.backup.incremental.ObjectElement; import com.keuin.kbackupfabric.backup.incremental.identifier.ObjectIdentifier; +import com.keuin.kbackupfabric.config.KBackupConfig; import com.keuin.kbackupfabric.util.FilesystemUtil; import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.cow.FileCopier; @@ -29,14 +30,19 @@ public class IncrementalBackupStorageManager { private final Logger logger = Logger.getLogger(IncrementalBackupStorageManager.class.getName()); private final Path backupStorageBase; private final Logger LOGGER = Logger.getLogger(IncrementalBackupStorageManager.class.getName()); - private FileCopier copier = null; + private FileCopier copier; public IncrementalBackupStorageManager(Path backupStorageBase) { this.backupStorageBase = backupStorageBase; - try { - this.copier = FileCowCopier.getInstance(); - } catch (Exception | UnsatisfiedLinkError ex) { - PrintUtil.error("Failed to initialize kbackup-cow: " + ex + ex.getMessage()); + if (KBackupConfig.getInstance().getIncbakCow()) { + // try to use cow copier, if failed, fallback to normal copier + try { + this.copier = FileCowCopier.getInstance(); + } catch (Exception | UnsatisfiedLinkError ex) { + PrintUtil.error("Failed to initialize kbackup-cow: " + ex + ex.getMessage()); + this.copier = new FileEagerCopier(); + } + } else { this.copier = new FileEagerCopier(); } if (this.copier.isCow()) { diff --git a/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java b/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java new file mode 100644 index 0000000..8ece8d2 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java @@ -0,0 +1,56 @@ +package com.keuin.kbackupfabric.config; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.keuin.kbackupfabric.util.PrintUtil; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class KBackupConfig { + + private static KBackupConfig instance = getDefault(); + private static final String CONFIG_FILE = "kbackup.json"; + + @JsonProperty("incbak_cow") + private Boolean incbakCow; + + public static KBackupConfig getInstance() { + return instance; + } + + private static KBackupConfig getDefault() { + return new KBackupConfig(false); + } + + public static void load() throws IOException { + File file = new File(CONFIG_FILE); + ObjectMapper om = new ObjectMapper(); + try { + instance = om.readValue(file, KBackupConfig.class); + } catch (FileNotFoundException ignored) { + // generate default config file + PrintUtil.info("Config file does not exist. Creating default config: " + CONFIG_FILE); + instance = getDefault(); + ObjectWriter w = om.writerWithDefaultPrettyPrinter(); + w.writeValue(file, instance); + } + } + + public KBackupConfig() { + } + + public KBackupConfig(Boolean incbakCow) { + this.incbakCow = incbakCow; + } + + public Boolean getIncbakCow() { + return this.incbakCow; + } + + public void setIncbakCow(Boolean incbakCow) { + this.incbakCow = incbakCow; + } +}
\ No newline at end of file |