SnowFlash383935 commited on
Commit
85229e0
·
verified ·
1 Parent(s): 714e6fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -1,6 +1,26 @@
1
  import gradio as gr
2
  import bz2
3
  import bson
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def analyze_cps(file):
6
  if file is None:
@@ -10,44 +30,47 @@ def analyze_cps(file):
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,
 
1
  import gradio as gr
2
  import bz2
3
  import bson
4
+ import struct
5
+
6
+ PARTICLE_FORMAT = "<iii ffff f ii iiiii I" # 14 полей = 52 байта
7
+ PARTICLE_SIZE = 128 # размер записи в parts
8
+
9
+ FIELDS = [
10
+ "type", "life", "ctype",
11
+ "x", "y", "vx", "vy", "temp",
12
+ "tmp3", "tmp4", "flags", "tmp", "tmp2", "dcolour"
13
+ ]
14
+
15
+ def parse_parts(binary_data):
16
+ count = len(binary_data) // PARTICLE_SIZE
17
+ particles = []
18
+ for i in range(count):
19
+ chunk = binary_data[i * PARTICLE_SIZE : i * PARTICLE_SIZE + PARTICLE_SIZE]
20
+ values = struct.unpack(PARTICLE_FORMAT, chunk[:52])
21
+ particle = dict(zip(FIELDS, values))
22
+ particles.append(particle)
23
+ return particles
24
 
25
  def analyze_cps(file):
26
  if file is None:
 
30
  with open(file.name, "rb") as f:
31
  raw_data = f.read()
32
 
 
33
  hex_orig = ' '.join(f'{b:02x}' for b in raw_data[:256])
34
  hex_original = f"Оригинал (первые 256 байт):\n{hex_orig}"
35
 
 
36
  if raw_data[:4] != b'OPS1':
37
  return hex_original, "[Ошибка] Неверная сигнатура файла.", "", ""
38
 
 
39
  compressed = raw_data[12:]
40
  try:
41
  decompressed = bz2.decompress(compressed)
42
  except Exception as e:
43
  return hex_original, f"[Ошибка bzip2]: {e}", "", ""
44
 
 
45
  hex_decomp = ' '.join(f'{b:02x}' for b in decompressed[:512])
46
  hex_decomp_view = f"Распакованные данные (первые 512 байт):\n{hex_decomp}"
47
 
 
48
  try:
49
+ doc = bson.decode(decompressed)
 
50
  except Exception as e:
51
+ return hex_original, hex_decomp_view, f"[Ошибка BSON]: {e}", ""
52
+
53
+ # Парсим parts
54
+ if 'parts' in doc:
55
+ try:
56
+ binary_data = doc['parts']
57
+ parsed_parts = parse_parts(bytes(binary_data))
58
+ doc['parts'] = parsed_parts
59
+ except Exception as e:
60
+ doc['parts'] = f"[Ошибка парсинга parts]: {e}"
61
 
62
+ return hex_original, hex_decomp_view, str(doc)
63
 
64
  except Exception as e:
65
  return f"[Ошибка]: {e}", "", "", ""
66
 
67
  with gr.Blocks() as demo:
68
+ gr.Markdown("## Распаковка .cps с красивым parts")
69
  file_input = gr.File(label="Загрузите .cps файл", file_types=[".cps"])
70
 
71
  hex_orig_out = gr.Textbox(label="HEX оригинала", lines=5)
72
  hex_decomp_out = gr.Textbox(label="HEX распакованных данных", lines=10)
73
+ bson_parsed = gr.Textbox(label="Распарсенный BSON с parts", lines=30)
74
 
75
  file_input.change(
76
  fn=analyze_cps,