Config reload after write and hash-debug

This commit is contained in:
Frank 2021-08-14 15:05:41 +02:00
parent 7e36ac4159
commit f28c3e0594
7 changed files with 76 additions and 5 deletions

View file

@ -5,9 +5,13 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.network.FriendlyByteBuf;
import ru.bclib.api.dataexchange.handler.DataExchange;
import ru.bclib.config.Config;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class DataExchangeAPI extends DataExchange {
private final static List<String> MODS = Lists.newArrayList();
@ -148,4 +152,19 @@ public class DataExchangeAPI extends DataExchange {
public static void addAutoSyncFile(String modID, String uniqueID, File fileName, NeedTransferPredicate needTransfer) {
getInstance().addAutoSyncFileData(modID, uniqueID, fileName, true, needTransfer);
}
/**
* Register a function that is called whenever the client receives a file from the server and replaced toe local
* file with the new content.
* <p>
* This callback is usefull if you need to reload the new content before the game is quit.
* @param callback A Function that receives the AutoSyncID as well as the Filename.
*/
public static void addOnWriteCallback(BiConsumer<AutoSyncID, File> callback) {
onWriteCallbacks.add(callback);
}
static {
addOnWriteCallback(Config::reloadSyncedConfig);
}
}

View file

@ -19,6 +19,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
abstract public class DataExchange {
@FunctionalInterface
@ -69,6 +70,8 @@ abstract public class DataExchange {
}
}
protected final static List<BiConsumer<AutoSyncID, File>> onWriteCallbacks = new ArrayList<>(2);
final static class AutoSyncTriple extends Triple<FileHash, byte[], AutoFileSyncEntry>{
public AutoSyncTriple(FileHash first, byte[] second, AutoFileSyncEntry third) {
super(first, second, third);
@ -203,4 +206,15 @@ abstract public class DataExchange {
protected void addAutoSyncFileData(String modID, String uniqueID, File fileName, boolean requestContent, NeedTransferPredicate needTransfer){
autoSyncFiles.add(new AutoFileSyncEntry(modID, uniqueID, fileName, requestContent, needTransfer));
}
/**
* Called when {@code SendFiles} received a File on the Client and wrote it to the FileSystem.
* <p>
* This is the place where reload Code should go.
* @param aid The ID of the received File
* @param file The location of the FIle on the client
*/
static void didReceiveFile(AutoSyncID aid, File file){
onWriteCallbacks.forEach(fkt -> fkt.accept(aid, file));
}
}

View file

@ -128,7 +128,8 @@ public class HelloClient extends DataHandler {
@Override
protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient) {
String localBclibVersion = getBCLibVersion();
final boolean debugHashes = Configs.CLIENT_CONFIG.getBoolean(Configs.MAIN_SYNC_CATEGORY, "debugHashes", false);
final String localBclibVersion = getBCLibVersion();
BCLib.LOGGER.info("Received Hello from Server. (client="+localBclibVersion+", server="+bclibVersion+")");
// if (DataFixerAPI.getModVersion(localBclibVersion) != DataFixerAPI.getModVersion(bclibVersion)){
@ -158,12 +159,16 @@ public class HelloClient extends DataHandler {
}
BCLib.LOGGER.info(" - " + e + ": " + (willRequest ? (" ("+requestText+")" ):""));
if (debugHashes) {
BCLib.LOGGER.info(" * " + e.first + " (Server)");
BCLib.LOGGER.info(" * " + e.third.getFileHash() + " (Client)");
}
}
/*if (filesToRequest.size()>0 && SendFiles.acceptFiles()) {
if (filesToRequest.size()>0 && SendFiles.acceptFiles()) {
showDonwloadConfigs(client, filesToRequest);
return;
}*/
}
}
@Environment(EnvType.CLIENT)

View file

@ -99,6 +99,7 @@ public class SendFiles extends DataHandler {
BCLib.LOGGER.info(" - Writing " + path + " (" + data.length + " Bytes)");
try {
Files.write(path, data);
DataExchange.didReceiveFile(e, e.fileName);
} catch (IOException ioException) {
BCLib.LOGGER.error(" --> Writing " + e.fileName + " failed: " + ioException);
}

View file

@ -3,6 +3,7 @@ package ru.bclib.config;
import org.jetbrains.annotations.Nullable;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataExchangeAPI;
import ru.bclib.api.dataexchange.handler.DataExchange;
import ru.bclib.config.ConfigKeeper.BooleanEntry;
import ru.bclib.config.ConfigKeeper.Entry;
import ru.bclib.config.ConfigKeeper.FloatEntry;
@ -10,7 +11,12 @@ import ru.bclib.config.ConfigKeeper.IntegerEntry;
import ru.bclib.config.ConfigKeeper.RangeEntry;
import ru.bclib.config.ConfigKeeper.StringEntry;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public abstract class Config {
protected final static Map<DataExchange.AutoSyncID, Config> autoSyncConfigs = new HashMap<>();
protected final ConfigKeeper keeper;
protected final boolean autoSync;
protected abstract void registerEntries();
@ -26,13 +32,26 @@ public abstract class Config {
this.autoSync = autoSync;
if (autoSync) {
DataExchangeAPI.addAutoSyncFile(BCLib.MOD_ID, "CONFIG_" + modID + "_" + group, keeper.getConfigFile());
final String uid = "CONFIG_" + modID + "_" + group;
DataExchangeAPI.addAutoSyncFile(BCLib.MOD_ID, uid, keeper.getConfigFile());
autoSyncConfigs.put(new DataExchange.AutoSyncID(modID, uid), this);
}
}
public void saveChanges() {
this.keeper.save();
}
public static void reloadSyncedConfig(DataExchange.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());
}
@Nullable
public <T, E extends Entry<T>> E getEntry(ConfigKey key, Class<E> type) {
@ -165,4 +184,6 @@ public abstract class Config {
}
return false;
}
}

View file

@ -6,6 +6,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.util.GsonHelper;
import org.jetbrains.annotations.Nullable;
import ru.bclib.api.dataexchange.handler.DataExchange;
import ru.bclib.util.JsonFactory;
import java.io.File;
@ -16,7 +17,7 @@ import java.util.function.Supplier;
public final class ConfigKeeper {
private final Map<ConfigKey, Entry<?>> configEntries = Maps.newHashMap();
private final JsonObject configObject;
private JsonObject configObject;
private final ConfigWriter writer;
private boolean changed = false;
@ -35,6 +36,11 @@ public final class ConfigKeeper {
this.writer.save();
this.changed = false;
}
void reload() {
this.configObject = this.writer.reload();
this.changed = false;
}
private <T, E extends Entry<T>> void initializeEntry(ConfigKey key, E entry) {
if (configObject == null) {

View file

@ -37,6 +37,11 @@ public class ConfigWriter {
}
save(configFile, configObject);
}
JsonObject reload() {
configObject = load(configFile);
return configObject;
}
public JsonObject load() {
if (configObject == null) {