Skip to content

Add Portal Stories: Mel (experimental, macOS) - #52

Open
Zertrox123 wants to merge 8 commits into
kanapitsas:mainfrom
Zertrox123:feat/portal-stories-mel
Open

Zertrox123 wants to merge 8 commits into
kanapitsas:mainfrom
Zertrox123:feat/portal-stories-mel

Conversation

@Zertrox123

Copy link
Copy Markdown
Contributor

Summary

Adds experimental macOS support for Portal Stories: Mel (Steam app 317400, mod portal_stories, Linux engine depot 317404) alongside Portal 2, Half-Life 2 and Portal.

Note: the first two commits (stat inode truncation and LazyInsns._resync) are also proposed independently in #51 so they can be reviewed/merged separately if preferred.

Key changes

  • ELF / gameprep (tools/elfprep.py, tools/gameprep.py):
    • Accept STB_GNU_UNIQUE (10) exports for libstdc++.so.6 template statics.
    • Collect data_ptrs across all executable code sections (excluding .init, .fini, .plt*), picking up Bink function tables in BINK* sections of engine.so and valve_avi.so.
    • Keep base 0 for non-PIE ET_EXEC main executables (Mel's portal2_linux is linked at 0x08048000).
    • Support game_lib_dirs (portal_stories/bin, relative to game_root) and system_libs (libstdc++.so.6, libgcc_s.so.1 from system_lib_dir).
  • Steam bridge (tools/gen_steam.py, runtime/steam.c, regenerated steam_generated.c and steam_stub_generated.c):
    • Extract SDK 1.40 vtable slots and stack word counts from Mel's i386 libsteam_api.so (disassemble_i386), cross-checked against Steamworks.NET 20.1.0 signatures, and call the host vtable slots directly.
    • Handle SteamUtils009::GetCSERIPPort (slot 7) and SteamClient017::GetISteamUnifiedMessages (slot 24) in virtual_bridge.
  • D3D9 bridge (runtime/d3d9.c):
    • Detect 12-byte D3DVERTEXELEMENT9 arrays from Mel's 2021 DXVK-native headers (32-bit WORD), matching Stream == 0xff && Offset == 0 && Type == D3DDECLTYPE_UNUSED unambiguously so even element counts (12 * 2k == 8 * 3k) are never misread as 8-byte arrays, and convert to/from 8-byte elements in d3d9_decl and GetDeclaration.
  • Runtime shims (runtime/fonts.c, runtime/fortify.c, runtime/wchar.c, runtime/libc.c):
    • Add FT_Bitmap_New, FT_Bitmap_Convert, FT_Bitmap_Done, __recv_chk, __wcscpy_chk, __wcsncat_chk, _IO_getc, _IO_putc.
  • Integration & docs (tools/games.py, tools/make_app.py, config/mel-roots.json, setup.sh, uninstall.sh, installer/Installer.swift, docs/mel.md, docs/steam.md, README.md):
    • mel game profile (ios=False), 100 Bink indirect call roots in config/mel-roots.json, .ico to .icns conversion via sips in make_app.py, SHA-256-verified Debian bullseye i386 libstdc++6/libgcc-s1 download in setup.sh, and documentation.

Testing

  • Automated: make check (223 Python tests, 265 C checks in abi-smoke) and ./build/abi-smoke-ubsan pass on macOS Apple Silicon, including new unit tests for 64-bit inode stat, LazyInsns._resync, gameprep (ET_EXEC, game_lib_dirs, system_libs), disassemble_i386, 12-byte d3d9_decl (2 elements + end at offset 24), FT_Bitmap_Convert, and setup.sh/uninstall.sh/games.py.
  • In-game (--release on Apple Silicon):
    • Main menu (Bink background video, localized VGUI UI, Steam logon, Miles audio) on Metal.
    • Loaded and ran sp_a1_tramride, sp_a2_underbounce, sp_a3_paint_fling, sp_a4_overgrown on Metal and sp_a2_underbounce on OpenGL (P2_RENDERER=opengl) with no traps or crashes.
    • Verified save and load round-trip in sp_a2_underbounce and inspected in-game screenshots of the menu, chamber and reloaded save.
    • Built ~/Applications/Portal Stories Mel Natif.app with converted .icns icon.

Known limits

  • macOS only (--ios is rejected for mel).
  • Full playthrough, Steam achievements, and extended performance measurements have not been tested yet.
  • Additional indirect call roots may still be discovered during a full playthrough.

Sealed macOS system volumes (such as /System/Library/Fonts) commonly have
64-bit inode numbers. Returning EOVERFLOW from 32-bit stat broke guests that
ignore stat errors when probing system fonts; truncating st_ino to 32 bits
matches how games use it as an opaque identifier.
… out of step

Non-PIC code sections with inline jump tables (such as Bink in Portal Stories:
Mel's valve_avi.so and engine.so) can push objdump's linear sweep out of step
across the next function entry. Add LazyInsns._resync to decode a short
image-relative window from a missed entry while keeping already-decoded
instructions.
…ameprep: non-PIE, game_lib_dirs and system_libs

- Accept STB_GNU_UNIQUE (10) bindings in Elf.exports() so libstdc++.so.6
  exports template statics such as std::string's empty representation.
- Collect data_ptrs across all executable code sections (excluding .init,
  .fini and .plt*), not only .text, so Bink function tables are picked up.
- Keep base 0 for non-PIE ET_EXEC main executables in gameprep.
- Support game_lib_dirs (relative to game_root) and system_libs (resolved from
  system_lib_dir) in gameprep.
Extract vtable slots and stack word counts from the i386 flat functions of an
older libsteam_api.so (SDK 1.40, for which no arm64 dylib exists), cross-check
against Steamworks.NET 20.1.0 signatures, and call the host interface vtable
slots directly. Handle SteamUtils009::GetCSERIPPort and
SteamClient017::GetISteamUnifiedMessages in virtual_bridge, and regenerate
steam_generated.c and steam_stub_generated.c.
Portal Stories: Mel's 2021 Linux shaderapivk.so was compiled with 32-bit WORD
in its D3D9 headers, widening D3DVERTEXELEMENT9 to 12 bytes (u32 Stream and
Offset). Detect the 8-byte vs 12-byte D3DDECL_END sentinel unambiguously using
Stream, Offset and D3DDECLTYPE_UNUSED (avoiding collisions when 12*2k == 8*3k),
convert to 8-byte elements for libd3d9metal, and convert back in
GetDeclaration. Ignore SetGammaRamp when the 32-bit-WORD ramp layout is active.
…_getc/putc shims

Bridge FreeType FT_Bitmap_New, FT_Bitmap_Convert and FT_Bitmap_Done for
vguimatsurface bitmap font conversion, add fortified __recv_chk, __wcscpy_chk
and __wcsncat_chk, and alias glibc _IO_getc/_IO_putc to fgetc/fputc.
Add the mel game profile (Steam app 317400, Linux depot 317404, macOS only),
Bink indirect call roots (config/mel-roots.json), .ico to .icns conversion in
make_app.py, setup.sh support (including SHA-256-verified Debian bullseye i386
libstdc++.so.6 and libgcc_s.so.1 extraction), uninstall.sh, Installer.swift,
tests and docs/mel.md.
- d3d9_decl: probe the active 8- or 12-byte format first so 12-byte
  declarations near a page end do not scan 512 bytes ahead in 8-byte steps.
- __wcsncat_chk: check null src and 32-bit guest address wraparound before
  scanning, matching wlen.
- gameprep: expanduser() on system_lib_dir and report a missing system library
  explicitly.
- make_app: do not copy a raw .ico file as portal2.icns when sips is absent.
- setup.sh: resolve absolute .deb paths before cd into temp dir and download
  via a .part file before SHA-256 verification.
@kanapitsas

Copy link
Copy Markdown
Owner

Thanks for the PR! I'm currently testing this.

@Zertrox123

Copy link
Copy Markdown
Contributor Author

No problem ! We can speak in French if you want :D

@kanapitsas

Copy link
Copy Markdown
Owner

Merci pour ce travail ! J'ai pu compiler Mel depuis la branche sur un Mac Apple Silicon et jouer quelques minutes, aucun problème à signaler en jeu.

Quelques points à corriger avant la fusion :

  1. download_depot ne suffit pas pour installer Mel. Le dépôt 317401 ne contient que portal_stories/, alors que le gameinfo.txt de Mel cherche aussi portal2, portal2_dlc2 et platform. Du coup, setup.sh s'arrête parce que platform/ manque. Pour le lancer, j'ai ajouté des liens vers ces trois dossiers de mon installation de Portal 2. Est-ce que setup.sh pourrait plutôt les prendre dans le dossier de Portal 2 quand ils manquent (il sait déjà le trouver) ?
  2. Paquets Debian. Ok pour les télécharger : le script vérifie leur SHA-256 avant de les extraire, et ne les récupère que si Mel est choisi et que les bibliothèques manquent. Il faut que ça reste réservé à Mel. Ces versions finiront par quitter deb.debian.org, et l'adresse cassera. Est-ce qu'on pourrait prévoir une adresse datée sur snapshot.debian.org, au moins en secours ?
  3. Régénérer le pont Steam. gen_steam.py exige maintenant le libsteam_api.so de Mel, même pour régénérer les interfaces de Portal 2. Il faut qu'il marche sans Mel. J'aimerais plutôt qu'on enregistre dans un petit fichier JSON du dépôt ce qui est extrait de la bibliothèque (pour chaque méthode, sa position dans la table et le nombre de mots de 4 octets sur la pile), et qu'on lise ce fichier quand la bibliothèque n'est pas là.

Une question : maintenant que elfprep.py parcourt les sections BINK* et que le lifter redécode les entrées manquées, peut-on retirer une partie des 100 adresses ajoutées à la main dans config/mel-roots.json ? Dans #45, on a retiré une liste du même genre pour Half-Life 2 parce que le lifter retrouvait ces adresses tout seul ; une liste écrite à la main ne suit pas les mises à jour des binaires.

@kanapitsas kanapitsas left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changements demandés pour les trois points du commentaire ci-dessus, surtout le premier (l'installation par download_depot s'arrête faute de platform/). La question sur config/mel-roots.json n'est pas bloquante.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants