2018-09-11 21:00:12 +01:00
|
|
|
import struct
|
|
|
|
import random
|
2018-09-15 19:42:48 +01:00
|
|
|
import enum
|
2019-01-29 22:28:50 +00:00
|
|
|
import socket
|
2018-09-11 21:00:12 +01:00
|
|
|
|
|
|
|
CURRENT_REQUEST_VERSION = 1
|
|
|
|
MAX_REQUEST_DATA_SIZE = 32
|
2019-01-29 22:28:50 +00:00
|
|
|
MAX_PACKET_SIZE = 48
|
2018-09-11 21:00:12 +01:00
|
|
|
|
2018-09-15 19:42:48 +01:00
|
|
|
class RequestType(enum.IntEnum):
|
|
|
|
ReadMemory = 1,
|
|
|
|
WriteMemory = 2
|
2018-09-11 21:00:12 +01:00
|
|
|
|
2019-01-29 22:28:50 +00:00
|
|
|
CITRA_PORT = 45987
|
2018-09-11 21:00:12 +01:00
|
|
|
|
|
|
|
class Citra:
|
|
|
|
def __init__(self, address="127.0.0.1", port=CITRA_PORT):
|
2019-01-29 22:28:50 +00:00
|
|
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
self.address = address
|
2018-09-11 21:00:12 +01:00
|
|
|
|
|
|
|
def is_connected(self):
|
|
|
|
return self.socket is not None
|
|
|
|
|
|
|
|
def _generate_header(self, request_type, data_size):
|
|
|
|
request_id = random.getrandbits(32)
|
|
|
|
return (struct.pack("IIII", CURRENT_REQUEST_VERSION, request_id, request_type, data_size), request_id)
|
|
|
|
|
|
|
|
def _read_and_validate_header(self, raw_reply, expected_id, expected_type):
|
|
|
|
reply_version, reply_id, reply_type, reply_data_size = struct.unpack("IIII", raw_reply[:4*4])
|
|
|
|
if (CURRENT_REQUEST_VERSION == reply_version and
|
|
|
|
expected_id == reply_id and
|
|
|
|
expected_type == reply_type and
|
|
|
|
reply_data_size == len(raw_reply[4*4:])):
|
|
|
|
return raw_reply[4*4:]
|
|
|
|
return None
|
|
|
|
|
|
|
|
def read_memory(self, read_address, read_size):
|
|
|
|
"""
|
|
|
|
>>> c.read_memory(0x100000, 4)
|
|
|
|
b'\\x07\\x00\\x00\\xeb'
|
|
|
|
"""
|
|
|
|
result = bytes()
|
|
|
|
while read_size > 0:
|
|
|
|
temp_read_size = min(read_size, MAX_REQUEST_DATA_SIZE)
|
|
|
|
request_data = struct.pack("II", read_address, temp_read_size)
|
2018-09-15 19:42:48 +01:00
|
|
|
request, request_id = self._generate_header(RequestType.ReadMemory, len(request_data))
|
2018-09-11 21:00:12 +01:00
|
|
|
request += request_data
|
2019-01-29 22:28:50 +00:00
|
|
|
self.socket.sendto(request, (self.address, CITRA_PORT))
|
2018-09-11 21:00:12 +01:00
|
|
|
|
2019-01-29 22:28:50 +00:00
|
|
|
raw_reply = self.socket.recv(MAX_PACKET_SIZE)
|
2018-09-15 19:42:48 +01:00
|
|
|
reply_data = self._read_and_validate_header(raw_reply, request_id, RequestType.ReadMemory)
|
2018-09-11 21:00:12 +01:00
|
|
|
|
|
|
|
if reply_data:
|
|
|
|
result += reply_data
|
|
|
|
read_size -= len(reply_data)
|
|
|
|
read_address += len(reply_data)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def write_memory(self, write_address, write_contents):
|
|
|
|
"""
|
|
|
|
>>> c.write_memory(0x100000, b"\\xff\\xff\\xff\\xff")
|
|
|
|
True
|
|
|
|
>>> c.read_memory(0x100000, 4)
|
|
|
|
b'\\xff\\xff\\xff\\xff'
|
|
|
|
>>> c.write_memory(0x100000, b"\\x07\\x00\\x00\\xeb")
|
|
|
|
True
|
|
|
|
>>> c.read_memory(0x100000, 4)
|
|
|
|
b'\\x07\\x00\\x00\\xeb'
|
|
|
|
"""
|
|
|
|
write_size = len(write_contents)
|
|
|
|
while write_size > 0:
|
|
|
|
temp_write_size = min(write_size, MAX_REQUEST_DATA_SIZE - 8)
|
|
|
|
request_data = struct.pack("II", write_address, temp_write_size)
|
|
|
|
request_data += write_contents[:temp_write_size]
|
2018-09-15 19:42:48 +01:00
|
|
|
request, request_id = self._generate_header(RequestType.WriteMemory, len(request_data))
|
2018-09-11 21:00:12 +01:00
|
|
|
request += request_data
|
2019-01-29 22:28:50 +00:00
|
|
|
self.socket.sendto(request, (self.address, CITRA_PORT))
|
2018-09-11 21:00:12 +01:00
|
|
|
|
2019-01-29 22:28:50 +00:00
|
|
|
raw_reply = self.socket.recv(MAX_PACKET_SIZE)
|
2018-09-15 19:42:48 +01:00
|
|
|
reply_data = self._read_and_validate_header(raw_reply, request_id, RequestType.WriteMemory)
|
2018-09-11 21:00:12 +01:00
|
|
|
|
|
|
|
if None != reply_data:
|
|
|
|
write_address += temp_write_size
|
|
|
|
write_size -= temp_write_size
|
|
|
|
write_contents = write_contents[temp_write_size:]
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
if "__main__" == __name__:
|
|
|
|
import doctest
|
|
|
|
doctest.testmod(extraglobs={'c': Citra()})
|