summaryrefslogtreecommitdiff
path: root/network
diff options
context:
space:
mode:
Diffstat (limited to 'network')
-rw-r--r--network/character_server.gd48
-rw-r--r--network/login_server.gd27
-rw-r--r--network/map_server.gd30
-rw-r--r--network/network.gd17
4 files changed, 93 insertions, 29 deletions
diff --git a/network/character_server.gd b/network/character_server.gd
index e710639..f629283 100644
--- a/network/character_server.gd
+++ b/network/character_server.gd
@@ -1,6 +1,11 @@
class_name CharacterServer
+signal received_packet(packet: Packet)
+signal logged_in(packet: CharacterServerLoginSuccessPacket)
+signal requested_character_list(packet: RequestCharacterListSuccessPacket)
+signal selected_character(packet: CharacterSelectionSuccessPacket)
+
var host: String
var port: int = 6121
var stream: PacketPeerStream = PacketPeerStream.new()
@@ -17,7 +22,20 @@ func _init(host: String, port: int = 6121) -> void:
peer.poll()
-func login(account_id: int, login_id1: int, login_id2: int, gender: Constants.Gender) -> CharacterServerLoginSuccessPacket:
+## Emits [signal received_packet].
+func listen() -> void:
+ if peer.get_status() == StreamPeerTCP.Status.STATUS_CONNECTED:
+ var raw_packet: PackedByteArray = peer.get_partial_data(2)[1]
+ if raw_packet.size() > 0:
+ raw_packet += peer.get_data(peer.get_available_bytes())[1]
+ var header = raw_packet.decode_u16(0)
+ var packet_type = Constants.PacketDB[header]
+ var packet = packet_type.from_bytes(raw_packet)
+
+ received_packet.emit(packet)
+
+
+func login(account_id: int, login_id1: int, login_id2: int, gender: Constants.Gender):
var character_server_login_packet = CharacterServerLoginPacket.new()
character_server_login_packet.account_id = account_id
character_server_login_packet.login_id1 = login_id1
@@ -26,29 +44,31 @@ func login(account_id: int, login_id1: int, login_id2: int, gender: Constants.Ge
peer.put_data(character_server_login_packet.to_bytes())
- var success = CharacterServerLoginSuccessPacket.from_bytes_via_peer(peer)
- print(inst_to_dict(success))
+ peer.get_data(4) # in-between packet
- return success
+ var packet = await received_packet
+ if packet is CharacterServerLoginSuccessPacket:
+ print(inst_to_dict(packet))
+ logged_in.emit(packet)
-func request_character_list() -> RequestCharacterListSuccessPacket:
+func request_character_list():
var request_character_list_packet = RequestCharacterListPacket.new()
peer.put_data(request_character_list_packet.to_bytes())
- var success = RequestCharacterListSuccessPacket.from_bytes_via_peer(peer)
- print(inst_to_dict(success))
-
- return success
+ var packet = await received_packet
+ if packet is RequestCharacterListSuccessPacket:
+ print(inst_to_dict(packet), inst_to_dict(packet.character_information[0]))
+ requested_character_list.emit(packet)
-func select_character(slot: int) -> CharacterSelectionSuccessPacket:
+func select_character(slot: int):
var select_character_packet = SelectCharacterPacket.new()
select_character_packet.selected_slot = slot
peer.put_data(select_character_packet.to_bytes())
- var success = CharacterSelectionSuccessPacket.from_bytes_via_peer(peer)
- print(inst_to_dict(success))
-
- return success
+ var packet = await received_packet
+ if packet is CharacterSelectionSuccessPacket:
+ print(inst_to_dict(packet))
+ selected_character.emit(packet)
diff --git a/network/login_server.gd b/network/login_server.gd
index bb7c093..6d878c4 100644
--- a/network/login_server.gd
+++ b/network/login_server.gd
@@ -1,6 +1,9 @@
class_name LoginServer
+signal received_packet(packet: Packet)
+signal logged_in(packet: LoginServerLoginSuccessPacket)
+
var host: String
var port: int = 6900
var stream: PacketPeerStream = PacketPeerStream.new()
@@ -17,15 +20,27 @@ func _init(host: String, port: int = 6900) -> void:
peer.poll()
-func login(username: String, password: String) -> LoginServerLoginSuccessPacket:
+## Emits [signal received_packet].
+func listen() -> void:
+ if peer.get_status() == StreamPeerTCP.Status.STATUS_CONNECTED:
+ var raw_packet: PackedByteArray = peer.get_partial_data(2)[1]
+ if raw_packet.size() > 0:
+ raw_packet += peer.get_data(peer.get_available_bytes())[1]
+ var header = raw_packet.decode_u16(0)
+ var packet_type = Constants.PacketDB[header]
+ var packet = packet_type.from_bytes(raw_packet)
+
+ received_packet.emit(packet)
+
+
+func login(username: String, password: String):
var login_server_login_packet = LoginServerLoginPacket.new()
login_server_login_packet.username = username
login_server_login_packet.password = password
peer.put_data(login_server_login_packet.to_bytes())
- var success = LoginServerLoginSuccessPacket.from_bytes_via_peer(peer)
- var character_server_information: CharacterServerInformation = success.character_server_information[0]
- print(inst_to_dict(success), inst_to_dict(character_server_information))
-
- return success
+ var packet = await received_packet
+ if packet is LoginServerLoginSuccessPacket:
+ print(inst_to_dict(packet))
+ logged_in.emit(packet)
diff --git a/network/map_server.gd b/network/map_server.gd
index af46c6c..f4dacc1 100644
--- a/network/map_server.gd
+++ b/network/map_server.gd
@@ -1,6 +1,9 @@
class_name MapServer
+signal received_packet(packet: Packet)
+signal logged_in(packet: MapServerLoginSuccessPacket)
+
var host: String
var port: int = 5121
var stream: PacketPeerStream = PacketPeerStream.new()
@@ -17,7 +20,24 @@ func _init(host: String, port: int = 5121) -> void:
peer.poll()
-func login(account_id: int, character_id: int, login_id1: int, gender: Constants.Gender) -> MapServerLoginSuccessPacket:
+## Emits [signal received_packet].
+func listen() -> void:
+ if peer.get_status() == StreamPeerTCP.Status.STATUS_CONNECTED:
+ var raw_packet: PackedByteArray = peer.get_partial_data(2)[1]
+
+ if raw_packet.size() > 0:
+ var header = raw_packet.decode_u16(0)
+ var packet_type = Constants.PacketDB[header]
+ if packet_type.BYTE_LENGTH > 0:
+ raw_packet += peer.get_data(packet_type.BYTE_LENGTH - 2)[1]
+ else:
+ raw_packet += peer.get_data(peer.get_available_bytes())[1]
+ var packet = packet_type.from_bytes(raw_packet)
+
+ received_packet.emit(packet)
+
+
+func login(account_id: int, character_id: int, login_id1: int, gender: Constants.Gender):
var map_server_login_packet = MapServerLoginPacket.new()
map_server_login_packet.account_id = account_id
map_server_login_packet.character_id = character_id
@@ -29,7 +49,7 @@ func login(account_id: int, character_id: int, login_id1: int, gender: Constants
peer.get_data(6) # in-between packet
peer.get_data(4) # in-between packet
- var success = MapServerLoginSuccessPacket.from_bytes_via_peer(peer)
- print(inst_to_dict(success))
-
- return success
+ var packet = await received_packet
+ if packet is MapServerLoginSuccessPacket:
+ print(inst_to_dict(packet))
+ logged_in.emit(packet)
diff --git a/network/network.gd b/network/network.gd
index 25607a2..1e665a3 100644
--- a/network/network.gd
+++ b/network/network.gd
@@ -109,7 +109,16 @@ func _ready() -> void:
#print(msls.get_position())
var file = FileAccess.open("res://data/data.grf", FileAccess.READ)
- var signature = file.get_buffer("Master of Magic".length())
- print(signature.get_string_from_utf8())
- print(file.get_16())
- print(file.get_32())
+ var header = GRF.Header.from_file(file)
+ print(inst_to_dict(header))
+
+
+func _process(_delta: float) -> void:
+ if login_server:
+ login_server.listen()
+
+ if character_server:
+ character_server.listen()
+
+ if map_server:
+ map_server.listen()