Add standalone BMH and FLTD to OBW converter (by Codex)

This commit is contained in:
Codex committed 2026-10-17 22:21:17 +02:00
1 parent 310e3e2464
commit 9bff07828a
23 files changed
+1928 -4

No files matched your search

+1
View File
@@ -316,6 +316,7 @@ if(MSVC)
endif()
add_subdirectory(src/ui)
add_subdirectory(src/import)
add_executable(driver_barrier_tests tests/driver_barrier_tests.cpp)
target_link_libraries(driver_barrier_tests PRIVATE bmhcore)
add_test(NAME driver_barrier_tests COMMAND driver_barrier_tests)
+2
View File
@@ -31,6 +31,8 @@ The graphical client and console share career management, matches, transfers, co
Version 0.1 establishes the playable engine and development interfaces. Later milestones introduce an open data format and typed model, configurable rules, a scalable interface and independently distributable content. These are goals rather than delivery dates; the [roadmap](docs/ROADMAP.md) describes their dependencies.
The source tree also provides an initial standalone [`openbmh_import` tool](docs/formats/obw/import.md) to convert BMH and Football Limited installation data into the planned OBW format. These intermediate worlds are for conversion/development only; the game will gain OBW support in a later version.
## Build from source
The following recipes target v0.1.2. They require Git, CMake 3.24 or newer and a C++20 compiler. The first configure fetches pinned open-source dependencies. Native build and package results are documented for each release; these instructions do not claim an untested platform result.
+1
View File
@@ -13,6 +13,7 @@ OpenBMH is a C++20 engine with graphical and console frontends. The implementati
| `src/cli` | Command language, structured protocol, console windows and multi-client seat routing |
| `src/audio` | Bank decoding, synthesis, playback and cues, without authority to change gameplay |
| `src/launch` | Locating user data, archive extraction, edition normalisation and file selection |
| `src/import` | Standalone legacy-to-OBW conversion; no game-state ownership or OBW engine integration |
`bmhcore` contains the format and game layers. `bmhui`, `openbmh_audio` and the application session provide reusable pieces above it. Frontends should translate actions and display results; they should not introduce independent transfer, calendar or financial rules.
+8 -3
View File
@@ -1,6 +1,6 @@
# OBW: OpenBMH world, save and pack format (concept, format version 1.0)
Status: **concept** for roadmap 0.2 (open data and typed state) with preparation for 0.4 (scalable interface, content packs). Nothing here is implemented yet; the legacy formats in this directory remain authoritative for the current engine.
Status: **concept with an initial standalone converter** for roadmap 0.2 (open data and typed state), with preparation for 0.4 (scalable interface, content packs). `openbmh_import` implements the guarded `legacy-database-v1` conversion profile described in [import.md](obw/import.md). Its intermediate files cannot yet start a career. The legacy formats remain authoritative for the current engine; an OBW engine adapter and save conversion are not implemented.
## 1. Goals and non-goals
@@ -112,6 +112,8 @@ Fields and directories whose name starts with `x-` belong to extensions (`x-matc
Each format version is published as JSON Schema (draft 2020-12) under `docs/formats/obw/schema/<major>.<minor>/`, one file per table. The schemas are the machine-readable specification and the basis of the validator (§10).
The initial schemas describe the `legacy-database-v1` intermediate conversion profile only; save, pack, theme and complete playable-world schemas are still planned. This profile uses `requires` to prevent a partial conversion being mistaken for a complete world. Optional fields and unknown extensions remain allowed.
## 4. Identifiers and names
- Every entity has a stable string id `<type>:<key>`, e.g. `nation:de`, `club:0042`, `player:01873`, `comp:de-1`, `stadium:0042`. Keys are opaque: they never encode a real name, so a world and a pack can be published independently.
@@ -161,7 +163,8 @@ Teams without a squad (`foreign`, or `national` in a world that does not simulat
|---|---|---|---|
| `id` | string | R | |
| `name` | object | R | `{"first": "...", "last": "...", "known_as": "..."}`, each a plain string; `known_as` optional |
| `born` | date | R | Age is derived from the in-game date |
| `born` | date | R unless `age_at_start` | Age is derived from the in-game date |
| `age_at_start` | object | Alternative to `born` | `{"years": 22, "on": "2026-07-01"}` when a legacy database gives an age but no exact birth date. Never invent a birthday. A consumer must explicitly support this precision. |
| `nationalities` | array | R | Nation ids, first entry is the sporting nationality (national-team eligibility) |
| `positions` | array | R | `gk`, `def`, `mid`, `fwd` in the classic set; finer positions in later attribute sets |
| `foot` | enum | O | `left`, `right`, `both` |
@@ -348,7 +351,7 @@ Edition detection is the one the launcher already performs (`src/launch/game_dat
### 8.2 Mapping per edition
Every edition has a mapping table in the importer (to be published with it as `docs/formats/obw/import.md`):
The first standalone converter and its coverage are documented in [the edition mapping](obw/import.md). The full intended mapping is:
| Legacy league | German editions | Football Limited | OBW |
|---|---|---|---|
@@ -363,6 +366,8 @@ Cups, European cups, the indoor tournament, the English FA Cup with its qualifyi
### 8.3 Fidelity
The initial `legacy-database-v1` converter preserves database seeds and source tables without starting a career. It does not yet satisfy the engine equivalence criteria below; those require the 0.2 adapter. Its manifest explicitly marks it non-playable, and its import report lists unresolved areas. Unmapped world data lives in `x-legacy/`; `state/x-legacy.json` is reserved for converted saves.
- A new career imported from an installation must produce the same first season in the engine as the legacy start (same fixtures, same participants), verified by the regression runs.
- A converted `.FBM` must continue with the same results as the legacy save for a fixed number of turns.
- Values the typed model does not yet understand go to `state/x-legacy.json` with their legacy name, so nothing is lost while the model grows area by area.
+1 -1
View File
@@ -19,7 +19,7 @@ Unless a page says otherwise, integer fields are little endian. Legacy strings u
| Audio containers | `S/2000` through `S/2003` | [SOUND](SOUND.md) | `src/audio/container.cpp`, `original.cpp` |
| Executable data | German LE and English X-32 images | [LE](LE.md), [executable layout](EXECUTABLE_LAYOUT.md) | `src/formats/le.cpp`, `src/launch/fltd.cpp` |
| IFF crest template | `WAPPEN.LBM` | [WAPPEN](WAPPEN.md) | Not loaded by OpenBMH |
| OpenBMH world, save and pack (concept) | `*.obw`, `*.obs`, `*.obp`, `*.obt` | [OBW](OBW.md) | Planned for 0.2 |
| OpenBMH world, save and pack | `*.obw`, `*.obs`, `*.obp`, `*.obt` | [OBW](OBW.md), [initial importer](obw/import.md) | `src/import` converts installation data; game integration/save support planned for 0.2 |
German and English editions can share a physical format while differing in text, league contents, executable layout and currency. Select the edition through the supplied installation; do not rename files or patch a save header to simulate another version. Original English V2.20 save import is a separate compatibility limitation from English installation support.
+143
View File
@@ -0,0 +1,143 @@
# Legacy installation importer (first version)
`openbmh_import` converts a locally supplied German BMH installation or English
Football Limited installation into an OBW ZIP, or an unpacked OBW directory.
It is a separate tool: neither game frontend nor the engine reads OBW yet.
Integration into the game belongs to version 0.2.
## Build and use
Configure the project as described in [BUILDING](../../BUILDING.md), then build
the additional target:
```sh
cmake --build build --config Release --target openbmh_import
./build/openbmh_import --data "path/to/game" --output "season.obw" \
--start-date 2026-07-01 --title "My imported season"
```
With a Visual Studio multi-configuration build, use
`build\Release\openbmh_import.exe`. On Linux and macOS, the executable is in
`build/openbmh_import`. To inspect or edit the JSON directly:
```sh
./build/openbmh_import --data "path/to/FLTD.EXE" --output "season-directory" \
--start-date 1995-07-01 --directory
```
`--data` accepts an installation directory, a parent containing exactly one
installation, or its `BMH.EXE`/`FLTD.EXE`. File discovery ignores case. An
installation containing both executables requires naming the executable.
Only the executable, master database and text database are required; absent
graphics, audio or desktop files do not prevent conversion. Source ZIP/7z
archives and individual `.FBM` saves are not input formats for this first tool.
The reference date is explicit because a master database records ages without
an unambiguous season year. It must be July 1, from 1900 through 9998. The tool
does not use the computer's current date. It refuses existing output paths,
including symlinks. Output is assembled in a sibling staging directory and
published after validation; the output parent must already exist.
Do not run competing writers against the same destination.
Runtime packages for 0.1 retain their existing products. The source build has
the extra importer; `cmake --install build --component ImportTools` installs it
separately. Installing it does not enable OBW in the game.
## Conversion profile and coverage
The manifest declares format `1.0`, `kind: world`, provenance `import:bmh-de`
or `import:fltd`, and `content_license: local-import-only`. Crucially,
`requires: ["import:legacy-database-v1"]` and `x-import.playable: false`
identify an **intermediate conversion**, not a fully initialized playable world.
A reader which does not support this profile must refuse it. Future conversion
passes can complete its mappings without needing to reopen the original database.
Every core table uses the `schema`/`items` envelope, including `rules` and
`calendar`. The schemas under [schema/1.0](schema/1.0) describe this first
conversion profile; complete save/pack/theme schemas and engine playability
validation remain future work.
| Source | Exported data |
|---|---|
| `M3D.MAN` / `M3E.MAN` | Named clubs, national teams, nations, all 2,574 players, master squad assignments, stadium name pool |
| Player words and bonus bytes | Position, age, nationality including the high bonus bit, form/technique/condition, seven skills, bonus and derived display strength |
| Executable, through existing loaders | Nine league sizes/tiers/participants, original fixture pairings, promotion-ladder inputs, 366 source calendar words, training-camp prices/quality and credit factors |
| `TEXTD.BLK` / `TEXTE.BLK` | Local `de-original` / `en-original` gettext layer and an indexed text table with raw bytes |
| Unmapped master fields | Named hexadecimal tables, original name pools, original player words, badge/kit selectors, squad words and flags |
| Source master database | Exact file bytes in `x-legacy/source-database.json`, hexadecimal, for lossless later re-mapping |
`x-legacy/import-report.json` records warnings and outstanding mappings. Staff,
contracts and finance tables are empty: their initialized values are created
by career setup, rather than supplied as complete records by the master database.
Stadium names are exported without invented owners or capacities. Squad rows are
preserved as legacy rows, not claimed to be shirt numbers. A source player name
is kept whole in `name.last`; the importer does not guess first/last-name splits.
Exact birth dates are unknown. A player instead carries
`age_at_start: {"years": 22, "on": "2026-07-01"}`. This is an explicit alternative
to `born`, not an assumed birthday. Attribute scales are the stored scales:
form, technique, condition and bonus 0–127; skills 0–3; display strength 0–99.
`bmh-classic@1` in `world/rules.json` declares those scales and marks strength as
derived. These are master seeds, before career initialization varies ratings,
rewrites nationality fields or shuffles league participants. No career is
started and no random stream is consumed during import.
### Identifiers and edition mappings
Ids derive from source identity slots, not names: `club:00000`, `player:00000`,
`stadium:00000`. National teams use `nt:<nation key>`. Recognized geographic
aliases get ISO nation keys (England/Scotland/Wales/Northern Ireland use private
football-association keys). Unrecognized or duplicate labels get
`nation:x-legacy-<index>` with confederation `other` and a warning. Foreign clubs
whose nation has not been decoded refer to `nation:x-legacy-unassigned`.
The source index always remains in `x-legacy`.
| Source leagues | German | Football Limited |
|---|---|---|
| 0, 1 | Tiers 1, 2; 18 clubs each | Tiers 1, 2; 20 clubs each |
| 2, 3 | Parallel tier 3, ids ending `-3-a` / `-3-b`; 18 each | Tiers 3, 4; 20 each |
| 4 | Tier 4; 18 clubs | Tier 5; 10 clubs, `played: false`, no fixture generator |
| 5–8 | Four 12-club leagues, using nation slots 1–4 | Same, with the English database's nation-slot order |
| 138–167 | Named amateur-pool slots | Only named slots become entities; unnamed slots remain in the source data |
| 168–356 | Foreign teams without squads | Same |
| 357–418 | National teams / nationality labels | Same |
Foreign strength bytes encode a band and style flags, not a 0–255 rating. They
stay in `x-legacy.strength_code` until a typed interpretation is implemented.
Fixture tables store home/away **draw-position pairs**, not final shuffled
career fixtures. Calendar words remain source day offsets; competition options
and weekday alignment have not run. Promotion and European-place bytes are
retained as inputs, without pretending that cup qualifications have been mapped.
The existing executable loaders apply OpenBMH's supported rule profile, and the
English loader translates FLTD data into the common address space. The manifest
records `rule_profile: openbmh-supported`; this tool does **not** claim an
unmodified dump of every original edition's rules. No executable is included in
the OBW file.
### Text and rights
Text follows the existing ISO 646-DE substitutions. Extended bytes are preserved
as Latin-1 Unicode code points and original hexadecimal bytes; control/layout
codes remain available in the legacy text table. The local gettext catalogue
uses stable `legacy.text.<index>` message ids with `legacy-text` context. It is
a preservation layer, not a completed translation of the modern interface.
Imported names and texts retain their original ownership. Keep the resulting
world on your machine; the importer does not grant permission to distribute it.
The public repository contains only implementation, schemas and synthetic tests.
## Validation and remaining work
The tool checks required documents, table versions, duplicate identities,
cross-table team/nation references, stage participant counts, fixture rows,
source squad ownership and supported edition sizes before publishing output.
Malformed/truncated input is an error; it does not substitute synthetic game
content. `obw_import_tests` covers synthetic German/English mappings, legacy
reader input, archive/directory round trips and rejected inputs.
Still to implement: full cup definitions/qualification and dated calendars,
complete rule mappings, initialized finances/contracts/staff/stadiums, asset
conversion, `.FBM` to `.obs`, a general OBW reader/validator and the 0.2 engine
adapter. First-season equivalence and save continuation regressions only become
possible when that adapter exists; the first converter does not claim them.
+58
View File
@@ -0,0 +1,58 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/calendar.json",
"type": "object",
"properties": {
"schema": {
"const": "calendar@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^calendar:[a-zA-Z0-9._-]+$"
},
"season_start": {
"const": "07-01"
},
"series": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"legacy_calendar_bit": {
"type": "integer",
"minimum": 0,
"maximum": 31
}
},
"required": [
"id",
"legacy_calendar_bit"
],
"additionalProperties": true
}
}
},
"required": [
"id",
"season_start",
"series"
],
"additionalProperties": true
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+55
View File
@@ -0,0 +1,55 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/clubs.json",
"type": "object",
"properties": {
"schema": {
"const": "clubs@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"$ref": "common.json#/$defs/team_id"
},
"type": {
"enum": [
"club",
"national",
"amateur",
"foreign"
]
},
"name": {
"$ref": "common.json#/$defs/localized"
},
"nation": {
"$ref": "common.json#/$defs/nation_id"
},
"stadium": {
"type": "string",
"pattern": "^stadium:[a-zA-Z0-9._-]+$"
},
"strength": {
"type": "integer"
}
},
"required": [
"id",
"type",
"name",
"nation"
],
"additionalProperties": true
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+42
View File
@@ -0,0 +1,42 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/common.json",
"$defs": {
"localized": {
"type": "object",
"properties": {
"default": {
"type": "string"
}
},
"required": [
"default"
],
"additionalProperties": true
},
"nation_id": {
"type": "string",
"pattern": "^nation:[a-zA-Z0-9._-]+$"
},
"team_id": {
"type": "string",
"pattern": "^(club|nt):[a-zA-Z0-9._-]+$"
},
"person_id": {
"type": "string",
"pattern": "^(player|staff):[a-zA-Z0-9._-]+$"
},
"date": {
"type": "string",
"format": "date",
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
},
"attributes": {
"type": "object",
"additionalProperties": {
"type": "integer"
}
}
},
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
@@ -0,0 +1,112 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/competitions.json",
"type": "object",
"properties": {
"schema": {
"const": "competitions@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^comp:[a-zA-Z0-9._-]+$"
},
"name": {
"$ref": "common.json#/$defs/localized"
},
"type": {
"const": "league"
},
"nation": {
"$ref": "common.json#/$defs/nation_id"
},
"tier": {
"type": "integer",
"minimum": 1
},
"played": {
"type": "boolean"
},
"stages": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"format": {
"const": "league"
},
"teams": {
"type": "integer",
"minimum": 4,
"maximum": 24
},
"legs": {
"type": "integer",
"minimum": 1
},
"generator": {
"type": "string"
},
"matchdays": {
"type": "string"
},
"slots": {
"type": "array",
"items": {
"type": "object",
"properties": {
"teams": {
"type": "array",
"items": {
"$ref": "common.json#/$defs/team_id"
},
"uniqueItems": true
}
},
"required": [
"teams"
],
"additionalProperties": true
},
"minItems": 1
}
},
"required": [
"id",
"format",
"teams",
"legs",
"slots"
],
"additionalProperties": true
},
"minItems": 1
}
},
"required": [
"id",
"name",
"type",
"nation",
"tier",
"played",
"stages"
],
"additionalProperties": true
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
@@ -0,0 +1,49 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/contracts.json",
"type": "object",
"properties": {
"schema": {
"const": "contracts@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^contract:[a-zA-Z0-9._-]+$"
},
"person": {
"$ref": "common.json#/$defs/person_id"
},
"team": {
"$ref": "common.json#/$defs/team_id"
},
"salary": {
"type": "integer"
},
"start": {
"$ref": "common.json#/$defs/date"
},
"end": {
"$ref": "common.json#/$defs/date"
}
},
"required": [
"id",
"person",
"team"
],
"additionalProperties": true
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+39
View File
@@ -0,0 +1,39 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/finance.json",
"type": "object",
"properties": {
"schema": {
"const": "finance@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^finance:[a-zA-Z0-9._-]+$"
},
"team": {
"$ref": "common.json#/$defs/team_id"
},
"balance": {
"type": "integer"
}
},
"required": [
"id",
"team"
],
"additionalProperties": true
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+138
View File
@@ -0,0 +1,138 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/manifest.json",
"type": "object",
"properties": {
"format": {
"const": "openbmh-world"
},
"format_version": {
"const": "1.0"
},
"kind": {
"const": "world"
},
"min_reader": {
"const": "1.0"
},
"title": {
"$ref": "common.json#/$defs/localized"
},
"generator": {
"type": "string"
},
"provenance": {
"enum": [
"import:bmh-de",
"import:fltd"
]
},
"content_license": {
"const": "local-import-only"
},
"currency": {
"type": "object",
"properties": {
"code": {
"type": "string"
},
"minor_units": {
"const": 0
}
},
"required": [
"code",
"minor_units"
],
"additionalProperties": true
},
"start_date": {
"$ref": "common.json#/$defs/date"
},
"languages": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
},
"limits": {
"type": "object",
"properties": {
"leagues": {
"const": 32
}
},
"required": [
"leagues"
],
"additionalProperties": true
},
"requires": {
"const": [
"import:legacy-database-v1"
]
},
"extensions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"version": {
"type": "string"
}
},
"required": [
"id",
"version"
],
"additionalProperties": true
}
},
"x-import": {
"type": "object",
"properties": {
"profile": {
"const": "legacy-database-v1"
},
"playable": {
"const": false
},
"rule_profile": {
"const": "openbmh-supported"
},
"default_attribute_set": {
"type": "string"
}
},
"required": [
"profile",
"playable",
"rule_profile"
],
"additionalProperties": true
}
},
"required": [
"format",
"format_version",
"kind",
"min_reader",
"title",
"generator",
"provenance",
"content_license",
"currency",
"start_date",
"languages",
"limits",
"requires",
"extensions",
"x-import"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+55
View File
@@ -0,0 +1,55 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/nations.json",
"type": "object",
"properties": {
"schema": {
"const": "nations@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^nation:[a-zA-Z0-9._-]+$"
},
"name": {
"$ref": "common.json#/$defs/localized"
},
"confederation": {
"enum": [
"uefa",
"conmebol",
"caf",
"afc",
"concacaf",
"ofc",
"other"
]
},
"national_team": {
"type": "string",
"pattern": "^nt:[a-zA-Z0-9._-]+$"
},
"playable": {
"type": "boolean"
}
},
"required": [
"id",
"name",
"confederation"
],
"additionalProperties": true
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+127
View File
@@ -0,0 +1,127 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/players.json",
"type": "object",
"properties": {
"schema": {
"const": "players@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^player:[a-zA-Z0-9._-]+$"
},
"name": {
"type": "object",
"properties": {
"first": {
"type": "string"
},
"last": {
"type": "string"
},
"known_as": {
"type": "string"
}
},
"required": [
"first",
"last"
],
"additionalProperties": true
},
"born": {
"$ref": "common.json#/$defs/date"
},
"age_at_start": {
"type": "object",
"properties": {
"years": {
"type": "integer",
"minimum": 16,
"maximum": 47
},
"on": {
"$ref": "common.json#/$defs/date"
}
},
"required": [
"years",
"on"
],
"additionalProperties": true
},
"nationalities": {
"type": "array",
"items": {
"$ref": "common.json#/$defs/nation_id"
},
"minItems": 1
},
"positions": {
"type": "array",
"items": {
"enum": [
"gk",
"def",
"mid",
"fwd"
]
},
"minItems": 1
},
"attribute_set": {
"const": "bmh-classic@1"
},
"attributes": {
"$ref": "common.json#/$defs/attributes"
},
"team": {
"$ref": "common.json#/$defs/team_id"
}
},
"required": [
"id",
"name",
"nationalities",
"positions",
"attribute_set",
"attributes"
],
"additionalProperties": true,
"oneOf": [
{
"required": [
"born"
],
"not": {
"required": [
"age_at_start"
]
}
},
{
"required": [
"age_at_start"
],
"not": {
"required": [
"born"
]
}
}
]
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+35
View File
@@ -0,0 +1,35 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/rules.json",
"type": "object",
"properties": {
"schema": {
"const": "rules@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
}
},
"required": [
"id",
"type"
],
"additionalProperties": true
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+52
View File
@@ -0,0 +1,52 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/stadiums.json",
"type": "object",
"properties": {
"schema": {
"const": "stadiums@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^stadium:[a-zA-Z0-9._-]+$"
},
"name": {
"$ref": "common.json#/$defs/localized"
},
"owner": {
"oneOf": [
{
"$ref": "common.json#/$defs/team_id"
},
{
"type": "null"
}
]
},
"capacity": {
"type": "object"
},
"facilities": {
"type": "object"
}
},
"required": [
"id",
"name"
],
"additionalProperties": true
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+43
View File
@@ -0,0 +1,43 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/Weyoune/openbmh/main/docs/formats/obw/schema/1.0/staff.json",
"type": "object",
"properties": {
"schema": {
"const": "staff@1.0"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^staff:[a-zA-Z0-9._-]+$"
},
"name": {
"$ref": "common.json#/$defs/localized"
},
"role": {
"type": "string"
},
"team": {
"$ref": "common.json#/$defs/team_id"
}
},
"required": [
"id",
"name",
"role"
],
"additionalProperties": true
}
}
},
"required": [
"schema",
"items"
],
"additionalProperties": true,
"$comment": "SPDX-License-Identifier: GPL-3.0-only; initial legacy-database-v1 conversion profile."
}
+17
View File
@@ -0,0 +1,17 @@
# SPDX-License-Identifier: GPL-3.0-only
add_library(bmh_obw STATIC obw.cpp ../cli/json.cpp)
target_include_directories(bmh_obw PUBLIC ..)
target_link_libraries(bmh_obw PUBLIC bmhlaunch PRIVATE bmh_miniz)
add_executable(openbmh_import main.cpp)
target_link_libraries(openbmh_import PRIVATE bmh_obw)
set_target_properties(openbmh_import PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
add_executable(obw_import_tests ../../tests/obw_import_tests.cpp)
target_link_libraries(obw_import_tests PRIVATE bmh_obw bmh_miniz)
if(MSVC)
target_compile_options(bmh_obw PRIVATE /W4 /utf-8)
target_compile_options(openbmh_import PRIVATE /W4 /utf-8)
target_compile_options(obw_import_tests PRIVATE /W4 /utf-8)
endif()
add_test(NAME obw_import_tests COMMAND obw_import_tests)
# A separate component: existing 0.1 runtime packages retain their two products.
install(TARGETS openbmh_import RUNTIME DESTINATION . COMPONENT ImportTools)
+55
View File
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "import/obw.hpp"
#include "launch/archive.hpp"
#include "version.hpp"
#include <iostream>
#include <stdexcept>
int main(int argc, char** argv) {
try {
const auto args = bmh::launch::utf8_arguments(argc, argv);
std::filesystem::path source, destination;
bmh::obw::Options options;
bool directory = false;
for (size_t i = 1; i < args.size(); ++i) {
const auto& arg = args[i];
if (arg == "--help" || arg == "-h") {
std::cout << "OpenBMH installation-to-OBW converter\n"
"Usage: openbmh_import --data DIR_OR_EXE --output WORLD.obw\n"
" --start-date YYYY-07-01 [--title TEXT] [--directory]\n\n"
"Reads BMH.EXE/M3D.MAN/TEXTD.BLK or FLTD.EXE/M3E.MAN/TEXTE.BLK.\n"
"--directory writes an unpacked world instead of a ZIP archive.\n"
"Existing destinations are never overwritten. Imported content stays local.\n"
"Conversion profile only: no game loading, FBM saves or graphics yet.\n";
return 0;
}
if (arg == "--version") {
std::cout << "OpenBMH importer " << bmh::kOpenBmhVersion << " (OBW 1.0)\n";
return 0;
}
if (arg == "--directory") { directory = true; continue; }
if (arg != "--data" && arg != "--output" && arg != "--start-date" && arg != "--title")
throw std::runtime_error("unknown option: " + arg);
if (++i == args.size()) throw std::runtime_error("missing value for " + arg);
if (arg == "--data") source = bmh::launch::path_from_utf8(args[i]);
if (arg == "--output") destination = bmh::launch::path_from_utf8(args[i]);
if (arg == "--start-date") options.start_date = args[i];
if (arg == "--title") options.title = args[i];
}
if (source.empty() || destination.empty() || options.start_date.empty())
throw std::runtime_error("--data, --output and --start-date are required; see --help");
if (!directory && destination.extension() != ".obw")
throw std::runtime_error("archive output must use the .obw extension");
const auto documents = bmh::obw::import_installation(source, options);
bmh::obw::write(documents, destination, directory);
std::cout << "Converted " << documents.size() << " documents to "
<< bmh::launch::path_to_utf8(destination) << "\n"
<< "Profile: legacy-database-v1; content_license: local-import-only.\n"
<< "See x-legacy/import-report.json for coverage and remaining mappings.\n";
return 0;
} catch (const std::exception& error) {
std::cerr << "openbmh_import: " << error.what() << '\n';
return 1;
}
}
+617
View File
@@ -0,0 +1,617 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "import/obw.hpp"
#include <algorithm>
#include <array>
#include <chrono>
#include <cctype>
#include <fstream>
#include <iomanip>
#include <set>
#include <sstream>
#include <stdexcept>
#include "formats/executable_layout.hpp"
#include "launch/archive.hpp"
#include "launch/fltd.hpp"
#include "launch/game_data.hpp"
#include "miniz.h"
#include "version.hpp"
namespace fs = std::filesystem;
namespace bmh::obw {
namespace {
using J = cli::Json;
using A = J::Array;
using O = J::Object;
[[noreturn]] void fail(const std::string& message) { throw FormatError("OBW: " + message); }
std::string id(const std::string& type, size_t index) {
std::ostringstream out;
out << type << ':' << std::setfill('0') << std::setw(5) << index;
return out.str();
}
std::string hex(const Bytes& bytes) {
constexpr char digits[] = "0123456789abcdef";
std::string result;
result.reserve(bytes.size() * 2);
for (const auto byte : bytes) {
result += digits[byte >> 4];
result += digits[byte & 15];
}
return result;
}
// The documented ISO 646-DE substitutions, with the installation's extended
// Latin-1 bytes represented as Unicode as well. Raw bytes are preserved separately.
std::string text(const std::string& raw) {
std::string result;
for (const unsigned char byte : raw) {
if (byte >= 128) {
result += char(0xc0 | (byte >> 6));
result += char(0x80 | (byte & 63));
} else {
const char one[] = {char(byte), 0};
result += iso646de_to_utf8(one, 1);
}
}
return result;
}
bool named(const std::string& value) {
return std::any_of(value.begin(), value.end(), [](unsigned char ch) {
return std::isalnum(ch) || ch >= 128;
});
}
J localized(const std::string& value) { return O{{"default", value}}; }
J table(const std::string& schema, A items) {
return O{{"schema", schema + "@1.0"}, {"items", std::move(items)}};
}
void put(Documents& out, const std::string& path, J value) {
out.emplace(path, cli::dump_json(value) + '\n');
}
int start_year(const std::string& date) {
if (date.size() != 10 || date.substr(4) != "-07-01" ||
!std::all_of(date.begin(), date.begin() + 4, [](unsigned char c) { return c >= '0' && c <= '9'; }))
fail("start_date must be YYYY-07-01 (legacy season anchor)");
const int year = std::stoi(date.substr(0, 4));
if (year < 1900 || year > 9998) fail("start_date year must be between 1900 and 9998");
return year;
}
A bytes_at(const LeImage& exe, uint32_t address, size_t count) {
A result;
for (size_t i = 0; i < count; ++i) result.emplace_back(int(exe.u8(address + uint32_t(i))));
return result;
}
struct NationKey { std::string key, confederation; };
NationKey nation_key(const std::string& label, size_t index) {
// Generic geographic aliases, never an edition-dependent positional inventory.
// Unknown or renamed countries retain a private, documented source key.
std::string upper = label;
for (char& ch : upper) if (ch >= 'a' && ch <= 'z') ch = char(ch - 'a' + 'A');
static const std::map<std::string, std::string> aliases = {
{"DEUTSCHLAND", "de"}, {"GERMANY", "de"}, {"ENGLAND", "x-eng"},
{"SPANIEN", "es"}, {"SPAIN", "es"}, {"FRANKREICH", "fr"}, {"FRANCE", "fr"},
{"ITALIEN", "it"}, {"ITALY", "it"}, {"PORTUGAL", "pt"},
{"NIEDERLANDE", "nl"}, {"HOLLAND", "nl"}, {"NETHERLANDS", "nl"},
{"BELGIEN", "be"}, {"BELGIUM", "be"}, {"SCHOTTLAND", "x-sco"}, {"SCOTLAND", "x-sco"},
{"WALES", "x-wal"}, {"N.IRELAND", "x-nir"}, {"NORTHERN IRELAND", "x-nir"},
{"IRLAND", "ie"}, {"IRELAND", "ie"}, {"REP. OF IRELAND", "ie"},
{"ÖSTERREICH", "at"}, {"AUSTRIA", "at"}, {"SCHWEIZ", "ch"}, {"SWITZERLAND", "ch"},
{"DÄNEMARK", "dk"}, {"DENMARK", "dk"}, {"SCHWEDEN", "se"}, {"SWEDEN", "se"},
{"NORWEGEN", "no"}, {"NORWAY", "no"}, {"FINNLAND", "fi"}, {"FINLAND", "fi"},
{"POLEN", "pl"}, {"POLAND", "pl"}, {"TÜRKEI", "tr"}, {"TURKEY", "tr"},
{"RUSSLAND", "ru"}, {"RUSSIA", "ru"}, {"UKRAINE", "ua"},
{"TSCHECHIEN", "cz"}, {"TSCHECHEI", "cz"}, {"SLOWAKEI", "sk"}, {"SLOVAKIA", "sk"},
{"UNGARN", "hu"}, {"HUNGARY", "hu"}, {"RUMÄNIEN", "ro"}, {"ROMANIA", "ro"},
{"KROATIEN", "hr"}, {"CROATIA", "hr"}, {"BULGARIEN", "bg"}, {"BULGARIA", "bg"},
{"GRIECHENLAND", "gr"}, {"GREECE", "gr"}, {"SERBIEN", "rs"}, {"SERBIA", "rs"},
{"SLOWENIEN", "si"}, {"SLOVENIA", "si"}, {"BOSNIEN", "ba"}, {"BOSNIA", "ba"},
{"KOSOVO", "x-kos"}, {"LITHUANIA", "lt"}, {"LUXEMBURG", "lu"},
{"CYPRUS", "cy"}, {"MALTA", "mt"}, {"ISRAEL", "il"},
{"BRASILIEN", "br"}, {"BRASIL", "br"}, {"BRAZIL", "br"},
{"ARGENTINIEN", "ar"}, {"ARGENTINA", "ar"}, {"URUGUAY", "uy"}, {"PERU", "pe"},
{"CHILE", "cl"}, {"KOLUMBIEN", "co"}, {"COLUMBIA", "co"}, {"COLOMBIA", "co"},
{"BOLIVIEN", "bo"}, {"BOLIVIA", "bo"}, {"PARAGUAY", "py"},
{"VENEZUELA", "ve"}, {"ECUADOR", "ec"},
{"USA", "us"}, {"KANADA", "ca"}, {"CANADA", "ca"},
{"MEXIKO", "mx"}, {"MEXICO", "mx"}, {"PANAMA", "pa"},
{"AUSTRALIEN", "au"}, {"AUSTRALIA", "au"}, {"NEW ZEALAND", "nz"},
{"MAROKKO", "ma"}, {"MOROCCO", "ma"}, {"KAMERUN", "cm"}, {"CAMEROON", "cm"},
{"SENEGAL", "sn"}, {"NIGERIA", "ng"}, {"ALGERIEN", "dz"}, {"ALGERIA", "dz"},
{"TUNESIEN", "tn"}, {"TUNISIA", "tn"}, {"ÄGYPTEN", "eg"}, {"EGYPT", "eg"},
{"TOGO", "tg"}, {"GHANA", "gh"}, {"SÜDAFRIKA", "za"}, {"SÜDARFIKA", "za"},
{"SOUTH AFRICA", "za"}, {"ELFENBEINKÜSTE", "ci"},
{"IRAN", "ir"}, {"JAPAN", "jp"}, {"CHINA", "cn"},
{"SÜDKOREA", "kr"}, {"SOUTH KOREA", "kr"}, {"SAUDI ARABIEN", "sa"}, {"SAUDI ARABIA", "sa"},
{"INDONESIA", "id"}, {"LEBANON", "lb"}, {"IRAQ", "iq"}, {"KUWAIT", "kw"},
{"JORDAN", "jo"}, {"JORADAN", "jo"}, {"SYRIA", "sy"}
};
const auto found = aliases.find(upper);
if (found == aliases.end()) return {"x-legacy-" + std::to_string(index), "other"};
const std::string& key = found->second;
const std::set<std::string> south = {"br", "ar", "uy", "pe", "cl", "co", "bo", "py", "ve", "ec"};
const std::set<std::string> north = {"us", "ca", "mx", "pa"};
const std::set<std::string> africa = {"ma", "cm", "sn", "ng", "dz", "tn", "eg", "tg", "gh", "za", "ci"};
const std::set<std::string> asia = {"au", "ir", "jp", "cn", "kr", "sa", "id", "lb", "iq", "kw", "jo", "sy"};
return {key, south.contains(key) ? "conmebol" : north.contains(key) ? "concacaf" :
africa.contains(key) ? "caf" : asia.contains(key) ? "afc" : key == "nz" ? "ofc" : "uefa"};
}
std::string po_quote(const std::string& value) {
std::string result = "\"";
for (unsigned char ch : value) {
switch (ch) {
case '\\': result += "\\\\"; break;
case '"': result += "\\\""; break;
case '\n': result += "\\n"; break;
case '\r': result += "\\r"; break;
case '\t': result += "\\t"; break;
default:
if (ch < 32 || ch == 127) {
result += '\\';
result += char('0' + (ch >> 6));
result += char('0' + ((ch >> 3) & 7));
result += char('0' + (ch & 7));
} else result += char(ch);
}
}
return result + '"';
}
const J& field(const J& value, const std::string& key, const std::string& path) {
const auto* object = value.object();
if (!object || !object->contains(key)) fail(path + ": missing " + key);
return object->at(key);
}
std::string string_field(const J& value, const std::string& key, const std::string& path) {
const auto* result = field(value, key, path).string();
if (!result) fail(path + ": " + key + " must be a string");
return *result;
}
} // namespace
Documents convert_database(const Database& db, const TextDb& texts, const LeImage& exe,
bool english, const Options& options) {
start_year(options.start_date);
if (db.club_names.size() != 419 || db.players.size() != 2574 || db.squad_list.size() < 2928 * 2)
fail("unsupported database counts (expected 419 clubs, 2574 players and 2928 squad slots)");
if (texts.strings.size() != TEXT_COUNT) fail("incomplete text database");
const int expected[9] = {english ? 20 : 18, english ? 20 : 18, english ? 20 : 18,
english ? 20 : 18, english ? 10 : 18, 12, 12, 12, 12};
for (size_t i = 0; i < 9; ++i)
if (exe.u8(layout::LeagueSizes + uint32_t(i)) != expected[i])
fail("unsupported edition league sizes");
Documents out;
A warnings, nations, clubs, players, stadiums, competitions, rules, calendar;
std::array<std::string, 62> nation_ids;
std::set<std::string> used_nation_ids;
const std::string language = english ? "en" : "de";
for (size_t i = 0; i < nation_ids.size(); ++i) {
const auto label = text(db.club_names.at(357 + i));
auto key = nation_key(label, i);
if (!used_nation_ids.insert(key.key).second) {
key = {"x-legacy-" + std::to_string(i), "other"};
used_nation_ids.insert(key.key);
}
if (key.key.starts_with("x-legacy-"))
warnings.emplace_back("unresolved nation identity at legacy index " + std::to_string(i));
nation_ids[i] = "nation:" + key.key;
nations.emplace_back(O{{"id", nation_ids[i]}, {"name", localized(label)},
{"confederation", key.confederation}, {"playable", i < 5},
{"national_team", "nt:" + key.key}, {"x-legacy", O{{"nationality_index", int(i)}, {"team_index", int(357 + i)}}}});
}
nations.emplace_back(O{{"id", "nation:x-legacy-unassigned"},
{"name", localized("Unassigned legacy nation")}, {"confederation", "other"}, {"playable", false}});
std::vector<std::string> club_ids(419);
int league_first = 0;
std::array<std::string, 9> comp_ids;
for (int league = 0; league < 9; ++league) {
const int nation = league < 5 ? 0 : league - 4;
const int tier = league < 5 ? (english ? league + 1 : (league < 3 ? league + 1 : league)) : 1;
comp_ids[size_t(league)] = "comp:" + nation_ids[size_t(nation)].substr(7) + '-' + std::to_string(tier);
if (!english && league == 2) comp_ids[2] += "-a";
if (!english && league == 3) comp_ids[3] += "-b";
for (int team = league_first; team < league_first + expected[league]; ++team)
club_ids[size_t(team)] = id("club", size_t(team));
league_first += expected[league];
}
for (size_t team = 138; team < 357; ++team)
if (named(text(db.club_names[team]))) club_ids[team] = id("club", team);
for (size_t i = 0; i < nation_ids.size(); ++i) club_ids[357 + i] = "nt:" + nation_ids[i].substr(7);
for (size_t team = 0; team < club_ids.size(); ++team) {
if (club_ids[team].empty()) continue;
int league = -1, first = 0;
for (int l = 0; l < 9; ++l) {
if (int(team) >= first && int(team) < first + expected[l]) league = l;
first += expected[l];
}
const std::string type = team >= 357 ? "national" : team >= 168 ? "foreign" : team >= 138 ? "amateur" : "club";
const std::string nation = team >= 357 ? nation_ids[team - 357] : team >= 168 ?
"nation:x-legacy-unassigned" : nation_ids[league >= 5 ? size_t(league - 4) : 0];
O row{{"id", club_ids[team]}, {"type", type}, {"name", localized(text(db.club_names[team]))}, {"nation", nation},
{"x-legacy", O{{"team_index", int(team)}, {"badge_word", int(le16(db.raw_club_badge_selectors, team * 2))},
{"kit_selector", int(db.raw_club_kit_selectors.at(team))}}}};
// This byte encodes a strength band and style flags, not a 0..99 rating.
if (team >= 168) row.at("x-legacy").object()->emplace("strength_code", int(db.raw_foreign_club_strength.at(team - 168)));
clubs.emplace_back(std::move(row));
}
std::vector<int> player_team(db.players.size(), -1);
std::vector<int> player_row(db.players.size(), -1);
for (int team = 0; team < 138; ++team) {
const size_t first = team < 90 ? size_t(team) * 24 : size_t(team) * 16 + 720;
const size_t count = team < 90 ? 24 : 16;
for (size_t row = 0; row < count; ++row) {
const auto word = le16(db.squad_list, (first + row) * 2);
const size_t player = word & 4095;
if (player == 4095) continue;
if (player >= db.players.size()) fail("squad slot references an unknown player");
if (player_team[player] >= 0) fail("player occurs in more than one squad slot");
player_team[player] = team;
player_row[player] = int(row);
}
}
constexpr const char* positions[] = {"gk", "def", "mid", "fwd"};
constexpr const char* skills[] = {"teamplay", "speed", "shooting", "passing", "tackling", "free_kick", "corner"};
for (size_t i = 0; i < db.players.size(); ++i) {
const auto& player = db.players[i];
int nationality = player.nationality() + ((db.raw_player_bonus_values.at(i) & 128) ? 32 : 0);
// Import the source record, not career initialization's nationality
// rewrites and random ratings. The original words remain in x-legacy.
if (nationality < 0 || size_t(nationality) >= nation_ids.size())
fail("player " + std::to_string(i) + " nationality " + std::to_string(nationality) + " is outside the nation table");
O attributes{{"form", player.form()}, {"technique", player.technique()}, {"condition", player.condition()},
{"strength", player.strength()}, {"bonus", int(db.raw_player_bonus_values.at(i) & 127)}};
for (int skill = 0; skill < 7; ++skill) attributes[skills[skill]] = player.skill(Player::Skill(skill));
O row{{"id", id("player", i)}, {"name", O{{"first", ""}, {"last", text(player.name)}}},
{"age_at_start", O{{"years", player.age()}, {"on", options.start_date}}},
{"nationalities", A{nation_ids[size_t(nationality)]}}, {"positions", A{positions[player.position()]}},
{"attribute_set", "bmh-classic@1"}, {"attributes", std::move(attributes)},
{"x-legacy", O{{"player_index", int(i)}, {"condition_technique_form_word", J(int64_t(player.w35c))},
{"skill_word", J(int64_t(player.w368))}, {"identity_word", J(int64_t(player.w364))},
{"squad_row", player_row[i]}}}};
if (player_team[i] >= 0) row["team"] = club_ids[size_t(player_team[i])];
players.emplace_back(std::move(row));
}
for (size_t i = 0; i < db.stadiums.size(); ++i)
stadiums.emplace_back(O{{"id", id("stadium", i)}, {"name", localized(text(db.stadiums[i]))},
{"x-legacy", O{{"name_pool_index", int(i)}}}});
A fixture_tables;
const std::vector<std::pair<int, uint32_t>> bindings = english ?
std::vector<std::pair<int, uint32_t>>{{4, layout::EnglishFourTeamFixtureTable}, {6, layout::EnglishSixTeamFixtureTable},
{12, layout::EnglishTwelveTeamFixtureTable}, {20, layout::EnglishTwentyTeamFixtureTable}} :
std::vector<std::pair<int, uint32_t>>{{4, layout::CareerSession_RuntimeError_RuntimeField},
{6, layout::CareerSession_RuntimeError_RuntimeField2}, {12, layout::CareerSession_RuntimeError_RuntimeField3},
{16, layout::CareerSession_RuntimeError_ExecutableField3}, {18, layout::CareerSession_RuntimeError_ExecutableField},
{20, layout::CareerSession_RuntimeError_ExecutableField2}};
for (const auto& [teams, address] : bindings) {
A rounds;
for (int round = 1; round < teams; ++round) {
const auto source = exe.cstring(exe.u32(address + uint32_t(round * 4)));
if (source.size() != size_t(teams)) fail("fixture table has an invalid round length");
std::set<char> seen;
A pairs;
for (const char ch : source)
if (ch < 'a' || ch >= 'a' + teams || !seen.insert(ch).second) fail("fixture round has an invalid team");
for (size_t i = 0; i < source.size(); i += 2) pairs.emplace_back(A{int(source[i] - 'a'), int(source[i + 1] - 'a')});
rounds.emplace_back(std::move(pairs));
}
fixture_tables.emplace_back(O{{"id", "legacy:" + std::to_string(teams)}, {"teams", teams}, {"rounds", std::move(rounds)}});
}
A series;
int first = 0, label_index = 0;
for (size_t league = 0; league < 9; ++league) {
const int teams = expected[league];
A members;
for (int t = first; t < first + teams; ++t) members.emplace_back(club_ids[size_t(t)]);
const auto bit = exe.u8(layout::LeagueCalendarBits + uint32_t(league));
const bool played = !(english && league == 4);
const auto series_id = "legacy-league-" + std::to_string(league);
std::string label;
if (league < 5) {
const auto label_slot = (layout::Achievements_FormatError_TextPointer3 - layout::TextPointerTable) / 4 + uint32_t(label_index);
label = text(texts.strings.at(label_slot));
const char suffix = char(exe.u8(layout::CareerSession_InstallLeagueSizes_RuntimeField3 + uint32_t(league * 2)));
if (suffix) label += std::string(" ") + suffix;
if (league == 4 || exe.u8(layout::CareerSession_InstallLeagueSizes_RuntimeField3 + uint32_t((league + 1) * 2)) < 'B') ++label_index;
} else label = text(db.club_names[357 + league - 4]);
O stage{{"id", "regular"}, {"format", "league"}, {"teams", teams}, {"legs", 2},
{"generator", "legacy:" + std::to_string(teams)}, {"slots", A{O{{"teams", std::move(members)}}}}};
// The unplayed English pool has no ten-team legacy fixture generator.
if (played) stage["matchdays"] = "calendar:" + series_id;
else stage.erase("generator");
const auto nation = league < 5 ? nation_ids[0] : nation_ids[league - 4];
const int tier = league < 5 ? (english ? int(league + 1) : (league < 3 ? int(league + 1) : int(league))) : 1;
competitions.emplace_back(O{{"id", comp_ids[league]}, {"name", localized(label)}, {"type", "league"},
{"nation", nation}, {"tier", tier}, {"played", played}, {"stages", A{std::move(stage)}},
{"x-legacy", O{{"league_index", int(league)}, {"calendar_bit", int(bit)},
{"relegation_places", int(exe.u8(layout::CareerSession_InstallLeagueSizes_RuntimeField2 + uint32_t(league)))},
{"europe_places", int(exe.u8(layout::CareerSession_InstallLeagueSizes_RuntimeField + uint32_t(league)))}}}});
if (played) series.emplace_back(O{{"id", series_id}, {"legacy_calendar_bit", int(bit)}});
first += teams;
}
O attribute_fields;
for (const char* attribute : {"form", "technique", "condition"})
attribute_fields[attribute] = O{{"min", 0}, {"max", 127}};
attribute_fields["strength"] = O{{"min", 0}, {"max", 99}, {"derived", true}};
attribute_fields["bonus"] = O{{"min", 0}, {"max", 127}};
for (const char* skill : skills) attribute_fields[skill] = O{{"min", 0}, {"max", 3}};
rules.emplace_back(O{{"id", "bmh-classic@1"}, {"type", "attribute_set"}, {"fields", std::move(attribute_fields)}});
rules.emplace_back(O{{"id", "rule:legacy-fixtures"}, {"type", "fixture_tables"}, {"tables", std::move(fixture_tables)}});
rules.emplace_back(O{{"id", "rule:legacy-ladder"}, {"type", "legacy_ladder"},
{"first_league", bytes_at(exe, layout::CalendarFirstLeague, 8)},
{"league_count", bytes_at(exe, layout::CalendarLeagueCounts, 8)},
{"below", bytes_at(exe, layout::CareerSession_InstallLeagueSizes_RuntimeField5, 8)},
{"below_count", bytes_at(exe, layout::CareerSession_InstallLeagueSizes_RuntimeField4, 8)},
{"newcomer_first", int(exe.u16(layout::CareerSession_DispatchLineup_RuntimeField))},
{"newcomer_count", int(exe.u16(layout::CareerSession_InstallLeagueSizes_RuntimeField6))}});
A resorts;
for (uint32_t resort = 0; resort < 8; ++resort)
resorts.emplace_back(O{{"id", "resort:" + std::to_string(resort)},
{"base_price", J(int64_t(int32_t(exe.u32(layout::DeskActions_ConfigureTrainingCampData_RuntimeField2 + resort * 4))))},
{"quality", bytes_at(exe, layout::DeskActions_ConfigureTrainingCampData_RuntimeField + resort * 4, 4)}});
rules.emplace_back(O{{"id", "rule:training-camps"}, {"type", "training_camps"}, {"resorts", std::move(resorts)}});
// Retain the floating-point divisor as IEEE-754 bytes, not a rounded money value.
rules.emplace_back(O{{"id", "rule:credit-rates"}, {"type", "credit_rates"},
{"factors", bytes_at(exe, layout::BankCredit_BankCreditContext_RuntimeField3, 49)},
{"x-legacy-divisor-low", J(int64_t(exe.u32(layout::BankCredit_BankCreditContext_RuntimeField)))},
{"x-legacy-divisor-high", J(int64_t(exe.u32(layout::BankCredit_BankCreditContext_RuntimeField2)))}});
A day_words;
for (uint32_t day = 0; day < 366; ++day) day_words.emplace_back(int(exe.u16(layout::CompetitionCalendar + day * 2)));
calendar.emplace_back(O{{"id", "calendar:legacy-season"}, {"season_start", "07-01"},
{"series", std::move(series)}, {"x-legacy", O{{"day_words", std::move(day_words)},
{"note", "Source day offsets, not final dated fixtures; career setup applies competition and weekday options."}}}});
const auto currency_suffix = exe.cstring(layout::CurrencySuffixText);
const std::string currency = english ? "GBP" : currency_suffix == " DM" ? "DEM" : currency_suffix == " EU" ? "EUR" : "X-LEGACY";
put(out, "manifest.json", O{{"format", "openbmh-world"}, {"format_version", "1.0"}, {"kind", "world"},
{"min_reader", "1.0"}, {"generator", std::string("OpenBMH importer ") + kOpenBmhVersion},
{"title", localized(options.title)}, {"provenance", english ? "import:fltd" : "import:bmh-de"},
{"content_license", "local-import-only"}, {"currency", O{{"code", currency}, {"minor_units", 0}, {"x-legacy-suffix", text(currency_suffix)}}},
{"start_date", options.start_date}, {"languages", A{language}}, {"limits", O{{"leagues", 32}}},
{"requires", A{"import:legacy-database-v1"}}, {"extensions", A{O{{"id", "x-legacy"}, {"version", "1.0"}}}},
{"x-import", O{{"profile", "legacy-database-v1"}, {"playable", false},
{"rule_profile", "openbmh-supported"}, {"default_attribute_set", "bmh-classic@1"}}}});
put(out, "world/nations.json", table("nations", std::move(nations)));
put(out, "world/clubs.json", table("clubs", std::move(clubs)));
put(out, "world/players.json", table("players", std::move(players)));
put(out, "world/stadiums.json", table("stadiums", std::move(stadiums)));
for (const char* empty : {"staff", "contracts", "finance"})
put(out, std::string("world/") + empty + ".json", table(empty, {}));
put(out, "world/competitions.json", table("competitions", std::move(competitions)));
put(out, "world/rules.json", table("rules", std::move(rules)));
put(out, "world/calendar.json", table("calendar", std::move(calendar)));
O raw_tables;
const std::pair<const char*, const Bytes*> raw[] = {
{"player_bonus_values", &db.raw_player_bonus_values}, {"database_metadata", &db.raw_database_metadata},
{"club_auxiliary_words", &db.raw_club_auxiliary_words}, {"club_badge_selectors", &db.raw_club_badge_selectors},
{"foreign_club_strength", &db.raw_foreign_club_strength}, {"league_auxiliary_bytes", &db.raw_league_auxiliary_bytes},
{"referee_properties", &db.raw_referee_properties}, {"club_kit_selectors", &db.raw_club_kit_selectors},
{"club_short_name_offsets", &db.raw_club_short_name_offsets}, {"club_name_offsets", &db.raw_club_name_offsets},
{"player_name_offsets", &db.raw_player_name_offsets}, {"squad_list", &db.squad_list}};
for (const auto& [name, data] : raw) raw_tables[name] = hex(*data);
A pools;
for (const auto& pool : db.pools) pools.emplace_back(hex(pool));
put(out, "x-legacy/database.json", O{{"schema", "legacy-database@1.0"}, {"encoding", "hex"},
{"counts", O{{"clubs", 419}, {"players", 2574}, {"n42", 168}, {"n66", 3096}}},
{"pools", std::move(pools)}, {"tables", std::move(raw_tables)}});
A original_texts;
std::string po = "# Local installation import; not licensed for redistribution.\nmsgid \"\"\nmsgstr \"\"\n"
"\"Content-Type: text/plain; charset=UTF-8\\n\"\n\"Language: " + language + "\\n\"\n";
for (size_t i = 0; i < texts.strings.size(); ++i) {
original_texts.emplace_back(O{{"index", int(i)}, {"text", text(texts.strings[i])},
{"raw_hex", hex(Bytes(texts.strings[i].begin(), texts.strings[i].end()))}});
po += "\nmsgctxt \"legacy-text\"\nmsgid \"legacy.text." + std::to_string(i) + "\"\nmsgstr " + po_quote(text(texts.strings[i])) + '\n';
}
put(out, "x-legacy/texts.json", table("legacy-texts", std::move(original_texts)));
out["locale/" + language + "-original/openbmh.po"] = std::move(po);
warnings.emplace_back("foreign club nation assignments remain unresolved outside the four simulated foreign leagues");
put(out, "x-legacy/import-report.json", O{{"schema", "legacy-import-report@1.0"},
{"profile", "legacy-database-v1"}, {"warnings", std::move(warnings)},
{"unmapped", A{"staff and referee roles", "stadium capacities/owners", "contracts and finances",
"cup definitions and qualification", "dated calendar/competition options", "complete executable rule tables",
"foreign strength-band interpretation", "career initialization transforms", "graphics/assets"}},
{"notes", A{"Database attributes are seeds: legacy career initialization varies player ratings.",
"No career was started and no random numbers were consumed.",
"Executable tables use the existing OpenBMH loader and its supported rule profile, not an unmodified executable dump.",
"Source ages are retained without inventing exact birthdays; reference date is supplied by the user."}}});
validate(out);
return out;
}
Documents import_installation(const fs::path& source, const Options& options) {
if (!fs::exists(source)) fail("source does not exist");
if (!fs::is_directory(source)) {
auto filename = launch::path_to_utf8(source.filename());
for (char& ch : filename) ch = char(std::toupper(static_cast<unsigned char>(ch)));
if (filename != "BMH.EXE" && filename != "FLTD.EXE") fail("source must be a directory, BMH.EXE or FLTD.EXE; FBM conversion is not implemented");
}
fs::path root = fs::is_directory(source) ? source : source.parent_path();
auto german = launch::find_file(root, "BMH.EXE"), english_exe = launch::find_file(root, "FLTD.EXE");
if (!german && !english_exe && fs::is_directory(root)) {
std::vector<fs::path> candidates;
for (const auto& entry : fs::directory_iterator(root))
if (entry.is_directory() && (launch::find_file(entry.path(), "BMH.EXE") || launch::find_file(entry.path(), "FLTD.EXE")))
candidates.push_back(entry.path());
if (candidates.size() != 1) fail("expected exactly one BMH.EXE or FLTD.EXE installation");
root = candidates[0];
german = launch::find_file(root, "BMH.EXE");
english_exe = launch::find_file(root, "FLTD.EXE");
}
if (!german && !english_exe) fail("no BMH.EXE or FLTD.EXE found");
bool english = bool(english_exe) && !german;
if (german && english_exe) {
if (fs::is_directory(source)) fail("ambiguous installation: name BMH.EXE or FLTD.EXE explicitly");
if (fs::equivalent(source, *english_exe)) english = true;
else if (!fs::equivalent(source, *german)) fail("name BMH.EXE or FLTD.EXE explicitly");
}
const auto required = [&](const std::string& name) {
const auto file = launch::find_file(root, name);
if (!file || !fs::is_regular_file(*file)) fail("missing " + name);
return *file;
};
const auto exe = english ? launch::translate_fltd(*english_exe) : load_le(*german);
const auto db_path = required(english ? "M3E.MAN" : "M3D.MAN");
const auto text_path = required(english ? "TEXTE.BLK" : "TEXTD.BLK");
const auto database = load_database(db_path);
auto out = convert_database(database, load_textd(text_path), exe, english, options);
// Exact source database for lossless future re-mapping, not an executable or a
// runnable legacy installation. No absolute developer paths enter the archive.
put(out, "x-legacy/source-database.json", O{{"schema", "legacy-source@1.0"},
{"name", english ? "M3E.MAN" : "M3D.MAN"}, {"encoding", "hex"}, {"bytes", hex(read_file(db_path))}});
return out;
}
void validate(const Documents& documents) {
const auto parse = [&](const std::string& path) {
const auto found = documents.find(path);
if (found == documents.end()) fail(path + ": missing document");
return cli::parse_json(found->second);
};
const auto manifest = parse("manifest.json");
for (const auto& [key, expected] : std::vector<std::pair<std::string, std::string>>{
{"format", "openbmh-world"}, {"format_version", "1.0"}, {"kind", "world"}, {"content_license", "local-import-only"}})
if (string_field(manifest, key, "manifest.json") != expected) fail("manifest.json: unsupported " + key);
start_year(string_field(manifest, "start_date", "manifest.json"));
const auto& features = field(manifest, "requires", "manifest.json");
if (!features.array() || features.array()->size() != 1 || !features.array()->front().string() ||
*features.array()->front().string() != "import:legacy-database-v1") fail("manifest.json: missing conversion-only feature guard");
const auto& profile = field(manifest, "x-import", "manifest.json");
const auto* playable = field(profile, "playable", "manifest.json:x-import").boolean();
if (!playable || *playable || string_field(profile, "profile", "manifest.json:x-import") != "legacy-database-v1")
fail("manifest.json: conversion profile must remain non-playable");
std::map<std::string, std::string> ids;
struct Reference { std::string value, prefix, path; };
std::vector<Reference> references;
for (const char* name : {"nations", "clubs", "players", "staff", "stadiums", "contracts", "finance", "competitions", "rules", "calendar"}) {
const std::string path = std::string("world/") + name + ".json";
const auto data = parse(path);
if (string_field(data, "schema", path) != std::string(name) + "@1.0") fail(path + ": unsupported schema");
const auto* items = field(data, "items", path).array();
if (!items) fail(path + ": items must be an array");
for (size_t i = 0; i < items->size(); ++i) {
const auto& item = items->at(i);
const auto at = path + ":items[" + std::to_string(i) + ']';
const auto entity_id = string_field(item, "id", at);
if (!ids.emplace(entity_id, name).second) fail(at + ": duplicate id " + entity_id);
const std::map<std::string, std::string> prefixes{{"nations", "nation:"}, {"players", "player:"},
{"staff", "staff:"}, {"stadiums", "stadium:"}, {"contracts", "contract:"},
{"finance", "finance:"}, {"competitions", "comp:"}, {"calendar", "calendar:"}};
if (const auto prefix = prefixes.find(name); prefix != prefixes.end() && !entity_id.starts_with(prefix->second))
fail(at + ": wrong id type " + entity_id);
if (std::string(name) == "clubs" && !entity_id.starts_with("club:") && !entity_id.starts_with("nt:"))
fail(at + ": wrong team id type " + entity_id);
const auto ref = [&](const std::string& key, const std::string& prefix) {
if (item.object()->contains(key)) references.push_back({string_field(item, key, at), prefix, at + '.' + key});
};
ref("nation", "nation:"); ref("team", "club:"); ref("stadium", "stadium:"); ref("national_team", "nt:");
if (std::string(name) == "players") {
const auto* nats = field(item, "nationalities", at).array();
if (!nats || nats->empty()) fail(at + ": nationalities must not be empty");
for (const auto& nat : *nats) {
if (!nat.string()) fail(at + ": invalid nationality");
references.push_back({*nat.string(), "nation:", at + ".nationalities"});
}
if (string_field(item, "attribute_set", at) != "bmh-classic@1") fail(at + ": invalid attribute set");
const auto& attributes = field(item, "attributes", at);
if (!attributes.object()) fail(at + ": attributes must be an object");
for (const auto& [attribute, value] : *attributes.object()) {
const auto* number = value.integer();
const int maximum = attribute == "strength" ? 99 :
(attribute == "form" || attribute == "technique" || attribute == "condition" || attribute == "bonus") ? 127 : 3;
if (!number || *number < 0 || *number > maximum) fail(at + ".attributes." + attribute + ": outside classic scale");
}
}
if (std::string(name) == "competitions") {
const auto* stages = field(item, "stages", at).array();
if (!stages) fail(at + ": stages must be an array");
for (const auto& stage : *stages) {
const auto* slots = field(stage, "slots", at).array();
if (!slots) fail(at + ": slots must be an array");
size_t participants = 0;
std::set<std::string> unique_teams;
for (const auto& slot : *slots) {
const auto* teams = field(slot, "teams", at).array();
if (!teams) fail(at + ": slot teams must be an array");
for (const auto& team : *teams) {
if (!team.string()) fail(at + ": invalid slot team");
if (!unique_teams.insert(*team.string()).second) fail(at + ": duplicate stage participant");
references.push_back({*team.string(), "club:", at + ".stages.slots"});
}
participants += teams->size();
}
const auto* count = field(stage, "teams", at).integer();
if (!count || *count != int64_t(participants)) fail(at + ": stage team count disagrees with slots");
}
}
}
}
for (const auto& ref : references)
if (!ids.contains(ref.value) || !ref.value.starts_with(ref.prefix)) fail(ref.path + ": unknown or wrong-type id " + ref.value);
}
void write(const Documents& documents, const fs::path& destination, bool directory) {
validate(documents);
if (fs::symlink_status(destination).type() != fs::file_type::not_found) fail("destination already exists");
const auto parent = destination.has_parent_path() ? destination.parent_path() : fs::path(".");
if (!fs::is_directory(parent)) fail("output parent directory does not exist");
fs::path staging;
for (int attempt = 0; attempt < 100; ++attempt) {
staging = parent / (".openbmh-import-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()) + '-' + std::to_string(attempt));
if (fs::create_directory(staging)) break;
staging.clear();
}
if (staging.empty()) fail("cannot reserve output staging directory");
struct Cleanup {
fs::path path;
~Cleanup() { std::error_code ec; fs::remove_all(path, ec); }
} cleanup{staging};
mz_zip_archive zip{};
if (!directory && !mz_zip_writer_init_heap(&zip, 0, 0)) fail("cannot initialize ZIP writer");
struct ZipCleanup { mz_zip_archive* zip; ~ZipCleanup() { if (zip) mz_zip_writer_end(zip); } } zip_cleanup{directory ? nullptr : &zip};
for (const auto& [name, contents] : documents) {
const fs::path relative = launch::path_from_utf8(name);
if (relative.empty() || relative.is_absolute() || relative.has_root_name() || name.find('\\') != std::string::npos)
fail("unsafe document path");
for (const auto& part : relative) if (part == ".." || part == ".") fail("unsafe document path");
if (directory) {
const auto file = staging / relative;
fs::create_directories(file.parent_path());
std::ofstream stream(file, std::ios::binary);
stream.write(contents.data(), std::streamsize(contents.size()));
stream.close();
if (!stream) fail("cannot write document " + name);
} else if (!mz_zip_writer_add_mem(&zip, name.c_str(), contents.data(), contents.size(), MZ_BEST_COMPRESSION))
fail("cannot add ZIP document " + name);
}
if (!directory) {
void* buffer = nullptr;
size_t size = 0;
if (!mz_zip_writer_finalize_heap_archive(&zip, &buffer, &size)) fail("cannot finish ZIP archive");
struct Free { void* value; ~Free() { mz_free(value); } } free{buffer};
std::ofstream stream(staging / "world.obw", std::ios::binary);
stream.write(static_cast<const char*>(buffer), std::streamsize(size));
stream.close();
if (!stream) fail("cannot write ZIP archive");
}
if (fs::symlink_status(destination).type() != fs::file_type::not_found) fail("destination appeared during conversion");
fs::rename(directory ? staging : staging / "world.obw", destination);
}
} // namespace bmh::obw
+36
View File
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <filesystem>
#include <map>
#include <string>
#include "cli/json.hpp"
#include "formats/le.hpp"
#include "formats/m3d.hpp"
#include "formats/textd.hpp"
namespace bmh::obw {
// A conversion document, deliberately independent of GameState and CareerSession.
// Keys are relative, authored archive paths; values are UTF-8 text.
using Documents = std::map<std::string, std::string>;
struct Options {
std::string start_date; // Required: legacy databases have ages, not birth dates.
std::string title = "Imported legacy world";
};
Documents convert_database(const Database& database, const TextDb& texts,
const LeImage& executable, bool english, const Options& options);
Documents import_installation(const std::filesystem::path& source, const Options& options);
// Checks the conversion profile and its cross-table references before any output.
// This is not the future engine's competition/playability validator.
void validate(const Documents& documents);
// Refuses existing destinations. Publishes a complete sibling staging path by rename.
// No files in the installation are changed, copied back or executed.
void write(const Documents& documents, const std::filesystem::path& destination, bool directory);
} // namespace bmh::obw
+242
View File
@@ -0,0 +1,242 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "import/obw.hpp"
#include "formats/executable_layout.hpp"
#include "launch/archive.hpp"
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iostream>
#include <stdexcept>
namespace fs = std::filesystem;
using namespace bmh;
using J = bmh::cli::Json;
namespace {
void check(bool condition, const std::string& message) {
if (!condition) throw std::runtime_error(message);
}
template<class F> void rejects(F&& action, const std::string& message) {
bool rejected = false;
try { action(); } catch (const std::exception&) { rejected = true; }
check(rejected, message);
}
void word(Bytes& data, size_t at, uint32_t value, size_t width = 2) {
for (size_t i = 0; i < width; ++i) data.at(at + i) = uint8_t(value >> (i * 8));
}
void append(Bytes& data, uint32_t value, size_t width = 2) {
for (size_t i = 0; i < width; ++i) data.push_back(uint8_t(value >> (i * 8)));
}
void save(const fs::path& path, const Bytes& bytes) {
std::ofstream out(path, std::ios::binary);
out.write(reinterpret_cast<const char*>(bytes.data()), std::streamsize(bytes.size()));
check(bool(out), "write synthetic fixture");
}
const J& f(const J& object, const char* name) { return object.object()->at(name); }
J document(const obw::Documents& documents, const char* path) { return cli::parse_json(documents.at(path)); }
Database database(bool english) {
Database db;
db.club_names.resize(419);
for (size_t i = 0; i < db.club_names.size(); ++i) db.club_names[i] = "Synthetic team " + std::to_string(i);
db.club_names[0] = "Test M}nchen";
db.club_names[357] = english ? "ENGLAND" : "DEUTSCHLAND";
db.club_names[358] = english ? "GERMANY" : "SPANIEN";
db.club_names[359] = english ? "ITALY" : "ENGLAND";
db.club_names[360] = "FRANCE";
db.club_names[361] = english ? "SPAIN" : "ITALIEN";
if (english) db.club_names[150] = " ";
db.players.resize(2574);
for (size_t i = 0; i < db.players.size(); ++i)
db.players[i] = {"Synthetic Player " + std::to_string(i), 60u | (90u << 7) | (80u << 14), 0x321, (6u << 21) | uint32_t(i % 4)};
db.players[0].name = "T{st \"Name\"";
db.players[1].w368 |= 1u << 27;
db.raw_player_bonus_values.resize(3146);
db.raw_player_bonus_values[1] = 128 | 17; // nationality 33, plus a bonus of 17
db.raw_database_metadata.resize(100);
db.raw_club_auxiliary_words.resize(168 * 2);
db.raw_club_badge_selectors.resize(419 * 2);
db.raw_foreign_club_strength.resize(251, 60);
db.raw_league_auxiliary_bytes.resize(189);
db.raw_referee_properties.resize(50);
db.raw_club_kit_selectors.resize(419);
db.raw_club_short_name_offsets.resize(168);
db.squad_list.resize((3096 - 4) * 2 + 108);
for (size_t i = 0; i < db.squad_list.size(); i += 2) word(db.squad_list, i, 4095);
word(db.squad_list, 0, 0x8000); word(db.squad_list, 2, 1);
word(db.squad_list, 90 * 24 * 2, 2); // first foreign squad, 16-slot stride
word(db.squad_list, (2928 - 1) * 2, 3); // last squad
db.referees = {"Synthetic Referee"};
db.stadiums = {"Synthetic Stadium"};
const auto pool = [](const std::vector<std::string>& names, Bytes& offsets) {
Bytes result;
for (const auto& name : names) {
append(offsets, uint32_t(result.size()));
result.insert(result.end(), name.begin(), name.end()); result.push_back(0);
}
return result;
};
db.pools[0] = pool(db.club_names, db.raw_club_name_offsets);
std::vector<std::string> names;
for (const auto& player : db.players) names.push_back(player.name);
db.pools[1] = pool(names, db.raw_player_name_offsets);
Bytes unused;
db.pools[2] = pool(db.referees, unused); db.pools[3] = pool(db.stadiums, unused);
return db;
}
LeImage executable(bool english) {
constexpr uint32_t base = 0x10000;
Bytes bytes(layout::GermanImageEnd - base);
const auto put = [&](uint32_t address, uint32_t value, size_t width = 1) { word(bytes, address - base, value, width); };
const int sizes[] = {english ? 20 : 18, english ? 20 : 18, english ? 20 : 18, english ? 20 : 18, english ? 10 : 18, 12, 12, 12, 12};
for (uint32_t i = 0; i < 9; ++i) {
put(layout::LeagueSizes + i, uint32_t(sizes[i]));
put(layout::LeagueCalendarBits + i, english && i == 4 ? 31 : i);
}
if (!english) { put(layout::CareerSession_InstallLeagueSizes_RuntimeField3 + 4, 'A'); put(layout::CareerSession_InstallLeagueSizes_RuntimeField3 + 6, 'B'); }
const std::vector<std::pair<int, uint32_t>> bindings = english ?
std::vector<std::pair<int, uint32_t>>{{4, layout::EnglishFourTeamFixtureTable}, {6, layout::EnglishSixTeamFixtureTable},
{12, layout::EnglishTwelveTeamFixtureTable}, {20, layout::EnglishTwentyTeamFixtureTable}} :
std::vector<std::pair<int, uint32_t>>{{4, layout::CareerSession_RuntimeError_RuntimeField}, {6, layout::CareerSession_RuntimeError_RuntimeField2},
{12, layout::CareerSession_RuntimeError_RuntimeField3}, {16, layout::CareerSession_RuntimeError_ExecutableField3},
{18, layout::CareerSession_RuntimeError_ExecutableField}, {20, layout::CareerSession_RuntimeError_ExecutableField2}};
uint32_t cursor = base + 400000; // synthetic string storage, outside pointer tables
for (const auto& [teams, address] : bindings) {
put(address, cursor, 4); ++cursor;
std::vector<int> order;
for (int i = 0; i < teams; ++i) order.push_back(i);
for (int round = 1; round < teams; ++round) {
put(address + uint32_t(round * 4), cursor, 4);
for (int i = 0; i < teams / 2; ++i) {
put(cursor++, uint32_t('a' + order[size_t(i)]));
put(cursor++, uint32_t('a' + order[size_t(teams - 1 - i)]));
}
++cursor;
std::rotate(order.begin() + 1, order.end() - 1, order.end());
}
}
put(layout::Career_BuildStadium_RuntimeField4 - 1, 2);
const std::string suffix = english ? " &" : " DM";
for (size_t i = 0; i < suffix.size(); ++i) put(layout::CurrencySuffixText + uint32_t(i), uint8_t(suffix[i]));
return LeImage::from_bytes(base, std::move(bytes));
}
Bytes serialize(const Database& db) {
Bytes bytes;
for (const auto& pool : db.pools) append(bytes, uint32_t(pool.size()));
const auto add = [&](const Bytes& data) { bytes.insert(bytes.end(), data.begin(), data.end()); };
add(db.raw_player_bonus_values);
for (const auto& pool : db.pools) add(pool);
for (const auto* table : {&db.raw_club_name_offsets, &db.raw_player_name_offsets, &db.raw_database_metadata,
&db.raw_club_auxiliary_words, &db.raw_club_badge_selectors, &db.raw_foreign_club_strength,
&db.raw_league_auxiliary_bytes, &db.raw_referee_properties, &db.raw_club_kit_selectors, &db.raw_club_short_name_offsets, &db.squad_list}) add(*table);
for (int table = 0; table < 3; ++table)
for (const auto& player : db.players) append(bytes, table == 0 ? player.w35c : table == 1 ? player.w368 : player.w364, 4);
return bytes;
}
} // namespace
int main() {
fs::path root;
try {
root = fs::temp_directory_path() / ("openbmh-obw-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
fs::create_directory(root);
const obw::Options options{"2026-07-01", "Synthetic world"};
TextDb texts;
texts.strings.resize(TEXT_COUNT, "Synthetic text\n\"quote\"\\slash");
texts.strings[0] += char(0xa7);
const auto db = database(false);
const auto exe = executable(false);
const auto documents = obw::convert_database(db, texts, exe, false, options);
check(documents == obw::convert_database(db, texts, exe, false, options), "conversion must be deterministic");
const auto clubs = document(documents, "world/clubs.json");
check(*f(f(f(clubs, "items").array()->at(0), "name"), "default").string() == "Test München", "decode German names");
const auto players = document(documents, "world/players.json");
const auto& rows = *f(players, "items").array();
check(rows.size() == 2574, "all player identities retained");
check(*f(rows[0], "team").string() == "club:00000", "lineup flag must not change player id");
check(*f(rows[2], "team").string() == "club:00090", "foreign squad stride");
check(*f(rows[3], "team").string() == "club:00137", "last squad assignment");
check(!rows[4].object()->contains("team"), "unassigned player remains free");
check(!rows[0].object()->contains("born"), "do not invent birthdays");
check(*f(f(rows[0], "name"), "last").string() == "Täst \"Name\"", "escaped player name survives JSON");
check(*f(f(rows[1], "attributes"), "bonus").integer() == 17, "bonus high bit is nationality, not rating");
check(*f(rows[1], "nationalities").array()->at(0).string() == "nation:x-legacy-33", "nationality extension bit");
check(*f(rows.back(), "nationalities").array()->at(0).string() == "nation:de", "database nationalities are preserved before career rewrites");
check(documents.at("locale/de-original/openbmh.po").find("\\\"quote\\\"") != std::string::npos, "gettext quoting");
const auto english = obw::convert_database(database(true), texts, executable(true), true, options);
const auto manifest = document(english, "manifest.json");
check(*f(manifest, "provenance").string() == "import:fltd", "English provenance");
check(*f(f(manifest, "currency"), "code").string() == "GBP", "English currency");
const auto comps = document(english, "world/competitions.json");
const auto& leagues = *f(comps, "items").array();
check(*f(leagues[3], "tier").integer() == 4, "English fourth tier");
check(!*f(leagues[4], "played").boolean(), "English fifth league is not simulated");
check(!f(leagues[4], "stages").array()->at(0).object()->contains("generator"), "no fabricated ten-team fixture table");
check(f(document(english, "world/clubs.json"), "items").array()->size() == 418, "ignore unnamed English slot");
const auto zip = root / "test.obw";
obw::write(documents, zip, false);
auto archive = launch::Archive::open(zip);
check(archive.entries().size() == documents.size(), "complete ZIP container");
for (size_t i = 0; i < archive.entries().size(); ++i) {
const auto& entry = archive.entries()[i];
const auto bytes = archive.read(i);
check(std::string(bytes.begin(), bytes.end()) == documents.at(entry.name), "ZIP document round trip");
}
const auto folder = root / "unpacked";
obw::write(documents, folder, true);
for (const auto& [name, contents] : documents) {
const auto bytes = read_file(folder / name);
check(std::string(bytes.begin(), bytes.end()) == contents, "unpacked document round trip");
}
const auto saved_zip = read_file(zip);
rejects([&] { obw::write(english, zip, false); }, "refuse to overwrite archive");
check(read_file(zip) == saved_zip, "existing output untouched");
rejects([&] { obw::write(documents, folder, true); }, "refuse to overwrite directory");
rejects([&] { obw::convert_database(db, texts, exe, false, {"2026-02-30", "bad"}); }, "reject invalid date");
auto bad_db = db;
word(bad_db.squad_list, 2, 0);
rejects([&] { obw::convert_database(bad_db, texts, exe, false, options); }, "reject duplicate squad ownership");
word(bad_db.squad_list, 2, 4000);
rejects([&] { obw::convert_database(bad_db, texts, exe, false, options); }, "reject unknown player");
auto bad = documents;
bad.erase("world/players.json");
rejects([&] { obw::write(bad, root / "bad.obw", false); }, "reject incomplete archive");
check(!fs::exists(root / "bad.obw"), "failure leaves no published output");
bad = documents;
auto changed = players;
std::get<J::Array>(changed.object()->at("items").value)[0].object()->at("team") = "club:99999";
bad["world/players.json"] = cli::dump_json(changed);
rejects([&] { obw::validate(bad); }, "reject dangling player team");
bad = documents;
bad["../escape.json"] = "{}";
rejects([&] { obw::write(bad, root / "escape.obw", false); }, "reject path traversal");
check(!fs::exists(root / "escape.obw"), "bad archive path leaves no output");
// Exercise the actual on-disk legacy readers and case-insensitive discovery.
const auto input = root / "synthetic-installation";
fs::create_directory(input);
const auto raw_database = serialize(db);
save(input / "m3d.man", raw_database);
save_image(exe, input / "bmh.EXE");
Bytes raw_texts(7);
for (const auto& value : texts.strings) { raw_texts.insert(raw_texts.end(), value.begin(), value.end()); raw_texts.push_back(0); }
save(input / "textd.blk", raw_texts);
const auto converted = obw::import_installation(input, options);
check(converted.contains("x-legacy/source-database.json"), "retain exact database for future mappings");
check(read_file(input / "m3d.man") == raw_database, "source database unchanged");
check(obw::import_installation(root, options) == converted, "follow one nested installation");
save(input / "m3d.man", Bytes{0, 1});
rejects([&] { obw::import_installation(input, options); }, "reject truncated legacy database");
fs::remove_all(root);
std::cout << "OBW importer: synthetic DE/EN mappings, containers and rejection paths passed\n";
return 0;
} catch (const std::exception& error) {
if (!root.empty()) { std::error_code ec; fs::remove_all(root, ec); }
std::cerr << error.what() << '\n';
return 1;
}
}