-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_io.py
166 lines (136 loc) · 6.07 KB
/
file_io.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""A class for reading binary data from a file """
from typing import List, Union
from struct import unpack as Unpack, pack as Pack
from mathutils import Vector, Matrix, Quaternion
class FileReader:
"""A class for reading binary data from a file"""
def __init__(self, FileName: str):
"""Initializes the FileReader class"""
self.Buffer = open(FileName, "rb")
def Seek(self, Offset: int, Origin: int = 0):
"""Seeks to a position in the buffer"""
self.Buffer.seek(Offset, Origin)
def ReadInt(self, n: int = 1) -> Union[int, List[int]]:
"""Reads n 32-bit integers from the buffer and returns them as a list or a single Value if n is 1"""
Value = Unpack(f"{n}i", self.Buffer.read(n * 4))
return Value[0] if n == 1 else list(Value)
def ReadUInt(self, n: int = 1) -> Union[int, List[int]]:
"""Reads n 32-bit unsigned integers from the buffer and returns them as a list or a single Value if n is 1"""
Value = Unpack(f"{n}I", self.Buffer.read(n * 4))
return Value[0] if n == 1 else list(Value)
def ReadFloat(self, n: int = 1) -> Union[float, List[float]]:
"""Reads n 32-bit floats from the buffer and returns them as a list or a single Value if n is 1"""
Value = Unpack(f"{n}f", self.Buffer.read(n * 4))
return Value[0] if n == 1 else list(Value)
def ReadShort(self, n: int = 1) -> Union[int, List[int]]:
"""Reads n 16-bit integers from the buffer and returns them as a list or a single Value if n is 1"""
Value = Unpack(f"{n}h", self.Buffer.read(n * 2))
return Value[0] if n == 1 else list(Value)
def ReadUShort(self, n: int = 1) -> Union[int, List[int]]:
"""Reads n 16-bit unsigned integers from the buffer and returns them as a list or a single Value if n is 1"""
Value = Unpack(f"{n}H", self.Buffer.read(n * 2))
return Value[0] if n == 1 else list(Value)
def ReadString(self, n: int = 1) -> str:
"""Reads n bytes from the buffer and returns them as a string"""
return Unpack(f"{n}s", self.Buffer.read(n))[0].decode("UTF-8")
def ReadByte(self, n: int = 1) -> Union[int, List[int]]:
"""Reads n bytes from the buffer and returns them as a list or a single Value if n is 1"""
Value = Unpack(f"{n}B", self.Buffer.read(n))
return Value[0] if n == 1 else list(Value)
def ReadVector2(self) -> Vector:
"""Reads 2 32-bit floats from the buffer and returns them as a Vector"""
return Vector((self.ReadFloat(), self.ReadFloat()))
def ReadVector3(self) -> Vector:
"""Reads 3 32-bit floats from the buffer and returns them as a Vector"""
return Vector((self.ReadFloat(), self.ReadFloat(), self.ReadFloat()))
def ReadVector4(self) -> Vector:
"""Reads 4 32-bit floats from the buffer and returns them as a Vector"""
return Vector((self.ReadFloat(), self.ReadFloat(), self.ReadFloat(), self.ReadFloat()))
def ReadMatrix3x3(self) -> Matrix:
"""Reads 9 32-bit floats from the buffer and returns them as a Matrix"""
return Matrix([self.ReadVector3(), self.ReadVector3(), self.ReadVector3()])
def ReadMatrix4x4(self) -> Matrix:
"""Reads 16 32-bit floats from the buffer and returns them as a Matrix"""
return Matrix([self.ReadVector4(), self.ReadVector4(), self.ReadVector4(), self.ReadVector4()])
def ReadQuaternion(self) -> Quaternion:
"""Reads 4 32-bit floats from the buffer and returns them as a Quaternion"""
return Quaternion([self.ReadFloat(), self.ReadFloat(), self.ReadFloat(), self.ReadFloat()])
class FileWriter:
"""A class for writing binary data to a file"""
def __init__(self, FileName: str):
"""Initializes the FileWriter class"""
self.Buffer = open(FileName, "wb")
def WriteInt(self, Value: Union[int, List[int]]):
"""Writes a 32-bit integer to the buffer"""
if isinstance(Value, list):
self.Buffer.write(Pack("i"*len(Value), *Value))
else:
self.Buffer.write(Pack("i", Value))
def WriteUInt(self, Value: Union[int, List[int]]):
"""Writes a 32-bit unsigned integer to the buffer"""
if isinstance(Value, list):
self.Buffer.write(Pack("I"*len(Value), *Value))
else:
self.Buffer.write(Pack("I", Value))
def WriteFloat(self, Value: Union[float, List[float]]):
"""Writes a 32-bit float to the buffer"""
if isinstance(Value, list):
self.Buffer.write(Pack("f"*len(Value), *Value))
else:
self.Buffer.write(Pack("f", Value))
def WriteShort(self, Value: Union[int, List[int]]):
"""Writes a 16-bit integer to the buffer"""
if isinstance(Value, list):
self.Buffer.write(Pack("h"*len(Value), *Value))
else:
self.Buffer.write(Pack("h", Value))
def WriteUShort(self, Value: Union[int, List[int]]):
"""Writes a 16-bit unsigned integer to the buffer"""
if isinstance(Value, list):
self.Buffer.write(Pack("H"*len(Value), *Value))
else:
self.Buffer.write(Pack("H", Value))
def WriteString(self, Value: str):
"""Writes a string to the buffer"""
self.Buffer.write(Pack(str(len(Value)) + "s", Value.encode("UTF-8")))
def WriteByte(self, Value: Union[int, List[int]]):
"""Writes a byte to the buffer"""
if isinstance(Value, list):
self.Buffer.write(Pack("B"*len(Value), *Value))
else:
self.Buffer.write(Pack("B", Value))
def WriteVector2(self, Value: Vector):
"""Writes a Vector to the buffer"""
self.WriteFloat(Value[0])
self.WriteFloat(Value[1])
def WriteVector3(self, Value: Vector):
"""Writes a Vector to the buffer"""
self.WriteFloat(Value[0])
self.WriteFloat(Value[1])
self.WriteFloat(Value[2])
def WriteVector4(self, Value: Vector):
"""Writes a Vector to the buffer"""
self.WriteFloat(Value[0])
self.WriteFloat(Value[1])
self.WriteFloat(Value[2])
self.WriteFloat(Value[3])
def WriteMatrix3x3(self, Value: Matrix):
"""Writes a Matrix to the buffer"""
self.WriteVector3(Value[0])
self.WriteVector3(Value[1])
self.WriteVector3(Value[2])
def WriteMatrix4x4(self, Value: Matrix):
"""Writes a Matrix to the buffer"""
self.WriteVector4(Value[0])
self.WriteVector4(Value[1])
self.WriteVector4(Value[2])
self.WriteVector4(Value[3])
def WriteQuaternion(self, Value: Quaternion):
"""Writes a Quaternion to the buffer"""
self.WriteFloat(Value[0])
self.WriteFloat(Value[1])
self.WriteFloat(Value[2])
self.WriteFloat(Value[3])
def Close(self):
"""Closes the buffer"""
self.Buffer.close()