# Windows Platform Guide

This document provides Windows-specific information for MPV.Rocks Installer development and deployment.

## Table of Contents

- [Prerequisites](#prerequisites)
- [Building](#building)
- [Installation](#installation)
- [File Associations](#file-associations)
- [GPU Detection](#gpu-detection)
- [Console Handling](#console-handling)
- [Troubleshooting](#troubleshooting)

---

## Prerequisites

### Development Tools

**Required:**
- Go 1.25 or later
- Git (optional, for version info)
- Make (for build automation)

**Recommended:**
- Windows Terminal (PowerShell 7+)
- Visual Studio Code (with Go extension)
- Resource hacker (for .res files)

### Build Dependencies

```powershell
# Install Go (if not already installed)
winget install Golang.Go

# Verify installation
go version
```

### PowerShell Requirements

- **Version:** PowerShell 5.1 or later
- **Execution Policy:** Allow local scripts (may need `Set-ExecutionPolicy`)

```powershell
# Check execution policy
Get-ExecutionPolicy

# Allow local scripts (if needed)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
```

---

## Building

### Build Commands

**Standard Build (with v2 baseline for compatibility):**
```bash
# From WSL/Git Bash
GOOS=windows GOARCH=amd64 GOAMD64=v2 go build \
  -ldflags="-s -w -X main.version=$(git describe --tags --always --dirty)" \
  -o dist/mpv-manager-win-x86_64.exe ./cmd/mpv-manager

# From PowerShell
$env:GOOS="windows"; $env:GOARCH="amd64"; $env:GOAMD64="v2"; go build `
  -ldflags="-s -w -X main.version=$(git describe --tags --always --dirty)" `
  -o dist/mpv-manager-win-x86_64.exe ./cmd/mpv-manager
```

**With Embedded Resources:**
```bash
make build-windows-x86_64
```

**All Platforms:**
```bash
make build-all
```

### Resource Files

**Windows Resources:** `internal/assets/installer.rc`

```rc
#include <windows.h>

// Version Information
1 VERSIONINFO FILEVERSION 1,2,3,0
1 VERSIONINFO PRODUCTVERSION 1,2,3,0
1 VERSIONINFO PRODUCTNAME "mpv-manager"
1 VERSIONINFO FILEDESCRIPTION "MPV.Rocks Installer"
1 VERSIONINFO LEGALCOPYRIGHT "Copyright 2026 MPV.Rocks"
1 VERSIONINFO COMPANYNAME "MPV.Rocks"

// Icons
1 IDI_ICON1 ICON "icon-128.png"

// Manifest
1 1 RT_MANIFEST "mpv-manager.exe"
```

---

## Installation

### Portable vs Installed

**Portable Installation:**
- Binary location: `~/mpv/`
- Config location: `~/mpv/portable_config/`
- No registry entries
- No file associations (initially)

**Installed Location:**
- Binary location: `C:\Program Files\mpv-manager\`
- Config location: `%USERPROFILE%\.config\mpv\`
- Registry entries for file associations
- Desktop and start menu shortcuts

### Installer Structure

```
mpv-manager/
├── mpv-manager.exe          # Main binary
├── mpv/                     # Portable MPV (if portable install)
├── portable_config/          # Configuration directory
├── installer/                # Installation scripts
│   ├── mpv-manager.bat      # MPV setup
│   ├── install-uosc.bat      # uosc setup
│   ├── create-shortcut.vbs  # Shortcut creation
│   └── remove-shortcut.vbs  # Shortcut removal
└── mpv-manager.ico          # Icon file
```

---

## File Associations

### Supported File Types

**Video Files:**
- `.mp4` - MP4 Video
- `.mkv` - Matroska Video
- `.webm` - WebM Video
- `.avi` - AVI Video
- `.mov` - QuickTime Video
- `.flv` - Flash Video
- `.wmv` - Windows Media Video
- `.m4v` - MPEG-4 Video

### File Association Methods

**Batch File Operations:**

**Install File Associations:**
```batch
@echo off
setlocal

:: UAC elevation
powershell -Command "Start-Process '%~f0' -ArgumentList '-Verb','RunAs' -WindowStyle Hidden" "%~dp0"

:: Wait for completion
timeout /t 5 >nul

:: Configure MPV
"%~dp0\mpv.exe" --mpv-config="%%USERPROFILE%%\.config\mpv\"

exit /b
```

**Remove File Associations:**
```batch
@echo off
setlocal

:: UAC elevation
powershell -Command "Start-Process '%~f0' -ArgumentList '-Verb','RunAs' -WindowStyle Hidden" "%~dp0"

:: Wait for completion
timeout /t 5 >nul

:: Remove associations
"%~dp0\mpv.exe" --restore-defaults

exit /b
```

### Fire-and-Forget Pattern

**Problem:** Blocking on UAC prompt causes installer to hang.

**Solution:** Non-blocking UAC elevation
```batch
:: Remove pause commands from batch files
powershell -Command "Start-Process -Verb RunAs" -WindowStyle Hidden
:: 500ms sleep to allow PowerShell to spawn elevated window
:: Installation completes immediately after launching
```

---

## GPU Detection

### PowerShell Detection

**Command:**
```powershell
Get-WmiObject Win32_VideoController | Select-Object Name, DriverVersion
```

**Example Output:**
```
Name           DriverVersion
----           --------------
NVIDIA GeForce RTX 4090 31.0.15.2872
AMD Radeon RX 7900 XTX  31.0.11000.1401
Intel UHD Graphics 770  30.0.10129
```

### GPU Model to Codec Mapping

**NVIDIA:**
- RTX 20 (Turing): no AV1
- RTX 30 (Ampere): AV1
- RTX 40 (Ada Lovelace): AV1
- RTX 50 (Blackwell): AV1
- GTX 950 (Maxwell 2nd): HEVC/VP9 (950 variant)
- GTX 750/750 Ti (Maxwell 1st): no HEVC/VP9

**AMD:**
- RX 7900/7800/7600 (RDNA 3): AV1
- RX 6800/6700/6600 (RDNA 2): AV1
- RX 6500 XT (RDNA 2): no AV1

**Intel:**
- Xe-HPG (Alchemist): AV1
- Arc (Battlemage): AV1
- UHD 700 (Xe-LPG): no AV1
- UHD 750/Xe-HPG+ (Xe2-LPG): no AV1

---

## Console Handling

### Auto-Resize

**Implementation:**
```go
func autoResizeConsole() error {
    cmd := exec.Command("powershell", "-Command",
        "$host.UI.RawUI.SuppressOutput() = $true; mode 120,40")
    if err := cmd.Run(); err != nil {
        log.Warning("Failed to resize console: %v", err)
    }
}
```

**Called in:** `cmd/mpv-manager/main.go` before TUI initialization

**Result:**
- Console resizes to 120x40 characters
- Full menu visibility
- Success messages properly displayed

### PowerShell Window Configuration

**Title:** MPV.Rocks Installer
**Colors:** Default PowerShell colors

**Window Style:**
```powershell
$psh = Get-Host
$window = $psh.UI.RawUI.Window("MPV.Rocks Installer", "MPV.Rocks Installer", "Present", "AlwaysOnTop", "Normal")
```

---

## Troubleshooting

### Silent Crashes

**Symptom:** Application crashes before showing window with no error messages.

**Cause:** x86-64-v4 binary (AVX-512) on CPU without AVX-512 support.

**Solution:**
```bash
# Always build with GOAMD64=v2
GOOS=windows GOARCH=amd64 GOAMD64=v2 go build -o mpv-manager-win-x86_64.exe
```

### UAC Elevation Issues

**Symptom:** Operations fail silently or show "access denied" errors.

**Solution:**
- Run as administrator (right-click → Run as administrator)
- Or press Ctrl+Shift while clicking

### Console Window Issues

**Symptom:** Menu items cut off, only 2 lines visible.

**Solution:**
- Auto-resize to 120x40 happens automatically
- If issues persist, run `powershell -Command "mode 120,40"` before starting

### Path Issues

**Symptom:** Configuration not saved or loaded.

**Solution:**
```powershell
# Check portable config location
$env:USERPROFILE\mpv\portable_config

# Check installed config location
$env:USERPROFILE\.config\mpv
```

---

## Quick Reference

### Common Commands

```powershell
# Check Go version
go version

# Build installer
go build -o mpv-manager.exe ./cmd/mpv-manager

# Run with debug mode
.\mpv-manager.exe --debug

# Run with verbose mode
.\mpv-manager.exe --verbose

# Check version
.\mpv-manager.exe --version
```

### Registry Keys

**File Associations:**
```
HKEY_CLASSES_ROOT\.mp4\Shell\Open\Command
HKEY_CLASSES_ROOT\.mp4\Shell\Open\command

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mp4
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mkv
```

**Uninstall Information:**
```
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\mpv-manager
```

---

## Conclusion

This Windows platform guide provides comprehensive information for building, installing, and troubleshooting the MPV.Rocks Installer on Windows.

For additional information, see:
- [Architecture](../ARCHITECTURE.md) - System architecture and design
- [Build & Deployment](../BUILD_DEPLOYMENT.md) - Build procedures and deployment
- [GPU Codec Detection](../FEATURES/GPU_CODEC_DETECTION.md) - GPU detection system
- [Troubleshooting](../TROUBLESHOOTING.md) - Common issues and solutions
