From c6d7e148df518fc2bbd20130ee1a7e02fd13d5bd Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Tue, 14 Feb 2023 16:27:02 +0100 Subject: [PATCH] save parser finished --- prototypes/saves_prototypes/parser.py | 23 ++++++++++++++++------- prototypes/saves_prototypes/test.map | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/prototypes/saves_prototypes/parser.py b/prototypes/saves_prototypes/parser.py index 78a96ba..6adf531 100644 --- a/prototypes/saves_prototypes/parser.py +++ b/prototypes/saves_prototypes/parser.py @@ -27,10 +27,14 @@ class save_Parser: def __str__(self): return str(self.map_shape) - def shape_to_bytes(self, shape): - bytes_ammount = len(shape) // 8 - bytes_trash = len(shape) % 8 + def shape_to_bytes(self, shape_matrix): + shape_list = [b for r in shape_matrix for b in r] + byte_ammount = len(shape_list) // 8 + 1 tray = 0 + for i in shape_list: + tray = (tray << 1) | i + return tray.to_bytes(byte_ammount, 'big') + def load(self): """ @@ -43,14 +47,19 @@ class save_Parser: """ save_data = b'' save_data += b'SMS' - save_data += bytes(len(map_shape)) - save_data += shape_to_bytes(self.map_shape) - save_data += bytes() + save_data += len(self.map_shape).to_bytes(1, 'big') + save_data += self.shape_to_bytes(self.map_shape) + 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' with open(self.filename, mode='bw') as file: file.write(save_data) if __name__ == "__main__": 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() + # print(p.shape_to_bytes([[1,0,1],[1,1,1], [1,0,0]])) diff --git a/prototypes/saves_prototypes/test.map b/prototypes/saves_prototypes/test.map index 406295c..fff7b49 100644 --- a/prototypes/saves_prototypes/test.map +++ b/prototypes/saves_prototypes/test.map @@ -1 +1 @@ -SMSSME \ No newline at end of file +SMS|SME \ No newline at end of file