File size: 3,397 Bytes
a311ec0
e82e859
b20c4c0
e82e859
b20c4c0
ab7a4c1
b20c4c0
 
 
e82e859
b20c4c0
022ec02
ee3bf14
e82e859
ae64262
e82e859
b20c4c0
 
 
497ffbc
 
b20c4c0
357a1f3
 
 
ab7a4c1
 
 
 
 
022ec02
357a1f3
3b73b4e
 
 
 
 
b20c4c0
357a1f3
b20c4c0
 
 
63008a0
574d828
e6be728
022ec02
700ec8b
a311ec0
b20c4c0
63008a0
 
022ec02
 
 
 
a311ec0
022ec02
b20c4c0
 
63008a0
b20c4c0
 
022ec02
 
b20c4c0
 
 
 
 
 
 
63008a0
b20c4c0
dfac2b3
6b86865
022ec02
574d828
4f17016
ab7a4c1
574d828
022ec02
 
 
 
b20c4c0
 
 
 
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
import os, shutil, subprocess, requests, zipfile, uuid
from fastapi import FastAPI, BackgroundTasks
from pydantic import BaseModel

# ==========================================
# PANEL CONFIG
# ==========================================
os.chdir("/tmp")
os.environ["HOME"] = "/tmp"
os.environ["UNITY_PATH"] = "/opt/unity/Editor/Unity"
os.environ["PYTHONUNBUFFERED"] = "1"
# Folder ini sekarang harus berisi Assets, Packages, ProjectSettings, DAN Library
BASE_PROJECT_PATH = "/app/UnityProject"

app = FastAPI()

class ConversionRequest(BaseModel):
    file_url: str
    filename: str
    z_id: str
    z_pw: str

def setup_license():
    target_dir = "/tmp/.local/share/unity3d/Unity"
    source_ulf = "/app/Unity_lic.ulf"
    if os.path.exists(source_ulf):
        os.makedirs(target_dir, exist_ok=True)
        target_path = os.path.join(target_dir, "Unity_lic.ulf")
        shutil.copy2(source_ulf, target_path)
        os.chmod(target_path, 0o777)
        print(f"✅ [LICENSE] Lisensi aktif.", flush=True)

@app.post("/convert")
async def api_convert(req: ConversionRequest, background_tasks: BackgroundTasks):
    background_tasks.add_task(process_pipeline, req)
    return {"status": "processing"}

def process_pipeline(req: ConversionRequest):
    setup_license()
    session_id = str(uuid.uuid4())[:8]
    SESSION_PATH = f"/tmp/Project_{session_id}"
    zip_tmp = f"/tmp/{session_id}.zepeto"
    extract_tmp = f"/tmp/extract_{session_id}"

    try:
        # 1. DOWNLOAD ASET BARU
        resp = requests.get(req.file_url, timeout=120)
        with open(zip_tmp, 'wb') as f: f.write(resp.content)
        os.makedirs(extract_tmp, exist_ok=True)
        with zipfile.ZipFile(zip_tmp, 'r') as z: z.extractall(extract_tmp)

        # 2. COPY TEMPLATE PROJECT (TERMASUK LIBRARY UNTUK CACHE)
        # Menghapus parameter ignore agar Library ikut tersalin
        if os.path.exists(SESSION_PATH): shutil.rmtree(SESSION_PATH)
        shutil.copytree(BASE_PROJECT_PATH, SESSION_PATH)
        
        # Pindahkan aset baru ke InputRaw
        target_in = os.path.join(SESSION_PATH, "Assets/InputRaw")
        os.makedirs(target_in, exist_ok=True)
        for item in os.listdir(extract_tmp):
            shutil.move(os.path.join(extract_tmp, item), os.path.join(target_in, item))

        # 3. JALANKAN BUILD (Jauh lebih cepat karena Library sudah ada)
        print(f"🚀 [CLI] Memulai Build Studio untuk {req.z_id}...", flush=True)
        subprocess.run([
            os.environ["UNITY_PATH"], 
            "-batchmode", "-nographics", "-quit", 
            "-projectPath", SESSION_PATH, 
            "-executeMethod", "TestBuilder.ManualConvert", 
            "-id", req.z_id, 
            "-password", req.z_pw,
            "-job-worker-count", "4",
            "-logFile", "/dev/stdout"
        ], capture_output=False, check=True)

        print(f"✅ [SUCCESS] Pipeline Selesai dengan Cache.", flush=True)

    except Exception as e:
        print(f"🔥 [ERROR] Pipeline Gagal: {str(e)}", flush=True)
    finally:
        # Kita hapus SESSION_PATH, tapi BASE_PROJECT_PATH/Library tetap aman di /app/
        if os.path.exists(SESSION_PATH): shutil.rmtree(SESSION_PATH)
        if os.path.exists(extract_tmp): shutil.rmtree(extract_tmp)
        if os.path.exists(zip_tmp): os.remove(zip_tmp)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)