SnowFlash383935 commited on
Commit
714e6fd
·
verified ·
1 Parent(s): 386d54c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -28
app.py CHANGED
@@ -1,54 +1,63 @@
1
  import gradio as gr
2
  import bz2
3
- from bson import BSON
4
 
5
- def parse_cps_bson(file):
6
- if not file:
7
- return "", "", ""
8
 
9
  try:
10
  with open(file.name, "rb") as f:
11
- raw = f.read()
12
 
13
- if raw[:4] != b'OPS1':
14
- return "Ошибка: неверная сигнатура файла", "", ""
 
15
 
16
- compressed = raw[12:]
17
- decompressed = bz2.decompress(compressed)
 
18
 
19
- # Парсим BSON
20
- bson_data = BSON(decompressed)
21
- decoded = bson_data.decode()
 
 
 
22
 
23
- # Выводим красиво
24
- import json
25
- json_str = json.dumps(decoded, indent=2, ensure_ascii=False)
26
 
27
- # HEX дамп
28
- hex_dump = ' '.join(f'{b:02x}' for b in decompressed[:512])
29
- hex_view = f"Распакованные данные (первые 512 байт):\n{hex_dump}"
 
 
 
30
 
31
- return hex_view, json_str, ""
32
 
33
  except Exception as e:
34
- return "", f"[Ошибка]: {e}", ""
35
 
36
  with gr.Blocks() as demo:
37
- gr.Markdown("## Распаковка и парсинг .cps (BSON)")
38
-
39
  file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
40
- hex_output = gr.Textbox(label="HEX дамп распакованных данных", lines=8)
41
- json_output = gr.Textbox(label="Структура BSON (JSON)", lines=20)
 
 
42
 
43
  file_input.change(
44
- fn=parse_cps_bson,
45
  inputs=file_input,
46
- outputs=[hex_output, json_output]
47
  )
48
 
49
  file_input.clear(
50
- fn=lambda: ("", ""),
51
- outputs=[hex_output, json_output]
52
  )
53
 
54
  demo.launch()
 
1
  import gradio as gr
2
  import bz2
3
+ import bson
4
 
5
+ def analyze_cps(file):
6
+ if file is None:
7
+ return "", "", "", ""
8
 
9
  try:
10
  with open(file.name, "rb") as f:
11
+ raw_data = f.read()
12
 
13
+ # HEX дамп оригинала
14
+ hex_orig = ' '.join(f'{b:02x}' for b in raw_data[:256])
15
+ hex_original = f"Оригинал (первые 256 байт):\n{hex_orig}"
16
 
17
+ # Проверка сигнатуры
18
+ if raw_data[:4] != b'OPS1':
19
+ return hex_original, "[Ошибка] Неверная сигнатура файла.", "", ""
20
 
21
+ # Пропустить заголовок (12 байт)
22
+ compressed = raw_data[12:]
23
+ try:
24
+ decompressed = bz2.decompress(compressed)
25
+ except Exception as e:
26
+ return hex_original, f"[Ошибка bzip2]: {e}", "", ""
27
 
28
+ # HEX дамп распакованных данных
29
+ hex_decomp = ' '.join(f'{b:02x}' for b in decompressed[:512])
30
+ hex_decomp_view = f"Распакованные данные (первые 512 байт):\n{hex_decomp}"
31
 
32
+ # Парсим BSON
33
+ try:
34
+ bson_doc = bson.decode(decompressed)
35
+ json_like = str(bson_doc)
36
+ except Exception as e:
37
+ json_like = f"[Ошибка BSON]: {e}"
38
 
39
+ return hex_original, hex_decomp_view, json_like
40
 
41
  except Exception as e:
42
+ return f"[Ошибка]: {e}", "", "", ""
43
 
44
  with gr.Blocks() as demo:
45
+ gr.Markdown("## Распаковка и анализ .cps (The Powder Toy Save)")
 
46
  file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
47
+
48
+ hex_orig_out = gr.Textbox(label="HEX оригинала", lines=5)
49
+ hex_decomp_out = gr.Textbox(label="HEX распакованных данных", lines=10)
50
+ bson_parsed = gr.Textbox(label="Распарсенный BSON (Python dict)", lines=20)
51
 
52
  file_input.change(
53
+ fn=analyze_cps,
54
  inputs=file_input,
55
+ outputs=[hex_orig_out, hex_decomp_out, bson_parsed]
56
  )
57
 
58
  file_input.clear(
59
+ fn=lambda: ("", "", ""),
60
+ outputs=[hex_orig_out, hex_decomp_out, bson_parsed]
61
  )
62
 
63
  demo.launch()