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);
}