33import json
44import sys
55
6- from hako_binary import binary_io
7- from hako_binary import offset_parser
6+ from . import binary_io
7+ from . import offset_parser
88
99class DynamicAllocator :
1010 def __init__ (self , is_heap : bool ):
@@ -13,16 +13,14 @@ def __init__(self, is_heap: bool):
1313 self .is_heap = is_heap
1414
1515 def add (self , bytes_data , expected_offset = None , key = None ):
16- if self .is_heap == False :
17- if expected_offset is not None :
18- current_size = len (self .data )
19- if current_size < expected_offset :
20- padding = bytearray (expected_offset - current_size )
21- self .data .extend (padding )
22-
16+ current_size = len (self .data )
17+ #print(f"is_heap: {self.is_heap} current_size: {current_size} expected_offset: {expected_offset} len(bytes_data): {len(bytes_data)}")
18+ if (current_size < expected_offset ):
19+ padding = bytearray (expected_offset - current_size )
20+ self .data .extend (padding )
2321 offset = len (self .data )
2422 self .data .extend (bytes_data )
25-
23+ #print(f"add: {bytes_data} offset: {offset} len(self.data): {len(self.data)}")
2624 if key :
2725 self .offset_map [key ] = offset
2826
@@ -84,6 +82,7 @@ def binary_write_recursive(parent_off: int, bw_container: BinaryWriterContainer,
8482 continue
8583 type = offset_parser .member_type (line )
8684 off = offset_parser .member_off (line )
85+ #print(f"key: {key} type: {type} off: {off} line: {line}")
8786 if offset_parser .is_primitive (line ):
8887 if offset_parser .is_single (line ):
8988 bin = binary_io .typeTobin (type , json_data [key ])
@@ -94,15 +93,25 @@ def binary_write_recursive(parent_off: int, bw_container: BinaryWriterContainer,
9493 elm_size = offset_parser .member_size (line )
9594 array_size = offset_parser .array_size (line )
9695 one_elm_size = int (elm_size / array_size )
97- for i , elm in enumerate (json_data [key ]):
98- bin = binary_io .typeTobin (type , elm )
99- bin = get_binary (type , bin , one_elm_size )
100- allocator .add (bin , expected_offset = (parent_off + off + i * one_elm_size ))
96+ #for i, elm in enumerate(json_data[key]):
97+ # bin = binary_io.typeTobin(type, elm)
98+ # bin = get_binary(type, bin, one_elm_size)
99+ # allocator.add(bin, expected_offset=(parent_off + off + i * one_elm_size))
100+ binary = binary_io .typeTobin_array (type , json_data [key ], one_elm_size )
101+ allocator .add (binary , expected_offset = (parent_off + off ))
101102 else : # varray
102- for i , elm in enumerate (json_data [key ]):
103- bin = binary_io .typeTobin (type , elm )
104- bin = get_binary (type , bin , offset_parser .member_size (line ))
105- bw_container .heap_allocator .add (bin , expected_offset = (off + i * offset_parser .member_size (line )))
103+ offset_from_heap = bw_container .heap_allocator .size ()
104+ array_size = len (json_data [key ])
105+ #print(f"varray: {key} array_size: {array_size} offset_from_heap: {offset_from_heap}")
106+ #for i, elm in enumerate(json_data[key]):
107+ # bin = binary_io.typeTobin(type, elm)
108+ # bin = get_binary(type, bin, offset_parser.member_size(line))
109+ # bw_container.heap_allocator.add(bin, expected_offset=0)
110+ binary = binary_io .typeTobin_array (type , json_data [key ], offset_parser .member_size (line ))
111+ bw_container .heap_allocator .add (binary , expected_offset = 0 )
112+ a_b = array_size .to_bytes (4 , byteorder = 'little' )
113+ o_b = offset_from_heap .to_bytes (4 , byteorder = 'little' )
114+ allocator .add (a_b + o_b , expected_offset = parent_off + off )
106115 else :
107116 if offset_parser .is_single (line ):
108117 binary_write_recursive (parent_off + off , bw_container , offmap , allocator , json_data [key ], type )
@@ -113,5 +122,11 @@ def binary_write_recursive(parent_off: int, bw_container: BinaryWriterContainer,
113122 one_elm_size = int (elm_size / array_size )
114123 binary_write_recursive ((parent_off + off + i * one_elm_size ), bw_container , offmap , allocator , elm , type )
115124 else : # varray
125+ offset_from_heap = bw_container .heap_allocator .size ()
126+ array_size = len (json_data [key ])
116127 for i , elm in enumerate (json_data [key ]):
117128 binary_write_recursive (0 , bw_container , offmap , bw_container .heap_allocator , elm , type )
129+ a_b = array_size .to_bytes (4 , byteorder = 'little' )
130+ o_b = offset_from_heap .to_bytes (4 , byteorder = 'little' )
131+ allocator .add (a_b + o_b , expected_offset = parent_off + off )
132+
0 commit comments