Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,52 +3,63 @@ import bz2
|
|
| 3 |
|
| 4 |
def analyze_cps(file):
|
| 5 |
if file is None:
|
| 6 |
-
return "", ""
|
| 7 |
-
|
| 8 |
try:
|
| 9 |
with open(file.name, "rb") as f:
|
| 10 |
raw_data = f.read()
|
| 11 |
|
| 12 |
-
# HEX дамп
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
# Проверка
|
| 17 |
magic = raw_data[:4]
|
| 18 |
if magic != b'OPS1':
|
| 19 |
-
return
|
| 20 |
|
| 21 |
-
# Пропускаем заголовок (
|
| 22 |
compressed_data = raw_data[12:]
|
| 23 |
|
| 24 |
# Распаковка
|
| 25 |
try:
|
| 26 |
decompressed = bz2.decompress(compressed_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
text_output = decompressed.decode("utf-8", errors="replace")
|
| 28 |
except Exception as e:
|
| 29 |
-
text_output = f"[Ошибка
|
| 30 |
|
| 31 |
-
return
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"Ошибка чтения
|
| 35 |
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
-
gr.Markdown("## Распаковка .cps файла (The Powder Toy)")
|
| 38 |
|
| 39 |
file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
file_input.change(
|
| 44 |
fn=analyze_cps,
|
| 45 |
inputs=file_input,
|
| 46 |
-
outputs=[
|
| 47 |
)
|
| 48 |
|
| 49 |
file_input.clear(
|
| 50 |
-
fn=lambda: ("", ""),
|
| 51 |
-
outputs=[
|
| 52 |
)
|
| 53 |
|
| 54 |
-
demo.launch()
|
|
|
|
| 3 |
|
| 4 |
def analyze_cps(file):
|
| 5 |
if file is None:
|
| 6 |
+
return "", "", ""
|
| 7 |
+
|
| 8 |
try:
|
| 9 |
with open(file.name, "rb") as f:
|
| 10 |
raw_data = f.read()
|
| 11 |
|
| 12 |
+
# HEX дамп оригинального файла
|
| 13 |
+
hex_dump_original = ' '.join(f'{byte:02x}' for byte in raw_data[:256])
|
| 14 |
+
hex_original = f"Оригинал (первые 256 байт):\n{hex_dump_original}"
|
| 15 |
|
| 16 |
+
# Проверка сигнатуры
|
| 17 |
magic = raw_data[:4]
|
| 18 |
if magic != b'OPS1':
|
| 19 |
+
return hex_original, "[Ошибка] Неверная сигнатура файла.", ""
|
| 20 |
|
| 21 |
+
# Пропускаем заголовок (12 байт)
|
| 22 |
compressed_data = raw_data[12:]
|
| 23 |
|
| 24 |
# Распаковка
|
| 25 |
try:
|
| 26 |
decompressed = bz2.decompress(compressed_data)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return hex_original, f"[Ошибка bzip2]: {e}", ""
|
| 29 |
+
|
| 30 |
+
# HEX дамп распакованных данных
|
| 31 |
+
hex_decompressed = ' '.join(f'{byte:02x}' for byte in decompressed[:512])
|
| 32 |
+
hex_decomp_view = f"Распакованные данные (первые 512 байт):\n{hex_decompressed}"
|
| 33 |
+
|
| 34 |
+
# Попробуем декодировать как UTF-8
|
| 35 |
+
try:
|
| 36 |
text_output = decompressed.decode("utf-8", errors="replace")
|
| 37 |
except Exception as e:
|
| 38 |
+
text_output = f"[Ошибка декодирования]: {e}"
|
| 39 |
|
| 40 |
+
return hex_original, hex_decomp_view, text_output
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
+
return f"[Ошибка чтения файла]: {e}", "", ""
|
| 44 |
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown("## Распаковка и анализ .cps файла (The Powder Toy)")
|
| 47 |
|
| 48 |
file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
|
| 49 |
+
|
| 50 |
+
hex_original = gr.Textbox(label="HEX дамп оригинального файла", lines=5)
|
| 51 |
+
hex_decomp = gr.Textbox(label="HEX дамп распакованных данных", lines=10)
|
| 52 |
+
text_output = gr.Textbox(label="Текст / частично читаемые данные", lines=15)
|
| 53 |
|
| 54 |
file_input.change(
|
| 55 |
fn=analyze_cps,
|
| 56 |
inputs=file_input,
|
| 57 |
+
outputs=[hex_original, hex_decomp, text_output]
|
| 58 |
)
|
| 59 |
|
| 60 |
file_input.clear(
|
| 61 |
+
fn=lambda: ("", "", ""),
|
| 62 |
+
outputs=[hex_original, hex_decomp, text_output]
|
| 63 |
)
|
| 64 |
|
| 65 |
+
demo.launch()
|