# Magic Ball 2 — Root-Cause Analysis of Modern Windows Bugs

Every compatibility bug in **Magic Ball 2** (`MagicBall2.exe`, MSVC 7.1, 2004–2005) traced to exact virtual addresses, instructions, and C++ struct layouts.

---

## 1. 16-Bit Color Format Filter in `CDisplay::EnumDisplayModes` (`0x0040BD9B..0x0040BDA8`)

### Disassembly (`0x0040BD80..0x0040BE14`)
```asm
0x0040BD80: mov eax, DWORD PTR [esp+edx*1+0x250] ; adapterMode.Format (D3DFORMAT)
0x0040BD87: cmp eax, DWORD PTR [esp+ecx*4+0xd0]
0x0040BD8E: jne 0x0040BE14
0x0040BD94: cmp DWORD PTR [esp+ecx*4+0x30], 0x1  ; bConfirmedFormat == TRUE
0x0040BD99: jne 0x0040BE14
0x0040BD9B: cmp eax, 0x17                        ; D3DFMT_R5G6B5   (23, 16-bit 5:6:5)
0x0040BD9E: je  0x0040BDAA
0x0040BDA0: cmp eax, 0x18                        ; D3DFMT_X1R5G5B5 (24, 16-bit 1:5:5:5)
0x0040BDA3: je  0x0040BDAA
0x0040BDA5: cmp eax, 0x19                        ; D3DFMT_A1R5G5B5 (25, 16-bit 1:5:5:5)
0x0040BDA8: jne 0x0040BE14                       ; rejects 32-bit D3DFMT_X8R8G8B8 (0x16 = 22)
```

### Root Cause
`CDisplay::EnumDisplayModes` (`0x0040BA50`, adapted from the DirectX 8.0 SDK `CD3DApplication::BuildDeviceList`) filters confirmed adapter formats at `0x0040BD9B..0x0040BDA8` and only accepts 16-bit `D3DFORMAT` values (`23`, `24`, `25`), rejecting 32-bit `D3DFMT_X8R8G8B8` (`22 = 0x16`).

On Windows 8/10/11, DXGI/DWM does not enumerate 16-bit modes in `IDirect3D8::EnumAdapterModes` unless the 16-bit Windows compatibility shim is active:
- `g_Display.m_dwNumModes` (`0x0048A2B0`) remains `0`.
- `g_Display.m_modes[0..11]` (`0x0048A1B8`) remains zero-filled (`Width = 0`, `Height = 0`, `Format = D3DFMT_UNKNOWN`, `DepthStencilFormat = D3DFMT_UNKNOWN`).
- `CDisplay::CreateDevice` (`0x0040B3A0`) sets `d3dpp.AutoDepthStencilFormat = m_modes[m_dwCurrentMode].DepthStencilFormat` (`0 = D3DFMT_UNKNOWN`) with `EnableAutoDepthStencil = TRUE`, causing `IDirect3D8::CreateDevice` (`0x0040B484`) to fail with `D3DERR_INVALIDCALL` and exit `WinMain`.

---

## 2. Hardcoded 6-Resolution 4:3 Whitelist & 12-Entry Mode Array (`0x0040BAC9..0x0040BB43`)

### Disassembly (`0x0040BAC5..0x0040BB43`)
```asm
0x0040BAC5: mov edi, DWORD PTR [esp+0x1c] ; mode.Width
0x0040BAC9: cmp edi, 0x280                ; 640
0x0040BACF: jne 0x0040BADD
0x0040BAD1: mov edx, DWORD PTR [esp+0x20] ; mode.Height
0x0040BAD5: cmp edx, 0x1e0                ; 480
0x0040BADB: jmp 0x0040BB43
0x0040BADD: cmp edi, 0x320                ; 800 x 600   (0x258)
...
0x0040BAF1: cmp edi, 0x400                ; 1024 x 768  (0x300)
...
0x0040BB05: cmp edi, 0x480                ; 1152 x 864  (0x360)
...
0x0040BB19: cmp edi, 0x500                ; 1280 x 960  (0x3c0)
...
0x0040BB2D: cmp edi, 0x640                ; 1600 x 1200 (0x4b0)
0x0040BB33: jne 0x0040BBCB
0x0040BB39: mov edx, DWORD PTR [esp+0x20]
0x0040BB3D: cmp edx, 0x4b0
0x0040BB43: jne 0x0040BBCB                ; skip mode if not one of the 6 resolutions
```

### Root Cause
Inside `CDisplay` (`g_Display` @ `0x0048A1A8`, `sizeof = 0x114`), `m_modes` at `+0x010` (`0x0048A1B8`) is a fixed 12-entry array (`D3DModeInfo m_modes[12]`, `12 * 20 = 240` bytes, immediately followed by `m_bSupportsHardwareShadows` at `+0x100 = 0x0048A2A8`). To prevent overflowing `m_modes[12]`, Dream Dale restricted enumeration to 6 4:3 resolutions (at most 2 16-bit formats each).

---

## 3. `LoadParams` Resolution Index Clobber (`0x0041ABD0`)

### Disassembly (`0x0041AB7A..0x0041ABF8`)
```asm
0x0041AB7A: push 0x0048A420                     ; &g_CfgResIndex
0x0041AB7F: push 0x0047948C                     ; "%d"
0x0041AB84: push eax
0x0041AB85: call 0x004684D1                     ; sscanf(line, "%d", &g_CfgResIndex)
0x0041AB8A: mov  eax, ds:0x0048A420             ; eax = g_CfgResIndex
0x0041AB8F: lea  eax, [eax+eax*4]
0x0041AB92: mov  ecx, DWORD PTR [eax*4+0x48A1B8]; ecx = g_Display.m_modes[g_CfgResIndex].Width
0x0041AB99: mov  DWORD PTR ds:0x0048A2D0, ecx   ; g_ScreenWidth = ecx
0x0041AB9F: mov  eax, DWORD PTR [eax*4+0x48A1BC]; eax = g_Display.m_modes[g_CfgResIndex].Height
0x0041ABA6: add  esp, 0x0C
0x0041ABA9: cmp  ecx, ebx
0x0041ABAB: mov  ds:0x0048A2D4, eax             ; g_ScreenHeight = eax
0x0041ABB0: jne  0x0041ABBC
0x0041ABB2: mov  DWORD PTR ds:0x0048A2D0, 0x320 ; fallback 800
0x0041ABBC: cmp  eax, ebx
0x0041ABBE: jne  0x0041ABCA
0x0041ABC0: mov  DWORD PTR ds:0x0048A2D4, 0x258 ; fallback 600
0x0041ABCA: fild DWORD PTR ds:0x0048A2D0
0x0041ABD0: mov  DWORD PTR ds:0x0048A420, 0x1   ; <-- BUG: overwrites g_CfgResIndex back to 1 (800x600)!
0x0041ABDA: fmul DWORD PTR ds:0x0047B178        ; * (1.0f / 800.0f) (0.00125f)
0x0041ABE0: fstp DWORD PTR ds:0x0048A430        ; g_UiScaleX
0x0041ABE6: fild DWORD PTR ds:0x0048A2D4
0x0041ABEC: fmul DWORD PTR ds:0x0047B174        ; * (1.0f / 600.0f) (0.00166667f)
0x0041ABF2: fstp DWORD PTR ds:0x0048A434        ; g_UiScaleY
```

### Root Cause
After parsing `resolution:` from `params.rs` and computing `g_ScreenWidth`, `g_ScreenHeight`, `g_UiScaleX`, and `g_UiScaleY`, instruction `0x0041ABD0` overwrites `g_CfgResIndex` (`0x0048A420`) with `1`.
Subsequently, `InitGameResources` (`0x004156BB`) calls `g_Display.SetResolutionIndex(g_CfgResIndex)` (`0x0040BA40`), setting `g_Display.m_dwCurrentMode = 1` (`800×600`).
Every 2D orthographic projection setup (`D3DXMatrixOrthoLH` at `0x0040FAC1`, `0x004250F8`, etc.) reads `g_Display.m_modes[g_Display.m_dwCurrentMode].Width` / `.Height` (`800×600`), while 2D UI quads are scaled by `g_UiScaleX`/`g_UiScaleY` to `g_ScreenWidth × g_ScreenHeight`.

---

## 4. "How to Play" (`CMenuHelpView::Render`) 3D Sub-Viewport vs. 2D Font Desync (`0x0041B870..0x0041BB9A`)

### Disassembly (`0x0041B870..0x0041BB24`)
```asm
; Sub-viewport for 3D power-up capsule uses g_UiScaleX (0x0048A430) & g_UiScaleY (0x0048A434):
0x0041B870: fld  DWORD PTR ds:0x0048A430        ; g_UiScaleX
0x0041B876: fmul DWORD PTR [esi+0x4]            ; * m_pos.x
0x0041B87C: call 0x004688D4                     ; _ftol -> viewport.X
0x0041B881: fld  DWORD PTR ds:0x0048A434        ; g_UiScaleY
0x0041B887: fmul DWORD PTR [esi+0x8]            ; * m_pos.y
0x0041B88E: call 0x004688D4                     ; _ftol -> viewport.Y
0x0041B893: fld  DWORD PTR ds:0x0048A430        ; g_UiScaleX
0x0041B899: fmul DWORD PTR [esi+0x4A0]          ; * m_width
0x0041B8A3: call 0x004688D4                     ; _ftol -> viewport.Width
0x0041B8A8: fld  DWORD PTR ds:0x0048A434        ; g_UiScaleY
0x0041B8AE: fmul DWORD PTR [esi+0x4A4]          ; * m_height
0x0041B8B8: call 0x004688D4                     ; _ftol -> viewport.Height
...
; Text description uses unscaled 800x600 virtual coordinates (-400..+400, -300..+300):
0x0041BAD3: fld  DWORD PTR [esi+0x80]           ; m_textOffset.x
0x0041BAD9: fadd DWORD PTR [esi+0x4A0]          ; + m_width
0x0041BAE6: fadd DWORD PTR [edi]                ; + m_pos.x
0x0041BAF3: fsub DWORD PTR ds:0x0047B718        ; - 400.0f
0x0041BB05: fstp DWORD PTR [esp+0x14]
0x0041BB11: fld  DWORD PTR ds:0x00479FD0        ; 300.0f
0x0041BB17: fsub DWORD PTR [esi+0x8]            ; - m_pos.y
0x0041BB1A: fadd DWORD PTR [esi+0x84]           ; + m_textOffset.y
0x0041BB20: fstp DWORD PTR [esp+0x18]
```

### Root Cause
`CMenuHelpView::Render` (`0x0041B850`) positions the 3D capsule's `D3DVIEWPORT8` in physical backbuffer pixels using `g_UiScaleX` and `g_UiScaleY`, then restores the main viewport and passes virtual `[-400..400, -300..300]` coordinates to `CFont::DrawText` (`0x00424DA0`).
Inside `CFont::DrawText` (`0x00424DA0` / `0x004250C0`), text quads are transformed by an orthographic matrix sized to `g_Display.m_modes[g_Display.m_dwCurrentMode].Width × Height` (`800×600` baseline). Whenever `m_modes[m_dwCurrentMode]` does not match `800×600` (or when `g_UiScaleX`/`g_UiScaleY` desync from the client rect), the 3D power-up viewports and their 2D text labels separate across the screen.

---

## 5. Windowed Mode (`window: 1`) Outer Frame vs. Client Area Desync (`0x0040B367..0x0040B407`)

### Disassembly (`0x0040B35F..0x0040B407`)
```asm
0x0040B367: call DWORD PTR ds:0x004791EC        ; SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, *pWidth, *pHeight, SWP_SHOWWINDOW)
...
0x0040B3D5: call DWORD PTR ds:0x004791F0        ; GetClientRect(hWnd, &rcClient)
0x0040B3DB: mov  edx, DWORD PTR [esp+0x4c]      ; rcClient.left
0x0040B3DF: mov  eax, DWORD PTR [esp+0x54]      ; rcClient.right
0x0040B3E3: mov  ecx, DWORD PTR [esp+0x14c]     ; pWidth (&g_ScreenWidth)
0x0040B3EA: sub  eax, edx                       ; clientWidth = right - left
0x0040B3EC: mov  edx, DWORD PTR [esp+0x150]     ; pHeight (&g_ScreenHeight)
0x0040B3F3: mov  DWORD PTR [ecx], eax           ; *pWidth = clientWidth
0x0040B3F5: mov  ecx, DWORD PTR [esp+0x50]      ; rcClient.top
0x0040B3F9: mov  DWORD PTR [esp+0x18], eax      ; d3dpp.BackBufferWidth = clientWidth
0x0040B3FD: mov  eax, DWORD PTR [esp+0x58]      ; rcClient.bottom
0x0040B401: sub  eax, ecx                       ; clientHeight = bottom - top
0x0040B403: mov  DWORD PTR [esp+0x1c], eax      ; d3dpp.BackBufferHeight = clientHeight
0x0040B407: mov  DWORD PTR [edx], eax           ; *pHeight = clientHeight
```

### Root Cause
`InitWindow` (`0x0041B292`) creates the window with style `WS_POPUP | WS_CAPTION | WS_SYSMENU` (`0x80C80000`) and passes `g_ScreenWidth × g_ScreenHeight` as the outer window size without calling `AdjustWindowRect`. Then `CDisplay::CreateDevice` (`0x0040B3D5`) overwrites `g_ScreenWidth` and `g_ScreenHeight` with `GetClientRect` (shrinking `800×600` by the title bar and border to e.g. `794×572`), without recalculating `g_UiScaleX` / `g_UiScaleY`.

---

## 6. Hardcoded `Aspect = 1.0f` in All Four 3D Perspective Matrices

### Call Sites of `D3DXMatrixPerspectiveFovLH` (`0x00429DCB`)
| Subsystem | Function | Address | Parameters Passed |
|---|---|---|---|
| 3D Intro Logo | `CLogoScene::Render` | `0x0040D194..0x0040D1B0` | `fovy = pi/4 (0x3F490FDB)`, `Aspect = 1.0f (0x3F800000)`, `zn = 1.0f`, `zf = 1000.0f` |
| 3D Gameplay Camera | `CLevel::SetupMatrices` | `0x0040FB2C..0x0040FB3B` | `fovy = pi/4 (0x3F490FDB)`, `Aspect = 1.0f (0x3F800000)`, `zn = 1.0f`, `zf = 100.0f` |
| 3D Menu Background | `Main::RenderMenuScene` | `0x00419EDC..0x00419EF8` | `fovy = pi/4 (0x3F490FDB)`, `Aspect = 1.0f (0x3F800000)`, `zn = 1.0f`, `zf = 1000.0f` |
| 3D "How to Play" Item | `CMenuHelpView::Render` | `0x0041BA18..0x0041BA34` | `fovy = pi/4 (0x3F490FDB)`, `Aspect = 1.0f (0x3F800000)`, `zn = 1.0f`, `zf = 1000.0f` |

### Root Cause
All four 3D perspective projections hardcode `Aspect = 1.0f` (which was designed to be displayed on a 4:3 monitor). On widescreen aspect ratios (`16:9`, `16:10`, `21:9`), `Aspect = 1.0f` stretches the 3D scene horizontally; setting `Aspect = ((float)width / (float)height) * (3.0f / 4.0f)` preserves the original 4:3 object proportions while widening the horizontal FOV naturally.

---

## 7. MoleBox 2 User-Mode `KERNEL32.dll` Hook Failure with `Bass.dll` (`0x00423BCC` & `0x00423E9F`)

### Root Cause
`MagicBall2.exe` has no built-in `.pak` archive reader; `Default.pak`, `Interface0.pak`, and `Lev0.pak` are MoleBox 2 encrypted virtual filesystem packages mounted by MoleBox's user-mode `KERNEL32.dll` hooks.
When `CSoundManager::LoadAllSounds` (`0x00424340`) calls `FindFirstFileA("Sounds/*.ogg")`, MoleBox's hook returns all 90 virtual `.ogg` filenames, which are passed to `CSoundManager::LoadSample` (`0x00423B80` -> `BASS_SampleLoad` @ `0x00423BCC`) and `CSoundManager::LoadMusic` (`0x00423E70` -> `BASS_MusicLoad` @ `0x00423E9F`).
On modern Windows (Vista through Windows 11), `Bass.dll`'s internal file open path bypasses MoleBox's user-mode `KERNEL32.dll` hook, hits the real filesystem where `Sounds/` and `Music/` do not exist, and fails with `BASS_ERROR_FILEOPEN` (`2`).
Unwrapping the PE (`tools/unwrap_pe.py`) and extracting all 680 files from `Default.pak`, `Interface0.pak`, and `Lev0.pak` (`tools/pak_tool.py`) eliminates MoleBox completely.

---

## 8. Fast Level-Transition Music Mute Bug (`CSoundManager` @ `0x00423330` & `0x004240E0`)

### Disassembly (`0x00423330..0x00423347` & `0x004240D6..0x004240F6`)
```asm
; CSoundManager::SlideMusicVolume (0x00423330):
0x00423338: push eax                     ; time (ms)
0x00423339: mov  eax, DWORD PTR [ecx+0x30]; this->m_curMusic (HMUSIC)
0x0042333C: push 0xffffffff              ; pan = -1
0x0042333E: push edx                     ; volume (-2 = fade out and stop)
0x0042333F: push 0xffffffff              ; freq = -1
0x00423341: push eax                     ; handle
0x00423342: call 0x00467A58              ; BASS_ChannelSlideAttributes(m_curMusic, -1, volume, -1, time)

; CSoundManager::PlayMusicOrder (0x00424030 at 0x004240D6):
0x004240D6: push 0xffffffff              ; pan = -1
0x004240D8: push 0x64                    ; volume = 100
0x004240DA: push 0xffffffff              ; freq = -1
0x004240DC: push edi                     ; handle (HMUSIC)
0x004240DD: mov  DWORD PTR [esi+0x30], edi
0x004240E0: call 0x00467A52              ; BASS_ChannelSetAttributes(edi, -1, 100, -1)
...
0x004240F6: call 0x00467A7C              ; BASS_MusicPlayEx(edi, pos, BASS_MUSIC_POSRESET|BASS_MUSIC_LOOP, TRUE)
```

### Root Cause
All level music tracks in Magic Ball 2 are sub-song orders inside the single `Music/music.mo3` tracker module (`Music/music1.txt`..`Music/music4.txt`), so `CSoundManager::m_musics["Music/music.mo3"]` returns the **same** `HMUSIC` handle across levels.
When a level ends, the game fades out the music by calling `SlideMusicVolume(-2, fadeTime)` (`0x00423330`), which starts an asynchronous BASS volume slide to `-2` (fade to 0 and stop channel).
If the next level starts before that fade timer expires, `PlayMusicOrder` (`0x004240E0`) calls `BASS_ChannelSetAttributes(handle, -1, 100, -1)`. In BASS 2.0, `BASS_ChannelSetAttributes` changes the current volume to `100` **without canceling an active `BASS_ChannelSlideAttributes` slide**—so BASS's background slide continues running, fades the newly started level's music to 0, and stops the channel. Calling `BASS_ChannelSlideAttributes(handle, -1, 100, -1, 0)` (or `BASS_ChannelStop(handle)`) before `BASS_MusicPlayEx` cancels any in-flight slide cleanly.
