*WIP* Prepared Folder Syncing - Filelist exchange
This commit is contained in:
parent
5df6de1e3a
commit
1f239baeb9
9 changed files with 323 additions and 190 deletions
|
@ -103,6 +103,7 @@ class AutoFileSyncEntry extends AutoSyncID {
|
|||
data = buf.readByteArray(size);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
public static AutoFileSyncEntry findMatching(FileHash hash) {
|
||||
return findMatching(hash.modID, hash.uniqueID);
|
||||
|
@ -115,7 +116,7 @@ class AutoFileSyncEntry extends AutoSyncID {
|
|||
public static AutoFileSyncEntry findMatching(String modID, String uniqueID) {
|
||||
return DataExchange
|
||||
.getInstance()
|
||||
.autoSyncFiles
|
||||
.getAutoSyncFiles()
|
||||
.stream()
|
||||
.filter(asf -> asf.modID.equals(modID) && asf.uniqueID.equals(uniqueID))
|
||||
.findFirst()
|
||||
|
|
|
@ -4,14 +4,17 @@ import net.fabricmc.api.EnvType;
|
|||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import ru.bclib.api.dataexchange.ConnectorClientside;
|
||||
import ru.bclib.api.dataexchange.ConnectorServerside;
|
||||
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.config.Configs;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
@ -19,6 +22,12 @@ import java.util.Set;
|
|||
import java.util.function.BiConsumer;
|
||||
|
||||
abstract public class DataExchange {
|
||||
public final static Path SYNC_FOLDER = FabricLoader.getInstance()
|
||||
.getGameDir()
|
||||
.resolve("bclib-sync")
|
||||
.toAbsolutePath();
|
||||
public final static String SYNC_FOLDER_ID = "BCLIB-SYNC";
|
||||
|
||||
@FunctionalInterface
|
||||
public interface NeedTransferPredicate {
|
||||
public boolean test(FileHash clientHash, FileHash serverHash, FileContentWrapper content);
|
||||
|
@ -54,7 +63,9 @@ abstract public class DataExchange {
|
|||
protected ConnectorServerside server;
|
||||
protected ConnectorClientside client;
|
||||
protected final Set<DataHandlerDescriptor> descriptors;
|
||||
protected final List<AutoFileSyncEntry> autoSyncFiles = new ArrayList<>(4);
|
||||
private final List<AutoFileSyncEntry> autoSyncFiles = new ArrayList<>(4);
|
||||
|
||||
private boolean didLoadSyncFolder = false;
|
||||
|
||||
abstract protected ConnectorClientside clientSupplier(DataExchange api);
|
||||
abstract protected ConnectorServerside serverSupplier(DataExchange api);
|
||||
|
@ -64,7 +75,11 @@ abstract public class DataExchange {
|
|||
}
|
||||
|
||||
public Set<DataHandlerDescriptor> getDescriptors() { return descriptors; }
|
||||
|
||||
|
||||
public List<AutoFileSyncEntry> getAutoSyncFiles(){
|
||||
return autoSyncFiles;
|
||||
}
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
protected void initClientside(){
|
||||
if (client!=null) return;
|
||||
|
@ -177,4 +192,43 @@ abstract public class DataExchange {
|
|||
static void didReceiveFile(AutoSyncID aid, File file){
|
||||
onWriteCallbacks.forEach(fkt -> fkt.accept(aid, file));
|
||||
}
|
||||
|
||||
private List<String> syncFolderContent;
|
||||
protected List<String> getSyncFolderContent(){
|
||||
if (syncFolderContent==null){
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
return syncFolderContent;
|
||||
}
|
||||
|
||||
//we call this from HelloServer to prepare transfer
|
||||
protected void loadSyncFolder() {
|
||||
if (Configs.MAIN_CONFIG.getBoolean(Configs.MAIN_SYNC_CATEGORY, "offserSyncFolder", true))
|
||||
{
|
||||
final File syncPath = SYNC_FOLDER.toFile();
|
||||
if (!syncPath.exists()) {
|
||||
syncPath.mkdirs();
|
||||
}
|
||||
|
||||
if (syncFolderContent == null) {
|
||||
syncFolderContent = new ArrayList<>(8);
|
||||
addFilesForSyncFolder(syncPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addFilesForSyncFolder(File path){
|
||||
for (final File f : path.listFiles()) {
|
||||
if (f.isDirectory()) {
|
||||
addFilesForSyncFolder(f);
|
||||
} else if (f.isFile()) {
|
||||
if (!f.getName().startsWith(".")) {
|
||||
Path p = f.toPath();
|
||||
p = SYNC_FOLDER.relativize(p);
|
||||
syncFolderContent.add(p.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,11 +77,11 @@ public class HelloClient extends DataHandler {
|
|||
buf.writeInt(0);
|
||||
}
|
||||
|
||||
if (Configs.MAIN_CONFIG.getBoolean(Configs.MAIN_SYNC_CATEGORY, "offerConfigs", true)) {
|
||||
if (Configs.MAIN_CONFIG.getBoolean(Configs.MAIN_SYNC_CATEGORY, "offerFiles", true)) {
|
||||
//do only include files that exist on the server
|
||||
final List<AutoFileSyncEntry> existingAutoSyncFiles = DataExchange
|
||||
.getInstance()
|
||||
.autoSyncFiles
|
||||
.getAutoSyncFiles()
|
||||
.stream()
|
||||
.filter(e -> e.fileName.exists())
|
||||
.collect(Collectors.toList());
|
||||
|
@ -93,9 +93,22 @@ public class HelloClient extends DataHandler {
|
|||
BCLib.LOGGER.info(" - Offering File " + entry);
|
||||
}
|
||||
} else {
|
||||
BCLib.LOGGER.info("Server will not offer Configs.");
|
||||
BCLib.LOGGER.info("Server will not offer Files.");
|
||||
buf.writeInt(0);
|
||||
}
|
||||
|
||||
//for the moment this is only hardcoded for the sync-folder offered by BCLIB, but it can be extended in future
|
||||
if (Configs.MAIN_CONFIG.getBoolean(Configs.MAIN_SYNC_CATEGORY, "offserSyncFolder", true)) {
|
||||
buf.writeInt(1); //currently we do only sync a single folder
|
||||
writeString(buf, DataExchange.SYNC_FOLDER_ID); //the UID of the Folder
|
||||
final List<String> fileNames = DataExchange.getInstance().getSyncFolderContent();
|
||||
buf.writeInt(fileNames.size());
|
||||
fileNames.forEach(fl -> writeString(buf, fl));
|
||||
} else {
|
||||
BCLib.LOGGER.info("Server will not offer Sync Folders.");
|
||||
buf.writeInt(0);
|
||||
}
|
||||
Configs.MAIN_CONFIG.saveChanges();
|
||||
}
|
||||
|
||||
String bclibVersion ="0.0.0";
|
||||
|
@ -124,6 +137,25 @@ public class HelloClient extends DataHandler {
|
|||
autoSyncedFiles.add(t);
|
||||
//System.out.println(t.first);
|
||||
}
|
||||
|
||||
//since this version we also send the sync folders
|
||||
if (DataFixerAPI.isLargerOrEqualVersion(bclibVersion, "0.4.1")) {
|
||||
final int folderCount = buf.readInt();
|
||||
for (int i=0; i<folderCount; i++){
|
||||
final String folderID = readString(buf);
|
||||
final int entries = buf.readInt();
|
||||
List<String> files = new ArrayList<>(entries);
|
||||
for (int j=0; j<entries; j++){
|
||||
files.add(readString(buf));
|
||||
}
|
||||
|
||||
if (folderID.equals(DataExchange.SYNC_FOLDER_ID)) {
|
||||
//TODO: implement the syncing here
|
||||
} else {
|
||||
BCLib.LOGGER.warning("Unknown Sync-Folder '"+folderID+"'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -84,5 +84,7 @@ public class HelloServer extends DataHandler {
|
|||
} else {
|
||||
BCLib.LOGGER.info("Auto-Sync was disabled on the server.");
|
||||
}
|
||||
|
||||
DataExchange.getInstance().loadSyncFolder();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue