File size: 7,645 Bytes
6967bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6de14d3
 
 
 
 
 
6967bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f1858c
6967bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""On-disk state for uploaded projects.

Everything a running job needs lives in files, not memory, because the Space
sleeps and restarts: a job that has translated 40,000 strings must come back
after a restart with those 40,000 still done. Progress is flushed
periodically rather than per string, so a crash costs seconds of work, not
hours.
"""

from __future__ import annotations

import json
import shutil
import time
import uuid
import zipfile
from dataclasses import asdict
from pathlib import Path

from .. import config
from .extract import Slot, Unit, detect_data_root, extract

# Job lifecycle. `translating` is the only one a restart needs to resume.
STATUS_EXTRACTING = "extracting"
STATUS_READY = "ready"
STATUS_TRANSLATING = "translating"
STATUS_DONE = "done"
STATUS_FAILED = "failed"
STATUS_CANCELLED = "cancelled"


def root() -> Path:
    path = Path(config.PROJECT_DIR)
    path.mkdir(parents=True, exist_ok=True)
    return path


def project_dir(project_id: str) -> Path:
    return root() / project_id


def _meta_path(project_id: str) -> Path:
    return project_dir(project_id) / "meta.json"


def load_meta(project_id: str) -> dict | None:
    try:
        return json.loads(_meta_path(project_id).read_text(encoding="utf-8"))
    except (OSError, json.JSONDecodeError):
        return None


def save_meta(project_id: str, meta: dict) -> None:
    meta["updated_at"] = time.time()
    path = _meta_path(project_id)
    # Write-then-rename: a restart in the middle of a flush must not leave a
    # half-written meta.json that makes the project unreadable.
    temp = path.with_suffix(".json.tmp")
    temp.write_text(json.dumps(meta, ensure_ascii=False), encoding="utf-8")
    temp.replace(path)


def load_units(project_id: str) -> list[Unit]:
    raw = json.loads((project_dir(project_id) / "units.json").read_text(encoding="utf-8"))
    return [
        Unit(
            source=item["source"],
            slots=[
                # .get for `raw`: projects indexed before raw slots existed have
                # no such key, and a resumed job must still read its own units.
                Slot(file=s["file"], paths=s["paths"], raw=s.get("raw", False))
                for s in item["slots"]
            ],
        )
        for item in raw
    ]


def save_units(project_id: str, units: list[Unit]) -> None:
    (project_dir(project_id) / "units.json").write_text(
        json.dumps([asdict(u) for u in units], ensure_ascii=False), encoding="utf-8"
    )


def load_translations(project_id: str) -> dict[str, str]:
    path = project_dir(project_id) / "translations.json"
    try:
        return json.loads(path.read_text(encoding="utf-8"))
    except (OSError, json.JSONDecodeError):
        return {}


def save_translations(project_id: str, translations: dict[str, str]) -> None:
    path = project_dir(project_id) / "translations.json"
    temp = path.with_suffix(".json.tmp")
    temp.write_text(json.dumps(translations, ensure_ascii=False), encoding="utf-8")
    temp.replace(path)


def data_dir(project_id: str, meta: dict) -> Path:
    return project_dir(project_id) / "source" / meta["data_root"]


class UploadError(ValueError):
    """Raised for uploads that are not usable RPG Maker projects."""


def create(zip_bytes: bytes, name: str) -> dict:
    """Unpack an upload, find its data folder and index the dialogue."""
    if len(zip_bytes) > config.MAX_UPLOAD_MB * 1024 * 1024:
        raise UploadError(
            f"Upload is larger than {config.MAX_UPLOAD_MB} MB. Zip only the "
            "data folder (MZ) or www/data (MV), not the whole game."
        )

    project_id = uuid.uuid4().hex[:12]
    base = project_dir(project_id)
    source = base / "source"
    source.mkdir(parents=True, exist_ok=True)

    archive = base / "upload.zip"
    archive.write_bytes(zip_bytes)
    try:
        with zipfile.ZipFile(archive) as zf:
            for member in zf.infolist():
                # Reject absolute paths and ../ escapes rather than trusting
                # the archive to stay inside its own folder.
                target = (source / member.filename).resolve()
                if not str(target).startswith(str(source.resolve())):
                    raise UploadError(f"Unsafe path in zip: {member.filename}")
            zf.extractall(source)
    except zipfile.BadZipFile as exc:
        shutil.rmtree(base, ignore_errors=True)
        raise UploadError("Not a valid .zip file") from exc
    except UploadError:
        shutil.rmtree(base, ignore_errors=True)
        raise
    finally:
        archive.unlink(missing_ok=True)

    found = detect_data_root(source)
    if found is None:
        shutil.rmtree(base, ignore_errors=True)
        raise UploadError(
            "No RPG Maker data folder found in the zip. Expected a folder "
            "named 'data' (MZ) or 'www/data' (MV) containing System.json and "
            "Map*.json."
        )
    found_dir, engine = found

    units, broken = extract(found_dir)

    files: dict[str, dict] = {}
    for unit in units:
        for slot in unit.slots:
            entry = files.setdefault(slot.file, {"total": 0, "done": 0})
            entry["total"] += 1

    meta = {
        "id": project_id,
        "name": name,
        "engine": engine,
        "data_root": str(found_dir.relative_to(source)),
        "status": STATUS_READY,
        "created_at": time.time(),
        "updated_at": time.time(),
        "target_lang": None,
        "total_units": len(units),
        "total_slots": sum(f["total"] for f in files.values()),
        "translated_units": 0,
        "failed_units": 0,
        "unreadable_files": broken,
        "files": files,
        "error": None,
    }
    save_units(project_id, units)
    save_translations(project_id, {})
    save_meta(project_id, meta)
    return meta


def list_projects() -> list[dict]:
    metas = []
    for child in root().iterdir():
        if child.is_dir():
            meta = load_meta(child.name)
            if meta:
                metas.append(meta)
    return sorted(metas, key=lambda m: m.get("created_at", 0), reverse=True)


def delete(project_id: str) -> bool:
    target = project_dir(project_id)
    if not target.is_dir():
        return False
    shutil.rmtree(target, ignore_errors=True)
    return True


def purge_expired() -> list[str]:
    """Delete projects untouched for longer than the retention window."""
    cutoff = time.time() - config.PROJECT_RETENTION_HOURS * 3600
    removed = []
    for meta in list_projects():
        if meta.get("updated_at", 0) < cutoff:
            if delete(meta["id"]):
                removed.append(meta["id"])
    return removed


def build_output(project_id: str, meta: dict) -> Path:
    """Produce the translated data folder as a downloadable zip.

    Rebuilt from the untouched source every time so a re-download after more
    strings finished reflects the newer state, and so a failed run never
    leaves a half-written tree behind.
    """
    from . import inject

    base = project_dir(project_id)
    staging = base / "build"
    shutil.rmtree(staging, ignore_errors=True)
    shutil.copytree(base / "source", staging)

    staged_data = staging / meta["data_root"]
    inject.apply(staged_data, load_units(project_id), load_translations(project_id))

    output = base / "translated.zip"
    output.unlink(missing_ok=True)
    with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as zf:
        for path in sorted(staging.rglob("*")):
            if path.is_file():
                zf.write(path, path.relative_to(staging))
    shutil.rmtree(staging, ignore_errors=True)
    return output