BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

The Flash layer: menus and their text

BF22142Heroes

The main menu, the server browser, the options, the loading screen and the end-of-round screens are Flash movies (SWF 7, ActionScript 2). The original plays them with a build of the open-source gameswf player; the port plays the same files with Ruffle and a patch set that makes Ruffle behave like that build. The in-game HUD is not Flash: it is the engine's own HUD nodes, set up by script (the Heroes HUD).

A menu movie and the three parts that run it the movie: mainMenu.swf (SWF 7, ActionScript 2) draws its own art; asks for everything: dice.bf2.Logic.getFrameLabel(), dice.General.getListEntries() .. the player SwiffPlayer.dll (gameswf) ours: Ruffle with a patch set runs the ActionScript; 27 native objects, 855 registrations the host SwiffHost in BF2.exe ours: bf2-ui screen size, cursors, profiles, settings, the server lists the renderer SwiffRender in RendDX9.dll ours: bf2-ui swiff_text, bf2-font textures; text in the engine's bitmap fonts, measured as it does The direction is pull: the host never calls into the movie, apart from input events. The movie is stretched to the screen: independent x and y factors, screen size over movie size. SwiffPlayer.dll!0x10006100 (loadMovie), 0x10086080 (format_engine_text); BF2.exe!0x004a2fb0 (activate)
The movie on top asks; the three parts below answer. Left to right: who runs the script, who knows the game, who draws.

The three parts#

All three are singletons fetched by name from the class manager:

NameWhereRole
SwiffPlayerSwiffPlayer.dllthe gameswf player and every dice.* native object
SwiffHostBF2.exe (dice.hfe.SwiffHost)the engine's services for the menus: the screen size, cursors, the wait state, some 190 methods
SwiffRenderRendDX9.dllgameswf's render back end: textures and the engine's text

A front-end logic state owns a movie: the main menu (mainMenu.swf), between rounds and end of round (endOfRound.swf), the loading screen (loadGame.swf). Its activate (BF2.exe!0x004a2fb0) gets the three parts, hands the host and the renderer to the player, loads the movie and sets the scale: the screen's width over the movie's width, and the same for the height, independently. The movie is stretched to the screen.

SwiffPlayer::loadMovie (SwiffPlayer.dll!0x10006100) creates the movie, asks the renderer for each external bitmap, and then creates the native objects under _global.dice before the first action runs. The movies hold stub classes of the same names from their authoring; whichever runs first, the natives win.

The native API: the movie pulls#

The direction is one way. The movie polls the host; the host never calls into the movie, apart from input events. What the host wants the menu to act on is a getter the menu asks every frame: the frame label to show, the reason it was activated, the number of waiting messages, a list's revision.

27 objects, 855 registrations:

ObjectMethodsFor
dice.General8lists (one handler, getListEntries, is 61 KB of code)
dice.MessageHandler9the message queue: pop-ups, errors
dice.bf2.Logic40the game's state, the local profile
dice.bf2.Multiplay87the server browser, creating a server
dice.bf2.Profile, PlayerStats52, 34profiles and their statistics
dice.bf2.ServerSettings98the settings of a server to create
dice.bf2.VideoSettings, AudioSettings, ControlSettings, GeneralSettings47, 43, 22, 60the options screens
dice.bf2.HapticSettings265a force feedback device; the largest object, used by one screen
dice.bf2.EndOfRound26the end-of-round record
dice.bf2.Sound, Locale21, 2menu sounds and the voice test; localized strings

576 of the natives are referenced by the movies' ActionScript. The table of every native, with its handler's address and what it returns, is in the repository (re/data/flash_api.tsv), extracted from the library and cross-checked against the decompiled ActionScript of all the movies.

The player we embed#

The port's player is Ruffle at a fixed revision with a patch series (crates/bf2-ui/vendor/ruffle/patches/), built as a crate of its own (crates/bf2-ui/player) so the main workspace never builds Ruffle. With a gameswf host installed (PlayerBuilder::with_gameswf_host), Ruffle switches to gameswf's behaviour:

  • the native objects are created under _global before the first action, overwriting members of the same name;
  • names are case-insensitive for every SWF version;
  • number and string conversions and a list of opcodes follow gameswf's interpreter, not Flash Player's;
  • MovieClip.loadMovie(url) asks the host for an image and shows it as a bitmap;
  • values refer to clips directly, not by path, as gameswf's do: a reference to a removed clip stays what it was;
  • a function called from another inherits its caller's register file, as in this gameswf build.

The menus were written against this build's behaviour, so each of these differences decides whether some screen works.

Numbers as the menu prints them#

gameswf prints numbers with the C runtime of its day and parses them with its atof; the menus' arithmetic runs on the x87 at 24 bits. Each is held bit for bit:

WhatChecked on
number printing and atof against the original C runtime10,034 and 4,030 inputs
menu arithmetic at 24-bit precision against the handlers' own instructions4,831 cases, among them every double-rounding case of 29,913
printf("%.nf") as the decimals setter uses it6,212 cases; 58 exact 18-digit ties differ and are listed
the native handlers' side410 natives, by running the original handlers under emulation

Text: laid out and measured as the engine does#

A text field whose font name is one of the engine's is not drawn with the movie's glyphs. It is drawn with the engine's bitmap fonts, looked up as <name>_<size> with the size round(height x 0.05): StandardText_13, DynamicText_11, StandardTextBold_15. A name without a font draws nothing.

Layout (format_engine_text SwiffPlayer.dll!0x10086080), run at every change of the text, the width, the height or the colour:

  1. Characters are appended one at a time to the current line.
  2. A carriage return or a line feed ends the line.
  3. With word wrap, when the position past the last character reaches the field's width less its right margin, the line is cut at the last space, or before the new character when the line has none.
  4. A line that would start below the field's height ends the text.
  5. A field whose text is taller than itself is grown (grow_to_fit 0x10084de0).

Measuring is the renderer's. FontString (RendDX9.dll!0x100a5770) stores the text and an alignment offset (right: the width less the text's width; centre: half of that). When a text is longer than its field it is cut per character: characters are kept while their right edge is within the field's width over the font's scale. Glyph quads go at floor(x + offset + 0.5) + fontScale x (box - 0.5), and the text's position on the screen is round(t x 0.05 x scale + 0.5) with the scale the screen over the movie.

So whether a server's name fits its column, where a line breaks in the briefing, and which letter of a long player name is the last one shown are all decided by the engine font's advance widths and this arithmetic, at 24 bits. The port's layout (crates/bf2-ui/src/swiff_text.rs, positions and widths through bf2_font::layout) is unit-tested against the decompiled code; it has not been compared pixel for pixel on screen with the original.

Fields without an engine font use the movie's own font; a font without glyphs is not substituted, so such text draws nothing in the original.

2142 and Heroes#

Battlefield 2142 and Heroes ship their movies encrypted (.dfm); the port reads them as the games do (Flash menus as files).

How it is tested#

  • The menu's data against the original (crates/bf2-ui/tests): the end-of-round data against the original's function (eor_retail.rs), the award and kit catalogues against the original run on the shipped files (catalogue_retail.rs), number-to-text conversions against the original's C runtime (gameswf_oracle.rs).
  • The native API's shape (crates/bf2-ui/src/api.rs): the functions the menu movies call, per game.
  • Every shipped font file is read (crates/bf2-font/tests/retail.rs).
  • Played: the HUD and menu sweeps play the screens in scenarios with the real client (scenarios).

The Flash player itself is a third-party player with the project's patches; its drawing is not compared with the original player's, and two text-width differences are filed (the workbench).

In the code#

WhatWhere
the host: the native objects' behaviour, lists, settingscrates/bf2-ui
the playercrates/bf2-ui/player (a workspace of its own), the patch series under crates/bf2-ui/vendor/ruffle
engine textcrates/bf2-ui/src/swiff_text.rs, crates/bf2-font
the native table and its semanticsre/data/flash_api.tsv, re/data/flash_natives.tsv

The HUD in a round is not Flash: the HUD of Battlefield 2, of 2142, of Heroes.

Notes: notes/subsystems/menu-flash.md, notes/tracks/ui.md.

Source: docs/dev/ui/flash.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.