Spaces:
webzepetoku
/
Sleeping

webzepetoku commited on
Commit
e6be728
·
verified ·
1 Parent(s): 5b60ac2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -45
app.py CHANGED
@@ -5,9 +5,12 @@ import requests
5
  import zipfile
6
  import uuid
7
  import boto3
 
8
  from fastapi import FastAPI, BackgroundTasks
 
9
  from pydantic import BaseModel
10
 
 
11
  os.chdir("/tmp")
12
  os.environ["HOME"] = "/tmp"
13
  os.environ["UNITY_PATH"] = "/opt/unity/Editor/Unity"
@@ -26,55 +29,63 @@ class ConversionRequest(BaseModel):
26
  file_url: str
27
  filename: str
28
 
29
- def make_zepeto_archive(source_dir, output_file):
30
- with zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED) as z:
31
- for root, _, files in os.walk(source_dir):
32
- for file in files:
33
- z.write(os.path.join(root, file), arcname=file)
34
-
35
- def process_pipeline(req: ConversionRequest):
36
- session_id = str(uuid.uuid4())[:8]
37
- SESSION_PATH = f"/tmp/Project_{session_id}"
38
- extract_tmp = f"/tmp/extract_{session_id}"
39
- zip_tmp = f"/tmp/{session_id}.zip"
40
- final_zepeto = f"/tmp/{req.filename}.zepeto"
41
 
42
  try:
43
- # 1. Prep
44
- r = requests.get(req.file_url)
45
- with open(zip_tmp, 'wb') as f: f.write(r.content)
46
- os.makedirs(extract_tmp, exist_ok=True)
47
- with zipfile.ZipFile(zip_tmp, 'r') as z: z.extractall(extract_tmp)
 
 
 
 
48
 
49
- # 2. Setup
50
- shutil.copytree(BASE_PROJECT_PATH, SESSION_PATH, ignore=shutil.ignore_patterns("Library", "Temp"))
51
- target_input = os.path.join(SESSION_PATH, "Assets", "InputRaw")
52
- os.makedirs(target_input, exist_ok=True)
53
- for item in os.listdir(extract_tmp):
54
- shutil.move(os.path.join(extract_tmp, item), os.path.join(target_input, item))
 
 
 
 
 
 
 
55
 
56
- # 3. Unity
57
- print(f"🚀 [UNITY] Running...", flush=True)
58
- subprocess.run([os.environ["UNITY_PATH"], "-batchmode", "-nographics", "-quit",
59
- "-projectPath", SESSION_PATH, "-executeMethod", "TestBuilder.ManualConvert",
60
- "-logFile", "/dev/stdout"], capture_output=False)
61
 
62
- # 4. Python Zipping
63
- bundle_out = os.path.join(SESSION_PATH, "Assets/BundleOutput")
64
- if os.path.exists(bundle_out) and os.listdir(bundle_out):
65
- make_zepeto_archive(bundle_out, final_zepeto)
66
- s3 = boto3.client('s3', endpoint_url=f"https://{R2_CONFIG['account_id']}.r2.cloudflarestorage.com",
67
- aws_access_key_id=R2_CONFIG['access_key'], aws_secret_access_key=R2_CONFIG['secret_key'])
68
- s3.upload_file(final_zepeto, R2_CONFIG['bucket_name'], f"packed/{req.filename}.zepeto")
69
- print(f"✅ [SUCCESS] Done.", flush=True)
70
- else:
71
- print("❌ [ERROR] No bundle found.", flush=True)
72
 
73
- finally:
74
- for p in [SESSION_PATH, extract_tmp, zip_tmp, final_zepeto]:
75
- if os.path.exists(p): shutil.rmtree(p) if os.path.isdir(p) else os.remove(p)
 
 
 
 
 
 
 
 
 
 
 
76
 
77
- @app.post("/convert")
78
- async def api_convert(req: ConversionRequest, background_tasks: BackgroundTasks):
79
- background_tasks.add_task(process_pipeline, req)
80
- return {"status": "processing"}
 
5
  import zipfile
6
  import uuid
7
  import boto3
8
+ import glob
9
  from fastapi import FastAPI, BackgroundTasks
10
+ from fastapi.responses import FileResponse, JSONResponse
11
  from pydantic import BaseModel
12
 
13
+ # --- CONFIG ---
14
  os.chdir("/tmp")
15
  os.environ["HOME"] = "/tmp"
16
  os.environ["UNITY_PATH"] = "/opt/unity/Editor/Unity"
 
29
  file_url: str
30
  filename: str
31
 
32
+ # --- ENDPOINT REQUEST LICENSE (.alf) ---
33
+ @app.get("/lisensi")
34
+ async def get_unity_alf():
35
+ """Menghasilkan file .alf untuk aktivasi manual Unity"""
36
+ alf_filename = "Unity_v2020.3.9f1.alf"
37
+ alf_path = f"/tmp/{alf_filename}"
38
+
39
+ # Hapus jika file lama masih ada
40
+ if os.path.exists(alf_path):
41
+ os.remove(alf_path)
 
 
42
 
43
  try:
44
+ print("🎫 [LICENSE] Menghasilkan file .alf...", flush=True)
45
+ # Perintah Unity untuk membuat Activation Request
46
+ subprocess.run([
47
+ os.environ["UNITY_PATH"],
48
+ "-batchmode",
49
+ "-nographics",
50
+ "-createManualActivationFile",
51
+ "-logfile", "/dev/stdout"
52
+ ], cwd="/tmp", timeout=60)
53
 
54
+ # Unity biasanya menghasilkan file dengan pola *.alf di folder saat ini (cwd)
55
+ found_alf = glob.glob("/tmp/*.alf")
56
+
57
+ if found_alf:
58
+ target_alf = found_alf[0]
59
+ print(f"✅ [LICENSE] File .alf ditemukan: {target_alf}", flush=True)
60
+ return FileResponse(
61
+ path=target_alf,
62
+ filename=os.path.basename(target_alf),
63
+ media_type='application/octet-stream'
64
+ )
65
+ else:
66
+ return JSONResponse(status_code=500, content={"error": "File .alf tidak berhasil dibuat oleh Unity."})
67
 
68
+ except Exception as e:
69
+ return JSONResponse(status_code=500, content={"error": str(e)})
 
 
 
70
 
71
+ # --- PIPELINE UTAMA ---
72
+ @app.get("/")
73
+ async def health_check():
74
+ return {"status": "ready", "engine": "Unity 2020.3.9f1 - Manual License Support"}
 
 
 
 
 
 
75
 
76
+ def setup_license():
77
+ # Mencari file .ulf di root aplikasi (hasil upload manual kamu nanti)
78
+ ulf_name = "Unity_lic.ulf" # Sesuaikan dengan nama file yang kamu upload ke repo
79
+ source_ulf = os.path.join("/app", ulf_name)
80
+ target_dir = "/tmp/.local/share/unity3d/Unity"
81
+ target_ulf = os.path.join(target_dir, "Unity_lic.ulf")
82
+
83
+ try:
84
+ if os.path.exists(source_ulf):
85
+ os.makedirs(target_dir, exist_ok=True)
86
+ shutil.copy2(source_ulf, target_ulf)
87
+ print(f"✅ [LICENSE] Lisensi .ulf terpasang dari {source_ulf}", flush=True)
88
+ except Exception as e:
89
+ print(f"❌ [LICENSE] Gagal pasang .ulf: {e}", flush=True)
90
 
91
+ # ... (sisanya sama seperti app.py sebelumnya: make_zepeto_archive, process_pipeline, dll) ...