Skip to content

Commit

Permalink
[#515] Implemented a mechanism to selectively disable auto-updates fo…
Browse files Browse the repository at this point in the history
…r bulk syncing large amount of objects
  • Loading branch information
SirEndii committed Aug 5, 2024
1 parent 50c06d4 commit 4b61372
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.srendi.advancedperipherals.client.smartglasses;

import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.ObjectDecodeRegistry;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Circle;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Panel;
Expand All @@ -20,8 +19,12 @@ public class OverlayObjectHolder {
public static void addOrUpdateObject(RenderableObject object) {
removeObject(object.getId());
objects.add(object);
AdvancedPeripherals.debug("Added object to client renderer " + object);
AdvancedPeripherals.debug("Having objects " + objects);
}

public static void addOrUpdateObjects(RenderableObject[] object) {
for (RenderableObject renderableObject : object) {
addOrUpdateObject(renderableObject);
}
}

public static List<RenderableObject> getObjects() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.common.network.base.IPacket;
import de.srendi.advancedperipherals.common.network.toclient.DistanceDetectorSyncPacket;
import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectBulkSyncPacket;
import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectClearPacket;
import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectDeletePacket;
import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectSyncPacket;
Expand Down Expand Up @@ -44,6 +45,7 @@ public static void init() {
registerServerToClient(RenderableObjectSyncPacket.class, RenderableObjectSyncPacket::decode);
registerServerToClient(RenderableObjectDeletePacket.class, RenderableObjectDeletePacket::decode);
registerServerToClient(RenderableObjectClearPacket.class, RenderableObjectClearPacket::decode);
registerServerToClient(RenderableObjectBulkSyncPacket.class, RenderableObjectBulkSyncPacket::decode);
registerClientToServer(GlassesHotkeyPacket.class, GlassesHotkeyPacket::decode);
registerClientToServer(SaddleTurtleControlPacket.class, SaddleTurtleControlPacket::decode);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.srendi.advancedperipherals.common.network.toclient;

import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.client.smartglasses.OverlayObjectHolder;
import de.srendi.advancedperipherals.common.network.base.IPacket;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.ObjectDecodeRegistry;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;

public class RenderableObjectBulkSyncPacket implements IPacket {

private final RenderableObject[] objects;

public RenderableObjectBulkSyncPacket(RenderableObject[] objects) {
this.objects = objects;
}

@Override
public void handle(NetworkEvent.Context context) {
OverlayObjectHolder.addOrUpdateObjects(objects);
}

@Override
public void encode(FriendlyByteBuf buffer) {
buffer.writeInt(objects.length);
for (RenderableObject object : objects)
object.encode(buffer);
}

public static RenderableObjectBulkSyncPacket decode(FriendlyByteBuf buffer) {
AdvancedPeripherals.debug("Size" + buffer.readableBytes());

int size = buffer.readInt();
RenderableObject[] objects = new RenderableObject[size];

for (int i = 0; i < size; i++) {
int id = buffer.readInt();
objects[i] = ObjectDecodeRegistry.getObject(id, buffer);
}

return new RenderableObjectBulkSyncPacket(objects);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@ public final MethodResult createCircle(String id, IArguments arguments) throws L
}

@LuaFunction
public final MethodResult getObjects(IArguments arguments) {
public final MethodResult getObjects() {
return MethodResult.of((Object) overlayModule.getObjects().stream().map(OverlayObject::getId).toArray(String[]::new));
}

@LuaFunction
public final MethodResult getObject(IArguments arguments) throws LuaException {
String id = arguments.getString(0);
return MethodResult.of(overlayModule.getObjects().stream().filter(object -> object.getId().equals(id)).findFirst());
}

@LuaFunction
public final MethodResult removeObject(String id) {
return MethodResult.of(overlayModule.removeObject(id));
Expand All @@ -69,4 +75,16 @@ public final MethodResult getSize() {
return MethodResult.of(Minecraft.getInstance().getWindow().getWidth(), Minecraft.getInstance().getWindow().getHeight());
}

@LuaFunction
public final MethodResult update() {
return MethodResult.of(overlayModule.bulkUpdate());
}

@LuaFunction
public final MethodResult autoUpdate(IArguments arguments) throws LuaException {
overlayModule.autoUpdate = arguments.optBoolean(0, !overlayModule.autoUpdate);
return MethodResult.of(overlayModule.autoUpdate);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.common.network.APNetworking;
import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectBulkSyncPacket;
import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectClearPacket;
import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectDeletePacket;
import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectSyncPacket;
Expand All @@ -14,6 +15,9 @@
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;


Expand All @@ -24,8 +28,11 @@
public class OverlayModule implements IModule {

public final CopyOnWriteArraySet<RenderableObject> objects = new CopyOnWriteArraySet<>();
public final CopyOnWriteArrayList<RenderableObject> objectsToUpdate = new CopyOnWriteArrayList<>();
public final SmartGlassesAccess access;

public boolean autoUpdate = true;

public OverlayModule(SmartGlassesAccess access) {
this.access = access;
}
Expand Down Expand Up @@ -65,12 +72,19 @@ public CopyOnWriteArraySet<RenderableObject> getObjects() {
* The object is the object which was added or the object which already exists(When not successful).
*/
public Pair<RenderableObject, Boolean> addObject(RenderableObject object) {
for (RenderableObject overlayObject : objects) {
List<RenderableObject> objectsToCheck = new ArrayList<>();
objectsToCheck.addAll(objects);
objectsToCheck.addAll(objectsToUpdate);
for (RenderableObject overlayObject : objectsToCheck) {
if (overlayObject.getId().equals(object.getId()))
return Pair.of(overlayObject, false);
}
APNetworking.sendTo(new RenderableObjectSyncPacket(object), (ServerPlayer) access.getEntity());
objects.add(object);
if (autoUpdate) {
APNetworking.sendTo(new RenderableObjectSyncPacket(object), (ServerPlayer) access.getEntity());
objects.add(object);
} else {
objectsToUpdate.add(object);
}
return Pair.of(object, true);
}

Expand All @@ -97,6 +111,7 @@ public boolean removeObject(String id) {
public int clear() {
int size = objects.size();
objects.clear();
objectsToUpdate.clear();
APNetworking.sendTo(new RenderableObjectClearPacket(), (ServerPlayer) access.getEntity());
return size;
}
Expand All @@ -107,6 +122,39 @@ public int clear() {
* @param object the object to sync to the player
*/
public void update(RenderableObject object) {
APNetworking.sendTo(new RenderableObjectSyncPacket(object), (ServerPlayer) access.getEntity());
if (autoUpdate) {
APNetworking.sendTo(new RenderableObjectSyncPacket(object), (ServerPlayer) access.getEntity());
return;
}

objectsToUpdate.add(object);
}


public int bulkUpdate() {
int size = objectsToUpdate.size();
AdvancedPeripherals.debug("Updating " + size);
int packetCount = (int) Math.ceil((double) size / 15000);

// In some cases, if the user creates a lot of objects above 15k, the packet payload can be too big.
// We split up the packets for every 15k objects to prevent the payload limit from mc

// Iterate and send packets
for (int i = 0; i < packetCount; i++) {
int startIndex = i * 15000;
int endIndex = Math.min(startIndex + 15000, size);

// Create a sublist for the current packet
List<RenderableObject> packetObjects = objectsToUpdate.subList(startIndex, endIndex);

// Send the packet
APNetworking.sendTo(new RenderableObjectBulkSyncPacket(packetObjects.toArray(new RenderableObject[0])),
(ServerPlayer) access.getEntity()
);
}
objects.addAll(objectsToUpdate);
objectsToUpdate.clear();

return size;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package de.srendi.advancedperipherals.common.smartglasses.modules.overlay.propertytypes;

import de.srendi.advancedperipherals.AdvancedPeripherals;

public class FixedPointNumberType implements PropertyType<Number> {

public long min;
Expand All @@ -28,7 +26,6 @@ public void init(Object property) {
FixedPointNumberProperty decimalProperty = (FixedPointNumberProperty) property;
min = decimalProperty.min();
max = decimalProperty.max();
AdvancedPeripherals.debug("Initialized number property with min " + min + " and max " + max);
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package de.srendi.advancedperipherals.common.smartglasses.modules.overlay.propertytypes;

import de.srendi.advancedperipherals.AdvancedPeripherals;

public class FloatingNumberType implements PropertyType<Number> {

public float min;
Expand All @@ -14,7 +12,6 @@ public boolean checkIsValid(Object type) {

@Override
public Number mapValue(Object type) {
AdvancedPeripherals.debug("Mapping value " + type + " to " + Math.min(Math.max((float) type, min), max));
return Math.min(Math.max((float) type, min), max);
}

Expand All @@ -23,7 +20,6 @@ public void init(Object property) {
FloatingNumberProperty decimalProperty = (FloatingNumberProperty) property;
min = decimalProperty.min();
max = decimalProperty.max();
AdvancedPeripherals.debug("Initialized decimal property with min " + min + " and max " + max);
}
}

0 comments on commit 4b61372

Please sign in to comment.