1+ from __future__ import annotations
2+
13from pathlib import Path
24
35
@@ -6,6 +8,7 @@ class BrsarGroup:
68 Each group in a brsar file holds a list of songs.
79 Different groups are used for different types of songs.
810 """
11+
912 Regular = 2
1013 Maestro = 21
1114 Handbell = 23
@@ -20,14 +23,15 @@ class __BrsarSection:
2023 :param parent: The `parent` attribute is the parent section of the section.
2124 :param offset: The `offset` attribute is the offset of the section from the start of the file.
2225 """
26+
2327 _referenceValueOffset = 0x04
2428 _referenceSize = 0x08
2529
26- root : ' Brsar'
27- parent : ' __BrsarSection'
30+ root : " Brsar"
31+ parent : " __BrsarSection"
2832 offset : int
2933
30- def __init__ (self , parent : ' __BrsarSection' , offset : int ):
34+ def __init__ (self , parent : " __BrsarSection" , offset : int ):
3135 self .parent = parent
3236 self .root = parent .root
3337 self .offset = offset
@@ -39,7 +43,7 @@ def read_bytes(self, offset: int) -> int:
3943 """
4044 length = 4
4145 offset += self .offset
42- return int .from_bytes (self .root .data [offset : offset + length ], "big" )
46+ return int .from_bytes (self .root .data [offset : offset + length ], "big" )
4347
4448 def section_reference (self , offset : int ) -> int :
4549 """
@@ -61,7 +65,7 @@ def increment_value(self, value: str, increment: int) -> None:
6165 offset = getattr (self , f"_{ value } " ) + self .offset
6266 val = getattr (self , value ) + increment
6367 setattr (self , value , val )
64- self .root .data [offset : offset + length ] = val .to_bytes (length , "big" )
68+ self .root .data [offset : offset + length ] = val .to_bytes (length , "big" )
6569
6670
6771class Brsar (__BrsarSection ):
@@ -70,6 +74,7 @@ class Brsar(__BrsarSection):
7074 A list of all the sections in a brsar file can be found at https://wiki.tockdom.com/wiki/BRSAR_(File_Format).
7175 :param path: The path to a brsar file.
7276 """
77+
7378 _fileLength = 0x08
7479 _infoSectionOffset = 0x18
7580 _infoSectionSize = 0x1C
@@ -84,8 +89,8 @@ class Brsar(__BrsarSection):
8489
8590 brsarPath : Path
8691 data : bytearray
87- infoSection : ' InfoSection'
88- fileSection : ' FileSection'
92+ infoSection : " InfoSection"
93+ fileSection : " FileSection"
8994
9095 def __init__ (self , path : Path ):
9196 self .root = self
@@ -118,22 +123,24 @@ def replace_song(self, song: bytearray, group_index: int, item_index: int):
118123 incrementAmount = len (song ) - itemGroup .rseqSize
119124
120125 # Replace song data
121- self .data = self .data [:rseqOffset ]+ song + self .data [rseqOffset + itemGroup .rseqSize :]
126+ self .data = (
127+ self .data [:rseqOffset ] + song + self .data [rseqOffset + itemGroup .rseqSize :]
128+ )
122129
123130 # Update Group Table
124- songGroup .increment_value (' rseqSize' , incrementAmount )
125- songGroup .increment_value (' rwarOffset' , incrementAmount )
126- itemGroup .increment_value (' rseqSize' , incrementAmount )
127- for item in songGroup .itemTable .entries [item_index + 1 :]:
128- item .increment_value (' rseqOffset' , incrementAmount )
131+ songGroup .increment_value (" rseqSize" , incrementAmount )
132+ songGroup .increment_value (" rwarOffset" , incrementAmount )
133+ itemGroup .increment_value (" rseqSize" , incrementAmount )
134+ for item in songGroup .itemTable .entries [item_index + 1 :]:
135+ item .increment_value (" rseqOffset" , incrementAmount )
129136
130137 # Update other group tables
131- for group in self .infoSection .groupDataTable .entries [group_index + 1 :]:
132- group .increment_value (' rseqOffset' , incrementAmount )
133- group .increment_value (' rwarOffset' , incrementAmount )
138+ for group in self .infoSection .groupDataTable .entries [group_index + 1 :]:
139+ group .increment_value (" rseqOffset" , incrementAmount )
140+ group .increment_value (" rwarOffset" , incrementAmount )
134141
135142 # Update Section Size
136- self .increment_value (' fileLength' , incrementAmount )
143+ self .increment_value (" fileLength" , incrementAmount )
137144
138145 def get_song (self , group_index : int , item_index : int ) -> bytearray :
139146 """
@@ -145,7 +152,7 @@ def get_song(self, group_index: int, item_index: int) -> bytearray:
145152 songGroup = self .infoSection .groupDataTable .entries [group_index ]
146153 itemGroup = songGroup .itemTable .entries [item_index ]
147154 rseqOffset = songGroup .rseqOffset + itemGroup .rseqOffset
148- return self .data [rseqOffset : rseqOffset + itemGroup .rseqSize ]
155+ return self .data [rseqOffset : rseqOffset + itemGroup .rseqSize ]
149156
150157 def save (self ):
151158 with open (self .brsarPath , "wb" ) as file :
@@ -159,10 +166,11 @@ class InfoSection(__BrsarSection):
159166 :param parent: A reference to the brsar header.
160167 :param offset: The `offset` attribute is the offset of the section from the start of the file.
161168 """
169+
162170 _groupTable = 0x28
163171
164172 parent : Brsar
165- groupDataTable : ' GroupDataTable'
173+ groupDataTable : " GroupDataTable"
166174
167175 def __init__ (self , parent : Brsar , offset : int ):
168176 super ().__init__ (parent , offset )
@@ -177,6 +185,7 @@ class FileSection(__BrsarSection):
177185 :param parent: A reference to the brsar header.
178186 :param offset: The offset from the start of the file.
179187 """
188+
180189 _sectionSize = 0x04
181190
182191 parent : Brsar
@@ -195,17 +204,20 @@ class GroupDataTable(__BrsarSection):
195204 :param parent: A reference to the info section.
196205 :param offset: The offset from the start of the file.
197206 """
207+
198208 __numEntries = 0x00
199209 __entries = 0x04
200210
201211 parent : InfoSection
202- entries : list [' GroupDataEntry' ]
212+ entries : list [" GroupDataEntry" ]
203213
204214 def __init__ (self , parent : InfoSection , offset : int ):
205215 super ().__init__ (parent , offset )
206216 numEntries = self .read_bytes (self .__numEntries )
207217 self .entries = [
208- GroupDataEntry (self , self .section_reference (self .__entries + i * self ._referenceSize ))
218+ GroupDataEntry (
219+ self , self .section_reference (self .__entries + i * self ._referenceSize )
220+ )
209221 for i in range (numEntries )
210222 ]
211223
@@ -219,20 +231,23 @@ class GroupDataEntry(__BrsarSection):
219231 :param parent: A reference to the group data table.
220232 :param offset: The offset from the start of the file.
221233 """
234+
222235 _rseqOffset = 0x10
223236 _rseqSize = 0x14
224237 _rwarOffset = 0x18
225238 _groupItemEntry = 0x20
226239
227240 parent : GroupDataTable
228- itemTable : ' GroupItemTable'
241+ itemTable : " GroupItemTable"
229242
230243 def __init__ (self , parent : GroupDataTable , offset : int ):
231244 super ().__init__ (parent , offset )
232245 self .rseqOffset = self .read_bytes (self ._rseqOffset )
233246 self .rseqSize = self .read_bytes (self ._rseqSize )
234247 self .rwarOffset = self .read_bytes (self ._rwarOffset )
235- self .itemTable = GroupItemTable (self , self .section_reference (self ._groupItemEntry ))
248+ self .itemTable = GroupItemTable (
249+ self , self .section_reference (self ._groupItemEntry )
250+ )
236251
237252
238253class GroupItemTable (__BrsarSection ):
@@ -243,17 +258,20 @@ class GroupItemTable(__BrsarSection):
243258 :param parent: A reference to the group data entry.
244259 :param offset: The offset from the start of the file.
245260 """
261+
246262 __numEntries = 0x00
247263 __entries = 0x04
248264
249265 parent : GroupDataEntry
250- entries : list [' GroupItemEntry' ]
266+ entries : list [" GroupItemEntry" ]
251267
252268 def __init__ (self , parent : GroupDataEntry , offset : int ):
253269 super ().__init__ (parent , offset )
254270 numEntries = self .read_bytes (self .__numEntries )
255271 self .entries = [
256- GroupItemEntry (self , self .section_reference (self .__entries + i * self ._referenceSize ))
272+ GroupItemEntry (
273+ self , self .section_reference (self .__entries + i * self ._referenceSize )
274+ )
257275 for i in range (numEntries )
258276 ]
259277
@@ -265,6 +283,7 @@ class GroupItemEntry(__BrsarSection):
265283 Each rseqOffset is relative to the group offset.
266284 https://wiki.tockdom.com/wiki/BRSAR_(File_Format)#Group_Item_Info_Entry
267285 """
286+
268287 _rseqOffset = 0x04
269288 _rseqSize = 0x08
270289
@@ -276,4 +295,3 @@ def __init__(self, parent: GroupItemTable, offset: int):
276295 super ().__init__ (parent , offset )
277296 self .rseqOffset = self .read_bytes (self ._rseqOffset )
278297 self .rseqSize = self .read_bytes (self ._rseqSize )
279-
0 commit comments