Skip to content

Commit

Permalink
Merge pull request #42 from olijeffers0n/feat/better_proto
Browse files Browse the repository at this point in the history
Feat/better proto Approved by @netspl0it
  • Loading branch information
olijeffers0n authored Apr 23, 2023
2 parents 47ab4cb + 77a05ef commit 1ffe9da
Show file tree
Hide file tree
Showing 23 changed files with 714 additions and 396 deletions.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
websocket_client
Pillow
protobuf==4.21.6
asyncio
rustPlusPushReceiver==0.4.1
http-ece
requests
numpy
scipy
scipy
betterproto
2 changes: 1 addition & 1 deletion rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@

__name__ = "rustplus"
__author__ = "olijeffers0n"
__version__ = "5.5.12"
__version__ = "5.5.13"
__support__ = "Discord: https://discord.gg/nQqJe8qvP8"
7 changes: 4 additions & 3 deletions rustplus/api/base_rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def _generate_protobuf(self) -> AppRequest:
"""
app_request = AppRequest()
app_request.seq = self.seq
app_request.playerId = self.server_id.player_id
app_request.playerToken = self.server_id.player_token
app_request.player_id = self.server_id.player_id
app_request.player_token = self.server_id.player_token

self.seq += 1

Expand Down Expand Up @@ -170,7 +170,8 @@ async def send_wakeup_request(self) -> None:
await self._handle_ratelimit()

app_request = self._generate_protobuf()
app_request.getTime.CopyFrom(AppEmpty())
app_request.get_time = AppEmpty()
app_request.get_time._serialized_on_wire = True

self.remote.ignored_responses.append(app_request.seq)

Expand Down
45 changes: 34 additions & 11 deletions rustplus/api/remote/camera/camera_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@

from .camera_parser import Parser
from ..events import EventLoopManager, EventHandler
from ..rustplus_proto import AppCameraInput, Vector2, AppEmpty
from ..rustplus_proto import (
AppCameraInput,
Vector2,
AppEmpty,
AppRequest,
AppCameraInfo,
)
from ...structures import Vector
from .structures import CameraInfo, Entity, LimitedQueue

RS = TypeVar("RS", bound="RustSocket")


class CameraManager:
def __init__(self, rust_socket: RS, cam_id: str, cam_info_message: CameraInfo) -> None:
def __init__(
self, rust_socket: RS, cam_id: str, cam_info_message: AppCameraInfo
) -> None:
self.rust_socket: RS = rust_socket
self._cam_id: str = cam_id
self._last_packets: LimitedQueue = LimitedQueue(6)
Expand Down Expand Up @@ -47,7 +55,12 @@ def on_frame_received(self, coro: Coroutine) -> Coroutine:
def has_frame_data(self) -> bool:
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]:
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_packets is None:
return None

Expand All @@ -72,11 +85,20 @@ def _create_frame(self, render_entities: bool = True, entity_render_distance: fl
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_packets.get_last().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]:
return self._create_frame(render_entities, entity_render_distance, max_entity_amount)
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]:
return self._create_frame(
render_entities, entity_render_distance, max_entity_amount
)

def can_move(self, control_type: int) -> bool:
return self._cam_info_message.is_move_option_permissible(control_type)
Expand Down Expand Up @@ -105,23 +127,24 @@ async def send_combined_movement(
value = value | movement

await self.rust_socket._handle_ratelimit(0.01)
app_request = self.rust_socket._generate_protobuf()
app_request: AppRequest = self.rust_socket._generate_protobuf()
cam_input = AppCameraInput()

cam_input.buttons = value
vector = Vector2()
vector.x = joystick_vector.x
vector.y = joystick_vector.y
cam_input.mouseDelta.CopyFrom(vector)
app_request.cameraInput.CopyFrom(cam_input)
cam_input.mouse_delta = vector
app_request.camera_input = cam_input

await self.rust_socket.remote.send_message(app_request)
self.rust_socket.remote.ignored_responses.append(app_request.seq)

async def exit_camera(self) -> None:
await self.rust_socket._handle_ratelimit()
app_request = self.rust_socket._generate_protobuf()
app_request.cameraUnsubscribe.CopyFrom(AppEmpty())
app_request: AppRequest = self.rust_socket._generate_protobuf()
app_request.camera_unsubscribe = AppEmpty()
app_request.camera_unsubscribe._serialized_on_wire = True

await self.rust_socket.remote.send_message(app_request)
self.rust_socket.remote.ignored_responses.append(app_request.seq)
Expand Down
22 changes: 12 additions & 10 deletions rustplus/api/remote/camera/structures.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from typing import Any

from ..rustplus_proto import AppCameraInfo, AppCameraRaysEntity, AppCameraRays


class CameraInfo:
def __init__(self, camera_info_message) -> None:
def __init__(self, camera_info_message: AppCameraInfo) -> None:
self.width: int = camera_info_message.width
self.height: int = camera_info_message.height
self.near_plane: float = camera_info_message.nearPlane
self.far_plane: float = camera_info_message.farPlane
self.control_flags: int = camera_info_message.controlFlags
self.near_plane: float = camera_info_message.near_plane
self.far_plane: float = camera_info_message.far_plane
self.control_flags: int = camera_info_message.control_flags

def is_move_option_permissible(self, value: int) -> bool:
return self.control_flags & value == value
Expand All @@ -20,8 +22,8 @@ def __str__(self) -> str:


class Entity:
def __init__(self, entity_data) -> None:
self.entity_id: int = entity_data.entityId
def __init__(self, entity_data: AppCameraRaysEntity) -> None:
self.entity_id: int = entity_data.entity_id
self.type: int = entity_data.type
self.position: Vector3 = Vector3(entity_data.position)
self.rotation: Vector3 = Vector3(entity_data.rotation)
Expand Down Expand Up @@ -58,10 +60,10 @@ def __str__(self) -> str:


class RayPacket:
def __init__(self, ray_packet) -> None:
self.vertical_fov = ray_packet.verticalFov
self.sample_offset = ray_packet.sampleOffset
self.ray_data = ray_packet.rayData
def __init__(self, ray_packet: AppCameraRays) -> None:
self.vertical_fov = ray_packet.vertical_fov
self.sample_offset = ray_packet.sample_offset
self.ray_data = ray_packet.ray_data
self.distance = ray_packet.distance
self.entities = [Entity(data) for data in ray_packet.entities]

Expand Down
8 changes: 6 additions & 2 deletions rustplus/api/remote/events/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@

class EventHandler:
@staticmethod
def schedule_event(loop: asyncio.AbstractEventLoop, coro: Coroutine, arg: Any) -> None:
def schedule_event(
loop: asyncio.AbstractEventLoop, coro: Coroutine, arg: Any
) -> None:
def callback(inner_future: Future):
if inner_future.exception() is not None:
logging.getLogger("rustplus.py").exception(inner_future.exception())

future: Future = asyncio.run_coroutine_threadsafe(coro(arg), loop)
future.add_done_callback(callback)

def run_entity_event(self, name: str, app_message: AppMessage, server_id: ServerID) -> None:
def run_entity_event(
self, name: str, app_message: AppMessage, server_id: ServerID
) -> None:

handlers: Set[RegisteredListener] = EntityEvent.handlers.get_handlers(
server_id
Expand Down
28 changes: 14 additions & 14 deletions rustplus/api/remote/events/events.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from typing import List

from ..rustplus_proto import AppMessage
from ..rustplus_proto import AppMessage, AppEntityPayloadItem
from ...structures import RustChatMessage
from ...structures.rust_team_info import RustTeamInfo
from ...structures.rust_marker import RustMarker
from .handler_list import HandlerList, EntityHandlerList


class Item:
def __init__(self, app_message: AppMessage) -> None:
self._item_id: int = app_message.itemId
def __init__(self, app_message: AppEntityPayloadItem) -> None:
self._item_id: int = app_message.item_id
self._quantity: int = app_message.quantity
self._item_is_blueprint: bool = app_message.itemIsBlueprint
self._item_is_blueprint: bool = app_message.item_is_blueprint

@property
def item_id(self) -> int:
Expand All @@ -31,8 +31,8 @@ class TeamEvent:
handlers = HandlerList()

def __init__(self, app_message: AppMessage) -> None:
self._player_id: int = app_message.broadcast.teamChanged.playerId
self._team_info = RustTeamInfo(app_message.broadcast.teamChanged.teamInfo)
self._player_id: int = app_message.broadcast.team_changed.player_id
self._team_info = RustTeamInfo(app_message.broadcast.team_changed.team_info)

@property
def player_id(self) -> int:
Expand All @@ -48,7 +48,7 @@ class ChatEvent:
handlers = HandlerList()

def __init__(self, app_message: AppMessage) -> None:
self._message = RustChatMessage(app_message.broadcast.newTeamMessage.message)
self._message = RustChatMessage(app_message.broadcast.team_message.message)

@property
def message(self) -> RustChatMessage:
Expand All @@ -59,20 +59,20 @@ class EntityEvent:

handlers = EntityHandlerList()

def __init__(self, app_message, entity_type) -> None:
def __init__(self, app_message: AppMessage, entity_type) -> None:
self._type = int(entity_type)
self._entity_id: int = app_message.broadcast.entityChanged.entityId
self._value: bool = app_message.broadcast.entityChanged.payload.value
self._capacity: int = app_message.broadcast.entityChanged.payload.capacity
self._entity_id: int = app_message.broadcast.entity_changed.entity_id
self._value: bool = app_message.broadcast.entity_changed.payload.value
self._capacity: int = app_message.broadcast.entity_changed.payload.capacity
self._has_protection: bool = (
app_message.broadcast.entityChanged.payload.hasProtection
app_message.broadcast.entity_changed.payload.has_protection
)
self._protection_expiry: int = (
app_message.broadcast.entityChanged.payload.protectionExpiry
app_message.broadcast.entity_changed.payload.protection_expiry
)

self._items: List[Item] = [
Item(item) for item in app_message.broadcast.entityChanged.payload.items
Item(item) for item in app_message.broadcast.entity_changed.payload.items
]

@property
Expand Down
16 changes: 7 additions & 9 deletions rustplus/api/remote/rust_remote_interface.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import asyncio
import logging

from asyncio import Future
from typing import Dict

from .camera.camera_manager import CameraManager
from .events import EventLoopManager, EntityEvent, RegisteredListener
from .events.event_handler import EventHandler
Expand Down Expand Up @@ -197,9 +194,10 @@ async def get_entity_info(self: RustRemote, eid):

await self.api._handle_ratelimit()

app_request = self.api._generate_protobuf()
app_request: AppRequest = self.api._generate_protobuf()
app_request.entityId = eid
app_request.getEntityInfo.CopyFrom(AppEmpty())
app_request.get_entity_info = AppEmpty()
app_request.get_entity_info._serialized_on_wire = True

await self.send_message(app_request)

Expand Down Expand Up @@ -230,10 +228,10 @@ async def subscribe_to_camera(
self, entity_id: int, ignore_response: bool = False
) -> AppRequest:
await self.api._handle_ratelimit()
app_request = self.api._generate_protobuf()
app_request: AppRequest = self.api._generate_protobuf()
subscribe = AppCameraSubscribe()
subscribe.cameraId = entity_id
app_request.cameraSubscribe.CopyFrom(subscribe)
subscribe.camera_id = entity_id
app_request.camera_subscribe = subscribe

await self.send_message(app_request)

Expand All @@ -252,6 +250,6 @@ async def create_camera_manager(self, cam_id) -> CameraManager:
app_message = await self.get_response(app_request.seq, app_request)

self.camera_manager = CameraManager(
self.api, cam_id, app_message.response.cameraSubscribeInfo
self.api, cam_id, app_message.response.camera_subscribe_info
)
return self.camera_manager
Loading

0 comments on commit 1ffe9da

Please sign in to comment.