Skip to content
Closed
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 dm/games/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ None of them has a default game: without one they stop and list the profiles. To
- `python3 dm/games/game.py list` lists the profiles.
- `python3 dm/games/game.py <id> <key>` prints one key.

**Launcher settings.** `settings.tsv` lists the player-facing settings once (the environment variable each sets, its kind, default, the condition under which a game offers it and its section on the single settings screen, **Game**, **Controls** or **Performance**; Reset to tested defaults forgets all launcher choices so the profile's defaults apply again; its header describes the columns). A profile key a setting defaults to (`DM_GAME_<NAME>`) is the value the game was tested with, which the player page [docs/settings.md](../../docs/settings.md#tested-defaults) explains. `build_ipa.sh` runs `settings.py <id> <rewritten modules> <control.nacp> <out.json>`, which keeps the rows that hold for the game: a profile macro (`DM_GAME_FPS_TARGET!=0`), a key in `main`'s rewriter table (`RENDER_SCALE`, written by `patch_nso.py` for the builds with a render-scale stub), the game's languages. It writes them into the app as `dm/launcher.json`, from which the iOS launcher (`dm/ios/Sources/Launcher.swift`) draws its screen; the NSP script packs the Mac app's copy (`SETTINGS_PLATFORM=mac`, without the rows marked `platform:ios`, `Contents/Resources/dm/launcher.json`) for a Mac front end, which shows the list and starts the app with the chosen values in its environment. A new player setting is one row; diagnostic switches stay environment-only.
**Launcher settings.** `settings.tsv` lists the player-facing settings once (the environment variable each sets, its kind, default, the condition under which a game offers it and its section on the single settings screen, **Game**, **Controls** or **Performance**; Reset to tested defaults forgets all launcher choices so the profile's defaults apply again; its header describes the columns). A profile key a setting defaults to (`DM_GAME_<NAME>`) is the value the game was tested with, which the player page [docs/settings.md](../../docs/settings.md#tested-defaults) explains. `build_ipa.sh` runs `settings.py <id> <rewritten modules> <control.nacp> <out.json>`, which keeps the rows that hold for the game: a profile macro (`DM_GAME_FPS_TARGET!=0`), a key in `main`'s rewriter table (`RENDER_SCALE`, written by `patch_nso.py` for the builds with a render-scale stub), the game's languages. It writes them into the app as `dm/launcher.json`, from which the iOS launcher (`dm/ios/Sources/Launcher.swift`) draws its screen; the NSP script packs the Mac app's copy (`SETTINGS_PLATFORM=mac`, without the rows marked `platform:ios`, `Contents/Resources/dm/launcher.json`) for a Mac front end, which shows the list and starts the app with the chosen values in its environment. A row's optional last column, `meta`, carries `key=value` fields front ends read (`level=basic`, `needs=<VAR>=<value>`; [Front ends](../../docs/frontends.md#the-settings-list), with the JSON Schema in `docs/launcher.schema.json`). A new player setting is one row; diagnostic switches stay environment-only.

Defaults may use `<ios>|<mac>` to select the platform's value before resolving a profile macro or build variable. For example, `DM_GAME_PSO_MTL4|1` follows the profile on iOS and the Mac host's always-on default on Mac; `1|0` matches the encode thread's iOS-on, Mac-off defaults. `SETTINGS_PLATFORM` accepts `ios` (the default) or `mac`; other values are rejected.

Expand Down
41 changes: 35 additions & 6 deletions dm/games/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,34 @@
import game, nacp

SECTIONS = ('Game', 'Controls', 'Performance') # headings on the single settings screen
SCHEMA = 1 # launcher.json's version (docs/launcher.schema.json); it grows only by optional fields, so a reader of 1 reads every 1
# The meta column's keys, each with the pattern its value must match. A key is added here, in docs/frontends.md and in
# docs/launcher.schema.json together (test_launcher_settings.py checks that the three agree); a reader ignores keys it does not know
META = {
'level': r'basic', # a setting a front end shows first; without it, the front end decides
'needs': r'DM_[A-Z0-9_]+=[^;=]+', # <VAR>=<value>: a sub-setting, drawn only while the row that sets <VAR> has that value
}


def meta(text, n):
"""The meta column as {key: value}: key=value pairs separated by ;, each key from META once, its value matching its pattern"""
out = {}
for pair in filter(None, (p.strip() for p in text.split(';'))):
key, eq, value = pair.partition('=')
if not eq or key not in META: sys.exit(f'settings.tsv:{n}: unknown meta key {pair!r} (keys: {", ".join(META)})')
if key in out: sys.exit(f'settings.tsv:{n}: meta key {key} given twice')
if not re.fullmatch(META[key], value): sys.exit(f'settings.tsv:{n}: meta {key}={value!r} does not match {META[key]}')
out[key] = value
return out


def rows():
for n, line in enumerate((HERE / 'settings.tsv').read_text(encoding='utf-8').splitlines(), 1):
if not line.strip() or line.startswith('#'): continue
c = line.split('\t')
if len(c) not in (7, 8): sys.exit(f'settings.tsv:{n}: 7 or 8 columns expected, {len(c)} found')
if len(c) not in (7, 8, 9): sys.exit(f'settings.tsv:{n}: 7 to 9 columns expected, {len(c)} found')
if c[5] not in SECTIONS: sys.exit(f'settings.tsv:{n}: unknown section: {c[5]}')
yield n, c + [''] * (8 - len(c))
yield n, c + [''] * (9 - len(c))


def fixups_keys(path): # '# KEY<TAB>value' header rows of a rewriter table
Expand Down Expand Up @@ -60,10 +79,20 @@
return {'YES': '1', 'NO': '0', '': '0'}.get(v, v) # build_ipa.sh's yes / no switches
return d

out = []
for n, (var, kind, values, default, cond, section, label, note) in rows():
def takes(kind, values): # the values a row's variable can take, for needs; None when any (a range, the game's languages)
if kind == 'toggle': return {'1', '0'} if values == '-' else set(values.split('/'))
if kind == 'choice' and values != 'languages': return {v.split('=', 1)[0] for v in values.split(',')}
return None

out, table = [], {r[1][0]: takes(r[1][1], r[1][2]) for r in rows()}
for n, (var, kind, values, default, cond, section, label, note, extra) in rows():
m = meta(extra, n)
if 'needs' in m:
need, want = m['needs'].split('=', 1)
if need not in table: sys.exit(f'settings.tsv:{n}: needs names a variable no row sets: {m["needs"]}')
if table[need] is not None and want not in table[need]: sys.exit(f'settings.tsv:{n}: needs a value {need} never takes: {want} (it takes {", ".join(sorted(table[need]))})')

Check failure on line 93 in dm/games/settings.py

View workflow job for this annotation

GitHub Actions / ruff and pyright (ratchet)

pyright reportArgumentType

Argument of type "set[str] | None" cannot be assigned to parameter "iterable" of type "Iterable[SupportsRichComparisonT@sorted]" in function "sorted" Type "set[str] | None" is not assignable to type "Iterable[str]" "None" is incompatible with protocol "Iterable[str]" "__iter__" is not present
if not shown(cond, n): continue
s = {'env': var, 'kind': kind, 'section': section, 'label': label, 'note': note, 'default': value(default, n)}
s = {'env': var, 'kind': kind, 'section': section, 'label': label, 'note': note, 'default': value(default, n), **m}
if kind == 'choice':
opts = [list(l) for l in langs] if values == 'languages' else [v.split('=', 1) for v in values.split(',')]
if any(len(o) != 2 for o in opts): sys.exit(f'settings.tsv:{n}: choice values are value=label,...')
Expand All @@ -79,7 +108,7 @@
s.update(on=on_off[0], off=on_off[1], default=on_off[0] if s['default'] == '1' else on_off[1]) # the default as the value the variable takes
else: sys.exit(f'settings.tsv:{n}: unknown kind {kind}')
out.append(s)
return {'game': g.id, 'settings': out}
return {'schema': SCHEMA, 'game': g.id, 'settings': out}


if __name__ == '__main__':
Expand Down
14 changes: 8 additions & 6 deletions dm/games/settings.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
# section Game, Controls or Performance: a heading on the single settings screen. Rows appear in table order within each section
# Reset to tested defaults clears all launcher choices; values fixed by an env file stay
# label, note the setting's name and the line under it (may be empty; only for a setting with a cost: latency, memory, heat)
DM_FPS_TARGET choice 30=30 fps,60=60 fps DM_GAME_FPS_TARGET DM_GAME_FPS_TARGET!=0 Game Frame rate
DM_RENDER_SCALE range 0.5:1.0:0.05 1.0 fixups:RENDER_SCALE Game Render scale
DM_OPMODE choice 1=Docked,0=Handheld DM_GAME_OPMODE always Game Console mode
# meta optional, key=value pairs separated by ; (settings.py META lists the keys; docs/frontends.md, The settings list, describes them):
# level=basic a setting a front end shows first; needs=<VAR>=<value> a sub-setting, drawn only while the row that sets <VAR> has that value
DM_FPS_TARGET choice 30=30 fps,60=60 fps DM_GAME_FPS_TARGET DM_GAME_FPS_TARGET!=0 Game Frame rate level=basic
DM_RENDER_SCALE range 0.5:1.0:0.05 1.0 fixups:RENDER_SCALE Game Render scale level=basic
DM_OPMODE choice 1=Docked,0=Handheld DM_GAME_OPMODE always Game Console mode level=basic
DM_PRESENT_UPSCALE choice off=Standard,metalfx=MetalFX off always Game Upscaling Uses more GPU time and can increase heat.
DM_LANG choice languages build:DM_LANG languages>1 Game Game language
DM_LANG choice languages build:DM_LANG languages>1 Game Game language level=basic
DM_SHOW_FPS toggle - build:DM_SHOW_FPS platform:ios Game Frame-rate counter
DM_TOUCH_HAPTICS toggle - 1 platform:ios Controls Touch feedback
DM_RUMBLE toggle - 1 always Controls Game rumble
DM_RUMBLE_GAIN range 0:3:0.1 1.0 always Controls Rumble strength
DM_RUMBLE toggle - 1 always Controls Game rumble level=basic
DM_RUMBLE_GAIN range 0:3:0.1 1.0 always Controls Rumble strength needs=DM_RUMBLE=1
DM_RUMBLE_MIN range 0:0.9:0.05 0 always Controls Rumble cutoff
DM_PRESENT_SNAP toggle - DM_GAME_PRESENT_SNAP gfxapi:nvn Performance Frame buffering Up to one frame more display latency.
DM_PSO_MTL4 toggle - DM_GAME_PSO_MTL4|1 gfxapi:nvn Performance Metal 4 pipelines Keeps a pipeline archive; uses more memory.
Expand Down
7 changes: 5 additions & 2 deletions dm/ios/Sources/Launcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ private struct LauncherView: View {
.frame(width: geometry.size.width / 3, height: geometry.size.height)
List {
ForEach(m.sections, id: \.self) { section in
Section(section) {
ForEach(m.settings.filter { $0.sectionTitle == section }) { SettingRow(s: $0, m: m) }
let rows = m.rows(in: section)
if !rows.isEmpty {
Section(section) {
ForEach(rows) { SettingRow(s: $0, m: m) }
}
}
}
Section {
Expand Down
13 changes: 12 additions & 1 deletion dm/ios/Sources/LauncherSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Combine
struct Setting: Decodable, Identifiable {
let env, kind, label, note, def: String
let section: String? // heading on the settings screen; absent in older bundles
let needs: String? // "<VAR>=<value>": a sub-setting, drawn only while that setting has that value
let options: [[String]]? // choice: [value, label]
let min, max, step: Double? // range
let on, off: String? // toggle: the values the variable takes (1 / 0 when not given)
Expand All @@ -25,7 +26,7 @@ struct Setting: Decodable, Identifiable {
default: return true
}
}
enum CodingKeys: String, CodingKey { case env, kind, label, note, def = "default", section, options, min, max, step, on, off }
enum CodingKeys: String, CodingKey { case env, kind, label, note, def = "default", section, needs, options, min, max, step, on, off }
}
struct SettingsFile: Decodable { let settings: [Setting] }

Expand All @@ -50,6 +51,16 @@ final class LauncherModel: ObservableObject {
var sections: [String] {
settings.reduce(into: []) { if !$0.contains($1.sectionTitle) { $0.append($1.sectionTitle) } }
}
// A row with "needs" is a sub-setting of the row it names: drawn only while that setting has that value, so a
// setting that cannot do anything is not offered (an upscaler's detail with the upscaler off, rumble strength
// with rumble off). A row naming a setting this game does not have stays drawn, as it has nothing to hide behind
func visible(_ s: Setting) -> Bool {
guard let n = s.needs, let eq = n.firstIndex(of: "=") else { return true }
let key = String(n[n.startIndex..<eq]), want = String(n[n.index(after: eq)...])
guard let v = values[key] else { return true }
return v == want
}
func rows(in section: String) -> [Setting] { settings.filter { $0.sectionTitle == section && visible($0) } }
var changed: Bool { settings.contains { !fromFile.contains($0.env) && !$0.isDefault(values[$0.env] ?? $0.def) } }

// Reset every launcher choice to the game's defaults; an env file's values stay.
Expand Down
7 changes: 7 additions & 0 deletions dm/ios/tests/launcher_storage_check.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ import Foundation
expect(d.string(forKey: "launcher.DM_PSO_MTL4") == nil && m.values["DM_PSO_MTL4"] == "1", "Reset clears performance choices and shows the defaults")
expect(d.string(forKey: "launcher.DM_FPS_TARGET") == nil && m.values["DM_FPS_TARGET"] == file.settings.first { $0.env == "DM_FPS_TARGET" }!.def, "Reset also clears game choices")
expect(m.values["DM_METAL_HUD"] == "1" && !m.changed, "Reset leaves the env file's value; nothing left to reset")
// Sub-settings: a row with "needs" is drawn only while the setting it names has that value
m.values["DM_RUMBLE"] = "1"
expect(m.rows(in: "Controls").contains { $0.env == "DM_RUMBLE_GAIN" }, "a sub-setting is drawn while its setting is on")
m.values["DM_RUMBLE"] = "0"
expect(!m.rows(in: "Controls").contains { $0.env == "DM_RUMBLE_GAIN" }, "a sub-setting is hidden while its setting is off")
expect(m.rows(in: "Controls").contains { $0.env == "DM_RUMBLE" }, "the setting it depends on stays drawn")
m.values["DM_RUMBLE"] = "1"
print("launcher storage: \(failed) failed")
exit(failed == 0 ? 0 : 1)
}
Expand Down
Loading
Loading