-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdar_pack.py
More file actions
85 lines (70 loc) · 2.71 KB
/
Copy pathdar_pack.py
File metadata and controls
85 lines (70 loc) · 2.71 KB
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
85
# DAR Pack
# A tool that creates "dar" files for BIT.TRIP COMPLETE on the Wii
# Copyright (C) 2026 Meatball132
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import sys
import os
def main():
try:
arg = str(sys.argv[1])
input_dir = arg
except:
print(f"Usage: {sys.argv[0]} input_dir")
return
if not os.path.isdir(f"{input_dir}"):
print("Folder does not exist")
return
# Build file table
file_count = 0
file_size = 0
file_offset = 32
file_table_paths = []
file_table_sizes = []
file_table_offsets = []
for root, dirs, files in os.walk(f"{input_dir}"):
for file in files:
file_count += 1
path = os.path.join(root, file)
file_path = path.replace("\\", "/").replace(f"{input_dir}/", "")
file_offset += file_size # calculated based on last filesize value
file_offset = (file_offset + 31) & ~31 # align to 32 bytes
file_size = os.path.getsize(path)
file_table_paths.append(file_path)
file_table_sizes.append(file_size)
file_table_offsets.append(file_offset)
# Write output file
out_filename = f"{input_dir}.dar"
magic = "DAR"
with open(out_filename, "wb") as out_file:
# header
out_file.write(magic.encode("utf-8"))
pad(out_file)
# file data
file_idx = -1
for file_path in file_table_paths:
with open(f"{input_dir}/{file_path}", "rb") as file:
out_file.write(file.read())
file_idx += 1
if file_idx != len(file_table_paths) - 1:
pad(out_file)
# file table
out_file.write((0xA).to_bytes())
file_table_offset = out_file.tell()
out_file.write(str(file_count).encode("utf-8"))
out_file.write((0xA).to_bytes())
for i in range(0, file_count):
out_file.write(file_table_paths[i].encode("utf-8"))
out_file.write(" ".encode("utf-8"))
out_file.write(str(file_table_sizes[i]).encode("utf-8"))
out_file.write(" ".encode("utf-8"))
out_file.write(str(file_table_offsets[i]).encode("utf-8"))
out_file.write((0xA).to_bytes())
out_file.write((file_table_offset).to_bytes(4))
def pad(out_file):
pad = "a"
target_offset = (out_file.tell() + 31) & ~31
for i in range(out_file.tell(), target_offset):
out_file.write(pad.encode("utf-8"))
main()