@@ -11,11 +11,19 @@ def __init__(self):
11
11
self .data = bytearray ()
12
12
self .offset_map = {}
13
13
14
- def add (self , bytes_data , key = None ):
14
+ def add (self , bytes_data , expected_offset = None , key = None ):
15
+ if expected_offset is not None :
16
+ current_size = len (self .data )
17
+ if current_size < expected_offset :
18
+ padding = bytearray (expected_offset - current_size )
19
+ self .data .extend (padding )
20
+
15
21
offset = len (self .data )
16
22
self .data .extend (bytes_data )
23
+
17
24
if key :
18
25
self .offset_map [key ] = offset
26
+
19
27
return offset
20
28
21
29
def to_array (self ):
@@ -67,47 +75,38 @@ def get_binary(type, bin, elm_size):
67
75
return bin
68
76
69
77
def binary_write_recursive (bw_container : BinaryWriterContainer , offmap , allocator , json_data , typename ):
70
- #lines = offmap[typename]
71
78
lines = offmap .get (typename )
72
79
for key in json_data :
73
80
line = offset_parser .select_by_name (lines , key )
74
81
if line is None :
75
82
continue
76
83
type = offset_parser .member_type (line )
77
- if (offset_parser .is_primitive (line )):
78
- if (offset_parser .is_single (line )):
84
+ off = offset_parser .member_off (line )
85
+ if offset_parser .is_primitive (line ):
86
+ if offset_parser .is_single (line ):
79
87
bin = binary_io .typeTobin (type , json_data [key ])
80
88
bin = get_binary (type , bin , offset_parser .member_size (line ))
81
- allocator .add (bin )
82
- elif (offset_parser .is_array (line )):
89
+ # print(f"{type} {key} = {json_data[key]} : bin: {bin} size: {offset_parser.member_size(line)} bin_size: {len(bin)}")
90
+ allocator .add (bin , expected_offset = off )
91
+ elif offset_parser .is_array (line ):
83
92
elm_size = offset_parser .member_size (line )
84
93
array_size = offset_parser .array_size (line )
85
- one_elm_size = int (elm_size / array_size )
86
- i = 0
87
- for elm in json_data [key ]:
94
+ one_elm_size = int (elm_size / array_size )
95
+ for i , elm in enumerate (json_data [key ]):
88
96
bin = binary_io .typeTobin (type , elm )
89
97
bin = get_binary (type , bin , one_elm_size )
90
- allocator .add (bin )
91
- i = i + 1
92
- else : #varray
93
- i = 0
94
- for elm in json_data [key ]:
98
+ allocator .add (bin , expected_offset = (off + i * one_elm_size ))
99
+ else : # varray
100
+ for i , elm in enumerate (json_data [key ]):
95
101
bin = binary_io .typeTobin (type , elm )
96
102
bin = get_binary (type , bin , offset_parser .member_size (line ))
97
- bw_container .heap_allocator .add (bin )
98
- i = i + 1
103
+ bw_container .heap_allocator .add (bin , expected_offset = (off + i * offset_parser .member_size (line )))
99
104
else :
100
- if ( offset_parser .is_single (line ) ):
105
+ if offset_parser .is_single (line ):
101
106
binary_write_recursive (bw_container , offmap , allocator , json_data [key ], type )
102
- elif (offset_parser .is_array (line )):
103
- i = 0
104
- for elm in json_data [key ]:
107
+ elif offset_parser .is_array (line ):
108
+ for i , elm in enumerate (json_data [key ]):
105
109
binary_write_recursive (bw_container , offmap , allocator , elm , type )
106
- i = i + 1
107
- else : #varray
108
- i = 0
109
- for elm in json_data [key ]:
110
+ else : # varray
111
+ for i , elm in enumerate (json_data [key ]):
110
112
binary_write_recursive (bw_container , offmap , bw_container .heap_allocator , elm , type )
111
- i = i + 1
112
-
113
-
0 commit comments