-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpersistency.py
72 lines (54 loc) · 1.96 KB
/
persistency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json
import typing
from models import ScanlineModel, PlayfieldMode, ColorSystem
from widgets import WPlayfield
def serialize_playfield(pf: WPlayfield, version: str) -> typing.Mapping:
scanlines = []
for j in range(pf.model.scanline_count):
line = pf[j]
line_data = [
0x00,
0x00,
0x00,
0x00,
0x00,
line.model.palette_code.value,
line.model.bg_palette_code.value,
]
for i in range(ScanlineModel.pixel_count):
if line.model.pixels[i]:
line_data[int(i / 8)] = line_data[int(i / 8)] | (0x80 >> int(i % 8))
scanlines.append(line_data)
return {
"version": version,
"name": pf.model.name,
"mode": pf.model.mode.name,
"color_system": pf.model.color_system.name,
"scanlines": scanlines,
}
def deserialize_playfield(data: typing.Mapping, *args, **kwargs) -> WPlayfield:
scanlines_data = data["scanlines"]
pf = WPlayfield(
name=data["name"],
mode=PlayfieldMode[data["mode"]],
color_system=ColorSystem[data["color_system"]],
scanline_count=len(scanlines_data),
*args,
**kwargs,
)
for j in range(pf.model.scanline_count):
line = pf[j]
line_data = scanlines_data[j]
for i in range(ScanlineModel.pixel_count):
line.model.pixels[i] = line_data[int(i / 8)] & (0x80 >> int(i % 8)) != 0
line.model.palette_code.value = line_data[5]
line.model.bg_palette_code.value = line_data[6]
return pf
def save_playfield(pf: WPlayfield, to: str, version: str):
data = serialize_playfield(pf=pf, version=version)
with open(to, "w") as f:
json.dump(obj=data, fp=f, sort_keys=True, indent=4)
def load_playfield(from_: str, *args, **kwargs) -> WPlayfield:
with open(from_) as file:
data = json.load(file)
return deserialize_playfield(data, *args, **kwargs)