mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-19 12:25:56 +00:00
Add the installer window: a tkinter front end over the CLI with steps, progress, space, log, cancel, update mode and a self-closing selftest
The check step now emits a space event with the bytes the install needs and the free bytes. The window logic (event model, form, CLI subprocess, Ctrl+Break) is in installer/guilogic.py and tested without Tk. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a56a0e6fda
commit
826448e5e3
6 files changed
+1323
-1
No files matched your search
@@ -5,6 +5,8 @@ Every event has "type" and "t" (seconds since the start). Types:
|
||||
start release, mode (install, resume, update), dest, log, steps (the step ids in order)
|
||||
step step, state: started | done (seconds) | skipped (reason: current, not_needed)
|
||||
progress step, phase, and done/total/unit (bytes, chunks, objects, shaders) when the amount is known
|
||||
space dest, need, free (bytes on the install disk; with the temp folder on the same disk need includes it),
|
||||
temp, temp_need, temp_free, same_disk
|
||||
warning id, params
|
||||
error id, params, step (absent outside a step)
|
||||
end result: ok | failed | cancelled, seconds
|
||||
|
||||
@@ -0,0 +1,544 @@
|
||||
"""The installer window: python -m installer.gui
|
||||
|
||||
A front end over the CLI (docs/installer.md): it collects the game files, the keys file path, the game language and the
|
||||
install folder, runs `python -m installer ...` as a subprocess and shows its JSON events. Decisions live in guilogic.py;
|
||||
this file only draws. The window text is in installer/text/<en|ru>.json under "ui.*" and "launcher.*" ids.
|
||||
|
||||
--selftest [--shots DIR] build every screen in both languages and close by itself (PNG per screen with --shots)
|
||||
--lang en|ru window language (default: the Windows display language)
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tkinter as tk
|
||||
from pathlib import Path
|
||||
from tkinter import filedialog, messagebox, ttk
|
||||
|
||||
from . import ini
|
||||
from . import layout as layouts
|
||||
from .guilogic import (DONE, FAILED, PENDING, RUNNING, SKIPPED, UI_LANGUAGES, Catalog, Form, Model, Process,
|
||||
amount, default_game_language, describe_event, parse_line, windows_ui_language)
|
||||
|
||||
POLL_MS = 100
|
||||
GLYPH = {PENDING: '\u25cb', RUNNING: '\u25b6', DONE: '\u2714', SKIPPED: '\u2013', FAILED: '\u2716'}
|
||||
COLOR = {PENDING: '#808080', RUNNING: '#1a5fb4', DONE: '#26a269', SKIPPED: '#808080', FAILED: '#c01c28'}
|
||||
PAD = 14
|
||||
|
||||
|
||||
def fix_tcl_paths():
|
||||
"""The embeddable Python keeps Tcl/Tk next to python.exe (python/tcl); point Tcl at it when it is not found."""
|
||||
tcl = Path(sys.executable).resolve().parent / 'tcl'
|
||||
if tcl.is_dir() and 'TCL_LIBRARY' not in os.environ:
|
||||
for lib, var in (('tcl9.0', 'TCL_LIBRARY'), ('tk9.0', 'TK_LIBRARY')):
|
||||
if (tcl / lib).is_dir():
|
||||
os.environ[var] = str(tcl / lib)
|
||||
|
||||
|
||||
class App:
|
||||
def __init__(self, root, ui_lang, layout, run_args=None):
|
||||
self.root, self.layout = root, layout
|
||||
self.cat = Catalog(ui_lang)
|
||||
self.model = Model()
|
||||
self.process = None
|
||||
self.closing = False
|
||||
self.run_args = run_args or {} # extra CLI options for tests: temp, verify
|
||||
self.bound = [] # (widget, option, id, params) retranslated on a language switch
|
||||
self.vars = {n: tk.StringVar() for n in ('base', 'update', 'keys', 'dest')}
|
||||
self.vars['lang'] = tk.StringVar()
|
||||
self.lang_code = default_game_language(ui_lang)
|
||||
self.screen = None
|
||||
|
||||
root.title(self.cat('ui.title'))
|
||||
root.minsize(680, 560)
|
||||
root.protocol('WM_DELETE_WINDOW', self.on_close)
|
||||
style = ttk.Style(root)
|
||||
style.configure('Title.TLabel', font=('Segoe UI', 15, 'bold'))
|
||||
style.configure('Notice.TLabel', background='#fff3cd', foreground='#664d03', padding=8)
|
||||
style.configure('Err.TLabel', foreground='#c01c28')
|
||||
|
||||
top = ttk.Frame(root, padding=(PAD, PAD, PAD, 0))
|
||||
top.pack(fill='x')
|
||||
self.title = self.label(top, 'ui.title', style='Title.TLabel')
|
||||
self.title.pack(side='left')
|
||||
sw = ttk.Frame(top)
|
||||
sw.pack(side='right')
|
||||
self.ui_var = tk.StringVar(value=self.cat.lang)
|
||||
for code in UI_LANGUAGES:
|
||||
ttk.Radiobutton(sw, text=code.upper(), value=code, variable=self.ui_var, style='Toolbutton',
|
||||
command=self.switch_language).pack(side='left', padx=1)
|
||||
self.body = ttk.Frame(root, padding=PAD)
|
||||
self.body.pack(fill='both', expand=True)
|
||||
self.frames = {'form': self.build_form(), 'run': self.build_run(), 'finish': self.build_finish()}
|
||||
self.vars['lang'].set(self.lang_label(self.lang_code))
|
||||
self.vars['dest'].trace_add('write', lambda *_: self.refresh_form())
|
||||
self.show('form')
|
||||
|
||||
# ---- text -----------------------------------------------------------------------------------------------------
|
||||
|
||||
def bind_text(self, widget, option, id, **params):
|
||||
self.bound.append((widget, option, id, params))
|
||||
widget.configure(**{option: self.cat(id, **params)})
|
||||
|
||||
def label(self, parent, id, **kw):
|
||||
w = ttk.Label(parent, **kw)
|
||||
self.bind_text(w, 'text', id)
|
||||
return w
|
||||
|
||||
def button(self, parent, id, command, **kw):
|
||||
w = ttk.Button(parent, command=command, **kw)
|
||||
self.bind_text(w, 'text', id)
|
||||
return w
|
||||
|
||||
def retext(self):
|
||||
self.root.title(self.cat('ui.title'))
|
||||
for widget, option, id, params in self.bound:
|
||||
widget.configure(**{option: self.cat(id, **params)})
|
||||
self.fill_languages()
|
||||
self.refresh_form()
|
||||
self.refresh_run()
|
||||
self.refresh_finish()
|
||||
|
||||
def switch_language(self):
|
||||
self.cat.set_language(self.ui_var.get())
|
||||
code = self.selected_lang()
|
||||
self.retext()
|
||||
self.vars['lang'].set(self.lang_label(code))
|
||||
|
||||
def lang_label(self, code):
|
||||
return self.cat('lang.' + code)
|
||||
|
||||
def fill_languages(self):
|
||||
code = self.selected_lang()
|
||||
self.lang_box.configure(values=[self.lang_label(c) for c in ini.LANGUAGES])
|
||||
self.vars['lang'].set(self.lang_label(code))
|
||||
|
||||
def selected_lang(self):
|
||||
"""The game language code behind the combobox text (kept when it is not one of the labels)."""
|
||||
shown = self.vars['lang'].get()
|
||||
for code in ini.LANGUAGES:
|
||||
for lang in UI_LANGUAGES:
|
||||
if shown == Catalog(lang)('lang.' + code):
|
||||
return code
|
||||
return self.lang_code
|
||||
|
||||
# ---- screens --------------------------------------------------------------------------------------------------
|
||||
|
||||
def show(self, name):
|
||||
for f in self.frames.values():
|
||||
f.pack_forget()
|
||||
self.frames[name].pack(fill='both', expand=True)
|
||||
self.screen = name
|
||||
|
||||
def notice(self, parent):
|
||||
w = ttk.Label(parent, style='Notice.TLabel', wraplength=620, justify='left')
|
||||
self.bind_text(w, 'text', 'ui.notice')
|
||||
return w
|
||||
|
||||
def build_form(self):
|
||||
f = ttk.Frame(self.body)
|
||||
self.form_intro = self.label(f, 'ui.intro', wraplength=620, justify='left')
|
||||
self.form_intro.pack(anchor='w', pady=(0, 8))
|
||||
self.notice(f).pack(fill='x', pady=(0, 10))
|
||||
grid = ttk.Frame(f)
|
||||
grid.pack(fill='x')
|
||||
grid.columnconfigure(1, weight=1)
|
||||
rows = (('base', 'ui.base', 'ui.pick_game'), ('update', 'ui.update', 'ui.pick_update'),
|
||||
('keys', 'ui.keys', 'ui.pick_keys'))
|
||||
self.form_rows = {}
|
||||
for i, (name, label_id, pick_id) in enumerate(rows):
|
||||
lab = self.label(grid, label_id)
|
||||
lab.grid(row=i, column=0, sticky='w', pady=4, padx=(0, 10))
|
||||
ttk.Entry(grid, textvariable=self.vars[name]).grid(row=i, column=1, sticky='ew', pady=4)
|
||||
b = self.button(grid, 'ui.browse', lambda n=name, p=pick_id: self.pick_file(n, p))
|
||||
b.grid(row=i, column=2, padx=(8, 0))
|
||||
self.form_rows[name] = lab
|
||||
i = len(rows)
|
||||
self.label(grid, 'ui.game_language').grid(row=i, column=0, sticky='w', pady=4)
|
||||
self.lang_box = ttk.Combobox(grid, textvariable=self.vars['lang'], state='readonly', width=30)
|
||||
self.lang_box.grid(row=i, column=1, sticky='w', pady=4)
|
||||
self.lang_touched = False
|
||||
self.lang_box.bind('<<ComboboxSelected>>', lambda _e: setattr(self, 'lang_touched', True))
|
||||
self.fill_languages()
|
||||
self.label(grid, 'ui.dest').grid(row=i + 1, column=0, sticky='w', pady=4)
|
||||
ttk.Entry(grid, textvariable=self.vars['dest']).grid(row=i + 1, column=1, sticky='ew', pady=4)
|
||||
self.button(grid, 'ui.browse', self.pick_dest).grid(row=i + 1, column=2, padx=(8, 0))
|
||||
self.update_hint = ttk.Label(f, wraplength=620, justify='left')
|
||||
self.update_hint.pack(anchor='w', pady=(8, 0))
|
||||
self.form_error = ttk.Label(f, style='Err.TLabel', wraplength=620, justify='left')
|
||||
self.form_error.pack(anchor='w', pady=(8, 0))
|
||||
self.label(f, 'ui.keys_private', wraplength=620, justify='left').pack(anchor='w', pady=(8, 0))
|
||||
bar = ttk.Frame(f)
|
||||
bar.pack(side='bottom', fill='x', pady=(12, 0))
|
||||
self.start_button = self.button(bar, 'ui.start', self.start)
|
||||
self.start_button.pack(side='right')
|
||||
return f
|
||||
|
||||
def build_run(self):
|
||||
f = ttk.Frame(self.body)
|
||||
self.run_header = ttk.Label(f, font=('Segoe UI', 11, 'bold'))
|
||||
self.run_header.pack(anchor='w')
|
||||
self.step_rows = {}
|
||||
self.steps_box = ttk.Frame(f)
|
||||
self.steps_box.pack(fill='x', pady=(8, 8))
|
||||
self.phase_label = ttk.Label(f, wraplength=620, justify='left')
|
||||
self.phase_label.pack(anchor='w')
|
||||
self.bar = ttk.Progressbar(f, mode='determinate', maximum=1000)
|
||||
self.bar.pack(fill='x', pady=(4, 6))
|
||||
self.space_label = ttk.Label(f, wraplength=620, justify='left')
|
||||
self.space_label.pack(anchor='w')
|
||||
self.warn_label = ttk.Label(f, wraplength=620, justify='left', foreground='#9a6700')
|
||||
self.warn_label.pack(anchor='w')
|
||||
bar = ttk.Frame(f)
|
||||
bar.pack(side='bottom', fill='x', pady=(8, 0))
|
||||
self.cancel_button = self.button(bar, 'ui.cancel', self.cancel)
|
||||
self.cancel_button.pack(side='right')
|
||||
self.log_toggle = self.button(bar, 'ui.show_log', self.toggle_log)
|
||||
self.log_toggle.pack(side='left')
|
||||
self.log_frame = ttk.Frame(f)
|
||||
self.log = tk.Text(self.log_frame, height=10, wrap='word', state='disabled', font=('Consolas', 9))
|
||||
scroll = ttk.Scrollbar(self.log_frame, command=self.log.yview)
|
||||
self.log.configure(yscrollcommand=scroll.set)
|
||||
scroll.pack(side='right', fill='y')
|
||||
self.log.pack(side='left', fill='both', expand=True)
|
||||
self.log_open = False
|
||||
return f
|
||||
|
||||
def build_finish(self):
|
||||
f = ttk.Frame(self.body)
|
||||
self.finish_title = ttk.Label(f, style='Title.TLabel', wraplength=620, justify='left')
|
||||
self.finish_title.pack(anchor='w')
|
||||
self.finish_text = ttk.Label(f, wraplength=620, justify='left')
|
||||
self.finish_text.pack(anchor='w', pady=(8, 8))
|
||||
self.finish_steps = ttk.Frame(f)
|
||||
self.finish_steps.pack(fill='x', pady=(0, 8))
|
||||
self.finish_notice = self.notice(f)
|
||||
self.finish_notice.pack(fill='x', pady=(8, 0))
|
||||
bar = ttk.Frame(f)
|
||||
bar.pack(side='bottom', fill='x', pady=(12, 0))
|
||||
self.button(bar, 'ui.close', self.on_close).pack(side='right')
|
||||
self.open_button = self.button(bar, 'ui.open_folder', self.open_folder)
|
||||
self.open_button.pack(side='right', padx=(0, 8))
|
||||
self.back_button = self.button(bar, 'ui.back', lambda: self.show('form'))
|
||||
self.back_button.pack(side='left')
|
||||
return f
|
||||
|
||||
# ---- form -----------------------------------------------------------------------------------------------------
|
||||
|
||||
def form(self):
|
||||
return Form(base=self.vars['base'].get(), update=self.vars['update'].get(), keys=self.vars['keys'].get(),
|
||||
lang=self.selected_lang(), dest=self.vars['dest'].get())
|
||||
|
||||
def refresh_form(self):
|
||||
form = self.form()
|
||||
state = form.installed_state()
|
||||
if state is not None:
|
||||
self.update_hint.configure(text=self.cat('ui.update_mode'))
|
||||
if state.get('lang') in ini.LANGUAGES and not self.lang_touched:
|
||||
self.vars['lang'].set(self.lang_label(state['lang']))
|
||||
else:
|
||||
self.update_hint.configure(text='')
|
||||
|
||||
def pick_file(self, name, pick_id):
|
||||
types = [(self.cat('ui.type_game'), '*.nsp *.xci'), (self.cat('ui.type_all'), '*.*')]
|
||||
if name == 'update':
|
||||
types = [(self.cat('ui.type_nsp'), '*.nsp'), (self.cat('ui.type_all'), '*.*')]
|
||||
if name == 'keys':
|
||||
types = [(self.cat('ui.type_keys'), '*.keys'), (self.cat('ui.type_all'), '*.*')]
|
||||
path = filedialog.askopenfilename(title=self.cat(pick_id), filetypes=types)
|
||||
if path:
|
||||
self.vars[name].set(os.path.normpath(path))
|
||||
|
||||
def pick_dest(self):
|
||||
path = filedialog.askdirectory(title=self.cat('ui.pick_dest'), mustexist=False)
|
||||
if path:
|
||||
self.vars['dest'].set(os.path.normpath(path))
|
||||
|
||||
# ---- run ------------------------------------------------------------------------------------------------------
|
||||
|
||||
def start(self):
|
||||
form = self.form()
|
||||
problems = form.problems()
|
||||
if problems:
|
||||
self.form_error.configure(text=' '.join(self.cat(p) for p in problems))
|
||||
return
|
||||
self.form_error.configure(text='')
|
||||
command = form.command(self.layout.python, root=self.layout.root if self.layout.dev else None,
|
||||
temp=self.run_args.get('temp'), verify=self.run_args.get('verify', False))
|
||||
self.begin(command, self.layout.root)
|
||||
|
||||
def begin(self, command, cwd):
|
||||
self.model = Model()
|
||||
self.model.dest = self.vars['dest'].get().strip()
|
||||
self.clear_log()
|
||||
self.build_step_rows(self.model.steps)
|
||||
self.cancel_button.configure(state='normal')
|
||||
self.bound = [b for b in self.bound if b[0] is not self.cancel_button]
|
||||
self.bind_text(self.cancel_button, 'text', 'ui.cancel')
|
||||
self.show('run')
|
||||
self.refresh_run()
|
||||
self.process = Process(command, cwd=cwd, helper_python=self.layout.python)
|
||||
self.root.after(POLL_MS, self.poll)
|
||||
|
||||
def build_step_rows(self, steps):
|
||||
for w in self.steps_box.winfo_children():
|
||||
w.destroy()
|
||||
self.step_rows = {}
|
||||
for s in steps:
|
||||
row = ttk.Frame(self.steps_box)
|
||||
row.pack(fill='x', pady=1)
|
||||
glyph = ttk.Label(row, width=3)
|
||||
glyph.pack(side='left')
|
||||
name = ttk.Label(row)
|
||||
name.pack(side='left')
|
||||
self.step_rows[s] = (glyph, name)
|
||||
|
||||
def poll(self):
|
||||
if self.process is None:
|
||||
return
|
||||
try:
|
||||
while True:
|
||||
line = self.process.lines.get_nowait()
|
||||
if line is not None:
|
||||
self.feed(line)
|
||||
except Exception:
|
||||
pass
|
||||
code = self.process.poll()
|
||||
self.refresh_run()
|
||||
if code is None:
|
||||
self.root.after(POLL_MS, self.poll)
|
||||
return
|
||||
self.model.finish(code)
|
||||
self.process = None
|
||||
self.refresh_run()
|
||||
self.refresh_finish()
|
||||
self.show('finish')
|
||||
if self.closing:
|
||||
self.root.destroy()
|
||||
|
||||
def feed(self, line):
|
||||
"""One stdout line of the CLI: an event updates the model and the log, anything else goes to the log as is."""
|
||||
event = parse_line(line)
|
||||
if event is None:
|
||||
if line.strip():
|
||||
self.add_log(line.rstrip())
|
||||
return
|
||||
before = list(self.model.steps)
|
||||
self.model.apply(event)
|
||||
if self.model.steps != before:
|
||||
self.build_step_rows(self.model.steps)
|
||||
text = describe_event(self.cat, event)
|
||||
if text:
|
||||
self.add_log(text)
|
||||
|
||||
def refresh_run(self):
|
||||
m = self.model
|
||||
for s, (glyph, name) in self.step_rows.items():
|
||||
state = m.states.get(s, PENDING)
|
||||
glyph.configure(text=GLYPH[state], foreground=COLOR[state])
|
||||
name.configure(text=self.cat('step.' + s) + (f' ({self.cat("ui.state_" + state)})'
|
||||
if state in (SKIPPED, FAILED) else ''),
|
||||
font=('Segoe UI', 10, 'bold') if state == RUNNING else ('Segoe UI', 10))
|
||||
mode = m.mode or 'install'
|
||||
self.run_header.configure(text=self.cat('ui.running_' + mode if mode in ('install', 'resume', 'update')
|
||||
else 'ui.running_install'))
|
||||
if m.current and m.phase:
|
||||
text = self.cat(f'phase.{m.current}.{m.phase}')
|
||||
if m.total:
|
||||
text += ' ' + amount(self.cat, m.done, m.total, m.unit)
|
||||
elif m.current:
|
||||
text = self.cat('step.' + m.current)
|
||||
else:
|
||||
text = ''
|
||||
self.phase_label.configure(text=text)
|
||||
frac = m.fraction()
|
||||
if frac is None:
|
||||
if str(self.bar.cget('mode')) != 'indeterminate':
|
||||
self.bar.configure(mode='indeterminate')
|
||||
self.bar.start(15)
|
||||
else:
|
||||
if str(self.bar.cget('mode')) == 'indeterminate':
|
||||
self.bar.stop()
|
||||
self.bar.configure(mode='determinate')
|
||||
self.bar.configure(value=frac * 1000)
|
||||
self.space_label.configure(text=m.need_text(self.cat))
|
||||
self.warn_label.configure(text='\n'.join(self.cat(i, **p) for i, p in m.warnings))
|
||||
|
||||
def toggle_log(self):
|
||||
self.log_open = not self.log_open
|
||||
if self.log_open:
|
||||
self.log_frame.pack(fill='both', expand=True, pady=(8, 0))
|
||||
else:
|
||||
self.log_frame.pack_forget()
|
||||
self.bound = [b for b in self.bound if b[0] is not self.log_toggle]
|
||||
self.bind_text(self.log_toggle, 'text', 'ui.hide_log' if self.log_open else 'ui.show_log')
|
||||
|
||||
def clear_log(self):
|
||||
self.log.configure(state='normal')
|
||||
self.log.delete('1.0', 'end')
|
||||
self.log.configure(state='disabled')
|
||||
|
||||
def add_log(self, text):
|
||||
self.log.configure(state='normal')
|
||||
self.log.insert('end', text + '\n')
|
||||
self.log.see('end')
|
||||
self.log.configure(state='disabled')
|
||||
|
||||
def cancel(self):
|
||||
if self.process:
|
||||
self.cancel_button.configure(state='disabled')
|
||||
self.bound = [b for b in self.bound if b[0] is not self.cancel_button]
|
||||
self.bind_text(self.cancel_button, 'text', 'ui.cancelling')
|
||||
self.process.cancel()
|
||||
|
||||
def on_close(self):
|
||||
if self.process:
|
||||
if not messagebox.askyesno(self.cat('ui.title'), self.cat('ui.confirm_cancel')):
|
||||
return
|
||||
self.closing = True
|
||||
self.cancel()
|
||||
return
|
||||
self.root.destroy()
|
||||
|
||||
# ---- finish ---------------------------------------------------------------------------------------------------
|
||||
|
||||
def refresh_finish(self):
|
||||
m = self.model
|
||||
result = m.result or 'ok'
|
||||
self.finish_title.configure(text=self.cat('ui.result_' + result))
|
||||
if result == 'ok':
|
||||
text = self.cat('ui.done_text', dest=m.dest)
|
||||
elif result == 'cancelled':
|
||||
text = self.cat('ui.cancelled_text')
|
||||
else:
|
||||
err = m.error or ('ui.process_died', {'code': m.exit_code}, None)
|
||||
text = self.cat(err[0], **err[1])
|
||||
if m.log_path:
|
||||
text += '\n\n' + self.cat('ui.log_file', path=m.log_path)
|
||||
self.finish_text.configure(text=text)
|
||||
for w in self.finish_steps.winfo_children():
|
||||
w.destroy()
|
||||
for s in m.steps:
|
||||
state = m.states.get(s, PENDING)
|
||||
ttk.Label(self.finish_steps, text=f'{GLYPH[state]} {self.cat("step." + s)}',
|
||||
foreground=COLOR[state]).pack(anchor='w')
|
||||
if result == 'ok':
|
||||
self.open_button.state(['!disabled'])
|
||||
else:
|
||||
self.open_button.state(['disabled' if not Path(m.dest or '.').is_dir() else '!disabled'])
|
||||
|
||||
def open_folder(self):
|
||||
dest = self.model.dest
|
||||
if dest and Path(dest).is_dir():
|
||||
os.startfile(dest)
|
||||
|
||||
|
||||
# ---- self test ---------------------------------------------------------------------------------------------------
|
||||
|
||||
SAMPLE_START = {'type': 'start', 'release': 'selftest', 'mode': 'install', 'dest': 'Z:/selftest/TotK', 'log': 'D:\\log.txt',
|
||||
'steps': ['check', 'extract', 'lift', 'compile', 'prepare', 'cleanup']}
|
||||
SAMPLE_RUN = [
|
||||
SAMPLE_START,
|
||||
{'type': 'plan', 'run': ['check', 'extract'], 'skip': {}},
|
||||
{'type': 'step', 'step': 'check', 'state': 'started'},
|
||||
{'type': 'progress', 'step': 'check', 'phase': 'inputs'},
|
||||
{'type': 'space', 'dest': 'Z:/selftest/TotK', 'need': 36_400_000_000, 'free': 210_000_000_000, 'temp': 'C:\\t',
|
||||
'temp_need': 0, 'temp_free': 0, 'same_disk': True},
|
||||
{'type': 'warning', 'id': 'warn.lang_needs_fonts', 'params': {'lang': 'ko'}},
|
||||
{'type': 'step', 'step': 'check', 'state': 'done', 'seconds': 12.0},
|
||||
{'type': 'step', 'step': 'extract', 'state': 'started'},
|
||||
{'type': 'progress', 'step': 'extract', 'phase': 'copy', 'done': 6_000_000_000, 'total': 16_000_000_000,
|
||||
'unit': 'bytes'},
|
||||
]
|
||||
|
||||
|
||||
def grab(root, path):
|
||||
from PIL import ImageGrab
|
||||
root.attributes('-topmost', True)
|
||||
root.update()
|
||||
x, y, w, h = root.winfo_rootx(), root.winfo_rooty(), root.winfo_width(), root.winfo_height()
|
||||
ImageGrab.grab(bbox=(x, y, x + w, y + h), all_screens=True).save(path)
|
||||
|
||||
|
||||
def selftest(layout, shots=None):
|
||||
"""Every screen in both languages; the window closes itself. Returns 0 when nothing raised."""
|
||||
fix_tcl_paths()
|
||||
root = tk.Tk()
|
||||
root.geometry('720x640+60+40')
|
||||
app = App(root, 'en', layout)
|
||||
steps = []
|
||||
|
||||
def shot(name):
|
||||
root.update()
|
||||
if shots:
|
||||
Path(shots).mkdir(parents=True, exist_ok=True)
|
||||
grab(root, Path(shots) / f'{name}.png')
|
||||
|
||||
def run_all():
|
||||
for lang in UI_LANGUAGES:
|
||||
app.ui_var.set(lang)
|
||||
app.switch_language()
|
||||
tag = lang
|
||||
app.vars['dest'].set('Z:/selftest/TotK')
|
||||
app.vars['base'].set('Z:/selftest/totk.xci')
|
||||
app.vars['update'].set('Z:/selftest/totk-1.2.1.nsp')
|
||||
app.vars['keys'].set('Z:/selftest/prod.keys')
|
||||
app.show('form')
|
||||
app.start() # missing files: shows the problems
|
||||
steps.append(app.form_error.cget('text'))
|
||||
shot(f'form-{tag}')
|
||||
app.model = Model()
|
||||
app.build_step_rows(app.model.steps)
|
||||
app.show('run')
|
||||
for e in SAMPLE_RUN:
|
||||
app.feed(json.dumps(e))
|
||||
app.log_open = False
|
||||
app.toggle_log()
|
||||
app.refresh_run()
|
||||
shot(f'run-{tag}')
|
||||
app.model.finish(0)
|
||||
app.model.result = 'ok'
|
||||
app.refresh_finish()
|
||||
app.show('finish')
|
||||
shot(f'finish-ok-{tag}')
|
||||
app.model = Model()
|
||||
for e in SAMPLE_RUN[:3] + [{'type': 'error', 'id': 'check.disk_space', 'step': 'check',
|
||||
'params': {'path': 'Z:/selftest/TotK', 'need_gb': 36.4, 'free_gb': 20.1}},
|
||||
{'type': 'end', 'result': 'failed', 'seconds': 3}]:
|
||||
app.model.apply(e)
|
||||
app.model.finish(1)
|
||||
app.refresh_finish()
|
||||
shot(f'finish-failed-{tag}')
|
||||
app.model = Model()
|
||||
app.model.finish(3)
|
||||
app.refresh_finish()
|
||||
shot(f'finish-cancelled-{tag}')
|
||||
app.toggle_log()
|
||||
root.destroy()
|
||||
|
||||
root.after(200, run_all)
|
||||
root.after(60000, root.destroy) # never leaves a window open
|
||||
root.mainloop()
|
||||
return 0 if len(steps) == len(UI_LANGUAGES) and all(steps) else 1
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument('--selftest', action='store_true')
|
||||
ap.add_argument('--shots', type=Path)
|
||||
ap.add_argument('--lang', choices=UI_LANGUAGES)
|
||||
ap.add_argument('--root', type=Path)
|
||||
args = ap.parse_args(argv)
|
||||
layout = layouts.find(args.root)
|
||||
if args.selftest:
|
||||
return selftest(layout, args.shots)
|
||||
fix_tcl_paths()
|
||||
root = tk.Tk()
|
||||
root.geometry('720x640')
|
||||
App(root, args.lang or windows_ui_language(), layout)
|
||||
root.mainloop()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,344 @@
|
||||
"""The installer window without Tk: text catalogs, the form, the event model and the CLI subprocess.
|
||||
|
||||
gui.py draws; everything it decides lives here so tests/installer_gui_test.py runs it headless.
|
||||
|
||||
Catalog text of an id in the window language (installer/text/<lang>.json), English when that lacks it
|
||||
Form what the player entered; command() and problems() build and check the CLI call
|
||||
Model folds the CLI's JSON events (events.py) into step states, progress, space, errors and the result
|
||||
Process runs `python -m installer ...`, cancels it with Ctrl+Break and feeds lines to a queue
|
||||
"""
|
||||
import ctypes
|
||||
import json
|
||||
import os
|
||||
import queue
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from . import ini
|
||||
from . import state as statefile
|
||||
from .events import render
|
||||
|
||||
TEXT = Path(__file__).resolve().parent / 'text'
|
||||
UI_LANGUAGES = ('en', 'ru')
|
||||
STEP_ORDER = ('check', 'extract', 'lift', 'compile', 'prepare', 'cleanup')
|
||||
LANG_RU = 0x19 # primary language id of Russian in a Windows LANGID
|
||||
|
||||
|
||||
def load_texts(lang):
|
||||
path = TEXT / f'{lang}.json'
|
||||
return json.loads(path.read_text(encoding='utf-8')) if path.is_file() else {}
|
||||
|
||||
|
||||
class Catalog:
|
||||
"""Window text: ids from text/<lang>.json, then text/en.json, then the id itself."""
|
||||
|
||||
def __init__(self, lang='en'):
|
||||
self.english = load_texts('en')
|
||||
self.set_language(lang)
|
||||
|
||||
def set_language(self, lang):
|
||||
self.lang = lang if lang in UI_LANGUAGES else 'en'
|
||||
self.texts = load_texts(self.lang)
|
||||
|
||||
def has(self, id):
|
||||
return id in self.texts or id in self.english
|
||||
|
||||
def __call__(self, id, **params):
|
||||
text = self.texts.get(id) or self.english.get(id)
|
||||
if text is None:
|
||||
return id
|
||||
return render({id: text}, id, {k: flat(v) for k, v in params.items()})
|
||||
|
||||
|
||||
def flat(value):
|
||||
return ', '.join(str(v) for v in value) if isinstance(value, (list, tuple)) else value
|
||||
|
||||
|
||||
def windows_ui_language():
|
||||
"""'ru' when the Windows display language is Russian, else 'en'."""
|
||||
try:
|
||||
langid = ctypes.windll.kernel32.GetUserDefaultUILanguage()
|
||||
except (AttributeError, OSError):
|
||||
return 'en'
|
||||
return 'ru' if langid & 0x3ff == LANG_RU else 'en'
|
||||
|
||||
|
||||
def default_game_language(ui):
|
||||
return 'ru' if ui == 'ru' else 'en-US'
|
||||
|
||||
|
||||
def gib(n):
|
||||
"""Bytes as decimal gigabytes with one digit, the unit the check.disk_space message uses."""
|
||||
return f'{n / 1e9:.1f}'
|
||||
|
||||
|
||||
def amount(cat, done, total, unit):
|
||||
"""'6.0 / 16.0 GB', '120 / 4000 files'..., the progress of a phase whose size is known."""
|
||||
if unit == 'bytes':
|
||||
return cat('ui.amount_bytes', done=gib(done or 0), total=gib(total))
|
||||
id = f'ui.amount_{unit}'
|
||||
return cat(id if cat.has(id) else 'ui.amount_other', done=done or 0, total=total, unit=unit or '')
|
||||
|
||||
|
||||
# ---- the form ----------------------------------------------------------------------------------------------------
|
||||
|
||||
@dataclass
|
||||
class Form:
|
||||
base: str = ''
|
||||
update: str = ''
|
||||
keys: str = ''
|
||||
lang: str = ''
|
||||
dest: str = ''
|
||||
|
||||
def installed_state(self):
|
||||
"""The recorded state of the install folder (totk-install.json), or None. Never raises."""
|
||||
if not self.dest.strip():
|
||||
return None
|
||||
try:
|
||||
return statefile.load(Path(self.dest.strip()))
|
||||
except statefile.StateError:
|
||||
return None
|
||||
|
||||
def update_mode(self):
|
||||
return self.installed_state() is not None
|
||||
|
||||
def problems(self):
|
||||
"""Ids of the entries that are missing or wrong, in screen order. A folder with totk-install.json needs only
|
||||
itself; the CLI asks for the game files when it must extract again."""
|
||||
found = []
|
||||
update = self.update_mode()
|
||||
if not self.dest.strip():
|
||||
found.append('ui.need_dest')
|
||||
if not update:
|
||||
for name in ('base', 'update', 'keys'):
|
||||
value = getattr(self, name).strip()
|
||||
if not value:
|
||||
found.append(f'ui.need_{name}')
|
||||
elif not Path(value).is_file():
|
||||
found.append(f'ui.missing_{name}')
|
||||
if self.lang not in ini.LANGUAGES:
|
||||
found.append('ui.need_lang')
|
||||
else:
|
||||
for name in ('base', 'update', 'keys'):
|
||||
value = getattr(self, name).strip()
|
||||
if value and not Path(value).is_file():
|
||||
found.append(f'ui.missing_{name}')
|
||||
return found
|
||||
|
||||
def command(self, python, root=None, temp=None, verify=False):
|
||||
cmd = [str(python), '-m', 'installer']
|
||||
for flag, value in (('--base', self.base), ('--update', self.update), ('--keys', self.keys),
|
||||
('--lang', self.lang), ('--dest', self.dest)):
|
||||
if value.strip():
|
||||
cmd += [flag, value.strip()]
|
||||
if root:
|
||||
cmd += ['--root', str(root)]
|
||||
if temp:
|
||||
cmd += ['--temp', str(temp)]
|
||||
if verify:
|
||||
cmd.append('--verify')
|
||||
return cmd
|
||||
|
||||
|
||||
# ---- events -> state ---------------------------------------------------------------------------------------------
|
||||
|
||||
PENDING, RUNNING, DONE, SKIPPED, FAILED = 'pending', 'running', 'done', 'skipped', 'failed'
|
||||
|
||||
|
||||
def parse_line(line):
|
||||
"""The event dict of a stdout line, or None when it is not a JSON event (a traceback line, noise)."""
|
||||
line = line.strip()
|
||||
if not line.startswith('{'):
|
||||
return None
|
||||
try:
|
||||
event = json.loads(line)
|
||||
except ValueError:
|
||||
return None
|
||||
return event if isinstance(event, dict) and isinstance(event.get('type'), str) else None
|
||||
|
||||
|
||||
class Model:
|
||||
"""What the window shows, folded from events. apply() takes one event; nothing else mutates it."""
|
||||
|
||||
def __init__(self):
|
||||
self.steps = list(STEP_ORDER)
|
||||
self.states = {s: PENDING for s in self.steps}
|
||||
self.mode = ''
|
||||
self.release = ''
|
||||
self.log_path = ''
|
||||
self.dest = ''
|
||||
self.current = None # step id running
|
||||
self.phase = None
|
||||
self.done = self.total = None
|
||||
self.unit = None
|
||||
self.space = None # the space event
|
||||
self.warnings = [] # (id, params)
|
||||
self.error = None # (id, params, step)
|
||||
self.result = None # ok | failed | cancelled once the end event arrived
|
||||
self.seconds = None
|
||||
self.exit_code = None
|
||||
|
||||
def apply(self, e):
|
||||
kind = e['type']
|
||||
if kind == 'start':
|
||||
self.steps = list(e.get('steps') or STEP_ORDER)
|
||||
self.states = {s: PENDING for s in self.steps}
|
||||
self.mode, self.release = e.get('mode', ''), e.get('release', '')
|
||||
self.log_path, self.dest = e.get('log', ''), e.get('dest', '')
|
||||
elif kind == 'plan':
|
||||
for s, reason in (e.get('skip') or {}).items():
|
||||
if s in self.states and self.states[s] == PENDING:
|
||||
self.states[s] = SKIPPED
|
||||
elif kind == 'step':
|
||||
s, state = e.get('step'), e.get('state')
|
||||
if s not in self.states:
|
||||
self.states[s] = PENDING
|
||||
self.steps.append(s)
|
||||
if state == 'started':
|
||||
self.states[s], self.current = RUNNING, s
|
||||
self.phase = self.done = self.total = self.unit = None
|
||||
elif state == 'done':
|
||||
self.states[s] = DONE
|
||||
self.current = None if self.current == s else self.current
|
||||
elif state == 'skipped':
|
||||
self.states[s] = SKIPPED
|
||||
elif kind == 'progress':
|
||||
if e.get('step') in self.states and self.states[e['step']] == PENDING:
|
||||
self.states[e['step']] = RUNNING
|
||||
self.current = e.get('step', self.current)
|
||||
self.phase = e.get('phase')
|
||||
self.done, self.total, self.unit = e.get('done'), e.get('total'), e.get('unit')
|
||||
elif kind == 'space':
|
||||
self.space = e
|
||||
elif kind == 'warning':
|
||||
self.warnings.append((e.get('id', ''), e.get('params') or {}))
|
||||
elif kind == 'error':
|
||||
self.error = (e.get('id', ''), e.get('params') or {}, e.get('step'))
|
||||
if e.get('step') in self.states:
|
||||
self.states[e['step']] = FAILED
|
||||
elif kind == 'end':
|
||||
self.result = e.get('result')
|
||||
self.seconds = e.get('seconds')
|
||||
self.current = None
|
||||
|
||||
def fraction(self):
|
||||
"""0..1 of the current phase, or None when its size is unknown."""
|
||||
if self.total:
|
||||
return max(0.0, min(1.0, (self.done or 0) / self.total))
|
||||
return None
|
||||
|
||||
def finish(self, exit_code):
|
||||
"""The process ended. A missing end event (the process died) counts as failed, or cancelled on exit code 3."""
|
||||
self.exit_code = exit_code
|
||||
if self.result is None:
|
||||
self.result = 'cancelled' if exit_code == 3 else 'failed'
|
||||
if self.result == 'failed' and self.error is None:
|
||||
self.error = ('ui.process_died', {'code': exit_code}, self.current)
|
||||
for s, state in self.states.items():
|
||||
if state == RUNNING:
|
||||
self.states[s] = FAILED if self.result == 'failed' else PENDING
|
||||
|
||||
def need_text(self, cat):
|
||||
"""The space line, or '' before the check step reported."""
|
||||
sp = self.space
|
||||
if not sp:
|
||||
return ''
|
||||
key = 'ui.space_one' if sp.get('same_disk') or not sp.get('temp_need') else 'ui.space_two'
|
||||
return cat(key, path=sp.get('dest', ''), need=gib(sp.get('need', 0)), free=gib(sp.get('free', 0)),
|
||||
temp=sp.get('temp', ''), temp_need=gib(sp.get('temp_need', 0)),
|
||||
temp_free=gib(sp.get('temp_free', 0)))
|
||||
|
||||
|
||||
def describe_event(cat, e):
|
||||
"""One log line for an event, in the window language."""
|
||||
kind = e['type']
|
||||
if kind == 'step':
|
||||
text = f'{cat("step." + e["step"])}: {cat("ui.state_" + e["state"])}'
|
||||
if 'seconds' in e:
|
||||
text += f' ({e["seconds"]:.1f} s)'
|
||||
elif e.get('reason'):
|
||||
text += f' ({e["reason"]})'
|
||||
return text
|
||||
if kind == 'progress':
|
||||
text = cat(f'phase.{e["step"]}.{e["phase"]}')
|
||||
if e.get('total'):
|
||||
text += ' ' + amount(cat, e.get('done'), e['total'], e.get('unit'))
|
||||
return text
|
||||
if kind in ('warning', 'error'):
|
||||
return f'{cat("ui.log_" + kind)}: {cat(e["id"], **(e.get("params") or {}))}'
|
||||
if kind == 'space':
|
||||
return cat('ui.log_space', need=gib(e.get('need', 0)), free=gib(e.get('free', 0)))
|
||||
if kind == 'end':
|
||||
return f'{cat("ui.log_end")}: {cat("ui.result_" + str(e.get("result")))}'
|
||||
return ''
|
||||
|
||||
|
||||
# ---- the CLI subprocess ------------------------------------------------------------------------------------------
|
||||
|
||||
CREATE_NEW_PROCESS_GROUP = 0x200
|
||||
CREATE_NO_WINDOW = 0x08000000
|
||||
|
||||
_BREAK = """
|
||||
import ctypes, sys, time
|
||||
k = ctypes.windll.kernel32
|
||||
pid = int(sys.argv[1])
|
||||
k.FreeConsole()
|
||||
if k.AttachConsole(pid):
|
||||
k.SetConsoleCtrlHandler(None, True)
|
||||
k.GenerateConsoleCtrlEvent(1, pid)
|
||||
time.sleep(1)
|
||||
k.FreeConsole()
|
||||
else:
|
||||
sys.exit(1)
|
||||
"""
|
||||
|
||||
|
||||
def send_break(proc, python=None):
|
||||
"""Ctrl+Break to the process group of proc (started with CREATE_NEW_PROCESS_GROUP). The window itself may have no
|
||||
console (pythonw), so a short helper process attaches to the child's console and sends it."""
|
||||
helper = [str(python or sys.executable), '-c', _BREAK, str(proc.pid)]
|
||||
flags = CREATE_NO_WINDOW if os.name == 'nt' else 0
|
||||
r = subprocess.run(helper, creationflags=flags, capture_output=True)
|
||||
return r.returncode == 0
|
||||
|
||||
|
||||
class Process:
|
||||
"""`python -m installer ...` with its stdout lines going to self.lines (a queue).
|
||||
|
||||
poll() returns the exit code once the process ended and its output is drained, else None."""
|
||||
|
||||
def __init__(self, command, cwd=None, helper_python=None, creationflags=None):
|
||||
self.command = command
|
||||
self.helper_python = helper_python
|
||||
self.lines = queue.Queue()
|
||||
flags = (CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW) if creationflags is None else creationflags
|
||||
env = dict(os.environ, PYTHONIOENCODING='utf-8', PYTHONUTF8='1', PYTHONUNBUFFERED='1')
|
||||
self.proc = subprocess.Popen(command, cwd=cwd, env=env, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT, creationflags=flags)
|
||||
self.cancelled_at = None
|
||||
self.reader = threading.Thread(target=self.read, daemon=True)
|
||||
self.reader.start()
|
||||
|
||||
def read(self):
|
||||
for raw in iter(self.proc.stdout.readline, b''):
|
||||
self.lines.put(raw.decode('utf-8', errors='replace'))
|
||||
self.proc.stdout.close()
|
||||
|
||||
def cancel(self):
|
||||
if self.cancelled_at is None and self.proc.poll() is None:
|
||||
self.cancelled_at = time.monotonic()
|
||||
send_break(self.proc, self.helper_python)
|
||||
|
||||
def poll(self):
|
||||
code = self.proc.poll()
|
||||
if code is not None and not self.reader.is_alive() and self.lines.empty():
|
||||
return code
|
||||
return None
|
||||
|
||||
def kill(self):
|
||||
if self.proc.poll() is None:
|
||||
self.proc.kill()
|
||||
@@ -321,6 +321,8 @@ class Check(Step):
|
||||
if dest_dev == temp_dev:
|
||||
dest_need += temp_need
|
||||
temp_need = 0
|
||||
ctx.reporter.emit('space', dest=str(ctx.dest), need=dest_need, free=dest_free, temp=str(ctx.temp),
|
||||
temp_need=temp_need, temp_free=temp_free, same_disk=dest_dev == temp_dev)
|
||||
for path, need, free in ((ctx.dest, dest_need, dest_free), (ctx.temp, temp_need, temp_free)):
|
||||
if need > free:
|
||||
raise InstallError('check.disk_space', path=str(path), need_gb=round(need / 1e9, 1),
|
||||
|
||||
+87
-1
@@ -76,5 +76,91 @@
|
||||
"cleanup.ini_failed": "totk.ini cannot be written: {detail}",
|
||||
|
||||
"warn.lang_needs_fonts": "Text in {lang} needs the Switch system fonts, which the installer does not extract; some text may not show.",
|
||||
"warn.npdm_differs": "main.npdm differs from the reference 1.2.1 dump; only the code modules are used."
|
||||
"warn.npdm_differs": "main.npdm differs from the reference 1.2.1 dump; only the code modules are used.",
|
||||
|
||||
"ui.title": "TotK Setup",
|
||||
"ui.intro": "Builds a playable Tears of the Kingdom folder from your own game files. This takes a while and runs once.",
|
||||
"ui.notice": "The folder this setup builds contains the game. It is for your own use only; do not share or upload it.",
|
||||
"ui.keys_private": "The keys file is read in place. Its contents are never shown, copied or stored.",
|
||||
"ui.base": "Base game (NSP or XCI)",
|
||||
"ui.update": "Update 1.2.1 (NSP)",
|
||||
"ui.keys": "prod.keys",
|
||||
"ui.game_language": "Game language",
|
||||
"ui.dest": "Install folder",
|
||||
"ui.browse": "Browse...",
|
||||
"ui.pick_game": "Choose the base game file",
|
||||
"ui.pick_update": "Choose the 1.2.1 update file",
|
||||
"ui.pick_keys": "Choose prod.keys",
|
||||
"ui.pick_dest": "Choose the install folder",
|
||||
"ui.type_game": "Game files (NSP, XCI)",
|
||||
"ui.type_nsp": "NSP files",
|
||||
"ui.type_keys": "Key files (*.keys)",
|
||||
"ui.type_all": "All files",
|
||||
"ui.start": "Start",
|
||||
"ui.cancel": "Cancel",
|
||||
"ui.cancelling": "Cancelling...",
|
||||
"ui.show_log": "Show log",
|
||||
"ui.hide_log": "Hide log",
|
||||
"ui.close": "Close",
|
||||
"ui.back": "Back",
|
||||
"ui.open_folder": "Open folder",
|
||||
"ui.update_mode": "This folder holds an earlier installation. The setup continues or updates it; the game files are asked for only if they are needed again.",
|
||||
"ui.need_dest": "Choose the install folder.",
|
||||
"ui.need_base": "Choose the base game file.",
|
||||
"ui.need_update": "Choose the 1.2.1 update file.",
|
||||
"ui.need_keys": "Choose the prod.keys file.",
|
||||
"ui.need_lang": "Choose the game language.",
|
||||
"ui.missing_base": "The base game file does not exist.",
|
||||
"ui.missing_update": "The update file does not exist.",
|
||||
"ui.missing_keys": "The keys file does not exist.",
|
||||
"ui.running_install": "Installing",
|
||||
"ui.running_resume": "Continuing the earlier setup",
|
||||
"ui.running_update": "Updating the installation",
|
||||
"ui.state_started": "started",
|
||||
"ui.state_done": "done",
|
||||
"ui.state_skipped": "skipped",
|
||||
"ui.state_pending": "waiting",
|
||||
"ui.state_running": "running",
|
||||
"ui.state_failed": "failed",
|
||||
"ui.log_warning": "Warning",
|
||||
"ui.log_error": "Error",
|
||||
"ui.log_space": "Space needed: {need} GB, free: {free} GB",
|
||||
"ui.log_end": "Finished",
|
||||
"ui.result_ok": "The game is ready",
|
||||
"ui.result_failed": "The setup failed",
|
||||
"ui.result_cancelled": "The setup was cancelled",
|
||||
"ui.done_text": "Start totk.exe from {dest}. The setup program and the game files you chose are no longer needed.",
|
||||
"ui.cancelled_text": "The finished steps are kept. Start the setup again with the same folder to continue.",
|
||||
"ui.log_file": "Log file: {path}",
|
||||
"ui.process_died": "The setup stopped unexpectedly (exit code {code}).",
|
||||
"ui.confirm_cancel": "The setup is running. Cancel it and close the window?",
|
||||
"ui.space_one": "Free space needed on {path}: {need} GB (free: {free} GB).",
|
||||
"ui.space_two": "Free space needed on {path}: {need} GB (free: {free} GB); on {temp}: {temp_need} GB (free: {temp_free} GB).",
|
||||
"ui.amount_bytes": "{done} / {total} GB",
|
||||
"ui.amount_chunks": "{done} / {total} parts",
|
||||
"ui.amount_objects": "{done} / {total} files",
|
||||
"ui.amount_shaders": "{done} / {total} shaders",
|
||||
"ui.amount_other": "{done} / {total} {unit}",
|
||||
|
||||
"launcher.title": "TotK Setup",
|
||||
"launcher.python_missing": "The setup files are incomplete: the python folder next to TotK-Setup.exe is missing. Unpack the whole archive again and start TotK-Setup.exe from the unpacked folder.",
|
||||
|
||||
"lang.ja": "Japanese",
|
||||
"lang.en-US": "English (US)",
|
||||
"lang.en-GB": "English (UK)",
|
||||
"lang.fr": "French",
|
||||
"lang.fr-CA": "French (Canada)",
|
||||
"lang.de": "German",
|
||||
"lang.it": "Italian",
|
||||
"lang.es": "Spanish",
|
||||
"lang.es-419": "Spanish (Latin America)",
|
||||
"lang.nl": "Dutch",
|
||||
"lang.pt": "Portuguese",
|
||||
"lang.pt-BR": "Portuguese (Brazil)",
|
||||
"lang.ru": "Russian",
|
||||
"lang.ko": "Korean",
|
||||
"lang.zh-CN": "Chinese (Simplified, China)",
|
||||
"lang.zh-TW": "Chinese (Traditional, Taiwan)",
|
||||
"lang.zh-Hans": "Chinese (Simplified)",
|
||||
"lang.zh-Hant": "Chinese (Traditional)"
|
||||
}
|
||||
@@ -0,0 +1,344 @@
|
||||
"""The installer window: event model, form, catalogs, cancel, a self-closing Tk run and one end-to-end run.
|
||||
|
||||
.venv-py314\\Scripts\\python tests\\installer_gui_test.py
|
||||
|
||||
The end-to-end test runs the real CLI in --verify mode on a finished install (every step skips) and folds its events
|
||||
through the same code the window uses. The install is build/inst-gui/e2e-install: hard links to build/install-test, except
|
||||
romfs.bin, which links to build/extracted/v121 because the copy in install-test carries four flipped bits (the --verify run
|
||||
reports them, correctly) and the state files, which are copied. Nothing in install-test is written. It is skipped when
|
||||
either source is absent. The Tk test starts
|
||||
`python -m installer.gui --selftest`, which builds every screen in both languages and closes itself.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
from installer import state as statefile # noqa: E402
|
||||
from installer.guilogic import (DONE, FAILED, PENDING, RUNNING, SKIPPED, Catalog, Form, Model, Process, # noqa: E402
|
||||
amount, describe_event, parse_line, windows_ui_language)
|
||||
|
||||
INSTALLED = ROOT / 'build' / 'install-test'
|
||||
EXTRACTED_ROMFS = ROOT / 'build' / 'extracted' / 'v121' / 'romfs' / 'romfs.bin'
|
||||
FIXTURE = ROOT / 'build' / 'inst-gui' / 'e2e-install'
|
||||
COPIED = ('totk.ini', statefile.NAME) # written by a run, so never linked
|
||||
START = {'type': 'start', 'release': 'r1', 'mode': 'install', 'dest': 'D:\\T', 'log': 'D:\\T.log',
|
||||
'steps': ['check', 'extract', 'lift', 'compile', 'prepare', 'cleanup']}
|
||||
|
||||
|
||||
def fold(*events):
|
||||
m = Model()
|
||||
for e in events:
|
||||
m.apply(e)
|
||||
return m
|
||||
|
||||
|
||||
class ParseTest(unittest.TestCase):
|
||||
def test_event_line(self):
|
||||
self.assertEqual(parse_line('{"type": "step", "step": "check", "state": "started"}\n')['step'], 'check')
|
||||
|
||||
def test_noise_is_not_an_event(self):
|
||||
for line in ('', 'Traceback (most recent call last):', '{broken', '[1, 2]', '{"no_type": 1}', '{"type": 3}'):
|
||||
self.assertIsNone(parse_line(line), line)
|
||||
|
||||
def test_unicode_survives(self):
|
||||
e = parse_line(json.dumps({'type': 'warning', 'id': 'x', 'params': {'p': 'Привет'}}, ensure_ascii=False))
|
||||
self.assertEqual(e['params']['p'], 'Привет')
|
||||
|
||||
|
||||
class ModelTest(unittest.TestCase):
|
||||
def test_steps_follow_the_events(self):
|
||||
m = fold(START, {'type': 'plan', 'run': ['check', 'extract'], 'skip': {'lift': 'current', 'compile': 'current'}},
|
||||
{'type': 'step', 'step': 'check', 'state': 'started'})
|
||||
self.assertEqual([m.states[s] for s in m.steps],
|
||||
[RUNNING, PENDING, SKIPPED, SKIPPED, PENDING, PENDING])
|
||||
self.assertEqual(m.current, 'check')
|
||||
m.apply({'type': 'step', 'step': 'check', 'state': 'done', 'seconds': 1.0})
|
||||
self.assertEqual(m.states['check'], DONE)
|
||||
self.assertIsNone(m.current)
|
||||
m.apply({'type': 'step', 'step': 'extract', 'state': 'skipped', 'reason': 'current'})
|
||||
self.assertEqual(m.states['extract'], SKIPPED)
|
||||
|
||||
def test_progress_fraction(self):
|
||||
m = fold(START, {'type': 'step', 'step': 'extract', 'state': 'started'},
|
||||
{'type': 'progress', 'step': 'extract', 'phase': 'copy', 'done': 5, 'total': 20, 'unit': 'bytes'})
|
||||
self.assertEqual((m.current, m.phase, m.fraction()), ('extract', 'copy', 0.25))
|
||||
m.apply({'type': 'progress', 'step': 'extract', 'phase': 'merge'})
|
||||
self.assertIsNone(m.fraction()) # a phase without size is indeterminate
|
||||
m.apply({'type': 'progress', 'step': 'extract', 'phase': 'copy', 'done': 30, 'total': 20})
|
||||
self.assertEqual(m.fraction(), 1.0)
|
||||
|
||||
def test_a_new_step_resets_progress(self):
|
||||
m = fold(START, {'type': 'progress', 'step': 'check', 'phase': 'inputs', 'done': 1, 'total': 2},
|
||||
{'type': 'step', 'step': 'extract', 'state': 'started'})
|
||||
self.assertIsNone(m.phase)
|
||||
self.assertIsNone(m.fraction())
|
||||
|
||||
def test_error_marks_the_step(self):
|
||||
m = fold(START, {'type': 'step', 'step': 'check', 'state': 'started'},
|
||||
{'type': 'error', 'id': 'check.disk_space', 'step': 'check', 'params': {'path': 'D:\\T'}},
|
||||
{'type': 'end', 'result': 'failed', 'seconds': 2})
|
||||
self.assertEqual(m.states['check'], FAILED)
|
||||
self.assertEqual(m.error[0], 'check.disk_space')
|
||||
self.assertEqual(m.result, 'failed')
|
||||
|
||||
def test_space_text(self):
|
||||
m = fold({'type': 'space', 'dest': 'D:\\T', 'need': 36_400_000_000, 'free': 100_000_000_000, 'temp': 'C:\\t',
|
||||
'temp_need': 0, 'temp_free': 5, 'same_disk': True})
|
||||
text = m.need_text(Catalog('en'))
|
||||
self.assertIn('36.4', text)
|
||||
self.assertIn('100.0', text)
|
||||
self.assertEqual(Model().need_text(Catalog('en')), '')
|
||||
m = fold({'type': 'space', 'dest': 'D:\\T', 'need': 20_000_000_000, 'free': 1, 'temp': 'C:\\t',
|
||||
'temp_need': 2_000_000_000, 'temp_free': 3, 'same_disk': False})
|
||||
self.assertIn('C:\\t', m.need_text(Catalog('en')))
|
||||
|
||||
def test_warnings_collect(self):
|
||||
m = fold(START, {'type': 'warning', 'id': 'warn.lang_needs_fonts', 'params': {'lang': 'ko'}})
|
||||
self.assertEqual(m.warnings, [('warn.lang_needs_fonts', {'lang': 'ko'})])
|
||||
|
||||
def test_finish_without_end_event(self):
|
||||
m = fold(START, {'type': 'step', 'step': 'lift', 'state': 'started'})
|
||||
m.finish(-1073741510)
|
||||
self.assertEqual(m.result, 'failed')
|
||||
self.assertEqual(m.states['lift'], FAILED)
|
||||
self.assertEqual(m.error[0], 'ui.process_died')
|
||||
|
||||
def test_finish_after_cancel(self):
|
||||
m = fold(START, {'type': 'step', 'step': 'lift', 'state': 'started'}, {'type': 'end', 'result': 'cancelled'})
|
||||
m.finish(3)
|
||||
self.assertEqual(m.result, 'cancelled')
|
||||
self.assertIsNone(m.error)
|
||||
self.assertEqual(m.states['lift'], PENDING) # not finished; the next run resumes here
|
||||
|
||||
def test_exit_code_3_alone_is_cancelled(self):
|
||||
m = Model()
|
||||
m.finish(3)
|
||||
self.assertEqual(m.result, 'cancelled')
|
||||
|
||||
def test_unknown_event_is_ignored(self):
|
||||
m = fold(START, {'type': 'future_thing', 'x': 1})
|
||||
self.assertEqual(m.states['check'], PENDING)
|
||||
|
||||
|
||||
class FormTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.dir = Path(self.tmp.name)
|
||||
for name in ('base.nsp', 'upd.nsp', 'prod.keys'):
|
||||
(self.dir / name).write_bytes(b'x')
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def form(self, **kw):
|
||||
values = dict(base=str(self.dir / 'base.nsp'), update=str(self.dir / 'upd.nsp'), keys=str(self.dir / 'prod.keys'),
|
||||
lang='en-US', dest=str(self.dir / 'out'))
|
||||
values.update(kw)
|
||||
return Form(**values)
|
||||
|
||||
def test_complete_form(self):
|
||||
self.assertEqual(self.form().problems(), [])
|
||||
|
||||
def test_empty_form(self):
|
||||
self.assertEqual(Form().problems(), ['ui.need_dest', 'ui.need_base', 'ui.need_update', 'ui.need_keys',
|
||||
'ui.need_lang'])
|
||||
|
||||
def test_missing_files(self):
|
||||
self.assertEqual(self.form(base=str(self.dir / 'no.nsp'), keys=str(self.dir / 'no.keys')).problems(),
|
||||
['ui.missing_base', 'ui.missing_keys'])
|
||||
|
||||
def test_unknown_language(self):
|
||||
self.assertEqual(self.form(lang='xx').problems(), ['ui.need_lang'])
|
||||
|
||||
def test_command(self):
|
||||
cmd = self.form().command('py.exe', root=ROOT, temp=self.dir / 't', verify=True)
|
||||
self.assertEqual(cmd[:3], ['py.exe', '-m', 'installer'])
|
||||
for flag in ('--base', '--update', '--keys', '--lang', '--dest', '--root', '--temp', '--verify'):
|
||||
self.assertIn(flag, cmd)
|
||||
self.assertEqual(cmd[cmd.index('--lang') + 1], 'en-US')
|
||||
|
||||
def test_installed_folder_needs_only_itself(self):
|
||||
out = self.dir / 'out'
|
||||
statefile.save(self._mkdir(out), statefile.empty('r0') | {'lang': 'ru'})
|
||||
form = Form(dest=str(out))
|
||||
self.assertTrue(form.update_mode())
|
||||
self.assertEqual(form.problems(), [])
|
||||
cmd = form.command('py.exe')
|
||||
self.assertEqual(cmd, ['py.exe', '-m', 'installer', '--dest', str(out)])
|
||||
|
||||
def test_installed_folder_still_checks_given_files(self):
|
||||
out = self.dir / 'out'
|
||||
statefile.save(self._mkdir(out), statefile.empty('r0'))
|
||||
self.assertEqual(Form(dest=str(out), base=str(self.dir / 'no.nsp')).problems(), ['ui.missing_base'])
|
||||
|
||||
def test_broken_state_is_a_new_install(self):
|
||||
out = self._mkdir(self.dir / 'out')
|
||||
(out / statefile.NAME).write_text('{nope', encoding='utf-8')
|
||||
self.assertFalse(Form(dest=str(out)).update_mode())
|
||||
|
||||
@staticmethod
|
||||
def _mkdir(path):
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
class CatalogTest(unittest.TestCase):
|
||||
def test_russian_covers_english_with_the_same_placeholders(self):
|
||||
en, ru = Catalog('en').english, Catalog('ru').texts
|
||||
self.assertEqual(sorted(set(en) - set(ru)), [])
|
||||
self.assertEqual(sorted(set(ru) - set(en)), [])
|
||||
holes = [k for k in en if sorted(re.findall(r'\{\w+\}', en[k])) != sorted(re.findall(r'\{\w+\}', ru[k]))]
|
||||
self.assertEqual(holes, [])
|
||||
|
||||
def test_every_id_the_window_uses_exists(self):
|
||||
cat = Catalog('en')
|
||||
ids = ['ui.' + n for n in ('title', 'start', 'cancel', 'open_folder', 'notice', 'space_one', 'space_two',
|
||||
'process_died', 'result_ok', 'result_failed', 'result_cancelled')]
|
||||
ids += [f'step.{s}' for s in ('check', 'extract', 'lift', 'compile', 'prepare', 'cleanup')]
|
||||
ids += ['ui.state_' + n for n in ('started', 'done', 'skipped', 'pending', 'running', 'failed')]
|
||||
for lang in ('en', 'ru'):
|
||||
cat.set_language(lang)
|
||||
self.assertEqual([i for i in ids if not cat.has(i)], [])
|
||||
|
||||
def test_fallback_and_params(self):
|
||||
cat = Catalog('ru')
|
||||
self.assertIn('36.4', cat('ui.log_space', need='36.4', free='1.0'))
|
||||
self.assertEqual(cat('no.such.id'), 'no.such.id')
|
||||
self.assertIn('a, b', cat('check.release_incomplete', paths=['a', 'b']))
|
||||
cat.set_language('de') # unknown language falls back to English
|
||||
self.assertEqual(cat.lang, 'en')
|
||||
|
||||
def test_describe_and_amount(self):
|
||||
cat = Catalog('en')
|
||||
self.assertEqual(amount(cat, 6e9, 16e9, 'bytes'), '6.0 / 16.0 GB')
|
||||
self.assertEqual(amount(cat, 1, 9, 'shaders'), '1 / 9 shaders')
|
||||
self.assertEqual(amount(cat, 1, 9, 'widgets'), '1 / 9 widgets')
|
||||
line = describe_event(cat, {'type': 'step', 'step': 'check', 'state': 'done', 'seconds': 1.5})
|
||||
self.assertIn('1.5 s', line)
|
||||
self.assertEqual(describe_event(cat, {'type': 'start'}), '')
|
||||
|
||||
def test_windows_language_is_a_known_value(self):
|
||||
self.assertIn(windows_ui_language(), ('en', 'ru'))
|
||||
|
||||
|
||||
BREAK_CHILD = 'import time\nprint("ready", flush=True)\ntime.sleep(60)\n'
|
||||
|
||||
|
||||
class CancelTest(unittest.TestCase):
|
||||
def test_ctrl_break_ends_the_process_group(self):
|
||||
p = Process([sys.executable, '-c', BREAK_CHILD])
|
||||
try:
|
||||
self.assertEqual(p.lines.get(timeout=20).strip(), 'ready')
|
||||
t0 = time.monotonic()
|
||||
p.cancel()
|
||||
code = None
|
||||
while code is None and time.monotonic() - t0 < 20:
|
||||
code = p.poll()
|
||||
time.sleep(0.05)
|
||||
self.assertIsNotNone(code, 'the child ignored Ctrl+Break')
|
||||
self.assertNotEqual(code, 0)
|
||||
finally:
|
||||
p.kill()
|
||||
|
||||
|
||||
class SmokeTest(unittest.TestCase):
|
||||
def test_every_screen_in_both_languages(self):
|
||||
try:
|
||||
import tkinter # noqa: F401
|
||||
except ImportError:
|
||||
self.skipTest('no tkinter')
|
||||
r = subprocess.run([sys.executable, '-m', 'installer.gui', '--selftest'], cwd=ROOT, timeout=120,
|
||||
capture_output=True, text=True)
|
||||
self.assertEqual(r.returncode, 0, r.stderr[-2000:])
|
||||
|
||||
|
||||
def make_fixture():
|
||||
"""build/inst-gui/e2e-install: install-test with a good romfs.bin (see the module docstring); rebuilt when absent."""
|
||||
if (FIXTURE / statefile.NAME).is_file():
|
||||
return FIXTURE
|
||||
shutil.rmtree(FIXTURE, ignore_errors=True)
|
||||
for folder, _dirs, files in os.walk(INSTALLED):
|
||||
target = FIXTURE / Path(folder).relative_to(INSTALLED)
|
||||
target.mkdir(parents=True, exist_ok=True)
|
||||
for name in files:
|
||||
src, dst = Path(folder) / name, target / name
|
||||
if name in COPIED and target == FIXTURE:
|
||||
shutil.copy2(src, dst)
|
||||
elif name == 'romfs.bin' and target.name == 'romfs':
|
||||
os.link(EXTRACTED_ROMFS, dst)
|
||||
else:
|
||||
os.link(src, dst)
|
||||
state = statefile.load(FIXTURE)
|
||||
state['steps']['extract']['romfs_mtime_ns'] = (FIXTURE / 'romfs' / 'romfs.bin').stat().st_mtime_ns
|
||||
statefile.save(FIXTURE, state)
|
||||
return FIXTURE
|
||||
|
||||
|
||||
@unittest.skipUnless((INSTALLED / statefile.NAME).is_file() and EXTRACTED_ROMFS.is_file(), 'the install sources are absent')
|
||||
class EndToEndTest(unittest.TestCase):
|
||||
def test_verify_run_skips_every_step(self):
|
||||
form = Form(dest=str(make_fixture()))
|
||||
self.assertTrue(form.update_mode())
|
||||
temp = ROOT / 'build' / 'inst-gui' / 'e2e-temp'
|
||||
cmd = form.command(sys.executable, root=ROOT, temp=temp, verify=True)
|
||||
p = Process(cmd, cwd=ROOT)
|
||||
model, stray = Model(), []
|
||||
t0, code = time.monotonic(), None
|
||||
while code is None and time.monotonic() - t0 < 600:
|
||||
try:
|
||||
line = p.lines.get(timeout=0.2)
|
||||
except Exception:
|
||||
line = ''
|
||||
if line:
|
||||
event = parse_line(line)
|
||||
if event:
|
||||
model.apply(event)
|
||||
elif line.strip():
|
||||
stray.append(line)
|
||||
code = p.poll()
|
||||
p.kill()
|
||||
model.finish(code)
|
||||
elapsed = time.monotonic() - t0
|
||||
print(f'\nend-to-end: exit {code}, result {model.result}, {elapsed:.1f} s, states {model.states}')
|
||||
self.assertEqual(code, 0, stray[-5:] or model.error)
|
||||
self.assertEqual(model.result, 'ok')
|
||||
self.assertEqual(model.mode in ('resume', 'update'), True)
|
||||
self.assertEqual({s: model.states[s] for s in model.steps if s not in ('check', 'cleanup')},
|
||||
{s: SKIPPED for s in model.steps if s not in ('check', 'cleanup')})
|
||||
self.assertIsNotNone(model.space)
|
||||
self.assertEqual(stray, [])
|
||||
|
||||
def test_cancel_during_the_check(self):
|
||||
cmd = Form(dest=str(make_fixture())).command(sys.executable, root=ROOT, temp=ROOT / 'build' / 'inst-gui' / 'e2e-temp',
|
||||
verify=True)
|
||||
p = Process(cmd, cwd=ROOT)
|
||||
model, t0, code, cancelled = Model(), time.monotonic(), None, False
|
||||
while code is None and time.monotonic() - t0 < 120:
|
||||
try:
|
||||
event = parse_line(p.lines.get(timeout=0.2))
|
||||
except Exception:
|
||||
event = None
|
||||
if event:
|
||||
model.apply(event)
|
||||
if event.get('phase') == 'verify_romfs' and not cancelled:
|
||||
cancelled = True
|
||||
p.cancel()
|
||||
code = p.poll()
|
||||
p.kill()
|
||||
model.finish(code)
|
||||
self.assertTrue(cancelled)
|
||||
self.assertEqual((code, model.result), (3, 'cancelled'))
|
||||
self.assertIsNone(model.error)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(verbosity=2)
|
||||
Reference in new issue
Block a user