Files
Reims-Go/internal/opencore/lang.go
T
Nwflower b2f7f036d8 feat(m5): add macOS prev-lang preset, disk resize/compact, audio diagnostics, and VM thumbnails
- M5.5: add pure-Go internal/opencore reader/in-place patcher for OpenCore NVRAM prev-lang:kbd with sidecar persistence, automatic replay after update-opencore/graphics changes/CoW clones, wizard/settings/CLI (--guest-lang) support
- M5.6: add stopped VM disk resize (qemu-img resize + dynamic macOS APFS expand command) and standalone qcow2 compaction (backing-file guard, free-space check, qemu-img check, atomic swap) in GUI Settings and CLI (vm resize/compact)
- M5.9: add Win32 winmm audio output/input device probing, fix reims-ctl show audio_backend parsing, expose audio backend selector (auto/dsound/none) and latency/mic diagnostics in Settings & CLI
- UI & thumbnails: add QMP screendump VM thumbnails (internal/thumb, thumbnail_flow.go), VMPage/CreateWizard layout polish, and docs/ui-audit.md
- Update docs/roadmap.md for M5.5, M5.6, and M5.9
2026-11-28 10:17:54 -07:00

113 lines
3.4 KiB
Go

package opencore
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/Nwflower/reims-go/internal/i18n"
)
const (
// GuestLangSidecarFile stores the user's chosen OpenCore prev-lang:kbd value
// inside the VM directory so `update-opencore` rebuilds can replay it.
GuestLangSidecarFile = ".reims-go-guest-lang"
)
// Preset describes a selectable macOS boot/recovery language (`prev-lang:kbd`).
type Preset struct {
Value string `json:"value"`
LabelZH string `json:"label_zh"`
LabelEN string `json:"label_en"`
}
// Presets returns the built-in macOS prev-lang:kbd choices.
// `:0` pairs the locale with the standard US keyboard layout so terminal
// commands in macOS Recovery / Setup Assistant stay predictable; `:252` uses
// OpenCore's Pinyin layout ID.
func Presets() []Preset {
return []Preset{
{Value: "zh-Hans:0", LabelZH: "简体中文(美式键盘,推荐)", LabelEN: "Simplified Chinese (US Keyboard)"},
{Value: "zh-Hans:252", LabelZH: "简体中文(拼音键盘)", LabelEN: "Simplified Chinese (Pinyin)"},
{Value: "zh-Hant:0", LabelZH: "繁體中文(美式键盘)", LabelEN: "Traditional Chinese (US Keyboard)"},
{Value: "en-US:0", LabelZH: "English (US)", LabelEN: "English (US)"},
{Value: "ja-JP:0", LabelZH: "日本語(美式键盘)", LabelEN: "Japanese (US Keyboard)"},
{Value: "ko-KR:0", LabelZH: "한국어(美式键盘)", LabelEN: "Korean (US Keyboard)"},
}
}
// DefaultGuestLang chooses the default macOS prev-lang:kbd matching the app UI locale.
func DefaultGuestLang(locale i18n.Locale) string {
if locale == i18n.EnUS {
return "en-US:0"
}
return "zh-Hans:0"
}
var reGuestLang = regexp.MustCompile(`^[A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,2}:[0-9]{1,6}$`)
// NormalizeGuestLang validates and normalizes a prev-lang:kbd string.
// An empty input returns ("", nil), meaning "unspecified / keep existing".
func NormalizeGuestLang(raw string) (string, error) {
s := strings.TrimSpace(raw)
if s == "" {
return "", nil
}
if !reGuestLang.MatchString(s) {
return "", fmt.Errorf("invalid macOS language code %q (expected format like zh-Hans:0 or en-US:0)", raw)
}
return s, nil
}
// ReadSavedGuestLang reads the persisted prev-lang:kbd preference from vmDir.
func ReadSavedGuestLang(vmDir string) string {
if vmDir == "" {
return ""
}
b, err := os.ReadFile(filepath.Join(vmDir, GuestLangSidecarFile))
if err != nil {
return ""
}
norm, err := NormalizeGuestLang(string(b))
if err != nil {
return ""
}
return norm
}
// WriteSavedGuestLang saves (or clears when empty) the VM's prev-lang:kbd preference.
func WriteSavedGuestLang(vmDir, lang string) error {
if vmDir == "" {
return fmt.Errorf("vm directory is empty")
}
norm, err := NormalizeGuestLang(lang)
if err != nil {
return err
}
p := filepath.Join(vmDir, GuestLangSidecarFile)
if norm == "" {
if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
return os.WriteFile(p, []byte(norm+"\n"), 0o644)
}
// ResolveOpenCorePath returns the OpenCore image path inside vmDir (preferring
// opencore.qcow2, falling back to opencore.img), or "" if neither exists.
func ResolveOpenCorePath(vmDir string) string {
if vmDir == "" {
return ""
}
for _, name := range []string{"opencore.qcow2", "opencore.img"} {
p := filepath.Join(vmDir, name)
if info, err := os.Stat(p); err == nil && !info.IsDir() {
return p
}
}
return ""
}