Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
import tempfile
|
| 4 |
+
import UnityPy
|
| 5 |
+
from fastapi import FastAPI, UploadFile, File
|
| 6 |
+
from fastapi.responses import FileResponse
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from typing import Optional
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
@app.post("/extract")
|
| 21 |
+
async def extract_dialogues(
|
| 22 |
+
file: UploadFile = File(...),
|
| 23 |
+
assembly_file: Optional[UploadFile] = File(None)
|
| 24 |
+
):
|
| 25 |
+
temp_dir = tempfile.mkdtemp()
|
| 26 |
+
asset_path = os.path.join(temp_dir, file.filename)
|
| 27 |
+
|
| 28 |
+
# Ana assets dosyasını kaydet
|
| 29 |
+
with open(asset_path, "wb") as f:
|
| 30 |
+
f.write(await file.read())
|
| 31 |
+
|
| 32 |
+
# Eğer DLL dosyası gönderilmişse, onu da aynı klasöre Managed alt yapısı gibi kaydet
|
| 33 |
+
# UnityPy DLL'leri okuyarak MonoBehaviour yapısını otomatik çözer
|
| 34 |
+
load_paths = [asset_path]
|
| 35 |
+
if assembly_file:
|
| 36 |
+
assembly_path = os.path.join(temp_dir, assembly_file.filename)
|
| 37 |
+
with open(assembly_path, "wb") as f:
|
| 38 |
+
f.write(await assembly_file.read())
|
| 39 |
+
load_paths.append(assembly_path)
|
| 40 |
+
|
| 41 |
+
# Tüm sağlanan dosyaları ortama yükle
|
| 42 |
+
env = UnityPy.load(*load_paths)
|
| 43 |
+
zip_filename = os.path.join(temp_dir, f"{file.filename}_dialogues.zip")
|
| 44 |
+
|
| 45 |
+
extracted_count = 0
|
| 46 |
+
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 47 |
+
for obj in env.objects:
|
| 48 |
+
if obj.type.name in ["TextAsset", "MonoBehaviour"]:
|
| 49 |
+
try:
|
| 50 |
+
data = obj.read()
|
| 51 |
+
file_name = getattr(data, "name", f"{obj.type.name}_{obj.path_id}")
|
| 52 |
+
|
| 53 |
+
if obj.type.name == "TextAsset":
|
| 54 |
+
content = data.text.encode('utf-8')
|
| 55 |
+
zipf.writestr(f"{file_name}.txt", content)
|
| 56 |
+
extracted_count += 1
|
| 57 |
+
|
| 58 |
+
elif obj.type.name == "MonoBehaviour":
|
| 59 |
+
# TypeTree artık DLL yüklendiği için çok daha isabetli okunacak
|
| 60 |
+
if obj.serialized_type and obj.serialized_type.nodes:
|
| 61 |
+
tree = obj.read_typetree()
|
| 62 |
+
import json
|
| 63 |
+
zipf.writestr(f"{file_name}.json", json.dumps(tree, indent=4).encode('utf-8'))
|
| 64 |
+
extracted_count += 1
|
| 65 |
+
except Exception as e:
|
| 66 |
+
# Bazı objeler okunamayabilir, sistemi çökertmemesi için atlıyoruz
|
| 67 |
+
pass
|
| 68 |
+
|
| 69 |
+
return FileResponse(zip_filename, media_type="application/zip", filename=f"{file.filename}_dialogues.zip")
|
| 70 |
+
|
| 71 |
+
@app.post("/repack")
|
| 72 |
+
async def repack_dialogues(
|
| 73 |
+
asset_file: UploadFile = File(...),
|
| 74 |
+
mod_zip: UploadFile = File(...),
|
| 75 |
+
assembly_file: Optional[UploadFile] = File(None)
|
| 76 |
+
):
|
| 77 |
+
temp_dir = tempfile.mkdtemp()
|
| 78 |
+
asset_path = os.path.join(temp_dir, asset_file.filename)
|
| 79 |
+
zip_path = os.path.join(temp_dir, mod_zip.filename)
|
| 80 |
+
|
| 81 |
+
with open(asset_path, "wb") as f: f.write(await asset_file.read())
|
| 82 |
+
with open(zip_path, "wb") as f: f.write(await mod_zip.read())
|
| 83 |
+
|
| 84 |
+
load_paths = [asset_path]
|
| 85 |
+
if assembly_file:
|
| 86 |
+
assembly_path = os.path.join(temp_dir, assembly_file.filename)
|
| 87 |
+
with open(assembly_path, "wb") as f: f.write(await assembly_file.read())
|
| 88 |
+
load_paths.append(assembly_path)
|
| 89 |
+
|
| 90 |
+
env = UnityPy.load(*load_paths)
|
| 91 |
+
|
| 92 |
+
with zipfile.ZipFile(zip_path, 'r') as zipf:
|
| 93 |
+
for file_info in zipf.infolist():
|
| 94 |
+
filename = file_info.filename
|
| 95 |
+
content = zipf.read(filename)
|
| 96 |
+
|
| 97 |
+
for obj in env.objects:
|
| 98 |
+
try:
|
| 99 |
+
data = obj.read()
|
| 100 |
+
obj_name = getattr(data, "name", f"{obj.type.name}_{obj.path_id}")
|
| 101 |
+
|
| 102 |
+
if obj.type.name == "TextAsset" and filename == f"{obj_name}.txt":
|
| 103 |
+
data.text = content.decode('utf-8')
|
| 104 |
+
data.save()
|
| 105 |
+
break
|
| 106 |
+
elif obj.type.name == "MonoBehaviour" and filename == f"{obj_name}.json":
|
| 107 |
+
import json
|
| 108 |
+
tree = json.loads(content.decode('utf-8'))
|
| 109 |
+
obj.save_typetree(tree)
|
| 110 |
+
break
|
| 111 |
+
except:
|
| 112 |
+
pass
|
| 113 |
+
|
| 114 |
+
output_path = os.path.join(temp_dir, f"repacked_{asset_file.filename}")
|
| 115 |
+
with open(output_path, "wb") as f:
|
| 116 |
+
f.write(env.file.save())
|
| 117 |
+
|
| 118 |
+
return FileResponse(output_path, media_type="application/octet-stream", filename=asset_file.filename)
|