forked from ArchipelagoMW/Archipelago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRustedMossClient.py
282 lines (249 loc) · 12 KB
/
RustedMossClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import os
import asyncio
import typing
import bsdiff4
import hashlib
import Utils
from worlds.rusted_moss.Utils import gameLocationToLocationName
from worlds import AutoWorldRegister
from NetUtils import NetworkItem, ClientStatus
from CommonClient import ClientCommandProcessor, CommonContext, \
server_loop, get_base_parser, gui_enabled
from MultiServer import mark_raw
RAWDATAHASH: str = "e435e374cab856df1b1f00570347b6ff"
MODDEDDATAHASH: str = "ef959da22dd5fd6412105b0ce4e9e03a"
# TODO add command for resyncing items/locations
class RustedMossCommandProcessor(ClientCommandProcessor):
def __init__(self, ctx):
super().__init__(ctx)
@mark_raw
def _cmd_patch(self, directory: str = ""):
"""Provide path to game install folder to patch the data.win file to modify the game."""
if isinstance(self.ctx, RustedMossContext):
dataWinPath = os.path.join(directory, "data.win")
if not os.path.isfile(dataWinPath):
self.output("ERROR: Could not find data.win file to patch in the folder provided.")
else:
basemd5 = hashlib.md5()
with open(dataWinPath, "rb") as file:
base_data_bytes = bytes(file.read())
file.close()
basemd5.update(base_data_bytes)
if RAWDATAHASH != basemd5.hexdigest():
self.output("ERROR: MD5 hash of data.win file does not match correct hash. Make sure you have downpatched to the correct version (1.47)")
else:
bsdiff4.file_patch_inplace(dataWinPath, os.path.join(os.getcwd(), "data/rusted_moss_patch.bsdiff"))
moddedmd5 = hashlib.md5()
with open(dataWinPath, "rb") as file:
modded_data_bytes = bytes(file.read())
file.close()
moddedmd5.update(modded_data_bytes)
if MODDEDDATAHASH != moddedmd5.hexdigest():
self.output("ERROR: MD5 hash of moddified data.win file does not match correct hash. Try again or contact mod owner.")
else:
self.output("Patching successful")
async def _cmd_deathlink(self):
"""Toggles deathlink on or off."""
if isinstance(self.ctx, RustedMossContext):
self.ctx.deathlink_status = not self.ctx.deathlink_status
await self.ctx.update_death_link(self.ctx.deathlink_status)
if self.ctx.deathlink_status:
self.output(f"Deathlink enabled.")
else:
self.output(f"Deathlink disabled.")
class RustedMossContext(CommonContext):
tags = {"AP"}
game = "Rusted Moss"
command_processor = RustedMossCommandProcessor
items_handling = 0b111
save_game_folder = os.path.expandvars(r"%localappdata%/Rusted_Moss")
deathlink_status = False
titania_pieces_required = 0
hard_maya = False
shop_discount_percentage = 100
def __init__(self, server_address, password):
super().__init__(server_address, password)
self.game = "Rusted Moss"
self.got_deathlink = False
self.save_game_folder = os.path.expandvars(r"%localappdata%/Rusted_Moss")
self.deathlink_status = False
self.hard_maya = False
self.ending = 0
self.shop_discount_percentage = 100
async def server_auth(self, password_requested: bool = False):
if password_requested and not self.password:
await super().server_auth(password_requested)
await self.get_username()
await self.send_connect()
def clear_rusted_moss_files(self):
path = self.save_game_folder
self.finished_game = False
for root, dirs, files in os.walk(path):
for file in files:
if file in ["deathlinkFromClient", "deathlinkFromServer", "checkedLocations", "receivedItems", "newItems", "scoutLocations", "newLocations", "endingAchieved", "options", "readOptions"]:
os.remove(os.path.join(root, file))
async def connect(self, address: typing.Optional[str] = None):
self.clear_rusted_moss_files()
await super().connect(address)
async def disconnect(self, allow_autoreconnect: bool = False):
self.clear_rusted_moss_files()
await super().disconnect(allow_autoreconnect)
async def connection_closed(self):
self.clear_rusted_moss_files()
await super().connection_closed()
async def shutdown(self):
self.clear_rusted_moss_files()
await super().shutdown()
def on_package(self, cmd: str, args: dict):
if cmd == "Connected":
self.game = self.slot_info[self.slot].game
Utils.async_start(process_rusted_moss_cmd(self, cmd, args))
def run_gui(self):
from kvui import GameManager
class RustedMossManager(GameManager):
logging_pairs = [
("Client", "Archipelago")
]
base_title = "Archipelago Rusted Moss Client"
self.ui = RustedMossManager(self)
self.ui_task = asyncio.create_task(self.ui.async_run(), name="UI")
def on_deathlink(self, data: typing.Dict[str, typing.Any]):
self.got_deathlink = True
super().on_deathlink(data)
async def process_rusted_moss_cmd(ctx: RustedMossContext, cmd: str, args: dict):
# TODO handle server commands https://github.com/ArchipelagoMW/Archipelago/blob/main/docs/network%20protocol.md#server---client
print("cmd: " + cmd)
print("args:")
print(args)
if cmd == "Connected":
if not os.path.exists(ctx.save_game_folder):
os.mkdir(ctx.save_game_folder)
ctx.deathlink_status = args["slot_data"]["deathlink"]
await ctx.update_death_link(ctx.deathlink_status)
ctx.hard_maya = args["slot_data"]["hard_maya"]
ctx.ending = args["slot_data"]["ending"]
ctx.shop_discount_percentage = args["slot_data"]["shop_discount_percentage"]
with open(os.path.join(ctx.save_game_folder, "options"), "w") as f:
f.write("hard_maya\n")
f.write(str(ctx.hard_maya) + "\n")
f.write("shop_discount_percentage\n")
f.write(str(ctx.shop_discount_percentage) + "\n")
f.close()
with open(os.path.join(ctx.save_game_folder, "readOptions"), "w") as f:
f.close()
with open(os.path.join(ctx.save_game_folder, "checkedLocations"), "w") as f:
for location in args["checked_locations"]:
f.write(str(location) + "\n")
f.close()
elif cmd == "LocationInfo":
# TODO investigate scout handling
pass
elif cmd == "ReceivedItems":
start_index = args["index"]
if start_index == 0:
ctx.items_received = []
try:
os.remove(os.path.join(ctx.save_game_folder, "receivedItems"))
except OSError:
pass
elif start_index != len(ctx.items_received):
sync_msg = [{"cmd": "Sync"}]
if ctx.locations_checked:
sync_msg.append({"cmd": "LocationChecks", "locations": list(ctx.locations_checked)})
await ctx.send_msgs(sync_msg)
if start_index == len(ctx.items_received):
for item in args["items"]:
ctx.items_received.append(NetworkItem(*item))
with open(os.path.join(ctx.save_game_folder, "receivedItems"), "a") as f:
for index, item in enumerate(ctx.items_received):
f.write(str(index) + "\n")
f.write(ctx.item_names[item.item] + "\n")
f.close()
with open(os.path.join(ctx.save_game_folder, "newItems"), "w") as f:
f.close()
elif cmd == "RoomUpdate":
# TODO handle location getting marked as checked from server
pass
async def game_watcher(ctx: RustedMossContext):
while not ctx.exit_event.is_set():
if ctx.got_deathlink:
ctx.got_deathlink = False
if ctx.deathlink_status:
with open(os.path.join(ctx.save_game_folder, "deathlinkFromServer"), "w") as f:
f.close()
sending = []
for root, dirs, files in os.walk(ctx.save_game_folder):
for file in files:
if "deathlinkFromClient" in file:
if ctx.deathlink_status:
await ctx.send_death()
os.remove(os.path.join(root, file))
if "scoutLocations" == file:
try:
with open(os.path.join(root, file), "r") as f:
locations = f.readlines()
f.close()
for location in locations:
locationName = gameLocationToLocationName[location.strip()]
locationId = AutoWorldRegister.world_types[ctx.game].location_name_to_id[locationName]
if locationId in ctx.server_locations:
sending.append(locationId)
finally:
await ctx.send_msgs([{"cmd": "LocationScouts", "locations": sending, "create_as_hint": 2}])
os.remove(os.path.join(root, file))
if "newLocations" == file:
try:
locations = []
with open(os.path.join(root, file), "r") as f:
locations = f.readlines()
with open(os.path.join(ctx.save_game_folder, "checkedLocations"), "a") as f:
for location in locations:
locationName = gameLocationToLocationName[location.strip()]
locationId = AutoWorldRegister.world_types[ctx.game].location_name_to_id[locationName]
if locationId in ctx.server_locations:
sending.append(locationId)
f.write(str(locationId) + "\n")
f.close()
finally:
await ctx.send_msgs([{"cmd": "LocationChecks", "locations": sending}])
os.remove(os.path.join(root, file))
if "endingAchieved" == file:
with open(os.path.join(root, file), "r") as f:
endingNumber = int(f.readline().strip())
sendVictory = False
if ctx.ending == 0 and endingNumber <= 3:
sendVictory = True
elif ctx.ending == 1 and endingNumber <= 2:
sendVictory = True
elif ctx.ending == 2 and endingNumber <= 2:
sendVictory = True
elif ctx.ending == 3 and endingNumber == 3:
sendVictory = True
elif ctx.ending == 4:
sendVictory = True
if not ctx.finished_game and sendVictory:
await ctx.send_msgs([{"cmd": "StatusUpdate", "status": ClientStatus.CLIENT_GOAL}])
ctx.finished_game = True
f.close()
os.remove(os.path.join(root, file))
ctx.locations_checked = sending
await asyncio.sleep(0.1)
def main():
Utils.init_logging("RustedMossClient", exception_logger="Client")
async def _main():
ctx = RustedMossContext(None, None)
ctx.server_task = asyncio.create_task(server_loop(ctx), name="server loop")
asyncio.create_task(game_watcher(ctx), name="RustedMossProgressionWatcher")
if gui_enabled:
ctx.run_gui()
ctx.run_cli()
await ctx.exit_event.wait()
await ctx.shutdown()
import colorama
colorama.init()
asyncio.run(_main())
colorama.deinit()
if __name__ == "__main__":
parser = get_base_parser(description="Rusted Moss Client, for text interfacing.")
args = parser.parse_args()
main()