Cnass commited on
Commit
f9c7630
·
verified ·
1 Parent(s): c2895e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -23,7 +23,6 @@ app.add_middleware(
23
  allow_headers=["*"],
24
  )
25
 
26
- # Arka plandaki görevleri takip edeceğimiz hafıza sözlüğü
27
  jobs = {}
28
 
29
  def process_repack(job_id: str, asset_path: str, zip_path: str, original_filename: str):
@@ -48,16 +47,15 @@ def process_repack(job_id: str, asset_path: str, zip_path: str, original_filenam
48
 
49
  for obj in env.objects:
50
  processed += 1
51
- # Her 50 objede bir hızı ölç ve ETA (Kalan Süre) hesapla
52
  if processed % 50 == 0:
53
  elapsed = time.time() - start_time
54
  rate = processed / elapsed if elapsed > 0 else 1
55
  remaining_seconds = int((total_objects - processed) / rate)
56
- prog = 5 + int((processed / total_objects) * 45) # İlk aşama %5 ile %50 arası sürer
57
 
58
  jobs[job_id].update({
59
  "progress": prog,
60
- "message": f"Diyaloglar yamalanıyor... (Kalan Süre: ~{remaining_seconds} saniye)"
61
  })
62
 
63
  if obj.type.name in ["TextAsset", "MonoBehaviour"]:
@@ -82,24 +80,42 @@ def process_repack(job_id: str, asset_path: str, zip_path: str, original_filenam
82
  patched_data = patched_data.replace(orig_block, trans_block)
83
  obj.set_raw_data(bytes(patched_data))
84
 
85
- # En ağır kısma geçiliyor: Orijinal boyuta sıkıştırma (LZMA)
86
  jobs[job_id].update({
87
  "progress": 60,
88
- "message": "Orijinal boyuta (LZMA) sıkıştırılıyor. Sunucu hızına göre 1-3 dakika sürebilir, bağlantınız kopmayacak..."
89
  })
90
 
91
  output_path = os.path.join(tempfile.gettempdir(), f"repacked_{job_id}_{original_filename}")
92
- with open(output_path, "wb") as f:
 
 
 
 
 
93
  try:
94
- # 300 MB'ı 20 MB'a düşüren asıl kod:
95
- f.write(env.file.save(packer=CompressionFlags.LZMA))
96
- except Exception:
97
- f.write(env.file.save())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  jobs[job_id].update({
100
  "status": "completed",
101
  "progress": 100,
102
- "message": "İşlem %100 Tamamlandı! Dosya İndiriliyor...",
103
  "file_path": output_path,
104
  "filename": original_filename
105
  })
@@ -108,11 +124,10 @@ def process_repack(job_id: str, asset_path: str, zip_path: str, original_filenam
108
  jobs[job_id].update({"status": "error", "message": f"Kritik Hata: {str(e)}"})
109
 
110
 
111
- # --------- YENİ API UÇ NOKTALARI (ENDPOINTS) ---------
112
 
113
  @app.post("/generate_dll")
114
  async def generate_dll(binary_file: UploadFile = File(...), metadata_file: UploadFile = File(...)):
115
- # Bu kısmı dokunmadık, aynı duruyor
116
  temp_dir = tempfile.mkdtemp()
117
  binary_path = os.path.join(temp_dir, binary_file.filename)
118
  metadata_path = os.path.join(temp_dir, metadata_file.filename)
@@ -130,7 +145,6 @@ async def generate_dll(binary_file: UploadFile = File(...), metadata_file: Uploa
130
 
131
  @app.post("/extract")
132
  async def extract_dialogues(file: UploadFile = File(...), assembly_file: Optional[UploadFile] = File(None)):
133
- # Bu kısmı dokunmadık, aynı duruyor
134
  temp_dir = tempfile.mkdtemp()
135
  asset_path = os.path.join(temp_dir, file.filename)
136
  with open(asset_path, "wb") as f: f.write(await file.read())
@@ -169,7 +183,6 @@ async def extract_dialogues(file: UploadFile = File(...), assembly_file: Optiona
169
  except: pass
170
  return FileResponse(zip_filename, media_type="application/zip", filename=f"{file.filename}_dialogues.zip")
171
 
172
- # ARKA PLAN GÖREVİ BAŞLATICISI
173
  @app.post("/repack_async")
174
  async def repack_async(background_tasks: BackgroundTasks, asset_file: UploadFile = File(...), mod_zip: UploadFile = File(...)):
175
  job_id = str(uuid.uuid4())
@@ -177,27 +190,20 @@ async def repack_async(background_tasks: BackgroundTasks, asset_file: UploadFile
177
  asset_path = os.path.join(temp_dir, asset_file.filename)
178
  zip_path = os.path.join(temp_dir, mod_zip.filename)
179
 
180
- # Dosyaları hızlıca diske al ve isteği hemen geri döndür ki 60 saniye sorunu yaşanmasın
181
  with open(asset_path, "wb") as f: f.write(await asset_file.read())
182
  with open(zip_path, "wb") as f: f.write(await mod_zip.read())
183
 
184
- # Gerçek işi arka plandaki isimsiz bir hayalete devret
185
  background_tasks.add_task(process_repack, job_id, asset_path, zip_path, asset_file.filename)
186
  jobs[job_id] = {"status": "queued", "progress": 0, "message": "İşlem sıraya alındı, başlıyor..."}
187
-
188
  return {"job_id": job_id}
189
 
190
- # DURUM KONTROLCÜSÜ (Yüzde ve Süre Buradan Okunur)
191
  @app.get("/status/{job_id}")
192
  async def get_status(job_id: str):
193
- if job_id not in jobs:
194
- return JSONResponse(status_code=404, content={"error": "Görev bulunamadı"})
195
  return jobs[job_id]
196
 
197
- # İNDİRME UÇ NOKTASI
198
  @app.get("/download/{job_id}")
199
  async def download_file(job_id: str):
200
  job = jobs.get(job_id)
201
- if not job or job.get("status") != "completed":
202
- return JSONResponse(status_code=400, content={"error": "Dosya henüz hazır değil"})
203
  return FileResponse(job["file_path"], media_type="application/octet-stream", filename=job["filename"])
 
23
  allow_headers=["*"],
24
  )
25
 
 
26
  jobs = {}
27
 
28
  def process_repack(job_id: str, asset_path: str, zip_path: str, original_filename: str):
 
47
 
48
  for obj in env.objects:
49
  processed += 1
 
50
  if processed % 50 == 0:
51
  elapsed = time.time() - start_time
52
  rate = processed / elapsed if elapsed > 0 else 1
53
  remaining_seconds = int((total_objects - processed) / rate)
54
+ prog = 5 + int((processed / total_objects) * 45)
55
 
56
  jobs[job_id].update({
57
  "progress": prog,
58
+ "message": f"Diyaloglar yamalanıyor... (Kalan Süre: ~{remaining_seconds} sn)"
59
  })
60
 
61
  if obj.type.name in ["TextAsset", "MonoBehaviour"]:
 
80
  patched_data = patched_data.replace(orig_block, trans_block)
81
  obj.set_raw_data(bytes(patched_data))
82
 
 
83
  jobs[job_id].update({
84
  "progress": 60,
85
+ "message": "Sıkıştırma algoritmaları deneniyor... (Sunucu hızına göre 1-3 dakika sürebilir)"
86
  })
87
 
88
  output_path = os.path.join(tempfile.gettempdir(), f"repacked_{job_id}_{original_filename}")
89
+
90
+ compression_error = ""
91
+ try:
92
+ # 1. Deneme: Modern UnityPy String Formatı (LZMA)
93
+ packed_data = env.file.save(packer="lzma")
94
+ except Exception as e1:
95
  try:
96
+ # 2. Deneme: Daha hızlı olan LZ4 Formatı
97
+ packed_data = env.file.save(packer="lz4")
98
+ except Exception as e2:
99
+ try:
100
+ # 3. Deneme: Eski Sürüm UnityPy Enum Formatı
101
+ packed_data = env.file.save(packer=CompressionFlags.LZMA)
102
+ except Exception as e3:
103
+ # HİÇBİRİ İŞE YARAMAZSA HATAYI EKRANA YANSIT
104
+ compression_error = f"LZMA_Str: {str(e1)} | LZ4_Str: {str(e2)} | Enum: {str(e3)}"
105
+ packed_data = env.file.save()
106
+
107
+ with open(output_path, "wb") as f:
108
+ f.write(packed_data)
109
+
110
+ if compression_error:
111
+ final_msg = f"Dosya hazırlandı AMA Sıkıştırılamadı! Hata Logu: {compression_error}"
112
+ else:
113
+ final_msg = "İşlem %100 Tamamlandı ve Orijinal Boyuta Sıkıştırıldı! Dosya İndiriliyor..."
114
 
115
  jobs[job_id].update({
116
  "status": "completed",
117
  "progress": 100,
118
+ "message": final_msg,
119
  "file_path": output_path,
120
  "filename": original_filename
121
  })
 
124
  jobs[job_id].update({"status": "error", "message": f"Kritik Hata: {str(e)}"})
125
 
126
 
127
+ # --------- API UÇ NOKTALARI (ENDPOINTS) ---------
128
 
129
  @app.post("/generate_dll")
130
  async def generate_dll(binary_file: UploadFile = File(...), metadata_file: UploadFile = File(...)):
 
131
  temp_dir = tempfile.mkdtemp()
132
  binary_path = os.path.join(temp_dir, binary_file.filename)
133
  metadata_path = os.path.join(temp_dir, metadata_file.filename)
 
145
 
146
  @app.post("/extract")
147
  async def extract_dialogues(file: UploadFile = File(...), assembly_file: Optional[UploadFile] = File(None)):
 
148
  temp_dir = tempfile.mkdtemp()
149
  asset_path = os.path.join(temp_dir, file.filename)
150
  with open(asset_path, "wb") as f: f.write(await file.read())
 
183
  except: pass
184
  return FileResponse(zip_filename, media_type="application/zip", filename=f"{file.filename}_dialogues.zip")
185
 
 
186
  @app.post("/repack_async")
187
  async def repack_async(background_tasks: BackgroundTasks, asset_file: UploadFile = File(...), mod_zip: UploadFile = File(...)):
188
  job_id = str(uuid.uuid4())
 
190
  asset_path = os.path.join(temp_dir, asset_file.filename)
191
  zip_path = os.path.join(temp_dir, mod_zip.filename)
192
 
 
193
  with open(asset_path, "wb") as f: f.write(await asset_file.read())
194
  with open(zip_path, "wb") as f: f.write(await mod_zip.read())
195
 
 
196
  background_tasks.add_task(process_repack, job_id, asset_path, zip_path, asset_file.filename)
197
  jobs[job_id] = {"status": "queued", "progress": 0, "message": "İşlem sıraya alındı, başlıyor..."}
 
198
  return {"job_id": job_id}
199
 
 
200
  @app.get("/status/{job_id}")
201
  async def get_status(job_id: str):
202
+ if job_id not in jobs: return JSONResponse(status_code=404, content={"error": "Görev bulunamadı"})
 
203
  return jobs[job_id]
204
 
 
205
  @app.get("/download/{job_id}")
206
  async def download_file(job_id: str):
207
  job = jobs.get(job_id)
208
+ if not job or job.get("status") != "completed": return JSONResponse(status_code=400, content={"error": "Dosya hazır değil"})
 
209
  return FileResponse(job["file_path"], media_type="application/octet-stream", filename=job["filename"])