Skip to content

Commit

Permalink
Revert To loading all data on frame request
Browse files Browse the repository at this point in the history
olijeffers0n committed Apr 15, 2023

Verified

This commit was signed with the committer’s verified signature.
1 parent 22cca9a commit 7c3d03d
Showing 3 changed files with 62 additions and 18 deletions.
2 changes: 1 addition & 1 deletion rustplus/__init__.py
Original file line number Diff line number Diff line change
@@ -22,5 +22,5 @@

__name__ = "rustplus"
__author__ = "olijeffers0n"
__version__ = "5.5.11"
__version__ = "5.5.12"
__support__ = "Discord: https://discord.gg/nQqJe8qvP8"
49 changes: 32 additions & 17 deletions rustplus/api/remote/camera/camera_manager.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions rustplus/api/remote/camera/structures.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7c3d03d

Please sign in to comment.