feat: manuellen FTP-Sync zwischen Desktop und Tablet
- FTP/FTPS-Client (USER/PASS, AUTH TLS + PROT P, EPSV/PASV, MLSD, SIZE, RETR, STOR, DELE, MKD, RNFR/RNTO) ohne externe Bibliothek - Sync-Vergleich pro Kürdatei mit lokalem Sync-Stand: hochladen, herunterladen, überspringen; beidseitige Änderungen bleiben als Kopie mit Server-Zeitstempel erhalten, die lokale Fassung wird nie verworfen - Sync-Einstellungen (Host, Port, Benutzer, Passwort, Verzeichnis, FTPS, Zertifikatsprüfung) unter user://sync.cfg, nicht im Repository - Sync läuft im Thread, Fortschritt in der Statusleiste, Zusammenfassung im Dialog; geöffnete Kür wird nur unverändert neu geladen - Atomare Ersetzung über .part/.bak, Uploads über .uploading + Umbenennen - Tests für Vergleichsregeln, Konfliktnamen, PASV/EPSV/MDTM, Sync-Stand und Einstellungs-Dialog; zusätzlich E2E gegen den echten FTP-Server - docs/sync.md mit Einrichtung, Verhalten und Grenzen
This commit is contained in:
parent
921cf623fd
commit
f553785710
15 changed files with 1554 additions and 7 deletions
|
|
@ -239,7 +239,12 @@ ggf. eines nach `touchend` (300-ms-Guard).
|
||||||
Details in `docs/builds.md`.
|
Details in `docs/builds.md`.
|
||||||
- **Lokale Daten:** `user://` liegt pro Plattform getrennt (Linux
|
- **Lokale Daten:** `user://` liegt pro Plattform getrennt (Linux
|
||||||
`~/.local/share/godot/app_userdata/Rinkplanner/`, Android im
|
`~/.local/share/godot/app_userdata/Rinkplanner/`, Android im
|
||||||
app-internen Speicher). Ein Kontosync ist noch nicht implementiert.
|
app-internen Speicher).
|
||||||
|
- **Kontosync:** manueller FTP-/FTPS-Abgleich der `.tres`-Dateien über
|
||||||
|
`FtpClient`, `SyncPlan` und `RoutineSync`; Details in `docs/sync.md`.
|
||||||
|
Zugangsdaten liegen in `user://sync.cfg`, der letzte gemeinsame Stand in
|
||||||
|
`user://sync_state.cfg`. Konflikte werden als Kopie mit Server-Zeitstempel
|
||||||
|
behalten.
|
||||||
|
|
||||||
## 7. Tests
|
## 7. Tests
|
||||||
|
|
||||||
|
|
@ -278,6 +283,10 @@ Aktueller Stand:
|
||||||
Leistenlayout bei 1024 und 1400 px.
|
Leistenlayout bei 1024 und 1400 px.
|
||||||
- **Performance:** Stresstest mit 480 Segmenten in 12 Sections; die mittlere
|
- **Performance:** Stresstest mit 480 Segmenten in 12 Sections; die mittlere
|
||||||
Redraw-Zeit muss unter 120 ms bleiben (aktuell ca. 7–10 ms).
|
Redraw-Zeit muss unter 120 ms bleiben (aktuell ca. 7–10 ms).
|
||||||
|
- **Sync:** 31 Assertions für Vergleichsregeln, Konfliktnamen, Zeitstempel,
|
||||||
|
PASV/EPSV/MDTM-Parsing, Sync-Stand und die Einstellungs-Verdrahtung.
|
||||||
|
Der FTP-Client wurde zusätzlich gegen den echten Zielserver geprüft
|
||||||
|
(Upload, Download, Konflikt, Aufräumen).
|
||||||
|
|
||||||
Autosave/Restore wird zusätzlich in einem isolierten `XDG_DATA_HOME`
|
Autosave/Restore wird zusätzlich in einem isolierten `XDG_DATA_HOME`
|
||||||
end-to-end geprüft.
|
end-to-end geprüft.
|
||||||
|
|
|
||||||
|
|
@ -75,13 +75,16 @@ Voraussetzung für Phase 1.
|
||||||
- [x] Kleine Displays (1024 px) geprüft
|
- [x] Kleine Displays (1024 px) geprüft
|
||||||
- [x] Performance bei vielen Segmenten und Sections (Cache + Stresstest)
|
- [x] Performance bei vielen Segmenten und Sections (Cache + Stresstest)
|
||||||
|
|
||||||
### Nächster Schritt: Kontosync (Desktop ↔ Tablet)
|
### Erledigt: Kontosync (Desktop ↔ Tablet)
|
||||||
|
|
||||||
- [ ] Sync-Einstellungen in der App (FTP-Host, Port, Benutzer, Passwort,
|
- [x] Sync-Einstellungen in der App (FTP-Host, Port, Benutzer, Passwort,
|
||||||
Verzeichnis), Zugangsdaten unter `user://`, nicht im Repo
|
Verzeichnis, FTPS, Zertifikatsprüfung), Zugangsdaten unter `user://`
|
||||||
- [ ] Manueller Sync: neueste `.tres` je Kür laden/hochladen
|
- [x] Manueller Sync: `.tres` je Kür laden/hochladen, Sync-Stand lokal
|
||||||
- [ ] Konflikte als Kopien mit Zeitstempel behalten
|
- [x] Konflikte als Kopien mit Server-Zeitstempel behalten
|
||||||
- [ ] Automatischer Sync und Sync-Status in der Oberfläche (später)
|
- [x] Fortschritt und Zusammenfassung in der Oberfläche
|
||||||
|
- [x] FTP/FTPS-Client ohne externe Bibliothek
|
||||||
|
- [ ] Automatischer Sync im Hintergrund (später)
|
||||||
|
- [ ] Konflikt-Zusammenführung in der Oberfläche (später)
|
||||||
|
|
||||||
### Danach: Element-Marker
|
### Danach: Element-Marker
|
||||||
|
|
||||||
|
|
|
||||||
80
docs/sync.md
Normal file
80
docs/sync.md
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
# Kontosync über FTP
|
||||||
|
|
||||||
|
Der Abgleich zwischen Desktop und Tablet läuft **manuell** über einen
|
||||||
|
FTP-Zugang. Es gibt kein Konto im Sinne einer Anmeldung auf einem Server:
|
||||||
|
Die Zugangsdaten sind das „Konto“, jedes Gerät bekommt dieselben Daten.
|
||||||
|
|
||||||
|
## Einrichten
|
||||||
|
|
||||||
|
1. Auf beiden Geräten: **Datei → Sync-Einstellungen…**
|
||||||
|
2. Eintragen:
|
||||||
|
- **Server** – Hostname des FTP-Servers (ohne `ftp://`)
|
||||||
|
- **Port** – normalerweise 21
|
||||||
|
- **Benutzer / Passwort**
|
||||||
|
- **Verzeichnis** – Zielordner relativ zum Login, z. B. `rinkplanner`
|
||||||
|
3. **FTPS** eingeschaltet lassen (empfohlen).
|
||||||
|
4. **Server-Zertifikat prüfen** nur abschalten, wenn der Hoster ein
|
||||||
|
abweichendes Zertifikat nutzt (bei Shared Hosting häufig). Die Verbindung
|
||||||
|
bleibt verschlüsselt, der Server wird dann aber nicht kryptografisch
|
||||||
|
bestätigt.
|
||||||
|
5. **Speichern**.
|
||||||
|
|
||||||
|
Danach **Datei → Jetzt synchronisieren**.
|
||||||
|
|
||||||
|
## Was passiert
|
||||||
|
|
||||||
|
- Jede Kür liegt als `.tres` unter `user://routines/` und wird 1:1 in das
|
||||||
|
FTP-Verzeichnis gespiegelt (flach, keine Unterordner).
|
||||||
|
- Der Sync vergleicht pro Datei den letzten gemeinsamen Stand
|
||||||
|
(`user://sync_state.cfg`) mit den aktuellen Zeitstempeln:
|
||||||
|
|
||||||
|
| Lage | Aktion |
|
||||||
|
| :--- | :--- |
|
||||||
|
| nur lokal, noch nie synchronisiert | hochladen |
|
||||||
|
| nur auf dem Server | herunterladen |
|
||||||
|
| seit dem letzten Sync nur lokal geändert | hochladen |
|
||||||
|
| seit dem letzten Sync nur auf dem Server geändert | herunterladen |
|
||||||
|
| beidseitig geändert | **Konflikt**: beide Fassungen behalten |
|
||||||
|
| Datei auf einer Seite gelöscht | nichts tun (keine Löschweitergabe) |
|
||||||
|
| erster Sync, Datei auf beiden Seiten | **Konflikt** (kein Stand bekannt) |
|
||||||
|
|
||||||
|
- Bei einem Konflikt bleibt die lokale Fassung unverändert; die Serverfassung
|
||||||
|
wird als `name (Server JJJJ-MM-TT HHMM).tres` daneben gespeichert. Beim
|
||||||
|
nächsten Sync wird diese Kopie als neue Kür hochgeladen. Zusammenführen
|
||||||
|
musst du selbst.
|
||||||
|
- Vor dem Sync wird eine geänderte, bereits gespeicherte Kür automatisch
|
||||||
|
gespeichert. Eine noch nie gespeicherte Kür öffnet zuerst „Speichern unter“.
|
||||||
|
- Läuft der Sync, zeigt die Statusleiste den Fortschritt; danach erscheint
|
||||||
|
eine Zusammenfassung (hochgeladen / heruntergeladen / Konflikte / Fehler).
|
||||||
|
- Ist die geöffnete Kür vom Download betroffen und ungespeichert, wird sie
|
||||||
|
**nicht** überschrieben – es erscheint ein Hinweis.
|
||||||
|
|
||||||
|
## Technik
|
||||||
|
|
||||||
|
| Datei | Aufgabe |
|
||||||
|
| :--- | :--- |
|
||||||
|
| `src/core/sync/ftp_client.gd` | FTP/FTPS-Client (USER/PASS, AUTH TLS + PROT P, EPSV/PASV, MLSD, SIZE, RETR, STOR, DELE, MKD, RNFR/RNTO) |
|
||||||
|
| `src/core/sync/sync_plan.gd` | reine Vergleichslogik, Konfliktnamen, Zeitstempel |
|
||||||
|
| `src/core/sync/routine_sync.gd` | Ablauf, Datei-Ersetzung, Sync-Stand |
|
||||||
|
|
||||||
|
- Der Sync läuft in einem `Thread`; die Oberfläche bleibt bedienbar und
|
||||||
|
pollt den Fortschritt in `_process`.
|
||||||
|
- Uploads gehen zuerst nach `name.uploading` und werden dann umbenannt
|
||||||
|
(falls der Server das verweigert: direkter Upload).
|
||||||
|
- Downloads landen in `name.part` und ersetzen die Zieldatei erst danach.
|
||||||
|
- Zugangsdaten liegen in `user://sync.cfg` (Klartext, app-privat). Auf
|
||||||
|
Android ist das der interne App-Speicher, auf dem Desktop
|
||||||
|
`~/.local/share/godot/app_userdata/Rinkplanner/`.
|
||||||
|
- Android braucht die `INTERNET`-Berechtigung – ist im APK gesetzt.
|
||||||
|
|
||||||
|
## Grenzen
|
||||||
|
|
||||||
|
- Kein automatischer Sync, kein Push, kein Locking. Zwei Geräte, die
|
||||||
|
gleichzeitig synchronisieren, können sich überholen; der Konfliktfall
|
||||||
|
verhindert dann Datenverlust, nicht aber doppelte Dateien.
|
||||||
|
- Löschungen werden nicht übertragen (bewusst, um Versehen nicht zu
|
||||||
|
multiplizieren).
|
||||||
|
- Umbenennen einer Kür wirkt wie „neu lokal“ + „nur Server“: beide Fassungen
|
||||||
|
landen nebeneinander.
|
||||||
|
- Sehr große Kürsammlungen werden vollständig verglichen; für hunderte
|
||||||
|
Dateien ist das noch unkritisch, für tausende wäre ein Manifest sinnvoll.
|
||||||
|
|
@ -550,6 +550,110 @@ size = Vector2i(460, 160)
|
||||||
ok_button_text = "Löschen"
|
ok_button_text = "Löschen"
|
||||||
cancel_button_text = "Abbrechen"
|
cancel_button_text = "Abbrechen"
|
||||||
|
|
||||||
|
[node name="SyncSettingsDialog" type="ConfirmationDialog" parent="."]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
title = "Sync-Einstellungen (FTP)"
|
||||||
|
size = Vector2i(620, 620)
|
||||||
|
ok_button_text = "Speichern"
|
||||||
|
cancel_button_text = "Abbrechen"
|
||||||
|
|
||||||
|
[node name="SyncBox" type="VBoxContainer" parent="SyncSettingsDialog"]
|
||||||
|
offset_left = 12.0
|
||||||
|
offset_top = 12.0
|
||||||
|
offset_right = 608.0
|
||||||
|
offset_bottom = 540.0
|
||||||
|
theme_override_constants/separation = 10
|
||||||
|
|
||||||
|
[node name="SyncHint" type="Label" parent="SyncSettingsDialog/SyncBox"]
|
||||||
|
custom_minimum_size = Vector2(560, 0)
|
||||||
|
layout_mode = 2
|
||||||
|
theme_override_colors/font_color = Color(0.580392, 0.639216, 0.721569, 1)
|
||||||
|
theme_override_font_sizes/font_size = 14
|
||||||
|
text = "Zugangsdaten werden nur lokal unter user://sync.cfg gespeichert. Der Abgleich läuft manuell über Datei → Jetzt synchronisieren."
|
||||||
|
autowrap_mode = 2
|
||||||
|
|
||||||
|
[node name="SyncGrid" type="GridContainer" parent="SyncSettingsDialog/SyncBox"]
|
||||||
|
layout_mode = 2
|
||||||
|
theme_override_constants/h_separation = 10
|
||||||
|
theme_override_constants/v_separation = 8
|
||||||
|
columns = 2
|
||||||
|
|
||||||
|
[node name="SyncHostLabel" type="Label" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
layout_mode = 2
|
||||||
|
text = "Server"
|
||||||
|
|
||||||
|
[node name="SyncHostEdit" type="LineEdit" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
custom_minimum_size = Vector2(330, 48)
|
||||||
|
layout_mode = 2
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
placeholder_text = "ftp.example.org"
|
||||||
|
|
||||||
|
[node name="SyncPortLabel" type="Label" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
layout_mode = 2
|
||||||
|
text = "Port"
|
||||||
|
|
||||||
|
[node name="SyncPortEdit" type="LineEdit" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
custom_minimum_size = Vector2(330, 48)
|
||||||
|
layout_mode = 2
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
text = "21"
|
||||||
|
|
||||||
|
[node name="SyncUserLabel" type="Label" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
layout_mode = 2
|
||||||
|
text = "Benutzer"
|
||||||
|
|
||||||
|
[node name="SyncUserEdit" type="LineEdit" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
custom_minimum_size = Vector2(330, 48)
|
||||||
|
layout_mode = 2
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
|
||||||
|
[node name="SyncPasswordLabel" type="Label" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
layout_mode = 2
|
||||||
|
text = "Passwort"
|
||||||
|
|
||||||
|
[node name="SyncPasswordEdit" type="LineEdit" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
custom_minimum_size = Vector2(330, 48)
|
||||||
|
layout_mode = 2
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
secret = true
|
||||||
|
|
||||||
|
[node name="SyncDirectoryLabel" type="Label" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
layout_mode = 2
|
||||||
|
text = "Verzeichnis"
|
||||||
|
|
||||||
|
[node name="SyncDirectoryEdit" type="LineEdit" parent="SyncSettingsDialog/SyncBox/SyncGrid"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
custom_minimum_size = Vector2(330, 48)
|
||||||
|
layout_mode = 2
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
placeholder_text = "rinkplanner"
|
||||||
|
|
||||||
|
[node name="SyncFtpsCheck" type="CheckBox" parent="SyncSettingsDialog/SyncBox"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
custom_minimum_size = Vector2(0, 48)
|
||||||
|
layout_mode = 2
|
||||||
|
button_pressed = true
|
||||||
|
text = "FTPS (verschlüsselte Verbindung)"
|
||||||
|
|
||||||
|
[node name="SyncVerifyCheck" type="CheckBox" parent="SyncSettingsDialog/SyncBox"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
custom_minimum_size = Vector2(0, 48)
|
||||||
|
layout_mode = 2
|
||||||
|
button_pressed = true
|
||||||
|
text = "Server-Zertifikat prüfen"
|
||||||
|
|
||||||
|
[node name="SyncVerifyHint" type="Label" parent="SyncSettingsDialog/SyncBox"]
|
||||||
|
custom_minimum_size = Vector2(560, 0)
|
||||||
|
layout_mode = 2
|
||||||
|
theme_override_colors/font_color = Color(0.580392, 0.639216, 0.721569, 1)
|
||||||
|
theme_override_font_sizes/font_size = 13
|
||||||
|
text = "Nur abschalten, wenn der Server ein abweichendes Zertifikat nutzt (z. B. Shared Hosting)."
|
||||||
|
autowrap_mode = 2
|
||||||
|
|
||||||
[node name="AutosaveTimer" type="Timer" parent="."]
|
[node name="AutosaveTimer" type="Timer" parent="."]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
wait_time = 1.5
|
wait_time = 1.5
|
||||||
|
|
|
||||||
514
rinkplanner/src/core/sync/ftp_client.gd
Normal file
514
rinkplanner/src/core/sync/ftp_client.gd
Normal file
|
|
@ -0,0 +1,514 @@
|
||||||
|
class_name FtpClient
|
||||||
|
extends RefCounted
|
||||||
|
|
||||||
|
## Minimal blocking FTP/FTPS client for the manual routine sync.
|
||||||
|
##
|
||||||
|
## Runs on a worker thread, so it never touches the scene tree. All methods
|
||||||
|
## return an error string ("" means success); [member last_error] carries the
|
||||||
|
## reason for value-returning helpers.
|
||||||
|
##
|
||||||
|
## Supported: USER/PASS, AUTH TLS + PROT P (explicit FTPS), EPSV/PASV,
|
||||||
|
## MLSD, SIZE, MDTM, RETR, STOR, DELE, MKD, CWD, PWD, RNFR/RNTO.
|
||||||
|
|
||||||
|
enum Encryption {
|
||||||
|
NONE,
|
||||||
|
EXPLICIT_TLS,
|
||||||
|
}
|
||||||
|
|
||||||
|
const DEFAULT_TIMEOUT_MSEC := 15000
|
||||||
|
const TRANSFER_TIMEOUT_MSEC := 120000
|
||||||
|
const POLL_DELAY_MSEC := 5
|
||||||
|
|
||||||
|
var last_error: String = ""
|
||||||
|
|
||||||
|
var _tcp: StreamPeerTCP
|
||||||
|
var _peer: StreamPeer
|
||||||
|
var _host: String = ""
|
||||||
|
var _port: int = 21
|
||||||
|
var _timeout_msec: int = DEFAULT_TIMEOUT_MSEC
|
||||||
|
var _encryption: Encryption = Encryption.NONE
|
||||||
|
var _tls_options: TLSOptions
|
||||||
|
var _buffer: String = ""
|
||||||
|
var _expected_size: int = -1
|
||||||
|
|
||||||
|
|
||||||
|
func connect_to(
|
||||||
|
host: String,
|
||||||
|
port: int = 21,
|
||||||
|
encryption: Encryption = Encryption.NONE,
|
||||||
|
verify_certificate: bool = true
|
||||||
|
) -> String:
|
||||||
|
last_error = ""
|
||||||
|
_host = host
|
||||||
|
_port = port
|
||||||
|
_encryption = encryption
|
||||||
|
_tls_options = (
|
||||||
|
TLSOptions.client()
|
||||||
|
if verify_certificate
|
||||||
|
else TLSOptions.client_unsafe()
|
||||||
|
)
|
||||||
|
|
||||||
|
_tcp = StreamPeerTCP.new()
|
||||||
|
var err := _tcp.connect_to_host(host, port)
|
||||||
|
if err != OK:
|
||||||
|
return "Verbindung zu %s:%d fehlgeschlagen (Fehler %d)" % [host, port, err]
|
||||||
|
|
||||||
|
if not _wait_for_tcp(_tcp, _timeout_msec):
|
||||||
|
close()
|
||||||
|
return "Zeitüberschreitung beim Verbindungsaufbau zu %s:%d" % [host, port]
|
||||||
|
|
||||||
|
_peer = _tcp
|
||||||
|
var reply := _read_reply()
|
||||||
|
if reply.is_empty() or int(reply["code"]) / 100 != 2:
|
||||||
|
close()
|
||||||
|
return "Server hat die Verbindung nicht bestätigt: %s" % _reply_text(reply)
|
||||||
|
|
||||||
|
if encryption == Encryption.EXPLICIT_TLS:
|
||||||
|
reply = _command("AUTH TLS")
|
||||||
|
if reply.get("code", 0) != 234:
|
||||||
|
close()
|
||||||
|
return "Server lehnt FTPS ab: %s" % _reply_text(reply)
|
||||||
|
var tls := StreamPeerTLS.new()
|
||||||
|
var tls_err := tls.connect_to_stream(_tcp, host, _tls_options)
|
||||||
|
if tls_err != OK:
|
||||||
|
close()
|
||||||
|
return "TLS-Handshake konnte nicht gestartet werden (Fehler %d)" % tls_err
|
||||||
|
var deadline := Time.get_ticks_msec() + _timeout_msec
|
||||||
|
while tls.get_status() == StreamPeerTLS.STATUS_HANDSHAKING:
|
||||||
|
if Time.get_ticks_msec() > deadline:
|
||||||
|
close()
|
||||||
|
return "TLS-Handshake: Zeitüberschreitung"
|
||||||
|
tls.poll()
|
||||||
|
OS.delay_msec(POLL_DELAY_MSEC)
|
||||||
|
if tls.get_status() != StreamPeerTLS.STATUS_CONNECTED:
|
||||||
|
var status := tls.get_status()
|
||||||
|
close()
|
||||||
|
return (
|
||||||
|
"TLS-Handshake fehlgeschlagen (Status %d). "
|
||||||
|
+ "Wenn der Server ein ungültiges Zertifikat nutzt, "
|
||||||
|
+ "die Zertifikatsprüfung in den Sync-Einstellungen abschalten."
|
||||||
|
) % status
|
||||||
|
_peer = tls
|
||||||
|
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func login(user: String, password: String) -> String:
|
||||||
|
var reply := _command("USER " + user)
|
||||||
|
if reply.get("code", 0) == 331:
|
||||||
|
reply = _command("PASS " + password)
|
||||||
|
if reply.get("code", 0) != 230:
|
||||||
|
return "Login fehlgeschlagen: %s" % _reply_text(reply)
|
||||||
|
|
||||||
|
if _encryption == Encryption.EXPLICIT_TLS:
|
||||||
|
reply = _command("PBSZ 0")
|
||||||
|
if reply.get("code", 0) != 200:
|
||||||
|
return "PBSZ fehlgeschlagen: %s" % _reply_text(reply)
|
||||||
|
reply = _command("PROT P")
|
||||||
|
if reply.get("code", 0) != 200:
|
||||||
|
return "PROT P fehlgeschlagen: %s" % _reply_text(reply)
|
||||||
|
|
||||||
|
reply = _command("TYPE I")
|
||||||
|
if reply.get("code", 0) != 200:
|
||||||
|
return "Binärmodus nicht möglich: %s" % _reply_text(reply)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func pwd() -> String:
|
||||||
|
var reply := _command("PWD")
|
||||||
|
if reply.get("code", 0) != 257:
|
||||||
|
last_error = _reply_text(reply)
|
||||||
|
return ""
|
||||||
|
var text: String = reply.get("text", "")
|
||||||
|
var start := text.find('"')
|
||||||
|
var end := text.find('"', start + 1)
|
||||||
|
if start < 0 or end < 0:
|
||||||
|
return ""
|
||||||
|
return text.substr(start + 1, end - start - 1)
|
||||||
|
|
||||||
|
|
||||||
|
func change_dir(path: String) -> String:
|
||||||
|
var reply := _command("CWD " + path)
|
||||||
|
if reply.get("code", 0) != 250:
|
||||||
|
return "Verzeichniswechsel fehlgeschlagen: %s" % _reply_text(reply)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func make_dir(path: String) -> String:
|
||||||
|
var reply := _command("MKD " + path)
|
||||||
|
if reply.get("code", 0) in [250, 257]:
|
||||||
|
return ""
|
||||||
|
return "Ordner konnte nicht angelegt werden: %s" % _reply_text(reply)
|
||||||
|
|
||||||
|
|
||||||
|
## Returns [name, size, modify] entries with type "file" only.
|
||||||
|
func list_files() -> Array[Dictionary]:
|
||||||
|
var entries: Array[Dictionary] = []
|
||||||
|
var socket := _open_data_socket()
|
||||||
|
if socket == null:
|
||||||
|
return entries
|
||||||
|
|
||||||
|
var reply := _command("MLSD")
|
||||||
|
if reply.get("code", 0) not in [125, 150]:
|
||||||
|
close_data_connection(socket)
|
||||||
|
last_error = "Verzeichnisliste nicht möglich: %s" % _reply_text(reply)
|
||||||
|
return entries
|
||||||
|
|
||||||
|
var data_peer := _secure_data_peer(socket)
|
||||||
|
if data_peer == null:
|
||||||
|
_read_reply()
|
||||||
|
return entries
|
||||||
|
var payload := _read_all(data_peer, Time.get_ticks_msec() + TRANSFER_TIMEOUT_MSEC)
|
||||||
|
close_data_connection(data_peer)
|
||||||
|
reply = _read_reply()
|
||||||
|
if reply.get("code", 0) != 226:
|
||||||
|
last_error = "Übertragung der Liste unvollständig: %s" % _reply_text(reply)
|
||||||
|
return entries
|
||||||
|
|
||||||
|
for raw_line: String in payload.get_string_from_utf8().split("\n"):
|
||||||
|
var line := raw_line.strip_edges()
|
||||||
|
if line.is_empty() or not line.contains(";"):
|
||||||
|
continue
|
||||||
|
var parts := line.split(" ", false, 1)
|
||||||
|
if parts.size() < 2:
|
||||||
|
continue
|
||||||
|
var facts := parts[0].split(";", false)
|
||||||
|
var name := parts[1]
|
||||||
|
var info: Dictionary = {"name": name, "size": -1, "modify": -1}
|
||||||
|
for fact: String in facts:
|
||||||
|
var pair := fact.split("=", true, 1)
|
||||||
|
if pair.size() != 2:
|
||||||
|
continue
|
||||||
|
match pair[0].to_lower():
|
||||||
|
"type":
|
||||||
|
info["type"] = pair[1].to_lower()
|
||||||
|
"size":
|
||||||
|
info["size"] = int(pair[1])
|
||||||
|
"modify":
|
||||||
|
info["modify"] = parse_mdtm(pair[1])
|
||||||
|
if info.get("type", "") != "file":
|
||||||
|
continue
|
||||||
|
if name in [".", ".."]:
|
||||||
|
continue
|
||||||
|
entries.append(info)
|
||||||
|
return entries
|
||||||
|
|
||||||
|
|
||||||
|
func file_size(name: String) -> int:
|
||||||
|
var reply := _command("SIZE " + name)
|
||||||
|
if reply.get("code", 0) != 213:
|
||||||
|
last_error = _reply_text(reply)
|
||||||
|
return -1
|
||||||
|
var value := str(reply.get("text", "")).strip_edges().split(" ", false, 1)
|
||||||
|
return int(value[-1]) if value.size() > 0 else -1
|
||||||
|
|
||||||
|
|
||||||
|
func download(remote_name: String, local_path: String) -> String:
|
||||||
|
_expected_size = file_size(remote_name)
|
||||||
|
var socket := _open_data_socket()
|
||||||
|
if socket == null:
|
||||||
|
return "Datenverbindung fehlgeschlagen: " + last_error
|
||||||
|
|
||||||
|
var reply := _command("RETR " + remote_name)
|
||||||
|
if reply.get("code", 0) not in [125, 150]:
|
||||||
|
close_data_connection(socket)
|
||||||
|
return "Download abgelehnt: %s" % _reply_text(reply)
|
||||||
|
|
||||||
|
var data_peer := _secure_data_peer(socket)
|
||||||
|
if data_peer == null:
|
||||||
|
_read_reply()
|
||||||
|
return last_error
|
||||||
|
var payload := _read_all(
|
||||||
|
data_peer,
|
||||||
|
Time.get_ticks_msec() + TRANSFER_TIMEOUT_MSEC,
|
||||||
|
_expected_size
|
||||||
|
)
|
||||||
|
close_data_connection(data_peer)
|
||||||
|
reply = _read_reply()
|
||||||
|
if reply.get("code", 0) != 226:
|
||||||
|
return "Download unvollständig: %s" % _reply_text(reply)
|
||||||
|
if _expected_size >= 0 and payload.size() != _expected_size:
|
||||||
|
return (
|
||||||
|
"Download unvollständig: %d von %d Bytes"
|
||||||
|
% [payload.size(), _expected_size]
|
||||||
|
)
|
||||||
|
|
||||||
|
var file := FileAccess.open(local_path, FileAccess.WRITE)
|
||||||
|
if file == null:
|
||||||
|
return "Lokale Datei nicht schreibbar: %s" % local_path
|
||||||
|
file.store_buffer(payload)
|
||||||
|
file.close()
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func upload(local_path: String, remote_name: String) -> String:
|
||||||
|
var file := FileAccess.open(local_path, FileAccess.READ)
|
||||||
|
if file == null:
|
||||||
|
return "Lokale Datei nicht lesbar: %s" % local_path
|
||||||
|
var payload := file.get_buffer(file.get_length())
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
var socket := _open_data_socket()
|
||||||
|
if socket == null:
|
||||||
|
return "Datenverbindung fehlgeschlagen: " + last_error
|
||||||
|
|
||||||
|
var reply := _command("STOR " + remote_name)
|
||||||
|
if reply.get("code", 0) not in [125, 150]:
|
||||||
|
close_data_connection(socket)
|
||||||
|
return "Upload abgelehnt: %s" % _reply_text(reply)
|
||||||
|
|
||||||
|
var data_peer := _secure_data_peer(socket)
|
||||||
|
if data_peer == null:
|
||||||
|
_read_reply()
|
||||||
|
return last_error
|
||||||
|
var err := data_peer.put_data(payload)
|
||||||
|
close_data_connection(data_peer)
|
||||||
|
if err != OK:
|
||||||
|
return "Upload abgebrochen (Fehler %d)" % err
|
||||||
|
|
||||||
|
reply = _read_reply()
|
||||||
|
if reply.get("code", 0) != 226:
|
||||||
|
return "Upload unvollständig: %s" % _reply_text(reply)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func delete_file(remote_name: String) -> String:
|
||||||
|
var reply := _command("DELE " + remote_name)
|
||||||
|
if reply.get("code", 0) != 250:
|
||||||
|
return "Löschen abgelehnt: %s" % _reply_text(reply)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func rename(from_name: String, to_name: String) -> String:
|
||||||
|
var reply := _command("RNFR " + from_name)
|
||||||
|
if reply.get("code", 0) != 350:
|
||||||
|
return "Umbenennen abgelehnt: %s" % _reply_text(reply)
|
||||||
|
reply = _command("RNTO " + to_name)
|
||||||
|
if reply.get("code", 0) != 250:
|
||||||
|
return "Umbenennen fehlgeschlagen: %s" % _reply_text(reply)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func close() -> void:
|
||||||
|
if _peer is StreamPeerTLS:
|
||||||
|
(_peer as StreamPeerTLS).disconnect_from_stream()
|
||||||
|
_peer = null
|
||||||
|
if _tcp != null:
|
||||||
|
_tcp.disconnect_from_host()
|
||||||
|
_tcp = null
|
||||||
|
_buffer = ""
|
||||||
|
|
||||||
|
|
||||||
|
# --- protocol helpers --------------------------------------------------------
|
||||||
|
|
||||||
|
static func parse_mdtm(value: String) -> int:
|
||||||
|
# YYYYMMDDHHMMSS[.sss], UTC.
|
||||||
|
var digits := value.strip_edges()
|
||||||
|
if digits.contains("."):
|
||||||
|
digits = digits.split(".", true, 1)[0]
|
||||||
|
if digits.length() < 14 or not digits.is_valid_int():
|
||||||
|
return -1
|
||||||
|
var iso := "%s-%s-%sT%s:%s:%s" % [
|
||||||
|
digits.substr(0, 4),
|
||||||
|
digits.substr(4, 2),
|
||||||
|
digits.substr(6, 2),
|
||||||
|
digits.substr(8, 2),
|
||||||
|
digits.substr(10, 2),
|
||||||
|
digits.substr(12, 2),
|
||||||
|
]
|
||||||
|
return int(Time.get_unix_time_from_datetime_string(iso))
|
||||||
|
|
||||||
|
|
||||||
|
static func parse_pasv(reply: String) -> Dictionary:
|
||||||
|
# 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)
|
||||||
|
var start := reply.find("(")
|
||||||
|
var end := reply.find(")")
|
||||||
|
if start < 0 or end <= start:
|
||||||
|
return {}
|
||||||
|
var numbers := reply.substr(start + 1, end - start - 1).split(",", false)
|
||||||
|
if numbers.size() != 6:
|
||||||
|
return {}
|
||||||
|
var host := "%s.%s.%s.%s" % [numbers[0], numbers[1], numbers[2], numbers[3]]
|
||||||
|
var port := int(numbers[4]) * 256 + int(numbers[5])
|
||||||
|
return {"host": host, "port": port}
|
||||||
|
|
||||||
|
|
||||||
|
static func parse_epsv(reply: String) -> int:
|
||||||
|
# 229 Entering Extended Passive Mode (|||port|)
|
||||||
|
var start := reply.find("(")
|
||||||
|
var end := reply.find(")", start + 1)
|
||||||
|
if start < 0 or end <= start:
|
||||||
|
return -1
|
||||||
|
var body := reply.substr(start + 1, end - start - 1)
|
||||||
|
var parts := body.split("|", true)
|
||||||
|
for part: String in parts:
|
||||||
|
if part.is_valid_int():
|
||||||
|
return int(part)
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
# --- transport ---------------------------------------------------------------
|
||||||
|
|
||||||
|
func _command(command: String) -> Dictionary:
|
||||||
|
if _peer == null:
|
||||||
|
last_error = "Nicht verbunden"
|
||||||
|
return {}
|
||||||
|
var payload := (command + "\r\n").to_utf8_buffer()
|
||||||
|
var err := _peer.put_data(payload)
|
||||||
|
if err != OK:
|
||||||
|
last_error = "Senden fehlgeschlagen (Fehler %d)" % err
|
||||||
|
return {}
|
||||||
|
return _read_reply()
|
||||||
|
|
||||||
|
|
||||||
|
func _read_reply() -> Dictionary:
|
||||||
|
var deadline := Time.get_ticks_msec() + _timeout_msec
|
||||||
|
var lines := PackedStringArray()
|
||||||
|
var code := -1
|
||||||
|
while true:
|
||||||
|
var line := _read_line(deadline)
|
||||||
|
if line.is_empty():
|
||||||
|
last_error = "Zeitüberschreitung beim Warten auf den Server"
|
||||||
|
return {}
|
||||||
|
lines.append(line)
|
||||||
|
if line.length() >= 4 and line[3] == " ":
|
||||||
|
code = int(line.substr(0, 3))
|
||||||
|
break
|
||||||
|
if line.length() == 3:
|
||||||
|
code = int(line)
|
||||||
|
break
|
||||||
|
return {"code": code, "text": "\n".join(lines)}
|
||||||
|
|
||||||
|
|
||||||
|
func _read_line(deadline: int) -> String:
|
||||||
|
while true:
|
||||||
|
var index := _buffer.find("\n")
|
||||||
|
if index >= 0:
|
||||||
|
var line := _buffer.substr(0, index).trim_suffix("\r")
|
||||||
|
_buffer = _buffer.substr(index + 1)
|
||||||
|
return line
|
||||||
|
if Time.get_ticks_msec() > deadline:
|
||||||
|
return ""
|
||||||
|
if _peer is StreamPeerTLS:
|
||||||
|
(_peer as StreamPeerTLS).poll()
|
||||||
|
var available := _peer.get_available_bytes()
|
||||||
|
if available > 0:
|
||||||
|
var chunk := _peer.get_data(available)
|
||||||
|
if chunk[0] == OK:
|
||||||
|
_buffer += (chunk[1] as PackedByteArray).get_string_from_utf8()
|
||||||
|
continue
|
||||||
|
if not _is_connected(_peer):
|
||||||
|
return ""
|
||||||
|
OS.delay_msec(POLL_DELAY_MSEC)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func _open_data_socket() -> StreamPeerTCP:
|
||||||
|
last_error = ""
|
||||||
|
var reply := _command("EPSV")
|
||||||
|
var host := _host
|
||||||
|
var port := parse_epsv(str(reply.get("text", "")))
|
||||||
|
if reply.get("code", 0) != 229 or port <= 0:
|
||||||
|
reply = _command("PASV")
|
||||||
|
var parsed := parse_pasv(str(reply.get("text", "")))
|
||||||
|
if reply.get("code", 0) != 227 or parsed.is_empty():
|
||||||
|
last_error = "Passiver Modus nicht möglich: %s" % _reply_text(reply)
|
||||||
|
return null
|
||||||
|
host = parsed["host"]
|
||||||
|
port = parsed["port"]
|
||||||
|
|
||||||
|
var tcp := StreamPeerTCP.new()
|
||||||
|
var err := tcp.connect_to_host(host, port)
|
||||||
|
if err != OK:
|
||||||
|
last_error = "Datenverbindung fehlgeschlagen (Fehler %d)" % err
|
||||||
|
return null
|
||||||
|
if not _wait_for_tcp(tcp, _timeout_msec):
|
||||||
|
tcp.disconnect_from_host()
|
||||||
|
last_error = "Zeitüberschreitung bei der Datenverbindung"
|
||||||
|
return null
|
||||||
|
return tcp
|
||||||
|
|
||||||
|
|
||||||
|
## Wraps the data socket in TLS for explicit FTPS. Called after the server
|
||||||
|
## accepted the transfer command, which is when the handshake actually starts.
|
||||||
|
func _secure_data_peer(socket: StreamPeerTCP) -> StreamPeer:
|
||||||
|
if _encryption != Encryption.EXPLICIT_TLS:
|
||||||
|
return socket
|
||||||
|
|
||||||
|
var tls := StreamPeerTLS.new()
|
||||||
|
var err := tls.connect_to_stream(socket, _host, _tls_options)
|
||||||
|
if err != OK:
|
||||||
|
socket.disconnect_from_host()
|
||||||
|
last_error = "TLS der Datenverbindung fehlgeschlagen (Fehler %d)" % err
|
||||||
|
return null
|
||||||
|
|
||||||
|
var deadline := Time.get_ticks_msec() + _timeout_msec
|
||||||
|
while tls.get_status() == StreamPeerTLS.STATUS_HANDSHAKING:
|
||||||
|
if Time.get_ticks_msec() > deadline:
|
||||||
|
tls.disconnect_from_stream()
|
||||||
|
last_error = "TLS-Handshake der Datenverbindung: Zeitüberschreitung"
|
||||||
|
return null
|
||||||
|
tls.poll()
|
||||||
|
OS.delay_msec(POLL_DELAY_MSEC)
|
||||||
|
if tls.get_status() != StreamPeerTLS.STATUS_CONNECTED:
|
||||||
|
tls.disconnect_from_stream()
|
||||||
|
last_error = "TLS-Handshake der Datenverbindung fehlgeschlagen (Status %d)" % tls.get_status()
|
||||||
|
return null
|
||||||
|
return tls
|
||||||
|
|
||||||
|
|
||||||
|
func _read_all(peer: StreamPeer, deadline: int, expected_size: int = -1) -> PackedByteArray:
|
||||||
|
var payload := PackedByteArray()
|
||||||
|
while true:
|
||||||
|
if peer is StreamPeerTLS:
|
||||||
|
(peer as StreamPeerTLS).poll()
|
||||||
|
if _is_connected(peer):
|
||||||
|
var available := peer.get_available_bytes()
|
||||||
|
if available > 0:
|
||||||
|
var chunk := peer.get_data(available)
|
||||||
|
if chunk[0] == OK:
|
||||||
|
payload.append_array(chunk[1])
|
||||||
|
continue
|
||||||
|
if expected_size >= 0:
|
||||||
|
if payload.size() >= expected_size:
|
||||||
|
break
|
||||||
|
if not _is_connected(peer):
|
||||||
|
break
|
||||||
|
elif not _is_connected(peer):
|
||||||
|
break
|
||||||
|
if Time.get_ticks_msec() > deadline:
|
||||||
|
last_error = "Zeitüberschreitung bei der Datenübertragung"
|
||||||
|
break
|
||||||
|
OS.delay_msec(POLL_DELAY_MSEC)
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
func close_data_connection(peer: StreamPeer) -> void:
|
||||||
|
if peer == null:
|
||||||
|
return
|
||||||
|
if peer is StreamPeerTLS:
|
||||||
|
(peer as StreamPeerTLS).disconnect_from_stream()
|
||||||
|
elif peer is StreamPeerTCP:
|
||||||
|
(peer as StreamPeerTCP).disconnect_from_host()
|
||||||
|
|
||||||
|
|
||||||
|
func _wait_for_tcp(tcp: StreamPeerTCP, timeout_msec: int) -> bool:
|
||||||
|
var deadline := Time.get_ticks_msec() + timeout_msec
|
||||||
|
while tcp.get_status() == StreamPeerTCP.STATUS_CONNECTING:
|
||||||
|
if Time.get_ticks_msec() > deadline:
|
||||||
|
return false
|
||||||
|
tcp.poll()
|
||||||
|
OS.delay_msec(POLL_DELAY_MSEC)
|
||||||
|
return tcp.get_status() == StreamPeerTCP.STATUS_CONNECTED
|
||||||
|
|
||||||
|
|
||||||
|
func _is_connected(peer: StreamPeer) -> bool:
|
||||||
|
if peer is StreamPeerTLS:
|
||||||
|
return (peer as StreamPeerTLS).get_status() == StreamPeerTLS.STATUS_CONNECTED
|
||||||
|
return (peer as StreamPeerTCP).get_status() == StreamPeerTCP.STATUS_CONNECTED
|
||||||
|
|
||||||
|
|
||||||
|
func _reply_text(reply: Dictionary) -> String:
|
||||||
|
if reply.is_empty():
|
||||||
|
return last_error
|
||||||
|
return str(reply.get("text", "")).strip_edges().replace("\n", " ")
|
||||||
1
rinkplanner/src/core/sync/ftp_client.gd.uid
Normal file
1
rinkplanner/src/core/sync/ftp_client.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://3n477aydpya3
|
||||||
290
rinkplanner/src/core/sync/routine_sync.gd
Normal file
290
rinkplanner/src/core/sync/routine_sync.gd
Normal file
|
|
@ -0,0 +1,290 @@
|
||||||
|
class_name RoutineSync
|
||||||
|
extends RefCounted
|
||||||
|
|
||||||
|
## Executes the manual sync between `user://routines` and an FTP directory.
|
||||||
|
## Intended to run on a worker thread: it only reports through [member result],
|
||||||
|
## [member error] and [member progress], never through signals.
|
||||||
|
|
||||||
|
const LOCAL_DIR := "user://routines"
|
||||||
|
const STATE_PATH := "user://sync_state.cfg"
|
||||||
|
const TEMP_SUFFIX := ".part"
|
||||||
|
const UPLOAD_SUFFIX := ".uploading"
|
||||||
|
const ROUTINE_EXTENSION := ".tres"
|
||||||
|
|
||||||
|
var progress: String = ""
|
||||||
|
var error: String = ""
|
||||||
|
var result: Dictionary = {}
|
||||||
|
|
||||||
|
|
||||||
|
func run(settings: Dictionary) -> void:
|
||||||
|
result = {
|
||||||
|
"uploaded": [],
|
||||||
|
"downloaded": [],
|
||||||
|
"conflicts": [],
|
||||||
|
"conflict_sources": [],
|
||||||
|
"skipped": [],
|
||||||
|
"errors": [],
|
||||||
|
}
|
||||||
|
error = ""
|
||||||
|
|
||||||
|
var host := str(settings.get("host", "")).strip_edges()
|
||||||
|
var user := str(settings.get("user", "")).strip_edges()
|
||||||
|
var password := str(settings.get("password", ""))
|
||||||
|
var port := int(settings.get("port", 21))
|
||||||
|
var directory := str(settings.get("directory", "")).strip_edges().trim_prefix("/").trim_suffix("/")
|
||||||
|
var encryption := (
|
||||||
|
FtpClient.Encryption.EXPLICIT_TLS
|
||||||
|
if bool(settings.get("ftps", true))
|
||||||
|
else FtpClient.Encryption.NONE
|
||||||
|
)
|
||||||
|
var verify := bool(settings.get("verify_certificate", true))
|
||||||
|
|
||||||
|
if host.is_empty() or user.is_empty():
|
||||||
|
error = "Host und Benutzer müssen in den Sync-Einstellungen gesetzt sein."
|
||||||
|
return
|
||||||
|
|
||||||
|
_ensure_local_dir()
|
||||||
|
var client := FtpClient.new()
|
||||||
|
|
||||||
|
progress = "Verbinde mit %s:%d …" % [host, port]
|
||||||
|
var err := client.connect_to(host, port, encryption, verify)
|
||||||
|
if not err.is_empty():
|
||||||
|
error = err
|
||||||
|
client.close()
|
||||||
|
return
|
||||||
|
|
||||||
|
progress = "Anmeldung …"
|
||||||
|
err = client.login(user, password)
|
||||||
|
if not err.is_empty():
|
||||||
|
error = err
|
||||||
|
client.close()
|
||||||
|
return
|
||||||
|
|
||||||
|
if not directory.is_empty():
|
||||||
|
err = client.change_dir(directory)
|
||||||
|
if not err.is_empty():
|
||||||
|
progress = "Lege Zielordner an …"
|
||||||
|
var mkdir_error := client.make_dir(directory)
|
||||||
|
if not mkdir_error.is_empty():
|
||||||
|
error = "%s (%s)" % [mkdir_error, err]
|
||||||
|
client.close()
|
||||||
|
return
|
||||||
|
err = client.change_dir(directory)
|
||||||
|
if not err.is_empty():
|
||||||
|
error = err
|
||||||
|
client.close()
|
||||||
|
return
|
||||||
|
|
||||||
|
progress = "Lese Serververzeichnis …"
|
||||||
|
var remote := _remote_files(client)
|
||||||
|
if not client.last_error.is_empty():
|
||||||
|
error = client.last_error
|
||||||
|
client.close()
|
||||||
|
return
|
||||||
|
|
||||||
|
var local := local_files()
|
||||||
|
var state := load_state(STATE_PATH)
|
||||||
|
var plan := SyncPlan.build(local, remote, state)
|
||||||
|
|
||||||
|
var used_names: Dictionary = {}
|
||||||
|
for name: String in local.keys():
|
||||||
|
used_names[name] = true
|
||||||
|
|
||||||
|
var transferred := false
|
||||||
|
for item: Dictionary in plan:
|
||||||
|
var name: String = item["name"]
|
||||||
|
var action: int = item["action"]
|
||||||
|
progress = "%s: %s" % [name, SyncPlan.action_name(action)]
|
||||||
|
match action:
|
||||||
|
SyncPlan.Action.UPLOAD:
|
||||||
|
var upload_error := _upload(client, name)
|
||||||
|
if upload_error.is_empty():
|
||||||
|
result["uploaded"].append(name)
|
||||||
|
transferred = true
|
||||||
|
else:
|
||||||
|
result["errors"].append(upload_error)
|
||||||
|
SyncPlan.Action.DOWNLOAD:
|
||||||
|
var download_error := _download(client, name)
|
||||||
|
if download_error.is_empty():
|
||||||
|
result["downloaded"].append(name)
|
||||||
|
transferred = true
|
||||||
|
else:
|
||||||
|
result["errors"].append(download_error)
|
||||||
|
SyncPlan.Action.CONFLICT:
|
||||||
|
var conflict_error := _keep_both(client, name, used_names)
|
||||||
|
if conflict_error.is_empty():
|
||||||
|
transferred = true
|
||||||
|
else:
|
||||||
|
result["errors"].append(conflict_error)
|
||||||
|
_:
|
||||||
|
result["skipped"].append(name)
|
||||||
|
|
||||||
|
if transferred:
|
||||||
|
progress = "Aktualisiere Sync-Stand …"
|
||||||
|
_save_state(client, state)
|
||||||
|
|
||||||
|
client.close()
|
||||||
|
progress = "Fertig."
|
||||||
|
|
||||||
|
|
||||||
|
func _upload(client: FtpClient, name: String) -> String:
|
||||||
|
var local_path := _local_path(name)
|
||||||
|
var temporary := name + UPLOAD_SUFFIX
|
||||||
|
var err := client.upload(local_path, temporary)
|
||||||
|
if not err.is_empty():
|
||||||
|
return "Upload %s: %s" % [name, err]
|
||||||
|
|
||||||
|
var rename_error := client.rename(temporary, name)
|
||||||
|
if rename_error.is_empty():
|
||||||
|
return ""
|
||||||
|
# Some servers refuse to overwrite on rename: remove the temp file and
|
||||||
|
# upload directly (non-atomic, but the only portable fallback).
|
||||||
|
client.delete_file(temporary)
|
||||||
|
err = client.upload(local_path, name)
|
||||||
|
if not err.is_empty():
|
||||||
|
return "Upload %s: %s" % [name, err]
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func _download(client: FtpClient, name: String) -> String:
|
||||||
|
var target := _local_path(name)
|
||||||
|
var temporary := target + TEMP_SUFFIX
|
||||||
|
var err := client.download(name, temporary)
|
||||||
|
if not err.is_empty():
|
||||||
|
return "Download %s: %s" % [name, err]
|
||||||
|
return _replace_file(temporary, target)
|
||||||
|
|
||||||
|
|
||||||
|
func _keep_both(client: FtpClient, name: String, used_names: Dictionary) -> String:
|
||||||
|
var stamp := SyncPlan.timestamp_label()
|
||||||
|
var conflict_name := SyncPlan.unique_name(
|
||||||
|
SyncPlan.conflict_file_name(name, stamp),
|
||||||
|
used_names
|
||||||
|
)
|
||||||
|
var target := _local_path(conflict_name)
|
||||||
|
var temporary := target + TEMP_SUFFIX
|
||||||
|
var err := client.download(name, temporary)
|
||||||
|
if not err.is_empty():
|
||||||
|
return "Konflikt %s: %s" % [name, err]
|
||||||
|
|
||||||
|
err = _replace_file(temporary, target)
|
||||||
|
if not err.is_empty():
|
||||||
|
return "Konflikt %s: %s" % [name, err]
|
||||||
|
|
||||||
|
used_names[conflict_name] = true
|
||||||
|
result["conflicts"].append(conflict_name)
|
||||||
|
result["conflict_sources"].append(name)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
## Replaces [param target] with [param temporary] without losing the target if
|
||||||
|
## the rename fails: the old file is kept as a backup until the swap worked.
|
||||||
|
func _replace_file(temporary: String, target: String) -> String:
|
||||||
|
var absolute_target := ProjectSettings.globalize_path(target)
|
||||||
|
var absolute_temp := ProjectSettings.globalize_path(temporary)
|
||||||
|
var backup := absolute_target + ".bak"
|
||||||
|
var has_backup := false
|
||||||
|
|
||||||
|
if FileAccess.file_exists(target):
|
||||||
|
if DirAccess.rename_absolute(absolute_target, backup) != OK:
|
||||||
|
return "Zieldatei nicht ersetzbar: %s" % target
|
||||||
|
has_backup = true
|
||||||
|
|
||||||
|
var rename_error := DirAccess.rename_absolute(absolute_temp, absolute_target)
|
||||||
|
if rename_error != OK:
|
||||||
|
if has_backup:
|
||||||
|
DirAccess.rename_absolute(backup, absolute_target)
|
||||||
|
DirAccess.remove_absolute(absolute_temp)
|
||||||
|
return "Temporäre Datei nicht umbenennbar: %s" % target
|
||||||
|
|
||||||
|
if has_backup:
|
||||||
|
DirAccess.remove_absolute(backup)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
func _save_state(client: FtpClient, state: Dictionary) -> void:
|
||||||
|
# Refresh the server listing once so uploaded files get their new MDTM.
|
||||||
|
var remote_after := _remote_files(client)
|
||||||
|
var local_after := local_files()
|
||||||
|
var touched: Array = []
|
||||||
|
touched.append_array(result["uploaded"])
|
||||||
|
touched.append_array(result["downloaded"])
|
||||||
|
touched.append_array(result["conflict_sources"])
|
||||||
|
|
||||||
|
for name: String in touched:
|
||||||
|
if not local_after.has(name) or not remote_after.has(name):
|
||||||
|
continue
|
||||||
|
state[name] = {
|
||||||
|
"local_mtime": int(local_after[name].get("mtime", 0)),
|
||||||
|
"remote_modify": int(remote_after[name].get("modify", 0)),
|
||||||
|
}
|
||||||
|
|
||||||
|
save_state(STATE_PATH, state)
|
||||||
|
|
||||||
|
|
||||||
|
func _remote_files(client: FtpClient) -> Dictionary:
|
||||||
|
var files: Dictionary = {}
|
||||||
|
for entry: Dictionary in client.list_files():
|
||||||
|
var name := str(entry.get("name", ""))
|
||||||
|
if not name.ends_with(ROUTINE_EXTENSION):
|
||||||
|
continue
|
||||||
|
files[name] = {
|
||||||
|
"modify": int(entry.get("modify", 0)),
|
||||||
|
"size": int(entry.get("size", -1)),
|
||||||
|
}
|
||||||
|
return files
|
||||||
|
|
||||||
|
|
||||||
|
static func local_files(directory: String = LOCAL_DIR) -> Dictionary:
|
||||||
|
var files: Dictionary = {}
|
||||||
|
var dir := DirAccess.open(directory)
|
||||||
|
if dir == null:
|
||||||
|
return files
|
||||||
|
dir.list_dir_begin()
|
||||||
|
var name := dir.get_next()
|
||||||
|
while not name.is_empty():
|
||||||
|
if not dir.current_is_dir() and name.ends_with(ROUTINE_EXTENSION):
|
||||||
|
var path := directory.path_join(name)
|
||||||
|
files[name] = {
|
||||||
|
"mtime": FileAccess.get_modified_time(path),
|
||||||
|
"size": _file_size(path),
|
||||||
|
}
|
||||||
|
name = dir.get_next()
|
||||||
|
dir.list_dir_end()
|
||||||
|
return files
|
||||||
|
|
||||||
|
|
||||||
|
static func _file_size(path: String) -> int:
|
||||||
|
var file := FileAccess.open(path, FileAccess.READ)
|
||||||
|
if file == null:
|
||||||
|
return -1
|
||||||
|
var size := file.get_length()
|
||||||
|
file.close()
|
||||||
|
return size
|
||||||
|
|
||||||
|
|
||||||
|
static func load_state(path: String = STATE_PATH) -> Dictionary:
|
||||||
|
if not FileAccess.file_exists(path):
|
||||||
|
return {}
|
||||||
|
var config := ConfigFile.new()
|
||||||
|
if config.load(path) != OK:
|
||||||
|
return {}
|
||||||
|
var stored: Variant = config.get_value("sync", "files", {})
|
||||||
|
if stored is Dictionary:
|
||||||
|
return (stored as Dictionary).duplicate(true)
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
static func save_state(path: String, state: Dictionary) -> void:
|
||||||
|
var config := ConfigFile.new()
|
||||||
|
config.set_value("sync", "files", state)
|
||||||
|
config.save(path)
|
||||||
|
|
||||||
|
|
||||||
|
static func _ensure_local_dir() -> void:
|
||||||
|
if not DirAccess.dir_exists_absolute(LOCAL_DIR):
|
||||||
|
DirAccess.make_dir_recursive_absolute(LOCAL_DIR)
|
||||||
|
|
||||||
|
|
||||||
|
static func _local_path(name: String) -> String:
|
||||||
|
return LOCAL_DIR.path_join(name)
|
||||||
1
rinkplanner/src/core/sync/routine_sync.gd.uid
Normal file
1
rinkplanner/src/core/sync/routine_sync.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://dw607bf5pkqls
|
||||||
122
rinkplanner/src/core/sync/sync_plan.gd
Normal file
122
rinkplanner/src/core/sync/sync_plan.gd
Normal file
|
|
@ -0,0 +1,122 @@
|
||||||
|
class_name SyncPlan
|
||||||
|
extends RefCounted
|
||||||
|
|
||||||
|
## Pure comparison logic for the manual routine sync. No file or network
|
||||||
|
## access, so the rules can be tested in isolation.
|
||||||
|
##
|
||||||
|
## Input shapes (file name -> values):
|
||||||
|
## local: {"mtime": int, "size": int}
|
||||||
|
## remote: {"modify": int, "size": int}
|
||||||
|
## state: {"local_mtime": int, "remote_modify": int}
|
||||||
|
##
|
||||||
|
## Without a state entry there is no common ancestor, so a file that exists on
|
||||||
|
## both sides is treated as a conflict instead of guessing which one is newer.
|
||||||
|
|
||||||
|
enum Action {
|
||||||
|
UPLOAD,
|
||||||
|
DOWNLOAD,
|
||||||
|
CONFLICT,
|
||||||
|
SKIP,
|
||||||
|
}
|
||||||
|
|
||||||
|
const ACTION_NAMES := {
|
||||||
|
Action.UPLOAD: "upload",
|
||||||
|
Action.DOWNLOAD: "download",
|
||||||
|
Action.CONFLICT: "conflict",
|
||||||
|
Action.SKIP: "skip",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static func build(
|
||||||
|
local: Dictionary,
|
||||||
|
remote: Dictionary,
|
||||||
|
state: Dictionary
|
||||||
|
) -> Array[Dictionary]:
|
||||||
|
var names: Array[String] = []
|
||||||
|
for name: String in local.keys():
|
||||||
|
names.append(name)
|
||||||
|
for name: String in remote.keys():
|
||||||
|
if not names.has(name):
|
||||||
|
names.append(name)
|
||||||
|
|
||||||
|
var plan: Array[Dictionary] = []
|
||||||
|
for name: String in names:
|
||||||
|
var has_local := local.has(name)
|
||||||
|
var has_remote := remote.has(name)
|
||||||
|
var known := state.has(name)
|
||||||
|
|
||||||
|
if not has_remote:
|
||||||
|
plan.append(_entry(
|
||||||
|
name,
|
||||||
|
Action.SKIP if known else Action.UPLOAD,
|
||||||
|
"nur lokal" if known else "neu lokal"
|
||||||
|
))
|
||||||
|
continue
|
||||||
|
if not has_local:
|
||||||
|
plan.append(_entry(
|
||||||
|
name,
|
||||||
|
Action.SKIP if known else Action.DOWNLOAD,
|
||||||
|
"nur Server" if not known else "lokal entfernt"
|
||||||
|
))
|
||||||
|
continue
|
||||||
|
if not known:
|
||||||
|
plan.append(_entry(name, Action.CONFLICT, "beidseitig ohne Stand"))
|
||||||
|
continue
|
||||||
|
|
||||||
|
var local_changed := int(local[name].get("mtime", 0)) != int(state[name].get("local_mtime", 0))
|
||||||
|
var remote_changed := int(remote[name].get("modify", 0)) != int(state[name].get("remote_modify", 0))
|
||||||
|
|
||||||
|
if not local_changed and not remote_changed:
|
||||||
|
plan.append(_entry(name, Action.SKIP, "unverändert"))
|
||||||
|
elif local_changed and remote_changed:
|
||||||
|
plan.append(_entry(name, Action.CONFLICT, "beidseitig geändert"))
|
||||||
|
elif local_changed:
|
||||||
|
plan.append(_entry(name, Action.UPLOAD, "lokal geändert"))
|
||||||
|
else:
|
||||||
|
plan.append(_entry(name, Action.DOWNLOAD, "Server geändert"))
|
||||||
|
return plan
|
||||||
|
|
||||||
|
|
||||||
|
static func _entry(name: String, action: Action, reason: String) -> Dictionary:
|
||||||
|
return {"name": name, "action": action, "reason": reason}
|
||||||
|
|
||||||
|
|
||||||
|
static func action_name(action: Action) -> String:
|
||||||
|
return ACTION_NAMES.get(action, "unknown")
|
||||||
|
|
||||||
|
|
||||||
|
## "kuer.tres" -> "kuer (Server 2026-10-30 1050).tres"
|
||||||
|
static func conflict_file_name(file_name: String, stamp: String) -> String:
|
||||||
|
var base := file_name.get_basename()
|
||||||
|
return "%s (Server %s).tres" % [base, stamp]
|
||||||
|
|
||||||
|
|
||||||
|
static func timestamp_label(unix_time: int = -1) -> String:
|
||||||
|
var moment := unix_time
|
||||||
|
if moment < 0:
|
||||||
|
moment = int(Time.get_unix_time_from_system())
|
||||||
|
else:
|
||||||
|
var zone: Dictionary = Time.get_time_zone_from_system()
|
||||||
|
moment += int(zone.get("bias", 0)) * 60
|
||||||
|
var parts := Time.get_datetime_dict_from_unix_time(moment)
|
||||||
|
return "%04d-%02d-%02d %02d%02d" % [
|
||||||
|
int(parts["year"]),
|
||||||
|
int(parts["month"]),
|
||||||
|
int(parts["day"]),
|
||||||
|
int(parts["hour"]),
|
||||||
|
int(parts["minute"]),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
## Returns a name that is not yet used locally.
|
||||||
|
static func unique_name(file_name: String, used: Dictionary) -> String:
|
||||||
|
if not used.has(file_name):
|
||||||
|
return file_name
|
||||||
|
var base := file_name.get_basename()
|
||||||
|
var index := 2
|
||||||
|
while true:
|
||||||
|
var candidate := "%s %d.tres" % [base, index]
|
||||||
|
if not used.has(candidate):
|
||||||
|
return candidate
|
||||||
|
index += 1
|
||||||
|
return file_name
|
||||||
1
rinkplanner/src/core/sync/sync_plan.gd.uid
Normal file
1
rinkplanner/src/core/sync/sync_plan.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://bviq55ra47v3a
|
||||||
|
|
@ -17,6 +17,8 @@ enum FileAction {
|
||||||
SAVE,
|
SAVE,
|
||||||
SAVE_AS,
|
SAVE_AS,
|
||||||
EXPORT,
|
EXPORT,
|
||||||
|
SYNC_NOW,
|
||||||
|
SYNC_SETTINGS,
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_SPEED_MPS := 4.3
|
const DEFAULT_SPEED_MPS := 4.3
|
||||||
|
|
@ -42,6 +44,7 @@ const EXPORTS_DIR := "user://exports"
|
||||||
const EXPORT_SHEET_SCENE := preload("res://scenes/export/export_sheet.tscn")
|
const EXPORT_SHEET_SCENE := preload("res://scenes/export/export_sheet.tscn")
|
||||||
const EXPORT_IMAGE_SIZE := Vector2i(2560, 1600)
|
const EXPORT_IMAGE_SIZE := Vector2i(2560, 1600)
|
||||||
const AUTOSAVE_PATH := "user://autosave.tres"
|
const AUTOSAVE_PATH := "user://autosave.tres"
|
||||||
|
const SYNC_SETTINGS_PATH := "user://sync.cfg"
|
||||||
const AUTOSAVE_DELAY_SECONDS := 1.5
|
const AUTOSAVE_DELAY_SECONDS := 1.5
|
||||||
const MAX_HISTORY := 50
|
const MAX_HISTORY := 50
|
||||||
const COMPACT_LAYOUT_WIDTH := 1280.0
|
const COMPACT_LAYOUT_WIDTH := 1280.0
|
||||||
|
|
@ -104,6 +107,14 @@ const COMPACT_LAYOUT_WIDTH := 1280.0
|
||||||
@onready var calibration_dialog: ConfirmationDialog = %CalibrationDialog
|
@onready var calibration_dialog: ConfirmationDialog = %CalibrationDialog
|
||||||
@onready var calibration_distance_edit: LineEdit = %CalibrationDistanceEdit
|
@onready var calibration_distance_edit: LineEdit = %CalibrationDistanceEdit
|
||||||
@onready var calibration_time_edit: LineEdit = %CalibrationTimeEdit
|
@onready var calibration_time_edit: LineEdit = %CalibrationTimeEdit
|
||||||
|
@onready var sync_settings_dialog: ConfirmationDialog = %SyncSettingsDialog
|
||||||
|
@onready var sync_host_edit: LineEdit = %SyncHostEdit
|
||||||
|
@onready var sync_port_edit: LineEdit = %SyncPortEdit
|
||||||
|
@onready var sync_user_edit: LineEdit = %SyncUserEdit
|
||||||
|
@onready var sync_password_edit: LineEdit = %SyncPasswordEdit
|
||||||
|
@onready var sync_directory_edit: LineEdit = %SyncDirectoryEdit
|
||||||
|
@onready var sync_ftps_check: CheckBox = %SyncFtpsCheck
|
||||||
|
@onready var sync_verify_check: CheckBox = %SyncVerifyCheck
|
||||||
@onready var export_dialog: ConfirmationDialog = %ExportDialog
|
@onready var export_dialog: ConfirmationDialog = %ExportDialog
|
||||||
@onready var export_name_edit: LineEdit = %ExportNameEdit
|
@onready var export_name_edit: LineEdit = %ExportNameEdit
|
||||||
@onready var export_format_option: OptionButton = %ExportFormatOption
|
@onready var export_format_option: OptionButton = %ExportFormatOption
|
||||||
|
|
@ -137,6 +148,9 @@ var _playback_segment_id: String = ""
|
||||||
var _playback_position: Vector2 = Vector2.ZERO
|
var _playback_position: Vector2 = Vector2.ZERO
|
||||||
var _playback_heading: Vector2 = Vector2.RIGHT
|
var _playback_heading: Vector2 = Vector2.RIGHT
|
||||||
var _updating_timeline: bool = false
|
var _updating_timeline: bool = false
|
||||||
|
var _sync_thread: Thread
|
||||||
|
var _sync_job: RoutineSync
|
||||||
|
var _sync_running: bool = false
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
|
|
@ -196,6 +210,7 @@ func _ready() -> void:
|
||||||
reset_timing_button.pressed.connect(_reset_selected_timing)
|
reset_timing_button.pressed.connect(_reset_selected_timing)
|
||||||
calibration_dialog.confirmed.connect(_on_calibration_confirmed)
|
calibration_dialog.confirmed.connect(_on_calibration_confirmed)
|
||||||
export_dialog.confirmed.connect(_on_export_confirmed)
|
export_dialog.confirmed.connect(_on_export_confirmed)
|
||||||
|
sync_settings_dialog.confirmed.connect(_on_sync_settings_confirmed)
|
||||||
label_button.pressed.connect(_open_label_dialog)
|
label_button.pressed.connect(_open_label_dialog)
|
||||||
labels_toggle.toggled.connect(_on_labels_toggled)
|
labels_toggle.toggled.connect(_on_labels_toggled)
|
||||||
segment_label_dialog.confirmed.connect(_on_label_confirmed)
|
segment_label_dialog.confirmed.connect(_on_label_confirmed)
|
||||||
|
|
@ -1078,6 +1093,10 @@ func advance_playback(delta: float) -> void:
|
||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
advance_playback(delta)
|
advance_playback(delta)
|
||||||
|
_poll_sync_thread()
|
||||||
|
|
||||||
|
if _sync_running and _sync_job != null and not _sync_job.progress.is_empty():
|
||||||
|
status_label.text = "Sync: " + _sync_job.progress
|
||||||
|
|
||||||
|
|
||||||
func _setup_playback_controls() -> void:
|
func _setup_playback_controls() -> void:
|
||||||
|
|
@ -1436,6 +1455,9 @@ func _setup_file_menu() -> void:
|
||||||
popup.add_item("Speichern unter…", FileAction.SAVE_AS)
|
popup.add_item("Speichern unter…", FileAction.SAVE_AS)
|
||||||
popup.add_separator()
|
popup.add_separator()
|
||||||
popup.add_item("Exportieren…", FileAction.EXPORT)
|
popup.add_item("Exportieren…", FileAction.EXPORT)
|
||||||
|
popup.add_separator()
|
||||||
|
popup.add_item("Jetzt synchronisieren", FileAction.SYNC_NOW)
|
||||||
|
popup.add_item("Sync-Einstellungen…", FileAction.SYNC_SETTINGS)
|
||||||
popup.id_pressed.connect(_on_file_menu_pressed)
|
popup.id_pressed.connect(_on_file_menu_pressed)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1451,6 +1473,10 @@ func _on_file_menu_pressed(action: int) -> void:
|
||||||
_open_save_as_dialog()
|
_open_save_as_dialog()
|
||||||
FileAction.EXPORT:
|
FileAction.EXPORT:
|
||||||
_open_export_dialog()
|
_open_export_dialog()
|
||||||
|
FileAction.SYNC_NOW:
|
||||||
|
start_sync()
|
||||||
|
FileAction.SYNC_SETTINGS:
|
||||||
|
_open_sync_settings_dialog()
|
||||||
|
|
||||||
|
|
||||||
func _set_routine(routine: Routine, path: String) -> void:
|
func _set_routine(routine: Routine, path: String) -> void:
|
||||||
|
|
@ -1720,6 +1746,165 @@ func _apply_responsive_layout() -> void:
|
||||||
profile_option.custom_minimum_size.x = 120.0 if compact else 140.0
|
profile_option.custom_minimum_size.x = 120.0 if compact else 140.0
|
||||||
|
|
||||||
|
|
||||||
|
func load_sync_settings() -> Dictionary:
|
||||||
|
var settings := {
|
||||||
|
"host": "",
|
||||||
|
"port": 21,
|
||||||
|
"user": "",
|
||||||
|
"password": "",
|
||||||
|
"directory": "",
|
||||||
|
"ftps": true,
|
||||||
|
"verify_certificate": true,
|
||||||
|
}
|
||||||
|
var config := ConfigFile.new()
|
||||||
|
if config.load(SYNC_SETTINGS_PATH) != OK:
|
||||||
|
return settings
|
||||||
|
for key: String in settings.keys():
|
||||||
|
settings[key] = config.get_value("connection", key, settings[key])
|
||||||
|
return settings
|
||||||
|
|
||||||
|
|
||||||
|
func save_sync_settings(settings: Dictionary) -> void:
|
||||||
|
var config := ConfigFile.new()
|
||||||
|
for key: String in settings.keys():
|
||||||
|
config.set_value("connection", key, settings[key])
|
||||||
|
config.save(SYNC_SETTINGS_PATH)
|
||||||
|
|
||||||
|
|
||||||
|
func _open_sync_settings_dialog() -> void:
|
||||||
|
var settings := load_sync_settings()
|
||||||
|
sync_host_edit.text = str(settings.get("host", ""))
|
||||||
|
sync_port_edit.text = str(settings.get("port", 21))
|
||||||
|
sync_user_edit.text = str(settings.get("user", ""))
|
||||||
|
sync_password_edit.text = str(settings.get("password", ""))
|
||||||
|
sync_directory_edit.text = str(settings.get("directory", ""))
|
||||||
|
sync_ftps_check.button_pressed = bool(settings.get("ftps", true))
|
||||||
|
sync_verify_check.button_pressed = bool(settings.get("verify_certificate", true))
|
||||||
|
sync_settings_dialog.popup_centered(Vector2i(620, 620))
|
||||||
|
sync_host_edit.grab_focus()
|
||||||
|
|
||||||
|
|
||||||
|
func _on_sync_settings_confirmed() -> void:
|
||||||
|
var port := int(sync_port_edit.text.strip_edges())
|
||||||
|
if port <= 0 or port > 65535:
|
||||||
|
port = 21
|
||||||
|
save_sync_settings({
|
||||||
|
"host": sync_host_edit.text.strip_edges(),
|
||||||
|
"port": port,
|
||||||
|
"user": sync_user_edit.text.strip_edges(),
|
||||||
|
"password": sync_password_edit.text,
|
||||||
|
"directory": sync_directory_edit.text.strip_edges(),
|
||||||
|
"ftps": sync_ftps_check.button_pressed,
|
||||||
|
"verify_certificate": sync_verify_check.button_pressed,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
## Manual sync: push/pull every routine file, keep both versions on conflict.
|
||||||
|
func start_sync() -> void:
|
||||||
|
if _sync_running:
|
||||||
|
return
|
||||||
|
|
||||||
|
if _routine != null and is_dirty():
|
||||||
|
if _current_path.is_empty():
|
||||||
|
_open_save_as_dialog()
|
||||||
|
return
|
||||||
|
save_to_path(_current_path)
|
||||||
|
|
||||||
|
var settings := load_sync_settings()
|
||||||
|
if str(settings.get("host", "")).is_empty() or str(settings.get("user", "")).is_empty():
|
||||||
|
_open_sync_settings_dialog()
|
||||||
|
return
|
||||||
|
|
||||||
|
_sync_job = RoutineSync.new()
|
||||||
|
_sync_thread = Thread.new()
|
||||||
|
var thread_error := _sync_thread.start(_sync_job.run.bind(settings))
|
||||||
|
if thread_error != OK:
|
||||||
|
_sync_thread = null
|
||||||
|
_sync_job = null
|
||||||
|
_show_message("Sync konnte nicht gestartet werden (Fehler %d)." % thread_error)
|
||||||
|
return
|
||||||
|
|
||||||
|
_sync_running = true
|
||||||
|
file_menu_button.disabled = true
|
||||||
|
status_label.text = "Sync: Verbinde …"
|
||||||
|
|
||||||
|
|
||||||
|
func _poll_sync_thread() -> void:
|
||||||
|
if _sync_thread == null or _sync_thread.is_alive():
|
||||||
|
return
|
||||||
|
|
||||||
|
_sync_thread.wait_to_finish()
|
||||||
|
_sync_thread = null
|
||||||
|
_sync_running = false
|
||||||
|
file_menu_button.disabled = false
|
||||||
|
|
||||||
|
var job := _sync_job
|
||||||
|
_sync_job = null
|
||||||
|
if job == null:
|
||||||
|
_update_status()
|
||||||
|
return
|
||||||
|
|
||||||
|
if not job.error.is_empty():
|
||||||
|
_show_message("Sync fehlgeschlagen:\n\n" + job.error)
|
||||||
|
_update_status()
|
||||||
|
return
|
||||||
|
|
||||||
|
var summary := _format_sync_summary(job.result)
|
||||||
|
_reload_after_sync(job.result)
|
||||||
|
_update_status()
|
||||||
|
_show_message("Sync abgeschlossen\n\n" + summary)
|
||||||
|
|
||||||
|
|
||||||
|
func _format_sync_summary(result: Dictionary) -> String:
|
||||||
|
var uploaded: Array = result.get("uploaded", [])
|
||||||
|
var downloaded: Array = result.get("downloaded", [])
|
||||||
|
var conflicts: Array = result.get("conflicts", [])
|
||||||
|
var skipped: Array = result.get("skipped", [])
|
||||||
|
var errors: Array = result.get("errors", [])
|
||||||
|
|
||||||
|
var lines := PackedStringArray()
|
||||||
|
lines.append("Hochgeladen: %d" % uploaded.size())
|
||||||
|
if not uploaded.is_empty():
|
||||||
|
lines.append(" " + ", ".join(uploaded))
|
||||||
|
lines.append("Heruntergeladen: %d" % downloaded.size())
|
||||||
|
if not downloaded.is_empty():
|
||||||
|
lines.append(" " + ", ".join(downloaded))
|
||||||
|
lines.append("Konflikte (beide Fassungen behalten): %d" % conflicts.size())
|
||||||
|
if not conflicts.is_empty():
|
||||||
|
lines.append(" " + ", ".join(conflicts))
|
||||||
|
if not skipped.is_empty():
|
||||||
|
lines.append("Unverändert: %d" % skipped.size())
|
||||||
|
if not errors.is_empty():
|
||||||
|
lines.append("")
|
||||||
|
lines.append("Fehler:")
|
||||||
|
for message: String in errors:
|
||||||
|
lines.append(" " + message)
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
func _reload_after_sync(result: Dictionary) -> void:
|
||||||
|
if _current_path.is_empty():
|
||||||
|
return
|
||||||
|
var current_name := _current_path.get_file()
|
||||||
|
var downloaded: Array = result.get("downloaded", [])
|
||||||
|
if not downloaded.has(current_name):
|
||||||
|
return
|
||||||
|
|
||||||
|
if is_dirty():
|
||||||
|
_show_message(
|
||||||
|
"Die geöffnete Kür wurde vom Server aktualisiert, hat aber "
|
||||||
|
+ "ungespeicherte Änderungen. Bitte manuell speichern oder neu laden."
|
||||||
|
)
|
||||||
|
return
|
||||||
|
load_from_path(_current_path)
|
||||||
|
|
||||||
|
|
||||||
|
func _exit_tree() -> void:
|
||||||
|
if _sync_thread != null:
|
||||||
|
_sync_thread.wait_to_finish()
|
||||||
|
_sync_thread = null
|
||||||
|
|
||||||
|
|
||||||
func _open_export_dialog() -> void:
|
func _open_export_dialog() -> void:
|
||||||
if _routine == null:
|
if _routine == null:
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,9 @@ $GODOT --headless --path rinkplanner --script res://tests/run_touch_tests.gd
|
||||||
|
|
||||||
# Run canvas performance stress test
|
# Run canvas performance stress test
|
||||||
$GODOT --headless --path rinkplanner --script res://tests/run_perf_tests.gd
|
$GODOT --headless --path rinkplanner --script res://tests/run_perf_tests.gd
|
||||||
|
|
||||||
|
# Run FTP sync planning/parser/settings tests
|
||||||
|
$GODOT --headless --path rinkplanner --script res://tests/run_sync_tests.gd
|
||||||
```
|
```
|
||||||
|
|
||||||
Manueller visueller Smoke-Test (Layout bei einer Fenstergröße prüfen):
|
Manueller visueller Smoke-Test (Layout bei einer Fenstergröße prüfen):
|
||||||
|
|
|
||||||
230
rinkplanner/tests/run_sync_tests.gd
Normal file
230
rinkplanner/tests/run_sync_tests.gd
Normal file
|
|
@ -0,0 +1,230 @@
|
||||||
|
extends SceneTree
|
||||||
|
|
||||||
|
## Headless tests for the manual FTP sync: planning rules, conflict naming,
|
||||||
|
## state persistence, protocol parsing and the settings UI wiring.
|
||||||
|
##
|
||||||
|
## Run with:
|
||||||
|
## godot --headless --path rinkplanner --script res://tests/run_sync_tests.gd
|
||||||
|
|
||||||
|
const WhiteboardScene := preload("res://scenes/whiteboard/whiteboard.tscn")
|
||||||
|
|
||||||
|
var _assertions: int = 0
|
||||||
|
var _failures: PackedStringArray = PackedStringArray()
|
||||||
|
|
||||||
|
|
||||||
|
func _init() -> void:
|
||||||
|
_run.call_deferred()
|
||||||
|
|
||||||
|
|
||||||
|
func _run() -> void:
|
||||||
|
_test_plan()
|
||||||
|
_test_naming()
|
||||||
|
_test_parsers()
|
||||||
|
_test_state_roundtrip()
|
||||||
|
await _test_controller_wiring()
|
||||||
|
_finish()
|
||||||
|
|
||||||
|
|
||||||
|
func _test_plan() -> void:
|
||||||
|
var local := {
|
||||||
|
"neu.tres": {"mtime": 100, "size": 10},
|
||||||
|
"lokal.tres": {"mtime": 200, "size": 10},
|
||||||
|
"server.tres": {"mtime": 300, "size": 10},
|
||||||
|
"beide.tres": {"mtime": 400, "size": 10},
|
||||||
|
"gleich.tres": {"mtime": 500, "size": 10},
|
||||||
|
"entfernt.tres": {"mtime": 600, "size": 10},
|
||||||
|
}
|
||||||
|
var remote := {
|
||||||
|
"server-neu.tres": {"modify": 700, "size": 10},
|
||||||
|
"lokal.tres": {"modify": 200, "size": 10},
|
||||||
|
"server.tres": {"modify": 900, "size": 10},
|
||||||
|
"beide.tres": {"modify": 900, "size": 10},
|
||||||
|
"gleich.tres": {"modify": 500, "size": 10},
|
||||||
|
"entfernt.tres": {"modify": 600, "size": 10},
|
||||||
|
}
|
||||||
|
var state := {
|
||||||
|
"lokal.tres": {"local_mtime": 100, "remote_modify": 200},
|
||||||
|
"server.tres": {"local_mtime": 300, "remote_modify": 300},
|
||||||
|
"beide.tres": {"local_mtime": 401, "remote_modify": 400},
|
||||||
|
"gleich.tres": {"local_mtime": 500, "remote_modify": 500},
|
||||||
|
"entfernt.tres": {"local_mtime": 600, "remote_modify": 600},
|
||||||
|
}
|
||||||
|
var plan := SyncPlan.build(local, remote, state)
|
||||||
|
var actions := {}
|
||||||
|
for entry: Dictionary in plan:
|
||||||
|
actions[entry["name"]] = entry["action"]
|
||||||
|
|
||||||
|
_check(actions.get("neu.tres") == SyncPlan.Action.UPLOAD, "nur lokal wird hochgeladen")
|
||||||
|
_check(actions.get("server-neu.tres") == SyncPlan.Action.DOWNLOAD, "nur Server wird geladen")
|
||||||
|
_check(
|
||||||
|
actions.get("beide.tres") == SyncPlan.Action.CONFLICT,
|
||||||
|
"beidseitige Änderung ist ein Konflikt"
|
||||||
|
)
|
||||||
|
_check(actions.get("lokal.tres") == SyncPlan.Action.UPLOAD, "lokale Änderung wird hochgeladen")
|
||||||
|
_check(actions.get("server.tres") == SyncPlan.Action.DOWNLOAD, "Server-Änderung wird geladen")
|
||||||
|
_check(actions.get("gleich.tres") == SyncPlan.Action.SKIP, "unverändert wird übersprungen")
|
||||||
|
_check(
|
||||||
|
actions.get("entfernt.tres") == SyncPlan.Action.SKIP,
|
||||||
|
"Server-Datei verschwunden: nicht wiederherstellen"
|
||||||
|
)
|
||||||
|
|
||||||
|
var first_sync := SyncPlan.build(
|
||||||
|
{"a.tres": {"mtime": 1, "size": 1}},
|
||||||
|
{"a.tres": {"modify": 2, "size": 1}},
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
first_sync.size() == 1 and first_sync[0]["action"] == SyncPlan.Action.CONFLICT,
|
||||||
|
"erster Abgleich ohne Stand ist ein Konflikt"
|
||||||
|
)
|
||||||
|
|
||||||
|
var local_deleted := SyncPlan.build(
|
||||||
|
{},
|
||||||
|
{"weg.tres": {"modify": 2, "size": 1}},
|
||||||
|
{"weg.tres": {"local_mtime": 1, "remote_modify": 2}}
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
local_deleted.size() == 1 and local_deleted[0]["action"] == SyncPlan.Action.SKIP,
|
||||||
|
"lokal gelöschte Datei wird nicht zurückgeholt"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func _test_naming() -> void:
|
||||||
|
_check(
|
||||||
|
SyncPlan.conflict_file_name("kuer.tres", "2026-10-30 1050")
|
||||||
|
== "kuer (Server 2026-10-30 1050).tres",
|
||||||
|
"Konfliktname enthält Zeitstempel"
|
||||||
|
)
|
||||||
|
var used := {"kuer (Server 2026-10-30 1050).tres": true}
|
||||||
|
_check(
|
||||||
|
SyncPlan.unique_name("kuer (Server 2026-10-30 1050).tres", used)
|
||||||
|
== "kuer (Server 2026-10-30 1050) 2.tres",
|
||||||
|
"Konfliktname wird bei Bedarf durchnummeriert"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
SyncPlan.unique_name("frei.tres", used) == "frei.tres",
|
||||||
|
"freier Name bleibt unverändert"
|
||||||
|
)
|
||||||
|
var label := SyncPlan.timestamp_label(0)
|
||||||
|
_check(
|
||||||
|
label.length() == 15 and label.contains("-") and label.contains(" "),
|
||||||
|
"Zeitstempel hat das Format YYYY-MM-DD HHMM"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func _test_parsers() -> void:
|
||||||
|
var pasv := FtpClient.parse_pasv("227 Entering Passive Mode (10,1,2,3,200,10)")
|
||||||
|
_check(
|
||||||
|
pasv.get("host") == "10.1.2.3" and pasv.get("port") == 200 * 256 + 10,
|
||||||
|
"PASV-Antwort wird geparst"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
FtpClient.parse_epsv("229 Entering Extended Passive Mode (|||6446|)") == 6446,
|
||||||
|
"EPSV-Antwort wird geparst"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
FtpClient.parse_epsv("229 Entering Extended Passive Mode (|||)") == -1,
|
||||||
|
"ungültige EPSV-Antwort liefert -1"
|
||||||
|
)
|
||||||
|
var expected := int(Time.get_unix_time_from_datetime_string("2026-10-30T09:48:28"))
|
||||||
|
_check(
|
||||||
|
FtpClient.parse_mdtm("20261030094828") == expected,
|
||||||
|
"MDTM wird als Unix-Zeit gelesen"
|
||||||
|
)
|
||||||
|
_check(FtpClient.parse_mdtm("kaputt") == -1, "ungültiges MDTM liefert -1")
|
||||||
|
|
||||||
|
|
||||||
|
func _test_state_roundtrip() -> void:
|
||||||
|
var path := "user://test_sync_state.cfg"
|
||||||
|
var state := {
|
||||||
|
"kuer.tres": {"local_mtime": 111, "remote_modify": 222},
|
||||||
|
"zweite.tres": {"local_mtime": 333, "remote_modify": 444},
|
||||||
|
}
|
||||||
|
RoutineSync.save_state(path, state)
|
||||||
|
var loaded := RoutineSync.load_state(path)
|
||||||
|
_check(loaded.size() == 2, "Sync-Stand wird gespeichert und geladen")
|
||||||
|
_check(
|
||||||
|
int(loaded.get("kuer.tres", {}).get("remote_modify", 0)) == 222,
|
||||||
|
"Sync-Stand behält die Server-Zeit"
|
||||||
|
)
|
||||||
|
DirAccess.remove_absolute(ProjectSettings.globalize_path(path))
|
||||||
|
|
||||||
|
var files := RoutineSync.local_files("user://does_not_exist")
|
||||||
|
_check(files.is_empty(), "fehlendes Verzeichnis liefert keine Dateien")
|
||||||
|
|
||||||
|
|
||||||
|
func _test_controller_wiring() -> void:
|
||||||
|
var scene := WhiteboardScene.instantiate()
|
||||||
|
var controller := scene as WhiteboardController
|
||||||
|
controller.restore_autosave = false
|
||||||
|
controller.autosave_enabled = false
|
||||||
|
root.add_child(scene)
|
||||||
|
await process_frame
|
||||||
|
|
||||||
|
_check(controller != null, "sync scene has a controller")
|
||||||
|
if controller == null:
|
||||||
|
return
|
||||||
|
|
||||||
|
controller.save_sync_settings({
|
||||||
|
"host": "ftp.example.org",
|
||||||
|
"port": 2121,
|
||||||
|
"user": "konto",
|
||||||
|
"password": "geheim",
|
||||||
|
"directory": "rinkplanner",
|
||||||
|
"ftps": false,
|
||||||
|
"verify_certificate": false,
|
||||||
|
})
|
||||||
|
var loaded := controller.load_sync_settings()
|
||||||
|
_check(loaded["host"] == "ftp.example.org", "Sync-Einstellungen speichern den Host")
|
||||||
|
_check(int(loaded["port"]) == 2121, "Sync-Einstellungen speichern den Port")
|
||||||
|
_check(loaded["ftps"] == false, "Sync-Einstellungen speichern FTPS")
|
||||||
|
_check(
|
||||||
|
loaded["verify_certificate"] == false,
|
||||||
|
"Sync-Einstellungen speichern die Zertifikatsprüfung"
|
||||||
|
)
|
||||||
|
|
||||||
|
controller._open_sync_settings_dialog()
|
||||||
|
_check(
|
||||||
|
controller.sync_settings_dialog.visible,
|
||||||
|
"Einstellungsdialog öffnet sich"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
controller.sync_host_edit.text == "ftp.example.org",
|
||||||
|
"Dialog ist mit den gespeicherten Werten gefüllt"
|
||||||
|
)
|
||||||
|
_check(
|
||||||
|
controller.sync_password_edit.secret,
|
||||||
|
"Passwortfeld ist verdeckt"
|
||||||
|
)
|
||||||
|
controller.sync_settings_dialog.hide()
|
||||||
|
|
||||||
|
# Ohne Host öffnet "Jetzt synchronisieren" die Einstellungen statt zu starten.
|
||||||
|
controller.save_sync_settings({"host": "", "port": 21, "user": "", "password": ""})
|
||||||
|
controller.start_sync()
|
||||||
|
_check(
|
||||||
|
controller.sync_settings_dialog.visible,
|
||||||
|
"Sync ohne Zugangsdaten öffnet die Einstellungen"
|
||||||
|
)
|
||||||
|
_check(not controller._sync_running, "ohne Zugangsdaten startet kein Sync")
|
||||||
|
controller.sync_settings_dialog.hide()
|
||||||
|
|
||||||
|
scene.queue_free()
|
||||||
|
await process_frame
|
||||||
|
|
||||||
|
|
||||||
|
func _check(condition: bool, message: String) -> void:
|
||||||
|
_assertions += 1
|
||||||
|
if not condition:
|
||||||
|
_failures.append(message)
|
||||||
|
|
||||||
|
|
||||||
|
func _finish() -> void:
|
||||||
|
if _failures.is_empty():
|
||||||
|
print("PASS: %d assertions" % _assertions)
|
||||||
|
quit(0)
|
||||||
|
return
|
||||||
|
|
||||||
|
for failure: String in _failures:
|
||||||
|
printerr("FAIL: %s" % failure)
|
||||||
|
printerr("%d of %d assertions failed" % [_failures.size(), _assertions])
|
||||||
|
quit(1)
|
||||||
1
rinkplanner/tests/run_sync_tests.gd.uid
Normal file
1
rinkplanner/tests/run_sync_tests.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://6ugjulridrh0
|
||||||
|
|
@ -45,3 +45,6 @@ fi
|
||||||
|
|
||||||
"$GODOT" --headless --path "$PROJECT_DIR" \
|
"$GODOT" --headless --path "$PROJECT_DIR" \
|
||||||
--script res://tests/run_perf_tests.gd
|
--script res://tests/run_perf_tests.gd
|
||||||
|
|
||||||
|
"$GODOT" --headless --path "$PROJECT_DIR" \
|
||||||
|
--script res://tests/run_sync_tests.gd
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue