Receive requested file contents on client

This commit is contained in:
Frank 2021-08-06 21:27:33 +02:00
parent 869dc762fa
commit d01b7923aa
3 changed files with 70 additions and 11 deletions

View file

@ -97,15 +97,27 @@ abstract public class DataExchange {
return new byte[0];
}
public void serializeContent(FriendlyByteBuf buf){
DataHandler.writeString(buf, modID);
DataHandler.writeString(buf, uniqueID);
serializeFileContent(buf);
}
public static Pair<AutoFileSyncEntry, byte[]> deserializeContent(FriendlyByteBuf buf){
final String modID = DataHandler.readString(buf);
final String uniqueID = DataHandler.readString(buf);
byte[] data = deserializeFileContent(buf);
AutoFileSyncEntry entry = AutoFileSyncEntry.findMatching(modID, uniqueID);
return new Pair<>(entry, data);
}
public void serialize(FriendlyByteBuf buf){
getFileHash().serialize(buf);
buf.writeBoolean(requestContent);
if (requestContent) {
byte[] content = getContent();
buf.writeInt(content.length);
buf.writeByteArray(content);
serializeFileContent(buf);
}
}
@ -120,19 +132,38 @@ abstract public class DataExchange {
boolean withContent = buf.readBoolean();
byte[] data = null;
if (withContent) {
int size = buf.readInt();
data = buf.readByteArray(size);
data = deserializeFileContent(buf);
}
return new Pair(hash, data);
}
private void serializeFileContent(FriendlyByteBuf buf) {
byte[] content = getContent();
buf.writeInt(content.length);
buf.writeByteArray(content);
}
private static byte[] deserializeFileContent(FriendlyByteBuf buf) {
byte[] data;
int size = buf.readInt();
data = buf.readByteArray(size);
return data;
}
public static AutoFileSyncEntry findMatching(FileHash hash){
return findMatching(hash.modID, hash.uniqueID);
}
public static AutoFileSyncEntry findMatching(AutoSyncID aid){
return findMatching(aid.getModID(), aid.getUniqueID());
}
public static AutoFileSyncEntry findMatching(String modID, String uniqueID){
return DataExchange
.getInstance()
.autoSyncFiles
.stream()
.filter(asf -> asf.modID.equals(hash.modID) && asf.uniqueID.equals(hash.uniqueID))
.filter(asf -> asf.modID.equals(modID) && asf.uniqueID.equals(uniqueID))
.findFirst()
.orElse(null);
}

View file

@ -12,6 +12,7 @@ import ru.bclib.api.dataexchange.handler.DataExchange.AutoSyncID;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RequestFiles extends DataHandler {
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "request_files"), RequestFiles::new, false, false);
@ -55,6 +56,11 @@ public class RequestFiles extends DataHandler {
@Override
protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient) {
List<DataExchange.AutoFileSyncEntry> syncEntries = files
.stream().map(asid -> DataExchange.AutoFileSyncEntry.findMatching(asid))
.filter(e -> e!=null)
.collect(Collectors.toList());
reply(new SendFiles(syncEntries), server);
}
}

View file

@ -8,22 +8,44 @@ import net.minecraft.server.MinecraftServer;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.util.Pair;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SendFiles extends DataHandler {
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "send_files"), SendFiles::new, false, false);
public SendFiles() {
protected List<DataExchange.AutoFileSyncEntry> files;
public SendFiles(){
this(null);
}
public SendFiles(List<DataExchange.AutoFileSyncEntry> files) {
super(DESCRIPTOR.IDENTIFIER, true);
this.files = files;
}
@Override
protected void serializeData(FriendlyByteBuf buf) {
List<DataExchange.AutoFileSyncEntry> existingFiles = files.stream().filter(e -> e.fileName.exists()).collect(Collectors.toList());
buf.writeInt(existingFiles.size());
for (DataExchange.AutoFileSyncEntry entry : existingFiles) {
entry.serializeContent(buf);
}
}
@Override
protected void deserializeFromIncomingData(FriendlyByteBuf buf, PacketSender responseSender, boolean fromClient) {
int size = buf.readInt();
List<Pair<DataExchange.AutoFileSyncEntry, byte[]>> receivedFiles = new ArrayList<>(size);
BCLib.LOGGER.info("Server sent " + size + " Files:");
for (int i=0; i<size; i++){
Pair<DataExchange.AutoFileSyncEntry, byte[]> p = DataExchange.AutoFileSyncEntry.deserializeContent(buf);
receivedFiles.add(p);
BCLib.LOGGER.info(" - " + p.first + " (" + p.second.length + " Bytes)");
}
}
@Override