Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ modavd [--root <dir>] [--json] [--quiet] [--verbose] <command> [args]
| `list` | All instances and their status |
| `info <i>` / `status <i>` | Details of one instance / its status |
| `create [--index N] [--title T] [--memory 6G] [--cpu N] [--data-gb auto\|N] [--fps 1-60] [--resolution 1080\|720] [--no-bridge] [--timeout 1200]`, `create --resume N` | Create an instance (automatic data disk by default, see below), then start it once in the background, headless, to finish initialization, and shut it down (see below). The arm64 execution bridge is included by default; `--no-bridge` records that this instance does not want the bridge, so it is not installed on later starts either. New instances never get the Google Play Store; add it afterwards with `playstore <i> install`. `--resume N` redoes the initialization of an instance whose creation failed |
| `set <i> [--title T] [--memory M] [--cpu N] [--fps F] [--resolution 1080\|720] [--data-gb auto\|N] [--diagnostics on\|off] [--boss-key combination\|none]` | Change settings. Diagnostics and the boss key apply immediately; other settings take effect at the next start. `--data-gb auto` makes the data disk automatic, `--data-gb N` gives it a fixed size (see below); the size can only be increased, and game data is untouched: the emulator (from 0.36) grows the disk image at start and grows the file system online after boot. A disk created before 0.36 at 16 GB or smaller can only be grown to just below the next backup group (a 16 GB disk to about 30 GB). `--boss-key` is this instance's boss key (see the ModAVD.exe manager section), for example `Ctrl+Alt+Shift+1`; `none` means no boss key; Windows only. |
| `set <i> [--title T] [--memory M] [--cpu N] [--fps F] [--resolution 1080\|720] [--data-gb auto\|N] [--diagnostics on\|off] [--sound on\|off] [--boss-key combination\|none]` | Change settings. Diagnostics and the boss key apply immediately; other settings take effect at the next start. `--data-gb auto` makes the data disk automatic, `--data-gb N` gives it a fixed size (see below); the size can only be increased, and game data is untouched: the emulator (from 0.36) grows the disk image at start and grows the file system online after boot. A disk created before 0.36 at 16 GB or smaller can only be grown to just below the next backup group (a 16 GB disk to about 30 GB). `--sound on` gives this instance sound from its next start (windowed and headless, at some extra CPU); the default `off` boots without a sound card; stored as `instances.<i>.sound` in `mas-avd.json`. `--boss-key` is this instance's boss key (see the ModAVD.exe manager section), for example `Ctrl+Alt+Shift+1`; `none` means no boss key; Windows only. |
| `proxy <i> [on\|off\|test] [--server URL\|auto] [--add PKG] [--remove PKG] [--apps PKG]` | Per-app proxy: the listed apps go through a proxy, the others direct (see below) |
| `delete <i> --yes` | Delete a stopped instance, together with its game data |
| `start <i> [--window \| --headless] [--timeout 300] [--no-bridge]` | Start (headless by default, with the window for `--window`), wait until boot finishes and the post-boot setup is done, and before returning confirm that files can be written to `/sdcard` (normally it is ready long before this; it waits up to 60 more seconds and only warns at the deadline). If the instance is shutting down (for example, right after a click on the emulator window's ×), it first waits for the shutdown to finish (up to 30 seconds) and then starts it. If the instance has no execution bridge yet, install it afterward; `--no-bridge` skips that this time. The install takes a cold boot or two: with `--window`, the boots that only prepare it are headless, and after a normal shutdown the last boot has the window (when the instance has no boot marker yet, so the bridge is surely to be installed, the first boot is headless too; with the marker there, whether the bridge is installed is only known once it runs, so the first boot has the window). No switch while it runs: if the instance is already running and `--window` / `--headless` asks for the other way, it fails (exit code 5) without taking it over or restarting it; to switch, `stop` and then `start`. With neither, it is taken over as it runs |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ modavd [--root <目录>] [--json] [--quiet] [--verbose] <命令> [参数]
| `list` | 全部实例和状态 |
| `info <i>` / `status <i>` | 一台实例的详细信息 / 状态 |
| `create [--index N] [--title T] [--memory 6G] [--cpu N] [--data-gb auto\|N] [--fps 1-60] [--resolution 1080\|720] [--no-bridge] [--timeout 1200]`、`create --resume N` | 新建(数据盘默认自动扩盘,见下),并在后台无头开机一次把初始化做完再关机(见下)。默认带 arm64 执行桥;`--no-bridge` 记下这台不要桥,以后开机也不装。新建的实例不带 Google Play 商店,要的话建好后用 `playstore <i> install`。`--resume N` 重做新建失败的那台的初始化 |
| `set <i> [--title T] [--memory M] [--cpu N] [--fps F] [--resolution 1080\|720] [--data-gb auto\|N] [--diagnostics on\|off] [--boss-key 组合\|none]` | 修改;诊断模式和老板键立即生效,其余设置下次开机生效。`--data-gb auto` 改成自动扩盘,`--data-gb N` 改成指定大小(见下);大小只能改大,游戏数据不动:模拟器(0.36 起)开机时扩磁盘镜像,开机后在线扩文件系统。0.36 之前按 16 GB 及以下建的盘只能扩到下一个备份组之前(16 GB 约到 30 GB)。`--boss-key` 是这台的老板键(见「管理器 ModAVD.exe」一节),比如 `Ctrl+Alt+Shift+1`,`none` 是不要,只有 Windows。 |
| `set <i> [--title T] [--memory M] [--cpu N] [--fps F] [--resolution 1080\|720] [--data-gb auto\|N] [--diagnostics on\|off] [--sound on\|off] [--boss-key 组合\|none]` | 修改;诊断模式和老板键立即生效,其余设置下次开机生效。`--data-gb auto` 改成自动扩盘,`--data-gb N` 改成指定大小(见下);大小只能改大,游戏数据不动:模拟器(0.36 起)开机时扩磁盘镜像,开机后在线扩文件系统。0.36 之前按 16 GB 及以下建的盘只能扩到下一个备份组之前(16 GB 约到 30 GB)。`--sound on` 让这台下次开机起有声音(带窗口和无头都是,会多占一些 CPU),默认 `off` 不带声卡;记在 `mas-avd.json` 的 `instances.<i>.sound`。`--boss-key` 是这台的老板键(见「管理器 ModAVD.exe」一节),比如 `Ctrl+Alt+Shift+1`,`none` 是不要,只有 Windows。 |
| `proxy <i> [on\|off\|test] [--server URL\|auto] [--add 包名] [--remove 包名] [--apps 包名]` | 按应用走代理:名单里的应用走代理,其余直连(见下) |
| `delete <i> --yes` | 删除已关机的实例,连同游戏数据 |
| `start <i> [--window \| --headless] [--timeout 300] [--no-bridge]` | 开机(默认无头,`--window` 带窗口),等开完并做完开机后的设置,返回前确认 `/sdcard` 能写进文件(一般这时早已就绪;最多再等 60 秒,到点只警告);实例正在关机时(比如刚点了模拟器窗口的 ×)先等它关完(最多 30 秒)再开机;实例还没有执行桥时接着装上,`--no-bridge` 这次不装;装桥要冷启动一两次,带 `--window` 时这几次准备用的开机都无头,装好后正常关机,最后一次才带窗口开机(实例还没有开机标记、一定要装桥时,第一次开机就无头;有标记的要开起来才知道装没装,第一次照样带窗口)。运行中不换方式:实例已经在运行时,给了 `--window` / `--headless` 而它不是这种方式,就报错(退出码 5),不接管也不重启,要换先 `stop` 再 `start`;两个都不给就照原样接管 |
Expand Down
1 change: 1 addition & 0 deletions changelog.d/226-sound.feat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
实例设置里可以打开声音,开了以后模拟器有声音 (ModAVD/ModAVD#226)
64 changes: 58 additions & 6 deletions internal/avd/avd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const (
DataDiskModeKey = "dataDiskMode"
DataDiskAuto = "auto"
DataDiskManual = "manual"
// SoundKey is instances.<i>.sound in mas-avd.json: true gives the instance a sound card from its next boot
// (ModAVD/ModAVD#226); anything else (absent: every instance before it) keeps it without one, the default. The
// boot reads only this key; config.ini's hw.audioInput / hw.audioOutput are written along with it (SoundConfig).
SoundKey = "sound"
// New instances default to 1080p. Existing instances are identified by their dimensions.
DefaultResolution = 1080
TargetAPI = "android-34"
Expand All @@ -60,8 +64,10 @@ const (
)

// ConfigTemplate is config.ini of a new instance before the per-instance keys (AUTO-MAS _CONFIG_TEMPLATE, same
// order). Taken from the tuned mas_p0: landscape, host GPU, no device frame, no sound card keys (the real switch is
// -feature -VirtioSndCard), hw.mainKeys=yes (no on-screen navigation bar; ModAVD #1).
// order). Taken from the tuned mas_p0: landscape, host GPU, no device frame, sound keys hw.audioInput / hw.audioOutput
// set to no (the real switch is -feature -VirtioSndCard; the instance's sound switch, SoundKey, writes them yes),
// hw.mainKeys=yes (no on-screen
// navigation bar; ModAVD #1).
var ConfigTemplate = [][2]string{
{"PlayStore.enabled", "no"},
{"abi.type", "x86_64"},
Expand Down Expand Up @@ -331,6 +337,22 @@ func DataDiskAutoMeta(meta *jsonord.Obj) bool {
return s != DataDiskManual
}

// SoundMeta: the instance with metadata meta boots with a sound card (SoundKey is true; nil metadata, a missing key or
// any other value is off).
func SoundMeta(meta *jsonord.Obj) bool {
on, ok := meta.Get(SoundKey).BoolVal()
return ok && on
}

// SoundConfig is config.ini's sound keys for the switch: hw.audioInput and hw.audioOutput, both yes or both no.
func SoundConfig(on bool) [][2]string {
v := "no"
if on {
v = "yes"
}
return [][2]string{{"hw.audioInput", v}, {"hw.audioOutput", v}}
}

// Display returns the size and density the instance will boot with (after DisplayChanges).
func (in *Instance) Display() (width, height, dpi int) {
ch := DisplayChanges(in.Config)
Expand Down Expand Up @@ -777,15 +799,21 @@ type UpdateOptions struct {
// DataDiskAuto makes the data disk automatic (dataDiskMode auto) and raises it to AutoDataPartitionGB unless it is
// larger already. Not together with DataPartitionGB.
DataDiskAuto bool
// Sound turns the instance's sound card on or off from its next boot: instances.<i>.sound (SoundKey) and
// config.ini's hw.audioInput / hw.audioOutput (SoundConfig).
Sound *bool
}

// Update writes the changed keys like AUTO-MAS write_instance_settings: hw.ramSize=<n>M and hw.cpu.ncore always
// (current values when not changed), hw.lcd.vsync only when FPS is given, avd.ini.displayname when Title is given,
// disk.dataPartition.size when DataPartitionGB is given, and instances.<i>.memoryMb always. Validates first
// (memory / cpu / fps steps). Returns the applied changes by option name ("title", "memory_mb", "cpu", "fps",
// "data_partition_gb"). Takes effect at the next boot.
// disk.dataPartition.size when DataPartitionGB is given, hw.audioInput / hw.audioOutput and instances.<i>.sound when
// Sound is given, and instances.<i>.memoryMb always. Validates first (memory / cpu / fps steps). Returns the applied
// changes by option name ("title", "memory_mb", "cpu", "fps", "data_partition_gb", "sound"). Takes effect at the next
// boot.
//
// Sound on its own only writes the sound keys and instances.<i>.sound: nothing is validated or rewritten then.
//
// As in AUTO-MAS the unchanged values are validated too: memory is MemoryFor (old instances get 6144 written), cores
// Otherwise, as in AUTO-MAS, the unchanged values are validated too: memory is MemoryFor (old instances get 6144 written), cores
// hw.cpu.ncore (DefaultCPU when unreadable), and the instance's data partition must still be within range unless a
// new DataPartitionGB is given (an old 8 GB instance can be raised that way). A given empty Title means the AVD name,
// like Create.
Expand All @@ -805,6 +833,21 @@ func Update(r *root.Root, i int, opt UpdateOptions) (map[string]any, error) {
if err != nil {
return nil, err
}
// Sound alone is not an AUTO-MAS hardware edit: like the diagnostics switch it neither validates nor rewrites the
// unrelated values (an old out-of-range data disk, AUTO-MAS's automatic memory), only the sound keys.
if opt.Sound != nil && opt.Title == nil && opt.MemoryMB == nil && opt.CPU == nil && opt.FPS == nil &&
opt.Resolution == nil && opt.DataPartitionGB == nil && !opt.DataDiskAuto {
if _, err := r.ReadMetadata(); err != nil {
return nil, err
}
if err := writeInstanceConfig(r, i, SoundConfig(*opt.Sound)); err != nil {
return nil, err
}
if err := r.UpdateInstanceMeta(i, map[string]*jsonord.Value{SoundKey: jsonord.NewBool(*opt.Sound)}); err != nil {
return nil, err
}
return map[string]any{"sound": *opt.Sound}, nil
}
if opt.Title != nil {
if err := validateTitle(*opt.Title); err != nil {
return nil, err
Expand Down Expand Up @@ -874,6 +917,9 @@ func Update(r *root.Root, i int, opt UpdateOptions) (map[string]any, error) {
if opt.DataPartitionGB != nil || opt.DataDiskAuto {
changes = append(changes, [2]string{"disk.dataPartition.size", strconv.Itoa(data) + "G"})
}
if opt.Sound != nil {
changes = append(changes, SoundConfig(*opt.Sound)...)
}
title := ""
if opt.Title != nil {
title = *opt.Title
Expand All @@ -893,6 +939,9 @@ func Update(r *root.Root, i int, opt UpdateOptions) (map[string]any, error) {
case opt.DataPartitionGB != nil:
metaChanges[DataDiskModeKey] = jsonord.NewString(DataDiskManual)
}
if opt.Sound != nil {
metaChanges[SoundKey] = jsonord.NewBool(*opt.Sound)
}
if err := r.UpdateInstanceMeta(i, metaChanges); err != nil {
return nil, err
}
Expand All @@ -919,6 +968,9 @@ func Update(r *root.Root, i int, opt UpdateOptions) (map[string]any, error) {
applied["data_disk_mode"] = DataDiskAuto
}
}
if opt.Sound != nil {
applied["sound"] = *opt.Sound
}
return applied, nil
}

Expand Down
103 changes: 103 additions & 0 deletions internal/avd/avd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"modavd/internal/errs"
"modavd/internal/jsonord"
"modavd/internal/root"
"modavd/internal/testutil"
)
Expand Down Expand Up @@ -994,6 +995,108 @@ func TestUpdateTitleAndApplied(t *testing.T) {
}
}

// TestUpdateSound: the sound switch (ModAVD/ModAVD#226) is off for a new instance, and Update writes it to
// instances.<i>.sound and config.ini's hw.audioInput / hw.audioOutput in place, nothing else of config.ini changing.
func TestUpdateSound(t *testing.T) {
r := newRoot(t)
if _, err := Create(r, 0, CreateOptions{MemoryMB: 4096, CPU: 4, DataPartitionGB: 64, FPS: 60}); err != nil {
t.Fatal(err)
}
if SoundMeta(r.InstanceMeta(0)) {
t.Fatal("a new instance has sound")
}
before := string(readFile(t, ConfigPath(r, 0)))
if !strings.Contains(before, "hw.audioInput=no\r\n") || !strings.Contains(before, "hw.audioOutput=no\r\n") {
t.Fatalf("new config.ini: %q", before)
}
for _, on := range []bool{true, false} {
applied, err := Update(r, 0, UpdateOptions{Sound: &on})
if err != nil {
t.Fatal(err)
}
if applied["sound"] != on || len(applied) != 1 {
t.Errorf("sound %v: applied %v", on, applied)
}
if SoundMeta(r.InstanceMeta(0)) != on {
t.Errorf("sound %v: instances.0.sound = %v", on, SoundMeta(r.InstanceMeta(0)))
}
want := before
if on {
want = strings.NewReplacer("hw.audioInput=no\r\n", "hw.audioInput=yes\r\n",
"hw.audioOutput=no\r\n", "hw.audioOutput=yes\r\n").Replace(before)
}
if got := string(readFile(t, ConfigPath(r, 0))); got != want {
t.Errorf("sound %v: config\n got %q\nwant %q", on, got, want)
}
}

// Sound alone neither validates nor rewrites unrelated values: an old 8 GB data disk is not refused, AUTO-MAS's
// automatic memory (no memoryMb) stays automatic, hw.ramSize stays as it was.
cfg := strings.NewReplacer("disk.dataPartition.size=64G", "disk.dataPartition.size=8G",
"hw.ramSize=4096M", "hw.ramSize=3000M").Replace(before)
if strings.Count(cfg, "=8G\r\n")+strings.Count(cfg, "=3000M\r\n") != 2 {
t.Fatalf("old config: %q", cfg)
}
writeFile(t, ConfigPath(r, 0), []byte(cfg))
if err := r.EditInstanceMeta(0, func(meta *jsonord.Obj) error { meta.Delete("memoryMb"); return nil }); err != nil {
t.Fatal(err)
}
on := true
if applied, err := Update(r, 0, UpdateOptions{Sound: &on}); err != nil || fmt.Sprint(applied) != "map[sound:true]" {
t.Fatalf("sound on an old instance: %v %v", applied, err)
}
want := strings.NewReplacer("hw.audioInput=no\r\n", "hw.audioInput=yes\r\n",
"hw.audioOutput=no\r\n", "hw.audioOutput=yes\r\n").Replace(cfg)
if got := string(readFile(t, ConfigPath(r, 0))); got != want {
t.Errorf("sound on an old instance: config\n got %q\nwant %q", got, want)
}
if r.InstanceMeta(0).Has("memoryMb") || !SoundMeta(r.InstanceMeta(0)) {
t.Errorf("sound on an old instance: metadata %v", r.InstanceMeta(0))
}

// Together with a hardware setting it goes the whole way, sound keys included.
writeFile(t, ConfigPath(r, 0), []byte(before))
off := false
if applied, err := Update(r, 0, UpdateOptions{Sound: &off, CPU: ip(6)}); err != nil ||
fmt.Sprint(applied) != "map[cpu:6 sound:false]" {
t.Fatalf("sound with cpu: %v %v", applied, err)
}
got := string(readFile(t, ConfigPath(r, 0)))
if !strings.Contains(got, "hw.cpu.ncore=6\r\n") || !strings.Contains(got, "hw.audioInput=no\r\n") ||
!strings.Contains(got, "hw.audioOutput=no\r\n") || SoundMeta(r.InstanceMeta(0)) {
t.Errorf("sound with cpu: sound %v, config %q", SoundMeta(r.InstanceMeta(0)), got)
}
writeFile(t, ConfigPath(r, 0), []byte(before))
if _, err := Update(r, 0, UpdateOptions{Sound: &on, CPU: ip(4)}); err != nil {
t.Fatal(err)
}
if got := string(readFile(t, ConfigPath(r, 0))); !strings.Contains(got, "hw.audioOutput=yes\r\n") ||
!SoundMeta(r.InstanceMeta(0)) {
t.Errorf("sound on with cpu: sound %v, config %q", SoundMeta(r.InstanceMeta(0)), got)
}
}

// TestSoundMeta: only a JSON true turns sound on.
func TestSoundMeta(t *testing.T) {
if SoundMeta(nil) {
t.Error("nil metadata")
}
for text, want := range map[string]bool{`{}`: false, `{"sound":true}`: true, `{"sound":false}`: false,
`{"sound":"true"}`: false, `{"sound":1}`: false} {
v, err := jsonord.Parse([]byte(text))
if err != nil {
t.Fatal(err)
}
if got := SoundMeta(v.Obj()); got != want {
t.Errorf("%s: SoundMeta = %v, want %v", text, got, want)
}
}
if fmt.Sprint(SoundConfig(true)) != "[[hw.audioInput yes] [hw.audioOutput yes]]" ||
fmt.Sprint(SoundConfig(false)) != "[[hw.audioInput no] [hw.audioOutput no]]" {
t.Errorf("SoundConfig: %v %v", SoundConfig(true), SoundConfig(false))
}
}

func TestUpdateDataPartition(t *testing.T) {
r := newRoot(t)
if _, err := Create(r, 0, CreateOptions{MemoryMB: 4096, CPU: 4, DataPartitionGB: 64, FPS: 60}); err != nil {
Expand Down
Loading
Loading