From 7c3d03d7a2daa20742120aff505311620be7b009 Mon Sep 17 00:00:00 2001 From: Ollie <69084614+olijeffers0n@users.noreply.github.com> Date: Sat, 15 Apr 2023 19:36:02 +0100 Subject: [PATCH] Revert To loading all data on frame request --- rustplus/__init__.py | 2 +- rustplus/api/remote/camera/camera_manager.py | 49 +++++++++++++------- rustplus/api/remote/camera/structures.py | 29 ++++++++++++ 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/rustplus/__init__.py b/rustplus/__init__.py index 57608ce..df741be 100644 --- a/rustplus/__init__.py +++ b/rustplus/__init__.py @@ -22,5 +22,5 @@ __name__ = "rustplus" __author__ = "olijeffers0n" -__version__ = "5.5.11" +__version__ = "5.5.12" __support__ = "Discord: https://discord.gg/nQqJe8qvP8" diff --git a/rustplus/api/remote/camera/camera_manager.py b/rustplus/api/remote/camera/camera_manager.py index 0feaeb8..448a641 100644 --- a/rustplus/api/remote/camera/camera_manager.py +++ b/rustplus/api/remote/camera/camera_manager.py @@ -7,7 +7,7 @@ from ..events import EventLoopManager, EventHandler from ..rustplus_proto import AppCameraInput, Vector2, AppEmpty from ...structures import Vector -from .structures import CameraInfo, Entity, RayPacket +from .structures import CameraInfo, Entity, LimitedQueue RS = TypeVar("RS", bound="RustSocket") @@ -16,7 +16,7 @@ class CameraManager: def __init__(self, rust_socket: RS, cam_id: str, cam_info_message: CameraInfo) -> None: self.rust_socket: RS = rust_socket self._cam_id: str = cam_id - self._last_packet: RayPacket = None + self._last_packets: LimitedQueue = LimitedQueue(6) self._cam_info_message: CameraInfo = CameraInfo(cam_info_message) self._open: bool = True self.parser: Parser = Parser( @@ -25,11 +25,8 @@ def __init__(self, rust_socket: RS, cam_id: str, cam_info_message: CameraInfo) - self.time_since_last_subscribe: float = time.time() self.frame_callbacks: Set[Coroutine] = set() - def add_packet(self, packet: RayPacket) -> None: - self._last_packet = packet - - self.parser.handle_camera_ray_data(packet) - self.parser.step() + def add_packet(self, packet) -> None: + self._last_packets.add(packet) if len(self.frame_callbacks) == 0: return @@ -48,22 +45,34 @@ def on_frame_received(self, coro: Coroutine) -> Coroutine: return coro def has_frame_data(self) -> bool: - return self._last_packet is not None + return self._last_packets is not None and len(self._last_packets) > 0 def _create_frame(self, render_entities: bool = True, entity_render_distance: float = float("inf"), max_entity_amount: int = float("inf")) -> Union[Image.Image, None]: - if self._last_packet is None: + if self._last_packets is None: + return None + + if len(self._last_packets) == 0: return None if not self._open: raise Exception("Camera is closed") + for i in range(len(self._last_packets)): + self.parser.handle_camera_ray_data(self._last_packets.get(i)) + self.parser.step() + + last_packet = self._last_packets.get_last() + + self._last_packets.clear() + self._last_packets.add(last_packet) + return self.parser.render( render_entities, - self._last_packet.entities, - self._last_packet.vertical_fov, + last_packet.entities, + last_packet.vertical_fov, self._cam_info_message.far_plane, entity_render_distance, - max_entity_amount if max_entity_amount is not None else len(self._last_packet.entities), + max_entity_amount if max_entity_amount is not None else len(self._last_packets.get_last().entities), ) async def get_frame(self, render_entities: bool = True, entity_render_distance: float = float("inf"), max_entity_amount: int = float("inf")) -> Union[Image.Image, None]: @@ -118,7 +127,7 @@ async def exit_camera(self) -> None: self.rust_socket.remote.ignored_responses.append(app_request.seq) self._open = False - self._last_packet = None + self._last_packets.clear() async def resubscribe(self) -> None: await self.rust_socket.remote.subscribe_to_camera(self._cam_id, True) @@ -127,16 +136,22 @@ async def resubscribe(self) -> None: self.rust_socket.remote.camera_manager = self async def get_entities_in_frame(self) -> List[Entity]: - if self._last_packet is None: + if self._last_packets is None: + return [] + + if len(self._last_packets) == 0: return [] - return self._last_packet.entities + return self._last_packets.get_last().entities async def get_distance_from_player(self) -> float: - if self._last_packet is None: + if self._last_packets is None: + return float("inf") + + if len(self._last_packets) == 0: return float("inf") - return self._last_packet.distance + return self._last_packets.get_last().distance async def get_max_distance(self) -> float: return self._cam_info_message.far_plane diff --git a/rustplus/api/remote/camera/structures.py b/rustplus/api/remote/camera/structures.py index 516f6eb..05cc30a 100644 --- a/rustplus/api/remote/camera/structures.py +++ b/rustplus/api/remote/camera/structures.py @@ -70,3 +70,32 @@ def __str__(self) -> str: f"RayPacket(vertical_fov={self.vertical_fov}, sample_offset={self.sample_offset}, " f"ray_data={self.ray_data}, distance={self.distance}, entities={self.entities})" ) + + +class LimitedQueue: + def __init__(self, length) -> None: + self._length = length + self._queue = [] + + def add(self, item) -> None: + self._queue.append(item) + if len(self._queue) > self._length: + self._queue.pop(0) + + def get(self, index=0) -> Any: + if index >= len(self._queue) or index < 0: + return None + + return self._queue[index] + + def get_last(self) -> Any: + return self._queue[-1] + + def pop(self) -> Any: + return self._queue.pop(0) + + def clear(self) -> None: + self._queue.clear() + + def __len__(self) -> int: + return len(self._queue)