scratchabit: Factor out call_script(), call main() if exists with APP object.
[ScratchABit.git] / saveload.py
blob6bda61c13c857ee75c0bfdde16445c4ac30cd7e6
1 import sys
2 import os
3 import glob
5 import engine
8 def save_exists(project_dir):
9 files = list(glob.glob(project_dir + "/project.aspace*"))
10 return bool(files)
13 def ensure_project_dir(project_dir):
14 if not os.path.isdir(project_dir):
15 os.makedirs(project_dir)
17 def backup_by_prefix(prefix):
18 for fname in glob.glob(prefix):
19 if not fname.endswith(".bak"):
20 os.rename(fname, fname + ".bak")
22 def save_state(project_dir):
23 ensure_project_dir(project_dir)
24 files = ["project.aspace", "project.aprops"]
25 for fname in files:
26 backup_by_prefix(project_dir + "/" + fname + "*")
28 for area in engine.ADDRESS_SPACE.get_areas():
29 with open(project_dir + "/project.aspace.%08x" % area[engine.START], "w") as f:
30 engine.ADDRESS_SPACE.save_area(f, area)
32 engine.ADDRESS_SPACE.save_addr_props(project_dir + "/project.aprops")
35 def load_state(project_dir):
36 files = list(glob.glob(project_dir + "/project.aprops*"))
37 if not files:
38 print("""
39 Cannot find project.aprops file. Possibly, you use old database format.
40 Use version 0.9 to migrate.
41 """)
42 sys.exit(1)
44 print("Loading state...")
46 for area in engine.ADDRESS_SPACE.get_areas():
47 fname = project_dir + "/project.aspace.%08x" % area[engine.START]
48 with open(fname) as f:
49 engine.ADDRESS_SPACE.load_area(f, area)
51 fname = project_dir + "/project.aprops.%08x" % area[engine.START]
52 if os.path.exists(fname):
53 with open(fname) as f:
54 engine.ADDRESS_SPACE.load_addr_props(f)
55 else:
56 print("Warning: %s doesn't exist" % fname)
59 # Save user-specific session parameter, like current address,
60 # address goto stack.
61 def save_session(project_dir, disasm_viewer):
62 ensure_project_dir(project_dir)
63 with open(project_dir + "/session.addr_stack", "w") as f:
64 for a in disasm_viewer.addr_stack:
65 if isinstance(a, tuple):
66 a = a[0]
67 f.write("%08x\n" % a)
68 f.write("%08x\n" % disasm_viewer.cur_addr())
71 def load_addr_stack(project_dir):
72 stack = []
73 with open(project_dir + "/session.addr_stack") as f:
74 for l in f:
75 stack.append(int(l, 16))
76 return stack