Add core functionality for loading and processing game assets
- Implemented palette loading from binary files in `palette.cpp`. - Added runtime utilities for reading text and JSON files in `runtime.cpp`. - Created shape archive handling in `shape_archive.cpp`, including frame parsing and pixel decoding. - Introduced type flag loading functionality in `type_flags.cpp`. - Developed tests for combat tactics, entity dispatch entries, file system operations, fixed maps, flex archives, glob archives, map rendering, palette loading, shape archives, and type flags. - Added a Python script for importing Ghidra-exported function slices with reporting capabilities.
This commit is contained in:
parent
6ca3b18c3e
commit
8907604b84
41 changed files with 2889 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -46,6 +46,8 @@ exports/**
|
||||||
out/**
|
out/**
|
||||||
binary/**
|
binary/**
|
||||||
psx-map-exporter/.output-render/**
|
psx-map-exporter/.output-render/**
|
||||||
|
rebuild/**/out/**
|
||||||
|
rebuild/**/imported/**
|
||||||
|
|
||||||
# JavaScript / Node
|
# JavaScript / Node
|
||||||
**/node_modules/**
|
**/node_modules/**
|
||||||
|
|
|
||||||
561
docs/remorse-windows-rebuild-roadmap.md
Normal file
561
docs/remorse-windows-rebuild-roadmap.md
Normal file
|
|
@ -0,0 +1,561 @@
|
||||||
|
# Remorse Windows Rebuild Roadmap
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
This note records the current effort to turn the live Ghidra analysis and the exported C decompilation of `CRUSADER.EXE` into a source tree that can be recompiled and eventually run on Windows, including modern Windows.
|
||||||
|
|
||||||
|
The short version: the project now has enough decompiled coverage to start a real rebuild lane, but the exported files are still evidence, not directly compilable game source. The safest path is to keep the export as an input corpus and move cleaned, subsystem-sized slices into a separate host rebuild tree with explicit ABI and platform boundaries.
|
||||||
|
|
||||||
|
## Current Inputs
|
||||||
|
|
||||||
|
- `exports/src_remorse/export_manifest.json` reports `CRUSADER.EXE`, 2,984 exported function files, 9 fallback function files, 57 global buckets, and 15,403 global definitions.
|
||||||
|
- `exports/src_remorse/CRUSADER.EXE.h` contains the bulk Ghidra data types and provisional structure definitions.
|
||||||
|
- `exports/src_remorse/functions/` contains split function bodies, including class-like folders such as `Remorse/EntityDispatchEntry`, `Remorse/SpriteNode`, `Remorse/EntityVmRuntime`, and related process families.
|
||||||
|
- Existing rebuild planning is in `docs/remorse-cpp-decompilation-plan.md`, `docs/remorse-rebuild-abi-notes.md`, and `docs/remorse-cpp-compatibility-header-draft.md`.
|
||||||
|
|
||||||
|
## First Concrete Scaffold
|
||||||
|
|
||||||
|
The first host rebuild scaffold is now under `rebuild/remorse/`.
|
||||||
|
|
||||||
|
It is deliberately separate from `exports/src_remorse/` because the export is generated, ignored by git, and still contains syntax and ABI artifacts that modern compilers cannot accept wholesale.
|
||||||
|
|
||||||
|
Current scaffold files:
|
||||||
|
|
||||||
|
- `rebuild/remorse/CMakeLists.txt`: CMake target for a Windows-friendly C++17 executable named `remorse_rebuild`.
|
||||||
|
- `rebuild/remorse/build_msvc.cmd`: repeatable MSVC/CMake build, test, and harness-run command.
|
||||||
|
- `rebuild/remorse/include/remorse/combat_tactics.h`: typed reader for `COMBAT.DAT` tactic records.
|
||||||
|
- `rebuild/remorse/include/remorse/compat.h`: exact-width aliases, far-pointer placeholders, packing macros, and calling-convention marker macros.
|
||||||
|
- `rebuild/remorse/include/remorse/export_prelude.h`: temporary bridge types for staged Ghidra-export imports.
|
||||||
|
- `rebuild/remorse/include/remorse/entity_dispatch_entry.h`: first hand-maintained class-family skeleton, anchored to the `11e0:0000` / older `0008:ba00` allocation evidence.
|
||||||
|
- `rebuild/remorse/include/remorse/file_system.h`: first host file/path discovery shim for replacing DOS install-path assumptions.
|
||||||
|
- `rebuild/remorse/include/remorse/flex_archive.h`: standard FLEX archive directory parser.
|
||||||
|
- `rebuild/remorse/include/remorse/fixed_map.h`: typed `FIXED.DAT` map table and 16-byte item-record reader.
|
||||||
|
- `rebuild/remorse/include/remorse/glob_archive.h`: typed `GLOB.FLX` object-entry reader.
|
||||||
|
- `rebuild/remorse/include/remorse/map_renderer.h`: first dependency-free map preview renderer and BMP writer.
|
||||||
|
- `rebuild/remorse/include/remorse/palette.h`: first game-data parser for 256-color RGB palette files.
|
||||||
|
- `rebuild/remorse/include/remorse/runtime.h`: minimal runtime/export-manifest surface.
|
||||||
|
- `rebuild/remorse/include/remorse/shape_archive.h`: typed `SHAPES.FLX` shape-frame parser and row-RLE decoder.
|
||||||
|
- `rebuild/remorse/include/remorse/type_flags.h`: typed `TYPEFLAG.DAT` shape-family and flag decoder.
|
||||||
|
- `rebuild/remorse/src/combat_tactics.cpp`: parses tactic names, four block offsets, and bytecode payloads from `COMBAT.DAT` entries.
|
||||||
|
- `rebuild/remorse/src/entity_dispatch_entry.cpp`: minimal host-side `EntityDispatchEntry` behavior stubs for compile-time shape and early probing.
|
||||||
|
- `rebuild/remorse/src/file_system.cpp`: repository/retail data-root discovery using modern `std::filesystem`.
|
||||||
|
- `rebuild/remorse/src/flex_archive.cpp`: loader for count-at-`0x54`, table-at-`0x80` FLEX archives.
|
||||||
|
- `rebuild/remorse/src/fixed_map.cpp`: loader for `FIXED.DAT` map tables and scaled fixed-map item coordinates.
|
||||||
|
- `rebuild/remorse/src/glob_archive.cpp`: parser for glob object entry lists inside `GLOB.FLX`.
|
||||||
|
- `rebuild/remorse/src/map_preview_main.cpp`: command-line preview executable for writing a viewable BMP image.
|
||||||
|
- `rebuild/remorse/src/map_renderer.cpp`: combines fixed maps, glob expansion, typeflags, shape frames, and palette data into a painted preview image.
|
||||||
|
- `rebuild/remorse/src/palette.cpp`: `GAMEPAL.PAL`-style 768-byte palette parser.
|
||||||
|
- `rebuild/remorse/src/runtime.cpp`: loads a summary from the Ghidra export manifest.
|
||||||
|
- `rebuild/remorse/src/shape_archive.cpp`: ports the documented Crusader frame-table and row-RLE decode rules.
|
||||||
|
- `rebuild/remorse/src/type_flags.cpp`: ports the verified 9-byte Crusader typeflag decode from the map-rendering tool.
|
||||||
|
- `rebuild/remorse/src/main.cpp`: starts the host harness and prints the export status.
|
||||||
|
- `rebuild/remorse/tests/combat_tactics_tests.cpp`: checks `COMBAT.DAT` parses to 14 tactics and entry 0 is `Dumb`.
|
||||||
|
- `rebuild/remorse/tests/entity_dispatch_entry_tests.cpp`: first host-side check for the confirmed `EntityDispatchEntry` offsets.
|
||||||
|
- `rebuild/remorse/tests/file_system_tests.cpp`: first host-side check that the current repository root can be recognized as a game-data root.
|
||||||
|
- `rebuild/remorse/tests/flex_archive_tests.cpp`: checks `STATIC/COMBAT.DAT` as a small FLEX archive with 14 populated records.
|
||||||
|
- `rebuild/remorse/tests/fixed_map_tests.cpp`: checks `STATIC/FIXED.DAT` map count and map 0 item-record loading.
|
||||||
|
- `rebuild/remorse/tests/glob_archive_tests.cpp`: checks `STATIC/GLOB.FLX` parses into at least one populated glob object.
|
||||||
|
- `rebuild/remorse/tests/map_renderer_tests.cpp`: checks map 1 can produce a non-empty preview image.
|
||||||
|
- `rebuild/remorse/tests/palette_tests.cpp`: checks that `STATIC/GAMEPAL.PAL` parses into 256 colors.
|
||||||
|
- `rebuild/remorse/tests/shape_archive_tests.cpp`: checks `STATIC/SHAPES.FLX` can decode at least one visible frame.
|
||||||
|
- `rebuild/remorse/tests/type_flags_tests.cpp`: checks `STATIC/TYPEFLAG.DAT` parses 2,048 shape records and includes glob-family records.
|
||||||
|
- `rebuild/remorse/tools/import_export_slice.py`: stages one exported function folder into `rebuild/remorse/imported/` for manual cleanup review.
|
||||||
|
|
||||||
|
This is not the game yet. It is the first buildable shell that future cleaned subsystems can link into without mutating the raw Ghidra export.
|
||||||
|
|
||||||
|
## Why The Export Does Not Compile Yet
|
||||||
|
|
||||||
|
The current exported C is very valuable, but it is not compiler-ready source for either DOS or Windows.
|
||||||
|
|
||||||
|
Known blockers visible in the export:
|
||||||
|
|
||||||
|
- C++ namespace-qualified function names appear inside `.c` files, for example `Remorse::EntityDispatchEntry::InitBase`.
|
||||||
|
- Ghidra calling-convention spellings such as `__cdecl16far`, `__cdecl16near`, `__stdcall16far`, and `__regcall` are not portable host compiler syntax.
|
||||||
|
- Ghidra undefined-width types and decompiler temporaries (`undefined`, `undefined2`, `undefined4`, `unaff_SS`, `unaff_CS`, synthetic stack variables, and return-storage pointers) need type and calling-convention cleanup.
|
||||||
|
- Header output includes invalid or provisional C declarations such as structure names containing spaces.
|
||||||
|
- Switch-table and global-byte exports are emitted as raw `undefined` objects that need relocation-aware or data-layout-aware modeling.
|
||||||
|
- Some functions still rely on segmented pointer reconstruction through expressions such as `CONCAT22`, not host pointers.
|
||||||
|
- The original binary is a DOS protected-mode `MZ -> NE` program using Phar Lap 286 extender behavior and far calls, so a modern Windows build needs a platform/runtime replacement rather than a simple relink.
|
||||||
|
|
||||||
|
## Rebuild Tracks
|
||||||
|
|
||||||
|
### Track A: Historical Executable Reconstruction
|
||||||
|
|
||||||
|
This track tries to rebuild something close to the original DOS protected-mode executable model.
|
||||||
|
|
||||||
|
It must preserve segmented pointers, far-call provenance, original-style packing, external runtime calls, and NE/Phar Lap assumptions. It is the hardest path and is not the primary route to modern Windows compatibility, but it remains important because it keeps the reverse-engineered ABI honest.
|
||||||
|
|
||||||
|
### Track B: Windows Source Port
|
||||||
|
|
||||||
|
This track uses the recovered source, object layouts, and resource formats to build a behaviorally equivalent Windows program.
|
||||||
|
|
||||||
|
It can replace DOS extender, VGA, sound, input, timing, file, and memory services with host implementations, but it should initially preserve exact-width data layouts and far-pointer provenance so behavior can be compared back to the original.
|
||||||
|
|
||||||
|
For the stated goal of running on modern Windows, Track B is the practical primary track. Track A should still guide early type cleanup so the port does not accidentally erase evidence.
|
||||||
|
|
||||||
|
## Conversion Strategy
|
||||||
|
|
||||||
|
1. Keep `exports/src_remorse/` as generated evidence.
|
||||||
|
2. Move one subsystem at a time into `rebuild/remorse/` as cleaned C++.
|
||||||
|
3. Compile every moved slice immediately, even before it is runnable game logic.
|
||||||
|
4. Replace Ghidra pseudo-types with `remorse::u8`, `remorse::u16`, `remorse::u32`, `remorse::FarPtr16`, and explicit packed structs.
|
||||||
|
5. Convert Ghidra calling-convention tokens into marker macros from `compat.h` until the final host calling surface is chosen.
|
||||||
|
6. Keep raw address provenance in comments or mapping tables for every imported function.
|
||||||
|
7. Build host runtime shims for platform services before trying to run gameplay.
|
||||||
|
8. Validate behavior with extracted resources and deterministic smoke tests.
|
||||||
|
|
||||||
|
## Full Rebuild Plan
|
||||||
|
|
||||||
|
The practical target is Track B first: a modern Windows source port that loads original data and reproduces gameplay behavior. Track A remains an evidence constraint for layout, calling convention, far-pointer provenance, and validation.
|
||||||
|
|
||||||
|
### Phase 0: Build And Evidence Discipline
|
||||||
|
|
||||||
|
Goal: every step builds, runs tests, and keeps provenance visible.
|
||||||
|
|
||||||
|
Exit criteria:
|
||||||
|
|
||||||
|
- `rebuild\remorse\build_msvc.cmd Debug` passes from the repository root
|
||||||
|
- every imported subsystem has address/source provenance in code or docs
|
||||||
|
- generated Ghidra exports remain input evidence, not hand-edited source
|
||||||
|
|
||||||
|
### Phase 1: Host Runtime Foundation
|
||||||
|
|
||||||
|
Goal: replace DOS installation assumptions with modern host services.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- game-data root discovery
|
||||||
|
- case-insensitive path resolution
|
||||||
|
- binary reads
|
||||||
|
- DOS-style file-handle facade
|
||||||
|
- writable config/save/user-data root
|
||||||
|
- command-line and config parsing
|
||||||
|
- logging and fatal-error reporting
|
||||||
|
|
||||||
|
Exit criteria: the harness can find data, open/read assets through the same facade future game code will use, and write test save/config output outside the read-only retail data root.
|
||||||
|
|
||||||
|
### Phase 2: Core Resource Loaders
|
||||||
|
|
||||||
|
Goal: typed loaders for the files needed before gameplay can start.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- palettes: `GAMEPAL.PAL`, misc palettes, palette transforms
|
||||||
|
- FLEX archives: `COMBAT.DAT`, `GLOB.FLX`, `SHAPES.FLX`, `EUSECODE.FLX`, fonts/gumps/icons
|
||||||
|
- maps: `FIXED.DAT`, glob expansion, type flags
|
||||||
|
- shapes: Crusader frame directory and RLE decode
|
||||||
|
- usecode archive chunk discovery
|
||||||
|
|
||||||
|
Exit criteria: a smoke test can load one map, expand globs, decode referenced shapes, and produce an in-memory scene without executing gameplay.
|
||||||
|
|
||||||
|
### Phase 3: Minimal Renderer
|
||||||
|
|
||||||
|
Goal: put a real Crusader world image on screen on Windows.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- choose initial graphics layer, likely SDL for window/input/timing/audio portability
|
||||||
|
- convert paletted frames to host pixels
|
||||||
|
- implement deterministic fixed-map painter first
|
||||||
|
- add debug overlay/status console for loaded map and camera position
|
||||||
|
|
||||||
|
Exit criteria: running the host executable opens a window and displays a recognizable fixed-map scene from retail data.
|
||||||
|
|
||||||
|
### Phase 4: Process, Item, And Entity Runtime
|
||||||
|
|
||||||
|
Goal: lift the core object/process model into host source.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- typed `Process` / `EntityDispatchEntry` layout
|
||||||
|
- item allocation, fixed-format conversion, map item cache
|
||||||
|
- scheduler/process list
|
||||||
|
- sprite-node/draw-list families
|
||||||
|
- core entity fields and update lanes
|
||||||
|
|
||||||
|
Exit criteria: the host runtime can instantiate map items/entities and advance a deterministic tick without requiring the full original VM yet.
|
||||||
|
|
||||||
|
### Phase 5: Input, Camera, And Player Loop
|
||||||
|
|
||||||
|
Goal: make the rendered world navigable.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- keyboard/mouse abstraction
|
||||||
|
- camera scroll/viewport state
|
||||||
|
- cursor/selection state
|
||||||
|
- player/avatar process skeleton
|
||||||
|
- collision/line-of-sight primitives as needed
|
||||||
|
|
||||||
|
Exit criteria: the user can launch the host build, view a map, move the camera/avatar test object, and observe stable tick/update behavior.
|
||||||
|
|
||||||
|
### Phase 6: USECODE / VM Runtime
|
||||||
|
|
||||||
|
Goal: execute game scripts from original `EUSECODE.FLX`.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- import and type `EntityVmRuntime`, owner-resource, context, and slot-entry families
|
||||||
|
- parse usecode chunks and dispatch tables
|
||||||
|
- implement VM value stack, globals, calls, and event dispatch
|
||||||
|
- cross-check against existing usecode extraction tools
|
||||||
|
|
||||||
|
Exit criteria: selected map triggers and object interactions execute through the host VM with traceable parity against known extracted usecode behavior.
|
||||||
|
|
||||||
|
### Phase 7: Gameplay Systems
|
||||||
|
|
||||||
|
Goal: make core No Remorse gameplay work.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- actor movement and animation
|
||||||
|
- combat tactic interpreter from `COMBAT.DAT`
|
||||||
|
- projectiles and hit resolution
|
||||||
|
- inventory/equipment
|
||||||
|
- doors, elevators, panels, triggers, movable walls
|
||||||
|
- save/load state
|
||||||
|
|
||||||
|
Exit criteria: a small scenario can be played end to end with movement, combat, interaction, and persistence.
|
||||||
|
|
||||||
|
### Phase 8: Media And Presentation
|
||||||
|
|
||||||
|
Goal: reach production game flow rather than only a debug map runner.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- menu/frontend flow
|
||||||
|
- fonts/gumps/icons/HUD
|
||||||
|
- music/audio mixer
|
||||||
|
- flic/movie playback
|
||||||
|
- cutscene and mission transition behavior
|
||||||
|
|
||||||
|
Exit criteria: the host build can boot through a recognizable frontend and enter gameplay using retail resources.
|
||||||
|
|
||||||
|
### Phase 9: Whole-Game Integration And Parity
|
||||||
|
|
||||||
|
Goal: turn subsystem success into a working game.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- mission progression
|
||||||
|
- all retail maps/resource variants
|
||||||
|
- error recovery and missing-data handling
|
||||||
|
- deterministic trace comparisons for high-risk systems
|
||||||
|
- long-run smoke tests
|
||||||
|
|
||||||
|
Exit criteria: the game can be started, played across mission transitions, saved, loaded, and exited on modern Windows.
|
||||||
|
|
||||||
|
### Phase 10: Packaging And Modernization
|
||||||
|
|
||||||
|
Goal: make it usable outside the development repo.
|
||||||
|
|
||||||
|
Work:
|
||||||
|
|
||||||
|
- data-root picker or command-line root selection
|
||||||
|
- per-user config/save directory
|
||||||
|
- release build profile
|
||||||
|
- crash logs
|
||||||
|
- user documentation
|
||||||
|
- optional quality-of-life settings that do not obscure parity bugs
|
||||||
|
|
||||||
|
Exit criteria: a clean checkout/build can run against a retail data directory with documented setup steps.
|
||||||
|
|
||||||
|
## Current Execution Focus
|
||||||
|
|
||||||
|
The current active phase is Phase 2, core resource loaders. The immediate path is `FIXED.DAT` map table and item-record loading, then glob expansion, then shape-frame decoding, then a minimal map scene smoke test.
|
||||||
|
|
||||||
|
## Import-Slice Tool
|
||||||
|
|
||||||
|
The first source-conversion helper is `rebuild/remorse/tools/import_export_slice.py`.
|
||||||
|
|
||||||
|
Its job is intentionally modest: copy one folder from `exports/src_remorse/functions/` into ignored staging output under `rebuild/remorse/imported/`, rewrite Ghidra's direct `CRUSADER.EXE.h` include to `remorse/export_prelude.h`, convert known 16-bit calling-convention tokens to compatibility macros, and write an import report listing cleanup signals such as `unaff_SS`, `unaff_CS`, `CONCAT22`, and hidden return-storage parameters.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
python rebuild/remorse/tools/import_export_slice.py classes/Remorse/EntityDispatchEntry
|
||||||
|
```
|
||||||
|
|
||||||
|
The generated staging files are not linked into `remorse_rebuild`. They are a workbench for turning one exported subsystem into hand-maintained source.
|
||||||
|
|
||||||
|
The first run against `classes/Remorse/EntityDispatchEntry` staged 24 files, replaced 62 calling-convention tokens, preserved 46 namespace-qualified references, and reported `CONCAT22`, `unaff_CS`, and `unaff_SS` as cleanup signals.
|
||||||
|
|
||||||
|
## Windows Runtime Work Needed
|
||||||
|
|
||||||
|
To run on Windows, the rebuild needs replacement layers for these original services:
|
||||||
|
|
||||||
|
- process startup and command-line/config parsing
|
||||||
|
- DOS file APIs and search paths
|
||||||
|
- Phar Lap / DOS extender memory services
|
||||||
|
- segmented far-pointer memory model, at least as a compatibility abstraction
|
||||||
|
- VGA/SVGA video mode setup, palette writes, bank switching, and blitting
|
||||||
|
- keyboard, mouse, and joystick input
|
||||||
|
- timer/tick scheduling
|
||||||
|
- audio device and mixer services
|
||||||
|
- movie playback and RIFF/audio frame handling
|
||||||
|
- save/load paths and user-writable data locations
|
||||||
|
- game data discovery for retail install layouts and repository fixtures
|
||||||
|
|
||||||
|
The first Windows runtime should prefer clear host abstractions over direct Win32 calls where possible. SDL or another portability layer is a reasonable later choice for graphics, input, timing, and audio, but that should be selected after the first resource-loading and render smoke tests are defined.
|
||||||
|
|
||||||
|
## Recommended First Subsystems
|
||||||
|
|
||||||
|
The first source slices should be small enough to compile and test without the entire game loop.
|
||||||
|
|
||||||
|
1. Compatibility/runtime layer: exact types, packed layout helpers, far-pointer wrappers, host file paths, and logging.
|
||||||
|
2. Resource/file loading helpers from the low segments, with DOS file calls replaced by host file services.
|
||||||
|
3. Palette and fixed-format data structures, validated against known assets.
|
||||||
|
4. `EntityDispatchEntry` as the first class-like family because it already has a named export folder and prior class-lifting notes.
|
||||||
|
5. Sprite/node or map rendering data structures after basic resource loading is stable.
|
||||||
|
|
||||||
|
## First Hand-Maintained Game Skeleton
|
||||||
|
|
||||||
|
The first manually maintained game-shaped source is `EntityDispatchEntry`.
|
||||||
|
|
||||||
|
Current evidence used:
|
||||||
|
|
||||||
|
- live export folder: `exports/src_remorse/functions/classes/Remorse/EntityDispatchEntry/`
|
||||||
|
- base initializer: `InitBase@11e0_0000.c`
|
||||||
|
- older note anchor recorded in the decompiler comment: `0008:ba00`
|
||||||
|
- allocation size visible in the decompiled initializer: `0x32` bytes
|
||||||
|
|
||||||
|
The current rebuild skeleton preserves the 0x32-byte object size and provides host-side stubs for base init, group-id update, source/item update, and event/proc-type update. The first confirmed offsets from the exported setter bodies are: source item at `+0x04`, event/proc type at `+0x06`, and group id in the low five bits at `+0x08`. These names are still conservative because the surrounding `Process` layout is not fully lifted into typed fields yet.
|
||||||
|
|
||||||
|
## First Windows Runtime Shim
|
||||||
|
|
||||||
|
The first platform-replacement layer is a host file-system shim.
|
||||||
|
|
||||||
|
Current behavior:
|
||||||
|
|
||||||
|
- inspects a candidate root for `CRUSADER.EXE`, `CRUSADER.CFG`, `STATIC`, and `USECODE`
|
||||||
|
- walks upward from the current directory until it finds a usable game-data root
|
||||||
|
- resolves game-data-relative paths case-insensitively for ASCII DOS-style names
|
||||||
|
- blocks parent traversal through `..`
|
||||||
|
- reads binary files into host byte buffers
|
||||||
|
- reports the root path from the host harness
|
||||||
|
- adds a CTest target that expects the repository root to contain the retail executable and data folders, then reads `STATIC/GAMEPAL.PAL`
|
||||||
|
|
||||||
|
This starts replacing the original DOS install-directory assumptions with explicit modern path discovery. It is intentionally narrow; file handles, writable save paths, and retail/install-layout variants are still to come.
|
||||||
|
|
||||||
|
## First Data Parser
|
||||||
|
|
||||||
|
The first parsed retail data asset is `STATIC/GAMEPAL.PAL`.
|
||||||
|
|
||||||
|
Current behavior:
|
||||||
|
|
||||||
|
- resolves `STATIC/GAMEPAL.PAL` through the host file-system shim
|
||||||
|
- requires exactly 768 bytes
|
||||||
|
- parses the file into 256 RGB triples
|
||||||
|
- reports the parsed color count from the host harness
|
||||||
|
- validates the path through `palette_tests`
|
||||||
|
|
||||||
|
This gives the future renderer a tiny but real data pipeline: host path discovery, binary load, format validation, and typed in-memory representation.
|
||||||
|
|
||||||
|
## First Archive Parser
|
||||||
|
|
||||||
|
The first archive parser handles the standard Crusader FLEX directory used by files such as `COMBAT.DAT`, `GLOB.FLX`, `SHAPES.FLX`, and `EUSECODE.FLX`.
|
||||||
|
|
||||||
|
Current behavior:
|
||||||
|
|
||||||
|
- reads the entry count at file offset `0x54`
|
||||||
|
- reads 8-byte directory rows at file offset `0x80`
|
||||||
|
- treats rows as little-endian `<u32 offset, u32 size>`
|
||||||
|
- skips empty rows and rejects out-of-file entries
|
||||||
|
- extracts individual populated entries by index
|
||||||
|
- validates `STATIC/COMBAT.DAT` as 14 populated records with entry 0 named `Dumb`
|
||||||
|
|
||||||
|
This is the first reusable loader shared by AI tactics, glob objects, shapes, and usecode archives.
|
||||||
|
|
||||||
|
## First Typed Archive Consumer
|
||||||
|
|
||||||
|
The first typed FLEX consumer is `COMBAT.DAT`.
|
||||||
|
|
||||||
|
Current behavior:
|
||||||
|
|
||||||
|
- loads the outer FLEX directory through `loadFlexArchive`
|
||||||
|
- parses each populated record's 16-byte NUL-padded tactic name
|
||||||
|
- reads four little-endian block offsets
|
||||||
|
- exposes the remaining bytes as tactic bytecode
|
||||||
|
- validates the shipped archive as 14 tactics, with entry 0 named `Dumb` and block offsets `0x002c` / `0x0031`
|
||||||
|
|
||||||
|
This is not yet the combat bytecode interpreter, but it establishes the pattern for moving from raw archive chunks to typed subsystem inputs.
|
||||||
|
|
||||||
|
## First Fixed-Map Loader
|
||||||
|
|
||||||
|
`FIXED.DAT` is now the next typed world-data input.
|
||||||
|
|
||||||
|
Current behavior:
|
||||||
|
|
||||||
|
- reads the map count at file offset `0x54`
|
||||||
|
- reads 8-byte map table rows at file offset `0x80`
|
||||||
|
- treats each row as little-endian `<u32 offset, u32 size>`
|
||||||
|
- parses 16-byte fixed-item records into typed fields
|
||||||
|
- applies the verified Crusader coordinate scale: disk `x/y * 2`
|
||||||
|
- validates all map table rows parse, at least one map is non-empty, and out-of-range map indices are rejected
|
||||||
|
|
||||||
|
This is the first step toward a host-side map scene assembled from retail data.
|
||||||
|
|
||||||
|
## First Glob Loader
|
||||||
|
|
||||||
|
`GLOB.FLX` is now parsed as the companion object-expansion archive for fixed maps.
|
||||||
|
|
||||||
|
Current behavior:
|
||||||
|
|
||||||
|
- loads the outer FLEX directory through `loadFlexArchive`
|
||||||
|
- reads each glob object as `u16 count` followed by repeated 6-byte entries
|
||||||
|
- parses each entry as `x:u8`, `y:u8`, `z:u8`, `shape:u16`, `frame:u8`
|
||||||
|
- validates the archive contains at least one glob object with entries
|
||||||
|
|
||||||
|
This prepares the next step: expanding fixed-map glob eggs into concrete world objects using the coordinate rules in `docs/map-rendering.md`.
|
||||||
|
|
||||||
|
## First Typeflag Loader
|
||||||
|
|
||||||
|
`TYPEFLAG.DAT` is now parsed as the shape-family metadata source needed by map expansion and rendering.
|
||||||
|
|
||||||
|
Current behavior:
|
||||||
|
|
||||||
|
- requires 9-byte records
|
||||||
|
- decodes the 20-bit typeflag field using the same byte layout as the Python map renderer
|
||||||
|
- decodes shape family, footpad dimensions, and animation type
|
||||||
|
- validates the shipped file contains 2,048 shape records and includes glob-family shapes
|
||||||
|
|
||||||
|
This is the shape metadata layer needed before fixed-map records can be classified as drawables, glob eggs, triggers, roofs, editor objects, or other family-specific runtime objects.
|
||||||
|
|
||||||
|
## First Shape Decoder
|
||||||
|
|
||||||
|
`SHAPES.FLX` is now parsed as the first renderable art source.
|
||||||
|
|
||||||
|
Current behavior:
|
||||||
|
|
||||||
|
- loads the outer FLEX directory through `loadFlexArchive`
|
||||||
|
- parses the Crusader shape header and 8-byte per-frame table
|
||||||
|
- reads 28-byte frame bodies, per-line RLE offsets, frame dimensions, and draw offsets
|
||||||
|
- decodes row RLE into transparent-or-palette-index pixels
|
||||||
|
- validates the shipped archive can decode at least one visible frame
|
||||||
|
|
||||||
|
This completes the remaining Phase 2 requirement needed for a minimal map image: fixed records, glob records, shape metadata, palette, and decoded shape pixels.
|
||||||
|
|
||||||
|
## First Visual Preview Renderer
|
||||||
|
|
||||||
|
Phase 3 now has its first concrete artifact: `remorse_map_preview` writes a 24-bit BMP preview using only the source-port rebuild code and the shipped retail data.
|
||||||
|
|
||||||
|
Current behavior:
|
||||||
|
|
||||||
|
- defaults to map 1, the first non-empty fixed-map table entry in this data set
|
||||||
|
- expands first-level glob eggs using `GLOB.FLX`
|
||||||
|
- filters hidden marker/egg/roof families by default, matching the offline map-rendering workbench's first-pass policy
|
||||||
|
- decodes visible `SHAPES.FLX` frames and paints them with `GAMEPAL.PAL`
|
||||||
|
- writes a dependency-free BMP image under `rebuild/remorse/out/preview/`
|
||||||
|
|
||||||
|
Validated preview:
|
||||||
|
|
||||||
|
- command: `rebuild\remorse\out\build\Debug\remorse_map_preview.exe rebuild\remorse\out\preview\map1-preview.bmp 1`
|
||||||
|
- output: `rebuild/remorse/out/preview/map1-preview.bmp`
|
||||||
|
- image size: 1600x900
|
||||||
|
- raw fixed records: 7,547
|
||||||
|
- render items after filters/globs: 15,318
|
||||||
|
- decoded items: 15,318
|
||||||
|
- painted items in the preview crop: 178
|
||||||
|
- full scene bounds: 30,977 x 15,617
|
||||||
|
|
||||||
|
This is still a smoke renderer, not the final game renderer. It uses a simple stable paint order rather than the full engine/item-sorter dependency graph, but it proves the rebuild tree can now load original resources and produce a visible world image.
|
||||||
|
|
||||||
|
After reviewing the first preview, the chosen next direction is render correctness: port fuller item sorting, improve roof/hidden-object policy, and add better crop/navigation controls before moving to an interactive runtime.
|
||||||
|
|
||||||
|
## Immediate Build Loop
|
||||||
|
|
||||||
|
The first chosen Windows toolchain is Visual Studio Build Tools plus CMake. That means the expected compiler is MSVC `cl.exe`, usually reached through a Developer PowerShell or Developer Command Prompt.
|
||||||
|
|
||||||
|
Until CMake and MSVC are available in PATH, the source scaffold can be edited and syntax-checked by VS Code, but it cannot be built locally from this shell.
|
||||||
|
|
||||||
|
Current local toolchain finding:
|
||||||
|
|
||||||
|
- Visual Studio Community 2026 is installed at `I:\Apps\VS\18\Community`.
|
||||||
|
- After installing the Desktop C++ workload, `vcvars64.bat`, MSVC `19.50.35729.0`, and VS-bundled CMake `4.2.3` are usable through the Visual Studio developer environment.
|
||||||
|
- Plain `cmake` is still not available before the Visual Studio environment is initialized, so `build_msvc.cmd` uses `vswhere` and `vcvars64.bat` before invoking CMake.
|
||||||
|
|
||||||
|
### Host Harness
|
||||||
|
|
||||||
|
From `rebuild/remorse/`, the intended host build loop is:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
cmake -S . -B out/build
|
||||||
|
cmake --build out/build
|
||||||
|
ctest --test-dir out/build -C Debug --output-on-failure
|
||||||
|
.\out\build\Debug\remorse_rebuild.exe ..\..\exports\src_remorse\export_manifest.json
|
||||||
|
```
|
||||||
|
|
||||||
|
For single-config generators, the executable may be under `out/build/remorse_rebuild.exe` instead of `out/build/Debug/`.
|
||||||
|
|
||||||
|
From the repository root, the repeatable MSVC path is:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
rebuild\remorse\build_msvc.cmd Debug
|
||||||
|
```
|
||||||
|
|
||||||
|
### Staging One Export Slice
|
||||||
|
|
||||||
|
From the repository root:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
python rebuild/remorse/tools/import_export_slice.py classes/Remorse/EntityDispatchEntry
|
||||||
|
```
|
||||||
|
|
||||||
|
## What Is Done
|
||||||
|
|
||||||
|
- Established a separate rebuild tree under `rebuild/remorse/`.
|
||||||
|
- Added a CMake-based C++17 executable target.
|
||||||
|
- Added an initial compatibility header matching the earlier ABI draft categories.
|
||||||
|
- Added a temporary export prelude for staged decompiler-source imports.
|
||||||
|
- Added a minimal runtime harness that reads the exported decompilation manifest and reports the current export scope.
|
||||||
|
- Added an import-slice tool for staging individual exported function folders without linking them yet.
|
||||||
|
- Ran the importer on `classes/Remorse/EntityDispatchEntry`; it staged 24 files, replaced 62 calling-convention tokens, and reported `CONCAT22`, `unaff_CS`, and `unaff_SS` as cleanup signals.
|
||||||
|
- Added the first hand-maintained `EntityDispatchEntry` source skeleton and wired it into the CMake target.
|
||||||
|
- Split the scaffold into a `remorse_core` library, host executable, and first CTest target for `EntityDispatchEntry` setters.
|
||||||
|
- Added the first host file-system shim and CTest target for game-data root discovery.
|
||||||
|
- Extended the host file-system shim with case-insensitive data-path resolution, parent-traversal blocking, and binary file reads.
|
||||||
|
- Added the first typed game-data parser for 256-color palette files and verified `STATIC/GAMEPAL.PAL` parses to 256 colors.
|
||||||
|
- Added a standard FLEX archive parser and verified `STATIC/COMBAT.DAT` exposes 14 populated records.
|
||||||
|
- Added a typed `COMBAT.DAT` tactic parser and verified entry 0 metadata against the existing combat note.
|
||||||
|
- Added a full staged rebuild plan from current harness through a playable and packaged modern Windows source port.
|
||||||
|
- Added a typed `FIXED.DAT` map loader for map table and 16-byte fixed-item records.
|
||||||
|
- Added a typed `GLOB.FLX` object parser for the fixed-map glob-expansion path.
|
||||||
|
- Added a typed `TYPEFLAG.DAT` parser for shape family, flags, dimensions, and animation metadata.
|
||||||
|
- Added a typed `SHAPES.FLX` parser and row-RLE decoder for renderable frame pixels.
|
||||||
|
- Added the first visual map preview renderer and BMP output command.
|
||||||
|
- Validated `remorse_map_preview` by rendering map 1 to `rebuild/remorse/out/preview/map1-preview.bmp` from original retail data.
|
||||||
|
- Verified the scaffold with Visual Studio Community 2026: CMake configure succeeded, MSVC build succeeded, `ctest -C Debug` passed 2/2 tests, and `remorse_rebuild.exe` ran against `exports/src_remorse/export_manifest.json`.
|
||||||
|
- Added `build_msvc.cmd` so the working build/test/harness loop is repeatable from the repository root.
|
||||||
|
- Documented the current blockers between the Ghidra export and compiler-ready source.
|
||||||
|
- Split the goal into historical reconstruction and Windows source-port tracks.
|
||||||
|
- Chose Visual Studio Build Tools plus CMake as the first intended Windows host toolchain.
|
||||||
|
|
||||||
|
## What Is Left To Do
|
||||||
|
|
||||||
|
- Keep the host scaffold building as new source slices are added.
|
||||||
|
- Lift the confirmed `EntityDispatchEntry` byte offsets into named packed fields once the surrounding `Process` layout is stable.
|
||||||
|
- Convert the compatibility header from placeholders into enforced layout macros with static assertions.
|
||||||
|
- Pick the first real subsystem slice and move it into `rebuild/remorse/`.
|
||||||
|
- Expand the host file-system shim into DOS file-handle replacements and writable save/config locations.
|
||||||
|
- Model far pointers and segmented memory in a way that can support both trace comparison and Windows execution.
|
||||||
|
- Normalize Ghidra exported types into valid C++ names without losing address provenance.
|
||||||
|
- Replace raw global-byte exports with typed data definitions or resource loaders.
|
||||||
|
- Define resource fixture tests for palette, shape, map, and usecode data.
|
||||||
|
- Replace the current preview painter's simple ordering with a fuller item-sorter dependency graph.
|
||||||
|
- Decide whether the first interactive window should use SDL, raw Win32/GDI, or another host layer.
|
||||||
|
- Decide the graphics/input/audio platform layer for the Windows runtime.
|
||||||
|
- Add CI-style smoke tests once the host toolchain and first resource loader compile reliably.
|
||||||
|
|
||||||
|
## Near-Term Next Actions
|
||||||
|
|
||||||
|
1. Open a Developer PowerShell with MSVC and CMake available, then run the CMake scaffold build.
|
||||||
|
2. Replace the current `EntityDispatchEntry` byte-array fields with named packed fields as evidence allows.
|
||||||
|
3. Add a small host-side test for the confirmed `+0x04`, `+0x06`, and `+0x08` setters once the MSVC/CMake toolchain is available.
|
||||||
|
4. Map the first old DOS file functions onto the new host file-system shim.
|
||||||
|
5. Add writable save/config path handling.
|
||||||
|
6. Add layout assertions for the first recovered packed structs.
|
||||||
|
7. Start a render/runtime decision note once the first resource smoke test passes.
|
||||||
|
|
||||||
|
## Current Bottom Line
|
||||||
|
|
||||||
|
The project is now at the start of the recompilation lane: there is a buildable host target scaffold, a compatibility surface, and a documented path for importing cleaned decompiler output. The major remaining work is systematic conversion, platform replacement, and behavior validation, not another bulk export.
|
||||||
144
rebuild/remorse/CMakeLists.txt
Normal file
144
rebuild/remorse/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,144 @@
|
||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
project(remorse_rebuild LANGUAGES CXX)
|
||||||
|
|
||||||
|
add_library(remorse_core
|
||||||
|
src/combat_tactics.cpp
|
||||||
|
src/entity_dispatch_entry.cpp
|
||||||
|
src/file_system.cpp
|
||||||
|
src/flex_archive.cpp
|
||||||
|
src/fixed_map.cpp
|
||||||
|
src/glob_archive.cpp
|
||||||
|
src/map_renderer.cpp
|
||||||
|
src/palette.cpp
|
||||||
|
src/runtime.cpp
|
||||||
|
src/shape_archive.cpp
|
||||||
|
src/type_flags.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_features(remorse_core PUBLIC cxx_std_17)
|
||||||
|
target_include_directories(remorse_core PUBLIC include)
|
||||||
|
|
||||||
|
add_executable(remorse_rebuild
|
||||||
|
src/main.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(remorse_rebuild PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_executable(remorse_map_preview
|
||||||
|
src/map_preview_main.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(remorse_map_preview PRIVATE remorse_core)
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_executable(entity_dispatch_entry_tests
|
||||||
|
tests/entity_dispatch_entry_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(entity_dispatch_entry_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME entity_dispatch_entry_tests COMMAND entity_dispatch_entry_tests)
|
||||||
|
|
||||||
|
add_executable(file_system_tests
|
||||||
|
tests/file_system_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(file_system_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME file_system_tests COMMAND file_system_tests)
|
||||||
|
|
||||||
|
add_executable(palette_tests
|
||||||
|
tests/palette_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(palette_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME palette_tests COMMAND palette_tests)
|
||||||
|
|
||||||
|
add_executable(flex_archive_tests
|
||||||
|
tests/flex_archive_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(flex_archive_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME flex_archive_tests COMMAND flex_archive_tests)
|
||||||
|
|
||||||
|
add_executable(fixed_map_tests
|
||||||
|
tests/fixed_map_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(fixed_map_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME fixed_map_tests COMMAND fixed_map_tests)
|
||||||
|
|
||||||
|
add_executable(glob_archive_tests
|
||||||
|
tests/glob_archive_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(glob_archive_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME glob_archive_tests COMMAND glob_archive_tests)
|
||||||
|
|
||||||
|
add_executable(type_flags_tests
|
||||||
|
tests/type_flags_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(type_flags_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME type_flags_tests COMMAND type_flags_tests)
|
||||||
|
|
||||||
|
add_executable(shape_archive_tests
|
||||||
|
tests/shape_archive_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(shape_archive_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME shape_archive_tests COMMAND shape_archive_tests)
|
||||||
|
|
||||||
|
add_executable(map_renderer_tests
|
||||||
|
tests/map_renderer_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(map_renderer_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME map_renderer_tests COMMAND map_renderer_tests)
|
||||||
|
|
||||||
|
add_executable(combat_tactics_tests
|
||||||
|
tests/combat_tactics_tests.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(combat_tactics_tests PRIVATE remorse_core)
|
||||||
|
|
||||||
|
add_test(NAME combat_tactics_tests COMMAND combat_tactics_tests)
|
||||||
|
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_options(remorse_core PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(combat_tactics_tests PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(map_renderer_tests PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(remorse_map_preview PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(remorse_rebuild PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(entity_dispatch_entry_tests PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(file_system_tests PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(flex_archive_tests PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(fixed_map_tests PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(glob_archive_tests PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(palette_tests PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(shape_archive_tests PRIVATE /W4 /permissive-)
|
||||||
|
target_compile_options(type_flags_tests PRIVATE /W4 /permissive-)
|
||||||
|
else()
|
||||||
|
target_compile_options(remorse_core PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(combat_tactics_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(map_renderer_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(remorse_map_preview PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(remorse_rebuild PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(entity_dispatch_entry_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(file_system_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(flex_archive_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(fixed_map_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(glob_archive_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(palette_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(shape_archive_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
target_compile_options(type_flags_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
|
endif()
|
||||||
43
rebuild/remorse/build_msvc.cmd
Normal file
43
rebuild/remorse/build_msvc.cmd
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
|
||||||
|
set "CONFIG=%~1"
|
||||||
|
if "%CONFIG%"=="" set "CONFIG=Debug"
|
||||||
|
|
||||||
|
set "REPO_ROOT=%~dp0..\.."
|
||||||
|
for %%I in ("%REPO_ROOT%") do set "REPO_ROOT=%%~fI"
|
||||||
|
|
||||||
|
set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
|
||||||
|
if not exist "%VSWHERE%" (
|
||||||
|
echo vswhere.exe was not found at "%VSWHERE%".
|
||||||
|
exit /b 2
|
||||||
|
)
|
||||||
|
|
||||||
|
for /f "usebackq delims=" %%I in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do set "VSINSTALL=%%I"
|
||||||
|
|
||||||
|
if "%VSINSTALL%"=="" (
|
||||||
|
echo Visual Studio with MSVC x64 tools was not found.
|
||||||
|
exit /b 2
|
||||||
|
)
|
||||||
|
|
||||||
|
set "VCVARS=%VSINSTALL%\VC\Auxiliary\Build\vcvars64.bat"
|
||||||
|
if not exist "%VCVARS%" (
|
||||||
|
echo vcvars64.bat was not found at "%VCVARS%".
|
||||||
|
exit /b 2
|
||||||
|
)
|
||||||
|
|
||||||
|
call "%VCVARS%" || exit /b 2
|
||||||
|
|
||||||
|
cd /d "%REPO_ROOT%" || exit /b 2
|
||||||
|
|
||||||
|
cmake -S rebuild/remorse -B rebuild/remorse/out/build || exit /b 1
|
||||||
|
cmake --build rebuild/remorse/out/build --config "%CONFIG%" || exit /b 1
|
||||||
|
ctest --test-dir rebuild/remorse/out/build -C "%CONFIG%" --output-on-failure || exit /b 1
|
||||||
|
|
||||||
|
set "EXE=rebuild\remorse\out\build\%CONFIG%\remorse_rebuild.exe"
|
||||||
|
if not exist "%EXE%" (
|
||||||
|
echo Built executable was not found at "%EXE%".
|
||||||
|
exit /b 1
|
||||||
|
)
|
||||||
|
|
||||||
|
"%EXE%" exports\src_remorse\export_manifest.json || exit /b 1
|
||||||
22
rebuild/remorse/include/remorse/combat_tactics.h
Normal file
22
rebuild/remorse/include/remorse/combat_tactics.h
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct CombatTactic {
|
||||||
|
u32 index = 0;
|
||||||
|
std::string name;
|
||||||
|
std::array<u16, 4> blockOffsets{};
|
||||||
|
std::vector<u8> bytecode;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<std::vector<CombatTactic>> loadCombatTactics(const std::filesystem::path& path);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
48
rebuild/remorse/include/remorse/compat.h
Normal file
48
rebuild/remorse/include/remorse/compat.h
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
using u8 = unsigned char;
|
||||||
|
using s8 = signed char;
|
||||||
|
using u16 = unsigned short;
|
||||||
|
using s16 = signed short;
|
||||||
|
using u32 = unsigned int;
|
||||||
|
using s32 = signed int;
|
||||||
|
|
||||||
|
static_assert(sizeof(u8) == 1, "u8 must be 8 bits");
|
||||||
|
static_assert(sizeof(u16) == 2, "u16 must be 16 bits");
|
||||||
|
static_assert(sizeof(u32) == 4, "u32 must be 32 bits");
|
||||||
|
|
||||||
|
struct FarPtr16 {
|
||||||
|
u16 offset;
|
||||||
|
u16 segment;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct FarPtr {
|
||||||
|
u16 offset;
|
||||||
|
u16 segment;
|
||||||
|
|
||||||
|
constexpr FarPtr16 raw() const noexcept
|
||||||
|
{
|
||||||
|
return FarPtr16{offset, segment};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#define CR_PACK_PUSH_1 __pragma(pack(push, 1))
|
||||||
|
#define CR_PACK_POP __pragma(pack(pop))
|
||||||
|
#define CR_PACKED
|
||||||
|
#else
|
||||||
|
#define CR_PACK_PUSH_1 _Pragma("pack(push, 1)")
|
||||||
|
#define CR_PACK_POP _Pragma("pack(pop)")
|
||||||
|
#define CR_PACKED __attribute__((packed))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define CR_CDECL
|
||||||
|
#define CR_STDCALL
|
||||||
|
#define CR_THISCALL
|
||||||
|
#define CR_FARCALL
|
||||||
|
#define CR_VSLOT(index)
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
31
rebuild/remorse/include/remorse/entity_dispatch_entry.h
Normal file
31
rebuild/remorse/include/remorse/entity_dispatch_entry.h
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
CR_PACK_PUSH_1
|
||||||
|
struct EntityDispatchEntryBase {
|
||||||
|
u8 bytes[0x32];
|
||||||
|
} CR_PACKED;
|
||||||
|
|
||||||
|
struct EntityDispatchEntryRuntimeState {
|
||||||
|
u8 bytes[0x18];
|
||||||
|
} CR_PACKED;
|
||||||
|
CR_PACK_POP
|
||||||
|
|
||||||
|
static_assert(sizeof(EntityDispatchEntryBase) == 0x32, "EntityDispatchEntryBase size must match allocation evidence");
|
||||||
|
|
||||||
|
class EntityDispatchEntry {
|
||||||
|
public:
|
||||||
|
static constexpr u16 initBaseSegment = 0x11e0;
|
||||||
|
static constexpr u16 initBaseOffset = 0x0000;
|
||||||
|
|
||||||
|
static EntityDispatchEntryBase* initBase(EntityDispatchEntryBase* entry);
|
||||||
|
static void setGroupId(EntityDispatchEntryBase& entry, u8 groupId);
|
||||||
|
static void incrementGroupId(EntityDispatchEntryBase& entry);
|
||||||
|
static void setSourceType(EntityDispatchEntryBase& entry, u16 sourceType);
|
||||||
|
static void setEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
20
rebuild/remorse/include/remorse/export_prelude.h
Normal file
20
rebuild/remorse/include/remorse/export_prelude.h
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
using remorse::s16;
|
||||||
|
using remorse::s32;
|
||||||
|
using remorse::s8;
|
||||||
|
using remorse::u16;
|
||||||
|
using remorse::u32;
|
||||||
|
using remorse::u8;
|
||||||
|
|
||||||
|
using undefined = u8;
|
||||||
|
using undefined1 = u8;
|
||||||
|
using undefined2 = u16;
|
||||||
|
using undefined4 = u32;
|
||||||
|
using byte = u8;
|
||||||
|
using word = u16;
|
||||||
|
using uint = unsigned int;
|
||||||
|
using ulong = unsigned long;
|
||||||
|
using dword = u32;
|
||||||
24
rebuild/remorse/include/remorse/file_system.h
Normal file
24
rebuild/remorse/include/remorse/file_system.h
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct GameDataRoot {
|
||||||
|
std::filesystem::path path;
|
||||||
|
bool hasExecutable = false;
|
||||||
|
bool hasConfig = false;
|
||||||
|
bool hasStaticData = false;
|
||||||
|
bool hasUsecode = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
GameDataRoot inspectGameDataRoot(const std::filesystem::path& root);
|
||||||
|
std::optional<GameDataRoot> findGameDataRoot(const std::filesystem::path& start);
|
||||||
|
std::optional<std::filesystem::path> resolveGameDataPath(const GameDataRoot& root, const std::filesystem::path& relativePath);
|
||||||
|
std::optional<std::vector<u8>> readBinaryFile(const std::filesystem::path& path);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
37
rebuild/remorse/include/remorse/fixed_map.h
Normal file
37
rebuild/remorse/include/remorse/fixed_map.h
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct FixedMapEntry {
|
||||||
|
u16 x = 0;
|
||||||
|
u16 y = 0;
|
||||||
|
u8 z = 0;
|
||||||
|
u16 shape = 0;
|
||||||
|
u8 frame = 0;
|
||||||
|
u16 flags = 0;
|
||||||
|
u16 quality = 0;
|
||||||
|
u8 npcNumber = 0;
|
||||||
|
u8 mapNumber = 0;
|
||||||
|
u16 next = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FixedMap {
|
||||||
|
u32 index = 0;
|
||||||
|
std::vector<FixedMapEntry> entries;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FixedMapArchive {
|
||||||
|
u32 mapCount = 0;
|
||||||
|
std::vector<u8> bytes;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<FixedMapArchive> loadFixedMapArchive(const std::filesystem::path& path);
|
||||||
|
std::optional<FixedMap> loadFixedMap(const FixedMapArchive& archive, u32 mapIndex);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
26
rebuild/remorse/include/remorse/flex_archive.h
Normal file
26
rebuild/remorse/include/remorse/flex_archive.h
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct FlexEntry {
|
||||||
|
u32 index = 0;
|
||||||
|
u32 offset = 0;
|
||||||
|
u32 size = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FlexArchive {
|
||||||
|
u32 entryCount = 0;
|
||||||
|
std::vector<FlexEntry> populatedEntries;
|
||||||
|
std::vector<u8> bytes;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<FlexArchive> loadFlexArchive(const std::filesystem::path& path);
|
||||||
|
std::optional<std::vector<u8>> readFlexEntry(const FlexArchive& archive, u32 index);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
26
rebuild/remorse/include/remorse/glob_archive.h
Normal file
26
rebuild/remorse/include/remorse/glob_archive.h
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct GlobObjectEntry {
|
||||||
|
u8 x = 0;
|
||||||
|
u8 y = 0;
|
||||||
|
u8 z = 0;
|
||||||
|
u16 shape = 0;
|
||||||
|
u8 frame = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GlobObject {
|
||||||
|
u32 index = 0;
|
||||||
|
std::vector<GlobObjectEntry> entries;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<std::vector<GlobObject>> loadGlobObjects(const std::filesystem::path& path);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
47
rebuild/remorse/include/remorse/map_renderer.h
Normal file
47
rebuild/remorse/include/remorse/map_renderer.h
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/palette.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct WorldRect {
|
||||||
|
s32 minX = 0;
|
||||||
|
s32 minY = 0;
|
||||||
|
s32 maxX = 0;
|
||||||
|
s32 maxY = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MapRenderOptions {
|
||||||
|
u32 mapIndex = 1;
|
||||||
|
u32 maxWidth = 1600;
|
||||||
|
u32 maxHeight = 900;
|
||||||
|
bool expandGlobs = true;
|
||||||
|
bool includeRoofs = false;
|
||||||
|
bool includeHiddenMarkers = false;
|
||||||
|
std::optional<WorldRect> worldRect;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RenderedMapImage {
|
||||||
|
u32 width = 0;
|
||||||
|
u32 height = 0;
|
||||||
|
u32 rawItemCount = 0;
|
||||||
|
u32 renderItemCount = 0;
|
||||||
|
u32 decodedItemCount = 0;
|
||||||
|
u32 paintedItemCount = 0;
|
||||||
|
s32 sourceLeft = 0;
|
||||||
|
s32 sourceTop = 0;
|
||||||
|
s32 fullWidth = 0;
|
||||||
|
s32 fullHeight = 0;
|
||||||
|
std::vector<RgbColor> pixels;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const MapRenderOptions& options);
|
||||||
|
bool writeBmp24(const std::filesystem::path& path, const RenderedMapImage& image);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
23
rebuild/remorse/include/remorse/palette.h
Normal file
23
rebuild/remorse/include/remorse/palette.h
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct RgbColor {
|
||||||
|
u8 red = 0;
|
||||||
|
u8 green = 0;
|
||||||
|
u8 blue = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Palette256 {
|
||||||
|
std::array<RgbColor, 256> colors{};
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<Palette256> loadPalette256(const std::filesystem::path& path);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
24
rebuild/remorse/include/remorse/runtime.h
Normal file
24
rebuild/remorse/include/remorse/runtime.h
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct ExportManifestSummary {
|
||||||
|
std::string program;
|
||||||
|
std::filesystem::path outputRoot;
|
||||||
|
u32 functionFiles = 0;
|
||||||
|
u32 fallbackFunctionFiles = 0;
|
||||||
|
u32 globalBuckets = 0;
|
||||||
|
u32 globalDefinitions = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<ExportManifestSummary> loadExportManifestSummary(const std::filesystem::path& manifestPath);
|
||||||
|
std::filesystem::path defaultManifestPath(const std::filesystem::path& currentPath);
|
||||||
|
void printStartupReport(const ExportManifestSummary& manifest);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
35
rebuild/remorse/include/remorse/shape_archive.h
Normal file
35
rebuild/remorse/include/remorse/shape_archive.h
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
#include "remorse/flex_archive.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct ShapeFrame {
|
||||||
|
bool compressed = false;
|
||||||
|
u32 width = 0;
|
||||||
|
u32 height = 0;
|
||||||
|
s32 xOffset = 0;
|
||||||
|
s32 yOffset = 0;
|
||||||
|
std::vector<u32> lineOffsets;
|
||||||
|
std::vector<u8> rleData;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DecodedShapeFrame {
|
||||||
|
ShapeFrame frame;
|
||||||
|
std::vector<s16> pixels;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ShapeArchive {
|
||||||
|
FlexArchive archive;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<ShapeArchive> loadShapeArchive(const std::filesystem::path& path);
|
||||||
|
std::optional<std::vector<ShapeFrame>> loadShapeFrames(const ShapeArchive& archive, u32 shapeIndex);
|
||||||
|
std::optional<DecodedShapeFrame> decodeShapeFrame(const ShapeArchive& archive, u32 shapeIndex, u32 frameIndex);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
22
rebuild/remorse/include/remorse/type_flags.h
Normal file
22
rebuild/remorse/include/remorse/type_flags.h
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "remorse/compat.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
struct ShapeTypeInfo {
|
||||||
|
u8 family = 0;
|
||||||
|
u32 flags = 0;
|
||||||
|
u8 x = 0;
|
||||||
|
u8 y = 0;
|
||||||
|
u8 z = 0;
|
||||||
|
u8 animationType = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<std::vector<ShapeTypeInfo>> loadTypeFlags(const std::filesystem::path& path);
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
58
rebuild/remorse/src/combat_tactics.cpp
Normal file
58
rebuild/remorse/src/combat_tactics.cpp
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
#include "remorse/combat_tactics.h"
|
||||||
|
|
||||||
|
#include "remorse/flex_archive.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constexpr u32 tacticNameSize = 16;
|
||||||
|
constexpr u32 tacticBlockOffsetCount = 4;
|
||||||
|
constexpr u32 tacticHeaderSize = tacticNameSize + tacticBlockOffsetCount * 2;
|
||||||
|
|
||||||
|
u16 readU16Le(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string readNullPaddedName(const std::vector<u8>& bytes)
|
||||||
|
{
|
||||||
|
std::string name;
|
||||||
|
for (u32 index = 0; index < tacticNameSize; ++index) {
|
||||||
|
if (bytes[index] == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
name.push_back(static_cast<char>(bytes[index]));
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::optional<std::vector<CombatTactic>> loadCombatTactics(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
const auto archive = loadFlexArchive(path);
|
||||||
|
if (!archive) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<CombatTactic> tactics;
|
||||||
|
for (const FlexEntry& entry : archive->populatedEntries) {
|
||||||
|
const auto entryBytes = readFlexEntry(*archive, entry.index);
|
||||||
|
if (!entryBytes || entryBytes->size() < tacticHeaderSize) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
CombatTactic tactic;
|
||||||
|
tactic.index = entry.index;
|
||||||
|
tactic.name = readNullPaddedName(*entryBytes);
|
||||||
|
for (u32 offsetIndex = 0; offsetIndex < tacticBlockOffsetCount; ++offsetIndex) {
|
||||||
|
tactic.blockOffsets[offsetIndex] = readU16Le(*entryBytes, tacticNameSize + offsetIndex * 2);
|
||||||
|
}
|
||||||
|
tactic.bytecode.assign(entryBytes->begin() + tacticHeaderSize, entryBytes->end());
|
||||||
|
tactics.push_back(tactic);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tactics;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
49
rebuild/remorse/src/entity_dispatch_entry.cpp
Normal file
49
rebuild/remorse/src/entity_dispatch_entry.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
#include "remorse/entity_dispatch_entry.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
constexpr u16 sourceItemOffset = 0x04;
|
||||||
|
constexpr u16 eventTypeOffset = 0x06;
|
||||||
|
constexpr u16 groupIdOffset = 0x08;
|
||||||
|
|
||||||
|
void writeLe16(EntityDispatchEntryBase& entry, u16 offset, u16 value)
|
||||||
|
{
|
||||||
|
entry.bytes[offset] = static_cast<u8>(value & 0xff);
|
||||||
|
entry.bytes[offset + 1] = static_cast<u8>(value >> 8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EntityDispatchEntryBase* EntityDispatchEntry::initBase(EntityDispatchEntryBase* entry)
|
||||||
|
{
|
||||||
|
if (entry == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u8& byte : entry->bytes) {
|
||||||
|
byte = 0;
|
||||||
|
}
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityDispatchEntry::setGroupId(EntityDispatchEntryBase& entry, u8 groupId)
|
||||||
|
{
|
||||||
|
entry.bytes[groupIdOffset] = static_cast<u8>((entry.bytes[groupIdOffset] & 0xe0) | (groupId & 0x1f));
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityDispatchEntry::incrementGroupId(EntityDispatchEntryBase& entry)
|
||||||
|
{
|
||||||
|
++entry.bytes[groupIdOffset];
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityDispatchEntry::setSourceType(EntityDispatchEntryBase& entry, u16 sourceType)
|
||||||
|
{
|
||||||
|
writeLe16(entry, sourceItemOffset, sourceType);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityDispatchEntry::setEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType)
|
||||||
|
{
|
||||||
|
writeLe16(entry, eventTypeOffset, eventType);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
145
rebuild/remorse/src/file_system.cpp
Normal file
145
rebuild/remorse/src/file_system.cpp
Normal file
|
|
@ -0,0 +1,145 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
bool pathExists(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
return std::filesystem::exists(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::filesystem::path> parentPath(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
const std::filesystem::path parent = path.parent_path();
|
||||||
|
if (parent.empty() || parent == path) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
char toLowerAscii(char value)
|
||||||
|
{
|
||||||
|
if (value < 'A' || value > 'Z') {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return static_cast<char>(value - 'A' + 'a');
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string lowerAscii(std::string value)
|
||||||
|
{
|
||||||
|
for (char& character : value) {
|
||||||
|
character = toLowerAscii(character);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool equalAsciiCaseInsensitive(const std::filesystem::path& left, const std::filesystem::path& right)
|
||||||
|
{
|
||||||
|
return lowerAscii(left.filename().string()) == lowerAscii(right.filename().string());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::filesystem::path> resolveChildCaseInsensitive(const std::filesystem::path& parent, const std::filesystem::path& child)
|
||||||
|
{
|
||||||
|
const std::filesystem::path exact = parent / child;
|
||||||
|
if (pathExists(exact)) {
|
||||||
|
return exact;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!std::filesystem::is_directory(parent)) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator(parent)) {
|
||||||
|
if (equalAsciiCaseInsensitive(entry.path(), child)) {
|
||||||
|
return entry.path();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
GameDataRoot inspectGameDataRoot(const std::filesystem::path& root)
|
||||||
|
{
|
||||||
|
GameDataRoot result;
|
||||||
|
result.path = root;
|
||||||
|
result.hasExecutable = pathExists(root / "CRUSADER.EXE");
|
||||||
|
result.hasConfig = pathExists(root / "CRUSADER.CFG");
|
||||||
|
result.hasStaticData = pathExists(root / "STATIC");
|
||||||
|
result.hasUsecode = pathExists(root / "USECODE");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<GameDataRoot> findGameDataRoot(const std::filesystem::path& start)
|
||||||
|
{
|
||||||
|
std::filesystem::path cursor = std::filesystem::absolute(start);
|
||||||
|
if (std::filesystem::is_regular_file(cursor)) {
|
||||||
|
cursor = cursor.parent_path();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
GameDataRoot candidate = inspectGameDataRoot(cursor);
|
||||||
|
if (candidate.hasExecutable && candidate.hasStaticData && candidate.hasUsecode) {
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto parent = parentPath(cursor);
|
||||||
|
if (!parent) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
cursor = *parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::filesystem::path> resolveGameDataPath(const GameDataRoot& root, const std::filesystem::path& relativePath)
|
||||||
|
{
|
||||||
|
if (relativePath.empty() || relativePath.is_absolute()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::filesystem::path cursor = root.path;
|
||||||
|
for (const std::filesystem::path& component : relativePath) {
|
||||||
|
if (component == ".") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (component == "..") {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto next = resolveChildCaseInsensitive(cursor, component);
|
||||||
|
if (!next) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
cursor = *next;
|
||||||
|
}
|
||||||
|
return cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::vector<u8>> readBinaryFile(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
std::ifstream input(path, std::ios::binary);
|
||||||
|
if (!input) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
input.seekg(0, std::ios::end);
|
||||||
|
const std::streamoff size = input.tellg();
|
||||||
|
if (size < 0) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
input.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
std::vector<u8> bytes(static_cast<std::size_t>(size));
|
||||||
|
if (!bytes.empty()) {
|
||||||
|
input.read(reinterpret_cast<char*>(bytes.data()), size);
|
||||||
|
if (!input) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
119
rebuild/remorse/src/fixed_map.cpp
Normal file
119
rebuild/remorse/src/fixed_map.cpp
Normal file
|
|
@ -0,0 +1,119 @@
|
||||||
|
#include "remorse/fixed_map.h"
|
||||||
|
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constexpr u32 fixedMapCountOffset = 0x54;
|
||||||
|
constexpr u32 fixedMapTableOffset = 0x80;
|
||||||
|
constexpr u32 fixedMapTableRowSize = 8;
|
||||||
|
constexpr u32 fixedMapRecordSize = 16;
|
||||||
|
constexpr u16 crusaderCoordinateScale = 2;
|
||||||
|
|
||||||
|
std::optional<u16> readU16Le(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
if (offset + 2 > bytes.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<u32> readU32Le(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
if (offset + 4 > bytes.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return static_cast<u32>(bytes[offset]) |
|
||||||
|
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
||||||
|
(static_cast<u32>(bytes[offset + 2]) << 16) |
|
||||||
|
(static_cast<u32>(bytes[offset + 3]) << 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<FixedMapEntry> readFixedMapEntry(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
if (offset + fixedMapRecordSize > bytes.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto diskX = readU16Le(bytes, offset);
|
||||||
|
const auto diskY = readU16Le(bytes, offset + 2);
|
||||||
|
const auto shape = readU16Le(bytes, offset + 5);
|
||||||
|
const auto flags = readU16Le(bytes, offset + 8);
|
||||||
|
const auto quality = readU16Le(bytes, offset + 10);
|
||||||
|
const auto next = readU16Le(bytes, offset + 14);
|
||||||
|
if (!diskX || !diskY || !shape || !flags || !quality || !next) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
FixedMapEntry entry;
|
||||||
|
entry.x = static_cast<u16>(*diskX * crusaderCoordinateScale);
|
||||||
|
entry.y = static_cast<u16>(*diskY * crusaderCoordinateScale);
|
||||||
|
entry.z = bytes[offset + 4];
|
||||||
|
entry.shape = *shape;
|
||||||
|
entry.frame = bytes[offset + 7];
|
||||||
|
entry.flags = *flags;
|
||||||
|
entry.quality = *quality;
|
||||||
|
entry.npcNumber = bytes[offset + 12];
|
||||||
|
entry.mapNumber = bytes[offset + 13];
|
||||||
|
entry.next = *next;
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::optional<FixedMapArchive> loadFixedMapArchive(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
auto bytes = readBinaryFile(path);
|
||||||
|
if (!bytes) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto mapCount = readU16Le(*bytes, fixedMapCountOffset);
|
||||||
|
if (!mapCount) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const u32 tableEnd = fixedMapTableOffset + (static_cast<u32>(*mapCount) * fixedMapTableRowSize);
|
||||||
|
if (tableEnd > bytes->size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
FixedMapArchive archive;
|
||||||
|
archive.mapCount = *mapCount;
|
||||||
|
archive.bytes = *bytes;
|
||||||
|
return archive;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<FixedMap> loadFixedMap(const FixedMapArchive& archive, u32 mapIndex)
|
||||||
|
{
|
||||||
|
if (mapIndex >= archive.mapCount) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const u32 tableOffset = fixedMapTableOffset + (mapIndex * fixedMapTableRowSize);
|
||||||
|
const auto mapOffset = readU32Le(archive.bytes, tableOffset);
|
||||||
|
const auto mapSize = readU32Le(archive.bytes, tableOffset + 4);
|
||||||
|
if (!mapOffset || !mapSize) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
if (*mapOffset > archive.bytes.size() || *mapSize > archive.bytes.size() - *mapOffset) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
if ((*mapSize % fixedMapRecordSize) != 0) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
FixedMap map;
|
||||||
|
map.index = mapIndex;
|
||||||
|
for (u32 offset = *mapOffset; offset < *mapOffset + *mapSize; offset += fixedMapRecordSize) {
|
||||||
|
const auto entry = readFixedMapEntry(archive.bytes, offset);
|
||||||
|
if (!entry) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
map.entries.push_back(*entry);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
83
rebuild/remorse/src/flex_archive.cpp
Normal file
83
rebuild/remorse/src/flex_archive.cpp
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
#include "remorse/flex_archive.h"
|
||||||
|
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constexpr u32 flexCountOffset = 0x54;
|
||||||
|
constexpr u32 flexTableOffset = 0x80;
|
||||||
|
constexpr u32 flexEntrySize = 8;
|
||||||
|
|
||||||
|
std::optional<u32> readU32Le(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
if (offset + 4 > bytes.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return static_cast<u32>(bytes[offset]) |
|
||||||
|
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
||||||
|
(static_cast<u32>(bytes[offset + 2]) << 16) |
|
||||||
|
(static_cast<u32>(bytes[offset + 3]) << 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::optional<FlexArchive> loadFlexArchive(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
auto bytes = readBinaryFile(path);
|
||||||
|
if (!bytes) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto entryCount = readU32Le(*bytes, flexCountOffset);
|
||||||
|
if (!entryCount) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const u32 tableEnd = flexTableOffset + (*entryCount * flexEntrySize);
|
||||||
|
if (tableEnd > bytes->size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
FlexArchive archive;
|
||||||
|
archive.entryCount = *entryCount;
|
||||||
|
archive.bytes = *bytes;
|
||||||
|
|
||||||
|
for (u32 index = 0; index < archive.entryCount; ++index) {
|
||||||
|
const u32 tableOffset = flexTableOffset + (index * flexEntrySize);
|
||||||
|
const auto dataOffset = readU32Le(archive.bytes, tableOffset);
|
||||||
|
const auto dataSize = readU32Le(archive.bytes, tableOffset + 4);
|
||||||
|
if (!dataOffset || !dataSize) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*dataOffset == 0 && *dataSize == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (*dataOffset == 0 || *dataSize == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (*dataOffset > archive.bytes.size() || *dataSize > archive.bytes.size() - *dataOffset) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
archive.populatedEntries.push_back(FlexEntry{index, *dataOffset, *dataSize});
|
||||||
|
}
|
||||||
|
|
||||||
|
return archive;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::vector<u8>> readFlexEntry(const FlexArchive& archive, u32 index)
|
||||||
|
{
|
||||||
|
for (const FlexEntry& entry : archive.populatedEntries) {
|
||||||
|
if (entry.index == index) {
|
||||||
|
const auto begin = archive.bytes.begin() + entry.offset;
|
||||||
|
const auto end = begin + entry.size;
|
||||||
|
return std::vector<u8>(begin, end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
56
rebuild/remorse/src/glob_archive.cpp
Normal file
56
rebuild/remorse/src/glob_archive.cpp
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
#include "remorse/glob_archive.h"
|
||||||
|
|
||||||
|
#include "remorse/flex_archive.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constexpr u32 globCountSize = 2;
|
||||||
|
constexpr u32 globEntrySize = 6;
|
||||||
|
|
||||||
|
u16 readU16Le(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::optional<std::vector<GlobObject>> loadGlobObjects(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
const auto archive = loadFlexArchive(path);
|
||||||
|
if (!archive) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<GlobObject> objects;
|
||||||
|
for (const FlexEntry& flexEntry : archive->populatedEntries) {
|
||||||
|
const auto bytes = readFlexEntry(*archive, flexEntry.index);
|
||||||
|
if (!bytes || bytes->size() < globCountSize) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const u16 entryCount = readU16Le(*bytes, 0);
|
||||||
|
const u32 expectedSize = globCountSize + static_cast<u32>(entryCount) * globEntrySize;
|
||||||
|
if (expectedSize > bytes->size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
GlobObject object;
|
||||||
|
object.index = flexEntry.index;
|
||||||
|
for (u32 index = 0; index < entryCount; ++index) {
|
||||||
|
const u32 offset = globCountSize + index * globEntrySize;
|
||||||
|
GlobObjectEntry entry;
|
||||||
|
entry.x = (*bytes)[offset];
|
||||||
|
entry.y = (*bytes)[offset + 1];
|
||||||
|
entry.z = (*bytes)[offset + 2];
|
||||||
|
entry.shape = readU16Le(*bytes, offset + 3);
|
||||||
|
entry.frame = (*bytes)[offset + 5];
|
||||||
|
object.entries.push_back(entry);
|
||||||
|
}
|
||||||
|
objects.push_back(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
return objects;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
69
rebuild/remorse/src/main.cpp
Normal file
69
rebuild/remorse/src/main.cpp
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
#include "remorse/combat_tactics.h"
|
||||||
|
#include "remorse/entity_dispatch_entry.h"
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/flex_archive.h"
|
||||||
|
#include "remorse/fixed_map.h"
|
||||||
|
#include "remorse/glob_archive.h"
|
||||||
|
#include "remorse/palette.h"
|
||||||
|
#include "remorse/runtime.h"
|
||||||
|
#include "remorse/type_flags.h"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
const std::filesystem::path manifestPath = argc > 1
|
||||||
|
? std::filesystem::path(argv[1])
|
||||||
|
: remorse::defaultManifestPath(std::filesystem::current_path());
|
||||||
|
|
||||||
|
const auto manifest = remorse::loadExportManifestSummary(manifestPath);
|
||||||
|
if (!manifest) {
|
||||||
|
std::cerr << "Could not read export manifest: " << manifestPath.string() << '\n';
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
remorse::printStartupReport(*manifest);
|
||||||
|
remorse::EntityDispatchEntryBase entry{};
|
||||||
|
remorse::EntityDispatchEntry::initBase(&entry);
|
||||||
|
remorse::EntityDispatchEntry::setGroupId(entry, 1);
|
||||||
|
|
||||||
|
const auto gameDataRoot = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
std::cout << "Game data root: " << (gameDataRoot ? gameDataRoot->path.string() : "not found") << '\n';
|
||||||
|
if (gameDataRoot) {
|
||||||
|
const auto palettePath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "GAMEPAL.PAL");
|
||||||
|
const auto paletteBytes = palettePath ? remorse::readBinaryFile(*palettePath) : std::nullopt;
|
||||||
|
std::cout << "GAMEPAL.PAL bytes: " << (paletteBytes ? paletteBytes->size() : 0) << '\n';
|
||||||
|
const auto palette = palettePath ? remorse::loadPalette256(*palettePath) : std::nullopt;
|
||||||
|
std::cout << "GAMEPAL.PAL colors: " << (palette ? palette->colors.size() : 0) << '\n';
|
||||||
|
|
||||||
|
const auto combatPath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "COMBAT.DAT");
|
||||||
|
const auto combatArchive = combatPath ? remorse::loadFlexArchive(*combatPath) : std::nullopt;
|
||||||
|
std::cout << "COMBAT.DAT populated entries: " << (combatArchive ? combatArchive->populatedEntries.size() : 0) << '\n';
|
||||||
|
const auto combatTactics = combatPath ? remorse::loadCombatTactics(*combatPath) : std::nullopt;
|
||||||
|
std::cout << "COMBAT.DAT tactics: " << (combatTactics ? combatTactics->size() : 0) << '\n';
|
||||||
|
|
||||||
|
const auto fixedPath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "FIXED.DAT");
|
||||||
|
const auto fixedArchive = fixedPath ? remorse::loadFixedMapArchive(*fixedPath) : std::nullopt;
|
||||||
|
std::cout << "FIXED.DAT maps: " << (fixedArchive ? fixedArchive->mapCount : 0) << '\n';
|
||||||
|
if (fixedArchive) {
|
||||||
|
for (remorse::u32 index = 0; index < fixedArchive->mapCount; ++index) {
|
||||||
|
const auto map = remorse::loadFixedMap(*fixedArchive, index);
|
||||||
|
if (map && !map->entries.empty()) {
|
||||||
|
std::cout << "FIXED.DAT first non-empty map: " << index << '\n';
|
||||||
|
std::cout << "FIXED.DAT first non-empty entries: " << map->entries.size() << '\n';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto globPath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "GLOB.FLX");
|
||||||
|
const auto globObjects = globPath ? remorse::loadGlobObjects(*globPath) : std::nullopt;
|
||||||
|
std::cout << "GLOB.FLX objects: " << (globObjects ? globObjects->size() : 0) << '\n';
|
||||||
|
|
||||||
|
const auto typeFlagPath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
||||||
|
const auto typeFlags = typeFlagPath ? remorse::loadTypeFlags(*typeFlagPath) : std::nullopt;
|
||||||
|
std::cout << "TYPEFLAG.DAT shapes: " << (typeFlags ? typeFlags->size() : 0) << '\n';
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
51
rebuild/remorse/src/map_preview_main.cpp
Normal file
51
rebuild/remorse/src/map_preview_main.cpp
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/map_renderer.h"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
if (!root) {
|
||||||
|
std::cerr << "Could not locate Crusader game data root.\n";
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
remorse::MapRenderOptions options;
|
||||||
|
std::filesystem::path outputPath = std::filesystem::path("rebuild") / "remorse" / "out" / "preview" / "map-preview.bmp";
|
||||||
|
if (argc > 1) {
|
||||||
|
outputPath = argv[1];
|
||||||
|
}
|
||||||
|
if (argc > 2) {
|
||||||
|
options.mapIndex = static_cast<remorse::u32>(std::stoul(argv[2], nullptr, 0));
|
||||||
|
}
|
||||||
|
if (argc == 7) {
|
||||||
|
options.worldRect = remorse::WorldRect{
|
||||||
|
std::stoi(argv[3], nullptr, 0),
|
||||||
|
std::stoi(argv[4], nullptr, 0),
|
||||||
|
std::stoi(argv[5], nullptr, 0),
|
||||||
|
std::stoi(argv[6], nullptr, 0)};
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto image = remorse::renderMapPreview(*root, options);
|
||||||
|
if (!image) {
|
||||||
|
std::cerr << "Could not render map preview.\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (!remorse::writeBmp24(outputPath, *image)) {
|
||||||
|
std::cerr << "Could not write preview BMP: " << outputPath.string() << '\n';
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Preview: " << outputPath.string() << '\n';
|
||||||
|
std::cout << "Map: " << options.mapIndex << '\n';
|
||||||
|
std::cout << "Raw fixed items: " << image->rawItemCount << '\n';
|
||||||
|
std::cout << "Render items after filters/globs: " << image->renderItemCount << '\n';
|
||||||
|
std::cout << "Decoded items: " << image->decodedItemCount << '\n';
|
||||||
|
std::cout << "Painted items in crop: " << image->paintedItemCount << '\n';
|
||||||
|
std::cout << "Image size: " << image->width << "x" << image->height << '\n';
|
||||||
|
std::cout << "Full scene bounds: " << image->fullWidth << "x" << image->fullHeight << '\n';
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
362
rebuild/remorse/src/map_renderer.cpp
Normal file
362
rebuild/remorse/src/map_renderer.cpp
Normal file
|
|
@ -0,0 +1,362 @@
|
||||||
|
#include "remorse/map_renderer.h"
|
||||||
|
|
||||||
|
#include "remorse/fixed_map.h"
|
||||||
|
#include "remorse/glob_archive.h"
|
||||||
|
#include "remorse/shape_archive.h"
|
||||||
|
#include "remorse/type_flags.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <fstream>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constexpr u16 flagInvisible = 0x0010;
|
||||||
|
constexpr u16 flagFlipped = 0x0020;
|
||||||
|
constexpr u32 siRoof = 0x0400;
|
||||||
|
constexpr s32 globCoordMask = ~0x3ff;
|
||||||
|
constexpr s32 globCoordShift = 2;
|
||||||
|
constexpr s32 globCoordOffset = 2;
|
||||||
|
|
||||||
|
struct SceneItem {
|
||||||
|
s32 x = 0;
|
||||||
|
s32 y = 0;
|
||||||
|
s32 z = 0;
|
||||||
|
u16 shape = 0;
|
||||||
|
u8 frame = 0;
|
||||||
|
u16 flags = 0;
|
||||||
|
u16 quality = 0;
|
||||||
|
u8 mapNumber = 0;
|
||||||
|
bool fromFixed = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RenderNode {
|
||||||
|
SceneItem item;
|
||||||
|
DecodedShapeFrame decoded;
|
||||||
|
s32 left = 0;
|
||||||
|
s32 top = 0;
|
||||||
|
s32 right = 0;
|
||||||
|
s32 bottom = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool isEggFamily(u8 family)
|
||||||
|
{
|
||||||
|
return family == 3 || family == 4 || family == 7 || family == 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool inWorldRect(const SceneItem& item, const std::optional<WorldRect>& rect)
|
||||||
|
{
|
||||||
|
if (!rect) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return item.x >= rect->minX && item.x <= rect->maxX && item.y >= rect->minY && item.y <= rect->maxY;
|
||||||
|
}
|
||||||
|
|
||||||
|
RgbColor scalePaletteColor(const RgbColor& color)
|
||||||
|
{
|
||||||
|
RgbColor scaled;
|
||||||
|
scaled.red = static_cast<u8>((static_cast<u32>(color.red) * 255) / 63);
|
||||||
|
scaled.green = static_cast<u8>((static_cast<u32>(color.green) * 255) / 63);
|
||||||
|
scaled.blue = static_cast<u8>((static_cast<u32>(color.blue) * 255) / 63);
|
||||||
|
return scaled;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<GlobObject> findGlobObject(const std::vector<GlobObject>& globs, u32 index)
|
||||||
|
{
|
||||||
|
for (const GlobObject& object : globs) {
|
||||||
|
if (object.index == index) {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<SceneItem> collectSceneItems(
|
||||||
|
const FixedMap& fixedMap,
|
||||||
|
const std::vector<ShapeTypeInfo>& typeFlags,
|
||||||
|
const std::vector<GlobObject>& globs,
|
||||||
|
const MapRenderOptions& options)
|
||||||
|
{
|
||||||
|
std::vector<SceneItem> pending;
|
||||||
|
pending.reserve(fixedMap.entries.size());
|
||||||
|
for (const FixedMapEntry& entry : fixedMap.entries) {
|
||||||
|
pending.push_back(SceneItem{
|
||||||
|
static_cast<s32>(entry.x),
|
||||||
|
static_cast<s32>(entry.y),
|
||||||
|
static_cast<s32>(entry.z),
|
||||||
|
entry.shape,
|
||||||
|
entry.frame,
|
||||||
|
entry.flags,
|
||||||
|
entry.quality,
|
||||||
|
entry.mapNumber,
|
||||||
|
true});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<SceneItem> items;
|
||||||
|
for (std::size_t index = 0; index < pending.size(); ++index) {
|
||||||
|
const SceneItem item = pending[index];
|
||||||
|
if (!inWorldRect(item, options.worldRect)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (item.shape >= typeFlags.size()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const ShapeTypeInfo& info = typeFlags[item.shape];
|
||||||
|
if ((item.flags & flagInvisible) != 0 && !options.includeHiddenMarkers) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ((info.flags & siRoof) != 0 && !options.includeRoofs) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (options.expandGlobs && info.family == 3 && item.fromFixed) {
|
||||||
|
const auto glob = findGlobObject(globs, item.quality);
|
||||||
|
if (glob) {
|
||||||
|
for (const GlobObjectEntry& entry : glob->entries) {
|
||||||
|
pending.push_back(SceneItem{
|
||||||
|
(item.x & globCoordMask) + (static_cast<s32>(entry.x) << globCoordShift) + globCoordOffset,
|
||||||
|
(item.y & globCoordMask) + (static_cast<s32>(entry.y) << globCoordShift) + globCoordOffset,
|
||||||
|
item.z + entry.z,
|
||||||
|
entry.shape,
|
||||||
|
entry.frame,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
item.mapNumber,
|
||||||
|
false});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!options.includeHiddenMarkers) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isEggFamily(info.family) && !options.includeHiddenMarkers) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
items.push_back(item);
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
s32 screenX(const SceneItem& item)
|
||||||
|
{
|
||||||
|
return (item.x - item.y) / 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
s32 screenY(const SceneItem& item)
|
||||||
|
{
|
||||||
|
return (item.x + item.y) / 8 - item.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<DecodedShapeFrame> cachedDecode(
|
||||||
|
const ShapeArchive& archive,
|
||||||
|
std::map<std::pair<u32, u32>, DecodedShapeFrame>& cache,
|
||||||
|
u32 shape,
|
||||||
|
u32 frame)
|
||||||
|
{
|
||||||
|
const auto key = std::make_pair(shape, frame);
|
||||||
|
const auto found = cache.find(key);
|
||||||
|
if (found != cache.end()) {
|
||||||
|
return found->second;
|
||||||
|
}
|
||||||
|
const auto decoded = decodeShapeFrame(archive, shape, frame);
|
||||||
|
if (!decoded) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
cache.emplace(key, *decoded);
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<RenderNode> prepareRenderNodes(const std::vector<SceneItem>& items, const ShapeArchive& shapeArchive)
|
||||||
|
{
|
||||||
|
std::map<std::pair<u32, u32>, DecodedShapeFrame> cache;
|
||||||
|
std::vector<RenderNode> nodes;
|
||||||
|
for (const SceneItem& item : items) {
|
||||||
|
const auto decoded = cachedDecode(shapeArchive, cache, item.shape, item.frame);
|
||||||
|
if (!decoded) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const s32 bottomX = screenX(item);
|
||||||
|
const s32 bottomY = screenY(item);
|
||||||
|
const bool flipped = (item.flags & flagFlipped) != 0;
|
||||||
|
RenderNode node;
|
||||||
|
node.item = item;
|
||||||
|
node.decoded = *decoded;
|
||||||
|
node.left = flipped
|
||||||
|
? bottomX + node.decoded.frame.xOffset - static_cast<s32>(node.decoded.frame.width)
|
||||||
|
: bottomX - node.decoded.frame.xOffset;
|
||||||
|
node.top = bottomY - node.decoded.frame.yOffset;
|
||||||
|
node.right = node.left + static_cast<s32>(node.decoded.frame.width);
|
||||||
|
node.bottom = node.top + static_cast<s32>(node.decoded.frame.height);
|
||||||
|
nodes.push_back(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(nodes.begin(), nodes.end(), [](const RenderNode& left, const RenderNode& right) {
|
||||||
|
const s32 leftKey = left.item.x + left.item.y;
|
||||||
|
const s32 rightKey = right.item.x + right.item.y;
|
||||||
|
if (leftKey != rightKey) {
|
||||||
|
return leftKey < rightKey;
|
||||||
|
}
|
||||||
|
if (left.item.z != right.item.z) {
|
||||||
|
return left.item.z < right.item.z;
|
||||||
|
}
|
||||||
|
if (left.item.y != right.item.y) {
|
||||||
|
return left.item.y < right.item.y;
|
||||||
|
}
|
||||||
|
return left.item.x < right.item.x;
|
||||||
|
});
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void paintNode(RenderedMapImage& image, const RenderNode& node, const Palette256& palette)
|
||||||
|
{
|
||||||
|
const bool flipped = (node.item.flags & flagFlipped) != 0;
|
||||||
|
for (u32 sourceY = 0; sourceY < node.decoded.frame.height; ++sourceY) {
|
||||||
|
const s32 destY = node.top - image.sourceTop + static_cast<s32>(sourceY);
|
||||||
|
if (destY < 0 || destY >= static_cast<s32>(image.height)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const std::size_t sourceRow = static_cast<std::size_t>(sourceY) * node.decoded.frame.width;
|
||||||
|
for (u32 sourceX = 0; sourceX < node.decoded.frame.width; ++sourceX) {
|
||||||
|
const u32 actualSourceX = flipped ? node.decoded.frame.width - 1 - sourceX : sourceX;
|
||||||
|
const s16 colorIndex = node.decoded.pixels[sourceRow + actualSourceX];
|
||||||
|
if (colorIndex < 0 || colorIndex >= 256) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const s32 destX = node.left - image.sourceLeft + static_cast<s32>(sourceX);
|
||||||
|
if (destX < 0 || destX >= static_cast<s32>(image.width)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
image.pixels[static_cast<std::size_t>(destY) * image.width + destX] = scalePaletteColor(palette.colors[colorIndex]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeLe16(std::ofstream& output, u16 value)
|
||||||
|
{
|
||||||
|
output.put(static_cast<char>(value & 0xff));
|
||||||
|
output.put(static_cast<char>((value >> 8) & 0xff));
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeLe32(std::ofstream& output, u32 value)
|
||||||
|
{
|
||||||
|
output.put(static_cast<char>(value & 0xff));
|
||||||
|
output.put(static_cast<char>((value >> 8) & 0xff));
|
||||||
|
output.put(static_cast<char>((value >> 16) & 0xff));
|
||||||
|
output.put(static_cast<char>((value >> 24) & 0xff));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const MapRenderOptions& options)
|
||||||
|
{
|
||||||
|
const auto fixedPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "FIXED.DAT");
|
||||||
|
const auto globPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "GLOB.FLX");
|
||||||
|
const auto shapePath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "SHAPES.FLX");
|
||||||
|
const auto palettePath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "GAMEPAL.PAL");
|
||||||
|
const auto typeFlagPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
||||||
|
if (!fixedPath || !globPath || !shapePath || !palettePath || !typeFlagPath) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto fixedArchive = loadFixedMapArchive(*fixedPath);
|
||||||
|
const auto fixedMap = fixedArchive ? loadFixedMap(*fixedArchive, options.mapIndex) : std::nullopt;
|
||||||
|
const auto globs = loadGlobObjects(*globPath);
|
||||||
|
const auto shapeArchive = loadShapeArchive(*shapePath);
|
||||||
|
const auto palette = loadPalette256(*palettePath);
|
||||||
|
const auto typeFlags = loadTypeFlags(*typeFlagPath);
|
||||||
|
if (!fixedMap || !globs || !shapeArchive || !palette || !typeFlags) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto items = collectSceneItems(*fixedMap, *typeFlags, *globs, options);
|
||||||
|
auto nodes = prepareRenderNodes(items, *shapeArchive);
|
||||||
|
if (nodes.empty()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
s32 minLeft = nodes[0].left;
|
||||||
|
s32 minTop = nodes[0].top;
|
||||||
|
s32 maxRight = nodes[0].right;
|
||||||
|
s32 maxBottom = nodes[0].bottom;
|
||||||
|
for (const RenderNode& node : nodes) {
|
||||||
|
minLeft = std::min(minLeft, node.left);
|
||||||
|
minTop = std::min(minTop, node.top);
|
||||||
|
maxRight = std::max(maxRight, node.right);
|
||||||
|
maxBottom = std::max(maxBottom, node.bottom);
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderedMapImage image;
|
||||||
|
image.rawItemCount = static_cast<u32>(fixedMap->entries.size());
|
||||||
|
image.renderItemCount = static_cast<u32>(items.size());
|
||||||
|
image.decodedItemCount = static_cast<u32>(nodes.size());
|
||||||
|
image.sourceLeft = nodes[0].left;
|
||||||
|
image.sourceTop = nodes[0].top;
|
||||||
|
image.fullWidth = maxRight - minLeft;
|
||||||
|
image.fullHeight = maxBottom - minTop;
|
||||||
|
image.width = std::min(options.maxWidth, static_cast<u32>(std::max<s32>(1, image.fullWidth)));
|
||||||
|
image.height = std::min(options.maxHeight, static_cast<u32>(std::max<s32>(1, image.fullHeight)));
|
||||||
|
image.pixels.assign(
|
||||||
|
static_cast<std::size_t>(image.width) * image.height,
|
||||||
|
RgbColor{static_cast<u8>(10), static_cast<u8>(12), static_cast<u8>(18)});
|
||||||
|
|
||||||
|
for (const RenderNode& node : nodes) {
|
||||||
|
if (node.right < image.sourceLeft || node.bottom < image.sourceTop ||
|
||||||
|
node.left >= image.sourceLeft + static_cast<s32>(image.width) ||
|
||||||
|
node.top >= image.sourceTop + static_cast<s32>(image.height)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
paintNode(image, node, *palette);
|
||||||
|
++image.paintedItemCount;
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool writeBmp24(const std::filesystem::path& path, const RenderedMapImage& image)
|
||||||
|
{
|
||||||
|
if (image.width == 0 || image.height == 0 || image.pixels.size() != static_cast<std::size_t>(image.width) * image.height) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::filesystem::create_directories(path.parent_path());
|
||||||
|
std::ofstream output(path, std::ios::binary);
|
||||||
|
if (!output) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const u32 rowStride = ((image.width * 3 + 3) / 4) * 4;
|
||||||
|
const u32 pixelBytes = rowStride * image.height;
|
||||||
|
const u32 fileSize = 54 + pixelBytes;
|
||||||
|
|
||||||
|
output.put('B');
|
||||||
|
output.put('M');
|
||||||
|
writeLe32(output, fileSize);
|
||||||
|
writeLe16(output, 0);
|
||||||
|
writeLe16(output, 0);
|
||||||
|
writeLe32(output, 54);
|
||||||
|
writeLe32(output, 40);
|
||||||
|
writeLe32(output, image.width);
|
||||||
|
writeLe32(output, image.height);
|
||||||
|
writeLe16(output, 1);
|
||||||
|
writeLe16(output, 24);
|
||||||
|
writeLe32(output, 0);
|
||||||
|
writeLe32(output, pixelBytes);
|
||||||
|
writeLe32(output, 2835);
|
||||||
|
writeLe32(output, 2835);
|
||||||
|
writeLe32(output, 0);
|
||||||
|
writeLe32(output, 0);
|
||||||
|
|
||||||
|
std::vector<u8> row(rowStride, 0);
|
||||||
|
for (s32 y = static_cast<s32>(image.height) - 1; y >= 0; --y) {
|
||||||
|
std::fill(row.begin(), row.end(), static_cast<u8>(0));
|
||||||
|
for (u32 x = 0; x < image.width; ++x) {
|
||||||
|
const RgbColor& pixel = image.pixels[static_cast<std::size_t>(y) * image.width + x];
|
||||||
|
row[x * 3] = pixel.blue;
|
||||||
|
row[x * 3 + 1] = pixel.green;
|
||||||
|
row[x * 3 + 2] = pixel.red;
|
||||||
|
}
|
||||||
|
output.write(reinterpret_cast<const char*>(row.data()), row.size());
|
||||||
|
}
|
||||||
|
return static_cast<bool>(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
23
rebuild/remorse/src/palette.cpp
Normal file
23
rebuild/remorse/src/palette.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
#include "remorse/palette.h"
|
||||||
|
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
std::optional<Palette256> loadPalette256(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
const auto bytes = readBinaryFile(path);
|
||||||
|
if (!bytes || bytes->size() != 256 * 3) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
Palette256 palette;
|
||||||
|
for (std::size_t index = 0; index < palette.colors.size(); ++index) {
|
||||||
|
palette.colors[index].red = (*bytes)[index * 3];
|
||||||
|
palette.colors[index].green = (*bytes)[index * 3 + 1];
|
||||||
|
palette.colors[index].blue = (*bytes)[index * 3 + 2];
|
||||||
|
}
|
||||||
|
return palette;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
79
rebuild/remorse/src/runtime.cpp
Normal file
79
rebuild/remorse/src/runtime.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
#include "remorse/runtime.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <regex>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
std::string readTextFile(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
std::ifstream input(path, std::ios::binary);
|
||||||
|
if (!input) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostringstream buffer;
|
||||||
|
buffer << input.rdbuf();
|
||||||
|
return buffer.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::string> readJsonString(const std::string& json, const char* key)
|
||||||
|
{
|
||||||
|
const std::regex pattern(std::string("\\\"") + key + "\\\"\\s*:\\s*\\\"([^\\\"]*)\\\"");
|
||||||
|
std::smatch match;
|
||||||
|
if (!std::regex_search(json, match, pattern)) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return match[1].str();
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 readJsonU32(const std::string& json, const char* key)
|
||||||
|
{
|
||||||
|
const std::regex pattern(std::string("\\\"") + key + "\\\"\\s*:\\s*([0-9]+)");
|
||||||
|
std::smatch match;
|
||||||
|
if (!std::regex_search(json, match, pattern)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return static_cast<u32>(std::stoul(match[1].str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::optional<ExportManifestSummary> loadExportManifestSummary(const std::filesystem::path& manifestPath)
|
||||||
|
{
|
||||||
|
const std::string json = readTextFile(manifestPath);
|
||||||
|
if (json.empty()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
ExportManifestSummary summary;
|
||||||
|
summary.program = readJsonString(json, "program").value_or("unknown");
|
||||||
|
summary.outputRoot = readJsonString(json, "outputRoot").value_or("");
|
||||||
|
summary.functionFiles = readJsonU32(json, "functionFiles");
|
||||||
|
summary.fallbackFunctionFiles = readJsonU32(json, "fallbackFunctionFiles");
|
||||||
|
summary.globalBuckets = readJsonU32(json, "globalBuckets");
|
||||||
|
summary.globalDefinitions = readJsonU32(json, "globalDefinitions");
|
||||||
|
return summary;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::filesystem::path defaultManifestPath(const std::filesystem::path& currentPath)
|
||||||
|
{
|
||||||
|
return currentPath / "exports" / "src_remorse" / "export_manifest.json";
|
||||||
|
}
|
||||||
|
|
||||||
|
void printStartupReport(const ExportManifestSummary& manifest)
|
||||||
|
{
|
||||||
|
std::cout << "Crusader: No Remorse rebuild harness\n";
|
||||||
|
std::cout << "Program: " << manifest.program << '\n';
|
||||||
|
std::cout << "Export root: " << manifest.outputRoot.string() << '\n';
|
||||||
|
std::cout << "Function files: " << manifest.functionFiles << '\n';
|
||||||
|
std::cout << "Fallback function files: " << manifest.fallbackFunctionFiles << '\n';
|
||||||
|
std::cout << "Global buckets: " << manifest.globalBuckets << '\n';
|
||||||
|
std::cout << "Global definitions: " << manifest.globalDefinitions << '\n';
|
||||||
|
std::cout << "Status: host runtime scaffold is alive; exported game code is not linked yet.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
205
rebuild/remorse/src/shape_archive.cpp
Normal file
205
rebuild/remorse/src/shape_archive.cpp
Normal file
|
|
@ -0,0 +1,205 @@
|
||||||
|
#include "remorse/shape_archive.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
std::optional<u16> readU16Le(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
if (offset + 2 > bytes.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<u32> readU24Le(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
if (offset + 3 > bytes.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return static_cast<u32>(bytes[offset]) |
|
||||||
|
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
||||||
|
(static_cast<u32>(bytes[offset + 2]) << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<u32> readU32Le(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
if (offset + 4 > bytes.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return static_cast<u32>(bytes[offset]) |
|
||||||
|
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
||||||
|
(static_cast<u32>(bytes[offset + 2]) << 16) |
|
||||||
|
(static_cast<u32>(bytes[offset + 3]) << 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<s32> readS32Le(const std::vector<u8>& bytes, u32 offset)
|
||||||
|
{
|
||||||
|
const auto value = readU32Le(bytes, offset);
|
||||||
|
if (!value) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return static_cast<s32>(*value);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<ShapeFrame> parseFrame(const std::vector<u8>& bytes, u32 frameOffset, u32 frameSize)
|
||||||
|
{
|
||||||
|
if (frameOffset > bytes.size() || frameSize > bytes.size() - frameOffset || frameSize < 28) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<u8> frameBytes(bytes.begin() + frameOffset, bytes.begin() + frameOffset + frameSize);
|
||||||
|
const auto compressed = readU32Le(frameBytes, 8);
|
||||||
|
const auto width = readU32Le(frameBytes, 12);
|
||||||
|
const auto height = readU32Le(frameBytes, 16);
|
||||||
|
const auto xOffset = readS32Le(frameBytes, 20);
|
||||||
|
const auto yOffset = readS32Le(frameBytes, 24);
|
||||||
|
if (!compressed || !width || !height || !xOffset || !yOffset) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const u32 lineTableSize = *height * 4;
|
||||||
|
if (*height == 0 || *width == 0 || 28 + lineTableSize > frameBytes.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShapeFrame frame;
|
||||||
|
frame.compressed = *compressed != 0;
|
||||||
|
frame.width = *width;
|
||||||
|
frame.height = *height;
|
||||||
|
frame.xOffset = *xOffset;
|
||||||
|
frame.yOffset = *yOffset;
|
||||||
|
const u32 rleOffset = 28 + lineTableSize;
|
||||||
|
for (u32 row = 0; row < frame.height; ++row) {
|
||||||
|
const auto storedOffset = readU32Le(frameBytes, 28 + row * 4);
|
||||||
|
if (!storedOffset) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
const u32 rowFixup = (frame.height - row) * 4;
|
||||||
|
if (*storedOffset < rowFixup) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
frame.lineOffsets.push_back(*storedOffset - rowFixup);
|
||||||
|
}
|
||||||
|
frame.rleData.assign(frameBytes.begin() + rleOffset, frameBytes.end());
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::vector<s16>> decodePixels(const ShapeFrame& frame)
|
||||||
|
{
|
||||||
|
if (frame.width == 0 || frame.height == 0 || frame.width > 4096 || frame.height > 4096) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<s16> pixels(static_cast<std::size_t>(frame.width) * frame.height, -1);
|
||||||
|
for (u32 row = 0; row < frame.height; ++row) {
|
||||||
|
if (row >= frame.lineOffsets.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
u32 pos = frame.lineOffsets[row];
|
||||||
|
u32 x = 0;
|
||||||
|
while (x < frame.width) {
|
||||||
|
if (pos >= frame.rleData.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
x += frame.rleData[pos++];
|
||||||
|
if (x == frame.width) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (x > frame.width || pos >= frame.rleData.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 runLength = frame.rleData[pos++];
|
||||||
|
u32 runType = 0;
|
||||||
|
if (frame.compressed) {
|
||||||
|
runType = runLength & 1;
|
||||||
|
runLength >>= 1;
|
||||||
|
}
|
||||||
|
if (runLength == 0 || x + runLength > frame.width) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::size_t rowBase = static_cast<std::size_t>(row) * frame.width + x;
|
||||||
|
if (runType == 0) {
|
||||||
|
if (pos + runLength > frame.rleData.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
for (u32 index = 0; index < runLength; ++index) {
|
||||||
|
pixels[rowBase + index] = frame.rleData[pos + index];
|
||||||
|
}
|
||||||
|
pos += runLength;
|
||||||
|
} else {
|
||||||
|
if (pos >= frame.rleData.size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
const s16 color = frame.rleData[pos++];
|
||||||
|
for (u32 index = 0; index < runLength; ++index) {
|
||||||
|
pixels[rowBase + index] = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x += runLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::optional<ShapeArchive> loadShapeArchive(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
const auto flex = loadFlexArchive(path);
|
||||||
|
if (!flex) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
ShapeArchive shapeArchive;
|
||||||
|
shapeArchive.archive = *flex;
|
||||||
|
return shapeArchive;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::vector<ShapeFrame>> loadShapeFrames(const ShapeArchive& archive, u32 shapeIndex)
|
||||||
|
{
|
||||||
|
const auto bytes = readFlexEntry(archive.archive, shapeIndex);
|
||||||
|
if (!bytes || bytes->size() < 6) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto frameCount = readU16Le(*bytes, 4);
|
||||||
|
if (!frameCount || 6 + static_cast<u32>(*frameCount) * 8 > bytes->size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ShapeFrame> frames;
|
||||||
|
for (u32 index = 0; index < *frameCount; ++index) {
|
||||||
|
const u32 headerOffset = 6 + index * 8;
|
||||||
|
const auto frameOffset = readU24Le(*bytes, headerOffset);
|
||||||
|
const auto frameSize = readU32Le(*bytes, headerOffset + 4);
|
||||||
|
if (!frameOffset || !frameSize) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
const auto frame = parseFrame(*bytes, *frameOffset, *frameSize);
|
||||||
|
if (!frame) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
frames.push_back(*frame);
|
||||||
|
}
|
||||||
|
return frames;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<DecodedShapeFrame> decodeShapeFrame(const ShapeArchive& archive, u32 shapeIndex, u32 frameIndex)
|
||||||
|
{
|
||||||
|
const auto frames = loadShapeFrames(archive, shapeIndex);
|
||||||
|
if (!frames || frameIndex >= frames->size()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
const auto pixels = decodePixels((*frames)[frameIndex]);
|
||||||
|
if (!pixels) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
DecodedShapeFrame decoded;
|
||||||
|
decoded.frame = (*frames)[frameIndex];
|
||||||
|
decoded.pixels = *pixels;
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
38
rebuild/remorse/src/type_flags.cpp
Normal file
38
rebuild/remorse/src/type_flags.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
#include "remorse/type_flags.h"
|
||||||
|
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
|
||||||
|
namespace remorse {
|
||||||
|
|
||||||
|
std::optional<std::vector<ShapeTypeInfo>> loadTypeFlags(const std::filesystem::path& path)
|
||||||
|
{
|
||||||
|
const auto bytes = readBinaryFile(path);
|
||||||
|
if (!bytes || (bytes->size() % 9) != 0) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ShapeTypeInfo> infos;
|
||||||
|
for (u32 offset = 0; offset < bytes->size(); offset += 9) {
|
||||||
|
const u8 byte0 = (*bytes)[offset];
|
||||||
|
const u8 byte1 = (*bytes)[offset + 1];
|
||||||
|
const u8 byte2 = (*bytes)[offset + 2];
|
||||||
|
const u8 byte3 = (*bytes)[offset + 3];
|
||||||
|
const u8 byte4 = (*bytes)[offset + 4];
|
||||||
|
const u8 byte6 = (*bytes)[offset + 6];
|
||||||
|
|
||||||
|
ShapeTypeInfo info;
|
||||||
|
info.flags = static_cast<u32>(byte0) |
|
||||||
|
(static_cast<u32>(byte1 & 0x0f) << 8) |
|
||||||
|
(static_cast<u32>(byte6) << 12);
|
||||||
|
info.family = static_cast<u8>((byte1 >> 4) + ((byte2 & 1) << 4));
|
||||||
|
info.x = static_cast<u8>(((byte3 << 3) | (byte2 >> 5)) & 0x1f);
|
||||||
|
info.y = static_cast<u8>((byte3 >> 2) & 0x1f);
|
||||||
|
info.z = static_cast<u8>(((byte4 << 1) | (byte3 >> 7)) & 0x1f);
|
||||||
|
info.animationType = static_cast<u8>(byte4 >> 4);
|
||||||
|
infos.push_back(info);
|
||||||
|
}
|
||||||
|
|
||||||
|
return infos;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace remorse
|
||||||
23
rebuild/remorse/tests/combat_tactics_tests.cpp
Normal file
23
rebuild/remorse/tests/combat_tactics_tests.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
#include "remorse/combat_tactics.h"
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
assert(root.has_value());
|
||||||
|
|
||||||
|
const auto combatPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "COMBAT.DAT");
|
||||||
|
assert(combatPath.has_value());
|
||||||
|
|
||||||
|
const auto tactics = remorse::loadCombatTactics(*combatPath);
|
||||||
|
assert(tactics.has_value());
|
||||||
|
assert(tactics->size() == 14);
|
||||||
|
assert((*tactics)[0].name == "Dumb");
|
||||||
|
assert((*tactics)[0].blockOffsets[0] == 0x002c);
|
||||||
|
assert((*tactics)[0].blockOffsets[1] == 0x0031);
|
||||||
|
assert(!(*tactics)[0].bytecode.empty());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
26
rebuild/remorse/tests/entity_dispatch_entry_tests.cpp
Normal file
26
rebuild/remorse/tests/entity_dispatch_entry_tests.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#include "remorse/entity_dispatch_entry.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
remorse::EntityDispatchEntryBase entry{};
|
||||||
|
remorse::EntityDispatchEntry::initBase(&entry);
|
||||||
|
|
||||||
|
remorse::EntityDispatchEntry::setSourceType(entry, 0x1234);
|
||||||
|
assert(entry.bytes[0x04] == 0x34);
|
||||||
|
assert(entry.bytes[0x05] == 0x12);
|
||||||
|
|
||||||
|
remorse::EntityDispatchEntry::setEventTypeChecked(entry, 0x00f2);
|
||||||
|
assert(entry.bytes[0x06] == 0xf2);
|
||||||
|
assert(entry.bytes[0x07] == 0x00);
|
||||||
|
|
||||||
|
entry.bytes[0x08] = 0xe0;
|
||||||
|
remorse::EntityDispatchEntry::setGroupId(entry, 0x23);
|
||||||
|
assert(entry.bytes[0x08] == 0xe3);
|
||||||
|
|
||||||
|
remorse::EntityDispatchEntry::incrementGroupId(entry);
|
||||||
|
assert(entry.bytes[0x08] == 0xe4);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
24
rebuild/remorse/tests/file_system_tests.cpp
Normal file
24
rebuild/remorse/tests/file_system_tests.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
assert(root.has_value());
|
||||||
|
assert(root->hasExecutable);
|
||||||
|
assert(root->hasStaticData);
|
||||||
|
assert(root->hasUsecode);
|
||||||
|
|
||||||
|
const auto palettePath = remorse::resolveGameDataPath(*root, std::filesystem::path("static") / "gamepal.pal");
|
||||||
|
assert(palettePath.has_value());
|
||||||
|
|
||||||
|
const auto paletteBytes = remorse::readBinaryFile(*palettePath);
|
||||||
|
assert(paletteBytes.has_value());
|
||||||
|
assert(!paletteBytes->empty());
|
||||||
|
|
||||||
|
const auto blockedParent = remorse::resolveGameDataPath(*root, std::filesystem::path("..") / "CRUSADER.EXE");
|
||||||
|
assert(!blockedParent.has_value());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
37
rebuild/remorse/tests/fixed_map_tests.cpp
Normal file
37
rebuild/remorse/tests/fixed_map_tests.cpp
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/fixed_map.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
assert(root.has_value());
|
||||||
|
|
||||||
|
const auto fixedPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "FIXED.DAT");
|
||||||
|
assert(fixedPath.has_value());
|
||||||
|
|
||||||
|
const auto archive = remorse::loadFixedMapArchive(*fixedPath);
|
||||||
|
assert(archive.has_value());
|
||||||
|
assert(archive->mapCount > 0);
|
||||||
|
|
||||||
|
const auto firstMap = remorse::loadFixedMap(*archive, 0);
|
||||||
|
assert(firstMap.has_value());
|
||||||
|
assert(firstMap->index == 0);
|
||||||
|
|
||||||
|
bool foundNonEmptyMap = false;
|
||||||
|
for (remorse::u32 index = 0; index < archive->mapCount; ++index) {
|
||||||
|
const auto map = remorse::loadFixedMap(*archive, index);
|
||||||
|
assert(map.has_value());
|
||||||
|
if (!map->entries.empty()) {
|
||||||
|
foundNonEmptyMap = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert(foundNonEmptyMap);
|
||||||
|
|
||||||
|
const auto outOfRange = remorse::loadFixedMap(*archive, archive->mapCount);
|
||||||
|
assert(!outOfRange.has_value());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
27
rebuild/remorse/tests/flex_archive_tests.cpp
Normal file
27
rebuild/remorse/tests/flex_archive_tests.cpp
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/flex_archive.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
assert(root.has_value());
|
||||||
|
|
||||||
|
const auto combatPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "COMBAT.DAT");
|
||||||
|
assert(combatPath.has_value());
|
||||||
|
|
||||||
|
const auto archive = remorse::loadFlexArchive(*combatPath);
|
||||||
|
assert(archive.has_value());
|
||||||
|
assert(archive->populatedEntries.size() == 14);
|
||||||
|
|
||||||
|
const auto firstEntry = remorse::readFlexEntry(*archive, 0);
|
||||||
|
assert(firstEntry.has_value());
|
||||||
|
assert(firstEntry->size() == 0x4f);
|
||||||
|
assert((*firstEntry)[0] == 'D');
|
||||||
|
assert((*firstEntry)[1] == 'u');
|
||||||
|
assert((*firstEntry)[2] == 'm');
|
||||||
|
assert((*firstEntry)[3] == 'b');
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
28
rebuild/remorse/tests/glob_archive_tests.cpp
Normal file
28
rebuild/remorse/tests/glob_archive_tests.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/glob_archive.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
assert(root.has_value());
|
||||||
|
|
||||||
|
const auto globPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "GLOB.FLX");
|
||||||
|
assert(globPath.has_value());
|
||||||
|
|
||||||
|
const auto objects = remorse::loadGlobObjects(*globPath);
|
||||||
|
assert(objects.has_value());
|
||||||
|
assert(!objects->empty());
|
||||||
|
|
||||||
|
bool foundObjectWithEntries = false;
|
||||||
|
for (const remorse::GlobObject& object : *objects) {
|
||||||
|
if (!object.entries.empty()) {
|
||||||
|
foundObjectWithEntries = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert(foundObjectWithEntries);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
25
rebuild/remorse/tests/map_renderer_tests.cpp
Normal file
25
rebuild/remorse/tests/map_renderer_tests.cpp
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/map_renderer.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
assert(root.has_value());
|
||||||
|
|
||||||
|
remorse::MapRenderOptions options;
|
||||||
|
options.mapIndex = 1;
|
||||||
|
options.maxWidth = 320;
|
||||||
|
options.maxHeight = 200;
|
||||||
|
|
||||||
|
const auto image = remorse::renderMapPreview(*root, options);
|
||||||
|
assert(image.has_value());
|
||||||
|
assert(image->width > 0);
|
||||||
|
assert(image->height > 0);
|
||||||
|
assert(!image->pixels.empty());
|
||||||
|
assert(image->decodedItemCount > 0);
|
||||||
|
assert(image->paintedItemCount > 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
19
rebuild/remorse/tests/palette_tests.cpp
Normal file
19
rebuild/remorse/tests/palette_tests.cpp
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/palette.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
assert(root.has_value());
|
||||||
|
|
||||||
|
const auto palettePath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "GAMEPAL.PAL");
|
||||||
|
assert(palettePath.has_value());
|
||||||
|
|
||||||
|
const auto palette = remorse::loadPalette256(*palettePath);
|
||||||
|
assert(palette.has_value());
|
||||||
|
assert(palette->colors.size() == 256);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
30
rebuild/remorse/tests/shape_archive_tests.cpp
Normal file
30
rebuild/remorse/tests/shape_archive_tests.cpp
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/shape_archive.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
assert(root.has_value());
|
||||||
|
|
||||||
|
const auto shapesPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "SHAPES.FLX");
|
||||||
|
assert(shapesPath.has_value());
|
||||||
|
|
||||||
|
const auto archive = remorse::loadShapeArchive(*shapesPath);
|
||||||
|
assert(archive.has_value());
|
||||||
|
|
||||||
|
bool decodedFrame = false;
|
||||||
|
for (remorse::u32 shape = 0; shape < archive->archive.entryCount; ++shape) {
|
||||||
|
const auto frame = remorse::decodeShapeFrame(*archive, shape, 0);
|
||||||
|
if (frame && !frame->pixels.empty()) {
|
||||||
|
assert(frame->frame.width > 0);
|
||||||
|
assert(frame->frame.height > 0);
|
||||||
|
decodedFrame = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert(decodedFrame);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
28
rebuild/remorse/tests/type_flags_tests.cpp
Normal file
28
rebuild/remorse/tests/type_flags_tests.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
#include "remorse/file_system.h"
|
||||||
|
#include "remorse/type_flags.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||||
|
assert(root.has_value());
|
||||||
|
|
||||||
|
const auto typeFlagPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
||||||
|
assert(typeFlagPath.has_value());
|
||||||
|
|
||||||
|
const auto infos = remorse::loadTypeFlags(*typeFlagPath);
|
||||||
|
assert(infos.has_value());
|
||||||
|
assert(infos->size() == 2048);
|
||||||
|
|
||||||
|
bool foundGlobFamily = false;
|
||||||
|
for (const remorse::ShapeTypeInfo& info : *infos) {
|
||||||
|
if (info.family == 3) {
|
||||||
|
foundGlobFamily = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert(foundGlobFamily);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
150
rebuild/remorse/tools/import_export_slice.py
Normal file
150
rebuild/remorse/tools/import_export_slice.py
Normal file
|
|
@ -0,0 +1,150 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import dataclasses
|
||||||
|
import pathlib
|
||||||
|
import re
|
||||||
|
import shutil
|
||||||
|
from collections.abc import Iterable
|
||||||
|
|
||||||
|
|
||||||
|
CALLING_CONVENTION_REPLACEMENTS = {
|
||||||
|
"__cdecl16far": "CR_FARCALL",
|
||||||
|
"__cdecl16near": "CR_CDECL",
|
||||||
|
"__stdcall16far": "CR_FARCALL",
|
||||||
|
"__stdcall16near": "CR_STDCALL",
|
||||||
|
"__cdecl": "CR_CDECL",
|
||||||
|
"__stdcall": "CR_STDCALL",
|
||||||
|
"__regcall": "CR_CDECL",
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPORT_HEADER_INCLUDE_RE = re.compile(r'#include\s+"(?:\.\./)*CRUSADER\.EXE\.h"')
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass(frozen=True)
|
||||||
|
class ImportStats:
|
||||||
|
files_read: int = 0
|
||||||
|
files_written: int = 0
|
||||||
|
callconv_replacements: int = 0
|
||||||
|
namespace_qualified_functions: int = 0
|
||||||
|
unresolved_tokens: tuple[str, ...] = ()
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_text(text: str) -> tuple[str, int]:
|
||||||
|
replacement_count = 0
|
||||||
|
text = EXPORT_HEADER_INCLUDE_RE.sub('#include "remorse/export_prelude.h"', text)
|
||||||
|
|
||||||
|
for source, target in CALLING_CONVENTION_REPLACEMENTS.items():
|
||||||
|
text, count = re.subn(rf"\b{re.escape(source)}\b", target, text)
|
||||||
|
replacement_count += count
|
||||||
|
|
||||||
|
return text, replacement_count
|
||||||
|
|
||||||
|
|
||||||
|
def find_unresolved_tokens(text: str) -> tuple[str, ...]:
|
||||||
|
tokens = []
|
||||||
|
for token in ("unaff_SS", "unaff_CS", "__return_storage_ptr__", "CONCAT22", "BADSPACEBASE"):
|
||||||
|
if token in text:
|
||||||
|
tokens.append(token)
|
||||||
|
return tuple(tokens)
|
||||||
|
|
||||||
|
|
||||||
|
def iter_source_files(source_root: pathlib.Path) -> Iterable[pathlib.Path]:
|
||||||
|
for path in sorted(source_root.rglob("*")):
|
||||||
|
if path.is_file() and path.suffix.lower() in {".c", ".h"}:
|
||||||
|
yield path
|
||||||
|
|
||||||
|
|
||||||
|
def import_slice(export_root: pathlib.Path, slice_path: pathlib.Path, output_root: pathlib.Path) -> ImportStats:
|
||||||
|
source_root = export_root / "functions" / slice_path
|
||||||
|
if not source_root.is_dir():
|
||||||
|
raise FileNotFoundError(f"export slice does not exist: {source_root}")
|
||||||
|
|
||||||
|
destination_root = output_root / slice_path
|
||||||
|
if destination_root.exists():
|
||||||
|
shutil.rmtree(destination_root)
|
||||||
|
destination_root.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
files_read = 0
|
||||||
|
files_written = 0
|
||||||
|
callconv_replacements = 0
|
||||||
|
namespace_qualified_functions = 0
|
||||||
|
unresolved_tokens: set[str] = set()
|
||||||
|
|
||||||
|
for source_file in iter_source_files(source_root):
|
||||||
|
files_read += 1
|
||||||
|
relative_path = source_file.relative_to(source_root)
|
||||||
|
destination_name = relative_path.with_suffix(".cpp") if source_file.suffix.lower() == ".c" else relative_path
|
||||||
|
destination_file = destination_root / destination_name
|
||||||
|
destination_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
original = source_file.read_text(encoding="utf-8", errors="replace")
|
||||||
|
normalized, replacement_count = normalize_text(original)
|
||||||
|
callconv_replacements += replacement_count
|
||||||
|
namespace_qualified_functions += normalized.count("Remorse::")
|
||||||
|
unresolved_tokens.update(find_unresolved_tokens(normalized))
|
||||||
|
destination_file.write_text(normalized, encoding="utf-8", newline="\n")
|
||||||
|
files_written += 1
|
||||||
|
|
||||||
|
return ImportStats(
|
||||||
|
files_read=files_read,
|
||||||
|
files_written=files_written,
|
||||||
|
callconv_replacements=callconv_replacements,
|
||||||
|
namespace_qualified_functions=namespace_qualified_functions,
|
||||||
|
unresolved_tokens=tuple(sorted(unresolved_tokens)),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def write_report(path: pathlib.Path, slice_path: pathlib.Path, stats: ImportStats) -> None:
|
||||||
|
lines = [
|
||||||
|
"# Export Slice Import Report",
|
||||||
|
"",
|
||||||
|
f"Slice: `{slice_path.as_posix()}`",
|
||||||
|
f"Files read: {stats.files_read}",
|
||||||
|
f"Files written: {stats.files_written}",
|
||||||
|
f"Calling-convention replacements: {stats.callconv_replacements}",
|
||||||
|
f"Namespace-qualified references: {stats.namespace_qualified_functions}",
|
||||||
|
"",
|
||||||
|
"## Remaining Cleanup Signals",
|
||||||
|
"",
|
||||||
|
]
|
||||||
|
|
||||||
|
if stats.unresolved_tokens:
|
||||||
|
lines.extend(f"- `{token}`" for token in stats.unresolved_tokens)
|
||||||
|
else:
|
||||||
|
lines.append("- No tracked unresolved tokens found by this importer.")
|
||||||
|
|
||||||
|
lines.extend(
|
||||||
|
[
|
||||||
|
"",
|
||||||
|
"This is a staging import only. Do not link the generated files into the host rebuild until their types, globals, and segmented-pointer behavior are reviewed.",
|
||||||
|
"",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
path.write_text("\n".join(lines), encoding="utf-8", newline="\n")
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args() -> argparse.Namespace:
|
||||||
|
parser = argparse.ArgumentParser(description="Stage one Ghidra-exported function slice for manual Remorse rebuild cleanup.")
|
||||||
|
parser.add_argument("slice", help="Path under exports/src_remorse/functions, such as classes/Remorse/EntityDispatchEntry")
|
||||||
|
parser.add_argument("--export-root", type=pathlib.Path, default=pathlib.Path("exports/src_remorse"))
|
||||||
|
parser.add_argument("--output-root", type=pathlib.Path, default=pathlib.Path("rebuild/remorse/imported"))
|
||||||
|
parser.add_argument("--report", type=pathlib.Path, default=pathlib.Path("rebuild/remorse/imported/import_report.md"))
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
args = parse_args()
|
||||||
|
slice_path = pathlib.Path(args.slice)
|
||||||
|
stats = import_slice(args.export_root, slice_path, args.output_root)
|
||||||
|
args.report.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
write_report(args.report, slice_path, stats)
|
||||||
|
print(f"Imported {stats.files_written} files from {slice_path.as_posix()} into {args.output_root / slice_path}")
|
||||||
|
print(f"Report: {args.report}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
Loading…
Add table
Add a link
Reference in a new issue