75 lines
No EOL
1.9 KiB
Python
75 lines
No EOL
1.9 KiB
Python
import struct
|
|
|
|
OPCODES = dict()
|
|
OPCODES['create'] = 0
|
|
OPCODES['endtick'] = 2
|
|
OPCODES['link'] = 1
|
|
OPCODES['setpin'] = 3
|
|
OPCODES['unlink'] = 8
|
|
OPCODES['printstate'] = 9
|
|
|
|
with open("zigsonnum/activity.zig", 'r') as zigfl:
|
|
|
|
zone = False
|
|
for ln in zigfl.read().split('\n'):
|
|
|
|
if 'STARTOPCODES' in ln:
|
|
zone = True
|
|
|
|
elif 'ENDOPCODES' in ln:
|
|
zone = False
|
|
|
|
elif zone and '=>' in ln:
|
|
parts = ln.split('=>')
|
|
num = parts[0].strip()
|
|
name = parts[1].split('(')[0].split('.')[1]
|
|
try:
|
|
OPCODES[name] = int(num)
|
|
except:
|
|
pass
|
|
|
|
|
|
class Activity:
|
|
|
|
def __repr__(self):
|
|
|
|
return f'{self.name.rjust(20)} | {OPCODES[self.name]} {self.tick_start} {self.tick_end} {self.src_node.order if self.src_node else 0} {self.trg_node.order if self.trg_node else 0} '+' '.join(str(op) for op in self.operands)
|
|
|
|
def __init__(self, sonnum, name, tick_start, tick_end, src_node, trg_node, operands):
|
|
|
|
self.sonnum = sonnum
|
|
|
|
self.name = name
|
|
self.tick_start = tick_start
|
|
self.tick_end = tick_end
|
|
self.src_node = src_node
|
|
self.trg_node = trg_node
|
|
self.operands = operands
|
|
|
|
def to_bytes(self):
|
|
|
|
if self.name in OPCODES:
|
|
|
|
b_opcode = OPCODES[self.name].to_bytes(2, byteorder="big")
|
|
b_tick_start = int(self.tick_start).to_bytes(4, byteorder="big")
|
|
b_tick_end = int(self.tick_end).to_bytes(4, byteorder="big")
|
|
|
|
if not self.src_node:
|
|
b_src_node = b'\x00' * 2
|
|
else:
|
|
b_src_node = self.src_node.order.to_bytes(2, byteorder="big")
|
|
|
|
if not self.trg_node:
|
|
b_trg_node = b'\x00' * 2
|
|
else:
|
|
b_trg_node = self.trg_node.order.to_bytes(2, byteorder="big")
|
|
|
|
b_operands = [struct.pack('>d', 0.0)] * 6
|
|
|
|
for i in range(0, 6):
|
|
if len(self.operands) > i: b_operands[i] = struct.pack('>d', self.operands[i])
|
|
|
|
return b_opcode + b_tick_start + b_tick_end + b_src_node + b_trg_node + b''.join(b_operands)
|
|
|
|
else:
|
|
return b'' |