Spaces:
webzepetoku
/
Sleeping

webzepetoku commited on
Commit
d5d3a37
Β·
verified Β·
1 Parent(s): 594b819

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -29
app.py CHANGED
@@ -20,9 +20,12 @@ os.chdir("/tmp")
20
  os.environ["HOME"] = "/tmp"
21
  os.environ["UNITY_PATH"] = "/opt/unity/Editor/Unity"
22
 
23
- # Source Project (Master)
24
  BASE_PROJECT_PATH = "/app/UnityProject"
25
 
 
 
 
26
  app = FastAPI()
27
  app.add_middleware(
28
  CORSMiddleware,
@@ -75,50 +78,57 @@ def upload_to_zepeto(file_path, z_id, z_pw):
75
  return (True, "Success") if res.status_code in [200, 201] else (False, f"HTTP {res.status_code}")
76
  except Exception as e: return False, f"Error: {str(e)}"
77
 
 
78
  @app.on_event("startup")
79
  async def startup_event():
80
- print("βœ… [SYSTEM] Server Ready. Menunggu Request...", flush=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- # --- 3. PIPELINE UTAMA ---
 
 
 
 
83
  def process_pipeline(req: ConversionRequest):
84
- # SESSION ID UNIK (PENTING BIAR GAK CRASH BEE)
85
  session_id = str(uuid.uuid4())[:8]
86
  SESSION_PATH = f"/tmp/Project_{session_id}"
87
 
88
- print(f"πŸ†• [SESSION] Membuat Environment Baru: {SESSION_PATH}", flush=True)
89
 
90
  download_path = f"/tmp/{req.filename}_{session_id}.zip"
91
  extract_tmp = f"/tmp/extract_{session_id}"
92
 
93
  try:
94
- # 1. FRESH COPY
95
  shutil.copytree(BASE_PROJECT_PATH, SESSION_PATH)
96
  silent_chmod(SESSION_PATH)
97
 
98
- # 2. OPERASI MANIFEST (HAPUS YANG BIKIN ERROR COMPILER)
99
  manifest_path = os.path.join(SESSION_PATH, "Packages", "manifest.json")
100
  if os.path.exists(manifest_path):
101
  with open(manifest_path, 'r') as f: data = json.load(f)
102
-
103
- # --- DAFTAR HITAM PAKET ---
104
- # 1. universal-config: Bikin error package resolution
105
- # 2. splines: Bikin error CS0117 (Versi tidak cocok)
106
- bad_pkgs = [
107
- "com.unity.render-pipelines.universal-config",
108
- "com.unity.splines"
109
- ]
110
-
111
  for pkg in bad_pkgs:
112
  if "dependencies" in data and pkg in data["dependencies"]:
113
  del data["dependencies"][pkg]
114
- print(f"πŸ—‘οΈ [MANIFEST] Membuang paket error: {pkg}", flush=True)
115
-
116
  with open(manifest_path, 'w') as f: json.dump(data, f, indent=2)
117
 
118
- # 3. HAPUS LOCK FILE (Wajib biar Unity gak download ulang paket yg kita hapus)
119
  subprocess.run(["find", SESSION_PATH, "-name", "packages-lock.json", "-delete"], check=False)
120
 
121
- # 4. DOWNLOAD USER ASSET
122
  print(f"πŸ“₯ [1/5] Downloading: {req.filename}", flush=True)
123
  r = requests.get(req.file_url, timeout=30)
124
  with open(download_path, 'wb') as f: f.write(r.content)
@@ -143,11 +153,7 @@ def process_pipeline(req: ConversionRequest):
143
  return
144
  prefab_name = os.path.basename(prefab_list[0])
145
 
146
- # 5. BERSIHKAN LIBRARY (PREVENTIF)
147
- if os.path.exists(os.path.join(SESSION_PATH, "Library")):
148
- shutil.rmtree(os.path.join(SESSION_PATH, "Library"))
149
-
150
- # 6. RUN UNITY
151
  print(f"βš™οΈ [2/5] Unity Processing: {prefab_name}...", flush=True)
152
  os.makedirs(os.path.join(SESSION_PATH, "Library"), exist_ok=True)
153
 
@@ -159,7 +165,8 @@ def process_pipeline(req: ConversionRequest):
159
  "-quit", "-logFile", "/dev/stdout",
160
  "-no-sprite-atlas-cache",
161
  "-job-worker-count", "2",
162
- "-accept-api-license"
 
163
  ]
164
 
165
  process = subprocess.Popen(
@@ -172,12 +179,11 @@ def process_pipeline(req: ConversionRequest):
172
  )
173
 
174
  start_time = time.time()
175
- timeout_seconds = 3600 # 1 Jam
176
 
177
  while True:
178
  output_line = process.stdout.readline()
179
  if output_line:
180
- # Filter log biar gak terlalu spam, tapi tetap informatif
181
  line = output_line.strip()
182
  if "attempt to write a readonly" not in line:
183
  print(f"[UNITY] {line}", flush=True)
@@ -216,4 +222,4 @@ async def api_convert(req: ConversionRequest, background_tasks: BackgroundTasks)
216
  return {"status": "success"}
217
 
218
  @app.get("/")
219
- async def root(): return {"status": "online", "mode": "NO SPLINES EDITION"}
 
20
  os.environ["HOME"] = "/tmp"
21
  os.environ["UNITY_PATH"] = "/opt/unity/Editor/Unity"
22
 
23
+ # Source Project
24
  BASE_PROJECT_PATH = "/app/UnityProject"
25
 
26
+ # Lokasi Tujuan Lisensi (Di /tmp biar bisa diedit Unity)
27
+ DEST_LICENSE_PATH = "/tmp/Unity_lic.ulf"
28
+
29
  app = FastAPI()
30
  app.add_middleware(
31
  CORSMiddleware,
 
78
  return (True, "Success") if res.status_code in [200, 201] else (False, f"HTTP {res.status_code}")
79
  except Exception as e: return False, f"Error: {str(e)}"
80
 
81
+ # --- 3. STARTUP (AUTO-DETECT LICENSE) ---
82
  @app.on_event("startup")
83
  async def startup_event():
84
+ print("πŸ”„ [SYSTEM] Starting...", flush=True)
85
+ try:
86
+ # Cari file .ulf di folder /app (Root Repository)
87
+ ulf_files = glob.glob("/app/*.ulf")
88
+
89
+ if ulf_files:
90
+ source_ulf = ulf_files[0]
91
+ print(f"πŸ“‚ [LICENSE] Ditemukan file lokal: {source_ulf}", flush=True)
92
+
93
+ # COPY ke /tmp (PENTING! Agar permission bisa 777)
94
+ shutil.copy(source_ulf, DEST_LICENSE_PATH)
95
+ os.chmod(DEST_LICENSE_PATH, 0o666)
96
+ print(f"βœ… [LICENSE] Siap digunakan di: {DEST_LICENSE_PATH}", flush=True)
97
+ else:
98
+ print("❌ [LICENSE] FATAL: Tidak ada file .ulf di root folder!", flush=True)
99
 
100
+ print("βœ… [SYSTEM] Server Ready.", flush=True)
101
+ except Exception as e:
102
+ print(f"❌ [SYSTEM] Boot Error: {e}", flush=True)
103
+
104
+ # --- 4. PIPELINE ---
105
  def process_pipeline(req: ConversionRequest):
 
106
  session_id = str(uuid.uuid4())[:8]
107
  SESSION_PATH = f"/tmp/Project_{session_id}"
108
 
109
+ print(f"πŸ†• [SESSION] Environment: {SESSION_PATH}", flush=True)
110
 
111
  download_path = f"/tmp/{req.filename}_{session_id}.zip"
112
  extract_tmp = f"/tmp/extract_{session_id}"
113
 
114
  try:
115
+ # 1. COPY PROJECT
116
  shutil.copytree(BASE_PROJECT_PATH, SESSION_PATH)
117
  silent_chmod(SESSION_PATH)
118
 
119
+ # 2. OPERASI MANIFEST
120
  manifest_path = os.path.join(SESSION_PATH, "Packages", "manifest.json")
121
  if os.path.exists(manifest_path):
122
  with open(manifest_path, 'r') as f: data = json.load(f)
123
+ bad_pkgs = ["com.unity.render-pipelines.universal-config", "com.unity.splines"]
 
 
 
 
 
 
 
 
124
  for pkg in bad_pkgs:
125
  if "dependencies" in data and pkg in data["dependencies"]:
126
  del data["dependencies"][pkg]
 
 
127
  with open(manifest_path, 'w') as f: json.dump(data, f, indent=2)
128
 
 
129
  subprocess.run(["find", SESSION_PATH, "-name", "packages-lock.json", "-delete"], check=False)
130
 
131
+ # 3. DOWNLOAD & EXTRACT
132
  print(f"πŸ“₯ [1/5] Downloading: {req.filename}", flush=True)
133
  r = requests.get(req.file_url, timeout=30)
134
  with open(download_path, 'wb') as f: f.write(r.content)
 
153
  return
154
  prefab_name = os.path.basename(prefab_list[0])
155
 
156
+ # 4. RUN UNITY
 
 
 
 
157
  print(f"βš™οΈ [2/5] Unity Processing: {prefab_name}...", flush=True)
158
  os.makedirs(os.path.join(SESSION_PATH, "Library"), exist_ok=True)
159
 
 
165
  "-quit", "-logFile", "/dev/stdout",
166
  "-no-sprite-atlas-cache",
167
  "-job-worker-count", "2",
168
+ "-accept-api-license",
169
+ "-manualLicenseFile", DEST_LICENSE_PATH # Arahkan ke file copy di /tmp
170
  ]
171
 
172
  process = subprocess.Popen(
 
179
  )
180
 
181
  start_time = time.time()
182
+ timeout_seconds = 3600
183
 
184
  while True:
185
  output_line = process.stdout.readline()
186
  if output_line:
 
187
  line = output_line.strip()
188
  if "attempt to write a readonly" not in line:
189
  print(f"[UNITY] {line}", flush=True)
 
222
  return {"status": "success"}
223
 
224
  @app.get("/")
225
+ async def root(): return {"status": "online", "mode": "LOCAL FILE LICENSE"}