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
6 changes: 6 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -3227,3 +3227,9 @@ broad header-qualifier guard. Macro-expanded pointer expressions still need alia
proof. The frozen exported-ROM CLI is MIPS-only; other targets need their copied
ordinary build/objdiff evaluator. Neither diagnostic expansion nor recipe hashes
replace that exact judge.

- Done: Settings offers a project-directory picker and machine-local remembered directories.
Desktop users can switch between independent project catalogues at runtime through a graceful
save/stop/reopen handoff. Files stay in place; the successfully opened directory becomes the
next-launch default. Explicit DECOMP_HOME retains precedence, release/development histories
are separate, unavailable drives are not recreated, and switching rotates window credentials.
12 changes: 10 additions & 2 deletions src/decompnet/app/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ def run(args) -> int:
os.environ.update(DECOMP_SERVE='1', DECOMP_WINDOW='0', DECOMP_TRAY='0')
use_own_wine_prefix()
home = home_dir()
from .data_directories import preferred
if not os.environ.get('DECOMP_HOME') and preferred() and not home.is_dir():
message = f'The remembered project directory is unavailable: {home}. Connect its drive or choose a directory using DECOMP_HOME.'
print(message, file=sys.stderr);window.tell_desktop(edition.NAME, message)
return 1
internal = home / ".decomp"
internal.mkdir(parents=True, exist_ok=True)
log = file_log(internal / "logs" / "decomp.log") # from the first line on: whatever stops the app is in it
Expand Down Expand Up @@ -198,7 +203,7 @@ def run(args) -> int:
# launcher from starting a second server/workers while this home is opening.
from ..writer_gate import WriterGate
instance = WriterGate(internal / 'server-instance')
with instance.hold(blocking=bool(restarted)) as admitted:
with instance.hold(blocking=bool(restarted) and not restarted.get('switch_home',False)) as admitted:
if not admitted:
print('decompdecomp is already starting for this home. Wait for its database upgrade to finish.')
return 0
Expand Down Expand Up @@ -229,13 +234,16 @@ def _start(args,home,internal,state_file,log,restarted,headless):
print(str(error), file=sys.stderr, flush=True)
return 1
mcp, manager, security, quit_app = build(net, internal, port, secret, **({"headless":True} if headless else {}))
if restarted and restarted.get('switch_home'):
from .data_directories import remember
remember(home,selected=True)
app = mcp.streamable_http_app(transport_security=security, max_request_body_size=REQUEST_BODY_LIMIT, host="127.0.0.1")
from ..ui_diagnostics import Diagnostics
app = Diagnostics(app, stack_path=internal / "logs" / "event-loop-stacks.log")
# with a tray icon, helpers kept working without the window have a way back to it
window.IN_TRAY = False if headless else tray.start(window.open_app, quit_app)
print(f"{edition.NAME} {__import__('decompnet').__version__}\nyour projects: {hostenv.shown_path(home)}"
+ ("\nupdated and restarted" if restarted else "" if headless else "\nstop with Ctrl-C, or quit it in its window"), flush=True)
+ ("\nproject directory switched" if restarted and restarted.get("switch_home") else "\nupdated and restarted" if restarted else "" if headless else "\nstop with Ctrl-C, or quit it in its window"), flush=True)
# (1.0.2 and older restart into a new version without saying: they had a browser tab, which can't follow)
if headless:
print(f"Server: http://127.0.0.1:{port}/\nWeb sign-in code: {net.web_login.code}\nStop with Ctrl-C or `decomp stop`.", flush=True)
Expand Down
149 changes: 149 additions & 0 deletions src/decompnet/app/data_directories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
"""Machine-local project directory history and explicit, isolated runtime handoffs."""
import json
import os
import secrets
import sqlite3
import tempfile
import threading
from contextlib import closing, nullcontext
from pathlib import Path

from .. import toolchains
from ..network import NetworkError
from ..writer_gate import WriterGate
from . import edition, updates, window


def registry_path():
override = os.environ.get('DECOMP_DIRECTORIES_FILE')
return Path(override).expanduser() if override else toolchains.data_dir() / ('RetroReversing-directories-dev.json' if edition.DEV else 'RetroReversing-directories.json')


def read():
path = registry_path()
if not path.exists():return {'directories':[], 'selected':''}
try:
value = json.loads(path.read_text(encoding='utf-8'))
if not isinstance(value, dict) or not isinstance(value.get('directories'), list):raise ValueError()
if any(not isinstance(p,str) or not Path(p).is_absolute() for p in value['directories']):raise ValueError()
selected = value.get('selected','')
if not isinstance(selected,str) or (selected and (not Path(selected).is_absolute() or selected not in value['directories'])):raise ValueError()
return value
except (ValueError, TypeError):
raise NetworkError('The remembered project directories file is invalid: '+str(path)) from None


def preferred():
try:return read().get('selected') or None
except (NetworkError, OSError):return None # A damaged history must not prevent access to the default home.


def remember(path, *, selected=False, current=None):
file = registry_path()
file.parent.mkdir(parents=True, exist_ok=True)
with WriterGate(file).hold():
data = read()
paths = [str(Path(p).resolve()) for p in (current, path) if p]
data['directories'] = list(dict.fromkeys(paths+data['directories']))
if selected:data['selected'] = str(Path(path).resolve())
fd, temporary = tempfile.mkstemp(prefix=file.name+'.',dir=file.parent)
try:
with os.fdopen(fd,'w',encoding='utf-8') as stream:json.dump(data,stream)
os.replace(temporary,file)
finally:Path(temporary).unlink(missing_ok=True)


def validate(raw):
if not isinstance(raw,str) or not raw.strip() or any(c in raw for c in '\0\r\n'):
raise NetworkError('Choose a project directory.')
path = Path(raw.strip()).expanduser()
if not path.is_absolute():raise NetworkError('Choose a full directory path.')
path = path.resolve()
if not path.is_dir():raise NetworkError('This directory is not available. Connect its drive or choose an existing folder.')
if any(part.lower()=='.decomp' for part in path.parts):raise NetworkError('Choose the project directory containing .decomp, not its internal app-state folder.')
internal = path/'.decomp'
if internal.is_symlink():raise NetworkError('The .decomp app-state directory must not be a symbolic link.')
# Check access before stopping any work. Never create a missing mount or copy projects.
fd, probe = tempfile.mkstemp(prefix='.decomp-write-check-',dir=path)
os.close(fd);Path(probe).unlink()
database = internal/'decomp.db'
if database.is_symlink():raise NetworkError('The app database must not be a symbolic link.')
if database.exists():
try:
with closing(sqlite3.connect(database.as_uri()+'?mode=ro',uri=True)) as db:db.execute('PRAGMA schema_version').fetchone()
except sqlite3.DatabaseError:
raise NetworkError('This directory’s app database could not be read. Repair it before switching directories.') from None
return path


class Controller:
def __init__(self, app):
self.app = app
self.lock = threading.Lock()
self.job = {'state':'idle'}
self.previous_update = None

def status(self):
current = str(self.app.home.parent.resolve())
paths = list(dict.fromkeys([current,str(edition.default_home().resolve())]+read()['directories']))
return {'current':current, 'directories':[{'path':p,'available':Path(p).is_dir()} for p in paths], 'job':dict(self.job)}

def choose(self, raw, switch=False):
target = validate(raw)
current = self.app.home.parent.resolve()
with self.lock:
if self.job['state'] in ('queued','restarting'):
if self.job.get('target') == str(target):return self.status()
raise NetworkError('A directory switch is already in progress.')
if switch and target != current:
if self.app.headless:raise NetworkError('Switch project directories in the desktop app.')
runtime = getattr(self.app,'source_runtime',None)
if getattr(self.app.jobs,'source_update_pending',False) or self.app.updater.job.get('state') in ('installing','restarting','switching-directory') or (runtime and runtime.job.get('state') in ('preparing','waiting','deferred','updating','restarting')):
raise NetworkError('Wait for the app update or restart to finish before switching directories.')
remember(target,current=current)
if not switch:return {**self.status(),'chosen':str(target)}
if target == current:
remember(target,selected=True)
return self.status()
with runtime.lock if runtime else nullcontext(), self.app.updater._lock:
if runtime and runtime.job.get('state') in ('preparing','waiting','deferred','updating','restarting'):
raise NetworkError('Wait for the source runtime update to finish before switching directories.')
if self.app.updater.job.get('state') in ('installing','restarting','switching-directory'):
raise NetworkError('Wait for the current app update to finish before switching directories.')
self.previous_update = dict(self.app.updater.job)
self.app.updater.job = {'state':'switching-directory','log':['Switching project directory…'],'error':None}
self.app.jobs.source_update_pending = True # Block new jobs during the handoff.
self.job = {'state':'queued','target':str(target),'message':'Preparing to save work and switch project directory…'}
try:threading.Thread(target=self.run,args=(target,),daemon=True,name='data-directory-switch').start()
except (RuntimeError,OSError):
self.job = {'state':'idle'}
self.app.jobs.source_update_pending = False
with self.app.updater._lock:self.app.updater.job = self.previous_update
raise NetworkError('Could not start the directory switch. Current work continues; retry shortly.') from None
return self.status()

def run(self, target):
stopped = False
try:
# Let the initiating request return before closing its window.
threading.Event().wait(.3)
internal = target/'.decomp'
internal.mkdir(exist_ok=True)
with WriterGate(internal/'server-instance').hold(blocking=False) as admitted:
if not admitted:raise NetworkError('This directory is already open or starting in another app. Switch to that window first.')
self.job.update(state='restarting',message='Saving current work and stopping agents and room connections…')
was_open = window.is_open()
stopped = True
self.app.updater.before_restart()
state_file = self.app.home/'server.json'
if state_file.is_file():
state = json.loads(state_file.read_text())
if state.get('secret') == self.app.secret:state_file.unlink()
# New identity rejects stale window/browser requests aimed at the previous data.
window.close_app()
updates.restart(self.app.port,secrets.token_urlsafe(24),was_open,home=target)
except (OSError,ValueError,NetworkError) as error:
self.job.update(state='error',message=str(error)+(' Current work may have stopped; quit and reopen the app to resume this directory.' if stopped else ' Current work continues.'))
self.app.jobs.source_update_pending = False
if self.previous_update is not None:
with self.app.updater._lock:self.app.updater.job = self.previous_update
5 changes: 3 additions & 2 deletions src/decompnet/app/edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def default_home(*, dev: bool | None = None) -> Path:


def home_dir() -> Path:
"""Use an explicit DECOMP_HOME, otherwise this fork's own installed or development home."""
return Path(os.environ.get("DECOMP_HOME") or default_home()).expanduser()
"""Use DECOMP_HOME, a remembered directory, or this fork's edition-specific default."""
from .data_directories import preferred
return Path(os.environ.get("DECOMP_HOME") or preferred() or default_home()).expanduser()


def icon_svg() -> str:
Expand Down
24 changes: 24 additions & 0 deletions src/decompnet/app/routes/data_directories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Directory history is local to this machine; switching requires the desktop window."""
from starlette.responses import JSONResponse

from ...network import NetworkError
from .. import data_directories, window


def register(app):
controller = data_directories.Controller(app)

@app.mcp.custom_route('/api/data-directories', methods=['GET','POST'])
async def directories(request):
if bad := app.guard(request,write=request.method=='POST'):return bad
try:
if request.method=='GET':result = await app.call(controller.status)
else:
data = await app.body(request)
action = data.get('action')
if action not in ('remember','switch'):raise NetworkError('Choose Remember directory or Switch directory.')
if action=='switch' and window.token_of(request.headers.get('user-agent','')) != app.secret:
raise NetworkError('Switch project directories in the desktop app.')
result = await app.call(controller.choose,str(data.get('path') or ''),action=='switch')
return JSONResponse({**result,'switchable':not app.headless and window.token_of(request.headers.get('user-agent','')) == app.secret})
except (NetworkError,OSError,ValueError) as error:return app.fail(error)
1 change: 1 addition & 0 deletions src/decompnet/app/routes/life.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async def api_checkout(request: Request) -> Response:

from ..source_runtime import ACTIVE, Runtime
runtime = Runtime(app)
app.source_runtime = runtime

@mcp.custom_route("/api/update/runtime", methods=["GET", "POST"])
async def api_runtime(request: Request) -> Response:
Expand Down
16 changes: 14 additions & 2 deletions src/decompnet/app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@
from ..network import NetworkError
from . import agents, collab, maintenance, routes, saves, tools, updates, window, workers
from .engine import RomNetwork
from .routes import browser_bridge, forks, life, pages, projects, repair_feedback, repository_sync, rooms, setup, state
from .routes import (
browser_bridge,
data_directories,
forks,
life,
pages,
projects,
repair_feedback,
repository_sync,
rooms,
setup,
state,
)
from .shutdown_progress import RUNTIME_STOP_SECONDS, SHUTDOWN_SECONDS, terminal_note
from .shutdown_progress import Progress as ShutdownProgress
from .support import Jobs, Settings, session_options
Expand Down Expand Up @@ -336,7 +348,7 @@ def before_restart() -> None:
busy=busy, quit_app=quit_app, web_login=net.web_login, headless=headless)
from . import web_login
web_login.register(app)
for part in (pages, state, projects, repair_feedback, repository_sync, forks, setup, life, rooms, pairing, browser_bridge):
for part in (pages, state, projects, repair_feedback, repository_sync, forks, setup, life, rooms, pairing, browser_bridge, data_directories):
part.register(app)

security = TransportSecuritySettings(enable_dns_rebinding_protection=True, allowed_hosts=sorted(allowed_hosts),
Expand Down
2 changes: 1 addition & 1 deletion src/decompnet/app/source_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def start(self,path,data_mode):
with self.lock:
if self.job['state'] in ACTIVE:
raise ValueError('A source handoff is already queued.')
if self.app.updater.job['state'] in ('installing','restarting'):
if self.app.updater.job['state'] in ('installing','restarting','switching-directory'):
raise ValueError('Wait for the packaged update to finish first.')
self.cancel.clear()
self.app.settings.data['source_runtime_path']=str(folder)
Expand Down
1 change: 1 addition & 0 deletions src/decompnet/app/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ <h2 id="fnName" class="mono"></h2>
<script src="/assets/js/repository-push.js"></script>
<script src="/assets/js/steering.js"></script>
<script src="/assets/js/background-work.js"></script>
<script src="/assets/js/data-directories.js"></script>
<script src="/assets/js/pages.js"></script>
<script src="/assets/js/database-cleanup.js"></script>
<script src="/assets/js/header-storage.js"></script>
Expand Down
Loading