uppercut-port-proof 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- uppercut_port_proof-1.0.0/.gitignore +16 -0
- uppercut_port_proof-1.0.0/LICENSE +21 -0
- uppercut_port_proof-1.0.0/PKG-INFO +167 -0
- uppercut_port_proof-1.0.0/README.md +145 -0
- uppercut_port_proof-1.0.0/pyproject.toml +38 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/__init__.py +19 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/__main__.py +5 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/cli.py +138 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/debug.py +178 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/errors.py +23 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/frames.py +89 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/png.py +65 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/ppm.py +109 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/presses.py +142 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/py.typed +0 -0
- uppercut_port_proof-1.0.0/src/uppercut_port_proof/wav.py +154 -0
- uppercut_port_proof-1.0.0/tests/conftest.py +6 -0
- uppercut_port_proof-1.0.0/tests/port_proof_synthetic.py +105 -0
- uppercut_port_proof-1.0.0/tests/test_cli.py +70 -0
- uppercut_port_proof-1.0.0/tests/test_debug.py +119 -0
- uppercut_port_proof-1.0.0/tests/test_frames.py +58 -0
- uppercut_port_proof-1.0.0/tests/test_ppm_png.py +129 -0
- uppercut_port_proof-1.0.0/tests/test_presses.py +72 -0
- uppercut_port_proof-1.0.0/tests/test_wav.py +113 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Uppercut Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: uppercut-port-proof
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Helpers for the proof run of a recompiled game port: PPM frame dumps to PNG, frame MD5 comparison, WAV statistics, press scripts and a debug-socket driver.
|
|
5
|
+
Project-URL: Source, https://github.com/uppercut-labs/game-kit/tree/main/packages/uppercut-port-proof
|
|
6
|
+
Author-email: Uppercut Labs <packages@uppercut-labs.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: debug-socket,frame-dump,game-port,png,ppm,proof-run,recompilation,wav
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Games/Entertainment
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# uppercut-port-proof
|
|
24
|
+
|
|
25
|
+
The small tools a proof run of a recompiled game port keeps needing: turn the frame dumps (PPM) into PNG files, crop and
|
|
26
|
+
enlarge a region, compare two runs frame by frame, measure the audio the port wrote, turn a list of button presses into
|
|
27
|
+
input for the port or for an emulator, and drive a port that has a debug socket. Standard library only.
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
pip install uppercut-port-proof
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Requires Python 3.10 or newer. Nothing here knows a game, a console's BIOS or a disc: a port's own commands, its pad
|
|
34
|
+
bits and its paths are arguments.
|
|
35
|
+
|
|
36
|
+
## Commands
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
uppercut-port-proof ppm2png out/png run/frame_*.ppm # PNGs, and each frame's MD5
|
|
40
|
+
uppercut-port-proof ppm-crop run/frame_0300.ppm out/hud.png 8 8 120 40 4
|
|
41
|
+
uppercut-port-proof md5-compare run-a run-b report.txt # frame_*.ppm of A against the same names in B
|
|
42
|
+
uppercut-port-proof wav-stats run/audio.wav --silence-db -66.2 --json
|
|
43
|
+
uppercut-port-proof presses2script presses.txt 9000 script.txt --shot-every 300
|
|
44
|
+
uppercut-port-proof drive --presses presses.txt --until 9000 --shot-every 300 --out run/shots --port 4370 --quit
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Every command exits 0 on success and 2 on a file or argument it cannot use, with the reason on stderr. `md5-compare` also
|
|
48
|
+
takes `--strict` (exit 1 unless every frame matches), and `ppm2png` and `ppm-crop` take `--stored` (see below).
|
|
49
|
+
|
|
50
|
+
## Python
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from uppercut_port_proof import (DebugClient, compare_frame_dirs, parse_presses, read_ppm, run_presses,
|
|
54
|
+
stats_for_file, to_script, write_png)
|
|
55
|
+
|
|
56
|
+
pic = read_ppm("run/frame_0300.ppm") # Ppm(width, height, pixels): 8-bit RGB
|
|
57
|
+
write_png("frame.png", pic.width, pic.height, pic.pixels)
|
|
58
|
+
|
|
59
|
+
result = compare_frame_dirs("run-a", "run-b") # FrameComparison: rows, same, different, missing
|
|
60
|
+
print(result.summary(), result.identical)
|
|
61
|
+
|
|
62
|
+
stats = stats_for_file("run/audio.wav", silence_db=-66.2) # a dict: see the keys below
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Frames and PNG
|
|
66
|
+
|
|
67
|
+
| Name | What it does |
|
|
68
|
+
| --- | --- |
|
|
69
|
+
| `read_ppm(path)`, `parse_ppm(bytes)` | A binary PPM (P6) of 8-bit samples. A comment in the header is skipped. A file with fewer pixel bytes than its header promises, a maxval over 255 or another magic raises `PpmError`; bytes after the pixels are ignored. |
|
|
70
|
+
| `write_ppm(path, pic)`, `encode_ppm(pic)` | The inverse. |
|
|
71
|
+
| `crop(pic, x0, y0, x1, y1)`, `enlarge(pic, k)` | A region (the end is not included), and each pixel repeated `k` times across and down. |
|
|
72
|
+
| `md5_of(path)` | The MD5 of a file, to tell two dumps apart. |
|
|
73
|
+
| `encode_png(w, h, pixels, channels=3, compress=True, level=6)`, `write_png(path, ...)`, `ppm_to_png(source, target)` | 8-bit RGB or RGBA with `zlib`. |
|
|
74
|
+
|
|
75
|
+
`compress=False` writes stored deflate blocks, with the same bytes the C package [`minimal-png`](../minimal-png) writes for the
|
|
76
|
+
same image (the tests pin one file in both), so a Python tool and a port can be compared with `cmp`.
|
|
77
|
+
|
|
78
|
+
### Frame comparison
|
|
79
|
+
|
|
80
|
+
`compare_frame_dirs(a, b, prefix="frame_", suffix=".ppm")` MD5s every matching file of folder `a` against the same name in `b`.
|
|
81
|
+
`FrameComparison.rows` are `FrameRow(name, md5_a, md5_b)` (`md5_b` is None when `b` lacks the file), with `status` of `same`,
|
|
82
|
+
`different` or `missing`; `only_in_b` lists the frames `a` lacks. `lines()` are the report lines, one per frame and then the
|
|
83
|
+
summary `N frames in A: x identical, y different, z missing in B`.
|
|
84
|
+
|
|
85
|
+
### Audio
|
|
86
|
+
|
|
87
|
+
`stats_for_file(path, silence_db=-66.2)` reads a 16-bit PCM WAV and returns a dict:
|
|
88
|
+
|
|
89
|
+
| Key | Meaning |
|
|
90
|
+
| --- | --- |
|
|
91
|
+
| `file`, `sample_rate`, `channels`, `frames`, `seconds` | the format and length (seconds rounded to 2 places) |
|
|
92
|
+
| `peak`, `peak_dbfs`, `rms_dbfs` | the largest absolute sample (32768 for a full-scale negative), and both in dBFS (`-Infinity` for digital silence) |
|
|
93
|
+
| `full_scale_samples`, `total_samples`, `full_scale_percent` | samples at 32767 or beyond, out of all samples of all channels |
|
|
94
|
+
| `silence_threshold_dbfs` | the threshold used: `silence_db` |
|
|
95
|
+
| `one_second_windows`, `silent_one_second_windows` | whole seconds, and those whose largest sample (any channel) is under the threshold |
|
|
96
|
+
| `silent_stretches_1s_or_more` | `[{"start_s", "length_s"}]`, found at 100 ms resolution with the same test |
|
|
97
|
+
| `longest_silence_s` | the longest stretch found, of any length |
|
|
98
|
+
| `header_repaired` | present and true only when the header's data length did not fit the file |
|
|
99
|
+
|
|
100
|
+
The header of a file from a run that was killed is never brought up to date: the data length reads 0, or more than the file
|
|
101
|
+
holds. The data then runs to the end of the file, so what was written is still measured (and `header_repaired` says so). A
|
|
102
|
+
half frame at the end is dropped. Only 16-bit PCM is handled; anything else raises `WavError`. The JSON uses Python's
|
|
103
|
+
`-Infinity` for digital silence, which Python reads back and strict JSON parsers do not.
|
|
104
|
+
|
|
105
|
+
### Press files
|
|
106
|
+
|
|
107
|
+
A press file is plain text, one press a line: `frame:button[+button...][:frames]`. The press starts when the game's frame counter
|
|
108
|
+
reaches `frame` and is held for `frames` frames (the default is yours: 4 for `drive`, 6 for `presses2script`). A `#` starts a
|
|
109
|
+
comment, and a line that begins with `p2 ` is left out (it belongs to a second pad).
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
# open the menu, then confirm
|
|
113
|
+
300:start
|
|
114
|
+
360:cross+up:12
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`parse_presses(text, default_hold=4, skip_prefixes=("p2 ",))` gives `Press(frame, buttons, hold)` records sorted by frame, and
|
|
118
|
+
names the line of anything it cannot read. `pad_word(buttons)` is the 16-bit pad word with those buttons pressed (all bits
|
|
119
|
+
set, each pressed button's bit cleared: the original console's pad protocol), from the table `PS1_BITS`.
|
|
120
|
+
`to_script(presses, total, shot_every=0, shot_ranges=(), prefix="f", turbo=False)` writes the lines of an emulator build's script for the same run:
|
|
121
|
+
held buttons as `count Button+Button`, idle stretches as `wait count`, `shot <prefix><frame>` before the input of that frame, then `quit`. The
|
|
122
|
+
button spellings are in `SCRIPT_BUTTON_NAMES`. Only pad 1 is scripted.
|
|
123
|
+
|
|
124
|
+
### The debug socket
|
|
125
|
+
|
|
126
|
+
Some recompiled ports built with their debug tools take commands over a local TCP socket: one JSON line in (`{"cmd": ...}`),
|
|
127
|
+
one JSON line out, then the server closes the connection. `DebugClient(host, port)` opens a new connection for every call
|
|
128
|
+
and tries again (5 times, 50 ms apart) when none answers, then raises `DebugError`, which is also a `ConnectionError`. The
|
|
129
|
+
commands are the port's own; `frame`, `press`, `clear_input`, `screenshot` and `quit` are the ones the drive loop uses.
|
|
130
|
+
|
|
131
|
+
| Name | What it does |
|
|
132
|
+
| --- | --- |
|
|
133
|
+
| `DebugClient(host="127.0.0.1", port=4370, timeout=30, wait=True)` | Waits up to `timeout` seconds for the server to listen. `.call(cmd, **fields)`, `.frame()`. |
|
|
134
|
+
| `run_presses(client, presses, until, shot_every=300, out_dir=None, quit_at_end=False)` | Presses each button when the frame counter reaches its frame, holds it, releases it a frame after, and saves `shot_NNNNNN.png` every `shot_every` frames, until `until`. Frame numbers are open loop (a poll or two of jitter). |
|
|
135
|
+
| `wait_frame(get_frame, target, timeout=90, alive=None)` | Polls until the counter reaches `target`. `alive()` returning False stops the wait with `DebugError` at once instead of at the timeout. |
|
|
136
|
+
| `wait_ready(ping, timeout=30, alive=None)` | Calls `ping()` until it stops raising `OSError` or `TimeoutError`. `alive()` is asked before each ping, so a program that is already gone is not pinged. |
|
|
137
|
+
|
|
138
|
+
`wait_frame` and `wait_ready` take plain callables, so a session that has its own way to talk to the port (a request that
|
|
139
|
+
carries an identity token, say) uses them with its own `command`.
|
|
140
|
+
|
|
141
|
+
## Not here, on purpose
|
|
142
|
+
|
|
143
|
+
Other packages in this repository, or in py-kit, already do these; use them rather than a copy.
|
|
144
|
+
|
|
145
|
+
- Finding a program's window on macOS and capturing it: [`mac-window-tools`](../mac-window-tools).
|
|
146
|
+
- Contact sheets from many frames: `uppercut-contact-sheet` in py-kit (it needs Pillow; `PIL` reads PPM).
|
|
147
|
+
- Controller replay files and capture scripts for a port's own replay format: [`pad-replay`](../pad-replay) and
|
|
148
|
+
[`uppercut-pad-replay-tools`](../uppercut-pad-replay-tools). `to_script` writes the same `wait` / `shot` / `quit` lines its
|
|
149
|
+
`CaptureScript` does, with held buttons added.
|
|
150
|
+
- Reading a PNG back and checking it: `validate_png` in [`uppercut-mgba-smoke`](../uppercut-mgba-smoke).
|
|
151
|
+
|
|
152
|
+
## What runs where
|
|
153
|
+
|
|
154
|
+
Everything runs on Linux, macOS and Windows with Python 3.10 or newer, with no display and no hardware. The tests start a fake
|
|
155
|
+
debug server on a local port (127.0.0.1, any free port) and write files under a temporary folder.
|
|
156
|
+
|
|
157
|
+
Tested at 1.0.0: `uv run pytest` passes (70 tests) on Windows 11 Pro (10.0.26300, Python 3.14.0) and on macOS 27.0
|
|
158
|
+
(Apple M6, Python 3.13.18). The Windows run found one test that split a screenshot path on `/` and failed on `\`; the test now uses
|
|
159
|
+
`os.path.basename`.
|
|
160
|
+
|
|
161
|
+
```sh
|
|
162
|
+
uv sync --all-packages && uv run pytest
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Licence
|
|
166
|
+
|
|
167
|
+
MIT, see `LICENSE`.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# uppercut-port-proof
|
|
2
|
+
|
|
3
|
+
The small tools a proof run of a recompiled game port keeps needing: turn the frame dumps (PPM) into PNG files, crop and
|
|
4
|
+
enlarge a region, compare two runs frame by frame, measure the audio the port wrote, turn a list of button presses into
|
|
5
|
+
input for the port or for an emulator, and drive a port that has a debug socket. Standard library only.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pip install uppercut-port-proof
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Python 3.10 or newer. Nothing here knows a game, a console's BIOS or a disc: a port's own commands, its pad
|
|
12
|
+
bits and its paths are arguments.
|
|
13
|
+
|
|
14
|
+
## Commands
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
uppercut-port-proof ppm2png out/png run/frame_*.ppm # PNGs, and each frame's MD5
|
|
18
|
+
uppercut-port-proof ppm-crop run/frame_0300.ppm out/hud.png 8 8 120 40 4
|
|
19
|
+
uppercut-port-proof md5-compare run-a run-b report.txt # frame_*.ppm of A against the same names in B
|
|
20
|
+
uppercut-port-proof wav-stats run/audio.wav --silence-db -66.2 --json
|
|
21
|
+
uppercut-port-proof presses2script presses.txt 9000 script.txt --shot-every 300
|
|
22
|
+
uppercut-port-proof drive --presses presses.txt --until 9000 --shot-every 300 --out run/shots --port 4370 --quit
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Every command exits 0 on success and 2 on a file or argument it cannot use, with the reason on stderr. `md5-compare` also
|
|
26
|
+
takes `--strict` (exit 1 unless every frame matches), and `ppm2png` and `ppm-crop` take `--stored` (see below).
|
|
27
|
+
|
|
28
|
+
## Python
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from uppercut_port_proof import (DebugClient, compare_frame_dirs, parse_presses, read_ppm, run_presses,
|
|
32
|
+
stats_for_file, to_script, write_png)
|
|
33
|
+
|
|
34
|
+
pic = read_ppm("run/frame_0300.ppm") # Ppm(width, height, pixels): 8-bit RGB
|
|
35
|
+
write_png("frame.png", pic.width, pic.height, pic.pixels)
|
|
36
|
+
|
|
37
|
+
result = compare_frame_dirs("run-a", "run-b") # FrameComparison: rows, same, different, missing
|
|
38
|
+
print(result.summary(), result.identical)
|
|
39
|
+
|
|
40
|
+
stats = stats_for_file("run/audio.wav", silence_db=-66.2) # a dict: see the keys below
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Frames and PNG
|
|
44
|
+
|
|
45
|
+
| Name | What it does |
|
|
46
|
+
| --- | --- |
|
|
47
|
+
| `read_ppm(path)`, `parse_ppm(bytes)` | A binary PPM (P6) of 8-bit samples. A comment in the header is skipped. A file with fewer pixel bytes than its header promises, a maxval over 255 or another magic raises `PpmError`; bytes after the pixels are ignored. |
|
|
48
|
+
| `write_ppm(path, pic)`, `encode_ppm(pic)` | The inverse. |
|
|
49
|
+
| `crop(pic, x0, y0, x1, y1)`, `enlarge(pic, k)` | A region (the end is not included), and each pixel repeated `k` times across and down. |
|
|
50
|
+
| `md5_of(path)` | The MD5 of a file, to tell two dumps apart. |
|
|
51
|
+
| `encode_png(w, h, pixels, channels=3, compress=True, level=6)`, `write_png(path, ...)`, `ppm_to_png(source, target)` | 8-bit RGB or RGBA with `zlib`. |
|
|
52
|
+
|
|
53
|
+
`compress=False` writes stored deflate blocks, with the same bytes the C package [`minimal-png`](../minimal-png) writes for the
|
|
54
|
+
same image (the tests pin one file in both), so a Python tool and a port can be compared with `cmp`.
|
|
55
|
+
|
|
56
|
+
### Frame comparison
|
|
57
|
+
|
|
58
|
+
`compare_frame_dirs(a, b, prefix="frame_", suffix=".ppm")` MD5s every matching file of folder `a` against the same name in `b`.
|
|
59
|
+
`FrameComparison.rows` are `FrameRow(name, md5_a, md5_b)` (`md5_b` is None when `b` lacks the file), with `status` of `same`,
|
|
60
|
+
`different` or `missing`; `only_in_b` lists the frames `a` lacks. `lines()` are the report lines, one per frame and then the
|
|
61
|
+
summary `N frames in A: x identical, y different, z missing in B`.
|
|
62
|
+
|
|
63
|
+
### Audio
|
|
64
|
+
|
|
65
|
+
`stats_for_file(path, silence_db=-66.2)` reads a 16-bit PCM WAV and returns a dict:
|
|
66
|
+
|
|
67
|
+
| Key | Meaning |
|
|
68
|
+
| --- | --- |
|
|
69
|
+
| `file`, `sample_rate`, `channels`, `frames`, `seconds` | the format and length (seconds rounded to 2 places) |
|
|
70
|
+
| `peak`, `peak_dbfs`, `rms_dbfs` | the largest absolute sample (32768 for a full-scale negative), and both in dBFS (`-Infinity` for digital silence) |
|
|
71
|
+
| `full_scale_samples`, `total_samples`, `full_scale_percent` | samples at 32767 or beyond, out of all samples of all channels |
|
|
72
|
+
| `silence_threshold_dbfs` | the threshold used: `silence_db` |
|
|
73
|
+
| `one_second_windows`, `silent_one_second_windows` | whole seconds, and those whose largest sample (any channel) is under the threshold |
|
|
74
|
+
| `silent_stretches_1s_or_more` | `[{"start_s", "length_s"}]`, found at 100 ms resolution with the same test |
|
|
75
|
+
| `longest_silence_s` | the longest stretch found, of any length |
|
|
76
|
+
| `header_repaired` | present and true only when the header's data length did not fit the file |
|
|
77
|
+
|
|
78
|
+
The header of a file from a run that was killed is never brought up to date: the data length reads 0, or more than the file
|
|
79
|
+
holds. The data then runs to the end of the file, so what was written is still measured (and `header_repaired` says so). A
|
|
80
|
+
half frame at the end is dropped. Only 16-bit PCM is handled; anything else raises `WavError`. The JSON uses Python's
|
|
81
|
+
`-Infinity` for digital silence, which Python reads back and strict JSON parsers do not.
|
|
82
|
+
|
|
83
|
+
### Press files
|
|
84
|
+
|
|
85
|
+
A press file is plain text, one press a line: `frame:button[+button...][:frames]`. The press starts when the game's frame counter
|
|
86
|
+
reaches `frame` and is held for `frames` frames (the default is yours: 4 for `drive`, 6 for `presses2script`). A `#` starts a
|
|
87
|
+
comment, and a line that begins with `p2 ` is left out (it belongs to a second pad).
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
# open the menu, then confirm
|
|
91
|
+
300:start
|
|
92
|
+
360:cross+up:12
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`parse_presses(text, default_hold=4, skip_prefixes=("p2 ",))` gives `Press(frame, buttons, hold)` records sorted by frame, and
|
|
96
|
+
names the line of anything it cannot read. `pad_word(buttons)` is the 16-bit pad word with those buttons pressed (all bits
|
|
97
|
+
set, each pressed button's bit cleared: the original console's pad protocol), from the table `PS1_BITS`.
|
|
98
|
+
`to_script(presses, total, shot_every=0, shot_ranges=(), prefix="f", turbo=False)` writes the lines of an emulator build's script for the same run:
|
|
99
|
+
held buttons as `count Button+Button`, idle stretches as `wait count`, `shot <prefix><frame>` before the input of that frame, then `quit`. The
|
|
100
|
+
button spellings are in `SCRIPT_BUTTON_NAMES`. Only pad 1 is scripted.
|
|
101
|
+
|
|
102
|
+
### The debug socket
|
|
103
|
+
|
|
104
|
+
Some recompiled ports built with their debug tools take commands over a local TCP socket: one JSON line in (`{"cmd": ...}`),
|
|
105
|
+
one JSON line out, then the server closes the connection. `DebugClient(host, port)` opens a new connection for every call
|
|
106
|
+
and tries again (5 times, 50 ms apart) when none answers, then raises `DebugError`, which is also a `ConnectionError`. The
|
|
107
|
+
commands are the port's own; `frame`, `press`, `clear_input`, `screenshot` and `quit` are the ones the drive loop uses.
|
|
108
|
+
|
|
109
|
+
| Name | What it does |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| `DebugClient(host="127.0.0.1", port=4370, timeout=30, wait=True)` | Waits up to `timeout` seconds for the server to listen. `.call(cmd, **fields)`, `.frame()`. |
|
|
112
|
+
| `run_presses(client, presses, until, shot_every=300, out_dir=None, quit_at_end=False)` | Presses each button when the frame counter reaches its frame, holds it, releases it a frame after, and saves `shot_NNNNNN.png` every `shot_every` frames, until `until`. Frame numbers are open loop (a poll or two of jitter). |
|
|
113
|
+
| `wait_frame(get_frame, target, timeout=90, alive=None)` | Polls until the counter reaches `target`. `alive()` returning False stops the wait with `DebugError` at once instead of at the timeout. |
|
|
114
|
+
| `wait_ready(ping, timeout=30, alive=None)` | Calls `ping()` until it stops raising `OSError` or `TimeoutError`. `alive()` is asked before each ping, so a program that is already gone is not pinged. |
|
|
115
|
+
|
|
116
|
+
`wait_frame` and `wait_ready` take plain callables, so a session that has its own way to talk to the port (a request that
|
|
117
|
+
carries an identity token, say) uses them with its own `command`.
|
|
118
|
+
|
|
119
|
+
## Not here, on purpose
|
|
120
|
+
|
|
121
|
+
Other packages in this repository, or in py-kit, already do these; use them rather than a copy.
|
|
122
|
+
|
|
123
|
+
- Finding a program's window on macOS and capturing it: [`mac-window-tools`](../mac-window-tools).
|
|
124
|
+
- Contact sheets from many frames: `uppercut-contact-sheet` in py-kit (it needs Pillow; `PIL` reads PPM).
|
|
125
|
+
- Controller replay files and capture scripts for a port's own replay format: [`pad-replay`](../pad-replay) and
|
|
126
|
+
[`uppercut-pad-replay-tools`](../uppercut-pad-replay-tools). `to_script` writes the same `wait` / `shot` / `quit` lines its
|
|
127
|
+
`CaptureScript` does, with held buttons added.
|
|
128
|
+
- Reading a PNG back and checking it: `validate_png` in [`uppercut-mgba-smoke`](../uppercut-mgba-smoke).
|
|
129
|
+
|
|
130
|
+
## What runs where
|
|
131
|
+
|
|
132
|
+
Everything runs on Linux, macOS and Windows with Python 3.10 or newer, with no display and no hardware. The tests start a fake
|
|
133
|
+
debug server on a local port (127.0.0.1, any free port) and write files under a temporary folder.
|
|
134
|
+
|
|
135
|
+
Tested at 1.0.0: `uv run pytest` passes (70 tests) on Windows 11 Pro (10.0.26300, Python 3.14.0) and on macOS 27.0
|
|
136
|
+
(Apple M6, Python 3.13.18). The Windows run found one test that split a screenshot path on `/` and failed on `\`; the test now uses
|
|
137
|
+
`os.path.basename`.
|
|
138
|
+
|
|
139
|
+
```sh
|
|
140
|
+
uv sync --all-packages && uv run pytest
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Licence
|
|
144
|
+
|
|
145
|
+
MIT, see `LICENSE`.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "uppercut-port-proof"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Helpers for the proof run of a recompiled game port: PPM frame dumps to PNG, frame MD5 comparison, WAV statistics, press scripts and a debug-socket driver."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{ name = "Uppercut Labs", email = "packages@uppercut-labs.com" }]
|
|
13
|
+
keywords = ["recompilation", "game-port", "ppm", "png", "wav", "frame-dump", "proof-run", "debug-socket"]
|
|
14
|
+
dependencies = []
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 5 - Production/Stable",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Topic :: Games/Entertainment",
|
|
25
|
+
"Typing :: Typed",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
uppercut-port-proof = "uppercut_port_proof.cli:main"
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Source = "https://github.com/uppercut-labs/game-kit/tree/main/packages/uppercut-port-proof"
|
|
33
|
+
|
|
34
|
+
[tool.hatch.build.targets.wheel]
|
|
35
|
+
packages = ["src/uppercut_port_proof"]
|
|
36
|
+
|
|
37
|
+
[tool.hatch.build.targets.sdist]
|
|
38
|
+
include = ["src", "tests", "README.md"]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Helpers for the proof run of a recompiled game port."""
|
|
2
|
+
|
|
3
|
+
__version__ = "1.0.0"
|
|
4
|
+
|
|
5
|
+
from .debug import DebugClient, run_presses, wait_frame, wait_ready # noqa: E402
|
|
6
|
+
from .errors import DebugError, PpmError, PressError, ProofError, WavError # noqa: E402
|
|
7
|
+
from .frames import FrameComparison, FrameRow, compare_frame_dirs, write_report # noqa: E402
|
|
8
|
+
from .png import encode_png, picture_to_png, ppm_to_png, write_png # noqa: E402
|
|
9
|
+
from .ppm import Ppm, crop, encode_ppm, enlarge, md5_of, parse_ppm, read_ppm, write_ppm # noqa: E402
|
|
10
|
+
from .presses import SCRIPT_BUTTON_NAMES, PS1_BITS, Press, pad_word, parse_presses, read_presses, to_script # noqa: E402
|
|
11
|
+
from .wav import DEFAULT_SILENCE_DB, Wav, format_stats, parse_wav, read_wav, stats_for_file, wav_stats # noqa: E402
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"DEFAULT_SILENCE_DB", "DebugClient", "DebugError", "FrameComparison", "FrameRow", "SCRIPT_BUTTON_NAMES", "PS1_BITS", "Ppm", "PpmError",
|
|
15
|
+
"Press", "PressError", "ProofError", "Wav", "WavError", "compare_frame_dirs", "crop", "encode_png", "encode_ppm", "enlarge",
|
|
16
|
+
"format_stats", "md5_of", "pad_word", "parse_ppm", "parse_presses", "parse_wav", "picture_to_png", "ppm_to_png", "read_ppm",
|
|
17
|
+
"read_presses", "read_wav", "run_presses", "stats_for_file", "to_script", "wait_frame", "wait_ready", "wav_stats", "write_png",
|
|
18
|
+
"write_ppm", "write_report",
|
|
19
|
+
]
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"""`uppercut-port-proof`: the helpers as commands. Every command exits 0 on success and 2 on a file or argument it cannot use."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import List, Optional, Sequence
|
|
10
|
+
|
|
11
|
+
from . import __version__
|
|
12
|
+
from .debug import DebugClient, run_presses
|
|
13
|
+
from .errors import ProofError
|
|
14
|
+
from .frames import compare_frame_dirs, write_report
|
|
15
|
+
from .png import ppm_to_png
|
|
16
|
+
from .ppm import crop, enlarge, md5_of, read_ppm
|
|
17
|
+
from .png import picture_to_png
|
|
18
|
+
from .presses import read_presses, to_script
|
|
19
|
+
from .wav import DEFAULT_SILENCE_DB, format_stats, stats_for_file
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _wav_stats(args: argparse.Namespace) -> int:
|
|
23
|
+
results = [stats_for_file(p, silence_db=args.silence_db) for p in args.wav]
|
|
24
|
+
if args.json:
|
|
25
|
+
print(json.dumps(results[0] if len(results) == 1 else results, indent=1))
|
|
26
|
+
else:
|
|
27
|
+
for r in results:
|
|
28
|
+
print(format_stats(r))
|
|
29
|
+
return 0
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _md5_compare(args: argparse.Namespace) -> int:
|
|
33
|
+
result = compare_frame_dirs(args.a, args.b)
|
|
34
|
+
print(result.summary())
|
|
35
|
+
if args.out:
|
|
36
|
+
write_report(result, args.out)
|
|
37
|
+
return 0 if result.identical or not args.strict else 1
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _ppm2png(args: argparse.Namespace) -> int:
|
|
41
|
+
out = Path(args.out_dir)
|
|
42
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
43
|
+
for f in args.frames:
|
|
44
|
+
picture = ppm_to_png(f, out / (Path(f).stem + ".png"), compress=not args.stored)
|
|
45
|
+
print(md5_of(f), Path(f).stem, picture.width, picture.height)
|
|
46
|
+
return 0
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _ppm_crop(args: argparse.Namespace) -> int:
|
|
50
|
+
picture = enlarge(crop(read_ppm(args.src), args.x0, args.y0, args.x1, args.y1), args.scale)
|
|
51
|
+
Path(args.out).write_bytes(picture_to_png(picture, compress=not args.stored))
|
|
52
|
+
return 0
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _presses2script(args: argparse.Namespace) -> int:
|
|
56
|
+
presses = read_presses(args.presses, default_hold=6)
|
|
57
|
+
lines = to_script(
|
|
58
|
+
presses, args.total, shot_every=args.shot_every, shot_ranges=[tuple(r) for r in args.shot_range], prefix=args.prefix, turbo=args.turbo
|
|
59
|
+
)
|
|
60
|
+
Path(args.out).write_text("\n".join(lines) + "\n")
|
|
61
|
+
shots = sum(1 for line in lines if line.startswith("shot "))
|
|
62
|
+
print("wrote %s: %d lines, %d shots" % (args.out, len(lines), shots))
|
|
63
|
+
return 0
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _drive(args: argparse.Namespace) -> int:
|
|
67
|
+
presses = read_presses(args.presses, default_hold=4)
|
|
68
|
+
client = DebugClient(args.host, args.port)
|
|
69
|
+
run_presses(client, presses, args.until, shot_every=args.shot_every, out_dir=Path(args.out), quit_at_end=args.quit)
|
|
70
|
+
return 0
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
74
|
+
p = argparse.ArgumentParser(prog="uppercut-port-proof", description=__doc__)
|
|
75
|
+
p.add_argument("--version", action="version", version=f"uppercut-port-proof {__version__}")
|
|
76
|
+
sub = p.add_subparsers(dest="command", required=True)
|
|
77
|
+
|
|
78
|
+
s = sub.add_parser("wav-stats", help="duration, peak, RMS, full-scale samples and silent stretches of 16-bit PCM WAV files")
|
|
79
|
+
s.add_argument("wav", nargs="+")
|
|
80
|
+
s.add_argument("--silence-db", type=float, default=DEFAULT_SILENCE_DB, help="a one-second window is silent under this peak (default %(default)s dBFS)")
|
|
81
|
+
s.add_argument("--json", action="store_true", help="print JSON (a list when several files are given)")
|
|
82
|
+
s.set_defaults(run=_wav_stats)
|
|
83
|
+
|
|
84
|
+
s = sub.add_parser("md5-compare", help="compare the frame_*.ppm dumps of two run folders by MD5")
|
|
85
|
+
s.add_argument("a")
|
|
86
|
+
s.add_argument("b")
|
|
87
|
+
s.add_argument("out", nargs="?", help="write one line per frame here")
|
|
88
|
+
s.add_argument("--strict", action="store_true", help="exit 1 unless every frame is the same")
|
|
89
|
+
s.set_defaults(run=_md5_compare)
|
|
90
|
+
|
|
91
|
+
s = sub.add_parser("ppm2png", help="convert PPM frames to PNG files in a folder and print each frame's MD5")
|
|
92
|
+
s.add_argument("out_dir")
|
|
93
|
+
s.add_argument("frames", nargs="+")
|
|
94
|
+
s.add_argument("--stored", action="store_true", help="write stored (uncompressed) blocks, as the C package minimal-png does")
|
|
95
|
+
s.set_defaults(run=_ppm2png)
|
|
96
|
+
|
|
97
|
+
s = sub.add_parser("ppm-crop", help="crop a region of a PPM frame, enlarge it by a whole number and write a PNG")
|
|
98
|
+
s.add_argument("src")
|
|
99
|
+
s.add_argument("out")
|
|
100
|
+
for name in ("x0", "y0", "x1", "y1"):
|
|
101
|
+
s.add_argument(name, type=int)
|
|
102
|
+
s.add_argument("scale", nargs="?", type=int, default=2)
|
|
103
|
+
s.add_argument("--stored", action="store_true")
|
|
104
|
+
s.set_defaults(run=_ppm_crop)
|
|
105
|
+
|
|
106
|
+
s = sub.add_parser("presses2script", help="turn a press file into a script for the PS2 emulator build, with screenshots at chosen frames")
|
|
107
|
+
s.add_argument("presses")
|
|
108
|
+
s.add_argument("total", type=int)
|
|
109
|
+
s.add_argument("out")
|
|
110
|
+
s.add_argument("--shot-every", type=int, default=0)
|
|
111
|
+
s.add_argument("--shot-range", type=int, nargs=2, action="append", default=[], metavar=("A", "B"))
|
|
112
|
+
s.add_argument("--prefix", default="f")
|
|
113
|
+
s.add_argument("--turbo", action="store_true")
|
|
114
|
+
s.set_defaults(run=_presses2script)
|
|
115
|
+
|
|
116
|
+
s = sub.add_parser("drive", help="press buttons at frame numbers and take screenshots over a port's debug socket")
|
|
117
|
+
s.add_argument("--presses", required=True)
|
|
118
|
+
s.add_argument("--until", type=int, required=True, help="stop after this frame")
|
|
119
|
+
s.add_argument("--shot-every", type=int, default=300)
|
|
120
|
+
s.add_argument("--out", required=True)
|
|
121
|
+
s.add_argument("--port", type=int, default=4370)
|
|
122
|
+
s.add_argument("--host", default="127.0.0.1")
|
|
123
|
+
s.add_argument("--quit", action="store_true", help="send quit to the program at the end")
|
|
124
|
+
s.set_defaults(run=_drive)
|
|
125
|
+
return p
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def main(argv: Optional[Sequence[str]] = None) -> int:
|
|
129
|
+
args = build_parser().parse_args(argv)
|
|
130
|
+
try:
|
|
131
|
+
return int(args.run(args))
|
|
132
|
+
except (ProofError, OSError) as error:
|
|
133
|
+
print(f"uppercut-port-proof {args.command}: {error}", file=sys.stderr)
|
|
134
|
+
return 2
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
if __name__ == "__main__":
|
|
138
|
+
sys.exit(main())
|