-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.py
86 lines (69 loc) · 3.03 KB
/
block.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright (C) 2021-2024
# Created by LightningPirate
# This file is part of SWE1R Import/Export.
# SWE1R Import/Export is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org
# /licenses>.
import struct
import os
from .general import *
class Block():
def __init__(self, path, arr):
self.asset_count = 0
self.buffers = []
self.data = arr
self.sub_chunks = len(arr)
self.path = path
self.dir = os.path.dirname(self.path)
def read(self):
with open(self.path, 'rb') as file:
file = file.read()
if len(file) == 0:
return None
asset_count = readUInt32BE(file, 0)
cursor = 4
self.data = [[] for f in range(self.sub_chunks)]
addresses = []
for i in range(asset_count):
for j in range(self.sub_chunks):
offset = i*4*self.sub_chunks + j * 4
asset_start = readUInt32BE(file, 4 + offset)
cursor += 4
asset_end = readUInt32BE(file, 4 + offset + 4)
if not asset_end:
asset_end = readUInt32BE(file, 4 + offset + 8)
asset = file[asset_start:asset_end] if asset_start else None
addresses.append(asset_start)
self.data[j].append(asset)
return self
def write(self):
asset_count = len(self.data[0])
header = bytearray((asset_count * self.sub_chunks + 2) * 4)
block = []
block.append(header)
struct.pack_into('>I', header, 0, asset_count) # write total number of assets
cursor = len(header)
for i in range(asset_count):
for j in range(self.sub_chunks):
struct.pack_into('>I', header, 4 + (i * self.sub_chunks + j) * 4, cursor if (self.data[j][i] and len(self.data[j][i])) else 0)
if self.data[j][i] is not None:
cursor += len(self.data[j][i])
block.append(self.data[j][i])
struct.pack_into('>I', header, (asset_count * self.sub_chunks + 1) * 4, cursor) # end of block offset
return b''.join(block)
def inject(self, data, index):
for j in range(self.sub_chunks):
print(j, index, len(data))
self.data[j][index] = data[j]
return self
def fetch(self, index):
return [self.data[j][index] for j in range(self.sub_chunks)]