forked from DevScarabyte/pypack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (112 loc) · 3.94 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import sys
import struct
from datetime import datetime
class PackingUtils:
@staticmethod
def writeStr(pbo, string):
try:
pbo.write(str(string).encode('utf-8'))
PackingUtils.writeNull(pbo)
except Exception as e:
print(f"Error writing string to PBO: {e}")
sys.exit(1)
@staticmethod
def writeNull(pbo):
try:
pbo.write(struct.pack('x'))
except Exception as e:
print(f"Error writing null to PBO: {e}")
sys.exit(1)
@staticmethod
def writeInt(pbo, num):
try:
pbo.write(num.to_bytes(4, byteorder='little'))
except Exception as e:
print(f"Error writing integer to PBO: {e}")
sys.exit(1)
@staticmethod
def writeUInt(pbo, num):
try:
pbo.write(num.to_bytes(4, byteorder='little'))
except Exception as e:
print(f"Error writing unsigned integer to PBO: {e}")
sys.exit(1)
@staticmethod
def writeByte(pbo, num):
try:
pbo.write(struct.pack('B', num))
except Exception as e:
print(f"Error writing byte to PBO: {e}")
sys.exit(1)
@staticmethod
def writeBytes(pbo, num, amount):
try:
for _ in range(amount):
pbo.write(struct.pack('B', num))
except Exception as e:
print(f"Error writing bytes to PBO: {e}")
sys.exit(1)
class FileEntry:
def __init__(self, name, winPath, entryData, packingType):
self.fileName = name
self.diskPath = winPath
self.data = entryData
self.packingType = packingType
self.repopulate()
def repopulate(self):
self.originalSize = len(self.data)
self.offset = 0
self.timestamp = int(round(datetime.now().timestamp()))
if self.packingType == 0:
self.dataSize = self.originalSize
self.dataCompressed = b''
else:
# Additional error handling could be placed here
print("Unsupported packing type.")
sys.exit(1)
def WritePbo(folder, files):
print("Writing headers...")
with open(folder + "-packed.pbo", "wb") as pbo:
try:
for _ in range(20):
PackingUtils.writeNull(pbo)
for info in ["sreV", "product", "dayz ugc", "prefix\0" + os.path.basename(os.path.normpath(folder))]:
PackingUtils.writeStr(pbo, info)
for f in files:
PackingUtils.writeStr(pbo, f.fileName)
PackingUtils.writeByte(pbo, f.packingType)
PackingUtils.writeUInt(pbo, f.originalSize)
PackingUtils.writeByte(pbo, f.offset)
PackingUtils.writeUInt(pbo, f.timestamp)
PackingUtils.writeUInt(pbo, f.dataSize)
for _ in range(5):
PackingUtils.writeNull(pbo)
for f in files:
pbo.write(f.data)
print("Done...")
except Exception as e:
print(f"Error while writing PBO: {e}")
sys.exit(1)
def main():
if len(sys.argv) != 2:
sys.exit(1)
folder = sys.argv[1]
if not os.path.exists(folder):
sys.exit("Source folder not found.")
allowed_ext = ['.c', '.cpp']
files = []
for r, d, f in os.walk(folder):
for file in f:
if any(ext in file for ext in allowed_ext):
try:
with open(os.path.join(r, file), 'rb') as fh:
ba = fh.read()
filename = os.path.join(r, file).replace(folder + "/", "")
files.append(FileEntry(filename, file, ba, 0))
except Exception as e:
print(f"Error reading file {file}: {e}")
sys.exit(1)
WritePbo(folder, files)
if __name__ == "__main__":
main()