2019-06-07 03:13:43 +00:00
|
|
|
import os
|
2019-06-07 02:28:15 +00:00
|
|
|
import shlex
|
2019-06-07 03:13:43 +00:00
|
|
|
import tempfile
|
|
|
|
from pathlib import Path
|
|
|
|
|
2019-06-07 02:55:59 +00:00
|
|
|
from .utils import load_file_path
|
|
|
|
from .error import InterpreterError
|
2019-06-07 02:28:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Interpreter:
|
|
|
|
"""Interpreter for scritcher instructions/statements."""
|
|
|
|
def __init__(self):
|
2019-06-07 03:13:43 +00:00
|
|
|
self.orig_path = None
|
2019-06-07 02:55:59 +00:00
|
|
|
self.loaded_path = None
|
2019-06-07 02:28:15 +00:00
|
|
|
|
|
|
|
def _cmd_noop(self):
|
|
|
|
pass
|
|
|
|
|
2019-06-07 02:55:59 +00:00
|
|
|
def _cmd_load(self, loadpath: str):
|
2019-06-07 03:13:43 +00:00
|
|
|
source_path = load_file_path(loadpath)
|
|
|
|
self.orig_path = source_path
|
|
|
|
|
|
|
|
# create a temporary file to hold bmp data
|
|
|
|
handle, bmp_path = tempfile.mkstemp('.bmp')
|
|
|
|
os.close(handle)
|
|
|
|
|
|
|
|
self.loaded_path = Path(bmp_path)
|
|
|
|
|
|
|
|
def _cmd_quicksave(self):
|
|
|
|
suffix = self.orig_path.suffix
|
|
|
|
name = self.orig_path.name.replace(suffix, '')
|
|
|
|
parent_folder = self.orig_path.parents[0]
|
|
|
|
|
|
|
|
# we need to search all files that match the pattern NAME_g*
|
|
|
|
index = 0
|
|
|
|
for glitched_out in parent_folder.glob(f'{name}*'):
|
|
|
|
try:
|
|
|
|
idx = int(glitched_out.name.strip(name + '_g')[0])
|
|
|
|
index = idx
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# create our next glitched iteration
|
|
|
|
glitched_path = parent_folder / f'{name}_g{index + 1}.raw'
|
|
|
|
print(glitched_path)
|
|
|
|
|
2019-06-07 02:55:59 +00:00
|
|
|
|
2019-06-07 02:28:15 +00:00
|
|
|
def run(self, line: str):
|
|
|
|
"""Run a single line."""
|
|
|
|
print(f'running {line!r}')
|
|
|
|
|
|
|
|
args = shlex.split(line)
|
|
|
|
command = args[0]
|
|
|
|
|
2019-06-07 02:55:59 +00:00
|
|
|
if command != 'load' and self.loaded_path is None:
|
|
|
|
print('warn: no file loaded.')
|
|
|
|
|
2019-06-07 02:28:15 +00:00
|
|
|
try:
|
|
|
|
method = getattr(self, f'_cmd_{command}')
|
|
|
|
except AttributeError:
|
|
|
|
raise InterpreterError(f'Command {command!r} not found')
|
|
|
|
|
|
|
|
method(*args[1:])
|