-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtermfish.py
More file actions
529 lines (430 loc) · 17.6 KB
/
termfish.py
File metadata and controls
529 lines (430 loc) · 17.6 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import struct
import os
import time
import sys
import yaml
from datetime import datetime
from pathlib import Path
# --- config ---
SCRIPT_DIR = Path(__file__).resolve().parent
DEFAULT_CONFIG = """\
# path to your webfishing save directory
# usually here on windows
save_directory: "%AppData%/Godot/app_userdata/webfishing_2_newver"
# which save slot to read (1-4)
save_slot: 1
# how often code check the save file for changes (seconds)
# note this is not how often it will update because the game autosaves every 5 minutes.
poll_interval: 2
# if u don't use windows terminal, this probably won't do anything. otherwise it'll change the tab color & name
windows_terminal:
enabled: true
tab_title: "termfish"
tab_color: "FFB6C1"
"""
def load_config():
config_path = SCRIPT_DIR / "config.yaml"
if not config_path.exists():
config_path.write_text(DEFAULT_CONFIG)
print(f"created default config.yaml at {config_path}")
with open(config_path, "r") as f:
return yaml.safe_load(f)
def resolve_save_path(config):
directory = os.path.expandvars(config.get("save_directory", ""))
slot = config.get("save_slot", 2)
filename = f"webfishing_save_slot_{slot - 1}.sav"
return os.path.join(directory, filename)
def setup_terminal(config):
# enable ansi on windows
os.system("")
wt = config.get("windows_terminal", {})
if not wt.get("enabled", False):
return
title = wt.get("tab_title", "")
if title:
sys.stdout.write(f"\x1b]2;{title}\x07")
color_hex = wt.get("tab_color", "")
if color_hex:
color_hex = color_hex.lstrip("#")
if len(color_hex) == 6:
r = int(color_hex[0:2], 16) * 257
g = int(color_hex[2:4], 16) * 257
b = int(color_hex[4:6], 16) * 257
sys.stdout.write(f"\x1b]4;264;rgb:{r:04x}/{g:04x}/{b:04x}\x1b\\")
sys.stdout.flush()
# --- ansi ---
RESET = "\033[0m"
BOLD = "\033[1m"
DIM = "\033[90m"
WHITE = "\033[97m"
QUALITY_INFO = {
0: {"name": "Normal", "color": "\033[38;5;180m", "bright": "\033[1;38;5;180m"},
1: {"name": "Shining", "color": "\033[38;5;137m", "bright": "\033[1;38;5;137m"},
2: {"name": "Glistening", "color": "\033[38;5;247m", "bright": "\033[1;38;5;247m"},
3: {"name": "Opulent", "color": "\033[38;5;30m", "bright": "\033[1;38;5;30m"},
4: {"name": "Radiant", "color": "\033[38;5;172m", "bright": "\033[1;38;5;172m"},
5: {"name": "Alpha", "color": "\033[38;5;161m", "bright": "\033[1;38;5;161m"},
}
BLOCK_FILLED = "\u2b1b"
BLOCK_EMPTY = "\u2b1c"
NAME_WIDTH = 20
# --- fish data ---
LOCATION_STYLE = {
"lake": {"name": "FRESHWATER", "bg": "\033[48;5;194m\033[38;5;22m"},
"ocean": {"name": "SALTWATER", "bg": "\033[48;5;189m\033[38;5;18m"},
"rain": {"name": "RAIN", "bg": "\033[48;5;195m\033[38;5;23m"},
"water_trash": {"name": "TRASH", "bg": "\033[48;5;230m\033[38;5;94m"},
"alien": {"name": "ALIEN", "bg": "\033[48;5;225m\033[38;5;54m"},
"void": {"name": "VOID", "bg": "\033[48;5;219m\033[38;5;54m"},
}
LOCATION_ORDER = ["lake", "ocean", "water_trash", "rain", "void", "alien"]
FISH_ORDER = {
"lake": [
"fish_lake_alligator", "fish_lake_axolotl", "fish_lake_bass", "fish_lake_bluegill",
"fish_lake_bowfin", "fish_lake_bullshark", "fish_lake_carp", "fish_lake_catfish",
"fish_lake_crab", "fish_lake_crappie", "fish_lake_crayfish", "fish_lake_drum",
"fish_lake_frog", "fish_lake_gar", "fish_lake_golden_bass", "fish_lake_goldfish",
"fish_lake_guppy", "fish_lake_kingsalmon", "fish_lake_koi", "fish_lake_leech",
"fish_lake_mooneye", "fish_lake_muskellunge", "fish_lake_perch", "fish_lake_pike",
"fish_lake_pupfish", "fish_lake_rainbowtrout", "fish_lake_salmon", "fish_lake_snail",
"fish_lake_sturgeon", "fish_lake_toad", "fish_lake_turtle", "fish_lake_walleye",
],
"ocean": [
"fish_ocean_angelfish", "fish_ocean_atlantic_salmon", "fish_ocean_bluefish",
"fish_ocean_clownfish", "fish_ocean_coalacanth", "fish_ocean_dogfish", "fish_ocean_eel",
"fish_ocean_flounder", "fish_ocean_golden_manta_ray", "fish_ocean_greatwhiteshark",
"fish_ocean_grouper", "fish_ocean_hammerhead_shark", "fish_ocean_herring", "fish_ocean_krill",
"fish_ocean_lionfish", "fish_ocean_lobster", "fish_ocean_manowar", "fish_ocean_manta_ray",
"fish_ocean_marlin", "fish_ocean_octopus", "fish_ocean_oyster", "fish_ocean_sawfish",
"fish_ocean_sea_turtle", "fish_ocean_seahorse", "fish_ocean_shrimp", "fish_ocean_squid",
"fish_ocean_stingray", "fish_ocean_sunfish", "fish_ocean_swordfish", "fish_ocean_tuna",
"fish_ocean_whale", "fish_ocean_wolffish",
],
"water_trash": [
"wtrash_bone", "wtrash_boot", "wtrash_branch", "wtrash_diamond",
"wtrash_drink_rings", "wtrash_plastic_bag", "wtrash_sodacan", "wtrash_weed",
],
"rain": [
"fish_rain_anomalocaris", "fish_rain_heliocoprion",
"fish_rain_horseshoe_crab", "fish_rain_leedsichthys",
],
"void": ["fish_void_voidfish"],
"alien": ["fish_alien_dog"],
}
# fish whose display name can't be derived from their id
NAME_OVERRIDES = {
"fish_alien_dog": "UFO",
"fish_deep_test": "Axolotl",
"fish_deep_testb": "Axolotl",
"fish_deep_testc": "Axolotl",
"fish_lake_bass": "Largemouth Bass",
"fish_lake_bullshark": "Bull Shark",
"fish_lake_kingsalmon": "King Salmon",
"fish_lake_rainbowtrout": "Rainbow Trout",
"fish_ocean_coalacanth": "Coelacanth",
"fish_ocean_greatwhiteshark": "Great White Shark",
"fish_ocean_manowar": "Man O' War",
"fish_ocean_stingray": "Sting Ray",
"fish_rain_heliocoprion": "Helicoprion",
"fish_void_voidfish": "CREATURE",
"wtrash_boot": "Old Boot",
"wtrash_sodacan": "Soda Can",
}
PREFIXES = ("fish_lake_", "fish_ocean_", "fish_rain_", "fish_void_", "fish_alien_", "fish_deep_", "wtrash_")
def _derive_name(fish_id):
for prefix in PREFIXES:
if fish_id.startswith(prefix):
return fish_id[len(prefix):].replace("_", " ").title()
return fish_id
def _build_fish_names():
names = {}
for fish_list in FISH_ORDER.values():
for fish_id in fish_list:
names[fish_id] = NAME_OVERRIDES.get(fish_id, _derive_name(fish_id))
for fish_id, name in NAME_OVERRIDES.items():
if fish_id not in names:
names[fish_id] = name
return names
FISH_NAMES = _build_fish_names()
# --- godot 3.x binary variant parser (read-only) ---
def _read_uint16(data, pos):
return struct.unpack_from("<H", data, pos)[0], pos + 2
def _read_uint32(data, pos):
return struct.unpack_from("<I", data, pos)[0], pos + 4
def _read_int32(data, pos):
return struct.unpack_from("<i", data, pos)[0], pos + 4
def _read_int64(data, pos):
return struct.unpack_from("<q", data, pos)[0], pos + 8
def _read_float(data, pos):
return struct.unpack_from("<f", data, pos)[0], pos + 4
def _read_double(data, pos):
return struct.unpack_from("<d", data, pos)[0], pos + 8
def _read_string(data, pos):
length, pos = _read_uint32(data, pos)
s = data[pos:pos + length].decode("utf-8")
pos += length
pos += (4 - (length % 4)) % 4
return s, pos
def _read_variant(data, pos):
vtype, pos = _read_uint16(data, pos)
flags, pos = _read_uint16(data, pos)
is_64 = (flags & 1) != 0
if vtype == 0:
return None, pos
elif vtype == 1:
val, pos = _read_uint32(data, pos)
return val != 0, pos
elif vtype == 2:
return _read_int64(data, pos) if is_64 else _read_int32(data, pos)
elif vtype == 3:
return _read_double(data, pos) if is_64 else _read_float(data, pos)
elif vtype == 4:
return _read_string(data, pos)
elif vtype == 5:
if is_64:
x, pos = _read_double(data, pos)
y, pos = _read_double(data, pos)
else:
x, pos = _read_float(data, pos)
y, pos = _read_float(data, pos)
return (x, y), pos
elif vtype == 7:
if is_64:
x, pos = _read_double(data, pos)
y, pos = _read_double(data, pos)
z, pos = _read_double(data, pos)
else:
x, pos = _read_float(data, pos)
y, pos = _read_float(data, pos)
z, pos = _read_float(data, pos)
return (x, y, z), pos
elif vtype == 14:
r, pos = _read_float(data, pos)
g, pos = _read_float(data, pos)
b, pos = _read_float(data, pos)
a, pos = _read_float(data, pos)
return (r, g, b, a), pos
elif vtype == 15:
name_count, pos = _read_uint32(data, pos)
subname_count = (name_count >> 16) & 0xFFFF
name_count = name_count & 0xFFFF
_, pos = _read_uint32(data, pos)
parts = []
for _ in range(name_count + subname_count):
s, pos = _read_string(data, pos)
parts.append(s)
return "/".join(parts), pos
elif vtype == 18:
size, pos = _read_uint32(data, pos)
size &= 0x7FFFFFFF
result = {}
for _ in range(size):
key, pos = _read_variant(data, pos)
val, pos = _read_variant(data, pos)
result[key] = val
return result, pos
elif vtype == 19:
size, pos = _read_uint32(data, pos)
size &= 0x7FFFFFFF
result = []
for _ in range(size):
val, pos = _read_variant(data, pos)
result.append(val)
return result, pos
elif vtype == 20:
length, pos = _read_uint32(data, pos)
val = data[pos:pos + length]
pos += length
pos += (4 - (length % 4)) % 4
return val, pos
elif vtype in (21, 22, 23):
length, pos = _read_uint32(data, pos)
vals = []
for _ in range(length):
if vtype == 21:
v, pos = _read_int32(data, pos)
elif vtype == 22:
v, pos = _read_float(data, pos)
else:
v, pos = _read_string(data, pos)
vals.append(v)
return vals, pos
else:
raise ValueError(f"unsupported godot variant type: {vtype} at position {pos - 4}")
def read_save(path):
data = Path(path).read_bytes()
_size, pos = _read_uint32(data, 0)
save_data, _ = _read_variant(data, pos)
return save_data
def safe_read_save(path):
try:
return read_save(path)
except (OSError, PermissionError, struct.error, ValueError, UnicodeDecodeError, OverflowError):
return None
# --- journal extraction ---
def extract_journal(save_data):
journal = save_data.get("journal", {})
snapshot = {}
counts = {}
for location, fish_dict in journal.items():
if not isinstance(fish_dict, dict):
continue
for fish_id, info in fish_dict.items():
if fish_id.startswith("_") or not isinstance(info, dict):
continue
if fish_id not in FISH_NAMES:
continue
qualities = info.get("quality", [])
snapshot[(location, fish_id)] = set(int(q) for q in qualities) if isinstance(qualities, list) else set()
counts[(location, fish_id)] = info.get("count", 0)
return snapshot, counts
# --- display ---
def make_row(name, qualities, bg, count=0, new_qualities=None):
if new_qualities is None:
new_qualities = set()
name_part = f"{bg} {name:<{NAME_WIDTH}} {RESET}"
cells = ""
for q in range(6):
info = QUALITY_INFO[q]
if q in new_qualities or q in qualities:
cells += f" {info['bright']}{BLOCK_FILLED}{RESET} "
else:
cells += f" {DIM}{BLOCK_EMPTY}{RESET} "
count_str = f"{DIM}x{count}{RESET}" if count else ""
return f"{name_part}{cells} {count_str}"
def format_duration(seconds):
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
if hours > 0:
return f"{hours}h {minutes:02d}m {secs:02d}s"
return f"{minutes}m {secs:02d}s"
def make_stats(snapshot, total_caught, mtime, session):
total_fish = len(snapshot)
if total_fish == 0:
return ""
lines = []
lines.append(f" {BOLD}{WHITE}COMPLETION{RESET}")
lines.append("")
total_owned = 0
total_slots = total_fish * 6
for q in range(6):
info = QUALITY_INFO[q]
count = sum(1 for qualities in snapshot.values() if q in qualities)
pct = (count / total_fish) * 100
total_owned += count
bar_filled = int(pct / 5)
bar_empty = 20 - bar_filled
bar = f"{info['color']}{chr(0x2588) * bar_filled}{DIM}{chr(0x2591) * bar_empty}{RESET}"
lines.append(f" {info['bright']}{info['name']:<12}{RESET} {bar} {BOLD}{WHITE}{pct:5.1f}%{RESET}")
total_pct = (total_owned / total_slots) * 100 if total_slots else 0
lines.append(f" {'':12} {'':20} {'':5}")
bar_filled = int(total_pct / 5)
bar_empty = 20 - bar_filled
bar = f"{WHITE}{chr(0x2588) * bar_filled}{DIM}{chr(0x2591) * bar_empty}{RESET}"
lines.append(f" {BOLD}{WHITE}{'Total':<12}{RESET} {bar} {BOLD}{WHITE}{total_pct:5.1f}%{RESET}")
lines.append(f" {DIM}{total_owned}/{total_slots} uniques \u2022 {total_caught} total{RESET}")
lines.append("")
timestamp = datetime.fromtimestamp(mtime).strftime("%H:%M:%S")
lines.append(f" {DIM}last updated {timestamp}{RESET}")
elapsed = time.time() - session["start_time"]
session_catches = total_caught - session["start_catches"]
new_uniques = session["new_uniques"]
lines.append(f" {DIM}this session: {format_duration(elapsed)} \u2022 {session_catches} catches \u2022 {new_uniques} new{RESET}")
return "\n".join(lines)
def build_display(snapshot, counts, total_caught, mtime, session, newly_obtained=None):
if newly_obtained is None:
newly_obtained = {}
lines = []
for location_key in LOCATION_ORDER:
style = LOCATION_STYLE.get(location_key)
if not style:
continue
order = FISH_ORDER.get(location_key, [])
caught_here = {fid: qualities for (location, fid), qualities in snapshot.items() if location == location_key}
ordered_ids = [fid for fid in order if fid in caught_here]
ordered_ids += [fid for fid in caught_here if fid not in order]
if not ordered_ids:
continue
for fish_id in ordered_ids:
name = FISH_NAMES.get(fish_id, fish_id)
qualities = caught_here[fish_id]
new_quals = newly_obtained.get((location_key, fish_id), set())
count = counts.get((location_key, fish_id), 0)
lines.append(make_row(name, qualities, style["bg"], count, new_quals))
lines.append("")
lines.append(make_stats(snapshot, total_caught, mtime, session))
return "\n".join(lines)
def format_new_log(fish_id, location, new_qualities):
name = FISH_NAMES.get(fish_id, fish_id)
timestamp = datetime.now().strftime("%H:%M:%S")
entries = []
for q in sorted(new_qualities):
info = QUALITY_INFO[q]
entries.append(f" {DIM}[{timestamp}]{RESET} {info['bright']}{info['name']}{RESET} {BOLD}{WHITE}{name}{RESET}")
return entries
def full_draw(snapshot, counts, total_caught, mtime, log_lines, session, newly_obtained=None):
content = build_display(snapshot, counts, total_caught, mtime, session, newly_obtained)
if log_lines:
content += f"\n\n {BOLD}{WHITE}NEW CATCHES{RESET}\n"
for line in log_lines:
content += line + "\n"
frame = f"\033[?25l\033[H\033[2J\033[3J{content}\033[?25h"
sys.stdout.write(frame)
sys.stdout.flush()
# --- main ---
def main():
config = load_config()
save_path = resolve_save_path(config)
poll_interval = config.get("poll_interval", 2)
setup_terminal(config)
if not os.path.exists(save_path):
print(f"save file not found: {save_path}")
sys.exit(1)
save_data = None
while save_data is None:
save_data = safe_read_save(save_path)
if save_data is None:
print("save file busy, retrying...")
time.sleep(2)
prev_snapshot, prev_counts = extract_journal(save_data)
total_caught = save_data.get("fish_caught", 0)
prev_mtime = os.path.getmtime(save_path)
log_lines = []
session = {
"start_time": time.time(),
"start_catches": total_caught,
"new_uniques": 0,
}
full_draw(prev_snapshot, prev_counts, total_caught, prev_mtime, log_lines, session)
try:
while True:
time.sleep(poll_interval)
try:
current_mtime = os.path.getmtime(save_path)
except OSError:
continue
if current_mtime == prev_mtime:
continue
save_data = safe_read_save(save_path)
if save_data is None:
continue
prev_mtime = current_mtime
new_snapshot, new_counts = extract_journal(save_data)
total_caught = save_data.get("fish_caught", 0)
newly_obtained = {}
for key, new_qualities in new_snapshot.items():
old_qualities = prev_snapshot.get(key, set())
fresh = new_qualities - old_qualities
if fresh:
newly_obtained[key] = fresh
session["new_uniques"] += len(fresh)
location, fish_id = key
log_lines.extend(format_new_log(fish_id, location, fresh))
full_draw(new_snapshot, new_counts, total_caught, prev_mtime, log_lines, session, newly_obtained or None)
prev_snapshot = new_snapshot
prev_counts = new_counts
except KeyboardInterrupt:
print(f"\n{RESET}stopped")
if __name__ == "__main__":
main()