-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasicparse.py
More file actions
62 lines (52 loc) · 2.08 KB
/
Copy pathbasicparse.py
File metadata and controls
62 lines (52 loc) · 2.08 KB
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
from sc3000decoder import read_bas_as_hex_string, decode_hex_string
from sc3000decoder import print_decoded, save_decoded_to, escape_char
from sc3000encoder import encode_script_string
import binascii
from basparse import getBasicSections
from section import KeyCode
from util import removeExtension
from pathlib import Path
def writeBasic(filename, d, opt): # already parsed
fname = removeExtension(filename)
codeChunks = []
for s in d["sections"]:
if s["type"] == "bytes" and KeyCode.code[s["keycode"]] == KeyCode.BasicData:
codeChunks.append(s["Program"])
ext = Path(filename).suffix
if len(codeChunks) == 1:
decode(fname + ext, codeChunks[0])
else:
for idx, c in enumerate(codeChunks):
decode(f"{fname}{idx}{ext}", c)
def writeFilename(filename, d, opt):
fname = removeExtension(filename)
names = []
for s in d["sections"]:
if s["type"] == "bytes" and KeyCode.code[s["keycode"]] == KeyCode.BasicHeader:
names.append(s["Filename"])
if len(names) == 1:
open(fname + ".filename", "w", encoding="utf-8").write(names[0])
else:
for idx, na in enumerate(names):
open(f"{fname}{idx}.filename", "w", encoding="utf-8").write(na)
def readBasic(filename, opts):
if "basic_raw_encoding" in opts:
raw = open(filename, "rb").read()
# print("==RAW==", raw)
script_string = "".join([escape_char(ch, toPass=[0x0A])
for ch in raw if ch not in [0x01, 0x0D]])
else:
script_string = open(filename, encoding="utf-8").read()
suppress_error = False
encoded = encode_script_string(script_string, suppress_error)
result = ""
for line in encoded:
result += line["encoded"]
resultb = list(binascii.a2b_hex(result))
if "\\bin" not in encoded[-1]["raw"]:
resultb += [0x00, 0x00]
return getBasicSections(resultb, opts)
def decode(fname, chunk):
hex_string = bytes(chunk).hex().upper()
decoded = decode_hex_string(hex_string, suppress_error=True)
save_decoded_to(fname, decoded)