Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import json
|
|
| 5 |
import re
|
| 6 |
import subprocess
|
| 7 |
import UnityPy
|
|
|
|
| 8 |
from fastapi import FastAPI, UploadFile, File
|
| 9 |
from fastapi.responses import FileResponse
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -50,8 +51,6 @@ async def extract_dialogues(file: UploadFile = File(...), assembly_file: Optiona
|
|
| 50 |
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 51 |
for obj in env.objects:
|
| 52 |
if obj.type.name in ["TextAsset", "MonoBehaviour"]:
|
| 53 |
-
|
| 54 |
-
# 1. YENİLİK: ASLA ÇAKIŞMAYAN BENZERSİZ DOSYA İSMİ OLUŞTURUCU
|
| 55 |
asset_name = getattr(obj.assets_file, 'name', 'assets')
|
| 56 |
asset_name = "".join([c if c.isalnum() else "_" for c in asset_name])
|
| 57 |
obj_name = f"{asset_name}_{obj.type.name}_{obj.path_id}"
|
|
@@ -103,56 +102,47 @@ async def repack_dialogues(asset_file: UploadFile = File(...), mod_zip: UploadFi
|
|
| 103 |
|
| 104 |
env = UnityPy.load(asset_path)
|
| 105 |
|
| 106 |
-
# 2. YENİLİK: ZIP İÇİNDEKİ İSİMLERİ GÜVENLİ ŞEKİLDE EŞLEŞTİRME VE TEMİZLEME
|
| 107 |
filename_to_translations = {}
|
| 108 |
with zipfile.ZipFile(zip_path, 'r') as zipf:
|
| 109 |
for file_info in zipf.infolist():
|
| 110 |
filename = file_info.filename
|
| 111 |
-
|
| 112 |
-
# Gizli OS dosyalarını veya metadata dosyalarını (Mac __MACOSX vb.) yoksay
|
| 113 |
if "__MACOSX" in filename or filename.split("/")[-1].startswith("._"):
|
| 114 |
continue
|
| 115 |
-
|
| 116 |
if filename.endswith(".json"):
|
| 117 |
try:
|
| 118 |
content = zipf.read(filename)
|
| 119 |
-
# Sadece isim ve ID'yi ayırarak anahtar oluştur
|
| 120 |
filename_to_translations[filename.split("/")[-1]] = json.loads(content.decode('utf-8'))
|
| 121 |
-
except json.JSONDecodeError:
|
| 122 |
-
pass # Bozuk veya formatı kaymış JSON dosyalarını çökmeden atla
|
| 123 |
|
| 124 |
-
# Oyun dosyasını tara ve eşleşen isimleri yama
|
| 125 |
for obj in env.objects:
|
| 126 |
if obj.type.name in ["TextAsset", "MonoBehaviour"]:
|
| 127 |
asset_name = getattr(obj.assets_file, 'name', 'assets')
|
| 128 |
asset_name = "".join([c if c.isalnum() else "_" for c in asset_name])
|
| 129 |
obj_name = f"{asset_name}_{obj.type.name}_{obj.path_id}"
|
| 130 |
-
|
| 131 |
target_filename = f"{obj_name}_raw.json"
|
| 132 |
|
| 133 |
if target_filename in filename_to_translations:
|
| 134 |
translations = filename_to_translations[target_filename]
|
| 135 |
-
|
| 136 |
if obj.type.name == "MonoBehaviour":
|
| 137 |
raw_data = obj.get_raw_data()
|
| 138 |
patched_data = bytearray(raw_data)
|
| 139 |
-
|
| 140 |
for orig_str, trans_str in translations.items():
|
| 141 |
if orig_str == trans_str: continue
|
| 142 |
-
|
| 143 |
orig_bytes = orig_str.encode('utf-8')
|
| 144 |
orig_pad = (4 - (len(orig_bytes) % 4)) % 4
|
| 145 |
orig_block = len(orig_bytes).to_bytes(4, 'little') + orig_bytes + (b'\x00' * orig_pad)
|
| 146 |
-
|
| 147 |
trans_bytes = trans_str.encode('utf-8')
|
| 148 |
trans_pad = (4 - (len(trans_bytes) % 4)) % 4
|
| 149 |
trans_block = len(trans_bytes).to_bytes(4, 'little') + trans_bytes + (b'\x00' * trans_pad)
|
| 150 |
-
|
| 151 |
patched_data = patched_data.replace(orig_block, trans_block)
|
| 152 |
-
|
| 153 |
obj.set_raw_data(bytes(patched_data))
|
| 154 |
|
| 155 |
output_path = os.path.join(temp_dir, f"repacked_{asset_file.filename}")
|
| 156 |
-
with open(output_path, "wb") as f:
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
return FileResponse(output_path, media_type="application/octet-stream", filename=asset_file.filename)
|
|
|
|
| 5 |
import re
|
| 6 |
import subprocess
|
| 7 |
import UnityPy
|
| 8 |
+
from UnityPy.enums import CompressionFlags
|
| 9 |
from fastapi import FastAPI, UploadFile, File
|
| 10 |
from fastapi.responses import FileResponse
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 51 |
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 52 |
for obj in env.objects:
|
| 53 |
if obj.type.name in ["TextAsset", "MonoBehaviour"]:
|
|
|
|
|
|
|
| 54 |
asset_name = getattr(obj.assets_file, 'name', 'assets')
|
| 55 |
asset_name = "".join([c if c.isalnum() else "_" for c in asset_name])
|
| 56 |
obj_name = f"{asset_name}_{obj.type.name}_{obj.path_id}"
|
|
|
|
| 102 |
|
| 103 |
env = UnityPy.load(asset_path)
|
| 104 |
|
|
|
|
| 105 |
filename_to_translations = {}
|
| 106 |
with zipfile.ZipFile(zip_path, 'r') as zipf:
|
| 107 |
for file_info in zipf.infolist():
|
| 108 |
filename = file_info.filename
|
|
|
|
|
|
|
| 109 |
if "__MACOSX" in filename or filename.split("/")[-1].startswith("._"):
|
| 110 |
continue
|
|
|
|
| 111 |
if filename.endswith(".json"):
|
| 112 |
try:
|
| 113 |
content = zipf.read(filename)
|
|
|
|
| 114 |
filename_to_translations[filename.split("/")[-1]] = json.loads(content.decode('utf-8'))
|
| 115 |
+
except json.JSONDecodeError: pass
|
|
|
|
| 116 |
|
|
|
|
| 117 |
for obj in env.objects:
|
| 118 |
if obj.type.name in ["TextAsset", "MonoBehaviour"]:
|
| 119 |
asset_name = getattr(obj.assets_file, 'name', 'assets')
|
| 120 |
asset_name = "".join([c if c.isalnum() else "_" for c in asset_name])
|
| 121 |
obj_name = f"{asset_name}_{obj.type.name}_{obj.path_id}"
|
|
|
|
| 122 |
target_filename = f"{obj_name}_raw.json"
|
| 123 |
|
| 124 |
if target_filename in filename_to_translations:
|
| 125 |
translations = filename_to_translations[target_filename]
|
|
|
|
| 126 |
if obj.type.name == "MonoBehaviour":
|
| 127 |
raw_data = obj.get_raw_data()
|
| 128 |
patched_data = bytearray(raw_data)
|
|
|
|
| 129 |
for orig_str, trans_str in translations.items():
|
| 130 |
if orig_str == trans_str: continue
|
|
|
|
| 131 |
orig_bytes = orig_str.encode('utf-8')
|
| 132 |
orig_pad = (4 - (len(orig_bytes) % 4)) % 4
|
| 133 |
orig_block = len(orig_bytes).to_bytes(4, 'little') + orig_bytes + (b'\x00' * orig_pad)
|
|
|
|
| 134 |
trans_bytes = trans_str.encode('utf-8')
|
| 135 |
trans_pad = (4 - (len(trans_bytes) % 4)) % 4
|
| 136 |
trans_block = len(trans_bytes).to_bytes(4, 'little') + trans_bytes + (b'\x00' * trans_pad)
|
|
|
|
| 137 |
patched_data = patched_data.replace(orig_block, trans_block)
|
|
|
|
| 138 |
obj.set_raw_data(bytes(patched_data))
|
| 139 |
|
| 140 |
output_path = os.path.join(temp_dir, f"repacked_{asset_file.filename}")
|
| 141 |
+
with open(output_path, "wb") as f:
|
| 142 |
+
try:
|
| 143 |
+
# YENİLİK: Dosyayı ışık hızında LZ4 ile sıkıştırarak RAM dostu hale getir
|
| 144 |
+
f.write(env.file.save(packer=CompressionFlags.LZ4))
|
| 145 |
+
except Exception:
|
| 146 |
+
f.write(env.file.save())
|
| 147 |
+
|
| 148 |
return FileResponse(output_path, media_type="application/octet-stream", filename=asset_file.filename)
|