Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
"""The user's own game settings, one profile per game, in one JSON file.
Lives at ``<DATA_DIR>/games/profiles.json``::
{"version": 1, "dpi": 800,
"games": {"fortnite": {"values": {"mouse_sens_x": 6.4},
"keybinds": {"jump": "Space"},
"alt_keybinds": {"jump": "Mouse Wheel Down"},
"bind_modes": {"sprint": "toggle"},
"custom_binds": [{"id": "c1", "action": "Quick build",
"key": "Shift + Mouse 4", "alt": ""}],
"dpi": null, "notes": "", "updated_at": "..."}}}
A key is one key or a combination joined with " + " ("Ctrl + Shift + F").
``dpi`` at the top is the mouse DPI every game uses unless its profile sets
its own. Values are checked against the catalog before they are written, and
the file is replaced atomically so a crash never leaves half a file.
"""
from __future__ import annotations
import json
import logging
import math
import os
import threading
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from dodaios.games.catalog import get_game, keybind_ids, settings_by_key
log = logging.getLogger(__name__)
DEFAULT_DPI = 800
MIN_DPI, MAX_DPI = 50, 64000
MAX_KEY_LEN = 60
MAX_ACTION_LEN = 60
MAX_CUSTOM_BINDS = 60
BIND_MODES = ("hold", "toggle")
MAX_TEXT_LEN = 200
MAX_NOTES_LEN = 4000
_LOCK = threading.Lock()
class UnknownGameError(LookupError):
"""The game id is not in the catalog."""
def store_path(data_dir: Path | None = None) -> Path:
if data_dir is None:
from dodaios.core import config
data_dir = config.DATA_DIR
return Path(data_dir) / "games" / "profiles.json"
def _empty() -> dict[str, Any]:
return {"version": 1, "dpi": DEFAULT_DPI, "games": {}}
def _read(path: Path) -> dict[str, Any]:
try:
raw = json.loads(path.read_text(encoding="utf-8"))
except FileNotFoundError: # no profile saved yet: a fresh install starts empty
return _empty()
except (OSError, ValueError) as exc:
log.warning("game profiles unreadable, starting empty: %s", exc)
return _empty()
if not isinstance(raw, dict) or not isinstance(raw.get("games"), dict):
return _empty()
raw.setdefault("version", 1)
if not isinstance(raw.get("dpi"), int):
raw["dpi"] = DEFAULT_DPI
return raw
def _write(path: Path, data: dict[str, Any]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(".json.tmp")
tmp.write_text(json.dumps(data, indent=1, ensure_ascii=False), encoding="utf-8")
os.replace(tmp, path)
def check_dpi(value: Any) -> int:
if isinstance(value, bool) or not isinstance(value, (int, float)):
raise ValueError("DPI must be a number")
dpi = int(round(value))
if not MIN_DPI <= dpi <= MAX_DPI:
raise ValueError(f"DPI must be between {MIN_DPI} and {MAX_DPI}")
return dpi
def check_value(setting: dict[str, Any], value: Any) -> Any:
"""``value`` normalized for ``setting``, or ``ValueError`` when it does not fit."""
if value is None:
return None
kind = setting["type"]
label = setting["label"]
if kind == "toggle":
if not isinstance(value, bool):
raise ValueError(f"{label}: expected on or off")
return value
if kind == "select":
if value not in setting.get("options", []):
raise ValueError(f"{label}: not one of the game's options")
return value
if kind == "text":
if not isinstance(value, str) or len(value) > MAX_TEXT_LEN:
raise ValueError(f"{label}: text up to {MAX_TEXT_LEN} characters")
return value.strip()
# slider / number
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value):
raise ValueError(f"{label}: expected a number")
low, high = setting.get("min"), setting.get("max")
if low is not None and value < low:
raise ValueError(f"{label}: at least {low}")
if high is not None and value > high:
raise ValueError(f"{label}: at most {high}")
return value
def check_key(value: Any) -> str:
if not isinstance(value, str) or len(value) > MAX_KEY_LEN:
raise ValueError(f"A key name is text up to {MAX_KEY_LEN} characters")
return value.strip()
def _game_or_raise(game_id: str) -> dict[str, Any]:
game = get_game(game_id)
if game is None:
raise UnknownGameError(game_id)
return game
def _blank_profile() -> dict[str, Any]:
return {
"values": {},
"keybinds": {},
"alt_keybinds": {},
"bind_modes": {},
"custom_binds": [],
"dpi": None,
"notes": "",
"updated_at": None,
}
def check_custom_binds(value: Any) -> list[dict[str, str]]:
"""The player's own actions: a list of ``{id, action, key, alt}``."""
if not isinstance(value, list) or len(value) > MAX_CUSTOM_BINDS:
raise ValueError(f"Custom actions are a list of at most {MAX_CUSTOM_BINDS}")
out: list[dict[str, str]] = []
seen: set[str] = set()
for row in value:
if not isinstance(row, dict):
raise ValueError("A custom action needs an id, a name and a key")
row_id = row.get("id")
action = row.get("action")
if not isinstance(row_id, str) or not row_id or len(row_id) > 40 or row_id in seen:
raise ValueError("Each custom action needs its own id")
if not isinstance(action, str) or len(action.strip()) > MAX_ACTION_LEN:
raise ValueError(f"A custom action name is text up to {MAX_ACTION_LEN} characters")
seen.add(row_id)
out.append(
{
"id": row_id,
"action": action.strip(),
"key": check_key(row.get("key", "")),
"alt": check_key(row.get("alt", "")),
}
)
return out
class GameProfileStore:
"""Read and change the profiles file. Every method is safe across threads."""
def __init__(self, path: Path | None = None) -> None:
self.path = path or store_path()
def snapshot(self) -> dict[str, Any]:
with _LOCK:
return _read(self.path)
def profile(self, game_id: str) -> dict[str, Any]:
_game_or_raise(game_id)
data = self.snapshot()
return {**_blank_profile(), **data["games"].get(game_id, {})}
def set_default_dpi(self, dpi: Any) -> int:
value = check_dpi(dpi)
with _LOCK:
data = _read(self.path)
data["dpi"] = value
_write(self.path, data)
return value
def update(
self,
game_id: str,
*,
values: dict[str, Any] | None = None,
keybinds: dict[str, Any] | None = None,
alt_keybinds: dict[str, Any] | None = None,
bind_modes: dict[str, Any] | None = None,
custom_binds: list[Any] | None = None,
dpi: Any = ...,
notes: str | None = None,
) -> dict[str, Any]:
"""Merge a change into one game's profile and return the whole profile.
A value or keybind of ``None`` removes it (back to the game default).
``dpi=None`` makes the game follow the default DPI again; leaving it
out keeps what is saved. Everything is checked before anything is
written, so one bad field leaves the file untouched.
"""
game = _game_or_raise(game_id)
settings = settings_by_key(game)
binds = keybind_ids(game)
clean_values: dict[str, Any] = {}
for key, value in (values or {}).items():
if key not in settings:
raise ValueError(f"Unknown setting: {key}")
clean_values[key] = check_value(settings[key], value)
def clean_map(raw: dict[str, Any] | None) -> dict[str, str | None]:
out: dict[str, str | None] = {}
for key, value in (raw or {}).items():
if key not in binds:
raise ValueError(f"Unknown keybind: {key}")
out[key] = None if value is None else check_key(value)
return out
clean_binds = clean_map(keybinds)
clean_alts = clean_map(alt_keybinds)
clean_modes: dict[str, str | None] = {}
for key, value in (bind_modes or {}).items():
if key not in binds:
raise ValueError(f"Unknown keybind: {key}")
if value is not None and value not in BIND_MODES:
raise ValueError("A keybind mode is hold or toggle")
clean_modes[key] = value
clean_custom = None if custom_binds is None else check_custom_binds(custom_binds)
clean_dpi = None if dpi is None or dpi is ... else check_dpi(dpi)
if notes is not None and (not isinstance(notes, str) or len(notes) > MAX_NOTES_LEN):
raise ValueError(f"Notes are text up to {MAX_NOTES_LEN} characters")
with _LOCK:
data = _read(self.path)
profile = {**_blank_profile(), **data["games"].get(game_id, {})}
for key, value in clean_values.items():
if value is None:
profile["values"].pop(key, None)
else:
profile["values"][key] = value
for field, changes in (
("keybinds", clean_binds),
("alt_keybinds", clean_alts),
("bind_modes", clean_modes),
):
target = profile.setdefault(field, {})
for key, value in changes.items():
if value is None:
target.pop(key, None)
else:
target[key] = value
if clean_custom is not None:
profile["custom_binds"] = clean_custom
if dpi is not ...:
profile["dpi"] = clean_dpi
if notes is not None:
profile["notes"] = notes
profile["updated_at"] = datetime.now(UTC).isoformat(timespec="seconds")
data["games"][game_id] = profile
_write(self.path, data)
return profile
def reset(self, game_id: str) -> bool:
"""Forget one game's profile. Returns whether there was one."""
_game_or_raise(game_id)
with _LOCK:
data = _read(self.path)
existed = data["games"].pop(game_id, None) is not None
if existed:
_write(self.path, data)
return existed