Config reload after write and hash-debug
This commit is contained in:
parent
7e36ac4159
commit
f28c3e0594
7 changed files with 76 additions and 5 deletions
|
@ -5,9 +5,13 @@ import net.fabricmc.api.EnvType;
|
||||||
import net.fabricmc.api.Environment;
|
import net.fabricmc.api.Environment;
|
||||||
import net.minecraft.network.FriendlyByteBuf;
|
import net.minecraft.network.FriendlyByteBuf;
|
||||||
import ru.bclib.api.dataexchange.handler.DataExchange;
|
import ru.bclib.api.dataexchange.handler.DataExchange;
|
||||||
|
import ru.bclib.config.Config;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class DataExchangeAPI extends DataExchange {
|
public class DataExchangeAPI extends DataExchange {
|
||||||
private final static List<String> MODS = Lists.newArrayList();
|
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) {
|
public static void addAutoSyncFile(String modID, String uniqueID, File fileName, NeedTransferPredicate needTransfer) {
|
||||||
getInstance().addAutoSyncFileData(modID, uniqueID, fileName, true, 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
abstract public class DataExchange {
|
abstract public class DataExchange {
|
||||||
@FunctionalInterface
|
@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>{
|
final static class AutoSyncTriple extends Triple<FileHash, byte[], AutoFileSyncEntry>{
|
||||||
public AutoSyncTriple(FileHash first, byte[] second, AutoFileSyncEntry third) {
|
public AutoSyncTriple(FileHash first, byte[] second, AutoFileSyncEntry third) {
|
||||||
super(first, second, 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){
|
protected void addAutoSyncFileData(String modID, String uniqueID, File fileName, boolean requestContent, NeedTransferPredicate needTransfer){
|
||||||
autoSyncFiles.add(new AutoFileSyncEntry(modID, uniqueID, fileName, requestContent, 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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,7 +128,8 @@ public class HelloClient extends DataHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient) {
|
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+")");
|
BCLib.LOGGER.info("Received Hello from Server. (client="+localBclibVersion+", server="+bclibVersion+")");
|
||||||
|
|
||||||
// if (DataFixerAPI.getModVersion(localBclibVersion) != DataFixerAPI.getModVersion(bclibVersion)){
|
// if (DataFixerAPI.getModVersion(localBclibVersion) != DataFixerAPI.getModVersion(bclibVersion)){
|
||||||
|
@ -158,12 +159,16 @@ public class HelloClient extends DataHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
BCLib.LOGGER.info(" - " + e + ": " + (willRequest ? (" ("+requestText+")" ):""));
|
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);
|
showDonwloadConfigs(client, filesToRequest);
|
||||||
return;
|
return;
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
|
|
|
@ -99,6 +99,7 @@ public class SendFiles extends DataHandler {
|
||||||
BCLib.LOGGER.info(" - Writing " + path + " (" + data.length + " Bytes)");
|
BCLib.LOGGER.info(" - Writing " + path + " (" + data.length + " Bytes)");
|
||||||
try {
|
try {
|
||||||
Files.write(path, data);
|
Files.write(path, data);
|
||||||
|
DataExchange.didReceiveFile(e, e.fileName);
|
||||||
} catch (IOException ioException) {
|
} catch (IOException ioException) {
|
||||||
BCLib.LOGGER.error(" --> Writing " + e.fileName + " failed: " + ioException);
|
BCLib.LOGGER.error(" --> Writing " + e.fileName + " failed: " + ioException);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package ru.bclib.config;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import ru.bclib.BCLib;
|
import ru.bclib.BCLib;
|
||||||
import ru.bclib.api.dataexchange.DataExchangeAPI;
|
import ru.bclib.api.dataexchange.DataExchangeAPI;
|
||||||
|
import ru.bclib.api.dataexchange.handler.DataExchange;
|
||||||
import ru.bclib.config.ConfigKeeper.BooleanEntry;
|
import ru.bclib.config.ConfigKeeper.BooleanEntry;
|
||||||
import ru.bclib.config.ConfigKeeper.Entry;
|
import ru.bclib.config.ConfigKeeper.Entry;
|
||||||
import ru.bclib.config.ConfigKeeper.FloatEntry;
|
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.RangeEntry;
|
||||||
import ru.bclib.config.ConfigKeeper.StringEntry;
|
import ru.bclib.config.ConfigKeeper.StringEntry;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public abstract class Config {
|
public abstract class Config {
|
||||||
|
protected final static Map<DataExchange.AutoSyncID, Config> autoSyncConfigs = new HashMap<>();
|
||||||
protected final ConfigKeeper keeper;
|
protected final ConfigKeeper keeper;
|
||||||
protected final boolean autoSync;
|
protected final boolean autoSync;
|
||||||
protected abstract void registerEntries();
|
protected abstract void registerEntries();
|
||||||
|
@ -26,13 +32,26 @@ public abstract class Config {
|
||||||
this.autoSync = autoSync;
|
this.autoSync = autoSync;
|
||||||
|
|
||||||
if (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() {
|
public void saveChanges() {
|
||||||
this.keeper.save();
|
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
|
@Nullable
|
||||||
public <T, E extends Entry<T>> E getEntry(ConfigKey key, Class<E> type) {
|
public <T, E extends Entry<T>> E getEntry(ConfigKey key, Class<E> type) {
|
||||||
|
@ -165,4 +184,6 @@ public abstract class Config {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import net.minecraft.util.GsonHelper;
|
import net.minecraft.util.GsonHelper;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import ru.bclib.api.dataexchange.handler.DataExchange;
|
||||||
import ru.bclib.util.JsonFactory;
|
import ru.bclib.util.JsonFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -16,7 +17,7 @@ import java.util.function.Supplier;
|
||||||
|
|
||||||
public final class ConfigKeeper {
|
public final class ConfigKeeper {
|
||||||
private final Map<ConfigKey, Entry<?>> configEntries = Maps.newHashMap();
|
private final Map<ConfigKey, Entry<?>> configEntries = Maps.newHashMap();
|
||||||
private final JsonObject configObject;
|
private JsonObject configObject;
|
||||||
private final ConfigWriter writer;
|
private final ConfigWriter writer;
|
||||||
|
|
||||||
private boolean changed = false;
|
private boolean changed = false;
|
||||||
|
@ -36,6 +37,11 @@ public final class ConfigKeeper {
|
||||||
this.changed = false;
|
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) {
|
private <T, E extends Entry<T>> void initializeEntry(ConfigKey key, E entry) {
|
||||||
if (configObject == null) {
|
if (configObject == null) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -38,6 +38,11 @@ public class ConfigWriter {
|
||||||
save(configFile, configObject);
|
save(configFile, configObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonObject reload() {
|
||||||
|
configObject = load(configFile);
|
||||||
|
return configObject;
|
||||||
|
}
|
||||||
|
|
||||||
public JsonObject load() {
|
public JsonObject load() {
|
||||||
if (configObject == null) {
|
if (configObject == null) {
|
||||||
configObject = load(configFile);
|
configObject = load(configFile);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue