Fixed local vector deserialization

- minor refactoring
This commit is contained in:
CPunch 2021-12-08 16:19:08 -06:00
parent 0b94b64c89
commit fda8ec118c

42
luac.py
View File

@ -39,6 +39,9 @@ class Constant:
self.type = type self.type = type
self.data = data self.data = data
def toString(self):
return "[" + self.type.name + "] " + str(self.data)
class Local: class Local:
def __init(self, name: str, start: int, end: int): def __init(self, name: str, start: int, end: int):
self.name = name self.name = name
@ -71,6 +74,20 @@ class Chunk:
def appendProto(self, proto): def appendProto(self, proto):
self.protos.append(proto) self.protos.append(proto)
def print(self):
print("\n==== [[" + str(self.name) + "'s constants]] ====\n")
for z in range(len(self.constants)):
i = self.constants[z]
print(str(z) + ": " + i.toString())
print("\n==== [[" + str(self.name) + "'s dissassembly]] ====\n")
for i in range(len(self.instructions)):
print("[%3d] %s" % (i, self.instructions[i].toString()))
print("\n==== [[" + str(self.name) + "'s protos]] ====\n")
for z in self.protos:
z.print()
instr_lookup_tbl = [ instr_lookup_tbl = [
Instruction(InstructionType.ABC, "MOVE"), Instruction(InstructionType.ABx, "LOADK"), Instruction(InstructionType.ABC, "LOADBOOL"), Instruction(InstructionType.ABC, "MOVE"), Instruction(InstructionType.ABx, "LOADK"), Instruction(InstructionType.ABC, "LOADBOOL"),
Instruction(InstructionType.ABC, "LOADNIL"), Instruction(InstructionType.ABC, "GETUPVAL"), Instruction(InstructionType.ABx, "GETGLOBAL"), Instruction(InstructionType.ABC, "LOADNIL"), Instruction(InstructionType.ABC, "GETUPVAL"), Instruction(InstructionType.ABx, "GETGLOBAL"),
@ -115,21 +132,12 @@ class LuaUndump:
@staticmethod @staticmethod
def dis_chunk(chunk: Chunk): def dis_chunk(chunk: Chunk):
print("\n==== [[" + str(chunk.name) + "'s constants]] ====\n") chunk.print()
for z in range(len(chunk.constants)):
i = chunk.constants[z]
print(str(z) + ": [" + i.type.name + "] " + str(i.data))
print("\n==== [[" + str(chunk.name) + "'s dissassembly]] ====\n")
for i in range(len(chunk.instructions)):
print("[%3d] %s" % (i, chunk.instructions[i].toString()))
print("\n==== [[" + str(chunk.name) + "'s protos]] ====\n")
for z in chunk.protos:
print("** decoding proto\n")
LuaUndump.dis_chunk(z)
def loadBlock(self, sz) -> bytearray: def loadBlock(self, sz) -> bytearray:
if self.index + sz > len(self.bytecode):
raise Exception("Malformed bytecode!")
temp = bytearray(self.bytecode[self.index:self.index+sz]) temp = bytearray(self.bytecode[self.index:self.index+sz])
self.index = self.index + sz self.index = self.index + sz
return temp return temp
@ -234,14 +242,14 @@ class LuaUndump:
# line numbers # line numbers
num = self.get_int() num = self.get_int()
for i in range(num): for i in range(num):
self.get_int32() self.get_int()
# locals # locals
num = self.get_int() num = self.get_int()
for i in range(num): for i in range(num):
#print(self.get_string(None)[:-1]) # local name self.get_string(None)[:-1] # local name
self.get_int32() # local start PC self.get_int() # local start PC
self.get_int32() # local end PC self.get_int() # local end PC
# upvalues # upvalues
num = self.get_int() num = self.get_int()