Receive requested file contents on client
This commit is contained in:
parent
869dc762fa
commit
d01b7923aa
3 changed files with 70 additions and 11 deletions
|
@ -97,15 +97,27 @@ abstract public class DataExchange {
|
||||||
return new byte[0];
|
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){
|
public void serialize(FriendlyByteBuf buf){
|
||||||
getFileHash().serialize(buf);
|
getFileHash().serialize(buf);
|
||||||
buf.writeBoolean(requestContent);
|
buf.writeBoolean(requestContent);
|
||||||
|
|
||||||
if (requestContent) {
|
if (requestContent) {
|
||||||
byte[] content = getContent();
|
serializeFileContent(buf);
|
||||||
buf.writeInt(content.length);
|
|
||||||
buf.writeByteArray(content);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,19 +132,38 @@ abstract public class DataExchange {
|
||||||
boolean withContent = buf.readBoolean();
|
boolean withContent = buf.readBoolean();
|
||||||
byte[] data = null;
|
byte[] data = null;
|
||||||
if (withContent) {
|
if (withContent) {
|
||||||
int size = buf.readInt();
|
data = deserializeFileContent(buf);
|
||||||
data = buf.readByteArray(size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Pair(hash, data);
|
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){
|
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
|
return DataExchange
|
||||||
.getInstance()
|
.getInstance()
|
||||||
.autoSyncFiles
|
.autoSyncFiles
|
||||||
.stream()
|
.stream()
|
||||||
.filter(asf -> asf.modID.equals(hash.modID) && asf.uniqueID.equals(hash.uniqueID))
|
.filter(asf -> asf.modID.equals(modID) && asf.uniqueID.equals(uniqueID))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import ru.bclib.api.dataexchange.handler.DataExchange.AutoSyncID;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class RequestFiles extends DataHandler {
|
public class RequestFiles extends DataHandler {
|
||||||
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "request_files"), RequestFiles::new, false, false);
|
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
|
@Override
|
||||||
protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,22 +8,44 @@ import net.minecraft.server.MinecraftServer;
|
||||||
import ru.bclib.BCLib;
|
import ru.bclib.BCLib;
|
||||||
import ru.bclib.api.dataexchange.DataHandler;
|
import ru.bclib.api.dataexchange.DataHandler;
|
||||||
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
|
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 class SendFiles extends DataHandler {
|
||||||
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "send_files"), SendFiles::new, false, false);
|
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);
|
super(DESCRIPTOR.IDENTIFIER, true);
|
||||||
|
this.files = files;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void serializeData(FriendlyByteBuf buf) {
|
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
|
@Override
|
||||||
protected void deserializeFromIncomingData(FriendlyByteBuf buf, PacketSender responseSender, boolean fromClient) {
|
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
|
@Override
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue