save parser finished
This commit is contained in:
parent
56cde5623e
commit
c6d7e148df
@ -27,10 +27,14 @@ class save_Parser:
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.map_shape)
|
return str(self.map_shape)
|
||||||
|
|
||||||
def shape_to_bytes(self, shape):
|
def shape_to_bytes(self, shape_matrix):
|
||||||
bytes_ammount = len(shape) // 8
|
shape_list = [b for r in shape_matrix for b in r]
|
||||||
bytes_trash = len(shape) % 8
|
byte_ammount = len(shape_list) // 8 + 1
|
||||||
tray = 0
|
tray = 0
|
||||||
|
for i in shape_list:
|
||||||
|
tray = (tray << 1) | i
|
||||||
|
return tray.to_bytes(byte_ammount, 'big')
|
||||||
|
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
"""
|
"""
|
||||||
@ -43,14 +47,19 @@ class save_Parser:
|
|||||||
"""
|
"""
|
||||||
save_data = b''
|
save_data = b''
|
||||||
save_data += b'SMS'
|
save_data += b'SMS'
|
||||||
save_data += bytes(len(map_shape))
|
save_data += len(self.map_shape).to_bytes(1, 'big')
|
||||||
save_data += shape_to_bytes(self.map_shape)
|
save_data += self.shape_to_bytes(self.map_shape)
|
||||||
save_data += bytes()
|
save_data += len(self.pieces).to_bytes(1, 'big')
|
||||||
|
for piece in self.pieces:
|
||||||
|
save_data +=len(piece).to_bytes(1, 'big')
|
||||||
|
save_data += self.shape_to_bytes(piece)
|
||||||
save_data += b'SME'
|
save_data += b'SME'
|
||||||
with open(self.filename, mode='bw') as file:
|
with open(self.filename, mode='bw') as file:
|
||||||
file.write(save_data)
|
file.write(save_data)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
p = save_Parser('test.map')
|
p = save_Parser('test.map')
|
||||||
p.define_map([[0,1],[0,1]])
|
p.define_map([[1,0,1],[1,1,1], [1,0,0]])
|
||||||
|
p.add_piece([[0, 1],[0, 1]])
|
||||||
p.save()
|
p.save()
|
||||||
|
# print(p.shape_to_bytes([[1,0,1],[1,1,1], [1,0,0]]))
|
||||||
|
@ -1 +1 @@
|
|||||||
SMSSME
|
SMS|SME
|
Loading…
Reference in New Issue
Block a user