Update app.py
Browse files
app.py
CHANGED
|
@@ -107,21 +107,23 @@ async def extract_dialogues(file: UploadFile = File(...), assembly_file: Optiona
|
|
| 107 |
zipf.writestr("islem_raporu.txt", "\n".join(log_content).encode('utf-8'))
|
| 108 |
|
| 109 |
return FileResponse(zip_filename, media_type="application/zip", filename=f"{file.filename}_dialogues.zip")
|
| 110 |
-
|
| 111 |
@app.post("/repack")
|
| 112 |
-
async def repack_dialogues(asset_file: UploadFile = File(...), mod_zip: UploadFile = File(...)
|
|
|
|
| 113 |
temp_dir = tempfile.mkdtemp()
|
| 114 |
asset_path = os.path.join(temp_dir, asset_file.filename)
|
| 115 |
zip_path = os.path.join(temp_dir, mod_zip.filename)
|
|
|
|
| 116 |
with open(asset_path, "wb") as f: f.write(await asset_file.read())
|
| 117 |
with open(zip_path, "wb") as f: f.write(await mod_zip.read())
|
| 118 |
|
|
|
|
| 119 |
env = UnityPy.load(asset_path)
|
| 120 |
|
| 121 |
with zipfile.ZipFile(zip_path, 'r') as zipf:
|
| 122 |
for file_info in zipf.infolist():
|
| 123 |
filename = file_info.filename
|
| 124 |
-
if filename == "islem_raporu.txt": continue
|
| 125 |
content = zipf.read(filename)
|
| 126 |
|
| 127 |
for obj in env.objects:
|
|
@@ -130,39 +132,26 @@ async def repack_dialogues(asset_file: UploadFile = File(...), mod_zip: UploadFi
|
|
| 130 |
if hasattr(obj.read(), "name") and obj.read().name: obj_name = obj.read().name
|
| 131 |
except: pass
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
data = obj.read()
|
| 136 |
-
data.text = content.decode('utf-8')
|
| 137 |
-
data.save()
|
| 138 |
-
except: pass
|
| 139 |
-
break
|
| 140 |
-
|
| 141 |
-
elif obj.type.name == "MonoBehaviour" and filename == f"{obj_name}_raw.json":
|
| 142 |
-
# HAM BELLEK YAMASI (BINARY PATCHING)
|
| 143 |
translations = json.loads(content.decode('utf-8'))
|
| 144 |
raw_data = obj.get_raw_data()
|
| 145 |
patched_data = bytearray(raw_data)
|
| 146 |
|
| 147 |
for orig_str, trans_str in translations.items():
|
| 148 |
if orig_str == trans_str: continue
|
| 149 |
-
|
| 150 |
-
# Orijinal metnin bayt bloğunu oluştur
|
| 151 |
orig_bytes = orig_str.encode('utf-8')
|
| 152 |
-
orig_pad = (4 - (len(orig_bytes) % 4)) % 4
|
| 153 |
-
orig_block = len(orig_bytes).to_bytes(4, 'little') + orig_bytes + (b'\x00' * orig_pad)
|
| 154 |
-
|
| 155 |
-
# Çevrilmiş metnin bayt bloğunu oluştur
|
| 156 |
trans_bytes = trans_str.encode('utf-8')
|
| 157 |
-
trans_pad = (4 - (len(trans_bytes) % 4)) % 4
|
| 158 |
-
trans_block = len(trans_bytes).to_bytes(4, 'little') + trans_bytes + (b'\x00' * trans_pad)
|
| 159 |
|
| 160 |
-
#
|
| 161 |
-
|
|
|
|
|
|
|
| 162 |
|
| 163 |
obj.set_raw_data(bytes(patched_data))
|
| 164 |
break
|
| 165 |
|
|
|
|
| 166 |
output_path = os.path.join(temp_dir, f"repacked_{asset_file.filename}")
|
| 167 |
with open(output_path, "wb") as f: f.write(env.file.save())
|
| 168 |
|
|
|
|
| 107 |
zipf.writestr("islem_raporu.txt", "\n".join(log_content).encode('utf-8'))
|
| 108 |
|
| 109 |
return FileResponse(zip_filename, media_type="application/zip", filename=f"{file.filename}_dialogues.zip")
|
| 110 |
+
# app.py'nin son kısmındaki repack fonksiyonunu bu şekilde güncelle:
|
| 111 |
@app.post("/repack")
|
| 112 |
+
async def repack_dialogues(asset_file: UploadFile = File(...), mod_zip: UploadFile = File(...)):
|
| 113 |
+
# Geçici dosyaları tanımla
|
| 114 |
temp_dir = tempfile.mkdtemp()
|
| 115 |
asset_path = os.path.join(temp_dir, asset_file.filename)
|
| 116 |
zip_path = os.path.join(temp_dir, mod_zip.filename)
|
| 117 |
+
|
| 118 |
with open(asset_path, "wb") as f: f.write(await asset_file.read())
|
| 119 |
with open(zip_path, "wb") as f: f.write(await mod_zip.read())
|
| 120 |
|
| 121 |
+
# Paketi yükle
|
| 122 |
env = UnityPy.load(asset_path)
|
| 123 |
|
| 124 |
with zipfile.ZipFile(zip_path, 'r') as zipf:
|
| 125 |
for file_info in zipf.infolist():
|
| 126 |
filename = file_info.filename
|
|
|
|
| 127 |
content = zipf.read(filename)
|
| 128 |
|
| 129 |
for obj in env.objects:
|
|
|
|
| 132 |
if hasattr(obj.read(), "name") and obj.read().name: obj_name = obj.read().name
|
| 133 |
except: pass
|
| 134 |
|
| 135 |
+
# Sadece _raw.json olanları işle (Binary Patching)
|
| 136 |
+
if obj.type.name == "MonoBehaviour" and filename == f"{obj_name}_raw.json":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
translations = json.loads(content.decode('utf-8'))
|
| 138 |
raw_data = obj.get_raw_data()
|
| 139 |
patched_data = bytearray(raw_data)
|
| 140 |
|
| 141 |
for orig_str, trans_str in translations.items():
|
| 142 |
if orig_str == trans_str: continue
|
|
|
|
|
|
|
| 143 |
orig_bytes = orig_str.encode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
trans_bytes = trans_str.encode('utf-8')
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
# Basit bayt değiştirme (Kapsayıcıyı bozmadan yama)
|
| 147 |
+
# Eğer çeviri daha uzunsa dosyayı bozabilir, bu yüzden sadece aynı uzunlukta ise değiştir
|
| 148 |
+
if len(trans_bytes) <= len(orig_bytes):
|
| 149 |
+
patched_data = patched_data.replace(orig_bytes, trans_bytes.ljust(len(orig_bytes), b'\x00'))
|
| 150 |
|
| 151 |
obj.set_raw_data(bytes(patched_data))
|
| 152 |
break
|
| 153 |
|
| 154 |
+
# Paketle ve kaydet (Daha hızlı yazma için optimize edilmiş)
|
| 155 |
output_path = os.path.join(temp_dir, f"repacked_{asset_file.filename}")
|
| 156 |
with open(output_path, "wb") as f: f.write(env.file.save())
|
| 157 |
|