Implemented content based syncing
This commit is contained in:
parent
f80b55aa50
commit
5df6de1e3a
7 changed files with 194 additions and 49 deletions
|
@ -1,6 +1,5 @@
|
|||
package ru.bclib.config;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import ru.bclib.BCLib;
|
||||
import ru.bclib.api.dataexchange.DataExchangeAPI;
|
||||
|
@ -13,9 +12,7 @@ import ru.bclib.config.ConfigKeeper.FloatEntry;
|
|||
import ru.bclib.config.ConfigKeeper.IntegerEntry;
|
||||
import ru.bclib.config.ConfigKeeper.RangeEntry;
|
||||
import ru.bclib.config.ConfigKeeper.StringEntry;
|
||||
import ru.bclib.util.JsonFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
@ -24,46 +21,46 @@ public abstract class Config {
|
|||
protected final static Map<AutoSyncID, Config> autoSyncConfigs = new HashMap<>();
|
||||
protected final ConfigKeeper keeper;
|
||||
protected final boolean autoSync;
|
||||
|
||||
protected abstract void registerEntries();
|
||||
|
||||
|
||||
protected Config(String modID, String group) {
|
||||
this(modID, group, true, false);
|
||||
}
|
||||
|
||||
protected Config(String modID, String group, boolean autoSync){
|
||||
protected Config(String modID, String group, boolean autoSync) {
|
||||
this(modID, group, autoSync, false);
|
||||
}
|
||||
|
||||
protected Config(String modID, String group, boolean autoSync, boolean diffContent) {
|
||||
BCLib.LOGGER.info("Registered Config " + modID+"."+group+" ("+autoSync+")");
|
||||
BCLib.LOGGER.info("Registered Config " + modID + "." + group + " (" + autoSync + ")");
|
||||
this.keeper = new ConfigKeeper(modID, group);
|
||||
this.registerEntries();
|
||||
this.autoSync = autoSync;
|
||||
|
||||
|
||||
if (autoSync) {
|
||||
final String uid = "CONFIG_" + modID + "_" + group;
|
||||
final AutoSyncID aid = new AutoSyncID(BCLib.MOD_ID, uid);
|
||||
DataExchangeAPI.addAutoSyncFile(aid.modID, aid.uniqueID, keeper.getConfigFile(), this::compareForSync );
|
||||
DataExchangeAPI.addAutoSyncFile(aid.modID, aid.uniqueID, keeper.getConfigFile(), this::compareForSync);
|
||||
autoSyncConfigs.put(aid, this);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean compareForSync(FileHash fileHash, FileHash fileHash1, FileContentWrapper content) {
|
||||
ByteArrayInputStream inputStream = content.getInputStream();
|
||||
final JsonObject other = JsonFactory.getJsonObject(inputStream);
|
||||
return this.keeper.compareAndUpdateForSync(other);
|
||||
return keeper.compareAndUpdateForSync(content);
|
||||
}
|
||||
|
||||
public void saveChanges() {
|
||||
this.keeper.save();
|
||||
}
|
||||
public static void reloadSyncedConfig(AutoSyncID aid, File file){
|
||||
Config cfg = autoSyncConfigs.get(aid);
|
||||
if (cfg!=null) {
|
||||
|
||||
public static void reloadSyncedConfig(AutoSyncID aid, File file) {
|
||||
Config cfg = autoSyncConfigs.get(aid);
|
||||
if (cfg != null) {
|
||||
cfg.reload();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void reload() {
|
||||
this.keeper.reload();
|
||||
BCLib.LOGGER.info("Did Reload " + keeper.getConfigFile());
|
||||
|
@ -200,6 +197,6 @@ public abstract class Config {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue