Send file requests
This commit is contained in:
parent
ae344c48ac
commit
869dc762fa
5 changed files with 163 additions and 11 deletions
|
@ -34,10 +34,29 @@ abstract public class DataExchange {
|
|||
public boolean test(FileHash clientHash, FileHash serverHash, byte[] content);
|
||||
}
|
||||
|
||||
final static class AutoSyncID extends Pair<String, String>{
|
||||
public AutoSyncID(String modID, String uniqueID) {
|
||||
super(modID, uniqueID);
|
||||
}
|
||||
|
||||
public String getModID() { return this.first; }
|
||||
public String getUniqueID() { return this.second; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return first+"."+second;
|
||||
}
|
||||
}
|
||||
|
||||
final static class AutoSyncTriple extends Triple<FileHash, byte[], DataExchange.AutoFileSyncEntry>{
|
||||
public AutoSyncTriple(FileHash first, byte[] second, AutoFileSyncEntry third) {
|
||||
super(first, second, third);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return first.modID+"."+first.uniqueID;
|
||||
}
|
||||
}
|
||||
static class AutoFileSyncEntry {
|
||||
public final NeedTransferPredicate needTransfer;
|
||||
|
|
|
@ -14,10 +14,9 @@ import ru.bclib.BCLib;
|
|||
import ru.bclib.api.dataexchange.DataExchangeAPI;
|
||||
import ru.bclib.api.dataexchange.DataHandler;
|
||||
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
|
||||
import ru.bclib.api.dataexchange.FileHash;
|
||||
import ru.bclib.api.dataexchange.handler.DataExchange.AutoSyncID;
|
||||
import ru.bclib.api.datafixer.DataFixerAPI;
|
||||
import ru.bclib.gui.screens.WarnBCLibVersionMismatch;
|
||||
import ru.bclib.util.Triple;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
@ -108,22 +107,35 @@ public class HelloClient extends DataHandler {
|
|||
String localBclibVersion = getBCLibVersion();
|
||||
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)){
|
||||
showBCLibError(client);
|
||||
return;
|
||||
}
|
||||
|
||||
List<AutoSyncID> filesToRequest = new ArrayList<>(4);
|
||||
|
||||
for (Entry<String, String> e : modVersion.entrySet()){
|
||||
String ver = getModVersion(e.getKey());
|
||||
BCLib.LOGGER.info(" - " + e.getKey() + " (client="+ver+", server="+ver+")");
|
||||
}
|
||||
|
||||
BCLib.LOGGER.info("Server offered Files to sync.");
|
||||
for (DataExchange.AutoSyncTriple e : autoSyncedFiles) {
|
||||
boolean willRequest = false;
|
||||
if (e.third == null) {
|
||||
BCLib.LOGGER.info(" - File " + e.first.modID + "." + e.first.uniqueID + ": Does not exist on client.");
|
||||
filesToRequest.add(new AutoSyncID(e.first.modID, e.first.uniqueID));
|
||||
willRequest = true;
|
||||
BCLib.LOGGER.info(" - File " + e + ": Does not exist on client.");
|
||||
} else if (e.third.needTransfer.test(e.third.getFileHash(), e.first, e.second)) {
|
||||
BCLib.LOGGER.info(" - File " + e.first.modID + "." + e.first.uniqueID + ": Needs Transfer");
|
||||
willRequest = true;
|
||||
filesToRequest.add(new AutoSyncID(e.first.modID, e.first.uniqueID));
|
||||
BCLib.LOGGER.info(" - File " + e + ": Needs Transfer");
|
||||
}
|
||||
|
||||
BCLib.LOGGER.info(" - " + e + ": " + (willRequest ? " (requesting)":""));
|
||||
}
|
||||
if (filesToRequest.size()>0) {
|
||||
showDonwloadConfigs(client, filesToRequest);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -133,14 +145,28 @@ public class HelloClient extends DataHandler {
|
|||
client.setScreen(new WarnBCLibVersionMismatch((download) -> {
|
||||
Minecraft.getInstance().setScreen((Screen)null);
|
||||
if (download){
|
||||
requestDownloads((hadErrors)->{
|
||||
requestBCLibDownload((hadErrors)->{
|
||||
client.stop();
|
||||
});
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private void requestDownloads(Consumer<Boolean> whenFinished){
|
||||
@Environment(EnvType.CLIENT)
|
||||
protected void showDonwloadConfigs(Minecraft client, List<AutoSyncID> files){
|
||||
requestFileDownloads((hadErrors)->{
|
||||
client.stop();
|
||||
}, files);
|
||||
}
|
||||
|
||||
private void requestBCLibDownload(Consumer<Boolean> whenFinished){
|
||||
BCLib.LOGGER.warning("Starting download of BCLib");
|
||||
whenFinished.accept(true);
|
||||
}
|
||||
|
||||
private void requestFileDownloads(Consumer<Boolean> whenFinished, List<AutoSyncID> files){
|
||||
BCLib.LOGGER.info("Starting download of Files:" + files.size());
|
||||
DataExchangeAPI.send(new RequestFiles(files));
|
||||
whenFinished.accept(true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
package ru.bclib.api.dataexchange.handler;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketSender;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.fabricmc.loader.api.ModContainer;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
|
@ -12,6 +11,8 @@ import ru.bclib.api.dataexchange.DataHandler;
|
|||
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
|
||||
import ru.bclib.api.datafixer.DataFixerAPI;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* This message is sent once a player enters the world. It initiates a sequence of Messages that will sync files between both
|
||||
* client and server.
|
||||
|
@ -36,7 +37,20 @@ import ru.bclib.api.datafixer.DataFixerAPI;
|
|||
* <td>{@link HelloClient}</td>
|
||||
* <td>--></td>
|
||||
* <td></td>
|
||||
* <td>Sends the current BClIb-Version and the Version of all Plugins on the Server</td>
|
||||
* <td>Sends the current BClIb-Version, the Version of all Plugins and data for all AutpoSync-Files
|
||||
* ({@link DataExchangeAPI#addAutoSyncFile(String, File)} on the Server</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td></td>
|
||||
* <td><--</td>
|
||||
* <td>{@link RequestFiles}</td>
|
||||
* <td>Request missing or out of sync Files from the Server</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@link SendFiles}</td>
|
||||
* <td>--></td>
|
||||
* <td></td>
|
||||
* <td>Send Files from the Server to the Client</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
package ru.bclib.api.dataexchange.handler;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketSender;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import ru.bclib.BCLib;
|
||||
import ru.bclib.api.dataexchange.DataHandler;
|
||||
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
|
||||
import ru.bclib.api.dataexchange.handler.DataExchange.AutoSyncID;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RequestFiles extends DataHandler {
|
||||
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "request_files"), RequestFiles::new, false, false);
|
||||
|
||||
protected List<AutoSyncID> files;
|
||||
private RequestFiles(){
|
||||
this(null);
|
||||
}
|
||||
|
||||
public RequestFiles(List<AutoSyncID> files) {
|
||||
super(DESCRIPTOR.IDENTIFIER, false);
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void serializeData(FriendlyByteBuf buf) {
|
||||
buf.writeInt(files.size());
|
||||
|
||||
for (AutoSyncID a : files){
|
||||
writeString(buf, a.getModID());
|
||||
writeString(buf, a.getUniqueID());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deserializeFromIncomingData(FriendlyByteBuf buf, PacketSender responseSender, boolean fromClient) {
|
||||
int size = buf.readInt();
|
||||
files = new ArrayList<>(size);
|
||||
|
||||
BCLib.LOGGER.info("Client requested " + size + " Files:");
|
||||
for (int i=0; i<size; i++){
|
||||
String modID = readString(buf);
|
||||
String uID = readString(buf);
|
||||
AutoSyncID asid = new AutoSyncID(modID, uID);
|
||||
files.add(asid);
|
||||
BCLib.LOGGER.info(" - " + asid);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient) {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package ru.bclib.api.dataexchange.handler;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketSender;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import ru.bclib.BCLib;
|
||||
import ru.bclib.api.dataexchange.DataHandler;
|
||||
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
|
||||
|
||||
public class SendFiles extends DataHandler {
|
||||
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "send_files"), SendFiles::new, false, false);
|
||||
|
||||
public SendFiles() {
|
||||
super(DESCRIPTOR.IDENTIFIER, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void serializeData(FriendlyByteBuf buf) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deserializeFromIncomingData(FriendlyByteBuf buf, PacketSender responseSender, boolean fromClient) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue